Port missing function in 4.6. Also some minor code style syncs.
authoreileen <emcnaughton@wikimedia.org>
Tue, 21 Jun 2016 22:40:07 +0000 (10:40 +1200)
committereileen <emcnaughton@wikimedia.org>
Tue, 21 Jun 2016 22:41:08 +0000 (10:41 +1200)
CRM/Core/BAO/OptionGroup.php
CRM/Core/OptionGroup.php

index 276d9b25c0ff33926f34b6e3867284caa033199f..995db1522fa8edf1e1dd6bd488c5ed19b1a28c91 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
+ * @copyright CiviCRM LLC (c) 2004-2016
  * $Id$
  *
  */
@@ -70,7 +70,7 @@ class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
    *   Value we want to set the is_active field.
    *
    * @return Object
-   *   DAO object on sucess, null otherwise
+   *   DAO object on success, null otherwise
    */
   public static function setIsActive($id, $is_active) {
     return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
@@ -141,4 +141,30 @@ class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
     return $optionGroup->name;
   }
 
+  /**
+   * Ensure an option group exists.
+   *
+   * This function is intended to be called from the upgrade script to ensure
+   * that an option group exists, without hitting an error if it already exists.
+   *
+   * This is sympathetic to sites who might pre-add it.
+   *
+   * @param array $params
+   *
+   * @return int
+   *   ID of the option group.
+   */
+  public static function ensureOptionGroupExists($params) {
+    $existingValues = civicrm_api3('OptionGroup', 'get', array(
+      'name' => $params['name'],
+    ));
+    if (!$existingValues['count']) {
+      $result = civicrm_api3('OptionGroup', 'create', $params);
+      return $result['id'];
+    }
+    else {
+      return $existingValues['id'];
+    }
+  }
+
 }
index 9756837f5e03ba029cd5956498a6213381726b6b..fd9475571a4949cdba38d7e67e2f6d6e07ade23b 100644 (file)
@@ -28,9 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2016
  */
 class CRM_Core_OptionGroup {
   static $_values = array();
@@ -89,7 +87,7 @@ class CRM_Core_OptionGroup {
    * This function retrieves all the values for the specific option group by name
    * this is primarily used to create various html based form elements
    * (radio, select, checkbox etc). OptionGroups for most cases have the
-   * 'label' in the label colum and the 'id' or 'name' in the value column
+   * 'label' in the label column and the 'id' or 'name' in the value column
    *
    * @param string $name
    *   name of the option group.
@@ -201,7 +199,7 @@ WHERE  v.option_group_id = g.id
    * This function retrieves all the values for the specific option group by id.
    * this is primarily used to create various html based form elements
    * (radio, select, checkbox etc). OptionGroups for most cases have the
-   * 'label' in the label colum and the 'id' or 'name' in the value column
+   * 'label' in the label column and the 'id' or 'name' in the value column
    *
    * @param int $id
    *   id of the option group.
@@ -272,9 +270,6 @@ WHERE  v.option_group_id = g.id
    *
    *
    * @param bool $flip
-   *
-   * @return void
-   *
    */
   public static function lookupValues(&$params, &$names, $flip = FALSE) {
     foreach ($names as $postName => $value) {
@@ -368,6 +363,10 @@ WHERE  v.option_group_id = g.id
   }
 
   /**
+   * @deprecated
+   *
+   * This function is not cached.
+   *
    * @param string $groupName
    * @param $label
    * @param string $labelField