tripal_entities.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. require_once "api/tripal_entities.api.inc";
  3. require_once "includes/tripal_entities.chado_entity.inc";
  4. require_once "includes/tripal_entities.entity_form.inc";
  5. require_once "includes/tripal_entities.tables.inc";
  6. require_once "includes/TripalEntity.inc";
  7. require_once "includes/TripalEntityController.inc";
  8. require_once "includes/TripalEntityUIController.inc";
  9. require_once "includes/TripalBundle.inc";
  10. require_once "includes/TripalBundleController.inc";
  11. require_once "includes/TripalBundleUIController.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/BioData'] = 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/BioData/default'] = array(
  36. 'title' => 'Biological Data',
  37. 'type' => MENU_DEFAULT_LOCAL_TASK,
  38. 'weight' => 1,
  39. );
  40. $items['admin/tripal/BioData/access'] = array(
  41. 'title' => 'Access',
  42. 'description' => 'Set default access permissions for collections of data.',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('tripal_entities_admin_access_form'),
  45. 'access arguments' => array('administer tripal data types'),
  46. 'file' => 'includes/tripal_entities.admin.inc',
  47. 'type' => MENU_LOCAL_TASK,
  48. 'weight' => 3
  49. );
  50. return $items;
  51. }
  52. /**
  53. * Implements hook_admin_paths().
  54. */
  55. function tripal_entities_admin_paths() {
  56. if (variable_get('node_admin_theme')) {
  57. $paths = array(
  58. 'BioData/*/edit' => TRUE,
  59. 'BioData/*/delete' => TRUE,
  60. 'BioData/add' => TRUE,
  61. 'BioData/add/*' => TRUE,
  62. );
  63. return $paths;
  64. }
  65. }
  66. /**
  67. * Implements hook_permission().
  68. */
  69. function tripal_entities_permission() {
  70. // We set up permisssions to manage entity types, manage all entities and the
  71. // permissions for each individual entity
  72. $permissions = array(
  73. 'administer tripal data types' => array(
  74. 'title' => t('Administer Tripal data types'),
  75. 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
  76. ),
  77. 'administer tripal data' => array(
  78. 'title' => t('Administer Tripal data'),
  79. 'description' => t('Edit and delete all tripal data'),
  80. ),
  81. );
  82. return $permissions;
  83. }
  84. /**
  85. * Implements hook_theme().
  86. */
  87. function tripal_entities_theme($existing, $type, $theme, $path) {
  88. return array(
  89. 'tripal_entity' => array(
  90. 'render element' => 'elements',
  91. 'template' => 'tripal_entity',
  92. 'path' => "$path/theme/templates"
  93. ),
  94. 'tripal_entities_date_combo' => array(
  95. 'render element' => 'element',
  96. 'file' => 'includes/tripal_entities.chado_entity.inc',
  97. ),
  98. 'tripal_entities_add_list' => array(
  99. 'variables' => array('content' => NULL),
  100. 'file' => 'includes/tripal_entities.entity_form.inc',
  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_entity',
  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. $entities['BioData'] = array (
  142. // A human readable label to identify our entity.
  143. 'label' => 'Biological Data',
  144. 'plural label' => 'Biological Data',
  145. // The entity class and controller class extend the classes provided by the
  146. // Entity API.
  147. 'entity class' => 'TripalEntity',
  148. 'controller class' => 'TripalEntityController',
  149. // The table for this entity defined in hook_schema()
  150. 'base table' => 'tripal_entity',
  151. // Returns the uri elements of an entity.
  152. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  153. // IF fieldable == FALSE, we can't attach fields.
  154. 'fieldable' => TRUE,
  155. // entity_keys tells the controller what database fields are used for key
  156. // functions. It is not required if we don't have bundles or revisions.
  157. // Here we do not support a revision, so that entity key is omitted.
  158. 'entity keys' => array (
  159. 'id' => 'id',
  160. 'bundle' => 'bundle'
  161. ),
  162. 'bundle keys' => array (
  163. 'bundle' => 'bundle'
  164. ),
  165. // Callback function for access to this entity.
  166. 'access callback' => 'tripal_entity_access',
  167. // FALSE disables caching. Caching functionality is handled by Drupal core.
  168. 'static cache' => FALSE,
  169. // Bundles are added dynamically below.
  170. 'bundles' => array (),
  171. 'label callback' => 'tripal_entity_label',
  172. // The information below is used by the TripalEntityUIController
  173. // (which extends the EntityDefaultUIController). The admin_ui
  174. // key here is mean to appear on the 'Find Content' page of the
  175. // administrative menu.
  176. 'admin ui' => array (
  177. 'path' => 'admin/content/BioData',
  178. 'controller class' => 'TripalEntityUIController',
  179. 'menu wildcard' => '%tripal_entity',
  180. 'file' => 'includes/TripalEntityUIController.inc'
  181. ),
  182. 'view modes' => array (
  183. 'full' => array (
  184. 'label' => t ('Full content'),
  185. 'custom settings' => FALSE
  186. ),
  187. 'teaser' => array (
  188. 'label' => t ('Teaser'),
  189. 'custom settings' => TRUE
  190. )
  191. )
  192. );
  193. // The entity that holds information about the entity types.
  194. $entities['BioData_bundles'] = array (
  195. 'label' => 'Biological Data Type', //$voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
  196. 'entity class' => 'TripalBundle',
  197. 'controller class' => 'TripalBundleController',
  198. 'base table' => 'tripal_bundle',
  199. 'fieldable' => FALSE,
  200. 'bundle of' => 'BioData',
  201. 'exportable' => FALSE,
  202. 'entity keys' => array (
  203. 'id' => 'id',
  204. 'name' => 'bundle',
  205. 'label' => 'label'
  206. ),
  207. 'access callback' => 'tripal_bundle_access',
  208. 'module' => 'tripal_entities',
  209. // Enable the entity API's admin UI.
  210. 'admin ui' => array (
  211. 'path' => 'admin/structure/BioData',
  212. 'controller class' => 'TripalBundleUIController',
  213. 'file' => 'includes/TripalBundleUIController.inc',
  214. 'menu wildcard' => '%tripal_bundle',
  215. )
  216. );
  217. // Dynamically add in the bundles. Bundles are alternative groups of fields
  218. // or configuration associated with an entity type .We want to dynamically
  219. // add the bundles to the entity.
  220. $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  221. foreach ($published_terms as $term) {
  222. $cvterm = $term->cvterm_id;
  223. $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
  224. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  225. $entities['BioData']['bundles'][$bundle_id] = array (
  226. 'label' => $label,
  227. 'admin' => array (
  228. 'path' => 'admin/structure/BioData/manage/%tripal_bundle',
  229. 'real path' => 'admin/structure/BioData/manage/' . $bundle_id,
  230. 'bundle argument' => 4,
  231. 'access arguments' => array (
  232. 'administer tripal data types'
  233. )
  234. )
  235. );
  236. }
  237. return $entities;
  238. }
  239. /**
  240. * Get published vocabularies as select options
  241. * @return multitype:NULL
  242. */
  243. function tripal_entities_get_published_vocabularies_as_select_options() {
  244. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  245. $options = array();
  246. foreach ($published_vocs as $voc) {
  247. $options [$voc->cv_id->cv_id] = $voc->cv_id->name;
  248. }
  249. return $options;
  250. }
  251. /**
  252. * Get published vocabularies as select options
  253. * @return multitype:NULL
  254. */
  255. function tripal_entities_get_db_names_for_published_vocabularies() {
  256. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  257. $db = array();
  258. foreach ($published_vocs as $voc) {
  259. $db [$voc->db_id->db_id] = $voc->db_id->name;
  260. }
  261. return $db;
  262. }
  263. /**
  264. * Get published terms as select options
  265. * @return multitype:NULL
  266. */
  267. function tripal_entities_get_published_terms_as_select_options($cv_id = NULL) {
  268. $where = array('publish' => 1);
  269. $published_terms = chado_generate_var('tripal_term', $where, array('return_array' => 1));
  270. $options = array();
  271. foreach ($published_terms as $term) {
  272. if (!$cv_id) {
  273. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  274. }
  275. else {
  276. if ($term->cvterm_id->cv_id->cv_id == $cv_id) {
  277. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  278. }
  279. }
  280. }
  281. return $options;
  282. }
  283. /**
  284. * Menu argument loader; Load a tripal data type by string.
  285. *
  286. * @param $type
  287. * The machine-readable name of a tripal data type to load.
  288. * @return
  289. * A tripal data type array or FALSE if $type does not exist.
  290. */
  291. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  292. // Get the type of entity by the ID.
  293. $bundle_types = db_select('tripal_bundle', 'tdt')
  294. ->fields('tdt', array('id', 'type'))
  295. ->condition('bundle', $bundle_type)
  296. ->execute()
  297. ->fetchObject();
  298. if ($bundle_types) {
  299. $entity = entity_load('BioData_bundles', array($bundle_types->id), array(), $reset);
  300. return reset($entity);
  301. }
  302. return FALSE;
  303. }
  304. /**
  305. * Allows the menu system to use a wildcard to fetch the entity.
  306. *
  307. * Make sure that the wildcard you choose in the tripal_entity entity
  308. * definition fits the function name here.
  309. *
  310. * @param $id
  311. * Integer specifying the tripal_entity id.
  312. * @param $reset
  313. * A boolean indicating that the internal cache should be reset.
  314. * @return
  315. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  316. *
  317. * @see tripal_entity_load_multiple()
  318. */
  319. function tripal_entity_load($id, $reset = FALSE) {
  320. $entity = entity_load('BioData', array($id), array(), $reset);
  321. return reset($entity);
  322. }