Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form element for free tag widget | |
38 | * | |
39 | */ | |
40 | class CRM_Core_Form_Tag { | |
41 | public $_entityTagValues; | |
42 | ||
43 | /** | |
44 | * Function to build tag widget if correct parent is passed | |
45 | * | |
6d538af3 | 46 | * @param CRM_Core_Form $form form object |
e4f4dc22 | 47 | * @param string $parentNames parent name ( tag name) |
6a488035 TO |
48 | * @param string $entityTable entitytable 'eg: civicrm_contact' |
49 | * @param int $entityId entityid 'eg: contact id' | |
50 | * @param boolean $skipTagCreate true if tag need be created using ajax | |
51 | * @param boolean $skipEntityAction true if need to add entry in entry table via ajax | |
52 | * @param boolean $searchMode true if widget is used in search eg: advanced search | |
53 | * @param string $tagsetElementName if you need to create tagsetlist with specific name | |
54 | * | |
55 | * @return void | |
56 | * @access public | |
57 | * @static | |
58 | */ | |
59 | static function buildQuickForm(&$form, $parentNames, $entityTable, $entityId = NULL, $skipTagCreate = FALSE, | |
60 | $skipEntityAction = FALSE, $searchMode = FALSE, $tagsetElementName = NULL ) { | |
61 | $tagset = $form->_entityTagValues = array(); | |
62 | $form->assign("isTagset", FALSE); | |
63 | $mode = NULL; | |
64 | ||
65 | foreach ($parentNames as & $parentNameItem) { | |
66 | // get the parent id for tag list input for keyword | |
67 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name'); | |
68 | ||
69 | // check if parent exists | |
6a488035 TO |
70 | if ($parentId) { |
71 | $tagsetItem = 'parentId_' . $parentId; | |
e4f4dc22 | 72 | $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction; |
6a488035 TO |
73 | $tagset[$tagsetItem]['parentID'] = $parentId; |
74 | ||
e4f4dc22 CW |
75 | // Fixme: these 3 variables may be unused |
76 | $tagset[$tagsetItem]['parentName'] = $parentNameItem; | |
6a488035 TO |
77 | $tagset[$tagsetItem]['entityTable'] = $entityTable; |
78 | $tagset[$tagsetItem]['skipTagCreate'] = $skipTagCreate; | |
6a488035 TO |
79 | |
80 | switch ($entityTable) { | |
81 | case 'civicrm_activity': | |
82 | $tagsetElementName = "activity_taglist"; | |
83 | $mode = 'activity'; | |
84 | break; | |
85 | ||
86 | case 'civicrm_case': | |
87 | $tagsetElementName = "case_taglist"; | |
88 | $mode = 'case'; | |
89 | break; | |
90 | ||
91 | case 'civicrm_file': | |
92 | $mode = 'attachment'; | |
93 | break; | |
94 | ||
95 | default: | |
96 | $tagsetElementName = "contact_taglist"; | |
97 | $mode = 'contact'; | |
98 | } | |
99 | ||
100 | $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName; | |
101 | if ($tagsetElementName) { | |
e4f4dc22 | 102 | $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, array( |
6d538af3 CW |
103 | 'entity' => 'tag', |
104 | 'multiple' => TRUE, | |
e4f4dc22 CW |
105 | 'create' => !$skipTagCreate, |
106 | 'api' => array('params' => array('parent_id' => $parentId)), | |
107 | 'data-entity_table' => $entityTable, | |
108 | 'data-entity_id' => $entityId, | |
109 | 'class' => "crm-$mode-tagset", | |
110 | )); | |
6a488035 TO |
111 | } |
112 | ||
113 | if ($entityId) { | |
114 | $tagset[$tagsetItem]['entityId'] = $entityId; | |
115 | $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable); | |
e4f4dc22 CW |
116 | if ($entityTags) { |
117 | $form->setDefaults(array("{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags)))); | |
6a488035 TO |
118 | } |
119 | } | |
e4f4dc22 CW |
120 | else { |
121 | $skipEntityAction = TRUE; | |
6a488035 TO |
122 | } |
123 | } | |
124 | } | |
125 | ||
126 | if (!empty($tagset)) { | |
2b2ba6a5 | 127 | // assign current tagsets which is used in postProcess |
2a6e33c8 | 128 | $form->_tagsetInfo = $tagset; |
f70ca446 | 129 | $form->assign("tagsetType", $mode); |
6d538af3 CW |
130 | $tagsetInfo = (array) $form->get_template_vars("tagsetInfo"); |
131 | $tagsetInfo[$mode] = $tagset; | |
132 | $form->assign("tagsetInfo", $tagsetInfo); | |
6a488035 TO |
133 | $form->assign("isTagset", TRUE); |
134 | } | |
135 | } | |
136 | ||
137 | /** | |
138 | * Function to save entity tags when it is not save used AJAX | |
139 | * | |
6fd5424b | 140 | * @param array $params associated array |
141 | * @param int $entityId entity id, eg: contact id, activity id, case id, file id | |
142 | * @param string $entityTable entity table | |
143 | * @param object $form form object | |
6fd5424b | 144 | * |
145 | * @return void | |
146 | * @access public | |
147 | * @static | |
6a488035 TO |
148 | */ |
149 | static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form) { | |
6fd5424b | 150 | if ($form && !empty($form->_entityTagValues)) { |
151 | $existingTags = $form->_entityTagValues; | |
152 | } | |
153 | else { | |
154 | $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable); | |
155 | } | |
156 | ||
2a6e33c8 | 157 | if ($form) { |
2b2ba6a5 | 158 | // if the key is missing from the form response then all the tags were deleted / cleared |
159 | // in that case we create empty tagset params so that below logic works and tagset are | |
e450bc79 | 160 | // deleted correctly |
2a6e33c8 | 161 | foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) { |
162 | $tagsetId = substr($tagsetName, strlen('parentId_')); | |
163 | if (empty($params[$tagsetId])) { | |
164 | $params[$tagsetId] = ''; | |
165 | } | |
6a488035 | 166 | } |
2a6e33c8 | 167 | } |
6fd5424b | 168 | |
2b2ba6a5 | 169 | // when form is submitted with tagset values below logic will work and in the case when all tags in a tagset |
170 | // are deleted we will have to set $params[tagset id] = '' which is done by above logic | |
2a6e33c8 | 171 | foreach ($params as $parentId => $value) { |
63b69fae | 172 | $newTagIds = array(); |
173 | $realTagIds = array(); | |
174 | ||
2a6e33c8 | 175 | if ($value) { |
176 | $tagsIDs = explode(',', $value); | |
6a488035 | 177 | foreach ($tagsIDs as $tagId) { |
0cc0faa9 | 178 | if (!is_numeric($tagId)) { |
179 | // check if user has selected existing tag or is creating new tag | |
180 | // this is done to allow numeric tags etc. | |
181 | $tagValue = explode(':::', $tagId); | |
182 | ||
183 | if (isset($tagValue[1]) && $tagValue[1] == 'value') { | |
6fd5424b | 184 | $tagParams = array( |
0cc0faa9 | 185 | 'name' => $tagValue[0], |
186 | 'parent_id' => $parentId, | |
187 | ); | |
6fd5424b | 188 | $tagObject = CRM_Core_BAO_Tag::add($tagParams, CRM_Core_DAO::$_nullArray); |
0cc0faa9 | 189 | $tagId = $tagObject->id; |
6a488035 TO |
190 | } |
191 | } | |
0cc0faa9 | 192 | |
63b69fae | 193 | $realTagIds[] = $tagId; |
0cc0faa9 | 194 | if ($form && $form->_action != CRM_Core_Action::UPDATE) { |
63b69fae | 195 | $newTagIds[] = $tagId; |
0cc0faa9 | 196 | } |
6fd5424b | 197 | elseif (!array_key_exists($tagId, $existingTags)) { |
63b69fae | 198 | $newTagIds[] = $tagId; |
0cc0faa9 | 199 | } |
6a488035 | 200 | } |
2a6e33c8 | 201 | } |
6a488035 | 202 | |
2a6e33c8 | 203 | // Any existing entity tags from this tagset missing from the $params should be deleted |
204 | $deleteSQL = "DELETE FROM civicrm_entity_tag | |
63b69fae | 205 | USING civicrm_entity_tag, civicrm_tag |
206 | WHERE civicrm_tag.id=civicrm_entity_tag.tag_id | |
207 | AND civicrm_entity_tag.entity_table='{$entityTable}' | |
208 | AND entity_id={$entityId} AND parent_id={$parentId}"; | |
2a6e33c8 | 209 | if (!empty($realTagIds)) { |
210 | $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $realTagIds) . ");"; | |
211 | } | |
63b69fae | 212 | |
2a6e33c8 | 213 | CRM_Core_DAO::executeQuery($deleteSQL); |
6a488035 | 214 | |
2a6e33c8 | 215 | if (!empty($newTagIds)) { |
216 | // New tag ids can be inserted directly into the db table. | |
217 | $insertValues = array(); | |
218 | foreach ($newTagIds as $tagId) { | |
219 | $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) "; | |
6a488035 | 220 | } |
2a6e33c8 | 221 | $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table ) |
222 | VALUES ' . implode(', ', $insertValues) . ';'; | |
223 | CRM_Core_DAO::executeQuery($insertSQL); | |
6a488035 TO |
224 | } |
225 | } | |
226 | } | |
227 | } | |
228 |