Merge pull request #19044 from seamuslee001/dev_drupal_149
[civicrm-core.git] / CRM / Core / Form / Tag.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
8eedd10a 19 * This class generates form element for free tag widget.
6a488035
TO
20 */
21class CRM_Core_Form_Tag {
22 public $_entityTagValues;
23
24 /**
100fef9d 25 * Build tag widget if correct parent is passed
6a488035 26 *
6a0b768e
TO
27 * @param CRM_Core_Form $form
28 * Form object.
29 * @param string $parentNames
30 * Parent name ( tag name).
31 * @param string $entityTable
32 * Entitytable 'eg: civicrm_contact'.
33 * @param int $entityId
34 * Entityid 'eg: contact id'.
35 * @param bool $skipTagCreate
36 * True if tag need be created using ajax.
37 * @param bool $skipEntityAction
38 * True if need to add entry in entry table via ajax.
39 * @param string $tagsetElementName
40 * If you need to create tagsetlist with specific name.
6a488035 41 */
608e6658 42 public static function buildQuickForm(
f9f40af3 43 &$form, $parentNames, $entityTable, $entityId = NULL, $skipTagCreate = FALSE,
481a74f4 44 $skipEntityAction = FALSE, $tagsetElementName = NULL) {
be2fb01f 45 $tagset = $form->_entityTagValues = [];
6a488035
TO
46 $form->assign("isTagset", FALSE);
47 $mode = NULL;
48
310bbbe5 49 foreach ($parentNames as &$parentNameItem) {
6a488035
TO
50 // get the parent id for tag list input for keyword
51 $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name');
52
53 // check if parent exists
6a488035 54 if ($parentId) {
310bbbe5 55 $tagsetItem = $tagsetElementName . 'parentId_' . $parentId;
6a488035
TO
56 $tagset[$tagsetItem]['parentID'] = $parentId;
57
310bbbe5
CW
58 list(, $mode) = explode('_', $entityTable);
59 if (!$tagsetElementName) {
60 $tagsetElementName = $mode . "_taglist";
6a488035 61 }
6a488035 62 $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName;
310bbbe5 63
be2fb01f 64 $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, [
af00ced5 65 'entity' => 'Tag',
310bbbe5
CW
66 'multiple' => TRUE,
67 'create' => !$skipTagCreate,
be2fb01f 68 'api' => ['params' => ['parent_id' => $parentId]],
310bbbe5
CW
69 'data-entity_table' => $entityTable,
70 'data-entity_id' => $entityId,
71 'class' => "crm-$mode-tagset",
be2fb01f
CW
72 'select' => ['minimumInputLength' => 0],
73 ]);
6a488035
TO
74
75 if ($entityId) {
76 $tagset[$tagsetItem]['entityId'] = $entityId;
77 $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable);
e4f4dc22 78 if ($entityTags) {
be2fb01f 79 $form->setDefaults(["{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags))]);
6a488035
TO
80 }
81 }
e4f4dc22
CW
82 else {
83 $skipEntityAction = TRUE;
6a488035 84 }
310bbbe5 85 $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
6a488035
TO
86 }
87 }
88
89 if (!empty($tagset)) {
2b2ba6a5 90 // assign current tagsets which is used in postProcess
2a6e33c8 91 $form->_tagsetInfo = $tagset;
f70ca446 92 $form->assign("tagsetType", $mode);
310bbbe5 93 // Merge this tagset info with possibly existing info in the template
6d538af3 94 $tagsetInfo = (array) $form->get_template_vars("tagsetInfo");
310bbbe5 95 if (empty($tagsetInfo[$mode])) {
be2fb01f 96 $tagsetInfo[$mode] = [];
310bbbe5
CW
97 }
98 $tagsetInfo[$mode] = array_merge($tagsetInfo[$mode], $tagset);
6d538af3 99 $form->assign("tagsetInfo", $tagsetInfo);
6a488035
TO
100 $form->assign("isTagset", TRUE);
101 }
102 }
103
104 /**
fe482240 105 * Save entity tags when it is not save used AJAX.
6a488035 106 *
6a0b768e 107 * @param array $params
6a0b768e
TO
108 * @param int $entityId
109 * Entity id, eg: contact id, activity id, case id, file id.
110 * @param string $entityTable
111 * Entity table.
112 * @param CRM_Core_Form $form
113 * Form object.
6a488035 114 */
79564bd4 115 public static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form = NULL) {
6fd5424b 116 if ($form && !empty($form->_entityTagValues)) {
117 $existingTags = $form->_entityTagValues;
118 }
119 else {
120 $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable);
121 }
122
2a6e33c8 123 if ($form) {
2b2ba6a5 124 // if the key is missing from the form response then all the tags were deleted / cleared
125 // in that case we create empty tagset params so that below logic works and tagset are
e450bc79 126 // deleted correctly
2a6e33c8 127 foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) {
703aec38 128 $tagsetId = explode('parentId_', $tagsetName);
129 $tagsetId = $tagsetId[1];
2a6e33c8 130 if (empty($params[$tagsetId])) {
131 $params[$tagsetId] = '';
132 }
6a488035 133 }
2a6e33c8 134 }
6fd5424b 135
2b2ba6a5 136 // when form is submitted with tagset values below logic will work and in the case when all tags in a tagset
137 // are deleted we will have to set $params[tagset id] = '' which is done by above logic
2a6e33c8 138 foreach ($params as $parentId => $value) {
be2fb01f
CW
139 $newTagIds = [];
140 $tagIds = [];
63b69fae 141
2a6e33c8 142 if ($value) {
47bc9cec
CW
143 $tagIds = explode(',', $value);
144 foreach ($tagIds as $tagId) {
145 if ($form && $form->_action != CRM_Core_Action::UPDATE || !array_key_exists($tagId, $existingTags)) {
63b69fae 146 $newTagIds[] = $tagId;
0cc0faa9 147 }
6a488035 148 }
2a6e33c8 149 }
6a488035 150
2a6e33c8 151 // Any existing entity tags from this tagset missing from the $params should be deleted
152 $deleteSQL = "DELETE FROM civicrm_entity_tag
63b69fae 153 USING civicrm_entity_tag, civicrm_tag
154 WHERE civicrm_tag.id=civicrm_entity_tag.tag_id
155 AND civicrm_entity_tag.entity_table='{$entityTable}'
156 AND entity_id={$entityId} AND parent_id={$parentId}";
47bc9cec
CW
157 if (!empty($tagIds)) {
158 $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $tagIds) . ");";
2a6e33c8 159 }
63b69fae 160
2a6e33c8 161 CRM_Core_DAO::executeQuery($deleteSQL);
6a488035 162
2a6e33c8 163 if (!empty($newTagIds)) {
164 // New tag ids can be inserted directly into the db table.
be2fb01f 165 $insertValues = [];
2a6e33c8 166 foreach ($newTagIds as $tagId) {
167 $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
6a488035 168 }
2a6e33c8 169 $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table )
170 VALUES ' . implode(', ', $insertValues) . ';';
171 CRM_Core_DAO::executeQuery($insertSQL);
6a488035
TO
172 }
173 }
174 }
96025800 175
6a488035 176}