TripalEntityController.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * TripalEntityController extends DrupalDefaultEntityController.
  4. *
  5. * Our subclass of DrupalDefaultEntityController lets us add a few
  6. * important create, update, and delete methods.
  7. */
  8. class TripalEntityController extends EntityAPIController {
  9. public function __construct($entityType) {
  10. parent::__construct($entityType);
  11. }
  12. /**
  13. * Create a Tripal data entity
  14. *
  15. * We first set up the values that are specific
  16. * to our data schema but then also go through the EntityAPIController
  17. * function.
  18. *
  19. * @param $type
  20. * The machine-readable type of the entity.
  21. *
  22. * @return
  23. * An object with all default fields initialized.
  24. */
  25. public function create(array $values = array()) {
  26. // Add some items to the values array passed to the constructor
  27. global $user;
  28. $values['uid'] = $user->uid;
  29. $values['created'] = time();
  30. $values['changed'] = time();
  31. $values['title'] = '';
  32. // The incoming values should have at a minimum the bundle_id;
  33. $bundle = $values['bundle'];
  34. $matches = array();
  35. if (preg_match('/dbxref_(.*)/', $bundle, $matches)) {
  36. $dbxref_id = $matches[1];
  37. $values['type'] = 'BioData';
  38. // Get the CVterm.
  39. $match = array('dbxref_id' => $dbxref_id);
  40. $cvterm = chado_generate_var('cvterm', $match);
  41. if ($cvterm) {
  42. $values['cvterm_id'] = $cvterm->cvterm_id;
  43. }
  44. }
  45. return parent::create($values);
  46. }
  47. /**
  48. * Delete a single entity.
  49. *
  50. * Really a convenience function for deleteMultiple().
  51. */
  52. public function delete($entity) {
  53. $transaction = db_transaction();
  54. try {
  55. // Invoke hook_entity_delete().
  56. module_invoke_all('entity_delete', $entity, $entity->type);
  57. field_attach_delete('BioData', $entity);
  58. db_delete('tripal_entity')
  59. ->condition('id', $entity->id)
  60. ->execute();
  61. }
  62. catch (Exception $e) {
  63. $transaction->rollback();
  64. watchdog_exception('tripal_entities', $e);
  65. throw $e;
  66. return FALSE;
  67. }
  68. return TRUE;
  69. }
  70. /**
  71. * Sets the title for an entity.
  72. *
  73. * @param $entity
  74. * @param $title
  75. */
  76. public function setTitle($entity, $title) {
  77. db_update('tripal_entity')
  78. ->fields(array(
  79. 'title' => $title
  80. ))
  81. ->condition('id', $entity->id)
  82. ->execute();
  83. }
  84. /**
  85. * Saves the custom fields using drupal_write_record().
  86. */
  87. public function save($entity) {
  88. global $user;
  89. $pkeys = array();
  90. $transaction = db_transaction();
  91. try {
  92. // If our entity has no id, then we need to give it a
  93. // time of creation.
  94. if (empty($entity->id)) {
  95. $entity->created = time();
  96. $invocation = 'entity_insert';
  97. }
  98. else {
  99. $invocation = 'entity_update';
  100. $pkeys = array('id');
  101. }
  102. // Invoke hook_entity_presave().
  103. module_invoke_all('entity_presave', $entity, $entity->type);
  104. // Write out the entity record.
  105. $record = array(
  106. 'cvterm_id' => $entity->cvterm_id,
  107. 'type' => $entity->type,
  108. 'bundle' => $entity->bundle,
  109. 'title' => $entity->title,
  110. 'uid' => $user->uid,
  111. 'created' => $entity->created,
  112. 'changed' => time(),
  113. );
  114. if ($invocation == 'entity_update') {
  115. $record['id'] = $entity->id;
  116. }
  117. $success = drupal_write_record('tripal_entity', $record, $pkeys);
  118. if ($success == SAVED_NEW) {
  119. $entity->id = $record['id'];
  120. }
  121. // Now we need to either insert or update the fields which are
  122. // attached to this entity. We use the same primary_keys logic
  123. // to determine whether to update or insert, and which hook we
  124. // need to invoke.
  125. if ($invocation == 'entity_insert') {
  126. field_attach_insert('BioData', $entity);
  127. }
  128. else {
  129. field_attach_update('BioData', $entity);
  130. }
  131. // Invoke either hook_entity_update() or hook_entity_insert().
  132. module_invoke_all('entity_postsave', $entity, $entity->type);
  133. module_invoke_all($invocation, $entity, $entity->type);
  134. return $entity;
  135. }
  136. catch (Exception $e) {
  137. $transaction->rollback();
  138. watchdog_exception('tripal_core', $e);
  139. drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
  140. return FALSE;
  141. }
  142. }
  143. }