Commit | Line | Data |
---|---|---|
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 | */ |
17 | ||
18 | /** | |
19 | * This class generates form components for tags | |
20 | * | |
21 | */ | |
22 | class CRM_Tag_Form_Tag extends CRM_Core_Form { | |
23 | ||
24 | /** | |
25 | * The contact id, used when add/edit tag | |
26 | * | |
27 | * @var int | |
28 | */ | |
29 | protected $_entityID; | |
430ae6dd TO |
30 | protected $_entityTable; |
31 | ||
00be9182 | 32 | public function preProcess() { |
6a488035 TO |
33 | if ($this->get('entityID')) { |
34 | $this->_entityID = $this->get('entityID'); | |
35 | } | |
36 | else { | |
37 | $this->_entityID = $this->get('contactId'); | |
38 | } | |
39 | ||
40 | $this->_entityTable = $this->get('entityTable'); | |
41 | ||
42 | if (empty($this->_entityTable)) { | |
43 | $this->_entityTable = 'civicrm_contact'; | |
44 | } | |
45 | ||
46 | $this->assign('entityID', $this->_entityID); | |
47 | $this->assign('entityTable', $this->_entityTable); | |
48 | } | |
49 | ||
50 | /** | |
fe482240 | 51 | * Build the form object. |
6a488035 | 52 | * |
355ba699 | 53 | * @return void |
6a488035 TO |
54 | */ |
55 | public function buildQuickForm() { | |
56 | // get categories for the contact id | |
57 | $entityTag = CRM_Core_BAO_EntityTag::getTag($this->_entityID, $this->_entityTable); | |
58 | $this->assign('tagged', $entityTag); | |
59 | ||
60 | // get the list of all the categories | |
d73974ac | 61 | $allTags = CRM_Core_BAO_Tag::getTagsUsedFor($this->_entityTable, FALSE); |
6a488035 TO |
62 | |
63 | // need to append the array with the " checked " if contact is tagged with the tag | |
d73974ac | 64 | foreach ($allTags as $tagID => $varValue) { |
6a488035 | 65 | if (in_array($tagID, $entityTag)) { |
be2fb01f | 66 | $tagAttribute = [ |
6a488035 TO |
67 | 'checked' => 'checked', |
68 | 'id' => "tag_{$tagID}", | |
be2fb01f | 69 | ]; |
6a488035 TO |
70 | } |
71 | else { | |
be2fb01f | 72 | $tagAttribute = [ |
6a488035 | 73 | 'id' => "tag_{$tagID}", |
be2fb01f | 74 | ]; |
6a488035 TO |
75 | } |
76 | ||
77 | $tagChk[$tagID] = $this->createElement('checkbox', $tagID, '', '', $tagAttribute); | |
78 | } | |
79 | ||
80 | $this->addGroup($tagChk, 'tagList', NULL, NULL, TRUE); | |
81 | ||
82 | $tags = new CRM_Core_BAO_Tag(); | |
83 | $tree = $tags->getTree($this->_entityTable, TRUE); | |
84 | $this->assign('tree', $tree); | |
85 | ||
d73974ac | 86 | $this->assign('allTags', $allTags); |
6a488035 TO |
87 | |
88 | //build tag widget | |
89 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); | |
90 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, $this->_entityTable, $this->_entityID); | |
6a488035 TO |
91 | } |
92 | ||
93 | /** | |
6a488035 | 94 | * |
355ba699 | 95 | * @return void |
6a488035 TO |
96 | */ |
97 | public function postProcess() { | |
d7d6c461 | 98 | CRM_Utils_System::flushCache(); |
6a488035 TO |
99 | |
100 | // array contains the posted values | |
101 | // exportvalues is not used because its give value 1 of the checkbox which were checked by default, | |
102 | // even after unchecking them before submitting them | |
103 | $entityTag = $_POST['tagList']; | |
104 | ||
105 | CRM_Core_BAO_EntityTag::create($entityTag, $this->_entityTable, $this->_entityID); | |
106 | ||
107 | CRM_Core_Session::setStatus(ts('Your update(s) have been saved.'), ts('Saved'), 'success'); | |
108 | } | |
96025800 | 109 | |
6a488035 | 110 | } |