Merge pull request #14367 from MegaphoneJon/financial-58
[civicrm-core.git] / CRM / Price / BAO / PriceField.php
index a35726009010aea301bdcc250ec6603c3521200b..299832a3f682f8e9a21357a48d16a33784a81acd 100644 (file)
@@ -61,6 +61,9 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
    * @return CRM_Price_BAO_PriceField
    */
   public static function add(&$params) {
+    $hook = empty($params['id']) ? 'create' : 'edit';
+    CRM_Utils_Hook::pre($hook, 'PriceField', CRM_Utils_Array::value('id', $params), $params);
+
     $priceFieldBAO = new CRM_Price_BAO_PriceField();
 
     $priceFieldBAO->copyValues($params);
@@ -70,6 +73,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
     }
 
     $priceFieldBAO->save();
+    CRM_Utils_Hook::post($hook, 'PriceField', $priceFieldBAO->id, $priceFieldBAO);
     return $priceFieldBAO;
   }
 
@@ -82,7 +86,9 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
    *   (reference) an assoc array of name/value pairs.
    *
    * @return CRM_Price_DAO_PriceField
+   *
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public static function create(&$params) {
     if (empty($params['id']) && empty($params['name'])) {
@@ -100,14 +106,14 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
     if (!empty($params['id']) && empty($priceField->html_type)) {
       $priceField->html_type = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $params['id'], 'html_type');
     }
-    $optionsIds = array();
+    $optionsIds = [];
     $maxIndex = CRM_Price_Form_Field::NUM_OPTION;
     if ($priceField->html_type == 'Text') {
       $maxIndex = 1;
-      $fieldOptions = civicrm_api3('price_field_value', 'get', array(
+      $fieldOptions = civicrm_api3('price_field_value', 'get', [
         'price_field_id' => $priceField->id,
         'sequential' => 1,
-      ));
+      ]);
       foreach ($fieldOptions['values'] as $option) {
         $optionsIds['id'] = $option['id'];
         // CRM-19741 If we are dealing with price fields that are Text only set the field value label to match
@@ -119,7 +125,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         }
       }
     }
-    $defaultArray = array();
+    $defaultArray = [];
     //html type would be empty in update scenario not sure what would happen ...
     if (!empty($params['html_type']) && $params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) {
       $tempArray = array_keys($params['default_checkbox_option']);
@@ -141,7 +147,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_label', $params)) || !empty($params['is_quick_config'])) &&
         !CRM_Utils_System::isNull($params['option_amount'][$index])
       ) {
-        $options = array(
+        $options = [
           'price_field_id' => $priceField->id,
           'label' => trim($params['option_label'][$index]),
           'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64),
@@ -156,7 +162,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           'membership_num_terms' => NULL,
           'non_deductible_amount' => CRM_Utils_Array::value('non_deductible_amount', $params),
           'visibility_id' => CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_visibility_id', $params), self::getVisibilityOptionID('public')),
-        );
+        ];
 
         if ($options['membership_type_id']) {
           $options['membership_num_terms'] = CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_num_terms', $params), 1);
@@ -186,7 +192,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         }
       }
       elseif (!empty($optionsIds) && !empty($optionsIds['id'])) {
-        $optionsLoad = civicrm_api3('price_field_value', 'get', array('id' => $optionsIds['id']));
+        $optionsLoad = civicrm_api3('price_field_value', 'get', ['id' => $optionsIds['id']]);
         $options = $optionsLoad['values'][$optionsIds['id']];
         $options['is_active'] = CRM_Utils_Array::value('is_active', $params, 1);
         try {
@@ -288,7 +294,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
     $useRequired = TRUE,
     $label = NULL,
     $fieldOptions = NULL,
-    $freezeOptions = array()
+    $freezeOptions = []
   ) {
 
     $field = new CRM_Price_DAO_PriceField();
@@ -348,13 +354,13 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $qf->assign('taxTerm', $taxTerm);
           $qf->assign('invoicing', $invoicing);
         }
-        $priceVal = implode($seperator, array(
+        $priceVal = implode($seperator, [
           $customOption[$optionKey][$valueFieldName] + $taxAmount,
           $count,
           $max_value,
-        ));
+        ]);
 
-        $extra = array();
+        $extra = [];
         if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) {
           $useRequired = 0;
         }
