tripal_entities.module 12 KB

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