TripalTermController.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = array()) {
  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. }
  36. catch (Exception $e) {
  37. $transaction->rollback();
  38. watchdog_exception('tripal', $e);
  39. throw $e;
  40. return FALSE;
  41. }
  42. return TRUE;
  43. }
  44. /**
  45. * Saves the custom fields using drupal_write_record().
  46. */
  47. public function save($entity, DatabaseTransaction $transaction = NULL) {
  48. global $user;
  49. $pkeys = array();
  50. $transaction = isset($transaction) ? $transaction : db_transaction();
  51. try {
  52. // If our entity has no id, then we need to give it a
  53. // time of creation.
  54. if (empty($entity->id)) {
  55. $entity->created = time();
  56. $invocation = 'entity_insert';
  57. }
  58. else {
  59. $invocation = 'entity_update';
  60. $pkeys = array('id');
  61. }
  62. // Invoke hook_entity_presave().
  63. module_invoke_all('entity_presave', $entity, 'TripalTerm');
  64. // Write out the entity record.
  65. $record = array(
  66. 'vocab_id' => $entity->vocab_id,
  67. 'accession' => $entity->accession,
  68. 'name' => $entity->name,
  69. 'created' => $entity->created,
  70. 'changed' => time(),
  71. );
  72. if ($invocation == 'entity_update') {
  73. $record['id'] = $entity->id;
  74. }
  75. $success = drupal_write_record('tripal_term', $record, $pkeys);
  76. if ($success == SAVED_NEW) {
  77. $entity->id = $record['id'];
  78. }
  79. // Now we need to either insert or update the fields which are
  80. // attached to this entity. We use the same primary_keys logic
  81. // to determine whether to update or insert, and which hook we
  82. // need to invoke.
  83. if ($invocation == 'entity_insert') {
  84. field_attach_insert('TripalTerm', $entity);
  85. }
  86. else {
  87. field_attach_update('TripalTerm', $entity);
  88. }
  89. // Invoke either hook_entity_update() or hook_entity_insert().
  90. module_invoke_all('entity_postsave', $entity, 'TripalTerm');
  91. module_invoke_all($invocation, $entity, 'TripalTerm');
  92. return $entity;
  93. }
  94. catch (Exception $e) {
  95. $transaction->rollback();
  96. watchdog_exception('tripal_entity', $e);
  97. drupal_set_message("Could not save the TripalTerm:" . $e->getMessage(), "error");
  98. return FALSE;
  99. }
  100. }
  101. }