Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-11-24-15-59-25
[civicrm-core.git] / CRM / Price / BAO / PriceFieldValue.php
index eb4db3781db672a10c1f251c14aaf9d3d10c059b..73a22e9406d965fb23eb691baf51a1c9b313e7fc 100644 (file)
@@ -44,11 +44,13 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
    *
    * @param array $params (reference), array $ids
    *
+   * @param $ids
+   *
    * @return object CRM_Price_DAO_PriceFieldValue object
    * @access public
    * @static
    */
-  static function &add(&$params, $ids) {
+  static function add(&$params, $ids = array()) {
 
     $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue();
     $fieldValueBAO->copyValues($params);
@@ -71,17 +73,22 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
    *
    * @param array $params (reference), array $ids
    *
+   * @param $ids
+   *
    * @return object CRM_Price_DAO_PriceFieldValue object
    * @access public
    * @static
    */
-  static function create(&$params, $ids) {
-
+  static function create(&$params, $ids = array()) {
+    $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
     if (!is_array($params) || empty($params)) {
       return;
     }
+    if(!$id && empty($params['name'])) {
+      $params['name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 242));
+    }
 
-    if ($id = CRM_Utils_Array::value('id', $ids)) {
+    if ($id  && !empty($params['weight'])) {
       if (isset($params['name']))unset($params['name']);
 
       $oldWeight = NULL;
@@ -93,18 +100,28 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
       $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues);
     }
     else {
-      if (empty($params['name'])) {
-        $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
-      }
-      if (empty($params['weight'])) {
-        $params['weight'] = 1;
+      if (!$id) {
+        CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
+        if (empty($params['name'])) {
+          $params['name'] = CRM_Utils_String::munge(CRM_Utils_Array::value('label', $params), '_', 64);
+        }
       }
     }
-    $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
-
     return self::add($params, $ids);
   }
 
+  /**
+   * Get defaults for new entity
+   * @return array
+   */
+  static function getDefaults() {
+    return array(
+      'is_active' => 1,
+      'weight' => 1,
+    );
+
+  }
+
   /**
    * Takes a bunch of params that are needed to match certain criteria and
    * retrieves the relevant objects.
@@ -126,7 +143,7 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
    * @param int $fieldId price_field_id
    * @param array $values (reference ) to hold the values
    * @param string $orderBy for order by, default weight
-   * @param int $isActive is_active, default false
+   * @param bool|int $isActive is_active, default false
    *
    * @return array $values
    *
@@ -184,14 +201,13 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
    *
    * @param  int    $fieldId    Price field id
    *
-   * @return boolean
    *
    * @access public
    * @static
    */
   static function deleteValues($fieldId) {
     if (!$fieldId) {
-      return FALSE;
+      return;
     }
 
     $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue();
@@ -218,13 +234,13 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
     $fieldValueDAO->id = $id;
     return $fieldValueDAO->delete();
   }
-  
+
   /**
-   * Update civicrm_price_field_value.financial_type_id 
+   * Update civicrm_price_field_value.financial_type_id
    * when financial_type_id of contribution_page or event is changed
    *
-   * @param   int   $entityId  Id
-   * @param   String $entityTable  entity table
+   * @param   int $entityId Id
+   * @param   String $entityTable entity table
    * @param   String $financialTypeID financial type id
    *
    * @access public
@@ -232,7 +248,7 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
    */
   static function updateFinancialType($entityId, $entityTable, $financialTypeID) {
     if (!$entityId || !$entityTable || !$financialTypeID) {
-      return FALSE;
+      return;
     }
     $params = array(
       1 => array($entityId, 'Integer'),
@@ -245,8 +261,8 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
       $join = " LEFT JOIN civicrm_discount cd ON cd.price_set_id = cps.id AND cd.entity_id = %1  AND cd.entity_table = %2 ";
       $where = ' OR cd.id IS NOT NULL ';
     }
-    $sql = "UPDATE civicrm_price_set cps 
-LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2 
+    $sql = "UPDATE civicrm_price_set cps
+LEFT JOIN civicrm_price_set_entity cpse ON cpse.price_set_id = cps.id AND cpse.entity_id = %1 AND cpse.entity_table = %2
 LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
 LEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id
 {$join}
@@ -257,7 +273,7 @@ SET cpfv.financial_type_id = CASE
 END,
 cps.financial_type_id = %3
 WHERE cpse.id IS NOT NULL {$where}";
-    
+
     CRM_Core_DAO::executeQuery($sql, $params);
   }
 }