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