tripal_entities.module 11 KB

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