3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
62 static function buildQuickForm(&$form,
67 $groupName = 'Group(s)',
70 $groupElementType = 'checkbox'
72 if (!isset($form->_tagGroup
)) {
73 $form->_tagGroup
= array();
77 if (!$contactId && !empty($form->_contactId
)) {
78 $contactId = $form->_contactId
;
82 if ($type & self
::GROUP
) {
89 $groupID = isset($form->_grid
) ?
$form->_grid
: NULL;
90 if ($groupID && $visibility) {
91 $ids = array($groupID => $groupID);
95 $group = CRM_Core_PseudoConstant
::allGroup();
98 $group = CRM_Core_PseudoConstant
::group();
103 if ($groupID ||
!empty($group)) {
104 $groups = CRM_Contact_BAO_Group
::getGroupsHierarchy($ids);
106 $attributes['skiplabel'] = TRUE;
108 $groupsOptions = array();
109 foreach ($groups as $id => $group) {
110 // make sure that this group has public visibility
112 $group['visibility'] == 'User and User Admin Only'
117 if ($groupElementType == 'select') {
118 $groupsOptions[$id] = $group['title'];
121 $form->_tagGroup
[$fName][$id]['description'] = $group['description'];
122 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
126 if ($groupElementType == 'select' && !empty($groupsOptions)) {
127 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
128 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
130 $form->assign('groupCount', count($groupsOptions));
133 if ($groupElementType == 'checkbox' && !empty($elements)) {
134 $form->addGroup($elements, $fName, $groupName, ' <br />');
135 $form->assign('groupCount', count($elements));
137 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
140 $form->assign('groupElementType', $groupElementType);
144 if ($type & self
::TAG
) {
145 // CODE FROM CRM/Tag/Form/Tag.php //
146 CRM_Core_Resources
::singleton()
147 ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
148 ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
154 $form->_tagGroup
[$fName] = 1;
156 // get the list of all the categories
157 $tags = new CRM_Core_BAO_Tag();
158 $tree = $tags->getTree('civicrm_contact', TRUE);
161 self
::climbtree($form, $tree, $elements);
163 $form->addGroup($elements, $fName, $tagName, '<br />');
164 $form->assign('tagCount', count($elements));
165 $form->assign('tree', $tree);
166 $form->assign('tag', $tree);
167 $form->assign('entityID', $contactId);
168 $form->assign('entityTable', 'civicrm_contact');
171 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
175 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
176 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
178 $form->assign('tagGroup', $form->_tagGroup
);
181 static function climbtree($form, $tree, &$elements) {
182 foreach ($tree as $tagID => $varValue) {
183 $tagAttribute = array(
184 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
185 'id' => "tag_{$tagID}",
188 $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
190 if (array_key_exists('children', $varValue)) {
191 self
::climbtree($form, $varValue['children'], $elements);
198 * set defaults for relevant form elements
200 * @param int $id the contact id
201 * @param array $defaults the defaults array to store the values in
202 * @param int $type what components are we interested in
203 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
205 * @param string $groupElementType
211 static function setDefaults($id, &$defaults, $type = self
::ALL
, $fieldName = NULL, $groupElementType = 'checkbox') {
212 $type = (int ) $type;
213 if ($type & self
::GROUP
) {
219 $contactGroup = CRM_Contact_BAO_GroupContact
::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
221 foreach ($contactGroup as $group) {
222 if ($groupElementType == 'select') {
223 $defaults[$fName][] = $group['group_id'];
226 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
232 if ($type & self
::TAG
) {
238 $contactTag = CRM_Core_BAO_EntityTag
::getTag($id);
240 foreach ($contactTag as $tag) {
241 $defaults[$fName . '[' . $tag . ']'] = 1;
248 * Set default values for the form. Note that in edit/view mode
249 * the default values are retrieved from the database
253 * @param CRM_Core_Form $form
258 public static function setDefaultValues(&$form, &$defaults) {
259 $contactEditOptions = $form->get('contactEditOptions');
261 if ($form->_action
& CRM_Core_Action
::ADD
) {
262 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
263 // set group and tag defaults if any
265 $defaults['group'][$form->_gid
] = 1;
268 $defaults['tag'][$form->_tid
] = 1;
273 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
274 // set the group and tag ids
275 $groupElementType = 'checkbox';
276 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
277 $groupElementType = 'select';
279 self
::setDefaults($form->_contactId
, $defaults, self
::ALL
, NULL, $groupElementType);