tripal_entities.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/bio_data'] = 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/bio_data/default'] = array(
  36. 'title' => 'Biological Data',
  37. 'type' => MENU_DEFAULT_LOCAL_TASK,
  38. 'weight' => 1,
  39. );
  40. $items['admin/tripal/bio_data/publish'] = array(
  41. 'title' => 'Publish',
  42. 'description' => 'Publish Data',
  43. 'page callback' => 'tripal_entities_publish',
  44. 'access arguments' => array('administer tripal data types'),
  45. 'file' => 'includes/tripal_entities.admin.inc',
  46. 'type' => MENU_LOCAL_TASK,
  47. 'weight' => 2
  48. );
  49. $items['admin/tripal/bio_data/access'] = array(
  50. 'title' => 'Access',
  51. 'description' => 'Set default access permissions for collections of data.',
  52. 'page callback' => 'drupal_get_form',
  53. 'page arguments' => array('tripal_entities_admin_access_form'),
  54. 'access arguments' => array('administer tripal data types'),
  55. 'file' => 'includes/tripal_entities.admin.inc',
  56. 'type' => MENU_LOCAL_TASK,
  57. 'weight' => 3
  58. );
  59. return $items;
  60. }
  61. /**
  62. * Implements hook_permission().
  63. */
  64. function tripal_entities_permission() {
  65. // We set up permisssions to manage entity types, manage all entities and the
  66. // permissions for each individual entity
  67. $permissions = array(
  68. 'administer tripal data types' => array(
  69. 'title' => t('Administer Tripal data types'),
  70. 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
  71. ),
  72. 'administer tripal data' => array(
  73. 'title' => t('Administer Tripal data'),
  74. 'description' => t('Edit and delete all tripal data'),
  75. ),
  76. );
  77. return $permissions;
  78. }
  79. /**
  80. * Implements hook_theme().
  81. */
  82. function tripal_entities_theme($existing, $type, $theme, $path) {
  83. return array(
  84. 'tripal_entity' => array(
  85. 'render element' => 'elements',
  86. 'template' => 'tripal_entity',
  87. 'path' => "$path/theme/templates"
  88. ),
  89. );
  90. }
  91. /**
  92. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  93. */
  94. function tripal_entities_rdf_mapping() {
  95. return array();
  96. /* return array(
  97. 'type' => 'tripal_entity',
  98. 'bundle' => 'gene',
  99. 'mapping' => array(
  100. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  101. 'title' => array(
  102. 'predicates' => array('dc:title'),
  103. ),
  104. 'uid' => array(
  105. 'predicates' => array('sioc:has_creator'),
  106. 'type' => 'rel',
  107. ),
  108. 'name' => array(
  109. 'predicates' => array('foaf:name'),
  110. ),
  111. 'uniquename' => array(
  112. 'predicates' => array('foaf:name'),
  113. ),
  114. 'organism_id' => array(
  115. 'predicates' => array('sioc:has_parent'),
  116. 'type' => 'rel'
  117. )
  118. ),
  119. ); */
  120. }
  121. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  122. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  123. /**
  124. * Implement hook_entity_info().
  125. */
  126. function tripal_entities_entity_info() {
  127. $entities = array();
  128. // Get a list of published vocabularies from 'tripal_vocabulary
  129. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  130. foreach ($published_vocs as $voc) {
  131. $entities[$voc->db_id->name] = array (
  132. // A human readable label to identify our entity.
  133. 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
  134. 'plural label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
  135. // The entity class and controller class extend the classes provided by the
  136. // Entity API.
  137. 'entity class' => 'TripalEntity',
  138. 'controller class' => 'TripalEntityController',
  139. // The table for this entity defined in hook_schema()
  140. 'base table' => 'tripal_entity',
  141. // Returns the uri elements of an entity.
  142. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  143. // IF fieldable == FALSE, we can't attach fields.
  144. 'fieldable' => TRUE,
  145. // entity_keys tells the controller what database fields are used for key
  146. // functions. It is not required if we don't have bundles or revisions.
  147. // Here we do not support a revision, so that entity key is omitted.
  148. 'entity keys' => array (
  149. 'id' => 'id',
  150. 'bundle' => 'bundle'
  151. ),
  152. 'bundle keys' => array (
  153. 'bundle' => 'bundle'
  154. ),
  155. // Callback function for access to this entity.
  156. 'access callback' => 'tripal_entity_access',
  157. // FALSE disables caching. Caching functionality is handled by Drupal core.
  158. //'static cache' => FALSE,
  159. // Bundles are added in the hook_entities_info_alter() function.
  160. 'bundles' => array (),
  161. 'label callback' => 'tripal_entity_label',
  162. // The information below is used by the TripalEntityUIController
  163. // (which extends the EntityDefaultUIController). The admin_ui
  164. // key here is mean to appear on the 'Find Content' page of the
  165. // administrative menu.
  166. 'admin ui' => array (
  167. 'path' => 'admin/content/bio_data',
  168. 'controller class' => 'TripalEntityUIController',
  169. 'menu wildcard' => '%tripal_entity',
  170. 'file' => 'includes/TripalEntityUIController.inc'
  171. ),
  172. 'view modes' => array (
  173. 'full' => array (
  174. 'label' => t ( 'Full content' ),
  175. 'custom settings' => FALSE
  176. ),
  177. 'teaser' => array (
  178. 'label' => t ( 'Teaser' ),
  179. 'custom settings' => TRUE
  180. )
  181. )
  182. );
  183. // The entity that holds information about the entity types.
  184. $entities [$voc->db_id->name . '_bundle'] = array (
  185. 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
  186. 'entity class' => 'TripalBundle',
  187. 'controller class' => 'TripalBundleController',
  188. 'base table' => 'tripal_bundle',
  189. 'fieldable' => FALSE,
  190. 'exportable' => FALSE,
  191. 'entity keys' => array (
  192. 'id' => 'id',
  193. 'name' => 'bundle',
  194. 'label' => 'label'
  195. ),
  196. 'access callback' => 'tripal_bundle_access',
  197. 'module' => 'tripal_entities',
  198. // Enable the entity API's admin UI.
  199. 'admin ui' => array (
  200. 'path' => 'admin/structure/bio_data/' . $voc->db_id->name,
  201. 'controller class' => 'TripalBundleUIController',
  202. 'file' => 'includes/TripalBundleUIController.inc',
  203. 'menu wildcard' => '%tripal_bundle',
  204. )
  205. );
  206. }
  207. return $entities;
  208. }
  209. /**
  210. * Implements hook_entity_info_alter().
  211. *
  212. * We are adding the info about the tripal data types via a hook to avoid a
  213. * recursion issue as loading the model types requires the entity info as well.
  214. *
  215. */
  216. function tripal_entities_entity_info_alter(&$entity_info) {
  217. // Get a list of published terms from 'tripal_term
  218. $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  219. foreach ($published_terms as $term) {
  220. // Bundles are alternative groups of fields or configuration
  221. // associated with a base entity type.
  222. // We want to dynamically add the bundles (or term types) to the entity.
  223. $cvterm = $term->cvterm_id;
  224. $entity_type = $cvterm->dbxref_id->db_id->name;
  225. $accession = $cvterm->dbxref_id->accession;
  226. $bundle_id = $entity_type . '_' . $accession;
  227. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  228. $entity_info[$entity_type]['bundles'][$bundle_id] = array (
  229. 'label' => $label,
  230. 'admin' => array (
  231. 'path' => 'admin/structure/bio_data/' . $entity_type . '/manage/%tripal_bundle',
  232. 'real path' => 'admin/structure/bio_data/' . $entity_type . '/manage/' . $bundle_id,
  233. 'bundle argument' => 5,
  234. 'access arguments' => array (
  235. 'administer tripal data types'
  236. )
  237. )
  238. );
  239. }
  240. }
  241. /**
  242. * Get published vocabularies as select options
  243. * @return multitype:NULL
  244. */
  245. function tripal_entities_get_published_vocabularies_as_select_options() {
  246. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  247. $options = array();
  248. foreach ($published_vocs as $voc) {
  249. $options [$voc->cv_id->cv_id] = $voc->cv_id->name;
  250. }
  251. return $options;
  252. }
  253. /**
  254. * Get published vocabularies as select options
  255. * @return multitype:NULL
  256. */
  257. function tripal_entities_get_db_names_for_published_vocabularies() {
  258. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  259. $db = array();
  260. foreach ($published_vocs as $voc) {
  261. $db [$voc->db_id->db_id] = $voc->db_id->name;
  262. }
  263. return $db;
  264. }
  265. /**
  266. * Get published terms as select options
  267. * @return multitype:NULL
  268. */
  269. function tripal_entities_get_published_terms_as_select_options($cv_id = NULL) {
  270. $where = array('publish' => 1);
  271. $published_terms = chado_generate_var('tripal_term', $where, array('return_array' => 1));
  272. $options = array();
  273. foreach ($published_terms as $term) {
  274. if (!$cv_id) {
  275. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  276. } else {
  277. if ($term->cvterm_id->cv_id->cv_id == $cv_id) {
  278. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  279. }
  280. }
  281. }
  282. return $options;
  283. }
  284. /**
  285. * Menu argument loader; Load a tripal data type by string.
  286. *
  287. * @param $type
  288. * The machine-readable name of a tripal data type to load.
  289. * @return
  290. * A tripal data type array or FALSE if $type does not exist.
  291. */
  292. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  293. // Get the type of entity by the ID.
  294. $bundle_types = db_select('tripal_bundle', 'tdt')
  295. ->fields('tdt', array('id', 'type'))
  296. ->condition('bundle', $bundle_type)
  297. ->execute()
  298. ->fetchObject();
  299. // Load the tripal_bundle entity. These entities are always the same
  300. // as an Tripal entity type but with a '_bundle' extension.
  301. $entity = entity_load($bundle_types->type . '_bundle', array($bundle_types->id), array(), $reset);
  302. return reset($entity);
  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. // Get the type of entity by the ID.
  321. $entity_type = db_select('tripal_entity', 'td')
  322. ->fields('td', array('type'))
  323. ->condition('id', $id)
  324. ->execute()
  325. ->fetchField();
  326. // Load the entity.
  327. if ($entity_type) {
  328. $entity = entity_load($entity_type, array($id), array(), $reset);
  329. return reset($entity);
  330. }
  331. return FALSE;
  332. }
  333. /**
  334. * Implements hook_form_alter().
  335. *
  336. * @param unknown $form
  337. * @param unknown $form_state
  338. */
  339. function tripal_entities_form_alter(&$form, &$form_state, $form_id) {
  340. switch ($form_id) {
  341. case 'field_ui_field_edit_form':
  342. // For entity fields added by Tripal Entities we don't want the
  343. // the end-user to change the cardinality and the required fields
  344. // such that record can't be saved in Chado.
  345. $dbs = tripal_entities_get_db_names_for_published_vocabularies ();
  346. if (in_array($form['#instance']['entity_type'], $dbs)) {
  347. $form['field']['cardinality']['#access'] = FALSE;
  348. $form['instance']['required']['#access'] = FALSE;
  349. }
  350. break;
  351. }
  352. }