ChadoDataController.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * ChadoDataController extends DrupalDefaultEntityController.
  4. *
  5. * Our subclass of DrupalDefaultEntityController lets us add a few
  6. * important create, update, and delete methods.
  7. */
  8. class ChadoDataController extends EntityAPIController {
  9. public function __construct($entityType) {
  10. parent::__construct($entityType);
  11. }
  12. /**
  13. * Create a Chado data entity - we first set up the values that are specific
  14. * to our data schema but then also go through the EntityAPIController
  15. * function.
  16. *
  17. * @param $type
  18. * The machine-readable type of the entity.
  19. *
  20. * @return
  21. * An object with all default fields initialized.
  22. */
  23. public function create(array $values = array()) {
  24. // Add values that are specific to our entity
  25. $values += array(
  26. 'entity_id' => '',
  27. 'title' => '',
  28. 'created' => '',
  29. 'changed' => '',
  30. );
  31. return parent::create($values);
  32. }
  33. /**
  34. * Overriding the buildContent function to add entity specific fields
  35. */
  36. public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
  37. $content = parent::buildContent($entity, $view_mode, $langcode, $content);
  38. return $content;
  39. }
  40. /**
  41. * Delete a single entity.
  42. *
  43. * Really a convenience function for deleteMultiple().
  44. */
  45. public function delete($entity) {
  46. $this->deleteMultiple(array($entity));
  47. }
  48. /**
  49. * Delete one or more tripal_entities entities.
  50. *
  51. * Deletion is unfortunately not supported in the base
  52. * DrupalDefaultEntityController class.
  53. *
  54. * @param array $entities
  55. * An array of entity IDs or a single numeric ID.
  56. */
  57. public function deleteMultiple($entities) {
  58. $entity_ids = array();
  59. if (!empty($entities)) {
  60. $transaction = db_transaction();
  61. try {
  62. foreach ($entities as $entity) {
  63. // Invoke hook_entity_delete().
  64. module_invoke_all('entity_delete', $entity, 'chado_data');
  65. field_attach_delete('chado_data', $entity);
  66. $entity_ids[] = $entity->entity_id;
  67. }
  68. db_delete('chado_data')
  69. ->condition('entity_id', $entity_ids, 'IN')
  70. ->execute();
  71. }
  72. catch (Exception $e) {
  73. $transaction->rollback();
  74. watchdog_exception('entity_example', $e);
  75. throw $e;
  76. }
  77. }
  78. }
  79. /**
  80. * Saves the custom fields using drupal_write_record().
  81. */
  82. public function save($entity) {
  83. global $user;
  84. // If our entity has no entity_id, then we need to give it a
  85. // time of creation.
  86. if (empty($entity->entity_id)) {
  87. $entity->created = time();
  88. $invocation = 'entity_insert';
  89. }
  90. else {
  91. $invocation = 'entity_update';
  92. }
  93. // Now we need to either insert or update the fields which are
  94. // attached to this entity. We use the same primary_keys logic
  95. // to determine whether to update or insert, and which hook we
  96. // need to invoke.
  97. if ($invocation == 'entity_insert') {
  98. field_attach_insert('chado_data', $entity);
  99. }
  100. else {
  101. field_attach_update('chado_data', $entity);
  102. }
  103. // Invoke hook_entity_presave().
  104. module_invoke_all('entity_presave', $entity, 'chado_data');
  105. // Write out the entity record.
  106. $tablename = 'feature';
  107. $type_field = 'type_id';
  108. $schema = chado_get_schema($tablename);
  109. $pkey_field = $schema['primary key'][0];
  110. $record = array(
  111. 'cvterm_id' => $entity->cvterm_id,
  112. 'type' => $entity->type,
  113. 'tablename' => $tablename,
  114. 'record_id' => $entity->record_id,
  115. 'title' => 'title',
  116. 'uid' => $user->uid,
  117. 'created' => $entity->created,
  118. 'changed' => time(),
  119. );
  120. $success = drupal_write_record('chado_data', $record);
  121. if ($success == SAVED_NEW) {
  122. $entity->entity_id = $record['entity_id'];
  123. }
  124. // Invoke either hook_entity_update() or hook_entity_insert().
  125. module_invoke_all($invocation, $entity, 'chado_data');
  126. return $entity;
  127. }
  128. }