Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-10-28-14-52-15
[civicrm-core.git] / CRM / Tag / Form / Tag.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for tags
38 *
39 */
40class CRM_Tag_Form_Tag extends CRM_Core_Form {
41
42 /**
43 * The contact id, used when add/edit tag
44 *
45 * @var int
46 */
47 protected $_entityID;
430ae6dd
TO
48 protected $_entityTable;
49
50 function preProcess() {
6a488035
TO
51 if ($this->get('entityID')) {
52 $this->_entityID = $this->get('entityID');
53 }
54 else {
55 $this->_entityID = $this->get('contactId');
56 }
57
58 $this->_entityTable = $this->get('entityTable');
59
60 if (empty($this->_entityTable)) {
61 $this->_entityTable = 'civicrm_contact';
62 }
63
64 $this->assign('entityID', $this->_entityID);
65 $this->assign('entityTable', $this->_entityTable);
66 }
67
68 /**
69 * Function to build the form
70 *
71 * @return None
72 * @access public
73 */
74 public function buildQuickForm() {
75 // get categories for the contact id
76 $entityTag = CRM_Core_BAO_EntityTag::getTag($this->_entityID, $this->_entityTable);
77 $this->assign('tagged', $entityTag);
78
79 // get the list of all the categories
80 $allTag = CRM_Core_BAO_Tag::getTagsUsedFor($this->_entityTable);
81
82 // need to append the array with the " checked " if contact is tagged with the tag
83 foreach ($allTag as $tagID => $varValue) {
84 if (in_array($tagID, $entityTag)) {
85 $tagAttribute = array(
86 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
87 'checked' => 'checked',
88 'id' => "tag_{$tagID}",
89 );
90 }
91 else {
92 $tagAttribute = array(
93 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
94 'id' => "tag_{$tagID}",
95 );
96 }
97
98 $tagChk[$tagID] = $this->createElement('checkbox', $tagID, '', '', $tagAttribute);
99 }
100
101 $this->addGroup($tagChk, 'tagList', NULL, NULL, TRUE);
102
103 $tags = new CRM_Core_BAO_Tag();
104 $tree = $tags->getTree($this->_entityTable, TRUE);
105 $this->assign('tree', $tree);
106
107 $this->assign('tag', $allTag);
108
109 //build tag widget
110 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
111 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, $this->_entityTable, $this->_entityID);
112
113 if ($this->_action & CRM_Core_Action::BROWSE) {
114 $this->freeze();
115 }
116 else {
117 $this->addButtons(array(
118 array(
119 'type' => 'next',
120 'name' => ts('Update Tags'),
121 'isDefault' => TRUE,
122 ),
123 array(
124 'type' => 'cancel',
125 'name' => ts('Cancel'),
126 ),
127 )
128 );
129 }
130 }
131
132 /**
133 *
134 * @access public
135 *
136 * @return None
137 */
138 public function postProcess() {
139 CRM_Utils_System::flushCache('CRM_Core_DAO_Tag');
140
141 // array contains the posted values
142 // exportvalues is not used because its give value 1 of the checkbox which were checked by default,
143 // even after unchecking them before submitting them
144 $entityTag = $_POST['tagList'];
145
146 CRM_Core_BAO_EntityTag::create($entityTag, $this->_entityTable, $this->_entityID);
147
148 CRM_Core_Session::setStatus(ts('Your update(s) have been saved.'), ts('Saved'), 'success');
149 }
150 //end of function
151}
152