TripalDataController.inc 3.6 KB

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