You are here

function page_manager_page_form_basic_validate in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 page_manager/plugins/tasks/page.admin.inc \page_manager_page_form_basic_validate()

Validate the basic form.

2 calls to page_manager_page_form_basic_validate()
page_manager_page_form_clone_validate in page_manager/plugins/tasks/page.admin.inc
Validate clone page form.
page_manager_page_import_subtask_validate in page_manager/plugins/tasks/page.admin.inc
Ensure we got a valid page.

File

page_manager/plugins/tasks/page.admin.inc, line 486
Administrative functions for the page subtasks.

Code

function page_manager_page_form_basic_validate(&$form, &$form_state) {

  // Ensure path is unused by other pages.
  $page = $form_state['page']->subtask['subtask'];
  $name = !empty($form_state['values']['name']) ? $form_state['values']['name'] : $page->name;
  if (empty($name)) {
    form_error($form['name'], t('Name is required.'));
  }

  // If this is new, make sure the name is unique:
  if (empty($page->name)) {
    $test = page_manager_page_load($name);
    if ($test) {
      form_error($form['name'], t('That name is used by another page: @page', array(
        '@page' => $test->admin_title,
      )));
    }

    // Ensure name fits the rules:
    if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
      form_error($form['name'], t('Page name must be alphanumeric or underscores only.'));
    }
  }
  $pages = page_manager_page_load_all();
  foreach ($pages as $test) {
    if ($test->name != $name && $test->path == $form_state['values']['path'] && empty($test->disabled)) {
      form_error($form['path'], t('That path is used by another page: @page', array(
        '@page' => $test->admin_title,
      )));
    }
  }

  // Ensure path is unused by things NOT pages. We do the double check because
  // we're checking against our page callback.
  $path = array();
  if (empty($form_state['values']['path'])) {
    form_error($form['path'], t('Path is required.'));

    // Stop processing here if there is no path.
    return;
  }
  $found = FALSE;
  $error = FALSE;
  foreach (explode('/', $form_state['values']['path']) as $position => $bit) {
    if (!isset($bit) || $bit === '') {
      continue;
    }
    if ($bit == '%' || $bit == '!') {
      form_error($form['path'], t('You cannot have an unnamed placeholder (% or ! by itself). Please name your placeholder by adding a short piece of descriptive text to the % or !, such as %user or %node.'));
    }
    if ($bit[0] == '%') {
      if ($found) {
        form_error($form['path'], t('You cannot have a dynamic path element after an optional path element.'));
      }
      if ($position == 0) {
        form_error($form['path'], t('The first element in a path may not be dynamic.'));
      }
      $path[] = '%';
    }
    elseif ($bit[0] == '!') {
      $found = TRUE;
    }
    else {
      if ($found) {
        form_error($form['path'], t('You cannot have a static path element after an optional path element.'));
      }
      $path[] = $bit;
    }
  }

  // Check to see if something that isn't a page manager page is using the path.
  $path = implode('/', $path);
  $result = db_query('SELECT * FROM {menu_router} WHERE path = :path', array(
    ':path' => $path,
  ));
  foreach ($result as $router) {
    if ($router->page_callback != 'page_manager_page_execute') {
      form_error($form['path'], t('That path is already in use. This system cannot override existing paths.'));
    }
  }

  // Ensure the path is not already an alias to something else.
  if (strpos($path, '%') === FALSE) {
    $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(
      ':path' => $path,
    ))
      ->fetchObject();
    if ($alias) {
      form_error($form['path'], t('That path is currently assigned to be an alias for @alias. This system cannot override existing aliases.', array(
        '@alias' => $alias->source,
      )));
    }
  }
  else {
    if (!empty($form_state['values']['frontpage'])) {
      form_error($form['path'], t('You cannot make this page your site home page if it uses % placeholders.'));
    }
  }

  // Ensure path is properly formed.
  $args = page_manager_page_get_named_arguments($form_state['values']['path']);
  if ($invalid_args = array_filter($args, 'page_manager_page_form_basic_validate_filter')) {
    foreach ($invalid_args as $arg => $position) {
      form_error($form['path'], t('Duplicated argument %arg', array(
        '%arg' => $arg,
      )));
    }
  }
  if (isset($args['%'])) {
    form_error($form['path'], t('Invalid arg <em>%</em>. All arguments must be named with keywords.'));
  }
  $form_state['arguments'] = $args;
}