Skip to content
Snippets Groups Projects
Commit ad980eb2 authored by Dave Greenberg's avatar Dave Greenberg
Browse files

CRM-13090 uniqueness on title + activity type + campaign (optional). Moved the...

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
parent 03578845
Branches
Tags
No related merge requests found
......@@ -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;
}
......
......@@ -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) &&
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment