tripal_entities.module 12 KB

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