Use ?? operator instead of CRM_Utils_Array::value() when fetching ids
[civicrm-core.git] / CRM / Core / BAO / Tag.php
index 9528336a3b316f1443583512d3215df886d08288..623dd412dee1d8395bf91d3984ffd71ed0d403fc 100644 (file)
@@ -1,34 +1,18 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
 
@@ -90,7 +74,7 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
   public function buildTree($usedFor = NULL, $excludeHidden = FALSE) {
     $sql = "SELECT id, parent_id, name, description, is_selectable FROM civicrm_tag";
 
-    $whereClause = array();
+    $whereClause = [];
     if ($usedFor) {
       $whereClause[] = "used_for like '%{$usedFor}%'";
     }
@@ -104,10 +88,10 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
 
     $sql .= " ORDER BY parent_id,name";
 
-    $dao = CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, FALSE, FALSE);
+    $dao = CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
 
-    $refs = array();
-    $this->tree = array();
+    $refs = [];
+    $this->tree = [];
     while ($dao->fetch()) {
       $thisref = &$refs[$dao->id];
 
@@ -136,18 +120,18 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    * @return array
    */
   public static function getTagsUsedFor(
-    $usedFor = array('civicrm_contact'),
+    $usedFor = ['civicrm_contact'],
     $buildSelect = TRUE,
     $all = FALSE,
     $parentId = NULL
   ) {
-    $tags = array();
+    $tags = [];
 
     if (empty($usedFor)) {
       return $tags;
     }
     if (!is_array($usedFor)) {
-      $usedFor = array($usedFor);
+      $usedFor = [$usedFor];
     }
 
     if ($parentId === NULL) {
@@ -185,7 +169,6 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
           $tags[$tag->id]['color'] = !empty($tag->color) ? $tag->color : NULL;
         }
       }
-      $tag->free();
     }
 
     return $tags;
@@ -210,26 +193,26 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    */
   public static function getTags(
     $usedFor = 'civicrm_contact',
-    &$tags = array(),
+    &$tags = [],
     $parentId = NULL,
     $separator = '&nbsp;&nbsp;',
     $formatSelectable = FALSE
   ) {
     if (!is_array($tags)) {
-      $tags = array();
+      $tags = [];
     }
     // We need to build a list of tags ordered by hierarchy and sorted by
     // name. The hierarchy will be communicated by an accumulation of
     // separators in front of the name to give it a visual offset.
     // Instead of recursively making mysql queries, we'll make one big
     // query and build the hierarchy with the algorithm below.
-    $args = array(1 => array('%' . $usedFor . '%', 'String'));
+    $args = [1 => ['%' . $usedFor . '%', 'String']];
     $query = "SELECT id, name, parent_id, is_tagset, is_selectable
                   FROM civicrm_tag
               WHERE used_for LIKE %1";
     if ($parentId) {
       $query .= " AND parent_id = %2";
-      $args[2] = array($parentId, 'Integer');
+      $args[2] = [$parentId, 'Integer'];
     }
     $query .= " ORDER BY name";
     $dao = CRM_Core_DAO::executeQuery($query, $args, TRUE, NULL, FALSE, FALSE);
@@ -239,7 +222,7 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
     // $roots represents the current leaf nodes that need to be checked for
     // children. $rows represents the unplaced nodes, not all of much
     // are necessarily placed.
-    $roots = $rows = array();
+    $roots = $rows = [];
     while ($dao->fetch()) {
       // note that we are prepending id with "crm_disabled_opt" which identifies
       // them as disabled so that they cannot be selected. We do some magic
@@ -252,39 +235,38 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
         $idPrefix = "crm_disabled_opt";
       }
       if ($dao->parent_id == $parentId && $dao->is_tagset == 0) {
-        $roots[] = array(
+        $roots[] = [
           'id' => $dao->id,
           'prefix' => '',
           'name' => $dao->name,
           'idPrefix' => $idPrefix,
-        );
+        ];
       }
       else {
-        $rows[] = array(
+        $rows[] = [
           'id' => $dao->id,
           'prefix' => '',
           'name' => $dao->name,
           'parent_id' => $dao->parent_id,
           'idPrefix' => $idPrefix,
-        );
+        ];
       }
     }
 