@@ -364,21 +370,21 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount) && strtolower($fieldOptions[$optionKey]['name']) == 'other_amount') {
             $label .= '  ' . $currencySymbol;
             $qf->assign('priceset', $elementName);
-            $extra = array('onclick' => 'useAmountOther();');
+            $extra = ['onclick' => 'useAmountOther();'];
           }
         }
 
         $element = &$qf->add('text', $elementName, $label,
           array_merge($extra,
-            array(
-              'price' => json_encode(array($optionKey, $priceVal)),
+            [
+              'price' => json_encode([$optionKey, $priceVal]),
               'size' => '4',
-            )
+            ]
           ),
           $useRequired && $field->is_required
         );
         if ($is_pay_later) {
-          $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4'));
+          $qf->add('text', 'txt-' . $elementName, $label, ['size' => '4']);
         }
 
         // CRM-6902 - Add "max" option for a price set field
@@ -394,7 +400,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $type = 'money';
         }
         else {
-          $message = ts('%1 must be a number (with or without decimal point).', array(1 => $label));
+          $message = ts('%1 must be a number (with or without decimal point).', [1 => $label]);
           $type = 'numeric';
         }
         // integers will have numeric rule applied to them.
@@ -402,7 +408,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         break;
 
       case 'Radio':
-        $choice = array();
+        $choice = [];
 
         if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) {
           $qf->assign('contriPriceset', $elementName);
@@ -439,34 +445,34 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           }
           $count = CRM_Utils_Array::value('count', $opt, '');
           $max_value = CRM_Utils_Array::value('max_value', $opt, '');
-          $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
+          $priceVal = implode($seperator, [$opt[$valueFieldName] + $taxAmount, $count, $max_value]);
           if (isset($opt['visibility_id'])) {
             $visibility_id = $opt['visibility_id'];
           }
           else {
             $visibility_id = self::getVisibilityOptionID('public');
           }
-          $extra = array(
-            'price' => json_encode(array($elementName, $priceVal)),
+          $extra = [
+            'price' => json_encode([$elementName, $priceVal]),
             'data-amount' => $opt[$valueFieldName],
             'data-currency' => $currencyName,
             'data-price-field-values' => json_encode($customOption),
             'visibility' => $visibility_id,
-          );
+          ];
           if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') {
-            $extra += array('onclick' => 'clearAmountOther();');
+            $extra += ['onclick' => 'clearAmountOther();'];
           }
           if ($field->name == 'membership_amount') {
-            $extra += array(
+            $extra += [
               'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});",
               'membership-type' => $opt['membership_type_id'],
-            );
+            ];
             $qf->assign('membershipFieldID', $field->id);
           }
 
           $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra);
           if ($is_pay_later) {
-            $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4'));
+            $qf->add('text', 'txt-' . $elementName, $label, ['size' => '4']);
           }
 
           // CRM-6902 - Add "max" option for a price set field
@@ -478,11 +484,11 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         }
         if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') {
           $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1',
-            array(
-              'price' => json_encode(array($elementName, '0|0')),
+            [
+              'price' => json_encode([$elementName, '0|0']),
               'data-currency' => $currencyName,
               'onclick' => 'clearAmountOther();',
-            )
+            ]
           );
         }
 
@@ -499,7 +505,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           }
 
           $choice[] = $qf->createElement('radio', NULL, '', $none, '0',
-            array('price' => json_encode(array($elementName, '0')))
+            ['price' => json_encode([$elementName, '0'])]
           );
         }
 
@@ -513,12 +519,12 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         }
 
         if ($useRequired && $field->is_required) {
-          $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
+          $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
         }
         break;
 
       case 'Select':
