TripalTermController.inc 2.9 KB

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