tripal_core.toc.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_core_node_toc_form($form, &$form_state, $node) {
  6. $form["#tree"] = TRUE;
  7. $form["instructions"] = array(
  8. '#type' => 'fieldset',
  9. '#collapsed' => TRUE,
  10. '#collapsible' => TRUE,
  11. '#title' => 'Instructions',
  12. );
  13. $form["instructions"]["main"] = array(
  14. '#markup' => '</p>' . t('Below is a list of the titles of
  15. content panes that can appear on this page. These titles appear in the
  16. the following order in the Table of Contents (TOC). You may rename
  17. the titles or drag and drop them to change the order. <b>Any changes will
  18. only apply to this page</b>. If you would like to make changes apply to multiple
  19. pages of the same tpye, please visit the TOC administrative page.') . '<p>' .
  20. '<p>' . t('The list below shows all possible content panes that can appear.
  21. However, those without content are hidden and do not appear in the TOC.' . '</p>'),
  22. );
  23. $form['node'] = array(
  24. '#type' => 'value',
  25. '#value' => $node,
  26. );
  27. // Get the content array for this node, then pass it through the
  28. // tripal_core_node_view_alter which generates the TOC. After that
  29. // we can use the $build array to build the form. We have to add
  30. // a 'tripal_toc_mode' to the $node because we need to give the mode
  31. // to the tripal_core_node_view_build_toc function.
  32. $node->tripal_toc_mode = 'manage_node';
  33. node_build_content($node);
  34. $build = $node->content;
  35. $build["#node"] = $node;
  36. tripal_core_node_view_alter($build);
  37. // Iterate through the built items and add form elemetns for each one.
  38. foreach(element_children($build) as $key) {
  39. $element = $build[$key];
  40. if (array_key_exists('#tripal_toc_id', $element)) {
  41. $toc_id = $element['#tripal_toc_id'];
  42. $toc_title = $element['#tripal_toc_title'];
  43. $toc_weight = $element['#weight'];
  44. $toc_hide = $element['#hide'];
  45. $form['toc_items'][$toc_id]['title'] = array(
  46. '#type' => 'textfield',
  47. '#default_value' => $toc_title,
  48. );
  49. $form['toc_items'][$toc_id]['hide'] = array(
  50. '#type' => 'checkbox',
  51. '#default_value' => $toc_hide,
  52. );
  53. $form['toc_items'][$toc_id]['weight'] = array(
  54. '#type' => 'textfield',
  55. '#default_value' => $toc_weight,
  56. '#attributes' => array(
  57. 'class' => array('tripal-node-toc-items-weights'),
  58. ),
  59. '#size' => 5,
  60. );
  61. }
  62. }
  63. $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
  64. $form['submit'] = array(
  65. '#type' => 'submit',
  66. '#name' => 'toc_submit',
  67. '#value' => t('Submit'),
  68. );
  69. $form['unset'] = array(
  70. '#type' => 'submit',
  71. '#name' => 'toc_unset',
  72. '#value' => t('Unset Node Customizations'),
  73. );
  74. // Check to see if this node's TOC is specifically being managed.
  75. $sql = "SELECT count(*) FROM {tripal_toc} where nid = :nid";
  76. $managed_items = db_query($sql, array(':nid' => $node->nid))->fetchField();
  77. if ($managed_items > 0) {
  78. $form['is_managed'] = array(
  79. '#markup' => '<p><font color="red">' .
  80. t('This page currently has customiations to the TOC.</font> This means
  81. that any customzations for the content type are overriden. Click the
  82. "Unset Node Customizations" button above to remove page-level
  83. customizations and default to the content type settings.') . '</p>',
  84. );
  85. }
  86. return $form;
  87. }
  88. /**
  89. *
  90. * @param $variables
  91. */
  92. function theme_tripal_node_toc_items_table($variables) {
  93. $elements = $variables['element'];
  94. $toc_items = array();
  95. // Sort the toc_items using a custom sort function. But we need to include
  96. // only the items we want in the table (exclude renderable stuff).
  97. foreach(element_children($elements) as $key) {
  98. $toc_items[] = $elements[$key];
  99. }
  100. usort($toc_items, 'theme_tripal_node_sort_toc_items');
  101. // Build the table header.
  102. $headers = array('Content Pane Name', 'Hide', 'Weight');
  103. // Format the form elements as rows in the table.
  104. $rows = array();
  105. foreach ($toc_items as $key => $item) {
  106. $rows[] = array(
  107. 'data' => array(
  108. drupal_render($item['title']),
  109. drupal_render($item['hide']),
  110. drupal_render($item['weight']),
  111. ),
  112. 'class' => array('draggable'),
  113. );
  114. }
  115. // Theme and return the table.
  116. $table = array(
  117. 'header' => $headers,
  118. 'rows' => $rows,
  119. 'attributes' => array("id" => 'tripal-node-toc-items-table'),
  120. 'sticky' => TRUE,
  121. 'caption' => t('Content Panes Available in the TOC'),
  122. 'colgroups' => array(),
  123. 'empty' => t('There are no content panes for this page'),
  124. );
  125. drupal_add_tabledrag('tripal-node-toc-items-table', 'order', 'sibling', 'tripal-node-toc-items-weights');
  126. return theme_table($table);
  127. }
  128. /**
  129. *
  130. * @param $a
  131. * @param $b
  132. */
  133. function theme_tripal_node_sort_toc_items($a, $b) {
  134. if ($a['weight']['#value'] < $b['weight']['#value']) {
  135. return -1;
  136. }
  137. if ($a['weight']['#value'] > $b['weight']['#value']) {
  138. return 1;
  139. }
  140. if ($a['weight']['#value'] == $b['weight']['#value']) {
  141. return strcmp($a['title']['#value'], $b['title']['#value']);
  142. }
  143. }
  144. /**
  145. * Implements hook_validate for the tripal_core_node_toc_form.
  146. */
  147. function tripal_core_node_toc_form_validate($form, &$form_state) {
  148. $toc_items = $form_state['values']['toc_items'];
  149. // Iterate through the TOC items and validate.
  150. foreach ($toc_items as $toc_id => $item) {
  151. if (!$item['title']) {
  152. form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
  153. }
  154. }
  155. }
  156. /**
  157. * Implements hook_submit for the tripal_core_node_toc_form.
  158. */
  159. function tripal_core_node_toc_form_submit($form, &$form_state) {
  160. $toc_items = $form_state['values']['toc_items'];
  161. $node = $form_state['values']['node'];
  162. if ($form_state['clicked_button']['#name'] == "toc_submit") {
  163. $transaction = db_transaction();
  164. try {
  165. // First delete any settings for this node
  166. db_delete('tripal_toc')
  167. ->condition('nid', $node->nid)
  168. ->execute();
  169. // Second add in any new settings for this node
  170. foreach ($toc_items as $toc_id => $item) {
  171. db_insert('tripal_toc')
  172. ->fields(array(
  173. 'node_type' => $node->type,
  174. 'key' => $toc_id,
  175. 'title' => $item['title'],
  176. 'weight' => $item['weight'],
  177. 'nid' => $node->nid,
  178. 'hide' => $item['hide'],
  179. ))
  180. ->execute();
  181. }
  182. drupal_set_message("TOC changes successfully applied to this node only.");
  183. }
  184. catch (Exception $e) {
  185. $transaction->rollback();
  186. drupal_set_message("Failed to apply TOC changes.", "error");
  187. }
  188. }
  189. if ($form_state['clicked_button']['#name'] == "toc_unset") {
  190. $transaction = db_transaction();
  191. try {
  192. // First delete any settings for this node
  193. db_delete('tripal_toc')
  194. ->condition('nid', $node->nid)
  195. ->execute();
  196. drupal_set_message("TOC is no longer customized specifically for this page. Now using the content type settings.");
  197. }
  198. catch (Exception $e) {
  199. $transaction->rollback();
  200. drupal_set_message("Failed to apply TOC changes.", "error");
  201. }
  202. }
  203. }
  204. /**
  205. * To be called by tripal_core_node_view_alter() to generate the TOC.
  206. *
  207. * @param $build
  208. * The build array passed to hook_node_view_alter()
  209. *
  210. */
  211. function tripal_core_node_view_build_toc(&$build) {
  212. global $theme;
  213. // if this is not a full node view, we do not want to alter
  214. if ($build['#view_mode'] != 'full' OR !array_key_exists('#tripal_generic_node_template', $build)) {
  215. return;
  216. }
  217. $node_type = $build["#node"]->type;
  218. $nid = $build["#node"]->nid;
  219. // The mode alters the format of the build array. There are three types of
  220. // modes: "display", "manage_node", "manage_type". If "display" is provided
  221. // then the build array is formatted for the display of the content.
  222. // If "manage_node" is provided then the build array will contain all
  223. // content panes regardless if the pane should be hidden. This allows
  224. // the management tool to find all content panes and their settings. If
  225. // "manage_type" is provided then node-specific content panes are
  226. // excluded. Node-specific content panes are those that appear only on
  227. // specific nodes and therefore should not be used when managing the
  228. // TOC for a content type.
  229. $mode = isset($build["#node"]->tripal_toc_mode) ? $build["#node"]->tripal_toc_mode : "display";
  230. $cache = cache_get("theme_registry:$theme", 'cache');
  231. $node = $build['#node'];
  232. $toc = array();
  233. $toc_html = '';
  234. // If we are looking at a Tripal node template then we want to
  235. // make some changes to each pane of content so that we can associate
  236. // a table of contents and add administrator and curator messages.
  237. if ($build['#tripal_generic_node_template'] == TRUE) {
  238. // Iterate through all the elements of the $build array and for those
  239. // that are wanting to provide content for this node.
  240. $markup = array();
  241. foreach ($build as $key => $value) {
  242. $value = $build[$key];
  243. // Skip the body element as the Tripal node types do not use it.
  244. if ($key == 'body') {
  245. continue;
  246. }
  247. // Skip the table of contents and links as those will be placed elsewhere.
  248. if (preg_match('/^#/', $key) or $key == 'tripal_toc' or $key == 'links') {
  249. continue;
  250. }
  251. // For backwards compatibility we will handle the content type fields
  252. // named 'field_resource_blocks', 'field_resource_titles', and 'field_resource_links'
  253. // these fields can be added on the Drupal content types page and were
  254. // specifically recoginzed by Tripal v1.1.
  255. if ($mode != "manage_type" and $key == "field_resource_links") {
  256. // links should just appear on the sidebar as is and not open up a panel
  257. foreach (element_children($build[$key]) as $index) {
  258. $element = $build[$key][$index];
  259. $weight = 0;
  260. $toc_item_id = "resource-$index";
  261. $parts = explode("|", $element['#markup']);
  262. if (count($parts) == 2) {
  263. $toc[$weight][$parts[0]] = "<div id=\"$toc_item_id\" class=\"tripal_toc_list_item\">" . l($parts[0], $parts[1], array('attributes' => array('target' => '_blank'))) . "</div>";
  264. }
  265. else {
  266. $toc[$weight][$parts[0]] = "<div id=\"$toc_item_id\" class=\"tripal_toc_list_item\">" . $element['#markup'] . "</div>";
  267. }
  268. // remove this link from the build array as we've moved it to appear in the TOC
  269. unset($build[$key]);
  270. }
  271. continue;
  272. }
  273. if ($mode != "manage_type" and $key == "field_resource_titles") {
  274. // ignore these, we will use them in the field_resource_blocks if
  275. // statement below
  276. continue;
  277. }
  278. if ($mode != "manage_type" and $key == "field_resource_blocks") {
  279. foreach (element_children($build[$key]) as $index) {
  280. // get the details and the title
  281. $weight = 0;
  282. $hide = 0;
  283. $markup = $build[$key][$index]["#markup"];
  284. $toc_item_id = "resource-$index";
  285. // Get any overrides for this key.
  286. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type);
  287. // If the element should be hidden then unset this key the build
  288. // array continue to the next one
  289. if ($mode == "display" and $overrides['hide'] == 1) {
  290. continue;
  291. }
  292. $toc_item_title = $build["field_resource_titles"][$index]["#markup"];
  293. $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
  294. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  295. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  296. $updated_markup = "
  297. <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
  298. <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
  299. $markup
  300. </div>
  301. </div>
  302. ";
  303. $build[$toc_item_id]['#markup'] = $updated_markup;
  304. $build[$toc_item_id]['#toc_handled'] = TRUE;
  305. $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
  306. $build[$toc_item_id]['#tripal_toc_title'] = $toc_item_title;
  307. $build[$toc_item_id]['#weight'] = $weight;
  308. $build[$toc_item_id]['#hide'] = $hide;
  309. // add the entry to the TOC
  310. $toc_item_link = "
  311. <div class=\"tripal_toc_list_item\">
  312. <a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a>
  313. </div>
  314. ";
  315. $toc[$weight][$toc_item_title] = $toc_item_link;
  316. }
  317. // Remove the key from the build array. We have have replaced it
  318. unset($build[$key]);
  319. unset($build["field_resource_titles"]);
  320. continue;
  321. } // end if ($mode != "manage_type" and $key == "field_resource_blocks") {
  322. // Skip any keys we may have already handled. This is the case for
  323. // the field_resource_blocks where we removed the old CCK fields
  324. // and added new ones. We don't want these new ones to be processed
  325. // again by the code below.
  326. if (array_key_exists('#toc_handled', $build[$key]) and $build[$key]['#toc_handled'] == TRUE) {
  327. continue;
  328. }
  329. // For all other fields we will handle in the following way.
  330. //-----------------------
  331. // INITIALIZE THE CONTENT VARIABLES
  332. //-----------------------
  333. $toc_item_title = $key;
  334. $toc_item_id = $key;
  335. $toc_item_link = '';
  336. $weight = 0;
  337. $hide = 0;
  338. // get the title for the table of contents. Tripal templates should
  339. // have a '#tripal_toc_title' element in the build array
  340. if (array_key_exists('#tripal_toc_title', $build[$key])) {
  341. $toc_item_title = $build[$key]['#tripal_toc_title'];
  342. }
  343. // other elements in the $build array may just have a '#title' element,
  344. if (array_key_exists('#title', $build[$key])) {
  345. $toc_item_title = $build[$key]['#title'];
  346. }
  347. $toc_item_title = ucwords($toc_item_title);
  348. if (array_key_exists('#weight', $build[$key])) {
  349. $weight = $build[$key]['#weight'];
  350. }
  351. if (array_key_exists('#tripal_toc_id', $build[$key])) {
  352. $toc_item_id = $build[$key]['#tripal_toc_id'];
  353. }
  354. // Get any overrides for this key.
  355. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type);
  356. // If the element should be hidden then unset this key the build
  357. // array continue to the next one
  358. if ($mode == "display" and $overrides['hide'] == 1) {
  359. unset($build[$key]);
  360. continue;
  361. }
  362. // now override the title, weight, hidden values if a value is set in the tripal_toc table
  363. $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
  364. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  365. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  366. $toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a></div>";
  367. //-----------------------
  368. // GET THE MARKUP FOR EACH ELEMENT
  369. //-----------------------
  370. $markup = '';
  371. // find the markup. Some fields will have a '#markup' and others, such
  372. // as CCK elements may have a set of '#markup' elements organized by
  373. // numerical keys.
  374. if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
  375. $markup = $build[$key]['#markup'];
  376. }
  377. // For backwards copmatibility we should support the '#value' element as well.
  378. elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
  379. $markup = $build[$key]['#markup'];
  380. }
  381. // if we have no '#markup' field then this element has not yet
  382. // been rendered. Let's render it and substitute that for markup
  383. if (!$markup) {
  384. $markup = trim(render($build[$key]));
  385. }
  386. // Setup the content array for this element
  387. $build[$key] = array(
  388. '#markup' => $markup,
  389. '#tripal_toc_id' => $toc_item_id,
  390. '#tripal_toc_title' => $toc_item_title,
  391. '#weight' => $weight,
  392. '#hide' => $hide,
  393. );
  394. // if we still don't have markup then skip this one
  395. if (!$markup) {
  396. continue;
  397. }
  398. //-----------------------
  399. // FIND THE TEMPLATE PATH
  400. //-----------------------
  401. // get the template path so we can put it in an admin message box
  402. $path = '';
  403. if (!array_key_exists('#tripal_template_show', $build[$key]) or
  404. $build[$key]['#tripal_template_show'] == TRUE) {
  405. if ($cache and array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
  406. $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
  407. $path = tripal_set_message("Administrators, you can
  408. customize the way the content above is presented. Tripal provides a template
  409. file for each pane of content. To customize, copy the template file to your
  410. site's default theme, edit then " .
  411. l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
  412. Currently, the content above is provided by this template: <br><br>$path",
  413. TRIPAL_INFO,
  414. array('return_html' => 1)
  415. );
  416. }
  417. }
  418. //-----------------------
  419. // ADD THIS PANE TO THE TOC BY ORDER OF WEIGHT
  420. //-----------------------
  421. // set the weight of the TOC item and add it to our $toc array
  422. // for building of the TOC below
  423. $weight = 0;
  424. if (array_key_exists('#weight', $build[$key])) {
  425. $weight = $build[$key]['#weight'];
  426. }
  427. $toc[$weight][$toc_item_title] = $toc_item_link;
  428. //-----------------------
  429. // CREATE THE CONTENT PANE MARKUP
  430. //-----------------------
  431. // add a surrounding <div> box around the content
  432. $updated_markup = "
  433. <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
  434. <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
  435. $markup
  436. $path
  437. </div>
  438. </div>
  439. ";
  440. $build[$key]['#markup'] = $updated_markup;
  441. } // end foreach ($build as $key => $value) {
  442. } // end if ($build['#tripal_generic_node_template'] == TRUE) {
  443. //-----------------------
  444. // BUILD THE TABLE OF CONTENTS LINKS
  445. //-----------------------
  446. // first sort the links numerically by their weight
  447. ksort($toc, SORT_NUMERIC);
  448. $toc_html = '';
  449. foreach ($toc as $weight => $links) {
  450. // for links in the same weight, sort them alphabetically
  451. ksort($links);
  452. foreach ($links as $toc_item_title => $toc_item_link) {
  453. $toc_html .= $toc_item_link;
  454. }
  455. }
  456. $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-pane\" class=\"tripal-toc-pane\">$toc_html</div>";
  457. }
  458. /**
  459. *
  460. * @param $build
  461. */
  462. function tripal_core_get_toc_overrides($nid, $key, $node_type) {
  463. // Set override defaults
  464. $override_title = '';
  465. $override_weight = '';
  466. $override_hide = 0;
  467. // First look to see if the node has customizations for this item.
  468. $toc_item_overrides = db_select('tripal_toc', 'tc')
  469. ->fields('tc', array('title', 'weight', 'hide'))
  470. ->condition('key', $key)
  471. ->condition('nid', $nid)
  472. ->execute()
  473. ->fetchObject();
  474. if ($toc_item_overrides) {
  475. $override_title = $toc_item_overrides->title;
  476. $override_weight = $toc_item_overrides->weight;
  477. $override_hide = $toc_item_overrides->hide;
  478. }
  479. // If there are no specific node customizations then look to see if there
  480. // are customizations for this content type.
  481. else {
  482. $toc_item_overrides = db_select('tripal_toc', 'tc')
  483. ->fields('tc', array('title', 'weight', 'hide'))
  484. ->condition('node_type', $node_type)
  485. ->condition('key', $key)
  486. ->isNull('nid')
  487. ->execute()
  488. ->fetchObject();
  489. if ($toc_item_overrides) {
  490. $override_title = $toc_item_overrides->title;
  491. $override_weight = $toc_item_overrides->weight;
  492. $override_hide = $toc_item_overrides->hide;
  493. }
  494. }
  495. return array(
  496. 'title' => $override_title,
  497. 'weight' => $override_weight,
  498. 'hide' => $override_hide,
  499. );
  500. }