TripalVocabController.inc 3.3 KB

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