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 d53f27a18555eca8decaf4dd60791d4d2294975b..73a22e9406d965fb23eb691baf51a1c9b313e7fc 100644 (file)
@@ -80,15 +80,15 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue {
    * @static
    */
   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(empty($params['id']) && empty($params['name'])) {
+    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;
@@ -100,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.