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