TripalEntityController.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_id) {
  57. // Invoke hook_entity_delete().
  58. $entity = entity_load($entity_id);
  59. module_invoke_all('entity_delete', $entity, $entity->type);
  60. field_attach_delete($entity->type, $entity);
  61. $ids[] = $entity->id;
  62. }
  63. db_delete('tripal_entity')
  64. ->condition('id', $ids, 'IN')
  65. ->execute();
  66. }
  67. catch (Exception $e) {
  68. $transaction->rollback();
  69. watchdog_exception('entity_example', $e);
  70. throw $e;
  71. }
  72. }
  73. }
  74. /**
  75. * Sets the title for an entity.
  76. *
  77. * @param $entity
  78. * @param $title
  79. */
  80. public function setTitle($entity, $title) {
  81. db_update('tripal_entity')
  82. ->fields(array(
  83. 'title' => $title
  84. ))
  85. ->condition('id', $entity->id)
  86. ->execute();
  87. }
  88. /**
  89. * Saves the custom fields using drupal_write_record().
  90. */
  91. public function save($entity) {
  92. global $user;
  93. $pkeys = array();
  94. // If our entity has no id, then we need to give it a
  95. // time of creation.
  96. if (empty($entity->id)) {
  97. $entity->created = time();
  98. $invocation = 'entity_insert';
  99. }
  100. else {
  101. $invocation = 'entity_update';
  102. $pkeys = array('id');
  103. }
  104. // Invoke hook_entity_presave().
  105. module_invoke_all('entity_presave', $entity, $entity->type);
  106. // Write out the entity record.
  107. $record = array(
  108. 'cvterm_id' => $entity->cvterm_id,
  109. 'type' => $entity->type,
  110. 'bundle' => $entity->bundle,
  111. 'title' => $entity->title,
  112. 'uid' => $user->uid,
  113. 'created' => $entity->created,
  114. 'changed' => time(),
  115. );
  116. if ($invocation == 'entity_update') {
  117. $record['id'] = $entity->id;
  118. }
  119. $success = drupal_write_record('tripal_entity', $record, $pkeys);
  120. if ($success == SAVED_NEW) {
  121. $entity->id = $record['id'];
  122. }
  123. // Now we need to either insert or update the fields which are
  124. // attached to this entity. We use the same primary_keys logic
  125. // to determine whether to update or insert, and which hook we
  126. // need to invoke.
  127. if ($invocation == 'entity_insert') {
  128. field_attach_insert($entity->type, $entity);
  129. }
  130. else {
  131. field_attach_update($entity->type, $entity);
  132. }
  133. // Invoke either hook_entity_update() or hook_entity_insert().
  134. module_invoke_all('entity_postsave', $entity, $entity->type);
  135. module_invoke_all($invocation, $entity, $entity->type);
  136. return $entity;
  137. }
  138. }