Merge branch 'rcsheets-docstring-cleanup'
[civicrm-core.git] / CRM / Tag / Form / Tag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for tags
38 *
39 */
40 class 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;
48 protected $_entityTable;
49
50 function preProcess() {
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 void
72 * @access public
73 */
74 public function buildQuickForm() {
75 CRM_Core_Resources::singleton()
76 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
77 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
78 // get categories for the contact id
79 $entityTag = CRM_Core_BAO_EntityTag::getTag($this->_entityID, $this->_entityTable);
80 $this->assign('tagged', $entityTag);
81
82 // get the list of all the categories
83 $allTag = CRM_Core_BAO_Tag::getTagsUsedFor($this->_entityTable);
84
85 // need to append the array with the " checked " if contact is tagged with the tag
86 foreach ($allTag as $tagID => $varValue) {
87 if (in_array($tagID, $entityTag)) {
88 $tagAttribute = array(
89 'checked' => 'checked',
90 'id' => "tag_{$tagID}",
91 );
92 }
93 else {
94 $tagAttribute = array(
95 'id' => "tag_{$tagID}",
96 );
97 }
98
99 $tagChk[$tagID] = $this->createElement('checkbox', $tagID, '', '', $tagAttribute);
100 }
101
102 $this->addGroup($tagChk, 'tagList', NULL, NULL, TRUE);
103
104 $tags = new CRM_Core_BAO_Tag();
105 $tree = $tags->getTree($this->_entityTable, TRUE);
106 $this->assign('tree', $tree);
107
108 $this->assign('tag', $allTag);
109
110 //build tag widget
111 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
112 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, $this->_entityTable, $this->_entityID);
113
114 if ($this->_action & CRM_Core_Action::BROWSE) {
115 $this->freeze();
116 }
117 else {
118 $this->addButtons(array(
119 array(
120 'type' => 'next',
121 'name' => ts('Update Tags'),
122 'isDefault' => TRUE,
123 ),
124 array(
125 'type' => 'cancel',
126 'name' => ts('Cancel'),
127 ),
128 )
129 );
130 }
131 }
132
133 /**
134 *
135 * @access public
136 *
137 * @return void
138 */
139 public function postProcess() {
140 CRM_Utils_System::flushCache('CRM_Core_DAO_Tag');
141
142 // array contains the posted values
143 // exportvalues is not used because its give value 1 of the checkbox which were checked by default,
144 // even after unchecking them before submitting them
145 $entityTag = $_POST['tagList'];
146
147 CRM_Core_BAO_EntityTag::create($entityTag, $this->_entityTable, $this->_entityID);
148
149 CRM_Core_Session::setStatus(ts('Your update(s) have been saved.'), ts('Saved'), 'success');
150 }
151 //end of function
152 }
153