Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | /** | |
100fef9d | 44 | * Build tag widget if correct parent is passed |
6a488035 | 45 | * |
6a0b768e TO |
46 | * @param CRM_Core_Form $form |
47 | * Form object. | |
48 | * @param string $parentNames | |
49 | * Parent name ( tag name). | |
50 | * @param string $entityTable | |
51 | * Entitytable 'eg: civicrm_contact'. | |
52 | * @param int $entityId | |
53 | * Entityid 'eg: contact id'. | |
54 | * @param bool $skipTagCreate | |
55 | * True if tag need be created using ajax. | |
56 | * @param bool $skipEntityAction | |
57 | * True if need to add entry in entry table via ajax. | |
58 | * @param string $tagsetElementName | |
59 | * If you need to create tagsetlist with specific name. | |
6a488035 TO |
60 | * |
61 | * @return void | |
6a488035 TO |
62 | * @static |
63 | */ | |
f9f40af3 TO |
64 | static function buildQuickForm( |
65 | &$form, $parentNames, $entityTable, $entityId = NULL, $skipTagCreate = FALSE, | |
4dff5e17 | 66 | $skipEntityAction = FALSE, $tagsetElementName = NULL ) { |
6a488035 TO |
67 | $tagset = $form->_entityTagValues = array(); |
68 | $form->assign("isTagset", FALSE); | |
69 | $mode = NULL; | |
70 | ||
310bbbe5 | 71 | foreach ($parentNames as &$parentNameItem) { |
6a488035 TO |
72 | // get the parent id for tag list input for keyword |
73 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name'); | |
74 | ||
75 | // check if parent exists | |
6a488035 | 76 | if ($parentId) { |
310bbbe5 | 77 | $tagsetItem = $tagsetElementName . 'parentId_' . $parentId; |
6a488035 TO |
78 | $tagset[$tagsetItem]['parentID'] = $parentId; |
79 | ||
310bbbe5 CW |
80 | list(, $mode) = explode('_', $entityTable); |
81 | if (!$tagsetElementName) { | |
82 | $tagsetElementName = $mode . "_taglist"; | |
6a488035 | 83 | } |
6a488035 | 84 | $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName; |
310bbbe5 CW |
85 | |
86 | $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, array( | |
87 | 'entity' => 'tag', | |
88 | 'multiple' => TRUE, | |
89 | 'create' => !$skipTagCreate, | |
90 | 'api' => array('params' => array('parent_id' => $parentId)), | |
91 | 'data-entity_table' => $entityTable, | |
92 | 'data-entity_id' => $entityId, | |
93 | 'class' => "crm-$mode-tagset", | |
94 | )); | |
6a488035 TO |
95 | |
96 | if ($entityId) { | |
97 | $tagset[$tagsetItem]['entityId'] = $entityId; | |
98 | $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable); | |
e4f4dc22 CW |
99 | if ($entityTags) { |
100 | $form->setDefaults(array("{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags)))); | |
6a488035 TO |
101 | } |
102 | } | |
e4f4dc22 CW |
103 | else { |
104 | $skipEntityAction = TRUE; | |
6a488035 | 105 | } |
310bbbe5 | 106 | $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction; |
6a488035 TO |
107 | } |
108 | } | |
109 | ||
110 | if (!empty($tagset)) { | |
2b2ba6a5 | 111 | // assign current tagsets which is used in postProcess |
2a6e33c8 | 112 | $form->_tagsetInfo = $tagset; |
f70ca446 | 113 | $form->assign("tagsetType", $mode); |
310bbbe5 | 114 | // Merge this tagset info with possibly existing info in the template |
6d538af3 | 115 | $tagsetInfo = (array) $form->get_template_vars("tagsetInfo"); |
310bbbe5 CW |
116 | if (empty($tagsetInfo[$mode])) { |
117 | $tagsetInfo[$mode] = array(); | |
118 | } | |
119 | $tagsetInfo[$mode] = array_merge($tagsetInfo[$mode], $tagset); | |
6d538af3 | 120 | $form->assign("tagsetInfo", $tagsetInfo); |
6a488035 TO |
121 | $form->assign("isTagset", TRUE); |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
100fef9d | 126 | * Save entity tags when it is not save used AJAX |
6a488035 | 127 | * |
6a0b768e TO |
128 | * @param array $params |
129 | * Associated array. | |
130 | * @param int $entityId | |
131 | * Entity id, eg: contact id, activity id, case id, file id. | |
132 | * @param string $entityTable | |
133 | * Entity table. | |
134 | * @param CRM_Core_Form $form | |
135 | * Form object. | |
6fd5424b | 136 | * |
137 | * @return void | |
6fd5424b | 138 | * @static |
6a488035 | 139 | */ |
00be9182 | 140 | public static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form) { |
6fd5424b | 141 | if ($form && !empty($form->_entityTagValues)) { |
142 | $existingTags = $form->_entityTagValues; | |
143 | } | |
144 | else { | |
145 | $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable); | |
146 | } | |
147 | ||
2a6e33c8 | 148 | if ($form) { |
2b2ba6a5 | 149 | // if the key is missing from the form response then all the tags were deleted / cleared |
150 | // in that case we create empty tagset params so that below logic works and tagset are | |
e450bc79 | 151 | // deleted correctly |
2a6e33c8 | 152 | foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) { |
703aec38 | 153 | $tagsetId = explode('parentId_', $tagsetName); |
154 | $tagsetId = $tagsetId[1]; | |
2a6e33c8 | 155 | if (empty($params[$tagsetId])) { |
156 | $params[$tagsetId] = ''; | |
157 | } | |
6a488035 | 158 | } |
2a6e33c8 | 159 | } |
6fd5424b | 160 | |
2b2ba6a5 | 161 | // when form is submitted with tagset values below logic will work and in the case when all tags in a tagset |
162 | // are deleted we will have to set $params[tagset id] = '' which is done by above logic | |
2a6e33c8 | 163 | foreach ($params as $parentId => $value) { |
63b69fae | 164 | $newTagIds = array(); |
47bc9cec | 165 | $tagIds = array(); |
63b69fae | 166 | |
2a6e33c8 | 167 | if ($value) { |
47bc9cec CW |
168 | $tagIds = explode(',', $value); |
169 | foreach ($tagIds as $tagId) { | |
170 | if ($form && $form->_action != CRM_Core_Action::UPDATE || !array_key_exists($tagId, $existingTags)) { | |
63b69fae | 171 | $newTagIds[] = $tagId; |
0cc0faa9 | 172 | } |
6a488035 | 173 | } |
2a6e33c8 | 174 | } |
6a488035 | 175 | |
2a6e33c8 | 176 | // Any existing entity tags from this tagset missing from the $params should be deleted |
177 | $deleteSQL = "DELETE FROM civicrm_entity_tag | |
63b69fae | 178 | USING civicrm_entity_tag, civicrm_tag |
179 | WHERE civicrm_tag.id=civicrm_entity_tag.tag_id | |
180 | AND civicrm_entity_tag.entity_table='{$entityTable}' | |
181 | AND entity_id={$entityId} AND parent_id={$parentId}"; | |
47bc9cec CW |
182 | if (!empty($tagIds)) { |
183 | $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $tagIds) . ");"; | |
2a6e33c8 | 184 | } |
63b69fae | 185 | |
2a6e33c8 | 186 | CRM_Core_DAO::executeQuery($deleteSQL); |
6a488035 | 187 | |
2a6e33c8 | 188 | if (!empty($newTagIds)) { |
189 | // New tag ids can be inserted directly into the db table. | |
190 | $insertValues = array(); | |
191 | foreach ($newTagIds as $tagId) { | |
192 | $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) "; | |
6a488035 | 193 | } |
2a6e33c8 | 194 | $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table ) |
195 | VALUES ' . implode(', ', $insertValues) . ';'; | |
196 | CRM_Core_DAO::executeQuery($insertSQL); | |
6a488035 TO |
197 | } |
198 | } | |
199 | } | |
200 | } |