Merge branch 'CRM-12133' into master-CRM-12133
[civicrm-core.git] / CRM / Core / BAO / OptionValue.php
index 0340feabbc4e08febb5f4fc88f27e0acaa6f78db..edc771bf333f87a8757eb3ec0dc8b9ffa6bba20e 100644 (file)
@@ -40,7 +40,81 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
   function __construct() {
     parent::__construct();
   }
+  /**
+   * Create option value - note that the create function calls 'add' but
+  * has more business logic
+  *
+  * @param array $params input parameters
+  */
+  static function create($params) {
+    if (empty($params['id'])){
+      self::setDefaults($params);
+    }
+    $ids = array();
+    if (CRM_Utils_Array::value('id', $params)) {
+      $ids = array('optionValue' => $params['id']);
+    }
+    return  CRM_Core_BAO_OptionValue::add($params, $ids);
+    ;
+  }
+  /**
+   * Set default Parameters
+   * This functions sets default parameters if not set:
+   * - name & label are set to each other as required (it might make more sense for one
+   * to be required but this would mean a change to the api level)
+   * - weight & value will be set to their respective option groups next values
+   * if nothing is passed in.
+   *
+   * Note this function does not check for presence of $params['id'] so should only be called
+   * if 'id' is not present
+   *
+   * @param array $params
+   */
+  static function setDefaults(&$params){
+    if(CRM_Utils_Array::value('label', $params, NULL) === NULL){
+      $params['label'] = $params['name'];
+    }
+    if(CRM_Utils_Array::value('name', $params, NULL) === NULL){
+      $params['name'] = $params['label'];
+    }
+    if(CRM_Utils_Array::value('weight', $params, NULL) === NULL){
+      $params['weight'] = self::getDefaultWeight($params);
+    }
+    if (CRM_Utils_Array::value('value', $params, NULL) === NULL){
+      $params['value'] = self::getDefaultValue($params);
+    }
+  }
+  /**
+   * Get next available value
+   * We will take the highest numeric value (or 0 if no numeric values exist)
+   * and add one. The calling function is responsible for any
+   * more complex decision making
+   * @param array $params
+   */
+  static function getDefaultWeight($params){
+    return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
+          array('option_group_id' => $params['option_group_id']));
+  }
 
+  /**
+   * Get next available value
+   * We will take the highest numeric value (or 0 if no numeric values exist)
+   * and add one. The calling function is responsible for any
+   * more complex decision making
+   * @param array $params
+   */
+  static function getDefaultValue($params){
+     $bao = new CRM_Core_BAO_OptionValue();
+     $bao->option_group_id = $params['option_group_id'];
+     if(isset($params['domain_id'])){
+       $bao->domain_id = $params['domain_id'];
+     }
+     $bao->selectAdd();
+     $bao->whereAdd("value REGEXP '^[0-9]+$'");
+     $bao->selectAdd('(ROUND(COALESCE(MAX(value),0)) +1) as nextvalue');
+     $bao->find(TRUE);
+     return $bao->nextvalue;
+  }
   /**
    * Takes a bunch of params that are needed to match certain criteria and
    * retrieves the relevant objects. Typically the valid params are only
@@ -91,6 +165,10 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
    */
   static function add(&$params, &$ids) {
     // CRM-10921: do not reset attributes to default if this is an update
+    //@todo consider if defaults are being set in the right place. 'dumb' defaults like
+    // these would be usefully set @ the api layer so they are visible to api users
+    // complex defaults like the domain id below would make sense in the setDefauls function
+    // but unclear what other ways this function is being used
     if (!CRM_Utils_Array::value('optionValue', $ids)) {
       $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
       $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
@@ -117,7 +195,6 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
       $p = array(1 => array($params['option_group_id'], 'Integer'));
       CRM_Core_DAO::executeQuery($query, $p);
     }
-
     $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
       $params['option_group_id'], 'name', 'id'
     );