3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form element for free tag widget
40 class CRM_Core_Form_Tag
{
41 public $_entityTagValues;
44 * Function to build tag widget if correct parent is passed
46 * @param object $form form object
47 * @param string $parentName parent name ( tag name)
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
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);
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');
69 // check if parent exists
70 $entityTags = array();
72 $tagsetItem = 'parentId_' . $parentId;
73 $tagset[$tagsetItem]['parentName'] = $parentNameItem;
74 $tagset[$tagsetItem]['parentID'] = $parentId;
77 $qparams = "parentId={$parentId}";
80 $qparams .= '&search=1';
83 $tagUrl = CRM_Utils_System
::url('civicrm/ajax/taglist', $qparams, FALSE, NULL, FALSE);
85 $tagset[$tagsetItem]['tagUrl'] = $tagUrl;
86 $tagset[$tagsetItem]['entityTable'] = $entityTable;
87 $tagset[$tagsetItem]['skipTagCreate'] = $skipTagCreate;
88 $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
90 switch ($entityTable) {
91 case 'civicrm_activity':
92 $tagsetElementName = "activity_taglist";
97 $tagsetElementName = "case_taglist";
102 $mode = 'attachment';
106 $tagsetElementName = "contact_taglist";
110 $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName;
111 if ($tagsetElementName) {
112 $form->add('text', "{$tagsetElementName}[{$parentId}]", NULL);
116 $tagset[$tagsetItem]['entityId'] = $entityId;
117 $entityTags = CRM_Core_BAO_EntityTag
::getChildEntityTags($parentId, $entityId, $entityTable);
121 switch ($entityTable) {
122 case 'civicrm_activity':
123 if (!empty($form->_submitValues
['activity_taglist']) && !empty($form->_submitValues
['activity_taglist'][$parentId])) {
124 $allTags = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
125 $tagIds = explode(',', $form->_submitValues
['activity_taglist'][$parentId]);
126 foreach ($tagIds as $tagId) {
127 if (is_numeric($tagId)) {
128 $tagName = $allTags[$tagId];
133 $entityTags[$tagId] = array(
142 if (!empty($form->_submitValues
['case_taglist']) && !empty($form->_submitValues
['case_taglist'][$parentId])) {
143 $allTags = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
144 $tagIds = explode(',', $form->_submitValues
['case_taglist'][$parentId]);
145 foreach ($tagIds as $tagId) {
146 if (is_numeric($tagId)) {
147 $tagName = $allTags[$tagId];
152 $entityTags[$tagId] = array(
160 $numAttachments = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'max_attachments');
161 for ($i = 1; $i <= $numAttachments; $i++
) {
162 $tagset[$i] = $tagset[$tagsetItem];
163 $tagset[$i]['tagsetElementName'] = "attachment_taglist_$i";
164 $form->add('text', "attachment_taglist_{$i}[{$parentId}]", NULL);
165 if (!empty($form->_submitValues
["attachment_taglist_$i"]) && !empty($form->_submitValues
["attachment_taglist_$i"][$parentId])) {
166 $allTags = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
167 $tagIds = explode(',', $form->_submitValues
["attachment_taglist_$i"][$parentId]);
168 foreach ($tagIds as $tagId) {
169 if (is_numeric($tagId)) {
170 $tagName = $allTags[$tagId];
175 $entityTags[$tagId] = array(
182 unset($tagset[$tagsetItem]);
186 if (!empty($form->_formValues
['contact_tags'])) {
187 $contactTags = CRM_Core_BAO_Tag
::getTagsUsedFor('civicrm_contact', TRUE, FALSE, $parentId);
189 foreach (array_keys($form->_formValues
['contact_tags']) as $tagId) {
190 if (!empty($contactTags[$tagId])) {
192 if (is_numeric($tagId)) {
193 $tagName = $contactTags[$tagId];
196 $entityTags[$tagId] = array(
206 if (!empty($entityTags)) {
207 // assign as simple array for display in smarty
208 $tagset[$tagsetItem]['entityTagsArray'] = $entityTags;
209 // assign as json for js widget
210 $tagset[$tagsetItem]['entityTags'] = json_encode(array_values($entityTags));
212 if (!empty($form->_entityTagValues
)) {
213 $form->_entityTagValues
= CRM_Utils_Array
::crmArrayMerge($entityTags, $form->_entityTagValues
);
216 $form->_entityTagValues
= $entityTags;
222 if (!empty($tagset)) {
223 // assign current tagsets which is used in postProcess
224 $form->_tagsetInfo
= $tagset;
225 $form->assign("tagsetType", $mode);
226 $form->assign("tagsetInfo_$mode", $tagset);
227 $form->assign("isTagset", TRUE);
232 * Function to save entity tags when it is not save used AJAX
234 * @param array $params associated array
235 * @param int $entityId entity id, eg: contact id, activity id, case id, file id
236 * @param string $entityTable entity table
237 * @param object $form form object
243 static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form) {
244 if ($form && !empty($form->_entityTagValues
)) {
245 $existingTags = $form->_entityTagValues
;
248 $existingTags = CRM_Core_BAO_EntityTag
::getTag($entityId, $entityTable);
252 // if the key is missing from the form response then all the tags were deleted / cleared
253 // in that case we create empty tagset params so that below logic works and tagset are
255 foreach ($form->_tagsetInfo
as $tagsetName => $tagsetInfo) {
256 $tagsetId = substr($tagsetName, strlen('parentId_'));
257 if (empty($params[$tagsetId])) {
258 $params[$tagsetId] = '';
263 // when form is submitted with tagset values below logic will work and in the case when all tags in a tagset
264 // are deleted we will have to set $params[tagset id] = '' which is done by above logic
265 foreach ($params as $parentId => $value) {
266 $newTagIds = array();
267 $realTagIds = array();
270 $tagsIDs = explode(',', $value);
271 foreach ($tagsIDs as $tagId) {
272 if (!is_numeric($tagId)) {
273 // check if user has selected existing tag or is creating new tag
274 // this is done to allow numeric tags etc.
275 $tagValue = explode(':::', $tagId);
277 if (isset($tagValue[1]) && $tagValue[1] == 'value') {
279 'name' => $tagValue[0],
280 'parent_id' => $parentId,
282 $tagObject = CRM_Core_BAO_Tag
::add($tagParams, CRM_Core_DAO
::$_nullArray);
283 $tagId = $tagObject->id
;
287 $realTagIds[] = $tagId;
288 if ($form && $form->_action
!= CRM_Core_Action
::UPDATE
) {
289 $newTagIds[] = $tagId;
291 elseif (!array_key_exists($tagId, $existingTags)) {
292 $newTagIds[] = $tagId;
297 // Any existing entity tags from this tagset missing from the $params should be deleted
298 $deleteSQL = "DELETE FROM civicrm_entity_tag
299 USING civicrm_entity_tag, civicrm_tag
300 WHERE civicrm_tag.id=civicrm_entity_tag.tag_id
301 AND civicrm_entity_tag.entity_table='{$entityTable}'
302 AND entity_id={$entityId} AND parent_id={$parentId}";
303 if (!empty($realTagIds)) {
304 $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $realTagIds) . ");";
307 CRM_Core_DAO
::executeQuery($deleteSQL);
309 if (!empty($newTagIds)) {
310 // New tag ids can be inserted directly into the db table.
311 $insertValues = array();
312 foreach ($newTagIds as $tagId) {
313 $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
315 $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table )
316 VALUES ' . implode(', ', $insertValues) . ';';
317 CRM_Core_DAO
::executeQuery($insertSQL);