tripal_core.toc.inc 26 KB

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