tripal_core.module 25 KB

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