allow more log
[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.
8d99610f 29 * @param array $parentNames
6a0b768e
TO
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 = [];
8d99610f 46 $form->assign('isTagset', FALSE);
6a488035
TO
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
8d0370a1 89 $form->addExpectedSmartyVariable('tagsetInfo');
6a488035 90 if (!empty($tagset)) {
2b2ba6a5 91 // assign current tagsets which is used in postProcess
2a6e33c8 92 $form->_tagsetInfo = $tagset;
f70ca446 93 $form->assign("tagsetType", $mode);
310bbbe5 94 // Merge this tagset info with possibly existing info in the template
6d538af3 95 $tagsetInfo = (array) $form->get_template_vars("tagsetInfo");
310bbbe5 96 if (empty($tagsetInfo[$mode])) {
be2fb01f 97 $tagsetInfo[$mode] = [];
310bbbe5
CW
98 }
99 $tagsetInfo[$mode] = array_merge($tagsetInfo[$mode], $tagset);
8d99610f
EM
100 $form->assign('tagsetInfo', $tagsetInfo);
101 $form->assign('isTagset', TRUE);
6a488035
TO
102 }
103 }
104
105 /**
fe482240 106 * Save entity tags when it is not save used AJAX.
6a488035 107 *
6a0b768e 108 * @param array $params
6a0b768e
TO
109 * @param int $entityId
110 * Entity id, eg: contact id, activity id, case id, file id.
111 * @param string $entityTable
112 * Entity table.
113 * @param CRM_Core_Form $form
114 * Form object.
6a488035 115 */
79564bd4 116 public static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form = NULL) {
6fd5424b 117 if ($form && !empty($form->_entityTagValues)) {
118 $existingTags = $form->_entityTagValues;
119 }
120 else {
121 $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable);
122 }
123
2a6e33c8 124 if ($form) {
2b2ba6a5 125 // if the key is missing from the form response then all the tags were deleted / cleared
126 // in that case we create empty tagset params so that below logic works and tagset are
e450bc79 127 // deleted correctly
2a6e33c8 128 foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) {
703aec38 129 $tagsetId = explode('parentId_', $tagsetName);
130 $tagsetId = $tagsetId[1];
2a6e33c8 131 if (empty($params[$tagsetId])) {
132 $params[$tagsetId] = '';
133 }
6a488035 134 }
2a6e33c8 135 }
6fd5424b 136
2b2ba6a5 137 // when form is submitted with tagset values below logic will work and in the case when all tags in a tagset
138 // are deleted we will have to set $params[tagset id] = '' which is done by above logic
2a6e33c8 139 foreach ($params as $parentId => $value) {
be2fb01f
CW
140 $newTagIds = [];
141 $tagIds = [];
63b69fae 142
2a6e33c8 143 if ($value) {
47bc9cec
CW
144 $tagIds = explode(',', $value);
145 foreach ($tagIds as $tagId) {
146 if ($form && $form->_action != CRM_Core_Action::UPDATE || !array_key_exists($tagId, $existingTags)) {
63b69fae 147 $newTagIds[] = $tagId;
0cc0faa9 148 }
6a488035 149 }
2a6e33c8 150 }
6a488035 151
2a6e33c8 152 // Any existing entity tags from this tagset missing from the $params should be deleted
153 $deleteSQL = "DELETE FROM civicrm_entity_tag
63b69fae 154 USING civicrm_entity_tag, civicrm_tag
155 WHERE civicrm_tag.id=civicrm_entity_tag.tag_id
156 AND civicrm_entity_tag.entity_table='{$entityTable}'
157 AND entity_id={$entityId} AND parent_id={$parentId}";
47bc9cec
CW
158 if (!empty($tagIds)) {
159 $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $tagIds) . ");";
2a6e33c8 160 }
63b69fae 161
2a6e33c8 162 CRM_Core_DAO::executeQuery($deleteSQL);
6a488035 163
2a6e33c8 164 if (!empty($newTagIds)) {
165 // New tag ids can be inserted directly into the db table.
be2fb01f 166 $insertValues = [];
2a6e33c8 167 foreach ($newTagIds as $tagId) {
168 $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
6a488035 169 }
2a6e33c8 170 $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table )
171 VALUES ' . implode(', ', $insertValues) . ';';
172 CRM_Core_DAO::executeQuery($insertSQL);
6a488035
TO
173 }
174 }
175 }
96025800 176
6a488035 177}