TripalVocabController.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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($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, $entity->type);
  25. field_attach_delete('TripalVocab', $entity);
  26. db_delete('tripal_term')
  27. ->condition('term_id', $entity->term_id)
  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, $entity->type);
  58. // Write out the entity record.
  59. $record = array(
  60. 'namespace' => $entity->namespace,
  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_vocab', $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('TripalVocab', $entity);
  77. }
  78. else {
  79. field_attach_update('TripalVocab', $entity);
  80. }
  81. // Invoke either hook_entity_update() or hook_entity_insert().
  82. module_invoke_all('entity_postsave', $entity, $entity->type);
  83. module_invoke_all($invocation, $entity, $entity->type);
  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. }