tripal_core.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Core module
  5. */
  6. /**
  7. * @defgroup tripal_legacy_api Tripal Legacy API
  8. * @{
  9. * Provides an application programming interface (API) for Tripal v2
  10. * backwards-compatibilty. These functions are DEPRECATED and may not
  11. * be available in future version of Tripal.
  12. * @}
  13. */
  14. /**
  15. * @defgroup tripal_legacy_core Legacy Tripal Core Module
  16. * @ingroup tripal_legacy_modules
  17. * @{
  18. * Functionality useful for all other Tripal modules including the Tripal jobs, files,
  19. * materialized views and custom table functions.
  20. * @}
  21. */
  22. require_once 'api/tripal_core.chado_nodes.api.inc';
  23. require_once 'api/tripal_core.chado_nodes.title_and_path.inc';
  24. require_once 'api/tripal_core.chado_nodes.properties.api.inc';
  25. require_once 'api/tripal_core.chado_nodes.dbxrefs.api.inc';
  26. require_once 'api/tripal_core.chado_nodes.relationships.api.inc';
  27. require_once 'api/tripal_core.tripal_variables.api.inc';
  28. require_once 'includes/tripal_core.form_elements.inc';
  29. require_once 'includes/tripal_core.search.inc';
  30. /**
  31. * Implements hook_init().
  32. * Used to set the search_path, create default content and set default variables.
  33. *
  34. * @ingroup tripal_legacy_core
  35. */
  36. function tripal_core_init() {
  37. // If we want AHAH elements on the node forms (e.g. chado_pub form) then we need to include
  38. // the node.pages file. Otherwise this error message is given:
  39. //
  40. // warning: call_user_func_array() expects parameter 1 to be a valid callback,
  41. // function 'node_form' not found or invalid function name
  42. // in /var/www/includes/form.inc on line 382.
  43. module_load_include('inc', 'node', 'node.pages');
  44. }
  45. /**
  46. * Implements hook_menu().
  47. * Defines all menu items needed by Tripal Core
  48. *
  49. * @ingroup tripal_legacy_core
  50. */
  51. function tripal_core_menu() {
  52. $items = array();
  53. // Triapl setting groups
  54. $items['admin/tripal/legacy'] = array(
  55. 'title' => 'Tripal Legacy',
  56. 'description' => t("Legacy functionality from Tripal v2.0."),
  57. 'weight' => -8,
  58. 'page callback' => 'system_admin_menu_block_page',
  59. 'access arguments' => array('administer tripal'),
  60. 'file' => 'system.admin.inc',
  61. 'file path' => drupal_get_path('module', 'system'),
  62. );
  63. // Relationshi API autocomplete callback
  64. $items['tripal_ajax/relationship_nodeform/%/%/name_to_id'] = array(
  65. 'page callback' => 'chado_add_node_form_relationships_name_to_id_callback',
  66. 'page arguments' => array(2,3),
  67. 'access arguments' => array('access content'),
  68. 'type' => MENU_CALLBACK
  69. );
  70. // The node's TOC tab
  71. $items['node/%node/tripal_toc'] = array(
  72. 'title' => 'TOC',
  73. 'page callback' => 'drupal_get_form',
  74. 'page arguments' => array('tripal_core_node_toc_form', 1),
  75. 'access callback' => 'tripal_core_access_node_toc_form',
  76. 'access arguments' => array(1),
  77. 'type' => MENU_LOCAL_TASK,
  78. 'file' => '/includes/tripal_core.toc.inc',
  79. );
  80. return $items;
  81. }
  82. /**
  83. * An access wrapper function for editing the TOC
  84. *
  85. * @param $node
  86. * A node object
  87. * @return
  88. * Returns TRUE if the node is a Tripal-based node and the user hass
  89. * the 'administer tripal' role.
  90. */
  91. function tripal_core_access_node_toc_form($node) {
  92. $types = module_invoke_all('node_info');
  93. if (array_key_exists($node->type, $types) and
  94. array_key_exists('chado_node_api', $types[$node->type])) {
  95. return user_access('administer tripal');
  96. }
  97. return FALSE;
  98. }
  99. /**
  100. * Implements hook_permission().
  101. *
  102. * Set the permission types that the chado module uses. Essentially we
  103. * want permissionis that protect creation, editing and deleting of chado
  104. * data objects
  105. *
  106. * @ingroup tripal_legacy_core
  107. */
  108. function tripal_core_permission() {
  109. return array();
  110. }
  111. /**
  112. * Implements hook_theme().
  113. * Registers template files/functions used by this module.
  114. *
  115. * @ingroup tripal_legacy_core
  116. */
  117. function tripal_core_theme($existing, $type, $theme, $path) {
  118. return array(
  119. 'tripal_core_customize' => array(
  120. 'arguments' => array('job_id' => NULL),
  121. 'template' => 'tripal_core_customize',
  122. 'path' => "$path/theme/templates"
  123. ),
  124. 'theme_file_upload_combo' => array(
  125. 'render element' => 'element',
  126. ),
  127. 'theme_sequence_combo' => array(
  128. 'render element' => 'element',
  129. ),
  130. 'tripal_core_jobs_help' => array(
  131. 'template' => 'tripal_core_jobs_help',
  132. 'variables' => array(NULL),
  133. 'path' => "$path/theme/templates"
  134. ),
  135. 'tripal_core_customtables_help' => array(
  136. 'template' => 'tripal_core_customtables_help',
  137. 'variables' => array(NULL),
  138. 'path' => "$path/theme/templates"
  139. ),
  140. // Chado Node API Themes
  141. // --------------------------------
  142. // Properties Node Form
  143. 'chado_node_properties_form_table' => array(
  144. 'function' => 'theme_chado_add_node_form_properties',
  145. 'render element' => 'element',
  146. ),
  147. // Additional Dbxrefs Nore Form
  148. 'chado_node_additional_dbxrefs_form_table' => array(
  149. 'function' => 'theme_chado_add_node_form_dbxrefs_table',
  150. 'render element' => 'element',
  151. ),
  152. // Relationships Nore Form
  153. 'chado_node_relationships_form_table' => array(
  154. 'function' => 'theme_chado_add_node_form_relationships_table',
  155. 'render element' => 'element',
  156. ),
  157. // Form and form element themes.
  158. // --------------------------------
  159. 'tripal_node_toc_items_table' => array(
  160. 'render element' => 'element',
  161. ),
  162. // Theme function for the extension admin page.
  163. 'tripal_core_extensions_form_tables' => array(
  164. 'render element' => 'element',
  165. ),
  166. 'administer controlled vocabularies' => array(
  167. 'title' => t('Administer controlled vocabularies (CVs).'),
  168. 'description' => t('Allow a user to add, edit and delete controlled vocabularies as well as add and edit terms.')
  169. ),
  170. );
  171. }
  172. /**
  173. * Implements hook_coder_ignore().
  174. *
  175. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore
  176. * rules for coder are stored.
  177. *
  178. */
  179. function tripal_core_coder_ignore() {
  180. return array(
  181. 'path' => drupal_get_path('module', 'tripal_core'),
  182. 'line prefix' => drupal_get_path('module', 'tripal_core'),
  183. );
  184. }
  185. /**
  186. * Implements hook_views_api().
  187. *
  188. * Essentially this hook tells drupal that there is views support for
  189. * for this module which then includes tripal_db.views.inc where all the
  190. * views integration code is.
  191. *
  192. * @ingroup tripal_legacy_core
  193. */
  194. function tripal_core_views_api() {
  195. return array(
  196. 'api' => 3.0,
  197. );
  198. }
  199. /**
  200. * Implements hook_node_view_alter().
  201. *
  202. * @ingroup tripal_legacy_core
  203. */
  204. function tripal_core_node_view_alter(&$build) {
  205. module_load_include('inc', 'tripal_core', 'includes/tripal_core.toc');
  206. tripal_core_node_view_build_toc($build);
  207. }
  208. /**
  209. * Implements hook_node_view().
  210. *
  211. * @ingroup tripal_legacy_core
  212. */
  213. function tripal_core_node_view($node, $view_mode, $langcode) {
  214. // if this node type is a chado-based node (i.e. Tripal node)
  215. // the we want to add a table of contents to it's content list
  216. // this table of contents will be an empty
  217. if (preg_match('/^chado_/', $node->type)) {
  218. if ($view_mode == 'full') {
  219. if (!isset($node->content['#tripal_generic_node_template'])) {
  220. $node->content['#tripal_generic_node_template'] = TRUE;
  221. }
  222. }
  223. }
  224. }
  225. /**
  226. * Adds support for tokens in the field_resource_links field.
  227. *
  228. * The field_resource_links field is a special field that can be manually
  229. * added by the site admin for providing links on the Tripal TOC sidebar.
  230. * Using tokens will allow for creation of custom links. This function
  231. * will add a fieldset contiaining the list of appropriate tokens for the
  232. * content type.
  233. *
  234. * @param unknown $element
  235. * @param unknown $form_state
  236. * @param unknown $context
  237. */
  238. function tripal_core_field_widget_form_alter(&$element, &$form_state, $context) {
  239. // If the name of the field is 'field_resource_links' then we want to
  240. // add a fieldset of tokens.
  241. if (isset($element['#field_name']) AND $element['#field_name'] == 'field_resource_links') {
  242. // Add the tokens fieldset to the last element.
  243. $num_elements = count($context['items']);
  244. if ($num_elements == $element['#delta']) {
  245. $bundle = $element['#bundle'];
  246. $base_table = preg_replace('/^chado_(.*)$/', '\1', $bundle);
  247. $tokens = chado_node_generate_tokens($base_table);
  248. $token_list = chado_node_format_tokens($tokens);
  249. $element['tokens'] = array(
  250. '#type' => 'fieldset',
  251. '#title' => 'Available tokens',
  252. '#collapsible' => TRUE,
  253. '#collapsed' => TRUE,
  254. '#weight' => 100
  255. );
  256. $element['tokens']['tokens_table'] = array(
  257. '#type' => 'item',
  258. '#markup' => $token_list
  259. );
  260. }
  261. }
  262. }
  263. /**
  264. * Implements hook_block_info().
  265. */
  266. function tripal_core_block_info() {
  267. $blocks['tripal_search'] = array(
  268. 'info' => t('Tripal Search Block'),
  269. 'cache' => DRUPAL_NO_CACHE,
  270. );
  271. return $blocks;
  272. }
  273. /**
  274. * Implements hook_block_view().
  275. */
  276. function tripal_core_block_view($delta = '') {
  277. $block = array();
  278. switch ($delta) {
  279. case 'tripal_search':
  280. $block['title'] = 'Search';
  281. $form_render_arr = drupal_get_form('tripal_core_search_block');
  282. $block['content'] = array(
  283. '#markup' => drupal_render($form_render_arr),
  284. );
  285. break;
  286. }
  287. return $block;
  288. }
  289. /**
  290. * A simple search box form.
  291. */
  292. function tripal_core_search_block($form, $form_state) {
  293. $form['wrapper'] = array(
  294. '#prefix' => '<div id="search-block-form" class="container-inline">',
  295. '#suffix' => '</div>',
  296. );
  297. $form['wrapper']['title'] = array(
  298. '#markup' => '<h2 class="element-invisible">Search form</h2>',
  299. );
  300. $form['wrapper']['search_block_form'] = array(
  301. '#title' => 'Search',
  302. '#title_display' => 'invisible',
  303. '#type' => 'textfield',
  304. '#size' => 15,
  305. '#maxlength' => 128,
  306. '#attributes' =>array('placeholder' => t(variable_get('tripal_search_placeholder', 'Keywords')))
  307. );
  308. $form['wrapper']['submit'] = array(
  309. '#type' => 'submit',
  310. '#value' => 'Search',
  311. '#prefix' => '<div class="form-actions form-wrapper" id="edit-actions">',
  312. '#suffix' => '</div>'
  313. );
  314. $form['search_placeholder'] = array(
  315. '#type' => 'textfield',
  316. '#title' => 'Placeholder Text',
  317. '#description' => 'Change the text that shows up within the search box until the user enters any of their own text.',
  318. '#default_value' => variable_get('tripal_search_placeholder', 'Keywords'),
  319. );
  320. return $form;
  321. }
  322. /**
  323. * Submit for tripal_core_search_block form.
  324. */
  325. function tripal_core_search_block_submit($form, &$form_state) {
  326. $form_state['redirect'] = array(
  327. variable_get('tripal_search_block_url', 'search/data'),
  328. array(
  329. 'query' => array(
  330. 'keywords' => $form_state['values']['search_block_form']
  331. ),
  332. ),
  333. );
  334. }
  335. /**
  336. * Implements hook_block_configure().
  337. */
  338. function tripal_core_block_configure ($delta = '') {
  339. $form = array();
  340. $form['search_url'] = array(
  341. '#type' => 'textfield',
  342. '#title' => 'Search Page URL',
  343. '#description' => 'The URL for the page you would like to redirect to when the user
  344. clicks "Search". It is expected that this page will be a view with an exposed
  345. filter having a "Filter Identifier" (in "More" fieldset on the edit filter form)
  346. of "keywords".',
  347. '#default_value' => variable_get('tripal_search_block_url', 'search/data'),
  348. );
  349. return $form;
  350. }
  351. /**
  352. * Implements hook_block_save().
  353. */
  354. function tripal_core_block_save($delta = '', $edit = array()) {
  355. // We simply want to save this information in a Drupal variable for use in the form submit.
  356. if (!empty($edit['search_url'])) {
  357. variable_set('tripal_search_block_url', $edit['search_url']);
  358. }
  359. if (!empty($edit['search_placeholder'])) {
  360. variable_set('tripal_search_placeholder', $edit['search_placeholder']);
  361. }
  362. }