tripal_core.module 30 KB

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