TripalEntityController.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 - we first set up the values that are specific
  14. * to our data schema but then also go through the EntityAPIController
  15. * function.
  16. *
  17. * @param $type
  18. * The machine-readable type of the entity.
  19. *
  20. * @return
  21. * An object with all default fields initialized.
  22. */
  23. public function create(array $values = array()) {
  24. // Add values that are specific to our entity
  25. $values += array(
  26. 'id' => '',
  27. 'bundle' => '',
  28. 'title' => '',
  29. 'created' => '',
  30. 'changed' => '',
  31. );
  32. return parent::create($values);
  33. }
  34. /**
  35. * Delete a single entity.
  36. *
  37. * Really a convenience function for deleteMultiple().
  38. */
  39. public function delete($entity) {
  40. $this->deleteMultiple(array($entity));
  41. }
  42. /**
  43. * Delete one or more tripal_entities entities.
  44. *
  45. * Deletion is unfortunately not supported in the base
  46. * DrupalDefaultEntityController class.
  47. *
  48. * @param array $entities
  49. * An array of entity IDs or a single numeric ID.
  50. */
  51. public function deleteMultiple($entities) {
  52. $ids = array();
  53. if (!empty($entities)) {
  54. $transaction = db_transaction();
  55. try {
  56. foreach ($entities as $entity) {
  57. // Invoke hook_entity_delete().
  58. module_invoke_all('entity_delete', $entity, $entity->type);
  59. field_attach_delete($entity->type, $entity);
  60. $ids[] = $entity->id;
  61. }
  62. db_delete('tripal_entity')
  63. ->condition('id', $ids, 'IN')
  64. ->execute();
  65. }
  66. catch (Exception $e) {
  67. $transaction->rollback();
  68. watchdog_exception('entity_example', $e);
  69. throw $e;
  70. }
  71. }
  72. }
  73. /**
  74. * Sets the title for an entity.
  75. *
  76. * @param $entity
  77. * @param $title
  78. */
  79. public function setTitle($entity, $title) {
  80. db_update('tripal_entity')
  81. ->fields(array(
  82. 'title' => $title
  83. ))
  84. ->condition('id', $entity->id)
  85. ->execute();
  86. }
  87. /**
  88. * Saves the custom fields using drupal_write_record().
  89. */
  90. public function save($entity) {
  91. global $user;
  92. $pkeys = array();
  93. // If our entity has no id, then we need to give it a
  94. // time of creation.
  95. if (empty($entity->id)) {
  96. $entity->created = time();
  97. $invocation = 'entity_insert';
  98. }
  99. else {
  100. $invocation = 'entity_update';
  101. $pkeys = array('id');
  102. }
  103. // Invoke hook_entity_presave().
  104. module_invoke_all('entity_presave', $entity, $entity->type);
  105. // Write out the entity record.
  106. $record = array(
  107. 'cvterm_id' => $entity->cvterm_id,
  108. 'type' => $entity->type,
  109. 'bundle' => $entity->bundle,
  110. 'title' => $entity->title,
  111. 'uid' => $user->uid,
  112. 'created' => $entity->created,
  113. 'changed' => time(),
  114. );
  115. if ($invocation == 'entity_update') {
  116. $record['id'] = $entity->id;
  117. }
  118. $success = drupal_write_record('tripal_entity', $record, $pkeys);
  119. if ($success == SAVED_NEW) {
  120. $entity->id = $record['id'];
  121. }
  122. // Now we need to either insert or update the fields which are
  123. // attached to this entity. We use the same primary_keys logic
  124. // to determine whether to update or insert, and which hook we
  125. // need to invoke.
  126. if ($invocation == 'entity_insert') {
  127. field_attach_insert($entity->type, $entity);
  128. }
  129. else {
  130. field_attach_update($entity->type, $entity);
  131. }
  132. // Invoke either hook_entity_update() or hook_entity_insert().
  133. module_invoke_all('entity_postsave', $entity, $entity->type);
  134. module_invoke_all($invocation, $entity, $entity->type);
  135. return $entity;
  136. }
  137. }