tripal_core.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Core module
  5. */
  6. /**
  7. * @defgroup tripal_modules Tripal Modules
  8. * @{
  9. * All documented functions for the various Tripal Modules excluding API functions and Views Integration functions.
  10. * @}
  11. */
  12. /**
  13. * @defgroup tripal_api Tripal API
  14. * @{
  15. * Provides an application programming interface (API) for Tripal
  16. *
  17. * The Tripal API currently provides generic insert/update/select functions for all chado content as
  18. * well as some module specific functions that insert/update/delete/select specific chado content.
  19. *
  20. * This API is currently in its infancy and some necessary functions might be missing. If you find
  21. * a missing function that you think should be included go to the sourceforge feature request
  22. * page and request it's inclusion in the API. Such feature requests with a working function
  23. * definition will be given priority.
  24. * @}
  25. */
  26. /**
  27. * @defgroup tripal_core_api Core Module API
  28. * @ingroup tripal_api
  29. * @{
  30. * Functions providing support for all Tripal modules.
  31. * @}
  32. */
  33. /**
  34. * @defgroup tripal_core Tripal Core Module
  35. * @ingroup tripal_modules
  36. * @{
  37. * Functionality useful for all other Tripal modules including the Tripal jobs, files,
  38. * materialized views and custom table functions.
  39. * @}
  40. */
  41. // APPLICATION PROGRAMMER INTERFACE -------------
  42. // Chado API
  43. require_once "api/tripal_core.chado_general.api.inc";
  44. require_once 'api/tripal_core.chado_query.api.inc';
  45. require_once 'api/tripal_core.chado_variables.api.inc';
  46. require_once 'api/tripal_core.chado_schema.api.inc';
  47. require_once "api/tripal_core.chado_nodes.api.inc";
  48. require_once "api/tripal_core.chado_nodes.properties.api.inc";
  49. require_once "api/tripal_core.chado_nodes.dbxrefs.api.inc";
  50. require_once "api/tripal_core.chado_nodes.relationships.api.inc";
  51. // Table API
  52. require_once "api/tripal_core.custom_tables.api.inc";
  53. require_once "api/tripal_core.mviews.api.inc";
  54. // Miscellaneous API
  55. require_once "api/tripal_core.files.api.inc";
  56. require_once "api/tripal_core.jobs.api.inc";
  57. require_once 'api/tripal_core.DEPRECATED.inc';
  58. // INCLUDES -------------------------------------
  59. require_once "includes/jobs.inc";
  60. require_once "includes/mviews.inc";
  61. require_once "includes/custom_tables.inc";
  62. require_once "includes/chado_install.inc";
  63. require_once "includes/form_elements.inc";
  64. tripal_core_set_globals();
  65. /**
  66. * This function is used to set the global Chado variables
  67. * @ingroup tripal_core
  68. */
  69. function tripal_core_set_globals() {
  70. // these global variables are meant to be accessed by all Tripal
  71. // modules to find the chado version installed and if Chado is local.
  72. // these variables are stored as globals rather than using the drupal_set_variable
  73. // functions because the Drupal functions make databaes queries and for long
  74. // running loaders we don't want those queries repeatedly.
  75. $GLOBALS["chado_is_installed"] = chado_is_installed();
  76. if ($GLOBALS["chado_is_installed"]) {
  77. $GLOBALS["chado_is_local"] = chado_is_local();
  78. $GLOBALS["chado_version"] = chado_get_version();
  79. $GLOBALS["exact_chado_version"] = chado_get_version(TRUE);
  80. }
  81. }
  82. /**
  83. * Implements hook_init().
  84. * Used to set the search_path, create default content and set default variables.
  85. *
  86. * @ingroup tripal_core
  87. */
  88. function tripal_core_init() {
  89. global $base_url;
  90. // create the 'tripal' controlled volcabulary in chado but only if it doesn't already exist, and
  91. // only if the chado database is present.
  92. if ($GLOBALS["chado_is_installed"]) {
  93. // if the Tripal cv is missing then add
  94. $results = chado_query("SELECT * FROM {cv} WHERE name = 'tripal'");
  95. $tripal_cv = $results->fetchObject();
  96. if (!$tripal_cv) {
  97. $results = chado_query(
  98. "INSERT INTO {cv} (name,definition) " .
  99. "VALUES ('tripal', 'Terms used by Tripal for modules to manage data such as that stored in property tables like featureprop, analysisprop, etc')"
  100. );
  101. }
  102. // if the Tripal db is missing then add it
  103. $results = chado_query("SELECT * FROM {db} WHERE name = 'tripal'");
  104. $tripal_db = $results->fetchObject();
  105. if (!$tripal_db) {
  106. $results = chado_query(
  107. "INSERT INTO {db} (name,description) " .
  108. "VALUES ('tripal', 'Used as a database placeholder for tripal defined objects such as tripal cvterms')"
  109. );
  110. }
  111. }
  112. // add some variables for all javasript to use for building URLs
  113. $theme_dir = drupal_get_path('theme', 'tripal');
  114. $clean_urls = variable_get('clean_url', 0);
  115. drupal_add_js(
  116. " var baseurl = '$base_url';
  117. var themedir = '$theme_dir';
  118. var isClean = $clean_urls;",
  119. 'inline', 'header');
  120. // make sure the date time settings are the way Tripal will insert them
  121. // otherwise PostgreSQL version that may have a different datestyle setting
  122. // will fail when inserting or updating a date column in a table.
  123. db_query("SET DATESTYLE TO :style", array(':style' => 'MDY'));
  124. // If we want AHAH elements on the node forms (e.g. chado_pub form) then we need to include
  125. // the node.pages file. Otherwise this error message is given:
  126. //
  127. // warning: call_user_func_array() expects parameter 1 to be a valid callback,
  128. // function 'node_form' not found or invalid function name
  129. // in /var/www/includes/form.inc on line 382.
  130. module_load_include('inc', 'node', 'node.pages');
  131. }
  132. /**
  133. * Implements hook_menu().
  134. * Defines all menu items needed by Tripal Core
  135. *
  136. * @ingroup tripal_core
  137. */
  138. function tripal_core_menu() {
  139. $items = array();
  140. // Triapl setting groups
  141. $items['admin/tripal'] = array(
  142. 'title' => 'Tripal',
  143. 'description' => "Manage the behavior or Tripal and its various modules.",
  144. 'weight' => -8,
  145. 'page callback' => 'system_admin_menu_block_page',
  146. 'access arguments' => array('administer tripal'),
  147. 'file' => 'system.admin.inc',
  148. 'file path' => drupal_get_path('module', 'system'),
  149. );
  150. $items['admin/tripal/schema'] = array(
  151. 'title' => 'Chado Schema',
  152. 'description' => "Tools to extend the chado schema through custom tables & materialized views.",
  153. 'weight' => -2,
  154. 'access arguments' => array('administer tripal'),
  155. );
  156. $items['admin/tripal/chado'] = array(
  157. 'title' => 'Chado Modules',
  158. 'description' => 'Configuration for specific chado data types such as Vocabularies, Features, etc.',
  159. 'access arguments' => array('administer tripal'),
  160. 'type' => MENU_NORMAL_ITEM,
  161. 'weight' => -6
  162. );
  163. $items['admin/tripal/loaders'] = array(
  164. 'title' => 'Chado Data Loaders',
  165. 'description' => 'Tools facilitating loading data into the chado database. Includes a generic tab-delimited loader (Bulk Loader).',
  166. 'access arguments' => array('administer tripal'),
  167. 'type' => MENU_NORMAL_ITEM,
  168. 'weight' => -4
  169. );
  170. $items['admin/tripal/extension'] = array(
  171. 'title' => 'Extension Modules',
  172. 'description' => 'Configuration for Tripal extension modules.',
  173. 'access arguments' => array('administer tripal'),
  174. 'type' => MENU_NORMAL_ITEM,
  175. 'weight' => 0
  176. );
  177. // Tripal Setup
  178. $items['admin/tripal/setup'] = array(
  179. 'title' => 'Setup Tripal',
  180. 'description' => 'Tools for initial setup of Tripal',
  181. 'access arguments' => array('administer tripal'),
  182. 'weight' => -8
  183. );
  184. $items['admin/tripal/setup/customize'] = array(
  185. 'title' => 'Customize Tripal',
  186. 'description' => 'Information on how to customize tripal',
  187. 'page callback' => 'theme',
  188. 'page arguments' => array('tripal_core_customize'),
  189. 'access arguments' => array('administer tripal'),
  190. 'weight' => 10
  191. );
  192. $items['admin/tripal/setup/chado_install'] = array(
  193. 'title' => 'Install Chado Schema',
  194. 'description' => 'Installs the Chado database tables, views, etc., inside the current Drupal database',
  195. 'page callback' => 'drupal_get_form',
  196. 'page arguments' => array('tripal_core_chado_load_form'),
  197. 'access arguments' => array('install chado'),
  198. 'type' => MENU_NORMAL_ITEM,
  199. 'weight' => -10
  200. );
  201. // Jobs Management
  202. $items['admin/tripal/tripal_jobs'] = array(
  203. 'title' => 'Jobs',
  204. 'description' => 'Jobs managed by Tripal',
  205. 'page callback' => 'tripal_jobs_admin_view',
  206. 'access arguments' => array('administer tripal'),
  207. 'type' => MENU_NORMAL_ITEM,
  208. 'weight' => -10
  209. );
  210. $items['admin/tripal/tripal_jobs/help'] = array(
  211. 'title' => 'Help',
  212. 'description' => 'Help for the tripal job management system',
  213. 'page callback' => 'theme',
  214. 'page arguments' => array('tripal_core_job_help'),
  215. 'access arguments' => array('administer tripal'),
  216. 'type' => MENU_LOCAL_TASK,
  217. 'weight' => 10
  218. );
  219. $items['admin/tripal/tripal_jobs/cancel/%'] = array(
  220. 'title' => 'Jobs',
  221. 'description' => 'Cancel a pending job',
  222. 'page callback' => 'tripal_cancel_job',
  223. 'page arguments' => array(4),
  224. 'access arguments' => array('administer tripal'),
  225. 'type' => MENU_CALLBACK,
  226. );
  227. $items['admin/tripal/tripal_jobs/rerun/%'] = array(
  228. 'title' => 'Jobs',
  229. 'description' => 'Re-run an existing job.',
  230. 'page callback' => 'tripal_rerun_job',
  231. 'page arguments' => array(4),
  232. 'access arguments' => array('administer tripal'),
  233. 'type' => MENU_CALLBACK,
  234. );
  235. $items['admin/tripal/tripal_jobs/view/%'] = array(
  236. 'title' => 'Jobs Details',
  237. 'description' => 'View job details.',
  238. 'page callback' => 'tripal_jobs_view',
  239. 'page arguments' => array(4),
  240. 'access arguments' => array('administer tripal'),
  241. 'type' => MENU_CALLBACK,
  242. );
  243. $items['admin/tripal/tripal_jobs/views/jobs/enable'] = array(
  244. 'title' => 'Enable Jobs Administrative View',
  245. 'page callback' => 'tripal_views_admin_enable_view',
  246. 'page arguments' => array('tripal_core_admin_jobs', 'admin/tripal/tripal_jobs'),
  247. 'access arguments' => array('administer tripal'),
  248. 'type' => MENU_CALLBACK,
  249. );
  250. // Materialized Views
  251. $items['admin/tripal/schema/mviews'] = array(
  252. 'title' => 'Materialized Views',
  253. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  254. 'page callback' => 'tripal_mview_admin_view',
  255. 'access arguments' => array('administer tripal'),
  256. 'type' => MENU_NORMAL_ITEM,
  257. 'weight' => -10
  258. );
  259. $items['admin/tripal/schema/mviews/help'] = array(
  260. 'title' => 'Help',
  261. 'description' => 'Help for the materialized views management system',
  262. 'page callback' => 'theme',
  263. 'page arguments' => array('tripal_core_mviews_help'),
  264. 'access arguments' => array('administer tripal'),
  265. 'type' => MENU_LOCAL_TASK,
  266. 'weight' => 10
  267. );
  268. /**
  269. $items['admin/tripal/schema/mviews/list'] = array(
  270. 'title' => 'List Materialized Views',
  271. 'description' => 'A list of existing materialized views with the ability to edit & delete existing materialized views.',
  272. 'page callback' => 'tripal_mviews_report',
  273. 'access arguments' => array('administer tripal'),
  274. 'type' => MENU_NORMAL_ITEM,
  275. 'weight' => -10
  276. );
  277. */
  278. $items['admin/tripal/schema/mviews/report/%'] = array(
  279. 'title' => 'Materialized View',
  280. 'description' => 'Materialized views are used to improve speed of large or complex queries. These are database views as compared to Drupal views.',
  281. 'page callback' => 'tripal_mview_report',
  282. 'page arguments' => array(5),
  283. 'access arguments' => array('administer tripal'),
  284. 'type' => MENU_CALLBACK,
  285. );
  286. $items['admin/tripal/schema/mviews/new'] = array(
  287. 'title' => 'Create Materialized View',
  288. 'description' => 'Create a new materialized view.',
  289. 'page callback' => 'drupal_get_form',
  290. 'page arguments' => array('tripal_mviews_form'),
  291. 'access arguments' => array('administer tripal'),
  292. 'type' => MENU_CALLBACK,
  293. );
  294. $items['admin/tripal/schema/mviews/edit/%'] = array(
  295. 'title' => 'Edit Materialized View',
  296. 'page callback' => 'drupal_get_form',
  297. 'page arguments' => array('tripal_mviews_form', 5),
  298. 'access arguments' => array('administer tripal'),
  299. 'type' => MENU_CALLBACK,
  300. );
  301. $items['admin/tripal/schema/mviews/action/%/%'] = array(
  302. 'title' => 'Create Materialized View',
  303. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  304. 'page callback' => 'tripal_mviews_action',
  305. 'page arguments' => array(5, 6, "1"),
  306. 'access arguments' => array('administer tripal'),
  307. 'type' => MENU_CALLBACK,
  308. );
  309. // Custom Tables
  310. $items['admin/tripal/schema/custom_tables'] = array(
  311. 'title' => 'Custom Tables',
  312. 'description' => 'Creation of custom tables that are added to Chado database.',
  313. 'page callback' => 'tripal_custom_table_admin_view',
  314. 'access arguments' => array('administer tripal'),
  315. 'type' => MENU_NORMAL_ITEM,
  316. 'weight' => -10
  317. );
  318. $items['admin/tripal/schema/custom_tables/help'] = array(
  319. 'title' => 'Help',
  320. 'description' => 'Help for the tripal job management system',
  321. 'page callback' => 'theme',
  322. 'page arguments' => array('tripal_core_job_help'),
  323. 'access arguments' => array('administer tripal'),
  324. 'type' => MENU_LOCAL_TASK,
  325. 'weight' => 10
  326. );
  327. /**
  328. $items['admin/tripal/schema/custom_tables/list'] = array(
  329. 'title' => 'List of Custom Tables',
  330. 'description' => 'Provides a list of all custom tables created by Tripal and allows for editing or removing existing custom tables.',
  331. 'page callback' => 'tripal_custom_tables_list',
  332. 'access arguments' => array('administer tripal'),
  333. 'type' => MENU_NORMAL_ITEM,
  334. 'weight' => -10
  335. );
  336. */
  337. $items['admin/tripal/schema/custom_tables/view/%'] = array(
  338. 'title' => 'Custom Tables',
  339. 'description' => 'Custom tables are added to Chado.',
  340. 'page callback' => 'tripal_custom_table_view',
  341. 'page arguments' => array(4),
  342. 'access arguments' => array('administer tripal'),
  343. 'type' => MENU_CALLBACK,
  344. );
  345. $items['admin/tripal/schema/custom_tables/new'] = array(
  346. 'title' => 'Create Custom Table',
  347. 'description' => 'An interface for creating your own custom tables.',
  348. 'page callback' => 'tripal_custom_table_new_page',
  349. 'access arguments' => array('administer tripal'),
  350. 'type' => MENU_CALLBACK,
  351. );
  352. $items['admin/tripal/schema/custom_tables/edit/%'] = array(
  353. 'title' => 'Edit Custom Table',
  354. 'page callback' => 'drupal_get_form',
  355. 'page arguments' => array('tripal_custom_tables_form', 4),
  356. 'access arguments' => array('administer tripal'),
  357. 'type' => MENU_CALLBACK,
  358. );
  359. $items['admin/tripal/schema/custom_tables/action/%/%'] = array(
  360. 'title' => 'Create Custom Table',
  361. 'description' => 'Custom tables are added to Chado.',
  362. 'page callback' => 'tripal_custom_tables_action',
  363. 'page arguments' => array(4, 5, "1"),
  364. 'access arguments' => array('administer tripal'),
  365. 'type' => MENU_CALLBACK,
  366. );
  367. $items['admin/tripal/schema/custom_tables/views/tables/enable'] = array(
  368. 'title' => 'Enable Custom Tables Administrative View',
  369. 'page callback' => 'tripal_views_admin_enable_view',
  370. 'page arguments' => array('tripal_core_admin_custom_table', 'admin/tripal/schema/custom_tables'),
  371. 'access arguments' => array('administer tripal'),
  372. 'type' => MENU_CALLBACK,
  373. );
  374. // Relationshi API autocomplete callback
  375. $items['tripal_ajax/relationship_nodeform/%/%/name_to_id'] = array(
  376. 'page callback' => 'chado_node_relationships_name_to_id_callback',
  377. 'page arguments' => array(2,3),
  378. 'access arguments' => array('access content'),
  379. 'type' => MENU_CALLBACK
  380. );
  381. return $items;
  382. }
  383. /**
  384. * Set the permission types that the chado module uses. Essentially we
  385. * want permissionis that protect creation, editing and deleting of chado
  386. * data objects
  387. *
  388. * @ingroup tripal_core
  389. */
  390. function tripal_core_permission() {
  391. return array(
  392. 'install chado' => array(
  393. 'title' => t('Install Chado'),
  394. 'description' => t('Allow the user to install or upgrade a Chado database in the existing Drupal database.')
  395. ),
  396. 'administer tripal' => array(
  397. 'title' => t('Administer Tripal'),
  398. 'description' => t('Allow the user to access administrative pages of Tripal.')
  399. ),
  400. );
  401. }
  402. /**
  403. * Implements hook_theme().
  404. * Registers template files/functions used by this module.
  405. *
  406. * @ingroup tripal_core
  407. */
  408. function tripal_core_theme($existing, $type, $theme, $path) {
  409. return array(
  410. 'tripal_core_customize' => array(
  411. 'arguments' => array('job_id' => NULL),
  412. 'template' => 'tripal_core_customize',
  413. 'path' => "$path/theme"
  414. ),
  415. 'theme_file_upload_combo' => array(
  416. 'render element' => 'element',
  417. ),
  418. 'theme_sequence_combo' => array(
  419. 'render element' => 'element',
  420. ),
  421. 'tripal_core_jobs_help' => array(
  422. 'template' => 'tripal_core_jobs_help',
  423. 'variables' => array(NULL),
  424. 'path' => "$path/theme"
  425. ),
  426. 'tripal_core_customtables_help' => array(
  427. 'template' => 'tripal_core_customtables_help',
  428. 'variables' => array(NULL),
  429. 'path' => "$path/theme"
  430. ),
  431. // Chado Node API Themes
  432. // --------------------------------
  433. // Properties Node Form
  434. 'chado_node_properties_form_table' => array(
  435. 'function' => 'theme_chado_add_node_form_properties',
  436. 'render element' => 'element',
  437. ),
  438. // Additional Dbxrefs Nore Form
  439. 'chado_node_additional_dbxrefs_form_table' => array(
  440. 'function' => 'theme_chado_add_node_form_dbxrefs_table',
  441. 'render element' => 'element',
  442. ),
  443. // Relationships Nore Form
  444. 'chado_node_relationships_form_table' => array(
  445. 'function' => 'theme_chado_add_node_form_relationships_tables',
  446. 'render element' => 'element',
  447. ),
  448. // Admin messages theme
  449. // --------------------------------
  450. 'tripal_admin_message' => array(
  451. 'function' => 'theme_tripal_admin_message',
  452. 'variables' => array('message' => NULL),
  453. )
  454. );
  455. }
  456. /**
  457. * Implements hook_job_describe_args().
  458. * Describes the arguements for the tripal_populate_mview job to allow for greater
  459. * readability in the jobs details pages.
  460. *
  461. * @param $callback
  462. * The callback of the current tripal job (this is the function that will be executed
  463. * when tripal_launch_jobs.php is run.
  464. * @param $args
  465. * An array of arguments passed in when the job was registered.
  466. *
  467. * @return
  468. * A more readable $args array
  469. *
  470. * @ingroup tripal_core
  471. */
  472. function tripal_core_job_describe_args($callback, $args) {
  473. $new_args = array();
  474. if ($callback == 'tripal_populate_mview') {
  475. // get this mview details
  476. $sql = "SELECT * FROM {tripal_mviews} WHERE mview_id = :mview_id ";
  477. $results = db_query($sql, array(':mview_id' => $args[0]));
  478. $mview = $results->fetchObject();
  479. $new_args['View Name'] = $mview->name;
  480. }
  481. elseif ($callback == 'tripal_core_install_chado') {
  482. $new_args['Action'] = $args[0];
  483. }
  484. return $new_args;
  485. }
  486. /**
  487. * this is just a wrapper for backwards compatibility with a naming mistake.
  488. * it can go away in the future as it only is useful for jobs created by v0.3b
  489. *
  490. * @todo remove this function
  491. */
  492. function tripal_core_load_gff3($gff_file, $organism_id, $analysis_id, $add_only = 0,
  493. $update = 0, $refresh = 0, $remove = 0, $job = NULL) {
  494. tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id, $add_only,
  495. $update, $refresh, $remove, $job);
  496. }
  497. /**
  498. * Implements hook_coder_ignore().
  499. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for coder are stored
  500. */
  501. function tripal_core_coder_ignore() {
  502. return array(
  503. 'path' => drupal_get_path('module', 'tripal_core'),
  504. 'line prefix' => drupal_get_path('module', 'tripal_core'),
  505. );
  506. }
  507. /**
  508. * Implements hook_views_api()
  509. * Purpose: Essentially this hook tells drupal that there is views support for
  510. * for this module which then includes tripal_db.views.inc where all the
  511. * views integration code is
  512. *
  513. * @ingroup tripal_organism
  514. */
  515. function tripal_core_views_api() {
  516. return array(
  517. 'api' => 3.0,
  518. );
  519. }
  520. /**
  521. * After the node is built, we want to add instructions to each
  522. * content section letting the administrator know which template
  523. * they can customize
  524. *
  525. * @param unknown $build
  526. */
  527. function tripal_core_node_view_alter(&$build) {
  528. global $theme;
  529. //dpm($build);
  530. // if the $build['tripal_toc'] element is not present, then this is not
  531. // a full node view so we do not want to alter
  532. if (!array_key_exists('tripal_toc', $build)) {
  533. return;
  534. }
  535. $cache = cache_get("theme_registry:$theme", 'cache');
  536. $node = $build['#node'];
  537. $toc = array();
  538. $toc_html = '';
  539. // if we are looking at a Tripal node template then we want to
  540. // make some changes to each block of content so that we can associate
  541. // a table of contents and add administrator and curator messages
  542. if (preg_match('/chado_/', $node->type)) {
  543. // iterate through all the elements of the $build array and for those
  544. // that are wanting to provide content for this node
  545. $markup = array();
  546. foreach ($build as $key => $value) {
  547. // examine elements without a '#' prefix as these should be adding
  548. // contents to the page. Skip the table of contents and links as those
  549. // will be placed elsewhere
  550. if (!preg_match('/^#/', $key) and $key != 'tripal_toc' and $key != 'links') {
  551. //-----------------------
  552. // INITIALIZE THE CONTENT VARIABLES
  553. //-----------------------
  554. $toc_item_title = $key;
  555. $toc_item_id = $key;
  556. $toc_item_link = '';
  557. // get the title for the table of contents. Tripal templates should
  558. // have a '#tripal_toc_title' element in the build array
  559. if (array_key_exists('#tripal_toc_title', $build[$key])) {
  560. $toc_item_title = $build[$key]['#tripal_toc_title'];
  561. }
  562. // other elements in the $build array may just have a '#title' element,
  563. if (array_key_exists('#title', $build[$key])) {
  564. $toc_item_title = $build[$key]['#title'];
  565. }
  566. $toc_item_title = ucwords($toc_item_title);
  567. if (array_key_exists('#tripal_toc_id', $build[$key])) {
  568. $toc_item_id = $build[$key]['#tripal_toc_id'];
  569. }
  570. $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?block=$toc_item_id\">$toc_item_title</a></div>";
  571. //-----------------------
  572. // GET THE MARKUP FOR EACH ELEMENT
  573. //-----------------------
  574. $markup = '';
  575. // find the markup. Some fields will have a '#markup' and others, such
  576. // as CCK elements may have a set of '#markup' elements organized by
  577. // numerical keys.
  578. if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
  579. $markup = $build[$key]['#markup'];
  580. }
  581. // For backwards copmatibility we should support the '#value' element as well.
  582. elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
  583. $markup = $build[$key]['#markup'];
  584. }
  585. // if we have no '#markup' field then this element has not yet
  586. // been rendered. Let's render it and substitute that for markup
  587. if (!$markup) {
  588. $markup = trim(render($build[$key]));
  589. $build[$key] = array(
  590. '#markup' => $markup,
  591. );
  592. }
  593. // if we still don't have markup then skip this one
  594. if (!$markup) {
  595. continue;
  596. }
  597. //-----------------------
  598. // FIND THE TEMPLATE PATH
  599. //-----------------------
  600. // get the template path so we can put it in an admin message box
  601. $path = '';
  602. if (array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
  603. $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
  604. $path = theme('tripal_admin_message', array('message' => "Administrators, you can
  605. customize the way the content above is presented. Tripal provides a template
  606. file for each block of content. To customize, copy the template file to your
  607. site's default theme, edit then " .
  608. l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
  609. Currently, the content above is provided by this template: <br><br>$path")
  610. );
  611. }
  612. //-----------------------
  613. // SET THE WEIGHTS FOR THE TOC ELEMENTS
  614. //-----------------------
  615. // set the weight of the TOC item and add it to our $toc array
  616. // for building of the TOC below
  617. $weight = 0;
  618. if (array_key_exists('#weight', $build[$key])) {
  619. $weight = $build[$key]['#weight'];
  620. }
  621. $toc[$weight][$toc_item_title] = $toc_item_link;
  622. //-----------------------
  623. // CREATE THE DATA BLOCK
  624. //-----------------------
  625. // add a surrounding <div> box around the content
  626. $updated_markup = "
  627. <div id=\"$toc_item_id-tripal-data-block\" class=\"tripal-data-block\">
  628. <div class=\"$toc_item_id-tripal-data-block-title tripal-data-block-title\">$toc_item_title</div>
  629. $markup
  630. $path
  631. </div>
  632. </div>
  633. ";
  634. $build[$key]['#markup'] = $updated_markup;
  635. $build[$key]['#weight'] = $weight;
  636. }
  637. }
  638. }
  639. //-----------------------
  640. // BUILD THE TABLE OF CONTENTS LINKS
  641. //-----------------------
  642. // first sort the links numerically by their weight
  643. ksort($toc, SORT_NUMERIC);
  644. $toc_html = '';
  645. foreach ($toc as $weight => $links) {
  646. // for links in the same weight, sort them alphabetically
  647. ksort($links);
  648. foreach ($links as $toc_item_title => $toc_item_link) {
  649. $toc_html .= $toc_item_link;
  650. }
  651. }
  652. $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-block\" class=\"tripal-toc-block\">$toc_html</div>";
  653. //dpm($cache);
  654. //dpm($build);
  655. }
  656. /**
  657. *
  658. * @ingroup tripal_core
  659. */
  660. function tripal_core_node_view($node, $view_mode, $langcode) {
  661. // if this node type is a chado-based node (i.e. Tripal node)
  662. // the we want to add a table of contents to it's content list
  663. // this table of contents will be an empty
  664. if (preg_match('/^chado_/', $node->type)) {
  665. // Show feature browser and counts
  666. if ($view_mode == 'full') {
  667. $node->content['tripal_toc'] = array(
  668. '#markup' => "<div id=\"$node->type-tripal-toc-block\" class=\"tripal-toc-block\"></div>",
  669. );
  670. }
  671. }
  672. }