3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Contact_Form_Edit_TagsAndGroups
{
38 * Constant to determine which forms we are generating
40 * Used by both profile and edit contact
42 const GROUP
= 1, TAG
= 2, ALL
= 3;
45 * This function is to build form elements
46 * params object $form object of the form
48 * @param CRM_Core_Form $form
49 * The form object that we are operating on.
50 * @param int $contactId
53 * What components are we interested in.
54 * @param bool $visibility
55 * Visibility of the field.
56 * @param null $isRequired
57 * @param string $groupName
58 * If used for building group block.
59 * @param string $tagName
60 * If used for building tag block.
61 * @param string $fieldName
62 * This is used in batch profile(i.e to build multiple blocks).
64 * @param string $groupElementType
68 static function buildQuickForm(&$form,
73 $groupName = 'Group(s)',
76 $groupElementType = 'checkbox'
78 if (!isset($form->_tagGroup
)) {
79 $form->_tagGroup
= array();
83 if (!$contactId && !empty($form->_contactId
)) {
84 $contactId = $form->_contactId
;
88 if ($type & self
::GROUP
) {
95 $groupID = isset($form->_grid
) ?
$form->_grid
: NULL;
96 if ($groupID && $visibility) {
97 $ids = array($groupID => $groupID);
101 $group = CRM_Core_PseudoConstant
::allGroup();
104 $group = CRM_Core_PseudoConstant
::group();
109 if ($groupID ||
!empty($group)) {
110 $groups = CRM_Contact_BAO_Group
::getGroupsHierarchy($ids);
112 $attributes['skiplabel'] = TRUE;
114 $groupsOptions = array();
115 foreach ($groups as $id => $group) {
116 // make sure that this group has public visibility
118 $group['visibility'] == 'User and User Admin Only'
123 if ($groupElementType == 'select') {
124 $groupsOptions[$id] = $group['title'];
127 $form->_tagGroup
[$fName][$id]['description'] = $group['description'];
128 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
132 if ($groupElementType == 'select' && !empty($groupsOptions)) {
133 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
134 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
136 $form->assign('groupCount', count($groupsOptions));
139 if ($groupElementType == 'checkbox' && !empty($elements)) {
140 $form->addGroup($elements, $fName, $groupName, ' <br />');
141 $form->assign('groupCount', count($elements));
143 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
146 $form->assign('groupElementType', $groupElementType);
150 if ($type & self
::TAG
) {
151 // CODE FROM CRM/Tag/Form/Tag.php //
152 CRM_Core_Resources
::singleton()
153 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
154 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
160 $form->_tagGroup
[$fName] = 1;
162 // get the list of all the categories
163 $tags = new CRM_Core_BAO_Tag();
164 $tree = $tags->getTree('civicrm_contact', TRUE);
167 self
::climbtree($form, $tree, $elements);
169 $form->addGroup($elements, $fName, $tagName, '<br />');
170 $form->assign('tagCount', count($elements));
171 $form->assign('tree', $tree);
172 $form->assign('tag', $tree);
173 $form->assign('entityID', $contactId);
174 $form->assign('entityTable', 'civicrm_contact');
177 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
181 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
182 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
184 $form->assign('tagGroup', $form->_tagGroup
);
187 public static function climbtree($form, $tree, &$elements) {
188 foreach ($tree as $tagID => $varValue) {
189 $tagAttribute = array(
190 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
191 'id' => "tag_{$tagID}",
194 $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
196 if (array_key_exists('children', $varValue)) {
197 self
::climbtree($form, $varValue['children'], $elements);
204 * Set defaults for relevant form elements
208 * @param array $defaults
209 * The defaults array to store the values in.
211 * What components are we interested in.
212 * @param string $fieldName
213 * This is used in batch profile(i.e to build multiple blocks).
215 * @param string $groupElementType
220 public static function setDefaults($id, &$defaults, $type = self
::ALL
, $fieldName = NULL, $groupElementType = 'checkbox') {
221 $type = (int ) $type;
222 if ($type & self
::GROUP
) {
228 $contactGroup = CRM_Contact_BAO_GroupContact
::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
230 foreach ($contactGroup as $group) {
231 if ($groupElementType == 'select') {
232 $defaults[$fName][] = $group['group_id'];
235 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
241 if ($type & self
::TAG
) {
247 $contactTag = CRM_Core_BAO_EntityTag
::getTag($id);
249 foreach ($contactTag as $tag) {
250 $defaults[$fName . '[' . $tag . ']'] = 1;
257 * Set default values for the form. Note that in edit/view mode
258 * the default values are retrieved from the database
261 * @param CRM_Core_Form $form
266 public static function setDefaultValues(&$form, &$defaults) {
267 $contactEditOptions = $form->get('contactEditOptions');
269 if ($form->_action
& CRM_Core_Action
::ADD
) {
270 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
271 // set group and tag defaults if any
273 $defaults['group'][$form->_gid
] = 1;
276 $defaults['tag'][$form->_tid
] = 1;
281 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
282 // set the group and tag ids
283 $groupElementType = 'checkbox';
284 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
285 $groupElementType = 'select';
287 self
::setDefaults($form->_contactId
, $defaults, self
::ALL
, NULL, $groupElementType);