tripal_entities.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. * Define administrative paths.
  55. */
  56. function tripal_entities_admin_paths() {
  57. if (variable_get('node_admin_theme')) {
  58. $paths = array(
  59. 'BioData/*/edit' => TRUE,
  60. 'BioData/*/delete' => TRUE,
  61. 'BioData/add' => TRUE,
  62. 'BioData/add/*' => TRUE,
  63. );
  64. return $paths;
  65. }
  66. }
  67. /**
  68. * Implements hook_menu_local_tasks_alter().
  69. * Used to add action links to pages.
  70. */
  71. function tripal_entities_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  72. // Add an "Add Biological Data" action link to the Admin > Content > Biological Data page.
  73. if ($root_path == 'admin/content/BioData') {
  74. $item = menu_get_item('BioData/add');
  75. if ($item['access']) {
  76. $data['actions']['output'][] = array(
  77. '#theme' => 'menu_local_action',
  78. '#link' => $item,
  79. );
  80. }
  81. }
  82. }
  83. /**
  84. * Implements hook_shortcut_default_set().
  85. * Modify the shortcut menu to include Biological data links.
  86. *
  87. * @param object $account
  88. * The user account whose default shortcut set will be returned. If not provided, the
  89. * function will return the currently logged-in user's default shortcut set.
  90. *
  91. * @return
  92. * An object representing the default shortcut set.
  93. */
  94. function tripal_entities_shortcut_default_set($account) {
  95. $sets = shortcut_sets();
  96. $found = FALSE;
  97. foreach ($sets as $set) {
  98. if ($set->title == 'TripalDefault') {
  99. $found = TRUE;
  100. }
  101. }
  102. if (!$found) {
  103. $t = get_t();
  104. // Create an initial default shortcut set.
  105. $shortcut_set = new stdClass();
  106. $shortcut_set->title = $t('TripalDefault');
  107. $shortcut_set->links = array(
  108. array(
  109. 'link_path' => 'BioData/add',
  110. 'link_title' => 'Add Biological Data',
  111. 'weight' => -23,
  112. ),
  113. array(
  114. 'link_path' => 'admin/content/BioData',
  115. 'link_title' => 'Find Biological Data',
  116. 'weight' => -22,
  117. ),
  118. array(
  119. 'link_path' => 'node/add',
  120. 'link_title' => $t('Add content'),
  121. 'weight' => -21,
  122. ),
  123. array(
  124. 'link_path' => 'admin/content',
  125. 'link_title' => $t('Find content'),
  126. 'weight' => -20,
  127. ),
  128. );
  129. shortcut_set_save($shortcut_set);
  130. }
  131. $sets = shortcut_sets();
  132. foreach ($sets as $set) {
  133. if ($set->title == 'TripalDefault') {
  134. return $set->set_name;
  135. }
  136. }
  137. }
  138. /**
  139. * Implements hook_permission().
  140. */
  141. function tripal_entities_permission() {
  142. // We set up permisssions to manage entity types, manage all entities and the
  143. // permissions for each individual entity
  144. $permissions = array(
  145. 'administer tripal data types' => array(
  146. 'title' => t('Administer Tripal data types'),
  147. 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
  148. ),
  149. 'administer tripal data' => array(
  150. 'title' => t('Administer Tripal data'),
  151. 'description' => t('Edit and delete all tripal data'),
  152. ),
  153. );
  154. return $permissions;
  155. }
  156. /**
  157. * Implements hook_theme().
  158. */
  159. function tripal_entities_theme($existing, $type, $theme, $path) {
  160. return array(
  161. 'tripal_entity' => array(
  162. 'render element' => 'elements',
  163. 'template' => 'tripal_entity',
  164. 'path' => "$path/theme/templates"
  165. ),
  166. 'tripal_entities_date_combo' => array(
  167. 'render element' => 'element',
  168. 'file' => 'includes/tripal_entities.chado_entity.inc',
  169. ),
  170. 'tripal_entities_add_list' => array(
  171. 'variables' => array('content' => NULL),
  172. 'file' => 'includes/tripal_entities.entity_form.inc',
  173. ),
  174. );
  175. }
  176. /**
  177. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  178. */
  179. function tripal_entities_rdf_mapping() {
  180. return array();
  181. /* return array(
  182. 'type' => 'tripal_entity',
  183. 'bundle' => 'gene',
  184. 'mapping' => array(
  185. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  186. 'title' => array(
  187. 'predicates' => array('dc:title'),
  188. ),
  189. 'uid' => array(
  190. 'predicates' => array('sioc:has_creator'),
  191. 'type' => 'rel',
  192. ),
  193. 'name' => array(
  194. 'predicates' => array('foaf:name'),
  195. ),
  196. 'uniquename' => array(
  197. 'predicates' => array('foaf:name'),
  198. ),
  199. 'organism_id' => array(
  200. 'predicates' => array('sioc:has_parent'),
  201. 'type' => 'rel'
  202. )
  203. ),
  204. ); */
  205. }
  206. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  207. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  208. /**
  209. * Implement hook_entity_info().
  210. */
  211. function tripal_entities_entity_info() {
  212. $entities = array();
  213. $entities['BioData'] = array (
  214. // A human readable label to identify our entity.
  215. 'label' => 'Biological Data',
  216. 'plural label' => 'Biological Data',
  217. // The entity class and controller class extend the classes provided by the
  218. // Entity API.
  219. 'entity class' => 'TripalEntity',
  220. 'controller class' => 'TripalEntityController',
  221. // The table for this entity defined in hook_schema()
  222. 'base table' => 'tripal_entity',
  223. // Returns the uri elements of an entity.
  224. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  225. // IF fieldable == FALSE, we can't attach fields.
  226. 'fieldable' => TRUE,
  227. // entity_keys tells the controller what database fields are used for key
  228. // functions. It is not required if we don't have bundles or revisions.
  229. // Here we do not support a revision, so that entity key is omitted.
  230. 'entity keys' => array (
  231. 'id' => 'id',
  232. 'bundle' => 'bundle'
  233. ),
  234. 'bundle keys' => array (
  235. 'bundle' => 'bundle'
  236. ),
  237. // Callback function for access to this entity.
  238. 'access callback' => 'tripal_entity_access',
  239. // FALSE disables caching. Caching functionality is handled by Drupal core.
  240. 'static cache' => FALSE,
  241. // Bundles are added dynamically below.
  242. 'bundles' => array (),
  243. 'label callback' => 'tripal_entity_label',
  244. // The information below is used by the TripalEntityUIController
  245. // (which extends the EntityDefaultUIController). The admin_ui
  246. // key here is mean to appear on the 'Find Content' page of the
  247. // administrative menu.
  248. 'admin ui' => array (
  249. 'path' => 'admin/content/BioData',
  250. 'controller class' => 'TripalEntityUIController',
  251. 'menu wildcard' => '%tripal_entity',
  252. 'file' => 'includes/TripalEntityUIController.inc'
  253. ),
  254. 'view modes' => array (
  255. 'full' => array (
  256. 'label' => t ('Full content'),
  257. 'custom settings' => FALSE
  258. ),
  259. 'teaser' => array (
  260. 'label' => t ('Teaser'),
  261. 'custom settings' => TRUE
  262. )
  263. )
  264. );
  265. // The entity that holds information about the entity types.
  266. $entities['BioData_bundles'] = array (
  267. 'label' => 'Biological Data Type', //$voc->db_id->name . ' (' . $voc->cv_id->name . ') Data Type',
  268. 'entity class' => 'TripalBundle',
  269. 'controller class' => 'TripalBundleController',
  270. 'base table' => 'tripal_bundle',
  271. 'fieldable' => FALSE,
  272. 'bundle of' => 'BioData',
  273. 'exportable' => FALSE,
  274. 'entity keys' => array (
  275. 'id' => 'id',
  276. 'name' => 'bundle',
  277. 'label' => 'label'
  278. ),
  279. 'access callback' => 'tripal_bundle_access',
  280. 'module' => 'tripal_entities',
  281. // Enable the entity API's admin UI.
  282. 'admin ui' => array (
  283. 'path' => 'admin/structure/BioData',
  284. 'controller class' => 'TripalBundleUIController',
  285. 'file' => 'includes/TripalBundleUIController.inc',
  286. 'menu wildcard' => '%tripal_bundle',
  287. )
  288. );
  289. // Dynamically add in the bundles. Bundles are alternative groups of fields
  290. // or configuration associated with an entity type .We want to dynamically
  291. // add the bundles to the entity.
  292. $published_terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  293. foreach ($published_terms as $term) {
  294. $cvterm = $term->cvterm_id;
  295. $bundle_id = 'dbxref_' . $cvterm->dbxref_id->dbxref_id;
  296. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  297. $entities['BioData']['bundles'][$bundle_id] = array (
  298. 'label' => $label,
  299. 'admin' => array (
  300. 'path' => 'admin/structure/BioData/manage/%tripal_bundle',
  301. 'real path' => 'admin/structure/BioData/manage/' . $bundle_id,
  302. 'bundle argument' => 4,
  303. 'access arguments' => array (
  304. 'administer tripal data types'
  305. )
  306. )
  307. );
  308. }
  309. return $entities;
  310. }
  311. /**
  312. * Get published vocabularies as select options
  313. * @return multitype:NULL
  314. */
  315. function tripal_entities_get_published_vocabularies_as_select_options() {
  316. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  317. $options = array();
  318. foreach ($published_vocs as $voc) {
  319. $options [$voc->cv_id->cv_id] = $voc->cv_id->name;
  320. }
  321. return $options;
  322. }
  323. /**
  324. * Get published vocabularies as select options
  325. * @return multitype:NULL
  326. */
  327. function tripal_entities_get_db_names_for_published_vocabularies() {
  328. $published_vocs = chado_generate_var('tripal_vocabulary', array('publish' => 1), array('return_array' => 1));
  329. $db = array();
  330. foreach ($published_vocs as $voc) {
  331. $db [$voc->db_id->db_id] = $voc->db_id->name;
  332. }
  333. return $db;
  334. }
  335. /**
  336. * Get published terms as select options
  337. * @return multitype:NULL
  338. */
  339. function tripal_entities_get_published_terms_as_select_options($cv_id = NULL) {
  340. $where = array('publish' => 1);
  341. $published_terms = chado_generate_var('tripal_term', $where, array('return_array' => 1));
  342. $options = array();
  343. foreach ($published_terms as $term) {
  344. if (!$cv_id) {
  345. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  346. }
  347. else {
  348. if ($term->cvterm_id->cv_id->cv_id == $cv_id) {
  349. $options [$term->cvterm_id->name] = $term->cvterm_id->name;
  350. }
  351. }
  352. }
  353. return $options;
  354. }
  355. /**
  356. * Menu argument loader; Load a tripal data type by string.
  357. *
  358. * @param $type
  359. * The machine-readable name of a tripal data type to load.
  360. * @return
  361. * A tripal data type array or FALSE if $type does not exist.
  362. */
  363. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  364. // Get the type of entity by the ID.
  365. $bundle_types = db_select('tripal_bundle', 'tdt')
  366. ->fields('tdt', array('id', 'type'))
  367. ->condition('bundle', $bundle_type)
  368. ->execute()
  369. ->fetchObject();
  370. if ($bundle_types) {
  371. $entity = entity_load('BioData_bundles', array($bundle_types->id), array(), $reset);
  372. return reset($entity);
  373. }
  374. return FALSE;
  375. }
  376. /**
  377. * Allows the menu system to use a wildcard to fetch the entity.
  378. *
  379. * Make sure that the wildcard you choose in the tripal_entity entity
  380. * definition fits the function name here.
  381. *
  382. * @param $id
  383. * Integer specifying the tripal_entity id.
  384. * @param $reset
  385. * A boolean indicating that the internal cache should be reset.
  386. * @return
  387. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  388. *
  389. * @see tripal_entity_load_multiple()
  390. */
  391. function tripal_entity_load($id, $reset = FALSE) {
  392. $entity = entity_load('BioData', array($id), array(), $reset);
  393. return reset($entity);
  394. }