tripal_entities.module 11 KB

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