You are here

function autoassignrole_admin_form_validate in Auto Assign Role 6

Same name and namespace in other branches
  1. 6.2 autoassignrole-admin.inc \autoassignrole_admin_form_validate()

File

./autoassignrole-admin.inc, line 256
The autoassignrole-admin.inc file

Code

function autoassignrole_admin_form_validate($form_id, &$form_state) {

  // path based role validation
  $roles = user_roles(TRUE);
  unset($roles[DRUPAL_AUTHENTICATED_RID]);

  // Create a registration array and store all paths that are set to replace
  // the default user/register page
  $registration = array();
  foreach ($roles as $rid => $role) {
    if ($form_state['values']["path_active_{$rid}"]) {
      $format = $form_state['values']["path_format_{$rid}"];
      $path = $form_state['values']["path_{$rid}"];
      $title = $form_state['values']["path_title_{$rid}"];
      $description = $form_state['values']["path_description_{$rid}"];
      $display = $form_state['values']["path_display_{$rid}"];
      if ($form_state['values']["path_registration_{$rid}"]) {
        $registration[] = $rid;

        // If this is going to function as the default user registration page it
        // can not have a Display Method of Tabs on registration page
        if ($display == 1) {
          form_set_error("path_display_{$rid}", t('If a path serves as the default user registration page it can not have a Display Method of Tabs on registration page'));
        }
      }

      // Check that a display option is set
      if ($display == '') {
        form_set_error("path_display_{$rid}", t('Please select a valid display method for enabled URL role assignments'));
      }

      // check that path exists, is syntactically correct, and that it is not
      // an existing Drupal path
      if ($path) {
        if (ereg("[^A-Za-z0-9/_-]+", $path) || strpos(trim($path), '/') === 0) {
          form_set_error("path_{$rid}", '\'' . check_plain($path) . '\' ' . t('is not a valid path'));
        }
        else {
          $menu_link = db_fetch_object(db_query("SELECT count(mlid) AS count FROM {menu_links} where link_path like '%s'", $path));
          if (drupal_lookup_path('source', $path) || $menu_link->count > 0) {

            // the menu item exists so need to check if the path has
            // something other than autoassign_role_path registered
            // otherwise throw an error
            $menu_item = menu_get_item($path);
            if (!isset($menu_item['page_callback']) && $menu_item['page_callback'] != 'autoassign_role_path') {
              form_set_error("path_{$rid}", '\'' . check_plain($path) . '\' ' . t('is already in use as a path'));
            }
          }

          // if the menu items are being displayed as tabs on user/* the path can
          // not be constructed in a way that makes the tab a child of an existing
          // tab.  register/* and password/* will fail
          if ($display == 1 && (strpos(trim($path), 'register') === 0 || strpos(trim($path), 'password') === 0)) {
            form_set_error("path_{$rid}", '\'' . check_plain($path) . '\' ' . t('can only be used as a path if the Display Method is not Tabs on registration page.'));
          }
        }
      }
      else {
        form_set_error("path_{$rid}", t('Please enter a valid path for enabled URL role assignments'));
      }

      // If the display option is a menu item or links we need a title to display
      if ($display == '0' || $display == '1') {
        $title = ereg_replace('[:space:]', '', $title);
        if (!strlen($title)) {
          form_set_error("path_title_{$rid}", t('Please enter a valid title for enabled URL role assignments'));
        }
      }
    }
  }

  // If the registration array has more than one item in it throw an error
  if (count($registration) > 1) {
    form_set_error(NULL, t('Only one path can can serve as the default user register page'));
  }
  if (count($registration) > 0 && !empty($form_state['values']['node_user_register'])) {
    form_set_error('node_user_register', t('You can not replace the user register page with a node if a path has been designated as the user register page.'));
  }

  // User chooses their own role validation
  if ($form_state['values']['user_active'] == 1) {
    if ($form_state['values']['user_multiple'] == 1 && $form_state['values']['user_selection'] == 0) {
      form_set_error('user_selection', t('If a user can select multiple roles they can not use Radio Buttons as the selection method.'));
    }
    if (strlen(trim($form_state['values']['user_title'])) == 0) {
      form_set_error('user_title', t('Enter the title of the form fields the user will be presented with.'));
    }
  }
}