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 the form object that we are operating on
49 * @param int $contactId contact id
50 * @param int $type what components are we interested in
51 * @param boolean $visibility visibility of the field
52 * @param null $isRequired
53 * @param string $groupName if used for building group block
54 * @param string $tagName if used for building tag block
55 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
57 * @param string $groupElementType
61 static function buildQuickForm(&$form,
66 $groupName = 'Group(s)',
69 $groupElementType = 'checkbox'
71 if (!isset($form->_tagGroup
)) {
72 $form->_tagGroup
= array();
76 if (!$contactId && !empty($form->_contactId
)) {
77 $contactId = $form->_contactId
;
81 if ($type & self
::GROUP
) {
88 $groupID = isset($form->_grid
) ?
$form->_grid
: NULL;
89 if ($groupID && $visibility) {
90 $ids = array($groupID => $groupID);
94 $group = CRM_Core_PseudoConstant
::allGroup();
97 $group = CRM_Core_PseudoConstant
::group();
102 if ($groupID ||
!empty($group)) {
103 $groups = CRM_Contact_BAO_Group
::getGroupsHierarchy($ids);
105 $attributes['skiplabel'] = TRUE;
107 $groupsOptions = array();
108 foreach ($groups as $id => $group) {
109 // make sure that this group has public visibility
111 $group['visibility'] == 'User and User Admin Only'
116 if ($groupElementType == 'select') {
117 $groupsOptions[$id] = $group['title'];
120 $form->_tagGroup
[$fName][$id]['description'] = $group['description'];
121 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
125 if ($groupElementType == 'select' && !empty($groupsOptions)) {
126 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
127 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
129 $form->assign('groupCount', count($groupsOptions));
132 if ($groupElementType == 'checkbox' && !empty($elements)) {
133 $form->addGroup($elements, $fName, $groupName, ' <br />');
134 $form->assign('groupCount', count($elements));
136 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
139 $form->assign('groupElementType', $groupElementType);
143 if ($type & self
::TAG
) {
144 // CODE FROM CRM/Tag/Form/Tag.php //
145 CRM_Core_Resources
::singleton()
146 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
147 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
153 $form->_tagGroup
[$fName] = 1;
155 // get the list of all the categories
156 $tags = new CRM_Core_BAO_Tag();
157 $tree = $tags->getTree('civicrm_contact', TRUE);
160 self
::climbtree($form, $tree, $elements);
162 $form->addGroup($elements, $fName, $tagName, '<br />');
163 $form->assign('tagCount', count($elements));
164 $form->assign('tree', $tree);
165 $form->assign('tag', $tree);
166 $form->assign('entityID', $contactId);
167 $form->assign('entityTable', 'civicrm_contact');
170 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
174 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
175 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
177 $form->assign('tagGroup', $form->_tagGroup
);
180 public static function climbtree($form, $tree, &$elements) {
181 foreach ($tree as $tagID => $varValue) {
182 $tagAttribute = array(
183 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
184 'id' => "tag_{$tagID}",
187 $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
189 if (array_key_exists('children', $varValue)) {
190 self
::climbtree($form, $varValue['children'], $elements);
197 * Set defaults for relevant form elements
199 * @param int $id the contact id
200 * @param array $defaults the defaults array to store the values in
201 * @param int $type what components are we interested in
202 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
204 * @param string $groupElementType
209 public static function setDefaults($id, &$defaults, $type = self
::ALL
, $fieldName = NULL, $groupElementType = 'checkbox') {
210 $type = (int ) $type;
211 if ($type & self
::GROUP
) {
217 $contactGroup = CRM_Contact_BAO_GroupContact
::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
219 foreach ($contactGroup as $group) {
220 if ($groupElementType == 'select') {
221 $defaults[$fName][] = $group['group_id'];
224 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
230 if ($type & self
::TAG
) {
236 $contactTag = CRM_Core_BAO_EntityTag
::getTag($id);
238 foreach ($contactTag as $tag) {
239 $defaults[$fName . '[' . $tag . ']'] = 1;
246 * Set default values for the form. Note that in edit/view mode
247 * the default values are retrieved from the database
250 * @param CRM_Core_Form $form
255 public static function setDefaultValues(&$form, &$defaults) {
256 $contactEditOptions = $form->get('contactEditOptions');
258 if ($form->_action
& CRM_Core_Action
::ADD
) {
259 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
260 // set group and tag defaults if any
262 $defaults['group'][$form->_gid
] = 1;
265 $defaults['tag'][$form->_tid
] = 1;
270 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
271 // set the group and tag ids
272 $groupElementType = 'checkbox';
273 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
274 $groupElementType = 'select';
276 self
::setDefaults($form->_contactId
, $defaults, self
::ALL
, NULL, $groupElementType);