tripal_core.toc.inc

  1. 2.x tripal_core/includes/tripal_core.toc.inc
  2. 3.x legacy/tripal_core/includes/tripal_core.toc.inc

File

tripal_core/includes/tripal_core.toc.inc
View source
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_core_node_toc_form($form, &$form_state, $node) {
  6. // Get info about this content type
  7. $all_types = node_type_get_types();
  8. $type_info = $all_types[$node->type];
  9. $form["#tree"] = TRUE;
  10. $form["instructions"] = array(
  11. '#type' => 'fieldset',
  12. '#collapsed' => TRUE,
  13. '#collapsible' => TRUE,
  14. '#title' => 'Instructions',
  15. );
  16. $admin_link = l(
  17. $type_info->name . " TOC administrative page",
  18. "admin/tripal/chado/" . $type_info->module . "/" . $node->type . "toc",
  19. array('attributes' => array('target' => '_blank'))
  20. );
  21. $form["instructions"]["main"] = array(
  22. '#markup' => '<p>' . t("Below is a list of the titles of
  23. content panes that can appear on this page. These titles appear in the
  24. the following order in the Table of Contents (TOC). You may rename
  25. the titles or drag and drop them to change the order. <b>Any changes will
  26. only apply to this page</b>. If you would like to make changes apply to multiple
  27. pages of the same type, please visit the $admin_link. ") . '</p>' .
  28. '<p>' . t('The list below shows all possible content panes that can appear.
  29. However, those without content are automatically hidden and do not
  30. appear in the TOC.' . '</p>'),
  31. );
  32. $form['node'] = array(
  33. '#type' => 'value',
  34. '#value' => $node,
  35. );
  36. // Get the content array for this node, then pass it through the
  37. // tripal_core_node_view_alter which generates the TOC. After that
  38. // we can use the $build array to build the form. We have to add
  39. // a 'tripal_toc_mode' to the $node because we need to give the mode
  40. // to the tripal_core_node_view_build_toc function.
  41. $node->tripal_toc_mode = 'manage_node';
  42. node_build_content($node);
  43. $build = $node->content;
  44. $build["#node"] = $node;
  45. tripal_core_node_view_alter($build);
  46. // Iterate through the built items and add form elemetns for each one.
  47. foreach(element_children($build) as $key) {
  48. $element = $build[$key];
  49. if (array_key_exists('#tripal_toc_id', $element)) {
  50. $toc_id = $element['#tripal_toc_id'];
  51. $toc_title = $element['#tripal_toc_title'];
  52. $toc_weight = $element['#weight'];
  53. $toc_hide = $element['#hide'];
  54. // If this element is a link then we don't want to allow the user
  55. // to change the title as the link title is changed by using the
  56. // interface that created the link.
  57. $is_link = array_key_exists('#is_link', $element) ? $element['#is_link'] : FALSE;
  58. if (!$is_link) {
  59. $form['toc_items'][$toc_id]['title'] = array(
  60. '#type' => 'textfield',
  61. '#default_value' => $toc_title,
  62. );
  63. }
  64. else {
  65. $form['toc_items'][$toc_id]['title'] = array(
  66. '#markup' => '<i>link title:</i> ' . $toc_title,
  67. '#value' => $toc_title,
  68. );
  69. }
  70. $form['toc_items'][$toc_id]['hide'] = array(
  71. '#type' => 'checkbox',
  72. '#default_value' => $toc_hide,
  73. );
  74. $form['toc_items'][$toc_id]['weight'] = array(
  75. '#type' => 'textfield',
  76. '#default_value' => $toc_weight,
  77. '#attributes' => array(
  78. 'class' => array('tripal-node-toc-items-weights'),
  79. ),
  80. '#size' => 5,
  81. );
  82. }
  83. }
  84. $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
  85. $form['submit'] = array(
  86. '#type' => 'submit',
  87. '#name' => 'toc_submit',
  88. '#value' => t('Submit'),
  89. );
  90. $form['unset'] = array(
  91. '#type' => 'submit',
  92. '#name' => 'toc_unset',
  93. '#value' => t('Unset Node Customizations'),
  94. );
  95. // Check to see if this node's TOC is specifically being managed.
  96. $sql = "SELECT count(*) FROM {tripal_toc} where nid = :nid";
  97. $managed_items = db_query($sql, array(':nid' => $node->nid))->fetchField();
  98. if ($managed_items > 0) {
  99. $form['is_managed'] = array(
  100. '#markup' => '<p><font color="red">' .
  101. t('This page currently has customiations to the TOC.</font> This means
  102. that any customzations for the content type are overriden. Click the
  103. "Unset Node Customizations" button above to remove page-level
  104. customizations and default to the content type settings.') . '</p>',
  105. );
  106. }
  107. return $form;
  108. }
  109. /**
  110. *
  111. * @param $variables
  112. */
  113. function theme_tripal_node_toc_items_table($variables) {
  114. $elements = $variables['element'];
  115. $toc_items = array();
  116. // Sort the toc_items using a custom sort function. But we need to include
  117. // only the items we want in the table (exclude renderable stuff).
  118. foreach(element_children($elements) as $key) {
  119. $toc_items[] = $elements[$key];
  120. }
  121. usort($toc_items, 'theme_tripal_node_sort_toc_items');
  122. // Build the table header.
  123. $headers = array('Content Pane Name', 'Hide', 'Weight');
  124. // Format the form elements as rows in the table.
  125. $rows = array();
  126. foreach ($toc_items as $key => $item) {
  127. $rows[] = array(
  128. 'data' => array(
  129. drupal_render($item['title']),
  130. drupal_render($item['hide']),
  131. drupal_render($item['weight']),
  132. ),
  133. 'class' => array('draggable'),
  134. );
  135. }
  136. // Theme and return the table.
  137. $table = array(
  138. 'header' => $headers,
  139. 'rows' => $rows,
  140. 'attributes' => array("id" => 'tripal-node-toc-items-table'),
  141. 'sticky' => TRUE,
  142. 'caption' => t('Content Panes Available in the TOC'),
  143. 'colgroups' => array(),
  144. 'empty' => t('There are no content panes for this page'),
  145. );
  146. drupal_add_tabledrag('tripal-node-toc-items-table', 'order', 'sibling', 'tripal-node-toc-items-weights');
  147. return theme_table($table);
  148. }
  149. /**
  150. *
  151. * @param $a
  152. * @param $b
  153. */
  154. function theme_tripal_node_sort_toc_items($a, $b) {
  155. if ($a['weight']['#value'] < $b['weight']['#value']) {
  156. return -1;
  157. }
  158. if ($a['weight']['#value'] > $b['weight']['#value']) {
  159. return 1;
  160. }
  161. if ($a['weight']['#value'] == $b['weight']['#value']) {
  162. return strcmp($a['title']['#value'], $b['title']['#value']);
  163. }
  164. }
  165. /**
  166. * Implements hook_validate for the tripal_core_node_toc_form.
  167. */
  168. function tripal_core_node_toc_form_validate($form, &$form_state) {
  169. $toc_items = $form_state['values']['toc_items'];
  170. // Iterate through the TOC items and validate.
  171. foreach ($toc_items as $toc_id => $item) {
  172. if (array_key_exists('title', $item) and !$item['title']) {
  173. form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
  174. }
  175. }
  176. }
  177. /**
  178. * Implements hook_submit for the tripal_core_node_toc_form.
  179. */
  180. function tripal_core_node_toc_form_submit($form, &$form_state) {
  181. $toc_items = $form_state['values']['toc_items'];
  182. $node = $form_state['values']['node'];
  183. if ($form_state['clicked_button']['#name'] == "toc_submit") {
  184. $transaction = db_transaction();
  185. try {
  186. // First delete any settings for this node
  187. db_delete('tripal_toc')
  188. ->condition('nid', $node->nid)
  189. ->execute();
  190. // Second add in any new settings for this node
  191. foreach ($toc_items as $toc_id => $item) {
  192. db_insert('tripal_toc')
  193. ->fields(array(
  194. 'node_type' => $node->type,
  195. 'key' => $toc_id,
  196. 'title' => array_key_exists('title', $item) ? $item['title'] : '',
  197. 'weight' => $item['weight'],
  198. 'nid' => $node->nid,
  199. 'hide' => $item['hide'],
  200. ))
  201. ->execute();
  202. }
  203. drupal_set_message("TOC changes successfully applied to this node only.");
  204. }
  205. catch (Exception $e) {
  206. $transaction->rollback();
  207. drupal_set_message("Failed to apply TOC changes.", "error");
  208. }
  209. }
  210. if ($form_state['clicked_button']['#name'] == "toc_unset") {
  211. $transaction = db_transaction();
  212. try {
  213. // First delete any settings for this node
  214. db_delete('tripal_toc')
  215. ->condition('nid', $node->nid)
  216. ->execute();
  217. drupal_set_message("TOC is no longer customized specifically for this page. Now using the content type settings.");
  218. }
  219. catch (Exception $e) {
  220. $transaction->rollback();
  221. drupal_set_message("Failed to apply TOC changes.", "error");
  222. }
  223. }
  224. }
  225. /**
  226. * To be called by tripal_core_node_view_alter() to generate the TOC.
  227. *
  228. * @param $build
  229. * The build array passed to hook_node_view_alter()
  230. *
  231. */
  232. function tripal_core_node_view_build_toc(&$build) {
  233. global $theme;
  234. // if this is not a full node view, we do not want to alter
  235. if ($build['#view_mode'] != 'full' OR !array_key_exists('#tripal_generic_node_template', $build)) {
  236. return;
  237. }
  238. $node_type = $build["#node"]->type;
  239. $nid = $build["#node"]->nid;
  240. // The mode alters the format of the build array. There are three types of
  241. // modes: "display", "manage_node", "manage_type". If "display" is provided
  242. // then the build array is formatted for the display of the content.
  243. // If "manage_node" is provided then the build array will contain all
  244. // content panes regardless if the pane should be hidden. This allows
  245. // the management tool to find all content panes and their settings. If
  246. // "manage_type" is provided then node-specific content panes are
  247. // excluded. Node-specific content panes are those that appear only on
  248. // specific nodes and therefore should not be used when managing the
  249. // TOC for a content type.
  250. $mode = isset($build["#node"]->tripal_toc_mode) ? $build["#node"]->tripal_toc_mode : "display";
  251. $cache = cache_get("theme_registry:$theme", 'cache');
  252. $node = $build['#node'];
  253. $toc = array();
  254. $toc_html = '';
  255. // If we are looking at a Tripal node template then we want to
  256. // make some changes to each pane of content so that we can associate
  257. // a table of contents and add administrator and curator messages.
  258. if ($build['#tripal_generic_node_template'] == TRUE) {
  259. // Iterate through all the elements of the $build array and for those
  260. // that are wanting to provide content for this node.
  261. $markup = array();
  262. foreach ($build as $key => $value) {
  263. $value = $build[$key];
  264. // Skip the body element as the Tripal node types do not use it.
  265. if ($key == 'body') {
  266. continue;
  267. }
  268. // Skip the table of contents and links as those will be placed elsewhere.
  269. if (preg_match('/^#/', $key) or $key == 'tripal_toc' or $key == 'links') {
  270. continue;
  271. }
  272. // For backwards compatibility we will handle the content type fields
  273. // named 'field_resource_blocks', 'field_resource_titles', and
  274. // 'field_resource_links' these fields can be added on the Drupal content
  275. // types page and were specifically recoginzed by Tripal v1.1. If the
  276. // mode type is "manage_type" then remove these content panes because
  277. // they are node specific.
  278. if ($mode == "manage_type" and (
  279. $key == "field_resource_links" or
  280. $key == "field_resource_titles" or
  281. $key == "field_resource_blocks")) {
  282. unset($build[$key]);
  283. continue;
  284. }
  285. if ($key == "field_resource_links") {
  286. // links should just appear on the sidebar as is and not open up a panel
  287. foreach (element_children($build[$key]) as $index) {
  288. $element = $build[$key][$index];
  289. $weight = 0;
  290. $hide = 0;
  291. $toc_item_id = "resource-link-$index";
  292. // Get any overrides for this key.
  293. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
  294. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  295. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  296. // If the element should be hidden then unset this key the build
  297. // array continue to the next one
  298. if ($mode == "display" and $overrides['hide'] == 1) {
  299. continue;
  300. }
  301. // This field supports tokens, so we need to perform the substitutions
  302. // if one is needed. Get the tokens and format
  303. $base_table = preg_replace('/^chado_(.*)$/', '\1', $node_type);
  304. $tokens = chado_node_generate_tokens($base_table);
  305. $markup = $element['#markup'];
  306. // Determine which tokens were used in the format string
  307. if (preg_match_all('/\[[^]]+\]/', $markup, $used_tokens)) {
  308. // Get the value for each token used
  309. foreach ($used_tokens[0] as $token) {
  310. $token_info = $tokens[$token];
  311. if (!empty($token_info)) {
  312. $value = chado_get_token_value($token_info, $node);
  313. $markup = str_replace($token, $value, $markup);
  314. }
  315. }
  316. $element['#markup'] = $markup;
  317. }
  318. // Add the link to the TOC
  319. $parts = explode("|", $element['#markup']);
  320. if (count($parts) == 2) {
  321. $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>";
  322. }
  323. else {
  324. $toc[$weight][$parts[0]] = "<div id=\"$toc_item_id\" class=\"tripal_toc_list_item\">" . $element['#markup'] . "</div>";
  325. }
  326. // Add to the build array but do not add markup. This way
  327. // when the TOC is managed by the node 'TOC' menu these links can
  328. // be ordered as well.
  329. $build[$toc_item_id]['#toc_handled'] = TRUE;
  330. $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
  331. $build[$toc_item_id]['#tripal_toc_title'] = $parts[0];
  332. $build[$toc_item_id]['#weight'] = $weight;
  333. $build[$toc_item_id]['#hide'] = $hide;
  334. $build[$toc_item_id]['#is_link'] = TRUE;
  335. }
  336. // Remove the orilink from the build array as we've moved it to
  337. // appear in the TOC
  338. unset($build[$key]);
  339. continue;
  340. }
  341. if ($key == "field_resource_titles") {
  342. // ignore these, we will use them in the field_resource_blocks if
  343. // statement below
  344. continue;
  345. }
  346. if ($key == "field_resource_blocks") {
  347. foreach (element_children($build[$key]) as $index) {
  348. // get the details and the title
  349. $weight = 0;
  350. $hide = 0;
  351. $markup = $build[$key][$index]["#markup"];
  352. $toc_item_id = "resource-$index";
  353. // Get any overrides for this key.
  354. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
  355. // If the element should be hidden then unset this key the build
  356. // array continue to the next one
  357. if ($mode == "display" and $overrides['hide'] == 1) {
  358. continue;
  359. }
  360. $toc_item_title = $build["field_resource_titles"][$index]["#markup"];
  361. $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
  362. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  363. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  364. $updated_markup = "
  365. <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
  366. <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
  367. $markup
  368. </div>
  369. </div>
  370. ";
  371. $build[$toc_item_id]['#markup'] = $updated_markup;
  372. $build[$toc_item_id]['#toc_handled'] = TRUE;
  373. $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
  374. $build[$toc_item_id]['#tripal_toc_title'] = $toc_item_title;
  375. $build[$toc_item_id]['#weight'] = $weight;
  376. $build[$toc_item_id]['#hide'] = $hide;
  377. // add the entry to the TOC
  378. $toc_item_link = "
  379. <div class=\"tripal_toc_list_item\">
  380. <a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?pane=$toc_item_id\">$toc_item_title</a>
  381. </div>
  382. ";
  383. $toc[$weight][$toc_item_title] = $toc_item_link;
  384. }
  385. // Remove the key from the build array. We have have replaced it
  386. unset($build[$key]);
  387. unset($build["field_resource_titles"]);
  388. continue;
  389. } // end if ($mode != "manage_type" and $key == "field_resource_blocks") {
  390. // Skip any keys we may have already handled. This is the case for
  391. // the field_resource_blocks where we removed the old CCK fields
  392. // and added new ones. We don't want these new ones to be processed
  393. // again by the code below.
  394. if (array_key_exists('#toc_handled', $build[$key]) and $build[$key]['#toc_handled'] == TRUE) {
  395. continue;
  396. }
  397. // For all other fields we will handle in the following way.
  398. //-----------------------
  399. // INITIALIZE THE CONTENT VARIABLES
  400. //-----------------------
  401. $toc_item_title = $key;
  402. $toc_item_id = $key;
  403. $toc_item_link = '';
  404. $weight = 0;
  405. $hide = 0;
  406. // get the title for the table of contents. Tripal templates should
  407. // have a '#tripal_toc_title' element in the build array
  408. if (array_key_exists('#tripal_toc_title', $build[$key])) {
  409. $toc_item_title = $build[$key]['#tripal_toc_title'];
  410. }
  411. // other elements in the $build array may just have a '#title' element,
  412. if (array_key_exists('#title', $build[$key])) {
  413. $toc_item_title = $build[$key]['#title'];
  414. }
  415. $toc_item_title = ucwords($toc_item_title);
  416. if (array_key_exists('#weight', $build[$key])) {
  417. $weight = $build[$key]['#weight'];
  418. }
  419. if (array_key_exists('#tripal_toc_id', $build[$key])) {
  420. $toc_item_id = $build[$key]['#tripal_toc_id'];
  421. }
  422. // Get any overrides for this key.
  423. $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
  424. // If the element should be hidden then unset this key the build
  425. // array continue to the next one
  426. if ($mode == "display" and $overrides['hide'] == 1) {
  427. unset($build[$key]);
  428. continue;
  429. }
  430. // now override the title, weight, hidden values if a value is set in the tripal_toc table
  431. $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
  432. $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
  433. $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
  434. $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>";
  435. //-----------------------
  436. // GET THE MARKUP FOR EACH ELEMENT
  437. //-----------------------
  438. $markup = '';
  439. // find the markup. Some fields will have a '#markup' and others, such
  440. // as CCK elements may have a set of '#markup' elements organized by
  441. // numerical keys.
  442. if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
  443. $markup = $build[$key]['#markup'];
  444. }
  445. // For backwards copmatibility we should support the '#value' element as well.
  446. elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
  447. $markup = $build[$key]['#markup'];
  448. }
  449. // if we have no '#markup' field then this element has not yet
  450. // been rendered. Let's render it and substitute that for markup
  451. if (!$markup) {
  452. $markup = trim(render($build[$key]));
  453. }
  454. // Setup the content array for this element
  455. $build[$key] = array(
  456. '#markup' => $markup,
  457. '#tripal_toc_id' => $toc_item_id,
  458. '#tripal_toc_title' => $toc_item_title,
  459. '#weight' => $weight,
  460. '#hide' => $hide,
  461. );
  462. // if we still don't have markup then skip this one
  463. if (!$markup) {
  464. continue;
  465. }
  466. //-----------------------
  467. // FIND THE TEMPLATE PATH
  468. //-----------------------
  469. // get the template path so we can put it in an admin message box
  470. $path = '';
  471. if (!array_key_exists('#tripal_template_show', $build[$key]) or
  472. $build[$key]['#tripal_template_show'] == TRUE) {
  473. if ($cache and array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
  474. $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
  475. $path = tripal_set_message("Administrators, you can
  476. customize the way the content above is presented. Tripal provides a template
  477. file for each pane of content. To customize, copy the template file to your
  478. site's default theme, edit then " .
  479. l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
  480. Currently, the content above is provided by this template: <br><br>$path",
  481. TRIPAL_INFO,
  482. array('return_html' => 1)
  483. );
  484. }
  485. }
  486. //-----------------------
  487. // ADD THIS PANE TO THE TOC BY ORDER OF WEIGHT
  488. //-----------------------
  489. // set the weight of the TOC item and add it to our $toc array
  490. // for building of the TOC below
  491. $weight = 0;
  492. if (array_key_exists('#weight', $build[$key])) {
  493. $weight = $build[$key]['#weight'];
  494. }
  495. $toc[$weight][$toc_item_title] = $toc_item_link;
  496. //-----------------------
  497. // CREATE THE CONTENT PANE MARKUP
  498. //-----------------------
  499. // add a surrounding <div> box around the content
  500. $updated_markup = "
  501. <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
  502. <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
  503. $markup
  504. $path
  505. </div>
  506. </div>
  507. ";
  508. $build[$key]['#markup'] = $updated_markup;
  509. } // end foreach ($build as $key => $value) {
  510. } // end if ($build['#tripal_generic_node_template'] == TRUE) {
  511. //-----------------------
  512. // BUILD THE TABLE OF CONTENTS LINKS
  513. //-----------------------
  514. // first sort the links numerically by their weight
  515. ksort($toc, SORT_NUMERIC);
  516. $toc_html = '';
  517. foreach ($toc as $weight => $links) {
  518. // for links in the same weight, sort them alphabetically
  519. ksort($links);
  520. foreach ($links as $toc_item_title => $toc_item_link) {
  521. $toc_html .= $toc_item_link;
  522. }
  523. }
  524. $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-pane\" class=\"tripal-toc-pane\">$toc_html</div>";
  525. }
  526. /**
  527. *
  528. * @param $build
  529. */
  530. function tripal_core_get_toc_overrides($nid, $key, $node_type, $mode) {
  531. // Set override defaults
  532. $override_title = '';
  533. $override_weight = '';
  534. $override_hide = 0;
  535. if ($mode != "manage_type") {
  536. // First look to see if the node has customizations for this item.
  537. $toc_item_overrides = db_select('tripal_toc', 'tc')
  538. ->fields('tc', array('title', 'weight', 'hide'))
  539. ->condition('key', $key)
  540. ->condition('nid', $nid)
  541. ->execute()
  542. ->fetchObject();
  543. if ($toc_item_overrides) {
  544. $override_title = $toc_item_overrides->title;
  545. $override_weight = $toc_item_overrides->weight;
  546. $override_hide = $toc_item_overrides->hide;
  547. return array(
  548. 'title' => $override_title,
  549. 'weight' => $override_weight,
  550. 'hide' => $override_hide,
  551. );
  552. }
  553. }
  554. // If there are no specific node customizations then look to see if there
  555. // are customizations for this content type.
  556. $toc_item_overrides = db_select('tripal_toc', 'tc')
  557. ->fields('tc', array('title', 'weight', 'hide'))
  558. ->condition('node_type', $node_type)
  559. ->condition('key', $key)
  560. ->isNull('nid')
  561. ->execute()
  562. ->fetchObject();
  563. if ($toc_item_overrides) {
  564. $override_title = $toc_item_overrides->title;
  565. $override_weight = $toc_item_overrides->weight;
  566. $override_hide = $toc_item_overrides->hide;
  567. }
  568. return array(
  569. 'title' => $override_title,
  570. 'weight' => $override_weight,
  571. 'hide' => $override_hide,
  572. );
  573. }
  574. /**
  575. *
  576. */
  577. function tripal_core_content_type_toc_form($form, &$form_state, $content_type) {
  578. // Get the type details
  579. $all_types = node_type_get_types();
  580. $type_info = $all_types[$content_type];
  581. $form["#tree"] = TRUE;
  582. // Get a single node of this type so we can get all the possible content
  583. // for it.
  584. $sql = "SELECT nid FROM {node} WHERE type = :type LIMIT 1 OFFSET 0";
  585. $nid = db_query($sql, array(':type' => $content_type))->fetchField();
  586. if (!$nid) {
  587. $form["not_available"] = array(
  588. '#markup' => t('Please sync at least one %type_name record. A node
  589. must exist before customizations to the Table of Contents (TOC) can
  590. be performed.', array('%type_name' => $type_info->name)),
  591. );
  592. return $form;
  593. }
  594. // Load the node
  595. $node = node_load($nid);
  596. // Get the content array for this node, then pass it through the
  597. // tripal_core_node_view_alter which generates the TOC. After that
  598. // we can use the $build array to build the form. We have to add
  599. // a 'tripal_toc_mode' to the $node because we need to give the mode
  600. // to the tripal_core_node_view_build_toc function.
  601. $node->tripal_toc_mode = 'manage_type';
  602. node_build_content($node);
  603. $build = $node->content;
  604. $build["#node"] = $node;
  605. tripal_core_node_view_alter($build);
  606. $form["instructions"] = array(
  607. '#type' => 'fieldset',
  608. '#collapsed' => TRUE,
  609. '#collapsible' => TRUE,
  610. '#title' => 'Instructions',
  611. );
  612. $form["instructions"]["main"] = array(
  613. '#markup' => '</p>' . t('Below is a list of the titles of
  614. content panes that can appear on all %type_name pages. You may rename
  615. the titles or drag and drop them to change the order. Content that appears
  616. only on a single page can not be ordered here, but must be ordered using
  617. the TOC tab on the page itself. If a page has customized TOC settings
  618. then those settings will take precedent over these.',
  619. array('%type_name' => $type_info->name)) . '</p>' .
  620. '<p>' . t('The list below shows all possible content
  621. panes that can appear. However, those without content are automatically
  622. hidden and do not appear in the TOC.' . '</p>'),
  623. );
  624. $form['content_type'] = array(
  625. '#type' => 'value',
  626. '#value' => $content_type,
  627. );
  628. // Iterate through the built items and add form elemetns for each one.
  629. foreach(element_children($build) as $key) {
  630. $element = $build[$key];
  631. if (array_key_exists('#tripal_toc_id', $element)) {
  632. $toc_id = $element['#tripal_toc_id'];
  633. $toc_title = $element['#tripal_toc_title'];
  634. $toc_weight = $element['#weight'];
  635. $toc_hide = $element['#hide'];
  636. $form['toc_items'][$toc_id]['title'] = array(
  637. '#type' => 'textfield',
  638. '#default_value' => $toc_title,
  639. );
  640. $form['toc_items'][$toc_id]['hide'] = array(
  641. '#type' => 'checkbox',
  642. '#default_value' => $toc_hide,
  643. );
  644. $form['toc_items'][$toc_id]['weight'] = array(
  645. '#type' => 'textfield',
  646. '#default_value' => $toc_weight,
  647. '#attributes' => array(
  648. 'class' => array('tripal-node-toc-items-weights'),
  649. ),
  650. '#size' => 5,
  651. );
  652. }
  653. }
  654. $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
  655. $form['submit'] = array(
  656. '#type' => 'submit',
  657. '#name' => 'toc_submit',
  658. '#value' => t('Submit'),
  659. );
  660. $form['unset'] = array(
  661. '#type' => 'submit',
  662. '#name' => 'toc_unset',
  663. '#value' => t('Reset to Defaults'),
  664. );
  665. return $form;
  666. }
  667. /**
  668. * Implements hook_validate for the tripal_core_node_toc_form.
  669. */
  670. function tripal_core_content_type_toc_form_validate($form, &$form_state) {
  671. $toc_items = $form_state['values']['toc_items'];
  672. // Iterate through the TOC items and validate.
  673. foreach ($toc_items as $toc_id => $item) {
  674. if (!$item['title']) {
  675. form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
  676. }
  677. }
  678. }
  679. /**
  680. * Implements hook_submit for the tripal_core_node_toc_form.
  681. */
  682. function tripal_core_content_type_toc_form_submit($form, &$form_state) {
  683. $toc_items = $form_state['values']['toc_items'];
  684. $content_type = $form_state['values']['content_type'];
  685. if ($form_state['clicked_button']['#name'] == "toc_submit") {
  686. $transaction = db_transaction();
  687. try {
  688. // First delete any settings for this content type
  689. db_delete('tripal_toc')
  690. ->condition('node_type', $content_type)
  691. ->isNull('nid')
  692. ->execute();
  693. // Second add in any new settings for this node
  694. foreach ($toc_items as $toc_id => $item) {
  695. db_insert('tripal_toc')
  696. ->fields(array(
  697. 'node_type' => $content_type,
  698. 'key' => $toc_id,
  699. 'title' => $item['title'],
  700. 'weight' => $item['weight'],
  701. 'hide' => $item['hide'],
  702. ))
  703. ->execute();
  704. }
  705. drupal_set_message("TOC changes successfully applied to this content type.");
  706. }
  707. catch (Exception $e) {
  708. $transaction->rollback();
  709. drupal_set_message("Failed to apply TOC changes.", "error");
  710. }
  711. }
  712. if ($form_state['clicked_button']['#name'] == "toc_unset") {
  713. $transaction = db_transaction();
  714. try {
  715. // First delete any settings for this node
  716. db_delete('tripal_toc')
  717. ->condition('node_type', $content_type)
  718. ->isNull('nid')
  719. ->execute();
  720. drupal_set_message("The TOC is reset to defaults for this content type.");
  721. }
  722. catch (Exception $e) {
  723. $transaction->rollback();
  724. drupal_set_message("Failed to apply TOC changes.", "error");
  725. }
  726. }
  727. }