-        $selectOption = $allowedOptions = $priceVal = array();
+        $selectOption = $allowedOptions = $priceVal = [];
 
         foreach ($customOption as $opt) {
           $taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
@@ -535,7 +541,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             }
           }
 
-          $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
+          $priceVal[$opt['id']] = implode($seperator, [$opt[$valueFieldName] + $taxAmount, $count, $max_value]);
 
           if (!in_array($opt['id'], $freezeOptions)) {
             $allowedOptions[] = $opt['id'];
@@ -549,7 +555,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $selectOption[$opt['id']] = $opt['label'];
 
           if ($is_pay_later) {
-            $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4'));
+            $qf->add('text', 'txt-' . $elementName, $label, ['size' => '4']);
           }
         }
         if (isset($opt['visibility_id'])) {
@@ -559,11 +565,11 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $visibility_id = self::getVisibilityOptionID('public');
         }
         $element = &$qf->add('select', $elementName, $label,
-          array(
+          [
             '' => ts('- select -'),
-          ) + $selectOption,
+          ] + $selectOption,
           $useRequired && $field->is_required,
-          array('price' => json_encode($priceVal), 'class' => 'crm-select2', 'data-price-field-values' => json_encode($customOption))
+          ['price' => json_encode($priceVal), 'class' => 'crm-select2', 'data-price-field-values' => json_encode($customOption)]
         );
 
         // CRM-6902 - Add "max" option for a price set field
@@ -575,7 +581,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
 
       case 'CheckBox':
 
-        $check = array();
+        $check = [];
         foreach ($customOption as $opId => $opt) {
           $taxAmount = CRM_Utils_Array::value('tax_amount', $opt);
           $count = CRM_Utils_Array::value('count', $opt, '');
@@ -598,17 +604,17 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             }
             $opt['label'] = $preHelpText . $opt['label'] . $postHelpText;
           }
-          $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
+          $priceVal = implode($seperator, [$opt[$valueFieldName] + $taxAmount, $count, $max_value]);
           $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'],
-            array(
-              'price' => json_encode(array($opt['id'], $priceVal)),
+            [
+              'price' => json_encode([$opt['id'], $priceVal]),
               'data-amount' => $opt[$valueFieldName],
               'data-currency' => $currencyName,
               'visibility' => $opt['visibility_id'],
-            )
+            ]
           );
           if ($is_pay_later) {
-            $txtcheck[$opId] =& $qf->createElement('text', $opId, $opt['label'], array('size' => '4'));
+            $txtcheck[$opId] =& $qf->createElement('text', $opId, $opt['label'], ['size' => '4']);
             $qf->addGroup($txtcheck, 'txt-' . $elementName, $label);
           }
           // CRM-6902 - Add "max" option for a price set field
@@ -620,7 +626,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         }
         $element = &$qf->addGroup($check, $elementName, $label);
         if ($useRequired && $field->is_required) {
-          $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required');
+          $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
         }
         break;
     }
