function acquia_lift_personalize_campaign_wizard_targeting_validate in Acquia Lift Connector 7.2
Validation function for targeting form.
File
- ./
acquia_lift.admin.wizard.inc, line 1904 - acquia_lift.admin.wizard.inc Functions specific to the Acquia Lift alteration of the campaign creation wizard.
Code
function acquia_lift_personalize_campaign_wizard_targeting_validate(&$form, &$form_state) {
// Allow advanced AJAX functions such as add/remove contexts to skip
// validation and still operate on the full form value set.
if (!empty($form_state['triggering_element']['#skip_validation'])) {
return;
}
$agent_data = $form['#agent'];
/**
* Anonymous function to validate a specific audience.
*/
$validateAudienceFields = function ($values, $form_prefix, $is_new, $contexts) use ($agent_data) {
if (empty($values['name'])) {
form_set_error($form_prefix . 'name', t('You must specify the name of the audience.'));
return;
}
else {
if ($is_new) {
// Verify that a new audience will have a unique machine name.
module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
$machine_name = personalize_generate_machine_name($values['name'], NULL, '-');
$option_set = acquia_lift_get_option_set_for_targeting($agent_data->machine_name);
if (isset($option_set->targeting[$machine_name])) {
form_set_error($form_prefix . 'name', t('Please choose a different name for your audience as this one is already taken'));
return;
}
}
}
$has_context = FALSE;
$audience_label = $values['name'];
$contexts = isset($values['mapping']['contexts']) ? element_children($values['mapping']['contexts']) : array();
foreach ($contexts as $index) {
if (!empty($values['mapping']['contexts'][$index]['context'])) {
$has_operator = !empty($values['mapping']['contexts'][$index]['value']['operator']);
$match = $values['mapping']['contexts'][$index]['value']['match'];
$has_value = !empty($match) || $match === '0';
$context_value = $values['mapping']['contexts'][$index]['context'];
$context_label = $contexts[$index]['context']['#options'][$context_value];
//$context_label = $form['targeting']['audiences'][$audience_id]['details']['mapping']['contexts'][$index]['context']['#options'][$context_value];
// Make sure that an operator and value have been entered.
if (!$has_operator) {
form_set_error($form_prefix . 'mapping][contexts][' . $index . '][value][operator', t('Please choose the operator for %context in the %audience audience.', array(
'%context' => $context_label,
'%audience' => $audience_label,
)));
}
if (!$has_value) {
form_set_error($form_prefix . 'mapping][contexts][' . $index . '][value][match', t('Please choose the value for %context context in the %audience audience.', array(
'%context' => $context_label,
'%audience' => $audience_label,
)));
}
if ($has_operator && $has_value) {
$has_context = TRUE;
}
}
}
if (!$has_context) {
form_set_error($form_prefix . 'mapping][contexts][add][context', t('Please select at least one context to define the %audience audience.', array(
'%audience' => $audience_label,
)));
}
};
// If we are adding a single audience validate the required audience fields.
$delta = _acquia_lift_personalize_campaign_wizard_next_element($form_state, 'new');
if (!empty($delta) && is_numeric($delta)) {
$validateAudienceFields($form_state['values']['audiences']['new'][$delta]['details'], 'audiences[new][' . $delta . '][details][', TRUE, $form['targeting']['audiences']['new'][$delta]['details']['mapping']['contexts']);
return;
}
// Validate all submitted audiences.
$has_tests = FALSE;
foreach ($form_state['values']['audiences'] as $type_id => $type_data) {
$is_new = $type_id === 'new';
foreach ($form_state['values']['audiences'][$type_id] as $audience_id => $audience) {
if (strpos($audience_id, ACQUIA_LIFT_TARGETING_EVERYONE_ELSE) === 0) {
continue;
}
else {
if (!is_array($audience) || !isset($audience['details'])) {
// Not an audience but some other form field.
continue;
}
}
$validateAudienceFields($audience['details'], 'audiences[' . $type_id . '][' . $audience_id . '][details][', $is_new, $audience['details']['mapping']['contexts']);
if (isset($audience['assignment_order'])) {
$has_tests = $has_tests || count(explode(',', $audience['assignment_order'])) > 1;
}
}
}
// If any of the audiences contain tests, then also validate the test settings.
if (isset($form_state['values']['control_rate'])) {
$rate = $form_state['values']['control_rate'];
if (!is_numeric($rate) || !($rate >= 0 && $rate <= 100)) {
form_set_error('control_rate', t('Invalid percent to test specified'));
}
}
if (isset($form_state['values']['explore_rate'])) {
$rate = $form_state['values']['explore_rate'];
if (!is_numeric($rate) || !($rate >= 0 && $rate <= 100)) {
form_set_error('explore_rate', t('Invalid percent to test specified'));
}
}
}