TripalVocabController.inc 2.9 KB

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