tripal_entities.module 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  3. /**
  4. * Implement hook_entity_info().
  5. */
  6. function tripal_entities_entity_info() {
  7. $entities = array();
  8. $entities['cvterm'] = array(
  9. 'label' => t('Vocabulary Term'),
  10. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  11. 'plural label' => t('Vocabulary Terms'),
  12. 'entity class' => 'TrpVocabularyTerm',
  13. 'controller class' => 'TrpVocabularyTermController',
  14. 'fieldable' => TRUE,
  15. 'entity keys' => array(
  16. 'id' => 'internal_id'
  17. ),
  18. // Bundles are defined by the model types below
  19. 'bundles' => array(),
  20. );
  21. // We want to dynamically add the bundles (or term types) to the entity.
  22. $values = array(
  23. 'cv_id' => array(
  24. 'name' => 'sequence'
  25. ),
  26. 'name' => 'gene'
  27. );
  28. $cvterm = chado_generate_var('cvterm', $values);
  29. $bundle_id = 'trp_' . $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  30. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  31. $entities['trp_vocabulary_term']['bundles'][$bundle_id] = array(
  32. 'label' => $label,
  33. );
  34. return $entities;
  35. }
  36. function tripal_entities_load($pid = NULL, $reset = FALSE){
  37. $pids = (isset ($pid) ? array($pid) : array());
  38. $cvterm = trp_vocabulary_term_load_multiple($pids, $reset);
  39. return $cvterm ? reset ($cvterm) : FALSE;
  40. }
  41. function tripal_entities_load_multiple($pids = array(), $conditions = array(), $reset = FALSE){
  42. return entity_load('cvterm', $pids, $conditions, $reset);
  43. }
  44. function tripal_entities_menu() {
  45. $items['admin/structure/cvterm/manage'] = array(
  46. 'title' => 'Term Admin',
  47. 'description' => 'Manage cvterm structure',
  48. 'page callback' => 'cvterm_info',
  49. 'access arguments' => array('administer cvterms'),
  50. );
  51. $items['cvterm/%cvterm'] = array(
  52. 'title callback' => 'cvterm_page_title',
  53. 'title arguments' => array(1),
  54. 'page callback' => 'cvterm_page_view',
  55. 'page arguments' => array(1),
  56. 'access arguments' => array('view cvterms'),
  57. 'type' => MENU_CALLBACK,
  58. );
  59. $items['data/gene/add'] = array(
  60. 'title' => 'Add a gene',
  61. 'page callback' => 'cvterm_add',
  62. 'access arguments' => array('create cvterm'),
  63. );
  64. return $items;
  65. }
  66. function tripal_entities_permission(){
  67. return array(
  68. 'administer cvterms' => array(
  69. 'title' => t('Administer cvterms'),
  70. 'restrict access' => TRUE,
  71. ),
  72. 'view postsits' => array(
  73. 'title' => t('View cvterms'),
  74. )
  75. );
  76. }
  77. function cvterm_info() {
  78. return ('Welcome to the administration page for your CV Terms!');
  79. }
  80. function cvterm_page_title($cvterm){
  81. return $cvterm->pid;
  82. }
  83. function cvterm_page_view($cvterm, $view_mode = 'full'){
  84. $cvterm->content = array();
  85. // Build fields content.
  86. field_attach_prepare_view('cvterm', array($cvterm->pid => $cvterm), $view_mode);
  87. entity_prepare_view('cvterm', array($cvterm->pid => $cvterm));
  88. $cvterm->content += field_attach_view('cvterm', $cvterm, $view_mode);
  89. return $cvterm->content;
  90. }
  91. function tripal_entities_field_extra_fields() {
  92. $return = array();
  93. $return['cvterm']['cvterm'] = array(
  94. 'form' => array(
  95. 'note' => array(
  96. 'label' => t('Note'),
  97. 'description' => t('Term Note'),
  98. ),
  99. ),
  100. );
  101. return $return;
  102. }
  103. function tripal_entities_vocbulary_term_uri($cvterm) {
  104. return array(
  105. 'path' => 'cvterm/' . $cvterm->id,
  106. );
  107. }
  108. function cvterm_add() {
  109. $cvterm = (object) array (
  110. 'pid' => '',
  111. 'type' => 'cvterm',
  112. 'note' => '',
  113. );
  114. return drupal_get_form('cvterm_add_form', $cvterm);
  115. }
  116. function cvterm_add_form($form, &$form_state, $cvterm) {
  117. $form['note'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Note'),
  120. '#required' => TRUE,
  121. );
  122. $form['submit'] = array(
  123. '#type' => 'submit',
  124. '#value' => t('Save'),
  125. );
  126. field_attach_form('cvterm', $cvterm, $form, $form_state);
  127. return $form;
  128. }
  129. function cvterm_add_form_validate($form, &$form_state) {
  130. $cvterm_submission = (object) $form_state['values'];
  131. field_attach_form_validate('cvterm', $cvterm_submission, $form, $form_state);
  132. }
  133. function cvterm_add_form_submit($form, &$form_state) {
  134. $cvterm_submission = (object) $form_state['values'];
  135. field_attach_submit('cvterm', $cvterm_submission, $form, $form_state);
  136. $cvterm = cvterm_save($cvterm_submission);
  137. $form_state['redirect'] = "cvterm/$cvterm->pid";
  138. }
  139. function cvterm_save(&$cvterm) {
  140. return entity_get_controller('cvterm')->save($cvterm);
  141. }
  142. /**
  143. *
  144. *
  145. */
  146. class TrpVocabularyTermController extends DrupalDefaultEntityController {
  147. public function save($cvterm) {
  148. // drupal_write_record('cvterm', $cvterm);
  149. // field_attach_insert('cvterm', $cvterm);
  150. // module_invoke_all('entity_insert', 'cvterm', $cvterm);
  151. return $cvterm;
  152. }
  153. }