tripal_core.module 11 KB

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