* @var int
* @protected
*/
- protected $_surveyId;
+ public $_surveyId;
public function preProcess() {
if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
);
// 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;
}
*/
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) &&