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