-    $dao->free();
     // While we have nodes left to build, shift the first (alphabetically)
     // node of the list, place it in our tags list and loop through the
     // list of unplaced nodes to find its children. We make a copy to
     // iterate through because we must modify the unplaced nodes list
     // during the loop.
     while (count($roots)) {
-      $new_roots = array();
+      $new_roots = [];
       $current_rows = $rows;
       $root = array_shift($roots);
-      $tags[$root['id']] = array(
+      $tags[$root['id']] = [
         $root['prefix'],
         $root['name'],
         $root['idPrefix'],
-      );
+      ];
 
       // As you find the children, append them to the end of the new set
       // of roots (maintain alphabetical ordering). Also remove the node
@@ -292,12 +274,12 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
       if (is_array($current_rows)) {
         foreach ($current_rows as $key => $row) {
           if ($row['parent_id'] == $root['id']) {
-            $new_roots[] = array(
+            $new_roots[] = [
               'id' => $row['id'],
               'prefix' => $tags[$root['id']][0] . $separator,
               'name' => $row['name'],
               'idPrefix' => $row['idPrefix'],
-            );
+            ];
             unset($rows[$key]);
           }
         }
@@ -312,7 +294,7 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
     // appearance of ordering when transformed into HTML in the form layer.
     // here is the actual code that to prepends and set disabled attribute for
     // non-selectable tags
-    $formattedTags = array();
+    $formattedTags = [];
     foreach ($tags as $key => $tag) {
       if (!empty($tag[2])) {
         $key = $tag[2] . "-" . $key;
@@ -332,30 +314,30 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    * @throws \CiviCRM_API3_Exception
    */
   public static function getColorTags($usedFor = NULL, $allowSelectingNonSelectable = FALSE, $exclude = NULL) {
-    $params = array(
-      'options' => array(
+    $params = [
+      'options' => [
         'limit' => 0,
         'sort' => "name ASC",
-      ),
+      ],
       'is_tagset' => 0,
-      'return' => array('name', 'description', 'parent_id', 'color', 'is_selectable', 'used_for'),
-    );
+      'return' => ['name', 'description', 'parent_id', 'color', 'is_selectable', 'used_for'],
+    ];
     if ($usedFor) {
-      $params['used_for'] = array('LIKE' => "%$usedFor%");
+      $params['used_for'] = ['LIKE' => "%$usedFor%"];
     }
     if ($exclude) {
-      $params['id'] = array('!=' => $exclude);
+      $params['id'] = ['!=' => $exclude];
     }
-    $allTags = array();
+    $allTags = [];
     foreach (CRM_Utils_Array::value('values', civicrm_api3('Tag', 'get', $params)) as $id => $tag) {
-      $allTags[$id] = array(
+      $allTags[$id] = [
         'text' => $tag['name'],
         'id' => $id,
         'description' => CRM_Utils_Array::value('description', $tag),
         'parent_id' => CRM_Utils_Array::value('parent_id', $tag),
         'used_for' => CRM_Utils_Array::value('used_for', $tag),
         'color' => CRM_Utils_Array::value('color', $tag),
-      );
+      ];
       if (!$allowSelectingNonSelectable && empty($tag['is_selectable'])) {
         $allTags[$id]['disabled'] = TRUE;
       }
@@ -410,8 +392,8 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    * @return CRM_Core_DAO_Tag|null
    *   object on success, otherwise null
    */
-  public static function add(&$params, $ids = array()) {
-    $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('tag', $ids));
+  public static function add(&$params, $ids = []) {
+    $id = $params['id'] ?? $ids['tag'] ?? NULL;
     if (!$id && !self::dataExists($params)) {
       return NULL;
     }
@@ -464,13 +446,15 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
     $tag->find(TRUE);
     if (!$tag->parent_id && $tag->used_for) {
       CRM_Core_DAO::executeQuery("UPDATE civicrm_tag SET used_for=%1 WHERE parent_id = %2",
-        array(
-          1 => array($tag->used_for, 'String'),
-          2 => array($tag->id, 'Integer'),
-        )
+        [
+          1 => [$tag->used_for, 'String'],
+          2 => [$tag->id, 'Integer'],
+        ]
       );
     }
 
+    CRM_Core_PseudoConstant::flush();
+
     return $tag;
   }
 
@@ -502,19 +486,18 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    *   array of tag sets
    */
   public static function getTagSet($entityTable) {
-    $tagSets = array();
+    $tagSets = [];
     $query = "SELECT name, id FROM civicrm_tag
               WHERE is_tagset=1 AND parent_id IS NULL and used_for LIKE %1";
-    $dao = CRM_Core_DAO::executeQuery($query, array(
-        1 => array(
-          '%' . $entityTable . '%',
-          'String',
-        ),
-      ), TRUE, NULL, FALSE, FALSE);
+    $dao = CRM_Core_DAO::executeQuery($query, [
+      1 => [
+        '%' . $entityTable . '%',
+        'String',
+      ],
+    ], TRUE, NULL, FALSE, FALSE);
     while ($dao->fetch()) {
       $tagSets[$dao->id] = $dao->name;
     }
-    $dao->free();
     return $tagSets;
   }
 
@@ -525,7 +508,7 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    *   associated array of tag name and id
    */
   public static function getTagsNotInTagset() {
-    $tags = $tagSets = array();
+    $tags = $tagSets = [];
     // first get all the tag sets
     $query = "SELECT id FROM civicrm_tag WHERE is_tagset=1 AND parent_id IS NULL";
     $dao = CRM_Core_DAO::executeQuery($query);
@@ -553,24 +536,24 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
    *
    * @param string $searchString
    *
-   * @return array $childTagIDs
+   * @return array
    *   associated array of child tags in Array('Parent Tag ID' => Array('Child Tag 1', ...)) format
    */
   public static function getChildTags($searchString = NULL) {
-    $childTagIDs = array();
+    $childTagIDs = [];
 
-    $whereClauses = array('parent.is_tagset <> 1');
+    $whereClauses = ['parent.is_tagset <> 1'];
     if ($searchString) {
       $whereClauses[] = " child.name LIKE '%$searchString%' ";
     }
 
     // only fetch those tags which has child tags
     $dao = CRM_Utils_SQL_Select::from('civicrm_tag parent')
-              ->join('child', 'INNER JOIN civicrm_tag child ON child.parent_id = parent.id ')
-              ->select('parent.id as parent_id, GROUP_CONCAT(child.id) as child_id')
-              ->where($whereClauses)
-              ->groupBy('parent.id')
-              ->execute();
+      ->join('child', 'INNER JOIN civicrm_tag child ON child.parent_id = parent.id ')
+      ->select('parent.id as parent_id, GROUP_CONCAT(child.id) as child_id')
+      ->where($whereClauses)
+      ->groupBy('parent.id')
+      ->execute();
     while ($dao->fetch()) {
       $childTagIDs[$dao->parent_id] = (array) explode(',', $dao->child_id);
       $parentID = $dao->parent_id;
@@ -579,7 +562,7 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
         while ($parentID) {
           $newParentID = CRM_Core_DAO::singleValueQuery(" SELECT parent_id FROM civicrm_tag WHERE id = $parentID ");
           if ($newParentID) {
-            $childTagIDs[$newParentID] = array($parentID);
+            $childTagIDs[$newParentID] = [$parentID];
           }
           $parentID = $newParentID;
         }