tripal_entities.module 12 KB

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