TripalEntityController.inc 3.4 KB

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