tripal_entities.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. require_once "api/tripal_entities.api.inc";
  3. require_once "includes/tripal_entities.field_storage.inc";
  4. require_once "includes/tripal_entities.fields.inc";
  5. require_once "includes/tripal_entities.tables.inc";
  6. require_once "includes/TripalData.inc";
  7. require_once "includes/TripalDataController.inc";
  8. require_once "includes/TripalDataUIController.inc";
  9. require_once "includes/TripalDataType.inc";
  10. require_once "includes/TripalDataTypeController.inc";
  11. require_once "includes/TripalDataTypeUIController.inc";
  12. /**
  13. * Implements hook_views_api().
  14. */
  15. function tripal_entities_views_api() {
  16. return array(
  17. 'api' => 3,
  18. );
  19. }
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function tripal_entities_menu() {
  24. $items = array();
  25. // the administative settings menu
  26. $items['admin/tripal/data_types'] = array(
  27. 'title' => 'Biological Data',
  28. 'description' => 'Tools for publishing, configurating and managing biological data.',
  29. 'page callback' => 'tripal_entities_admin_view',
  30. 'access arguments' => array('administer tripal data types'),
  31. 'file' => 'includes/tripal_entities.admin.inc',
  32. 'type' => MENU_NORMAL_ITEM,
  33. );
  34. // the default tab
  35. $items['admin/tripal/data_types/default'] = array(
  36. 'title' => 'Biological Data',
  37. 'type' => MENU_DEFAULT_LOCAL_TASK,
  38. 'weight' => 1,
  39. );
  40. $items['admin/tripal/data_types/publish'] = array(
  41. 'title' => 'Publish',
  42. 'description' => 'Publish Data',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('tripal_entities_admin_publish_form'),
  45. 'access arguments' => array('administer tripal data types'),
  46. 'file' => 'includes/tripal_entities.admin.inc',
  47. 'type' => MENU_LOCAL_TASK,
  48. 'weight' => 2
  49. );
  50. $items['admin/tripal/data_types/access'] = array(
  51. 'title' => 'Access',
  52. 'description' => 'Set default access permissions for collections of data.',
  53. 'page callback' => 'drupal_get_form',
  54. 'page arguments' => array('tripal_entities_admin_access_form'),
  55. 'access arguments' => array('administer tripal data types'),
  56. 'file' => 'includes/tripal_entities.admin.inc',
  57. 'type' => MENU_LOCAL_TASK,
  58. 'weight' => 3
  59. );
  60. return $items;
  61. }
  62. /**
  63. * Implements hook_permission().
  64. */
  65. function tripal_entities_permission() {
  66. // We set up permisssions to manage entity types, manage all entities and the
  67. // permissions for each individual entity
  68. $permissions = array(
  69. 'administer tripal data types' => array(
  70. 'title' => t('Administer Tripal data types'),
  71. 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
  72. ),
  73. 'administer tripal data' => array(
  74. 'title' => t('Administer Tripal data'),
  75. 'description' => t('Edit and delete all tripal data'),
  76. ),
  77. );
  78. // Generate permissions per each data type.
  79. foreach (tripal_data_get_types() as $type) {
  80. $type_name = check_plain($type->type);
  81. $permissions += array(
  82. "edit any $type_name data" => array(
  83. 'title' => t('%type_name: Edit any', array('%type_name' => $type->label)),
  84. ),
  85. "view any $type_name data" => array(
  86. 'title' => t('%type_name: View any', array('%type_name' => $type->label)),
  87. ),
  88. );
  89. }
  90. return $permissions;
  91. }
  92. /**
  93. * Implements hook_theme().
  94. */
  95. function tripal_entities_theme($existing, $type, $theme, $path) {
  96. return array(
  97. 'tripal_data' => array(
  98. 'render element' => 'elements',
  99. 'template' => 'tripal_data',
  100. 'path' => "$path/theme/templates"
  101. ),
  102. );
  103. }
  104. /**
  105. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  106. */
  107. function tripal_entities_rdf_mapping() {
  108. return array();
  109. /* return array(
  110. 'type' => 'tripal_data',
  111. 'bundle' => 'gene',
  112. 'mapping' => array(
  113. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  114. 'title' => array(
  115. 'predicates' => array('dc:title'),
  116. ),
  117. 'uid' => array(
  118. 'predicates' => array('sioc:has_creator'),
  119. 'type' => 'rel',
  120. ),
  121. 'name' => array(
  122. 'predicates' => array('foaf:name'),
  123. ),
  124. 'uniquename' => array(
  125. 'predicates' => array('foaf:name'),
  126. ),
  127. 'organism_id' => array(
  128. 'predicates' => array('sioc:has_parent'),
  129. 'type' => 'rel'
  130. )
  131. ),
  132. ); */
  133. }
  134. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  135. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  136. /**
  137. * Implement hook_entity_info().
  138. */
  139. function tripal_entities_entity_info() {
  140. $entities = array();
  141. // Get a list of published vocabularies from 'tripal_vocabulary
  142. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  143. foreach ($published_vocs as $voc) {
  144. $entities [$voc->db_id->name] = array (
  145. // A human readable label to identify our entity.
  146. 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
  147. 'plural label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
  148. // The entity class and controller class extend the classes provided by the
  149. // Entity API.
  150. 'entity class' => 'TripalData',
  151. 'controller class' => 'TripalDataController',
  152. // The table for this entity defined in hook_schema()
  153. 'base table' => 'tripal_data',
  154. // Returns the uri elements of an entity.
  155. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  156. // IF fieldable == FALSE, we can't attach fields.
  157. 'fieldable' => TRUE,
  158. // entity_keys tells the controller what database fields are used for key
  159. // functions. It is not required if we don't have bundles or revisions.
  160. // Here we do not support a revision, so that entity key is omitted.
  161. 'entity keys' => array (
  162. 'id' => 'id',
  163. 'bundle' => 'type'
  164. ),
  165. 'bundle keys' => array (
  166. 'bundle' => 'type'
  167. ),
  168. // Callback function for access to this entity.
  169. 'access callback' => 'tripal_data_access',
  170. // FALSE disables caching. Caching functionality is handled by Drupal core.
  171. 'static cache' => FALSE,
  172. // Bundles are added in the hook_entities_info_alter() function.
  173. 'bundles' => array (),
  174. 'label callback' => 'tripal_data_label',
  175. 'creation callback' => 'tripal_data_create',
  176. // The information below is used by the TripalDataUIController
  177. // (which extends the EntityDefaultUIController). The admin_ui
  178. // key here is mean to appear on the 'Find Content' page of the
  179. // administrative menu.
  180. 'admin ui' => array (
  181. 'path' => 'admin/content/data',
  182. 'controller class' => 'TripalDataUIController',
  183. 'menu wildcard' => '%tripal_data',
  184. 'file' => 'includes/TripalDataUIController.inc'
  185. ),
  186. 'view modes' => array (
  187. 'full' => array (
  188. 'label' => t ( 'Full content' ),
  189. 'custom settings' => FALSE
  190. ),
  191. 'teaser' => array (
  192. 'label' => t ( 'Teaser' ),
  193. 'custom settings' => TRUE
  194. )
  195. )
  196. );
  197. // The entity that holds information about the entity types
  198. $entities [$voc->db_id->name . '_type'] = array (
  199. 'label' => t ($voc->db_id->name . ' (' . $voc->cv_id->name . ')' . ' Type' ),
  200. 'entity class' => 'TripalDataType',
  201. 'controller class' => 'TripalDataTypeController',
  202. 'base table' => 'tripal_data_type',
  203. 'fieldable' => FALSE,
  204. // If this entity can be used as a bundle of another entity then
  205. // that can be specified via the 'bundle of' key.
  206. 'bundle of' => $voc->db_id->name,
  207. 'exportable' => TRUE,
  208. 'entity keys' => array (
  209. 'id' => 'id',
  210. 'name' => 'type',
  211. 'label' => 'label'
  212. ),
  213. 'access callback' => 'tripal_data_type_access',
  214. 'module' => 'tripal_entities',
  215. // Enable the entity API's admin UI.
  216. 'admin ui' => array (
  217. 'path' => 'admin/structure/data_types',
  218. 'controller class' => 'TripalDataTypeUIController',
  219. 'file' => 'includes/TripalDataTypeUIController.inc'
  220. )
  221. );
  222. }
  223. return $entities;
  224. }
  225. /**
  226. * Implements hook_entity_info_alter().
  227. *
  228. * We are adding the info about the tripal data types via a hook to avoid a
  229. * recursion issue as loading the model types requires the entity info as well.
  230. *
  231. */
  232. function tripal_entities_entity_info_alter(&$entity_info) {
  233. // Get a list of published terms from 'tripal_term
  234. $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  235. foreach ( $published_terms as $term ) {
  236. // Bundles are alternative groups of fields or configuration
  237. // associated with a base entity type.
  238. // We want to dynamically add the bundles (or term types) to the entity.
  239. $cvterm = $term->cvterm_id;
  240. $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  241. $label = preg_replace ( '/_/', ' ', ucwords ( $cvterm->name ) );
  242. $entity_info [$cvterm->dbxref_id->db_id->name] ['bundles'] [$bundle_id] = array (
  243. 'label' => $label,
  244. 'admin' => array (
  245. 'path' => 'admin/structure/data_types/manage/%tripal_data_type',
  246. 'real path' => 'admin/structure/data_types/manage/' . $bundle_id,
  247. 'bundle argument' => 4,
  248. 'access arguments' => array (
  249. 'administer tripal data types'
  250. )
  251. )
  252. );
  253. }
  254. }
  255. /**
  256. * Get published vocabularies as select options
  257. * @return multitype:NULL
  258. */
  259. function tripal_entities_get_published_vocabularies_as_select_options() {
  260. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  261. $options = array();
  262. foreach ($published_vocs as $voc) {
  263. $options [$voc->cv_id->cv_id] = $voc->cv_id->name;
  264. }
  265. return $options;
  266. }
  267. /**
  268. * Get published terms as select options
  269. * @return multitype:NULL
  270. */
  271. function tripal_entities_get_published_terms_as_select_options($cv_id = NULL) {
  272. $where = array('publish' => 1);
  273. $published_terms = chado_generate_var('tripal_term', $where, array('return_array' => 1));
  274. $options = array();
  275. foreach ($published_terms as $term) {
  276. if (!$cv_id) {
  277. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  278. } else {
  279. if ($term->cvterm_id->cv_id->cv_id == $cv_id) {
  280. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  281. }
  282. }
  283. }
  284. return $options;
  285. }