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