APIv4 - Add Address::getCoordinates action
[civicrm-core.git] / CRM / Core / Form / Tag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form element for free tag widget.
20 */
21 class CRM_Core_Form_Tag {
22 public $_entityTagValues;
23
24 /**
25 * Build tag widget if correct parent is passed
26 *
27 * @param CRM_Core_Form $form
28 * Form object.
29 * @param array $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.
41 */
42 public static function buildQuickForm(
43 &$form, $parentNames, $entityTable, $entityId = NULL, $skipTagCreate = FALSE,
44 $skipEntityAction = FALSE, $tagsetElementName = NULL) {
45 $tagset = $form->_entityTagValues = [];
46 $form->assign('isTagset', FALSE);
47 $mode = NULL;
48
49 foreach ($parentNames as &$parentNameItem) {
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
54 if ($parentId) {
55 $tagsetItem = $tagsetElementName . 'parentId_' . $parentId;
56 $tagset[$tagsetItem]['parentID'] = $parentId;
57
58 list(, $mode) = explode('_', $entityTable);
59 if (!$tagsetElementName) {
60 $tagsetElementName = $mode . "_taglist";
61 }
62 $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName;
63
64 $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, [
65 'entity' => 'Tag',
66 'multiple' => TRUE,
67 'create' => !$skipTagCreate,
68 'api' => ['params' => ['parent_id' => $parentId]],
69 'data-entity_table' => $entityTable,
70 'data-entity_id' => $entityId,
71 'class' => "crm-$mode-tagset",
72 'select' => ['minimumInputLength' => 0],
73 ]);
74
75 if ($entityId) {
76 $tagset[$tagsetItem]['entityId'] = $entityId;
77 $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable);
78 if ($entityTags) {
79 $form->setDefaults(["{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags))]);
80 }
81 }
82 else {
83 $skipEntityAction = TRUE;
84 }
85 $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
86 }
87 }
88
89 $form->addExpectedSmartyVariable('tagsetInfo');
90 if (!empty($tagset)) {
91 // assign current tagsets which is used in postProcess
92 $form->_tagsetInfo = $tagset;
93 $form->assign("tagsetType", $mode);
94 // Merge this tagset info with possibly existing info in the template
95 $tagsetInfo = (array) $form->get_template_vars("tagsetInfo");
96 if (empty($tagsetInfo[$mode])) {
97 $tagsetInfo[$mode] = [];
98 }
99 $tagsetInfo[$mode] = array_merge($tagsetInfo[$mode], $tagset);
100 $form->assign('tagsetInfo', $tagsetInfo);
101 $form->assign('isTagset', TRUE);
102 }
103 }
104
105 /**
106 * Save entity tags when it is not save used AJAX.
107 *
108 * @param array $params
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.
115 */
116 public static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form = NULL) {
117 if ($form && !empty($form->_entityTagValues)) {
118 $existingTags = $form->_entityTagValues;
119 }
120 else {
121 $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable);
122 }
123
124 if ($form) {
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
127 // deleted correctly
128 foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) {
129 $tagsetId = explode('parentId_', $tagsetName);
130 $tagsetId = $tagsetId[1];
131 if (empty($params[$tagsetId])) {
132 $params[$tagsetId] = '';
133 }
134 }
135 }
136
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
139 foreach ($params as $parentId => $value) {
140 $newTagIds = [];
141 $tagIds = [];
142
143 if ($value) {
144 $tagIds = explode(',', $value);
145 foreach ($tagIds as $tagId) {
146 if ($form && $form->_action != CRM_Core_Action::UPDATE || !array_key_exists($tagId, $existingTags)) {
147 $newTagIds[] = $tagId;
148 }
149 }
150 }
151
152 // Any existing entity tags from this tagset missing from the $params should be deleted
153 $deleteSQL = "DELETE FROM civicrm_entity_tag
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}";
158 if (!empty($tagIds)) {
159 $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $tagIds) . ");";
160 }
161
162 CRM_Core_DAO::executeQuery($deleteSQL);
163
164 if (!empty($newTagIds)) {
165 // New tag ids can be inserted directly into the db table.
166 $insertValues = [];
167 foreach ($newTagIds as $tagId) {
168 $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
169 }
170 $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table )
171 VALUES ' . implode(', ', $insertValues) . ';';
172 CRM_Core_DAO::executeQuery($insertSQL);
173 }
174 }
175 }
176
177 }