TripalEntityController.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * TripalEntityController extends DrupalDefaultEntityController.
  4. *
  5. * Our subclass of DrupalDefaultEntityController lets us add a few
  6. * important create, update, and delete methods.
  7. */
  8. class TripalEntityController extends EntityAPIController {
  9. public function __construct($entityType) {
  10. parent::__construct($entityType);
  11. }
  12. /**
  13. * Create a Tripal data entity
  14. *
  15. * We first set up the values that are specific
  16. * to our data schema but then also go through the EntityAPIController
  17. * function.
  18. *
  19. * @param $type
  20. * The machine-readable type of the entity.
  21. *
  22. * @return
  23. * An object with all default fields initialized.
  24. */
  25. public function create(array $values = array()) {
  26. // Add some items to the values array passed to the constructor
  27. global $user;
  28. $values['uid'] = $user->uid;
  29. $values['created'] = time();
  30. $values['changed'] = time();
  31. $values['title'] = '';
  32. // The incoming values should have at a minimum the bundle_id;
  33. $bundle = $values['bundle'];
  34. $matches = array();
  35. if (preg_match('/(.*)_(.*)/', $bundle, $matches)) {
  36. $type = $matches[1];
  37. $accession = $matches[2];
  38. $values['type'] = $type;
  39. // Get the CVterm.
  40. $match = array(
  41. 'dbxref_id' => array(
  42. 'accession' => $accession,
  43. 'db_id' => array(
  44. 'name' => $type,
  45. ),
  46. ),
  47. );
  48. $cvterm = chado_generate_var('cvterm', $match);
  49. if ($cvterm) {
  50. $values['cvterm_id'] = $cvterm->cvterm_id;
  51. }
  52. }
  53. return parent::create($values);
  54. }
  55. /**
  56. * Delete a single entity.
  57. *
  58. * Really a convenience function for deleteMultiple().
  59. */
  60. public function delete($entity) {
  61. $transaction = db_transaction();
  62. try {
  63. // Invoke hook_entity_delete().
  64. module_invoke_all('entity_delete', $entity, $entity->type);
  65. field_attach_delete($entity->type, $entity);
  66. db_delete('tripal_entity')
  67. ->condition('id', $entity->id)
  68. ->execute();
  69. }
  70. catch (Exception $e) {
  71. $transaction->rollback();
  72. watchdog_exception('tripal_entities', $e);
  73. throw $e;
  74. return FALSE;
  75. }
  76. return TRUE;
  77. }
  78. /**
  79. * Sets the title for an entity.
  80. *
  81. * @param $entity
  82. * @param $title
  83. */
  84. public function setTitle($entity, $title) {
  85. db_update('tripal_entity')
  86. ->fields(array(
  87. 'title' => $title
  88. ))
  89. ->condition('id', $entity->id)
  90. ->execute();
  91. }
  92. /**
  93. * Saves the custom fields using drupal_write_record().
  94. */
  95. public function save($entity) {
  96. global $user;
  97. $pkeys = array();
  98. $transaction = db_transaction();
  99. try {
  100. // If our entity has no id, then we need to give it a
  101. // time of creation.
  102. if (empty($entity->id)) {
  103. $entity->created = time();
  104. $invocation = 'entity_insert';
  105. }
  106. else {
  107. $invocation = 'entity_update';
  108. $pkeys = array('id');
  109. }
  110. // Invoke hook_entity_presave().
  111. module_invoke_all('entity_presave', $entity, $entity->type);
  112. // Write out the entity record.
  113. $record = array(
  114. 'cvterm_id' => $entity->cvterm_id,
  115. 'type' => $entity->type,
  116. 'bundle' => $entity->bundle,
  117. 'title' => $entity->title,
  118. 'uid' => $user->uid,
  119. 'created' => $entity->created,
  120. 'changed' => time(),
  121. );
  122. if ($invocation == 'entity_update') {
  123. $record['id'] = $entity->id;
  124. }
  125. $success = drupal_write_record('tripal_entity', $record, $pkeys);
  126. if ($success == SAVED_NEW) {
  127. $entity->id = $record['id'];
  128. }
  129. // Now we need to either insert or update the fields which are
  130. // attached to this entity. We use the same primary_keys logic
  131. // to determine whether to update or insert, and which hook we
  132. // need to invoke.
  133. if ($invocation == 'entity_insert') {
  134. field_attach_insert($entity->type, $entity);
  135. }
  136. else {
  137. field_attach_update($entity->type, $entity);
  138. }
  139. // Invoke either hook_entity_update() or hook_entity_insert().
  140. module_invoke_all('entity_postsave', $entity, $entity->type);
  141. module_invoke_all($invocation, $entity, $entity->type);
  142. return $entity;
  143. }
  144. catch (Exception $e) {
  145. $transaction->rollback();
  146. watchdog_exception('tripal_core', $e);
  147. drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
  148. }
  149. }
  150. }