Fixes output if location type is primary
[civicrm-core.git] / CRM / Core / OptionValue.php
index 726b147e405c2f63dfcee26719e87a14372a9733..ed649405cd60d56952bb398b16b3442f32698006 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 class CRM_Core_OptionValue {
 
@@ -37,21 +37,21 @@ class CRM_Core_OptionValue {
    *
    * @var array
    */
-  static $_exportableFields = NULL;
+  public static $_exportableFields = NULL;
 
   /**
    * Static field for all the option value information that we can potentially export.
    *
    * @var array
    */
-  static $_importableFields = NULL;
+  public static $_importableFields = NULL;
 
   /**
    * Static field for all the option value information that we can potentially export.
    *
    * @var array
    */
-  static $_fields = NULL;
+  public static $_fields = NULL;
 
   /**
    * Return option-values of a particular group
@@ -71,7 +71,7 @@ class CRM_Core_OptionValue {
    *
    */
   public static function getRows($groupParams, $links, $orderBy = 'weight', $skipEmptyComponents = TRUE) {
-    $optionValue = array();
+    $optionValue = [];
     $optionGroupID = NULL;
     $isGroupLocked = FALSE;
 
@@ -116,7 +116,7 @@ class CRM_Core_OptionValue {
     $componentNames = CRM_Core_Component::getNames();
     $visibilityLabels = CRM_Core_PseudoConstant::visibility();
     while ($dao->fetch()) {
-      $optionValue[$dao->id] = array();
+      $optionValue[$dao->id] = [];
       CRM_Core_DAO::storeValues($dao, $optionValue[$dao->id]);
       if (!empty($optionValue[$dao->id]['component_id']) &&
         empty($componentNames[$optionValue[$dao->id]['component_id']]) &&
@@ -147,7 +147,7 @@ class CRM_Core_OptionValue {
       }
 
       // disallow deletion of option values for locked groups
-      if ($isGroupLocked) {
+      if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) {
         $action -= CRM_Core_Action::DELETE;
       }
 
@@ -155,11 +155,11 @@ class CRM_Core_OptionValue {
       $optionValue[$dao->id]['order'] = $optionValue[$dao->id]['weight'];
       $optionValue[$dao->id]['icon'] = CRM_Utils_Array::value('icon', $optionValue[$dao->id], '');
       $optionValue[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
-        array(
+        [
           'id' => $dao->id,
           'gid' => $optionGroupID,
           'value' => $dao->value,
-        ),
+        ],
         ts('more'),
         FALSE,
         'optionValue.row.actions',
@@ -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,23 +196,17 @@ class CRM_Core_OptionValue {
    * @return CRM_Core_DAO_OptionValue
    *
    */
-  public static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
-    $ids = array();
+  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;
@@ -220,13 +214,13 @@ class CRM_Core_OptionValue {
       if ($optionValueID) {
         $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
       }
-      $fieldValues = array('option_group_id' => $optionGroupID);
+      $fieldValues = ['option_group_id' => $optionGroupID];
       $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues);
     }
     $params['option_group_id'] = $optionGroupID;
 
-    if (($action & CRM_Core_Action::ADD) && empty($params['value'])) {
-      $fieldValues = array('option_group_id' => $optionGroupID);
+    if (($action & CRM_Core_Action::ADD) && !isset($params['value'])) {
+      $fieldValues = ['option_group_id' => $optionGroupID];
       // use the next available value
       /* CONVERT(value, DECIMAL) is used to convert varchar
       field 'value' to decimal->integer                    */
@@ -245,9 +239,9 @@ class CRM_Core_OptionValue {
       $params['name'] = $params['label'];
     }
     if ($action & CRM_Core_Action::UPDATE) {
-      $ids['optionValue'] = $optionValueID;
+      $params['id'] = $optionValueID;
     }
-    $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
+    $optionValue = CRM_Core_BAO_OptionValue::add($params);
     return $optionValue;
   }
 
@@ -301,7 +295,7 @@ class CRM_Core_OptionValue {
   public static function getFields($mode = '', $contactType = 'Individual') {
     $key = "$mode $contactType";
     if (empty(self::$_fields[$key]) || !self::$_fields[$key]) {
-      self::$_fields[$key] = array();
+      self::$_fields[$key] = [];
 
       $option = CRM_Core_DAO_OptionValue::import();
 
@@ -309,48 +303,48 @@ class CRM_Core_OptionValue {
         $optionName = $option[$id];
       }
 
-      $nameTitle = array();
+      $nameTitle = [];
       if ($mode == 'contribute') {
         // This is part of a move towards standardising option values but we
         // should derive them from the fields array so am deprecating it again...
         // note that the reason this was needed was that payment_instrument_id was
         // not set to exportable.
-        $nameTitle = array(
-          'payment_instrument' => array(
+        $nameTitle = [
+          'payment_instrument' => [
             'name' => 'payment_instrument',
             'title' => ts('Payment Method'),
             'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
-          ),
-        );
+          ],
+        ];
       }
       elseif ($mode == '') {
         //the fields email greeting and postal greeting are meant only for Individual and Household
         //the field addressee is meant for all contact types, CRM-4575
-        if (in_array($contactType, array(
+        if (in_array($contactType, [
           'Individual',
           'Household',
           'Organization',
           'All',
-        ))) {
-          $nameTitle = array(
-            'addressee' => array(
+        ])) {
+          $nameTitle = [
+            'addressee' => [
               'name' => 'addressee',
               'title' => ts('Addressee'),
               'headerPattern' => '/^addressee$/i',
-            ),
-          );
-          $title = array(
-            'email_greeting' => array(
+            ],
+          ];
+          $title = [
+            'email_greeting' => [
               'name' => 'email_greeting',
               'title' => ts('Email Greeting'),
               'headerPattern' => '/^email_greeting$/i',
-            ),
-            'postal_greeting' => array(
+            ],
+            'postal_greeting' => [
               'name' => 'postal_greeting',
               'title' => ts('Postal Greeting'),
               'headerPattern' => '/^postal_greeting$/i',
-            ),
-          );
+            ],
+          ];
           $nameTitle = array_merge($nameTitle, $title);
         }
       }
@@ -445,7 +439,7 @@ FROM
 
     if ($groupId) {
       $where .= " AND option_group.id = %1";
-      $params[1] = array($groupId, 'Integer');
+      $params[1] = [$groupId, 'Integer'];
       if (!$groupName) {
         $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
           $groupId, 'name', 'id'
@@ -455,7 +449,7 @@ FROM
 
     if ($groupName) {
       $where .= " AND option_group.name = %2";
-      $params[2] = array($groupName, 'String');
+      $params[2] = [$groupName, 'String'];
     }
 
     if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
@@ -467,7 +461,7 @@ FROM
     $dao = CRM_Core_DAO::executeQuery($query, $params);
 
     while ($dao->fetch()) {
-      $values[$dao->id] = array(
+      $values[$dao->id] = [
         'id' => $dao->id,
         'label' => $dao->label,
         'value' => $dao->value,
@@ -476,7 +470,7 @@ FROM
         'weight' => $dao->weight,
         'is_active' => $dao->is_active,
         'is_default' => $dao->is_default,
-      );
+      ];
     }
   }