Merge pull request #1597 from lcdservices/CRM-13341
[civicrm-core.git] / CRM / Campaign / Form / Petition.php
index 2cca8a3d111fa4fef77c085f75c485c9752b6e88..812a3281afc801ad2887254f0d70bf55ca87be3f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
  * This class generates form components for adding a petition
  *
  */
+
 class CRM_Campaign_Form_Petition extends CRM_Core_Form {
 
+  /**
+   * Making this public so we can reference it in the formRule
+   * @var int
+   * @public
+   */
+  public $_surveyId;
+
   public function preProcess() {
     if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
       CRM_Utils_System::permissionDenied();
@@ -79,7 +87,7 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
     $session->pushUserContext($url);
 
     $this->_values = $this->get('values');
-    
+
     if (!is_array($this->_values)) {
       $this->_values = array();
       if ($this->_surveyId) {
@@ -124,16 +132,25 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
   function setDefaultValues() {
     $defaults = $this->_values;
 
-    $ufJoinParams = array(
+    $ufContactJoinParams = array(
       'entity_table' => 'civicrm_survey',
       'entity_id' => $this->_surveyId,
       'weight' => 2,
     );
 
-    if ($ufGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParams)) {
-      $defaults['contact_profile_id'] = $ufGroupId;
+    if ($ufContactGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufContactJoinParams)) {
+      $defaults['contact_profile_id'] = $ufContactGroupId;
     }
-    
+    $ufActivityJoinParams = array(
+      'entity_table' => 'civicrm_survey',
+      'entity_id' => $this->_surveyId,
+      'weight' => 1,
+    );
+
+    if ($ufActivityGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufActivityJoinParams)) {
+      $defaults['profile_id'] = $ufActivityGroupId;
+    }
+
     if (!isset($defaults['is_active'])) {
       $defaults['is_active'] = 1;
     }
@@ -150,8 +167,8 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
   public function buildQuickForm() {
 
     if ($this->_action & CRM_Core_Action::DELETE) {
-
-      $this->addButtons(array(
+      $this->addButtons(
+        array(
           array(
             'type' => 'next',
             'name' => ts('Delete'),
@@ -166,7 +183,6 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
       return;
     }
 
-
     $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
 
     $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey');
@@ -192,16 +208,20 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
     // custom group id
     $this->add('select', 'profile_id', ts('Activity Profile'),
       array(
-        '' => ts('- select -')) + $customProfiles
+        '' => ts('- select -')
+      ) + $customProfiles
     );
 
     // thank you title and text (html allowed in text)
     $this->add('text', 'thankyou_title', ts('Thank-you Page Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_title'));
     $this->addWysiwyg('thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text'));
-    
+
     // bypass email confirmation?
     $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation'));
 
+    //is share through social media
+    $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
+
     // is active ?
     $this->add('checkbox', 'is_active', ts('Is Active?'));
 
@@ -209,7 +229,8 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
     $this->add('checkbox', 'is_default', ts('Is Default?'));
 
     // add buttons
-    $this->addButtons(array(
+    $this->addButtons(
+      array(
         array(
           'type' => 'next',
           'name' => ts('Save'),
@@ -228,7 +249,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;
   }
 
 
@@ -240,6 +305,7 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
 
     $params['last_modified_id'] = $session->get('userID');
     $params['last_modified_date'] = date('YmdHis');
+    $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
 
     if ($this->_surveyId) {
 
@@ -263,7 +329,6 @@ class CRM_Campaign_Form_Petition extends CRM_Core_Form {
 
     $surveyId = CRM_Campaign_BAO_Survey::create($params);
 
-
     // also update the ProfileModule tables
     $ufJoinParams = array(
       'is_active' => 1,