tripal_core.module 25 KB

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