Merge pull request #15825 from seamuslee001/dev_core_183_logging
[civicrm-core.git] / CRM / Tag / Form / Tag.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * This class generates form components for tags
22 *
23 */
24class CRM_Tag_Form_Tag extends CRM_Core_Form {
25
26 /**
27 * The contact id, used when add/edit tag
28 *
29 * @var int
30 */
31 protected $_entityID;
430ae6dd
TO
32 protected $_entityTable;
33
00be9182 34 public function preProcess() {
6a488035
TO
35 if ($this->get('entityID')) {
36 $this->_entityID = $this->get('entityID');
37 }
38 else {
39 $this->_entityID = $this->get('contactId');
40 }
41
42 $this->_entityTable = $this->get('entityTable');
43
44 if (empty($this->_entityTable)) {
45 $this->_entityTable = 'civicrm_contact';
46 }
47
48 $this->assign('entityID', $this->_entityID);
49 $this->assign('entityTable', $this->_entityTable);
50 }
51
52 /**
fe482240 53 * Build the form object.
6a488035 54 *
355ba699 55 * @return void
6a488035
TO
56 */
57 public function buildQuickForm() {
58 // get categories for the contact id
59 $entityTag = CRM_Core_BAO_EntityTag::getTag($this->_entityID, $this->_entityTable);
60 $this->assign('tagged', $entityTag);
61
62 // get the list of all the categories
d73974ac 63 $allTags = CRM_Core_BAO_Tag::getTagsUsedFor($this->_entityTable, FALSE);
6a488035
TO
64
65 // need to append the array with the " checked " if contact is tagged with the tag
d73974ac 66 foreach ($allTags as $tagID => $varValue) {
6a488035 67 if (in_array($tagID, $entityTag)) {
be2fb01f 68 $tagAttribute = [
6a488035
TO
69 'checked' => 'checked',
70 'id' => "tag_{$tagID}",
be2fb01f 71 ];
6a488035
TO
72 }
73 else {
be2fb01f 74 $tagAttribute = [
6a488035 75 'id' => "tag_{$tagID}",
be2fb01f 76 ];
6a488035
TO
77 }
78
79 $tagChk[$tagID] = $this->createElement('checkbox', $tagID, '', '', $tagAttribute);
80 }
81
82 $this->addGroup($tagChk, 'tagList', NULL, NULL, TRUE);
83
84 $tags = new CRM_Core_BAO_Tag();
85 $tree = $tags->getTree($this->_entityTable, TRUE);
86 $this->assign('tree', $tree);
87
d73974ac 88 $this->assign('allTags', $allTags);
6a488035
TO
89
90 //build tag widget
91 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
92 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, $this->_entityTable, $this->_entityID);
6a488035
TO
93 }
94
95 /**
6a488035 96 *
355ba699 97 * @return void
6a488035
TO
98 */
99 public function postProcess() {
d7d6c461 100 CRM_Utils_System::flushCache();
6a488035
TO
101
102 // array contains the posted values
103 // exportvalues is not used because its give value 1 of the checkbox which were checked by default,
104 // even after unchecking them before submitting them
105 $entityTag = $_POST['tagList'];
106
107 CRM_Core_BAO_EntityTag::create($entityTag, $this->_entityTable, $this->_entityID);
108
109 CRM_Core_Session::setStatus(ts('Your update(s) have been saved.'), ts('Saved'), 'success');
110 }
96025800 111
6a488035 112}