3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
35 * This class generates form element for free tag widget.
37 class CRM_Core_Form_Tag
{
38 public $_entityTagValues;
41 * Build tag widget if correct parent is passed
43 * @param CRM_Core_Form $form
45 * @param string $parentNames
46 * Parent name ( tag name).
47 * @param string $entityTable
48 * Entitytable 'eg: civicrm_contact'.
49 * @param int $entityId
50 * Entityid 'eg: contact id'.
51 * @param bool $skipTagCreate
52 * True if tag need be created using ajax.
53 * @param bool $skipEntityAction
54 * True if need to add entry in entry table via ajax.
55 * @param string $tagsetElementName
56 * If you need to create tagsetlist with specific name.
58 public static function buildQuickForm(
59 &$form, $parentNames, $entityTable, $entityId = NULL, $skipTagCreate = FALSE,
60 $skipEntityAction = 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
71 $tagsetItem = $tagsetElementName . 'parentId_' . $parentId;
72 $tagset[$tagsetItem]['parentID'] = $parentId;
74 list(, $mode) = explode('_', $entityTable);
75 if (!$tagsetElementName) {
76 $tagsetElementName = $mode . "_taglist";
78 $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName;
80 $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, array(
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 'select' => array('minimumInputLength' => 0),
92 $tagset[$tagsetItem]['entityId'] = $entityId;
93 $entityTags = CRM_Core_BAO_EntityTag
::getChildEntityTags($parentId, $entityId, $entityTable);
95 $form->setDefaults(array("{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags))));
99 $skipEntityAction = TRUE;
101 $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
105 if (!empty($tagset)) {
106 // assign current tagsets which is used in postProcess
107 $form->_tagsetInfo
= $tagset;
108 $form->assign("tagsetType", $mode);
109 // Merge this tagset info with possibly existing info in the template
110 $tagsetInfo = (array) $form->get_template_vars("tagsetInfo");
111 if (empty($tagsetInfo[$mode])) {
112 $tagsetInfo[$mode] = array();
114 $tagsetInfo[$mode] = array_merge($tagsetInfo[$mode], $tagset);
115 $form->assign("tagsetInfo", $tagsetInfo);
116 $form->assign("isTagset", TRUE);
121 * Save entity tags when it is not save used AJAX.
123 * @param array $params
124 * @param int $entityId
125 * Entity id, eg: contact id, activity id, case id, file id.
126 * @param string $entityTable
128 * @param CRM_Core_Form $form
131 public static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form = NULL) {
132 if ($form && !empty($form->_entityTagValues
)) {
133 $existingTags = $form->_entityTagValues
;
136 $existingTags = CRM_Core_BAO_EntityTag
::getTag($entityId, $entityTable);
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
143 foreach ($form->_tagsetInfo
as $tagsetName => $tagsetInfo) {
144 $tagsetId = explode('parentId_', $tagsetName);
145 $tagsetId = $tagsetId[1];
146 if (empty($params[$tagsetId])) {
147 $params[$tagsetId] = '';
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
154 foreach ($params as $parentId => $value) {
155 $newTagIds = array();
159 $tagIds = explode(',', $value);
160 foreach ($tagIds as $tagId) {
161 if ($form && $form->_action
!= CRM_Core_Action
::UPDATE ||
!array_key_exists($tagId, $existingTags)) {
162 $newTagIds[] = $tagId;
167 // Any existing entity tags from this tagset missing from the $params should be deleted
168 $deleteSQL = "DELETE FROM civicrm_entity_tag
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}";
173 if (!empty($tagIds)) {
174 $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $tagIds) . ");";
177 CRM_Core_DAO
::executeQuery($deleteSQL);
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}' ) ";
185 $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table )
186 VALUES ' . implode(', ', $insertValues) . ';';
187 CRM_Core_DAO
::executeQuery($insertSQL);