TripalEntityController.inc 5.5 KB

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