From 968c2c5fd76958150592fcb584e0edb86af6ef54 Mon Sep 17 00:00:00 2001 From: "Donald A. Lobo" Date: Wed, 31 Jul 2013 13:48:52 -0700 Subject: [PATCH] fix patch for CRM-13090 ---------------------------------------- * CRM-13090: New Petition/Survey form validation - enforce unique constraint on ActivityType-Campaign-Title http://issues.civicrm.org/jira/browse/CRM-13090 --- CRM/Campaign/Form/Survey/Results.php | 35 +++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/CRM/Campaign/Form/Survey/Results.php b/CRM/Campaign/Form/Survey/Results.php index cb2e5e4a24..48739acd96 100644 --- a/CRM/Campaign/Form/Survey/Results.php +++ b/CRM/Campaign/Form/Survey/Results.php @@ -207,7 +207,34 @@ class CRM_Campaign_Form_Survey_Results extends CRM_Campaign_Form_Survey { static function formRule($fields, $files, $form) { $errors = array(); - if (CRM_Utils_Array::value('option_label', $fields) && + // Petitions and Surveys are unique by: title, campaign ID and activity type ID + if (!CRM_Utils_Rule::integer($fields['campaign_id'])) { + $errors['campaign_id'] = ts('Please enter a valid integer.'); + } + else if (!CRM_Utils_Rule::integer($fields['activity_type_id'])) { + $errors['activity_type_id'] = ts('Please enter a valid integer.'); + } + else { + $query = " +SELECT COUNT(*) AS row_count +FROM civicrm_survey +WHERE campaign_id = %1 +AND activity_type_id = %2 +AND title = %3 +"; + $params = array( + 1 => array($fields['campaign_id'], 'Integer'), + 2 => array($fields['activity_type_id'], 'Integer'), + 3 => array($fields['title'], 'String'), + ); + $result = CRM_Core_DAO::singleValueQuery($query, $params); + if ($result >= 1) { + $errors['title'] = ts('Title is already associated with the specified campaign and activity type. Please specify a unique title.'); + } + } + + if ( + CRM_Utils_Array::value('option_label', $fields) && CRM_Utils_Array::value('option_value', $fields) && (count(array_filter($fields['option_label'])) == 0) && (count(array_filter($fields['option_value'])) == 0) @@ -215,13 +242,15 @@ class CRM_Campaign_Form_Survey_Results extends CRM_Campaign_Form_Survey { $errors['option_label[1]'] = ts('Enter at least one result option.'); return $errors; } - elseif (!CRM_Utils_Array::value('option_label', $fields) && + elseif ( + !CRM_Utils_Array::value('option_label', $fields) && !CRM_Utils_Array::value('option_value', $fields) ) { return $errors; } - if ($fields['option_type'] == 2 && + if ( + $fields['option_type'] == 2 && !CRM_Utils_Array::value('option_group_id', $fields) ) { $errors['option_group_id'] = ts("Please select a Survey Result Set."); -- 2.25.1