Skip to content

Commit

Permalink
Tag create cleanup: respect created_date and created_id params
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed May 8, 2020
1 parent 0240b5d commit 3a839de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
3 changes: 1 addition & 2 deletions CRM/Contact/Import/Form/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions CRM/Contact/Import/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 12 additions & 21 deletions CRM/Core/BAO/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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') {
Expand Down

0 comments on commit 3a839de

Please sign in to comment.