You are here

function ctools_context_node_settings_form_validate in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 plugins/contexts/node.inc \ctools_context_node_settings_form_validate()

Validate a node.

1 string reference to 'ctools_context_node_settings_form_validate'
node.inc in plugins/contexts/node.inc
Plugin to provide a node context. A node context is a node wrapped in a context object that can be utilized by anything that accepts contexts.

File

plugins/contexts/node.inc, line 113
Plugin to provide a node context. A node context is a node wrapped in a context object that can be utilized by anything that accepts contexts.

Code

function ctools_context_node_settings_form_validate($form, &$form_values, &$form_state) {

  // Validate the autocomplete
  if (empty($form_values['nid']) && empty($form_values['node'])) {
    form_error($form['node'], t('You must select a node.'));
    return;
  }
  if (empty($form_values['node'])) {
    return;
  }
  $nid = $form_values['node'];
  $preg_matches = array();
  $match = preg_match('/\\[nid: (\\d+)\\]/', $nid, $preg_matches);
  if (!$match) {
    $match = preg_match('/^nid: (\\d+)/', $nid, $preg_matches);
  }
  if ($match) {
    $nid = $preg_matches[1];
  }
  if (is_numeric($nid)) {
    $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $nid));
  }
  else {
    $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE LOWER(n.title) = LOWER('%s')"), $nid));
  }

  // Do not allow unpublished nodes to be selected by unprivileged users
  if (!$node || empty($node->status) && !user_access('administer nodes')) {
    form_error($form['node'], t('Invalid node selected.'));
  }
  else {
    form_set_value($form['nid'], $node->nid, $form_state);

    // $form_values['nid'] = $node->nid;
  }
}