From 3a839de8aeca975ca794bd1c18a250cb79449239 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 7 May 2020 17:11:19 -0400 Subject: [PATCH] Tag create cleanup: respect created_date and created_id params --- CRM/Contact/Import/Form/Preview.php | 3 +-- CRM/Contact/Import/ImportJob.php | 3 +-- CRM/Core/BAO/Tag.php | 33 +++++++++++------------------ 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/CRM/Contact/Import/Form/Preview.php b/CRM/Contact/Import/Form/Preview.php index 5c268de11802..f251299c0edf 100644 --- a/CRM/Contact/Import/Form/Preview.php +++ b/CRM/Contact/Import/Form/Preview.php @@ -513,8 +513,7 @@ public function postProcessOld() { 'description' => $newTagDesc, 'is_active' => TRUE, ); - $id = array(); - $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id); + $addedTag = CRM_Core_BAO_Tag::add($tagParams); $tag[$addedTag->id] = 1; } //add Tag to Import diff --git a/CRM/Contact/Import/ImportJob.php b/CRM/Contact/Import/ImportJob.php index 81e875b502a2..1605ebcd6388 100644 --- a/CRM/Contact/Import/ImportJob.php +++ b/CRM/Contact/Import/ImportJob.php @@ -355,8 +355,7 @@ private function _tagImportedContactsWithNewTag( 'is_selectable' => TRUE, 'used_for' => 'civicrm_contact', ); - $id = array(); - $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id); + $addedTag = CRM_Core_BAO_Tag::add($tagParams); $this->_tag[$addedTag->id] = 1; } //add Tag to Import diff --git a/CRM/Core/BAO/Tag.php b/CRM/Core/BAO/Tag.php index 2ef3afb41180..17c0bc47cceb 100644 --- a/CRM/Core/BAO/Tag.php +++ b/CRM/Core/BAO/Tag.php @@ -412,8 +412,6 @@ public static function add(&$params, $ids = []) { } } - $tag = new CRM_Core_DAO_Tag(); - // if parent id is set then inherit used for and is hidden properties if (!empty($params['parent_id'])) { // get parent details @@ -423,34 +421,28 @@ public static function add(&$params, $ids = []) { $params['used_for'] = implode(',', $params['used_for']); } + // Hack to make white null, because html5 color widget can't be empty if (isset($params['color']) && strtolower($params['color']) === '#ffffff') { $params['color'] = ''; } - $tag->copyValues($params); - $tag->id = $id; - $hook = !$id ? 'create' : 'edit'; - CRM_Utils_Hook::pre($hook, 'Tag', $tag->id, $params); - // save creator id and time - if (!$tag->id) { - $session = CRM_Core_Session::singleton(); - $tag->created_id = $session->get('userID'); - $tag->created_date = date('YmdHis'); + if (!$id) { + $params['created_id'] = $params['created_id'] ?? CRM_Core_Session::getLoggedInContactID(); + $params['created_date'] = $params['created_date'] ?? date('YmdHis'); } - $tag->save(); - CRM_Utils_Hook::post($hook, 'Tag', $tag->id, $tag); + $tag = self::writeRecord($params); // if we modify parent tag, then we need to update all children - $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", - [ + if ($id) { + $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", [ 1 => [$tag->used_for, 'String'], 2 => [$tag->id, 'Integer'], - ] - ); + ]); + } } CRM_Core_PseudoConstant::flush(); @@ -462,11 +454,10 @@ public static function add(&$params, $ids = []) { * Check if there is data to create the object. * * @param array $params - * (reference ) an assoc array of name/value pairs. * * @return bool */ - public static function dataExists(&$params) { + public static function dataExists($params) { // Disallow empty values except for the number zero. // TODO: create a utility for this since it's needed in many places if (!empty($params['name']) || (string) $params['name'] === '0') {