You are here

function dynamic_banner_admin_form_validate in Dynamic Banner 7

Same name and namespace in other branches
  1. 6 includes/callbacks.inc \dynamic_banner_admin_form_validate()
  2. 7.2 dynamic_banner.module \dynamic_banner_admin_form_validate()
  3. 8.x dynamic_banner.module \dynamic_banner_admin_form_validate()

Verify that the Banner is valid It makes sure that the sql does not throw errors

File

includes/callbacks.inc, line 577
Dynamic Banner Admin Pages and various other functions to make them work Most of the code in this file was derived from path module

Code

function dynamic_banner_admin_form_validate($form, &$form_state) {

  // For a banner to exist it needs a path that it is assigned to and an image, thats it.
  if (isset($form_state['values']['path']) && (isset($form_state['values']['image']) || isset($form_state['values']['imgurl']))) {
    $path = $form_state['values']['path'];
    if ($path != 'DEFAULT') {

      // check db before altering the path variable
      // check for more than one of the same path banners
      if (db_query("SELECT COUNT(path) FROM {dynamic_banner} WHERE path = :path", array(
        ':path' => $path,
      ))
        ->fetchField() > 1) {
        form_set_error('path', t('The path %path is already in use.', array(
          '%path' => $path,
        )));
        return;
      }

      // path is not clean at this point because of wildcard and random must chop those characters off
      // find the * or wildcard
      $wild_position = strrpos($path, "*");
      if ($wild_position !== FALSE) {
        $path = drupal_substr($path, 0, $wild_position);
      }

      // find the ! or random
      $rand_position = strrpos($path, "!");
      if ($rand_position !== FALSE) {
        $path = drupal_substr($path, 0, $rand_position);
      }
      if (drupal_lookup_path('source', $path)) {

        // We are making a new banner previous checks should be enough to deal with validation
        if ($dbid != 0) {
          return;
        }
      }
      else {
        form_set_error('path', t('The path %path is not known by drupal.', array(
          '%path' => $path,
        )));
        return;
      }
    }
  }
  else {
    form_set_error('path', t('There was a problem with the required fields please check the form and try again.'));
    return;
  }
}