You are here

function acquia_lift_personalize_campaign_wizard_variations_validate in Acquia Lift Connector 7.2

Validation function for variations form.

File

./acquia_lift.admin.wizard.inc, line 1761
acquia_lift.admin.wizard.inc Functions specific to the Acquia Lift alteration of the campaign creation wizard.

Code

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

  // Get a reference to the full values array.
  $values = $form_state['values'];

  // Handles validation of block values and setting form errors correctly.
  // Using #limit_validation_errors for the single submit means that any
  // form_set_error from the personalize_blocks_form_validate() function
  // will never be set.  For now reproducing the relevant validation here.
  // @todo there has to be a better way with some further refactoring.
  $validateBlock = function ($values, $form_error_parent) {
    $valid = FALSE;
    $first_block = '';
    if (isset($values['blocks'])) {
      $num_blocks = 0;
      foreach ($values['blocks'] as $j => $block) {
        if (empty($first_block)) {
          $first_block = $j;
        }
        if (empty($block['option_label'])) {
          form_set_error($form_error_parent . 'blocks][' . $j . '][option_label', t('A label is required for each block option.'));
        }
        if ($block['block']['block_type'] === 'add') {
          if (empty($block['block']['add']['info'])) {
            form_set_error($form_error_parent . 'blocks][' . $j . '][block][add][info', t('A block description is required when creating a new block.'));
          }
          if (empty($block['block']['add']['body']['value'])) {
            form_set_error($form_error_parent . 'blocks][' . $j . '][block][add][body', t('The block body is required when creating a new block.'));
          }

          // @see block_add_block_form_validate().
          $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE info = :info', 0, 1, array(
            ':info' => $block['block']['add']['info'],
          ))
            ->fetchField();
          if ($custom_block_exists) {
            form_set_error($form_error_parent . 'blocks][' . $j . '][block][add][info', t('Ensure that each block description is unique.'));
          }
          else {
            $num_blocks++;
          }
        }
        if (!empty($block['block']['bid'])) {
          $num_blocks++;
        }
      }
      $valid = $num_blocks > 1;
    }
    if (!$valid) {
      form_set_error($form_error_parent . 'blocks][' . $first_block . '][block][block_type]', t('You must add at least 2 blocks to your personalized block'));
    }
  };

  // Validation for existing option set types.
  if (!empty($form_state['values']['variations']['editing']['option_sets'])) {
    foreach ($form_state['values']['variations']['editing']['option_sets'] as $option_set_id => $option_set_values) {
      switch ($option_set_values['option_set']->plugin) {
        case "block":
          $validateBlock($option_set_values['content']['pblock_wrapper'], 'variations][editing][option_sets][' . $option_set_id . '][content][pblock_wrapper][');
          break;
        case "elements":
          if ($option_set_values['advanced']['pages_all'] == 0) {
            if (empty($option_set_values['advanced']['pages'])) {
              form_set_error('variations][editing][option_sets][' . $option_set_id . '][advanced][pages]', t('There must be at least one page entered where the webpage element variation is executed.'));
            }
            else {
              visitor_actions_form_element_path_validate($form['variations']['editing']['option_sets'][$option_set_id]['advanced']['pages'], $form_state);
            }
          }
          break;
        default:
      }

      // Validate advanced settings.
      personalize_campaign_wizard_validate_variations_advanced($form_state['values']['variations']['editing']['option_sets'][$option_set_id], 'variations][editing][option_sets][' . $option_set_id . ']');
    }
  }

  // Validation for any new option set types once the details have been input.
  if (!empty($form_state['new_option_sets'])) {
    foreach ($form_state['new_option_sets'] as $delta => $type) {
      if ($type != 'block') {
        continue;
      }
      $validateBlock($form_state['values']['variations']['editing']['new'][$delta]['block']['content']['pblock_wrapper'], 'variations][editing][new][' . $delta . '][block][content][pblock_wrapper][');
    }
  }

  // Put the form state back the way it was for the next steps in form processing.
  $form_state['values'] = $values;
}