You are here

function manualcrop_croptool_validate in Manual Crop 7

Element validation handler; Validates each crop selection.

2 string references to 'manualcrop_croptool_validate'
manualcrop_croptool_process in ./manualcrop.helpers.inc
Add a croptool to the form element. This extends the FAPI widget or simply adds a new form item to enable cropping in a regular form.
manualcrop_field_widget_form_alter in ./manualcrop.module
Implements hook_field_widget_form_alter().

File

./manualcrop.module, line 411

Code

function manualcrop_croptool_validate($element, &$form_state) {
  if (!empty($form_state['manualcrop_data']['images'])) {
    if (isset($form_state['triggering_element'])) {

      // Skip validation when needed.
      $te = $form_state['triggering_element'];
      if (!empty($te['#manualcrop_skip']) || in_array($te['#value'], array(
        t('Upload'),
        t('Remove'),
        t('Browse'),
      ))) {
        return;
      }
    }

    // Create a new key, to store processed selections, in the data array.
    $form_state['manualcrop_data']['selections'] = array();
    $selections =& $form_state['manualcrop_data']['selections'];

    // Loop trough the positions to get the Manual Crop data.
    foreach ($form_state['manualcrop_data']['images'] as $fid => $image) {
      $value = drupal_array_get_nested_value($form_state['values'], $image['element_parents']);
      if (!empty($value['manualcrop_selections'])) {

        // Create a selections array for the current image, the first element is
        // the path to the original image, needed for flushing the cache.
        $selections[$fid] = array(
          'path' => $image['uri'],
          'styles' => array_fill_keys(array_keys($value['manualcrop_selections']), FALSE),
        );

        // Indicates if an error on the form element has been set.
        $error_set = FALSE;

        // Loop over the selections.
        foreach ($value['manualcrop_selections'] as $style_name => $crop) {

          // Get the clean style name.
          $clean_style_name = _manualcrop_image_style_label($style_name);

          // Get the element key.
          if (!$error_set) {
            $element_key = implode('][', $image['element_parents']) . '][manualcrop_style';
          }
          else {
            $element_key = implode('][', $image['element_parents']) . '][manualcrop_selections][' . $style_name;
          }
          if (!empty($crop)) {

            // Validate the crop selection format.
            if (preg_match('/^([0-9]+\\|){3}[0-9]+$/', $crop)) {
              $crop = array_map('intval', explode('|', $crop));

              // Check position and minimum dimensions.
              if ($crop[0] >= 0 && $crop[1] >= 0 && $crop[2] > 0 && $crop[3] > 0) {

                // Check if the selections fits on the image.
                if ($crop[0] + $crop[2] <= $image['width'] && $crop[1] + $crop[3] <= $image['height']) {
                  $selections[$fid]['styles'][$style_name] = array(
                    'x' => $crop[0],
                    'y' => $crop[1],
                    'width' => $crop[2],
                    'height' => $crop[3],
                  );
                  continue;
                }
              }
            }

            // Invalid crop selection.
            $error_set = TRUE;
            form_set_error($element_key, t('The crop selection for %filename (@style image style) is invalid, please clear it or reselect.', array(
              '@style' => $clean_style_name,
              '%filename' => $image['filename'],
            )));
          }
          elseif (in_array($style_name, $image['required_styles'], TRUE)) {

            // Uncropped required style.
            $error_set = TRUE;
            form_set_error($element_key, t('%filename must have a cropping selection for the @style image style.', array(
              '@style' => $clean_style_name,
              '%filename' => $image['filename'],
            )));
          }
        }
      }
    }
  }
}