TripalTermController.inc 2.9 KB

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