123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816 |
- <?php
- function tripal_core_node_toc_form($form, &$form_state, $node) {
-
- $all_types = node_type_get_types();
- $type_info = $all_types[$node->type];
- $form["#tree"] = TRUE;
- $form["instructions"] = array(
- '#type' => 'fieldset',
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#title' => 'Instructions',
- );
- $admin_link = l(
- $type_info->name . " TOC administrative page",
- "admin/tripal/legacy/" . $type_info->module . "/" . $node->type . "toc",
- array('attributes' => array('target' => '_blank'))
- );
- $form["instructions"]["main"] = array(
- '#markup' => '<p>' . t("Below is a list of the titles of
- content panes that can appear on this page. These titles appear in the
- the following order in the Table of Contents (TOC). You may rename
- the titles or drag and drop them to change the order. <b>Any changes will
- only apply to this page</b>. If you would like to make changes apply to multiple
- pages of the same type, please visit the $admin_link. ") . '</p>' .
- '<p>' . t('The list below shows all possible content panes that can appear.
- However, those without content are automatically hidden and do not
- appear in the TOC.' . '</p>'),
- );
- $form['node'] = array(
- '#type' => 'value',
- '#value' => $node,
- );
-
-
-
-
-
- $node->tripal_toc_mode = 'manage_node';
- node_build_content($node);
- $build = $node->content;
- $build["#node"] = $node;
- tripal_core_node_view_alter($build);
-
- foreach(element_children($build) as $key) {
- $element = $build[$key];
- if (array_key_exists('#tripal_toc_id', $element)) {
- $toc_id = $element['#tripal_toc_id'];
- $toc_title = $element['#tripal_toc_title'];
- $toc_weight = $element['#weight'];
- $toc_hide = $element['#hide'];
-
-
-
- $is_link = array_key_exists('#is_link', $element) ? $element['#is_link'] : FALSE;
- if (!$is_link) {
- $form['toc_items'][$toc_id]['title'] = array(
- '#type' => 'textfield',
- '#default_value' => $toc_title,
- );
- }
- else {
- $form['toc_items'][$toc_id]['title'] = array(
- '#markup' => '<i>link title:</i> ' . $toc_title,
- '#value' => $toc_title,
- );
- }
- $form['toc_items'][$toc_id]['hide'] = array(
- '#type' => 'checkbox',
- '#default_value' => $toc_hide,
- );
- $form['toc_items'][$toc_id]['weight'] = array(
- '#type' => 'textfield',
- '#default_value' => $toc_weight,
- '#attributes' => array(
- 'class' => array('tripal-node-toc-items-weights'),
- ),
- '#size' => 5,
- );
- }
- }
- $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
- $form['submit'] = array(
- '#type' => 'submit',
- '#name' => 'toc_submit',
- '#value' => t('Submit'),
- );
- $form['unset'] = array(
- '#type' => 'submit',
- '#name' => 'toc_unset',
- '#value' => t('Unset Node Customizations'),
- );
-
- $sql = "SELECT count(*) FROM {tripal_toc} where nid = :nid";
- $managed_items = db_query($sql, array(':nid' => $node->nid))->fetchField();
- if ($managed_items > 0) {
- $form['is_managed'] = array(
- '#markup' => '<p><font color="red">' .
- t('This page currently has customiations to the TOC.</font> This means
- that any customzations for the content type are overriden. Click the
- "Unset Node Customizations" button above to remove page-level
- customizations and default to the content type settings.') . '</p>',
- );
- }
- return $form;
- }
- function theme_tripal_node_toc_items_table($variables) {
- $elements = $variables['element'];
- $toc_items = array();
-
-
- foreach(element_children($elements) as $key) {
- $toc_items[] = $elements[$key];
- }
- usort($toc_items, 'theme_tripal_node_sort_toc_items');
-
- $headers = array('Content Pane Name', 'Hide', 'Weight');
-
- $rows = array();
- foreach ($toc_items as $key => $item) {
- $rows[] = array(
- 'data' => array(
- drupal_render($item['title']),
- drupal_render($item['hide']),
- drupal_render($item['weight']),
- ),
- 'class' => array('draggable'),
- );
- }
-
- $table = array(
- 'header' => $headers,
- 'rows' => $rows,
- 'attributes' => array("id" => 'tripal-node-toc-items-table'),
- 'sticky' => TRUE,
- 'caption' => t('Content Panes Available in the TOC'),
- 'colgroups' => array(),
- 'empty' => t('There are no content panes for this page'),
- );
- drupal_add_tabledrag('tripal-node-toc-items-table', 'order', 'sibling', 'tripal-node-toc-items-weights');
- return theme_table($table);
- }
- function theme_tripal_node_sort_toc_items($a, $b) {
- if ($a['weight']['#value'] < $b['weight']['#value']) {
- return -1;
- }
- if ($a['weight']['#value'] > $b['weight']['#value']) {
- return 1;
- }
- if ($a['weight']['#value'] == $b['weight']['#value']) {
- return strcmp($a['title']['#value'], $b['title']['#value']);
- }
- }
- function tripal_core_node_toc_form_validate($form, &$form_state) {
- $toc_items = $form_state['values']['toc_items'];
-
- foreach ($toc_items as $toc_id => $item) {
- if (array_key_exists('title', $item) and !$item['title']) {
- form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
- }
- }
- }
- function tripal_core_node_toc_form_submit($form, &$form_state) {
- $toc_items = $form_state['values']['toc_items'];
- $node = $form_state['values']['node'];
- if ($form_state['clicked_button']['#name'] == "toc_submit") {
- $transaction = db_transaction();
- try {
-
- db_delete('tripal_toc')
- ->condition('nid', $node->nid)
- ->execute();
-
- foreach ($toc_items as $toc_id => $item) {
- db_insert('tripal_toc')
- ->fields(array(
- 'node_type' => $node->type,
- 'key' => $toc_id,
- 'title' => array_key_exists('title', $item) ? $item['title'] : '',
- 'weight' => $item['weight'],
- 'nid' => $node->nid,
- 'hide' => $item['hide'],
- ))
- ->execute();
- }
- drupal_set_message("TOC changes successfully applied to this node only.");
- }
- catch (Exception $e) {
- $transaction->rollback();
- drupal_set_message("Failed to apply TOC changes.", "error");
- }
- }
- if ($form_state['clicked_button']['#name'] == "toc_unset") {
- $transaction = db_transaction();
- try {
-
- db_delete('tripal_toc')
- ->condition('nid', $node->nid)
- ->execute();
- drupal_set_message("TOC is no longer customized specifically for this page. Now using the content type settings.");
- }
- catch (Exception $e) {
- $transaction->rollback();
- drupal_set_message("Failed to apply TOC changes.", "error");
- }
- }
- }
- function tripal_core_node_view_build_toc(&$build) {
- global $theme;
-
- if ($build['#view_mode'] != 'full' OR !array_key_exists('#tripal_generic_node_template', $build)) {
- return;
- }
- $node_type = $build["#node"]->type;
- $nid = $build["#node"]->nid;
-
-
-
-
-
-
-
-
-
-
- $mode = isset($build["#node"]->tripal_toc_mode) ? $build["#node"]->tripal_toc_mode : "display";
- $cache = cache_get("theme_registry:$theme", 'cache');
- $node = $build['#node'];
- $toc = array();
- $toc_html = '';
-
-
-
- if ($build['#tripal_generic_node_template'] == TRUE) {
-
-
- $markup = array();
- foreach ($build as $key => $value) {
- $value = $build[$key];
-
- if ($key == 'body') {
- continue;
- }
-
- if (preg_match('/^#/', $key) or $key == 'tripal_toc' or $key == 'links') {
- continue;
- }
-
-
-
-
-
- if ($mode == "manage_type" and (
- $key == "field_resource_links" or
- $key == "field_resource_titles" or
- $key == "field_resource_blocks")) {
- unset($build[$key]);
- continue;
- }
- if ($key == "field_resource_links") {
-
- foreach (element_children($build[$key]) as $index) {
- $element = $build[$key][$index];
- $weight = 0;
- $hide = 0;
- $toc_item_id = "resource-link-$index";
-
- $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
- $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
- $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
-
-
- if ($mode == "display" and $overrides['hide'] == 1) {
- continue;
- }
-
-
- $base_table = preg_replace('/^chado_(.*)$/', '\1', $node_type);
- $tokens = chado_node_generate_tokens($base_table);
- $markup = $element['#markup'];
-
- if (preg_match_all('/\[[^]]+\]/', $markup, $used_tokens)) {
-
- foreach ($used_tokens[0] as $token) {
- $token_info = $tokens[$token];
- if (!empty($token_info)) {
- $value = chado_get_token_value($token_info, $node);
- $markup = str_replace($token, $value, $markup);
- }
- }
- $element['#markup'] = $markup;
- }
-
- $parts = explode("|", $element['#markup']);
- if (count($parts) == 2) {
- $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>";
- }
- else {
- $toc[$weight][$parts[0]] = "<div id=\"$toc_item_id\" class=\"tripal_toc_list_item\">" . $element['#markup'] . "</div>";
- }
-
-
-
- $build[$toc_item_id]['#toc_handled'] = TRUE;
- $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
- $build[$toc_item_id]['#tripal_toc_title'] = $parts[0];
- $build[$toc_item_id]['#weight'] = $weight;
- $build[$toc_item_id]['#hide'] = $hide;
- $build[$toc_item_id]['#is_link'] = TRUE;
- }
-
-
- unset($build[$key]);
- continue;
- }
- if ($key == "field_resource_titles") {
-
-
- continue;
- }
- if ($key == "field_resource_blocks") {
- foreach (element_children($build[$key]) as $index) {
-
- $weight = 0;
- $hide = 0;
- $markup = $build[$key][$index]["#markup"];
- $toc_item_id = "resource-$index";
-
- $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
-
-
- if ($mode == "display" and $overrides['hide'] == 1) {
- continue;
- }
- $toc_item_title = $build["field_resource_titles"][$index]["#markup"];
- $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
- $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
- $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
- $updated_markup = "
- <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
- <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
- $markup
- </div>
- </div>
- ";
- $build[$toc_item_id]['#markup'] = $updated_markup;
- $build[$toc_item_id]['#toc_handled'] = TRUE;
- $build[$toc_item_id]['#tripal_toc_id'] = $toc_item_id;
- $build[$toc_item_id]['#tripal_toc_title'] = $toc_item_title;
- $build[$toc_item_id]['#weight'] = $weight;
- $build[$toc_item_id]['#hide'] = $hide;
-
- $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>
- ";
- $toc[$weight][$toc_item_title] = $toc_item_link;
- }
-
- unset($build[$key]);
- unset($build["field_resource_titles"]);
- continue;
- }
-
-
-
-
- if (array_key_exists('#toc_handled', $build[$key]) and $build[$key]['#toc_handled'] == TRUE) {
- continue;
- }
-
-
-
-
- $toc_item_title = $key;
- $toc_item_id = $key;
- $toc_item_link = '';
- $weight = 0;
- $hide = 0;
-
-
- if (array_key_exists('#tripal_toc_title', $build[$key])) {
- $toc_item_title = $build[$key]['#tripal_toc_title'];
- }
-
- if (array_key_exists('#title', $build[$key])) {
- $toc_item_title = $build[$key]['#title'];
- }
- $toc_item_title = ucwords($toc_item_title);
- if (array_key_exists('#weight', $build[$key])) {
- $weight = $build[$key]['#weight'];
- }
- if (array_key_exists('#tripal_toc_id', $build[$key])) {
- $toc_item_id = $build[$key]['#tripal_toc_id'];
- }
-
- $overrides = tripal_core_get_toc_overrides($nid, $toc_item_id, $node_type, $mode);
-
-
- if ($mode == "display" and $overrides['hide'] == 1) {
- unset($build[$key]);
- continue;
- }
-
- $toc_item_title = $overrides['title'] ? $overrides['title'] : $toc_item_title;
- $weight = $overrides['weight'] ? $overrides['weight'] : $weight;
- $hide = $overrides['hide'] ? $overrides['hide'] : $hide;
- $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>";
-
-
-
- $markup = '';
-
-
-
- if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
- $markup = $build[$key]['#markup'];
- }
-
- elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
- $markup = $build[$key]['#markup'];
- }
-
-
- if (!$markup) {
- $markup = trim(render($build[$key]));
- }
-
- $build[$key] = array(
- '#markup' => $markup,
- '#tripal_toc_id' => $toc_item_id,
- '#tripal_toc_title' => $toc_item_title,
- '#weight' => $weight,
- '#hide' => $hide,
- );
-
- if (!$markup) {
- continue;
- }
-
-
-
-
- $path = '';
- if (!array_key_exists('#tripal_template_show', $build[$key]) or
- $build[$key]['#tripal_template_show'] == TRUE) {
- if ($cache and array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
- $path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
- $path = tripal_set_message("Administrators, you can
- customize the way the content above is presented. Tripal provides a template
- file for each pane of content. To customize, copy the template file to your
- site's default theme, edit then " .
- l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
- Currently, the content above is provided by this template: <br><br>$path",
- TRIPAL_INFO,
- array('return_html' => 1)
- );
- }
- }
-
-
-
-
-
- $weight = 0;
- if (array_key_exists('#weight', $build[$key])) {
- $weight = $build[$key]['#weight'];
- }
- $toc[$weight][$toc_item_title] = $toc_item_link;
-
-
-
-
- $updated_markup = "
- <div id=\"$toc_item_id-tripal-data-pane\" class=\"tripal-data-pane\">
- <div class=\"$toc_item_id-tripal-data-pane-title tripal-data-pane-title\">$toc_item_title</div>
- $markup
- $path
- </div>
- </div>
- ";
- $build[$key]['#markup'] = $updated_markup;
- }
- }
-
-
-
-
- ksort($toc, SORT_NUMERIC);
- $toc_html = '';
- foreach ($toc as $weight => $links) {
-
- ksort($links);
- foreach ($links as $toc_item_title => $toc_item_link) {
- $toc_html .= $toc_item_link;
- }
- }
- $build['tripal_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-pane\" class=\"tripal-toc-pane\">$toc_html</div>";
- }
- function tripal_core_get_toc_overrides($nid, $key, $node_type, $mode) {
-
- $override_title = '';
- $override_weight = '';
- $override_hide = 0;
- if ($mode != "manage_type") {
-
- $toc_item_overrides = db_select('tripal_toc', 'tc')
- ->fields('tc', array('title', 'weight', 'hide'))
- ->condition('key', $key)
- ->condition('nid', $nid)
- ->execute()
- ->fetchObject();
- if ($toc_item_overrides) {
- $override_title = $toc_item_overrides->title;
- $override_weight = $toc_item_overrides->weight;
- $override_hide = $toc_item_overrides->hide;
- return array(
- 'title' => $override_title,
- 'weight' => $override_weight,
- 'hide' => $override_hide,
- );
- }
- }
-
-
- $toc_item_overrides = db_select('tripal_toc', 'tc')
- ->fields('tc', array('title', 'weight', 'hide'))
- ->condition('node_type', $node_type)
- ->condition('key', $key)
- ->isNull('nid')
- ->execute()
- ->fetchObject();
- if ($toc_item_overrides) {
- $override_title = $toc_item_overrides->title;
- $override_weight = $toc_item_overrides->weight;
- $override_hide = $toc_item_overrides->hide;
- }
- return array(
- 'title' => $override_title,
- 'weight' => $override_weight,
- 'hide' => $override_hide,
- );
- }
- function tripal_core_content_type_toc_form($form, &$form_state, $content_type) {
-
- $all_types = node_type_get_types();
- $type_info = $all_types[$content_type];
- $form["#tree"] = TRUE;
-
-
- $sql = "SELECT nid FROM {node} WHERE type = :type LIMIT 1 OFFSET 0";
- $nid = db_query($sql, array(':type' => $content_type))->fetchField();
- if (!$nid) {
- $form["not_available"] = array(
- '#markup' => t('Please sync at least one %type_name record. A node
- must exist before customizations to the Table of Contents (TOC) can
- be performed.', array('%type_name' => $type_info->name)),
- );
- return $form;
- }
-
- $node = node_load($nid);
-
-
-
-
-
- $node->tripal_toc_mode = 'manage_type';
- node_build_content($node);
- $build = $node->content;
- $build["#node"] = $node;
- tripal_core_node_view_alter($build);
- $form["instructions"] = array(
- '#type' => 'fieldset',
- '#collapsed' => TRUE,
- '#collapsible' => TRUE,
- '#title' => 'Instructions',
- );
- $form["instructions"]["main"] = array(
- '#markup' => '</p>' . t('Below is a list of the titles of
- content panes that can appear on all %type_name pages. You may rename
- the titles or drag and drop them to change the order. Content that appears
- only on a single page can not be ordered here, but must be ordered using
- the TOC tab on the page itself. If a page has customized TOC settings
- then those settings will take precedent over these.',
- array('%type_name' => $type_info->name)) . '</p>' .
- '<p>' . t('The list below shows all possible content
- panes that can appear. However, those without content are automatically
- hidden and do not appear in the TOC.' . '</p>'),
- );
- $form['content_type'] = array(
- '#type' => 'value',
- '#value' => $content_type,
- );
-
- foreach(element_children($build) as $key) {
- $element = $build[$key];
- if (array_key_exists('#tripal_toc_id', $element)) {
- $toc_id = $element['#tripal_toc_id'];
- $toc_title = $element['#tripal_toc_title'];
- $toc_weight = $element['#weight'];
- $toc_hide = $element['#hide'];
- $form['toc_items'][$toc_id]['title'] = array(
- '#type' => 'textfield',
- '#default_value' => $toc_title,
- );
- $form['toc_items'][$toc_id]['hide'] = array(
- '#type' => 'checkbox',
- '#default_value' => $toc_hide,
- );
- $form['toc_items'][$toc_id]['weight'] = array(
- '#type' => 'textfield',
- '#default_value' => $toc_weight,
- '#attributes' => array(
- 'class' => array('tripal-node-toc-items-weights'),
- ),
- '#size' => 5,
- );
- }
- }
- $form['toc_items']['#theme'] = 'tripal_node_toc_items_table';
- $form['submit'] = array(
- '#type' => 'submit',
- '#name' => 'toc_submit',
- '#value' => t('Submit'),
- );
- $form['unset'] = array(
- '#type' => 'submit',
- '#name' => 'toc_unset',
- '#value' => t('Reset to Defaults'),
- );
- return $form;
- }
- function tripal_core_content_type_toc_form_validate($form, &$form_state) {
- $toc_items = $form_state['values']['toc_items'];
-
- foreach ($toc_items as $toc_id => $item) {
- if (!$item['title']) {
- form_set_error('toc_items][' . $toc_id, "Please provide a valid title.");
- }
- }
- }
- function tripal_core_content_type_toc_form_submit($form, &$form_state) {
- $toc_items = $form_state['values']['toc_items'];
- $content_type = $form_state['values']['content_type'];
- if ($form_state['clicked_button']['#name'] == "toc_submit") {
- $transaction = db_transaction();
- try {
-
- db_delete('tripal_toc')
- ->condition('node_type', $content_type)
- ->isNull('nid')
- ->execute();
-
- foreach ($toc_items as $toc_id => $item) {
- db_insert('tripal_toc')
- ->fields(array(
- 'node_type' => $content_type,
- 'key' => $toc_id,
- 'title' => $item['title'],
- 'weight' => $item['weight'],
- 'hide' => $item['hide'],
- ))
- ->execute();
- }
- drupal_set_message("TOC changes successfully applied to this content type.");
- }
- catch (Exception $e) {
- $transaction->rollback();
- drupal_set_message("Failed to apply TOC changes.", "error");
- }
- }
- if ($form_state['clicked_button']['#name'] == "toc_unset") {
- $transaction = db_transaction();
- try {
-
- db_delete('tripal_toc')
- ->condition('node_type', $content_type)
- ->isNull('nid')
- ->execute();
- drupal_set_message("The TOC is reset to defaults for this content type.");
- }
- catch (Exception $e) {
- $transaction->rollback();
- drupal_set_message("Failed to apply TOC changes.", "error");
- }
- }
- }
|