From ad980eb2e4e8306566060f448942840fb2558f46 Mon Sep 17 00:00:00 2001 From: Dave Greenberg Date: Thu, 1 Aug 2013 14:07:32 -0700 Subject: [PATCH] CRM-13090 uniqueness on title + activity type + campaign (optional). Moved the petition form rule to Petition form class where it belongs (was put in Survey/Results for some reason ??). ---------------------------------------- * 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/Petition.php | 48 ++++++++++++++++++++++++++-- CRM/Campaign/Form/Survey/Results.php | 27 ---------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/CRM/Campaign/Form/Petition.php b/CRM/Campaign/Form/Petition.php index 3234afc73f..8533b5585c 100644 --- a/CRM/Campaign/Form/Petition.php +++ b/CRM/Campaign/Form/Petition.php @@ -44,7 +44,7 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form { * @var int * @protected */ - protected $_surveyId; + public $_surveyId; public function preProcess() { if (!CRM_Campaign_BAO_Campaign::accessCampaign()) { @@ -248,7 +248,51 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form { ); // add a form rule to check default value - $this->addFormRule(array('CRM_Campaign_Form_Survey_Results', 'formRule'), $this); + $this->addFormRule(array('CRM_Campaign_Form_Petition', 'formRule'), $this); + } + + /** + * global validation rules for the form + * + */ + static function formRule($fields, $files, $form) { + $errors = array(); + // Petitions should be unique by: title, campaign ID (if assigned) and activity type ID + // NOTE: This class is called for both Petition create / update AND for Survey Results tab, but this rule is only for Petition. + $where = array('activity_type_id = %1', 'title = %2'); + $params = array( + 1 => array($fields['activity_type_id'], 'Integer'), + 2 => array($fields['title'], 'String'), + ); + $uniqueRule = ts('activity type'); + + if (empty($fields['campaign_id'])) { + $where[] = 'campaign_id IS NULL'; + } else { + $where[] = 'campaign_id = %3'; + $params[3] = array($fields['campaign_id'], 'Integer'); + $uniqueRule = ts('campaign and activity type'); + } + + // Exclude current Petition row if UPDATE. + if ($form->_surveyId) { + $where[] = 'id != %4'; + $params[4] = array($form->_surveyId, 'Integer'); + } + + $whereClause = implode(' AND ', $where); + + $query = " +SELECT COUNT(*) AS row_count +FROM civicrm_survey +WHERE $whereClause +"; + + $result = CRM_Core_DAO::singleValueQuery($query, $params); + if ($result >= 1) { + $errors['title'] = ts('This title is already associated with the selected %1. Please specify a unique title.', array(1 => $uniqueRule)); + } + return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Campaign/Form/Survey/Results.php b/CRM/Campaign/Form/Survey/Results.php index 48739acd96..740f74529c 100644 --- a/CRM/Campaign/Form/Survey/Results.php +++ b/CRM/Campaign/Form/Survey/Results.php @@ -206,33 +206,6 @@ class CRM_Campaign_Form_Survey_Results extends CRM_Campaign_Form_Survey { */ static function formRule($fields, $files, $form) { $errors = array(); - - // 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) && -- 2.25.1