Merge pull request #13732 from mattwire/refactor_smartgroupquery_upper1
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Fee.php
index cbd038756d79dcd9f5bccdc443421785985abf7d..c69627a46463f66adbbcd2e0e3d2e271a93cec00 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -27,7 +27,7 @@
 
 /**
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 
 /**
@@ -60,6 +60,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
    */
   public function preProcess() {
     parent::preProcess();
+    $this->assign('selectedChild', 'fee');
   }
 
   /**
@@ -68,7 +69,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
    * For edit/view mode the default values are retrieved from the database.
    */
   public function setDefaultValues() {
-    $parentDefaults = parent::setDefaultValues();
+    parent::setDefaultValues();
 
     $eventId = $this->_id;
     $params = array();
@@ -118,14 +119,12 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
       $totalLables = $maxSize = $defaultDiscounts = array();
       foreach ($discountedEvent as $optionGroupId) {
         $defaults['discount_price_set'][] = $optionGroupId;
-        $name = $defaults["discount_name[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
-
-        list($defaults["discount_start_date[$i]"]) = CRM_Utils_Date::setDateDefaults(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
-          'start_date', 'price_set_id'
-        ));
-        list($defaults["discount_end_date[$i]"]) = CRM_Utils_Date::setDateDefaults(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
-          'end_date', 'price_set_id'
-        ));
+        $defaults["discount_name[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $optionGroupId, 'title');
+
+        $defaults["discount_start_date[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
+          'start_date', 'price_set_id');
+        $defaults["discount_end_date[$i]"] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $optionGroupId,
+          'end_date', 'price_set_id');
         $defaultDiscounts[] = CRM_Price_BAO_PriceSet::getSetDetail($optionGroupId);
         $i++;
       }
@@ -241,7 +240,6 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
    * Build the form object.
    */
   public function buildQuickForm() {
-
     $this->addYesNo('is_monetary',
       ts('Paid Event'),
       NULL,
@@ -342,11 +340,10 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
         isset($this->_submitValues['discount_end_date'][$i]) &&
         $i < self::NUM_DISCOUNT - 1
       ) {
-        $end_date = CRM_Utils_Date::processDate($this->_submitValues['discount_end_date'][$i]);
         if (!empty($this->_submitValues['discount_end_date'][$i + 1])
           && empty($this->_submitValues['discount_start_date'][$i + 1])
         ) {
-          list($this->_submitValues['discount_start_date'][$i + 1]) = CRM_Utils_Date::setDateDefaults(date('Y-m-d', strtotime("+1 days $end_date")));
+          $this->_submitValues['discount_start_date'][$i + 1] = date('Y-m-d', strtotime("+1 days " . $this->_submitValues['discount_end_date'][$i]));
         }
       }
       //Decrement by 1 of end date from next start date.
@@ -357,26 +354,19 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
         isset($this->_submitValues['discount_start_date']) &&
         isset($this->_submitValues['discount_start_date'][$i])
       ) {
-        $start_date = CRM_Utils_Date::processDate($this->_submitValues['discount_start_date'][$i]);
         if (!empty($this->_submitValues['discount_start_date'][$i])
           && empty($this->_submitValues['discount_end_date'][$i - 1])
         ) {
-          list($this->_submitValues['discount_end_date'][$i - 1]) = CRM_Utils_Date::setDateDefaults(date('Y-m-d', strtotime("-1 days $start_date")));
+          list($this->_submitValues['discount_end_date'][$i - 1]) = date('Y-m-d', strtotime("-1 days " . $this->_submitValues['discount_start_date'][$i]));
         }
       }
 
-      //discount name
       $this->add('text', 'discount_name[' . $i . ']', ts('Discount Name'),
         CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title')
       );
-
       $this->add('hidden', "discount_price_set[$i]", '', array('id' => "discount_price_set[$i]"));
-
-      //discount start date
-      $this->addDate('discount_start_date[' . $i . ']', ts('Discount Start Date'), FALSE, array('formatType' => 'activityDate'));
-
-      //discount end date
-      $this->addDate('discount_end_date[' . $i . ']', ts('Discount End Date'), FALSE, array('formatType' => 'activityDate'));
+      $this->add('datepicker', 'discount_start_date[' . $i . ']', ts('Discount Start Date'), [], FALSE, array('time' => FALSE));
+      $this->add('datepicker', 'discount_end_date[' . $i . ']', ts('Discount End Date'), [], FALSE, array('time' => FALSE));
     }
     $_showHide->addToTemplate();
     $this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
@@ -554,7 +544,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
    */
   public function postProcess() {
     $eventTitle = '';
-    $params = $this->exportValues();
+    $params = $this->cleanMoneyFields($this->exportValues());
 
     $this->set('discountSection', 0);
 
@@ -610,7 +600,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
             if (!empty($labels[$i]) && !CRM_Utils_System::isNull($values[$i])) {
               $options[] = array(
                 'label' => trim($labels[$i]),
-                'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i])),
+                'value' => $values[$i],
                 'weight' => $i,
                 'is_active' => 1,
                 'is_default' => $default == $i,
@@ -691,7 +681,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
                 ) {
                   $discountOptions[] = array(
                     'label' => trim($labels[$i]),
-                    'value' => CRM_Utils_Rule::cleanMoney(trim($values[$i][$j])),
+                    'value' => $values[$i][$j],
                     'weight' => $i,
                     'is_active' => 1,
                     'is_default' => $default == $i,
@@ -768,8 +758,8 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
                   'entity_table' => 'civicrm_event',
                   'entity_id' => $this->_id,
                   'price_set_id' => $priceSetID,
-                  'start_date' => CRM_Utils_Date::processDate($params['discount_start_date'][$j]),
-                  'end_date' => CRM_Utils_Date::processDate($params['discount_end_date'][$j]),
+                  'start_date' => $params['discount_start_date'][$j],
+                  'end_date' => $params['discount_end_date'][$j],
                 );
                 CRM_Core_BAO_Discount::add($discountParams);
               }
@@ -818,4 +808,36 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent {
     return ts('Event Fees');
   }
 
+  /**
+   * Clean money fields in submitted params to remove formatting.
+   *
+   * @param array $params
+   *
+   * @return array
+   */
+  protected function cleanMoneyFields($params) {
+    foreach ($params['value'] as $index => $value) {
+      if (CRM_Utils_System::isNull($value)) {
+        unset($params['value'][$index]);
+      }
+      else {
+        $params['value'][$index] = CRM_Utils_Rule::cleanMoney(trim($value));
+      }
+    }
+    foreach ($params['discounted_value'] as $index => $discountedValueSet) {
+      foreach ($discountedValueSet as $innerIndex => $value) {
+        if (CRM_Utils_System::isNull($value)) {
+          unset($params['discounted_value'][$index][$innerIndex]);
+        }
+        else {
+          $params['discounted_value'][$index][$innerIndex] = CRM_Utils_Rule::cleanMoney(trim($value));
+        }
+      }
+      if (empty($params['discounted_value'][$index])) {
+        unset($params['discounted_value'][$index]);
+      }
+    }
+    return $params;
+  }
+
 }