Simplify input params on OptionValue::addOptionValue
authoreileen <emcnaughton@wikimedia.org>
Wed, 4 Jul 2018 08:09:03 +0000 (20:09 +1200)
committereileen <emcnaughton@wikimedia.org>
Wed, 4 Jul 2018 08:58:03 +0000 (20:58 +1200)
The addOptionValue function is one we should probably deprecate/ remove but towards that we should
start to remove it. This commit removes some complexity by passing option_group_name rather than
groupParams - which is a more accurate reflection of usage and removes ambiguity around the passing
by reference

CRM/Admin/Form/Options.php
CRM/Campaign/Form/SurveyType.php
CRM/Core/OptionValue.php
CRM/Report/Form/Register.php
api/v3/ActivityType.php

index f68718d881b5886a5e55b02c36c8232390f1170e..7083e804260e0b176b09c15241b87357727ee34e 100644 (file)
@@ -484,8 +484,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form {
         $params['color'] = 'null';
       }
 
-      $groupParams = array('name' => ($this->_gName));
-      $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id);
+      $optionValue = CRM_Core_OptionValue::addOptionValue($params, $this->_gName, $this->_action, $this->_id);
 
       // CRM-11516
       if (!empty($params['financial_account_id'])) {
index cad02eabe6b0922f5d24a083400d354e905422a1..0cce9e4b358f8a5378c1037a0952625e678f308d 100644 (file)
@@ -156,9 +156,8 @@ class CRM_Campaign_Form_SurveyType extends CRM_Admin_Form {
         $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id');
       }
 
-      $groupParams = array('name' => ($this->_gName));
       $params['component_id'] = CRM_Core_Component::getComponentID('CiviCampaign');
-      $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id);
+      $optionValue = CRM_Core_OptionValue::addOptionValue($params, $this->_gName, $this->_action, $this->_id);
 
       CRM_Core_Session::setStatus(ts('The Survey type \'%1\' has been saved.', array(1 => $optionValue->label)), ts('Saved'), 'success');
     }
index 27a1a756f07ecc39e6806aafc53bdd793a6592bb..4b1e5105ba2b2dda714ed8aaacb07e107009e356 100644 (file)
@@ -187,7 +187,7 @@ class CRM_Core_OptionValue {
    *
    * @param array $params
    *   Array containing exported values from the invoking form.
-   * @param array $groupParams
+   * @param string $optionGroupName
    *   Array containing group fields whose option-values is to retrieved/saved.
    * @param $action
    * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc.
@@ -196,22 +196,17 @@ class CRM_Core_OptionValue {
    * @return CRM_Core_DAO_OptionValue
    *
    */
-  public static function addOptionValue(&$params, &$groupParams, $action, $optionValueID) {
+  public static function addOptionValue(&$params, $optionGroupName, $action, $optionValueID) {
     $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
     // checking if the group name with the given id or name (in $groupParams) exists
-    if (!empty($groupParams)) {
-      $config = CRM_Core_Config::singleton();
-      $groupParams['is_active'] = 1;
-      $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults);
-    }
+    $groupParams = ['name' => $optionGroupName, 'is_active' => 1];
+    $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults);
 
-    // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
+    // if the corresponding group doesn't exist, create one.
     if (!$optionGroup->id) {
-      if ($groupParams['name']) {
-        $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams, $defaults);
-        $params['weight'] = 1;
-        $optionGroupID = $newOptionGroup->id;
-      }
+      $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams);
+      $params['weight'] = 1;
+      $optionGroupID = $newOptionGroup->id;
     }
     else {
       $optionGroupID = $optionGroup->id;
index 094520df38ef897606ae060162ceabac4242d219..4f7bd4602896c0828b20a5b0384935481469819f 100644 (file)
@@ -191,8 +191,7 @@ class CRM_Report_Form_Register extends CRM_Core_Form {
       // get the submitted form values.
       $params = $this->controller->exportValues($this->_name);
 
-      $groupParams = array('name' => ('report_template'));
-      $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id);
+      $optionValue = CRM_Core_OptionValue::addOptionValue($params, 'report_template', $this->_action, $this->_id);
       CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(
             1 => 'Report Template',
             2 => $optionValue->label,
index 3f6d121f1a112ee8f5a0dc6cb1812934b8651a8d..db7bd23d73c4ffcb730faeea9d7ad33cc1d90296 100644 (file)
@@ -72,13 +72,12 @@ function civicrm_api3_activity_type_get($params) {
 function civicrm_api3_activity_type_create($params) {
 
   $action = 1;
-  $groupParams = array('name' => 'activity_type');
 
   if ($optionValueID = CRM_Utils_Array::value('option_value_id', $params)) {
     $action = 2;
   }
 
-  $activityObject = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $action, $optionValueID);
+  $activityObject = CRM_Core_OptionValue::addOptionValue($params, 'activity_type', $action, $optionValueID);
   $activityType = array();
   _civicrm_api3_object_to_array($activityObject, $activityType[$activityObject->id]);
   return civicrm_api3_create_success($activityType, $params, 'activity_type', 'create');