TripalEntityController.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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('/bio-data_(.*)/', $bundle, $matches)) {
  36. $cvterm_id = $matches[1];
  37. $values['type'] = 'TripalEntity';
  38. // Get the CVterm.
  39. $match = array('cvterm_id' => $cvterm_id);
  40. $cvterm = tripal_get_cvterm($match);
  41. if ($cvterm) {
  42. $values['cvterm_id'] = $cvterm->cvterm_id;
  43. }
  44. }
  45. return parent::create($values);
  46. }
  47. /**
  48. * Delete a single entity.
  49. *
  50. * Really a convenience function for deleteMultiple().
  51. */
  52. public function delete($entity) {
  53. $transaction = db_transaction();
  54. try {
  55. // Invoke hook_entity_delete().
  56. module_invoke_all('entity_delete', $entity, $entity->type);
  57. field_attach_delete('TripalEntity', $entity);
  58. db_delete('tripal_entity')
  59. ->condition('id', $entity->id)
  60. ->execute();
  61. }
  62. catch (Exception $e) {
  63. $transaction->rollback();
  64. watchdog_exception('tripal_entities', $e);
  65. throw $e;
  66. return FALSE;
  67. }
  68. return TRUE;
  69. }
  70. /**
  71. * Sets the title for an entity.
  72. *
  73. * @param $entity
  74. * @param $title
  75. */
  76. public function setTitle($entity, $title = NULL) {
  77. // If no title was supplied then we should try to generate one using the
  78. // default format set by admins.
  79. if (!$title) {
  80. // First get the format for the title based on the bundle of the entity.
  81. $bundle_entity = tripal_bundle_load($entity->bundle);
  82. $title = tripal_get_title_format($bundle_entity);
  83. // Determine which tokens were used in the format string
  84. if (preg_match_all('/\[\w+\.\w+\]/', $title, $matches)) {
  85. $used_tokens = $matches[0];
  86. foreach($used_tokens as $token) {
  87. $field = str_replace(array('.','[',']'),array('__','',''),$token);
  88. $value = '';
  89. if (isset($entity->{$field})) {
  90. // Render the value from the field.
  91. $field_value = field_get_items('TripalEntity', $entity, $field);
  92. $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
  93. $value = render($field_render_arr);
  94. }
  95. $title = str_replace($token, $value, $title);
  96. }
  97. }
  98. }
  99. // As long as we were able to determine a title, we should update it ;-).
  100. if ($title) {
  101. db_update('tripal_entity')
  102. ->fields(array(
  103. 'title' => $title
  104. ))
  105. ->condition('id', $entity->id)
  106. ->execute();
  107. }
  108. }
  109. /**
  110. * Saves the custom fields using drupal_write_record().
  111. */
  112. public function save($entity) {
  113. global $user;
  114. $pkeys = array();
  115. $transaction = db_transaction();
  116. try {
  117. // If our entity has no id, then we need to give it a
  118. // time of creation.
  119. if (empty($entity->id)) {
  120. $entity->created = time();
  121. $invocation = 'entity_insert';
  122. }
  123. else {
  124. $invocation = 'entity_update';
  125. $pkeys = array('id');
  126. }
  127. // Invoke hook_entity_presave().
  128. module_invoke_all('entity_presave', $entity, $entity->type);
  129. // Write out the entity record.
  130. $record = array(
  131. 'cvterm_id' => $entity->cvterm_id,
  132. 'type' => $entity->type,
  133. 'bundle' => $entity->bundle,
  134. 'title' => $entity->title,
  135. 'uid' => $user->uid,
  136. 'created' => $entity->created,
  137. 'changed' => time(),
  138. );
  139. if ($invocation == 'entity_update') {
  140. $record['id'] = $entity->id;
  141. }
  142. $success = drupal_write_record('tripal_entity', $record, $pkeys);
  143. if ($success == SAVED_NEW) {
  144. $entity->id = $record['id'];
  145. }
  146. // Now we need to either insert or update the fields which are
  147. // attached to this entity. We use the same primary_keys logic
  148. // to determine whether to update or insert, and which hook we
  149. // need to invoke.
  150. if ($invocation == 'entity_insert') {
  151. field_attach_insert('TripalEntity', $entity);
  152. }
  153. else {
  154. field_attach_update('TripalEntity', $entity);
  155. }
  156. // Invoke either hook_entity_update() or hook_entity_insert().
  157. module_invoke_all('entity_postsave', $entity, $entity->type);
  158. module_invoke_all($invocation, $entity, $entity->type);
  159. return $entity;
  160. }
  161. catch (Exception $e) {
  162. $transaction->rollback();
  163. watchdog_exception('tripal_core', $e);
  164. drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
  165. return FALSE;
  166. }
  167. }
  168. }