INFRA-132 - phpcbf Drupal.WhiteSpace.ScopeIndent.IncorrectExact
[civicrm-core.git] / CRM / Price / BAO / PriceField.php
index b8cb4ab5aada2e79164d1e6dd7d7460c4717fbdd..b837aedd1f2319dceb0b8cdb4c045c84c73ad5d5 100644 (file)
@@ -23,7 +23,7 @@
   | GNU Affero General Public License or the licensing of CiviCRM,     |
   | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
   +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -48,13 +48,12 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
    * price field object. the params array could contain additional unused name/value
    * pairs
    *
-   * @param array $params (reference) an assoc array of name/value pairs
+   * @param array $params
+   *   (reference) an assoc array of name/value pairs.
    *
-   * @return CRM_Price_BAO_PriceField object
-   * @access public
-   * @static
+   * @return CRM_Price_BAO_PriceField
    */
-  static function add(&$params) {
+  public static function add(&$params) {
     $priceFieldBAO = new CRM_Price_BAO_PriceField();
 
     $priceFieldBAO->copyValues($params);
@@ -72,14 +71,13 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
    *
    * This function is invoked from within the web form layer and also from the api layer
    *
-   * @param array $params (reference) an assoc array of name/value pairs
+   * @param array $params
+   *   (reference) an assoc array of name/value pairs.
    *
-   * @return CRM_Price_DAO_PriceField object
-   * @access public
-   * @static
+   * @return CRM_Price_DAO_PriceField
    */
-  static function create(&$params) {
-    if(empty($params['id']) && empty($params['name'])) {
+  public static function create(&$params) {
+    if (empty($params['id']) && empty($params['name'])) {
       $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
     }
     $transaction = new CRM_Core_Transaction();
@@ -146,16 +144,18 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $options['membership_num_terms'] = CRM_Utils_Array::value($index, CRM_Utils_Array::value('membership_num_terms', $params), 1);
         }
 
-        if (CRM_Utils_Array::value( $index, CRM_Utils_Array::value('option_financial_type_id', $params))) {
-          $options['financial_type_id'] =  $params['option_financial_type_id'][$index];
-        } elseif (!empty($params['financial_type_id'])) {
+        if (CRM_Utils_Array::value($index, CRM_Utils_Array::value('option_financial_type_id', $params))) {
+          $options['financial_type_id'] = $params['option_financial_type_id'][$index];
+        }
+        elseif (!empty($params['financial_type_id'])) {
           $options['financial_type_id'] = $params['financial_type_id'];
         }
 
         if ($opIds = CRM_Utils_Array::value('option_id', $params)) {
           if ($opId = CRM_Utils_Array::value($index, $opIds)) {
             $optionsIds['id'] = $opId;
-          } else {
+          }
+          else {
             $optionsIds['id'] = NULL;
           }
         }
@@ -170,41 +170,48 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   /**
    * Fetch object based on array of properties
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
-   * @param array $defaults (reference ) an assoc array to hold the flattened values
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
+   * @param array $defaults
+   *   (reference ) an assoc array to hold the flattened values.
    *
-   * @return CRM_Price_DAO_PriceField object
-   * @access public
-   * @static
+   * @return CRM_Price_DAO_PriceField
    */
-  static function retrieve(&$params, &$defaults) {
+  public static function retrieve(&$params, &$defaults) {
     return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceField', $params, $defaults);
   }
 
   /**
    * Update the is_active flag in the db
    *
-   * @param int      $id         Id of the database record
-   * @param boolean  $is_active  Value we want to set the is_active field
+   * @param int $id
+   *   Id of the database record.
+   * @param bool $is_active
+   *   Value we want to set the is_active field.
    *
-   * @return   Object            DAO object on sucess, null otherwise
+   * @return Object
+   *   DAO object on sucess, null otherwise
    *
-   * @access public
-   * @static
    */
-  static function setIsActive($id, $is_active) {
+  public static function setIsActive($id, $is_active) {
     return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active);
   }
 
+  public static function freezeIfEnabled(&$element, $fieldOptions) {
+    if (!empty($fieldOptions['is_full'])) {
+      $element->freeze();
+    }
+    return NULL;
+  }
+
   /**
    * Get the field title.
    *
-   * @param int $id id of field.
-   *
-   * @return string name
+   * @param int $id
+   *   Id of field.
    *
-   * @access public
-   * @static
+   * @return string
+   *   name
    *
    */
   public static function getTitle($id) {
@@ -214,22 +221,25 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   /**
    * This function for building custom fields
    *
-   * @param CRM_Core_Form $qf form object (reference)
-   * @param string $elementName name of the custom field
+   * @param CRM_Core_Form $qf
+   *   Form object (reference).
+   * @param string $elementName
+   *   Name of the custom field.
    * @param int $fieldId
-   * @param boolean $inactiveNeeded
-   * @param boolean $useRequired true if required else false
-   * @param string $label label for custom field
+   * @param bool $inactiveNeeded
+   * @param bool $useRequired
+   *   True if required else false.
+   * @param string $label
+   *   Label for custom field.
    *
    * @param null $fieldOptions
    * @param array $freezeOptions
    *
    * @return null
    * @internal param bool $search true if used for search else false
-   * @access public
-   * @static
    */
-  public static function addQuickFormElement(&$qf,
+  public static function addQuickFormElement(
+    &$qf,
     $elementName,
     $fieldId,
     $inactiveNeeded,
@@ -251,12 +261,12 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
       $is_pay_later = 1;
     }
     elseif (isset($qf->_values)) {
-      $is_pay_later = CRM_Utils_Array::value( 'is_pay_later', $qf->_values);
+      $is_pay_later = CRM_Utils_Array::value('is_pay_later', $qf->_values);
     }
 
     $otherAmount = $qf->get('values');
     $config = CRM_Core_Config::singleton();
-    $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency',$config->defaultCurrency,'symbol','name');
+    $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name');
     $qf->assign('currencySymbol', $currencySymbol);
     // get currency name for price field and option attributes
     $currencyName = $config->defaultCurrency;
@@ -296,11 +306,15 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           $qf->assign('taxTerm', $taxTerm);
           $qf->assign('invoicing', $invoicing);
         }
-        $priceVal = implode($seperator, array($customOption[$optionKey][$valueFieldName] + $taxAmount, $count, $max_value));
+        $priceVal = implode($seperator, array(
+            $customOption[$optionKey][$valueFieldName] + $taxAmount,
+            $count,
+            $max_value,
+          ));
 
         $extra = array();
         if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) {
-          foreach($fieldOptions as &$fieldOption) {
+          foreach ($fieldOptions as &$fieldOption) {
             if ($fieldOption['name'] == 'other_amount') {
               $fieldOption['label'] = $fieldOption['label'] . '  ' . $currencySymbol;
             }
@@ -312,26 +326,29 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) {
           $useRequired = 0;
         }
-        elseif (!empty($fieldOptions[$optionKey]['label'])) {      //check for label.
+        elseif (!empty($fieldOptions[$optionKey]['label'])) {
+          //check for label.
           $label = $fieldOptions[$optionKey]['label'];
         }
 
         $element = &$qf->add('text', $elementName, $label,
-                   array_merge($extra,
-                     array('price' => json_encode(array($optionKey, $priceVal)),
-                       'size' => '4',
-                     )
-                   ),
-                   $useRequired && $field->is_required
+          array_merge($extra,
+            array(
+              'price' => json_encode(array($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, array('size' => '4'));
         }
 
         // CRM-6902 - Add "max" option for a price set field
         if (in_array($optionKey, $freezeOptions)) {
-          $element->freeze();
-          // CRM-14696 - Styling for sold out Text input fields is handled in PriceSet.tpl
+          self::freezeIfEnabled($element, $freezeOptions);
+          // CRM-14696 - Improve display for sold out price set options
+          $element->setLabel($label . '&nbsp;<span class="sold-out-option">' . ts('Sold out') . '</span>');
         }
 
         //CRM-10117
@@ -360,15 +377,15 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             $opt['label'] = !empty($opt['label']) ? $opt['label'] . '&nbsp;-&nbsp;' : '';
             if (isset($taxAmount) && $invoicing) {
               if ($displayOpt == 'Do_not_show') {
-                $opt['label'] =  '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>';
+                $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>';
               }
-              else if ($displayOpt == 'Inclusive') {
+              elseif ($displayOpt == 'Inclusive') {
                 $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName] + $taxAmount) . '</span>';
                 $opt['label'] .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>';
               }
               else {
-                $opt['label'] =  '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>';
-                $opt['label'] .= '<span class="crm-price-amount-tax"> + '. CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>';
+                $opt['label'] = '<span class="crm-price-amount-label">' . $opt['label'] . '</span>' . '<span class="crm-price-amount-amount">' . CRM_Utils_Money::format($opt[$valueFieldName]) . '</span>';
+                $opt['label'] .= '<span class="crm-price-amount-tax"> + ' . CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>';
               }
             }
             else {
@@ -378,9 +395,10 @@ 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));
-          $extra = array('price' => json_encode(array($elementName, $priceVal)),
-                   'data-amount' => $opt[$valueFieldName],
-                   'data-currency' => $currencyName,
+          $extra = array(
+            'price' => json_encode(array($elementName, $priceVal)),
+            'data-amount' => $opt[$valueFieldName],
+            'data-currency' => $currencyName,
           );
           if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') {
             $extra += array('onclick' => 'clearAmountOther();');
@@ -390,18 +408,18 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
               'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});",
               'membership-type' => $opt['membership_type_id'],
             );
-            $qf->assign('membershipFieldID',$field->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, array('size' => '4'));
           }
 
           // CRM-6902 - Add "max" option for a price set field
           if (in_array($opId, $freezeOptions)) {
-            $choice[$opId]->freeze();
+            self::freezeIfEnabled($choice[$opId], $customOption[$opId]);
             // CRM-14696 - Improve display for sold out price set options
             $choice[$opId]->setText('<span class="sold-out-option">' . $choice[$opId]->getText() . '&nbsp;(' . ts('Sold out') . ')</span>');
           }
@@ -435,7 +453,8 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
 
         // make contribution field required for quick config when membership block is enabled
         if (($field->name == 'membership_amount' || $field->name == 'contribution_amount')
-          && !empty($qf->_membershipBlock) && !$field->is_required) {
+          && !empty($qf->_membershipBlock) && !$field->is_required
+        ) {
           $useRequired = $field->is_required = TRUE;
         }
 
@@ -454,17 +473,17 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
 
           if ($field->is_display_amounts) {
             $opt['label'] .= '&nbsp;-&nbsp;';
-            if (isset($taxAmount)  && $invoicing) {
+            if (isset($taxAmount) && $invoicing) {
               $opt['label'] = $opt['label'] . self::getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm);
             }
             else {
               $opt['label'] = $opt['label'] . CRM_Utils_Money::format($opt[$valueFieldName]);
             }
           }
-          
+
           $selectOption[$opt['id']] = $opt['label'];
           $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
-                                       
+
           if (!in_array($opt['id'], $freezeOptions)) {
             $allowedOptions[] = $opt['id'];
           }
@@ -472,20 +491,21 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           else {
             $opt['label'] = $opt['label'] . ' (' . ts('Sold out') . ')';
           }
-                                       
+
           $selectOption[$opt['id']] = $opt['label'];
 
           if ($is_pay_later) {
-            $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4'));
+            $qf->add('text', 'txt-' . $elementName, $label, array('size' => '4'));
           }
         }
-        
+
         $element = &$qf->add('select', $elementName, $label,
           array(
-            '' => ts('- select -')) + $selectOption,
-            $useRequired && $field->is_required,
-            array('price' => json_encode($priceVal))
-          );
+            '' => ts('- select -'),
+          ) + $selectOption,
+          $useRequired && $field->is_required,
+          array('price' => json_encode($priceVal))
+        );
 
         // CRM-6902 - Add "max" option for a price set field
         $button = substr($qf->controller->getButtonName(), -4);
@@ -513,18 +533,19 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           }
           $priceVal = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
           $check[$opId] = &$qf->createElement('checkbox', $opt['id'], NULL, $opt['label'],
-            array('price' => json_encode(array($opt['id'], $priceVal)),
-             'data-amount' => $opt[$valueFieldName],
-             'data-currency' => $currencyName,
+            array(
+              'price' => json_encode(array($opt['id'], $priceVal)),
+              'data-amount' => $opt[$valueFieldName],
+              'data-currency' => $currencyName,
             )
           );
           if ($is_pay_later) {
-            $txtcheck[$opId] =& $qf->createElement( 'text', $opId, $opt['label'], array( 'size' => '4' ) );
-            $qf->addGroup($txtcheck, 'txt-'.$elementName, $label);
+            $txtcheck[$opId] =& $qf->createElement('text', $opId, $opt['label'], array('size' => '4'));
+            $qf->addGroup($txtcheck, 'txt-' . $elementName, $label);
           }
           // CRM-6902 - Add "max" option for a price set field
           if (in_array($opId, $freezeOptions)) {
-            $check[$opId]->freeze();
+            self::freezeIfEnabled($check[$opId], $customOption[$opId]);
             // CRM-14696 - Improve display for sold out price set options
             $check[$opId]->setText('<span class="sold-out-option">' . $check[$opId]->getText() . '&nbsp;(' . ts('Sold out') . ')</span>');
           }
@@ -543,11 +564,15 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   /**
    * Retrieve a list of options for the specified field
    *
-   * @param int $fieldId price field ID
-   * @param bool $inactiveNeeded include inactive options
-   * @param bool $reset ignore stored values\
+   * @param int $fieldId
+   *   Price field ID.
+   * @param bool $inactiveNeeded
+   *   Include inactive options.
+   * @param bool $reset
+   *   Ignore stored values\.
    *
-   * @return array array of options
+   * @return array
+   *   array of options
    */
   public static function getOptions($fieldId, $inactiveNeeded = FALSE, $reset = FALSE) {
     static $options = array();
@@ -564,7 +589,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         if (isset($priceFieldValues['financial_type_id']) && array_key_exists($priceFieldValues['financial_type_id'], $taxRates)) {
           $options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']];
           $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']);
-          $options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'],2);
+          $options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'], 2);
         }
       }
     }
@@ -596,7 +621,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, array(
+        1 => array($optionGroupName, 'String'),
+        2 => array($optionLabel, 'String'),
+      ));
 
     while ($dao->fetch()) {
       return $dao->id;
@@ -606,12 +634,10 @@ WHERE
   /**
    * Delete the price set field.
    *
-   * @param   int   $id    Field Id
-   *
-   * @return  boolean
+   * @param int $id
+   *   Field Id.
    *
-   * @access public
-   * @static
+   * @return bool
    *
    */
   public static function deleteField($id) {
@@ -637,7 +663,7 @@ WHERE
   /**
    * @return array
    */
-  static function &htmlTypes() {
+  public static function &htmlTypes() {
     static $htmlTypes = NULL;
     if (!$htmlTypes) {
       $htmlTypes = array(
@@ -653,7 +679,8 @@ WHERE
   /**
    * Validate the priceset
    *
-   * @param int $priceSetId , array $fields
+   * @param int $priceSetId
+   *   , array $fields.
    *
    * retrun the error string
    *
@@ -661,10 +688,7 @@ WHERE
    * @param $error
    * @param bool $allowNoneSelection
    *
-   * @access public
-   * @static
    */
-
   public static function priceSetValidation($priceSetId, $fields, &$error, $allowNoneSelection = FALSE) {
     // check for at least one positive
     // amount price field should be selected.
@@ -744,7 +768,8 @@ WHERE  id IN (" . implode(',', array_keys($priceFields)) . ')';
         if (empty($noneSelectedPriceFields)) {
           $error['_qf_default'] = ts('Please select at least one option from price set.');
         }
-      } else {
+      }
+      else {
         $error['_qf_default'] = ts('Please select at least one option from price set.');
       }
     }
@@ -754,29 +779,29 @@ WHERE  id IN (" . implode(',', array_keys($priceFields)) . ')';
    * Generate the label for price fields based on tax display setting option on CiviContribute Component Settings page.
    *
    * @param array $opt
-   * @param string $valueFieldName amount
-   * @param string $displayOpt tax display setting option
+   * @param string $valueFieldName
+   *   Amount.
+   * @param string $displayOpt
+   *   Tax display setting option.
    *
-   * @return string $label tax label for custom field
-   *
-   * @access public
-   * @static
+   * @return string
+   *   tax label for custom field
    *
    */
   public static function getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) {
     if ($displayOpt == 'Do_not_show') {
       $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
     }
-    else if ($displayOpt == 'Inclusive') {
+    elseif ($displayOpt == 'Inclusive') {
       $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
       $label .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>';
     }
     else {
       $label = CRM_Utils_Money::format($opt[$valueFieldName]);
-      $label .= '<span class="crm-price-amount-tax"> + '. CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>';
+      $label .= '<span class="crm-price-amount-tax"> + ' . CRM_Utils_Money::format($opt['tax_amount']) . ' ' . $taxTerm . '</span>';
     }
 
     return $label;
   }
-}
 
+}