@@ -646,15 +652,15 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
    */
   public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE, $isDefaultContributionPriceSet = FALSE) {
     if ($reset || !isset(Civi::$statics[__CLASS__]['priceOptions'])) {
-      Civi::$statics[__CLASS__]['priceOptions'] = array();
+      Civi::$statics[__CLASS__]['priceOptions'] = [];
       // This would happen if the function was only called to clear the cache.
       if (empty($fieldId)) {
-        return array();
+        return [];
       }
     }
 
     if (empty(Civi::$statics[__CLASS__]['priceOptions'][$fieldId])) {
-      $values = $options = array();
+      $values = $options = [];
       CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $values, 'weight', !$inactiveNeeded);
       $options[$fieldId] = $values;
       $taxRates = CRM_Core_PseudoConstant::getTaxRates();
@@ -698,10 +704,10 @@ WHERE
     AND option_group.id = option_value.option_group_id
     AND option_value.label = %2";
 
-    $dao = CRM_Core_DAO::executeQuery($query, array(
-      1 => array($optionGroupName, 'String'),
-      2 => array($optionLabel, 'String'),
-    ));
+    $dao = CRM_Core_DAO::executeQuery($query, [
+      1 => [$optionGroupName, 'String'],
+      2 => [$optionLabel, 'String'],
+    ]);
 
     while ($dao->fetch()) {
       return $dao->id;
@@ -726,7 +732,7 @@ WHERE
       CRM_Price_BAO_PriceFieldValue::deleteValues($id);
 
       // reorder the weight before delete
-      $fieldValues = array('price_set_id' => $field->price_set_id);
+      $fieldValues = ['price_set_id' => $field->price_set_id];
 
       CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceField', $field->id, $fieldValues);
 
@@ -743,12 +749,12 @@ WHERE
   public static function &htmlTypes() {
     static $htmlTypes = NULL;
     if (!$htmlTypes) {
-      $htmlTypes = array(
+      $htmlTypes = [
         'Text' => ts('Text / Numeric Quantity'),
         'Select' => ts('Select'),
         'Radio' => ts('Radio'),
         'CheckBox' => ts('CheckBox'),
-      );
+      ];
     }
     return $htmlTypes;
   }
@@ -773,10 +779,10 @@ WHERE
     $priceField->price_set_id = $priceSetId;
     $priceField->find();
 
-    $priceFields = array();
+    $priceFields = [];
 
     if ($allowNoneSelection) {
-      $noneSelectedPriceFields = array();
+      $noneSelectedPriceFields = [];
     }
 
     while ($priceField->fetch()) {
@@ -802,15 +808,15 @@ SELECT  id, html_type
 FROM  civicrm_price_field
 WHERE  id IN (" . implode(',', array_keys($priceFields)) . ')';
       $fieldDAO = CRM_Core_DAO::executeQuery($sql);
-      $htmlTypes = array();
+      $htmlTypes = [];
       while ($fieldDAO->fetch()) {
         $htmlTypes[$fieldDAO->id] = $fieldDAO->html_type;
       }
 
-      $selectedAmounts = array();
+      $selectedAmounts = [];
 
       foreach ($htmlTypes as $fieldId => $type) {
-        $options = array();
+        $options = [];
         CRM_Price_BAO_PriceFieldValue::getValues($fieldId, $options);
 
         if (empty($options)) {
@@ -839,10 +845,11 @@ WHERE  id IN (" . implode(',', array_keys($priceFields)) . ')';
       // The form offers a field to enter the amount paid. This may differ from the amount that is due to complete the purchase
       $totalPaymentAmountEnteredOnForm = CRM_Utils_Array::value('partial_payment_total', $fields, CRM_Utils_Array::value('total_amount', $fields));
       if ($totalAmount < 0) {
-        $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', array(1 => $componentName));
+        $error['_qf_default'] = ts('%1 amount can not be less than zero. Please select the options accordingly.', [1 => $componentName]);
       }
       elseif ($totalAmount > 0 &&
-        $totalPaymentAmountEnteredOnForm >= $totalAmount && // if total amount is equal to all selected amount in hand
+      // if total amount is equal to all selected amount in hand
+        $totalPaymentAmountEnteredOnForm >= $totalAmount &&
         (CRM_Utils_Array::value('contribution_status_id', $fields) == CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Partially paid'))
       ) {
         $error['total_amount'] = ts('You have specified the status Partially Paid but have entered an amount that equals or exceeds the amount due. Please adjust the status of the payment or the amount');
@@ -903,10 +910,10 @@ WHERE  id IN (" . implode(',', array_keys($priceFields)) . ')';
       self::$visibilityOptionsKeys = CRM_Price_BAO_PriceField::buildOptions(
         'visibility_id',
         NULL,
-        array(
+        [
           'labelColumn' => 'name',
           'flip' => TRUE,
-        )
+        ]
       );
     }