TripalTermController.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * TripalTermController extends DrupalDefaultEntityController.
  4. *
  5. * Our subclass of DrupalDefaultEntityController lets us add a few
  6. * important create, update, and delete methods.
  7. */
  8. class TripalTermController extends EntityAPIController {
  9. public function __construct($entityType) {
  10. parent::__construct($entityType);
  11. }
  12. public function create(array $values = []) {
  13. return parent::create($values);
  14. }
  15. /**
  16. * Delete a single entity.
  17. */
  18. public function delete($ids, DatabaseTransaction $transaction = NULL) {
  19. $entities = $ids ? $this->load($ids) : FALSE;
  20. if (!$entities) {
  21. // Do nothing, in case invalid or no ids have been passed.
  22. return;
  23. }
  24. $transaction = isset($transaction) ? $transaction : db_transaction();
  25. try {
  26. // Invoke hook_entity_delete().
  27. $ids = array_keys($entities);
  28. foreach ($entities as $id => $entity) {
  29. module_invoke_all('entity_delete', $entity, 'TripalTerm');
  30. field_attach_delete('TripalTerm', $entity);
  31. db_delete('tripal_term')
  32. ->condition('accession', $entity->accession)
  33. ->execute();
  34. }
  35. } catch (Exception $e) {
  36. $transaction->rollback();
  37. watchdog_exception('tripal', $e);
  38. throw $e;
  39. return FALSE;
  40. }
  41. return TRUE;
  42. }
  43. /**
  44. * Saves the custom fields using drupal_write_record().
  45. */
  46. public function save($entity, DatabaseTransaction $transaction = NULL) {
  47. global $user;
  48. $pkeys = [];
  49. $transaction = isset($transaction) ? $transaction : db_transaction();
  50. try {
  51. // If our entity has no id, then we need to give it a
  52. // time of creation.
  53. if (empty($entity->id)) {
  54. $entity->created = time();
  55. $invocation = 'entity_insert';
  56. }
  57. else {
  58. $invocation = 'entity_update';
  59. $pkeys = ['id'];
  60. }
  61. // Invoke hook_entity_presave().
  62. module_invoke_all('entity_presave', $entity, 'TripalTerm');
  63. // Write out the entity record.
  64. $record = [
  65. 'vocab_id' => $entity->vocab_id,
  66. 'accession' => $entity->accession,
  67. 'name' => $entity->name,
  68. 'created' => $entity->created,
  69. 'changed' => time(),
  70. ];
  71. if ($invocation == 'entity_update') {
  72. $record['id'] = $entity->id;
  73. }
  74. $success = drupal_write_record('tripal_term', $record, $pkeys);
  75. if ($success == SAVED_NEW) {
  76. $entity->id = $record['id'];
  77. }
  78. // Now we need to either insert or update the fields which are
  79. // attached to this entity. We use the same primary_keys logic
  80. // to determine whether to update or insert, and which hook we
  81. // need to invoke.
  82. if ($invocation == 'entity_insert') {
  83. field_attach_insert('TripalTerm', $entity);
  84. }
  85. else {
  86. field_attach_update('TripalTerm', $entity);
  87. }
  88. // Invoke either hook_entity_update() or hook_entity_insert().
  89. module_invoke_all('entity_postsave', $entity, 'TripalTerm');
  90. module_invoke_all($invocation, $entity, 'TripalTerm');
  91. return $entity;
  92. } catch (Exception $e) {
  93. $transaction->rollback();
  94. watchdog_exception('tripal_entity', $e);
  95. drupal_set_message("Could not save the TripalTerm:" . $e->getMessage(), "error");
  96. return FALSE;
  97. }
  98. }
  99. }