tripal_core.module 26 KB

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