TripalEntityController.inc 3.8 KB

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