tripal_entities.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. );
  92. }
  93. /**
  94. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  95. */
  96. function tripal_entities_rdf_mapping() {
  97. return array();
  98. /* return array(
  99. 'type' => 'tripal_entity',
  100. 'bundle' => 'gene',
  101. 'mapping' => array(
  102. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  103. 'title' => array(
  104. 'predicates' => array('dc:title'),
  105. ),
  106. 'uid' => array(
  107. 'predicates' => array('sioc:has_creator'),
  108. 'type' => 'rel',
  109. ),
  110. 'name' => array(
  111. 'predicates' => array('foaf:name'),
  112. ),
  113. 'uniquename' => array(
  114. 'predicates' => array('foaf:name'),
  115. ),
  116. 'organism_id' => array(
  117. 'predicates' => array('sioc:has_parent'),
  118. 'type' => 'rel'
  119. )
  120. ),
  121. ); */
  122. }
  123. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  124. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  125. /**
  126. * Implement hook_entity_info().
  127. */
  128. function tripal_entities_entity_info() {
  129. $entities = array();
  130. // Get a list of published vocabularies from 'tripal_vocabulary
  131. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  132. foreach ($published_vocs as $voc) {
  133. $entities[$voc->db_id->name] = array (
  134. // A human readable label to identify our entity.
  135. 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
  136. 'plural label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ')',
  137. // The entity class and controller class extend the classes provided by the
  138. // Entity API.
  139. 'entity class' => 'TripalEntity',
  140. 'controller class' => 'TripalEntityController',
  141. // The table for this entity defined in hook_schema()
  142. 'base table' => 'tripal_entity',
  143. // Returns the uri elements of an entity.
  144. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  145. // IF fieldable == FALSE, we can't attach fields.
  146. 'fieldable' => TRUE,
  147. // entity_keys tells the controller what database fields are used for key
  148. // functions. It is not required if we don't have bundles or revisions.
  149. // Here we do not support a revision, so that entity key is omitted.
  150. 'entity keys' => array (
  151. 'id' => 'id',
  152. 'bundle' => 'bundle'
  153. ),
  154. 'bundle keys' => array (
  155. 'bundle' => 'bundle'
  156. ),
  157. // Callback function for access to this entity.
  158. 'access callback' => 'tripal_entity_access',
  159. // FALSE disables caching. Caching functionality is handled by Drupal core.
  160. 'static cache' => FALSE,
  161. // Bundles are added in the hook_entities_info_alter() function.
  162. 'bundles' => array (),
  163. 'label callback' => 'tripal_entity_label',
  164. // The information below is used by the TripalEntityUIController
  165. // (which extends the EntityDefaultUIController). The admin_ui
  166. // key here is mean to appear on the 'Find Content' page of the
  167. // administrative menu.
  168. 'admin ui' => array (
  169. 'path' => 'admin/content/bio_data',
  170. 'controller class' => 'TripalEntityUIController',
  171. 'menu wildcard' => '%tripal_entity',
  172. 'file' => 'includes/TripalEntityUIController.inc'
  173. ),
  174. 'view modes' => array (
  175. 'full' => array (
  176. 'label' => t ( 'Full content' ),
  177. 'custom settings' => FALSE
  178. ),
  179. 'teaser' => array (
  180. 'label' => t ( 'Teaser' ),
  181. 'custom settings' => TRUE
  182. )
  183. )
  184. );
  185. // The entity that holds information about the entity types.
  186. $entities [$voc->db_id->name . '_bundle'] = array (
  187. 'label' => $voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
  188. 'entity class' => 'TripalBundle',
  189. 'controller class' => 'TripalBundleController',
  190. 'base table' => 'tripal_bundle',
  191. 'fieldable' => FALSE,
  192. 'exportable' => FALSE,
  193. 'entity keys' => array (
  194. 'id' => 'id',
  195. 'name' => 'bundle',
  196. 'label' => 'label'
  197. ),
  198. 'access callback' => 'tripal_bundle_access',
  199. 'module' => 'tripal_entities',
  200. // Enable the entity API's admin UI.
  201. 'admin ui' => array (
  202. 'path' => 'admin/structure/bio_data/' . $voc->db_id->name,
  203. 'controller class' => 'TripalBundleUIController',
  204. 'file' => 'includes/TripalBundleUIController.inc',
  205. 'menu wildcard' => '%tripal_bundle',
  206. )
  207. );
  208. }
  209. return $entities;
  210. }
  211. /**
  212. * Implements hook_entity_info_alter().
  213. *
  214. * We are adding the info about the tripal data types via a hook to avoid a
  215. * recursion issue as loading the model types requires the entity info as well.
  216. *
  217. */
  218. function tripal_entities_entity_info_alter(&$entity_info) {
  219. // Get a list of published terms from 'tripal_term
  220. $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  221. foreach ($published_terms as $term) {
  222. // Bundles are alternative groups of fields or configuration
  223. // associated with a base entity type.
  224. // We want to dynamically add the bundles (or term types) to the entity.
  225. $cvterm = $term->cvterm_id;
  226. $entity_type = $cvterm->dbxref_id->db_id->name;
  227. $accession = $cvterm->dbxref_id->accession;
  228. $bundle_id = $entity_type . '_' . $accession;
  229. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  230. $entity_info[$entity_type]['bundles'][$bundle_id] = array (
  231. 'label' => $label,
  232. 'admin' => array (
  233. 'path' => 'admin/structure/bio_data/' . $entity_type . '/manage/%tripal_bundle',
  234. 'real path' => 'admin/structure/bio_data/' . $entity_type . '/manage/' . $bundle_id,
  235. 'bundle argument' => 5,
  236. 'access arguments' => array (
  237. 'administer tripal data types'
  238. )
  239. )
  240. );
  241. }
  242. }
  243. /**
  244. * Get published vocabularies as select options
  245. * @return multitype:NULL
  246. */
  247. function tripal_entities_get_published_vocabularies_as_select_options() {
  248. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  249. $options = array();
  250. foreach ($published_vocs as $voc) {
  251. $options [$voc->cv_id->cv_id] = $voc->cv_id->name;
  252. }
  253. return $options;
  254. }
  255. /**
  256. * Get published terms as select options
  257. * @return multitype:NULL
  258. */
  259. function tripal_entities_get_published_terms_as_select_options($cv_id = NULL) {
  260. $where = array('publish' => 1);
  261. $published_terms = chado_generate_var('tripal_term', $where, array('return_array' => 1));
  262. $options = array();
  263. foreach ($published_terms as $term) {
  264. if (!$cv_id) {
  265. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  266. } else {
  267. if ($term->cvterm_id->cv_id->cv_id == $cv_id) {
  268. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  269. }
  270. }
  271. }
  272. return $options;
  273. }
  274. /**
  275. * Menu argument loader; Load a tripal data type by string.
  276. *
  277. * @param $type
  278. * The machine-readable name of a tripal data type to load.
  279. * @return
  280. * A tripal data type array or FALSE if $type does not exist.
  281. */
  282. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  283. // Get the type of entity by the ID.
  284. $bundle_types = db_select('tripal_bundle', 'tdt')
  285. ->fields('tdt', array('id', 'type'))
  286. ->condition('bundle', $bundle_type)
  287. ->execute()
  288. ->fetchObject();
  289. // Load the tripal_bundle entity. These entities are always the same
  290. // as an Tripal entity type but with a '_bundle' extension.
  291. $entity = entity_load($bundle_types->type . '_bundle', array($bundle_types->id), array(), $reset);
  292. return reset($entity);
  293. }
  294. /**
  295. * Fetch a tripal_entity object. Make sure that the wildcard you choose
  296. * in the tripal_entity entity definition fits the function name here.
  297. *
  298. * @param $id
  299. * Integer specifying the tripal_entity id.
  300. * @param $reset
  301. * A boolean indicating that the internal cache should be reset.
  302. * @return
  303. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  304. *
  305. * @see tripal_entity_load_multiple()
  306. */
  307. function tripal_entity_load($id, $reset = FALSE) {
  308. // Get the type of entity by the ID.
  309. $entity_type = db_select('tripal_entity', 'td')
  310. ->fields('td', array('type'))
  311. ->condition('id', $id)
  312. ->execute()
  313. ->fetchField();
  314. // Load the entity.
  315. $entity = entity_load($entity_type, array($id), array(), $reset);
  316. return reset($entity);
  317. }