You are here

function homebox_check_page_object in Homebox 7.2

Same name and namespace in other branches
  1. 6.3 homebox.module \homebox_check_page_object()
  2. 6.2 homebox.module \homebox_check_page_object()
  3. 7.3 homebox.module \homebox_check_page_object()

Validation helper to check a page object

Parameters

$homebox: A page object.

$name: Optionally specify and override the machine name of the page

$element: Optionally specify a form element name to be used to throw form errors

Return value

A complete page object, or FALSE if data was invalid

4 calls to homebox_check_page_object()
homebox_admin_page_validate in ./homebox.admin.inc
Validation functino for the admin page form.
homebox_get_page in ./homebox.module
Helper function to fetch a page from the database or from a module implementing hook_homebox()
homebox_pages in ./homebox.module
Retrieve an array of all available pages either in the database or by hook_homebox()
homebox_save_page in ./homebox.module
Helper function to save an existing page

File

./homebox.module, line 1065
Homebox main file, takes care of global functions settings constants, etc.

Code

function homebox_check_page_object($homebox, $name = NULL, $element = NULL) {

  // Whether or not the import is valid
  $status = TRUE;

  // Check if data was a valid object
  if (!is_object($homebox)) {
    $status = FALSE;
  }
  else {

    // Check individual settings
    foreach ($homebox->settings as $key => $value) {
      switch ($key) {
        case 'regions':

          // Only allow numbers 1-9
          if ($value > 9 || $value < 1) {
            $status = FALSE;
            break 2;
          }
        case 'menu':
        case 'enabled':
        case 'auto_save':
        case 'cache':
        case 'full':
        case 'color':

          // Check that the previous are numeric values
          if (!is_numeric($value)) {
            $status = FALSE;
            break 2;
          }
          break;
        case 'title':

          // Filter title and make sure it still exists afterwards
          $homebox->settings[$key] = filter_xss($value);
          if (!$homebox->settings[$key]) {
            $status = FALSE;
            break 2;
          }
          break;
        case 'path':
          if (!homebox_check_path($value, NULL, $element)) {
            $status = FALSE;
            break 2;
          }
          break;
        case 'colors':
        case 'roles':
        case 'blocks':
          if (!is_array($value)) {
            $status = FALSE;
            break 2;
          }
          break;
        case 'widths':
          if ($homebox->settings['widths']) {
            if (!is_array($homebox->settings['widths']) || count($homebox->settings['widths']) > $homebox->settings['regions']) {
              $status = FALSE;
              break 2;
            }
            foreach ($homebox->settings['widths'] as $width) {
              if (!is_numeric($width) || $width > 100 || $width < 0) {
                $status = FALSE;
                break 3;
              }
            }
          }
          break;
      }
    }

    // If name is explicitly specified, used it
    $homebox->name = $name ? $name : $homebox->name;

    // Check name
    if ($status && $homebox->name && !homebox_check_name($homebox->name, $element)) {
      $status = FALSE;
    }

    // Make sure the required values actually exist
    $required_keys = array(
      'path',
      'blocks',
      'title',
      'regions',
    );
    if ($status) {
      foreach ($required_keys as $key) {
        if (!array_key_exists($key, $homebox->settings)) {
          $status = FALSE;
          break;
        }
      }
    }
  }

  // If the import isn't valid, and form element provided,
  // then flag a form error
  if (!$status && $element) {
    form_set_error($element, t('Invalid import data provided.'));
  }
  return $status ? $homebox : FALSE;
}