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 Object $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 string $groupName if used for building group block
53 * @param string $tagName if used for building tag block
54 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
59 static function buildQuickForm(&$form,
64 $groupName = 'Group(s)',
67 $groupElementType = 'checkbox'
69 if (!isset($form->_tagGroup
)) {
70 $form->_tagGroup
= array();
74 if (!$contactId && !empty($form->_contactId
)) {
75 $contactId = $form->_contactId
;
79 if ($type & self
::GROUP
) {
86 $groupID = isset($form->_grid
) ?
$form->_grid
: NULL;
87 if ($groupID && $visibility) {
88 $ids = array($groupID => $groupID);
92 $group = CRM_Core_PseudoConstant
::allGroup();
95 $group = CRM_Core_PseudoConstant
::group();
100 if ($groupID ||
!empty($group)) {
101 $groups = CRM_Contact_BAO_Group
::getGroupsHierarchy($ids);
103 $attributes['skiplabel'] = TRUE;
105 $groupsOptions = array();
106 foreach ($groups as $id => $group) {
107 // make sure that this group has public visibility
109 $group['visibility'] == 'User and User Admin Only'
114 if ($groupElementType == 'select') {
115 $groupsOptions[$id] = $group['title'];
118 $form->_tagGroup
[$fName][$id]['description'] = $group['description'];
119 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
123 if ($groupElementType == 'select' && !empty($groupsOptions)) {
124 $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
125 array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
127 $form->assign('groupCount', count($groupsOptions));
130 if ($groupElementType == 'checkbox' && !empty($elements)) {
131 $form->addGroup($elements, $fName, $groupName, ' <br />');
132 $form->assign('groupCount', count($elements));
134 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
137 $form->assign('groupElementType', $groupElementType);
141 if ($type & self
::TAG
) {
146 $form->_tagGroup
[$fName] = 1;
148 $tag = CRM_Core_BAO_Tag
::getTags();
150 foreach ($tag as $id => $name) {
151 $elements[] = $form->createElement('checkbox', $id, NULL, $name);
153 if (!empty($elements)) {
154 $form->addGroup($elements, $fName, $tagName, '<br />');
155 $form->assign('tagCount', count($elements));
159 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
163 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
165 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, TRUE, TRUE);
167 $form->assign('tagGroup', $form->_tagGroup
);
171 * set defaults for relevant form elements
173 * @param int $id the contact id
174 * @param array $defaults the defaults array to store the values in
175 * @param int $type what components are we interested in
176 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
182 static function setDefaults($id, &$defaults, $type = self
::ALL
, $fieldName = NULL, $groupElementType = 'checkbox') {
183 $type = (int ) $type;
184 if ($type & self
::GROUP
) {
190 $contactGroup = CRM_Contact_BAO_GroupContact
::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
192 foreach ($contactGroup as $group) {
193 if ($groupElementType == 'select') {
194 $defaults[$fName][] = $group['group_id'];
197 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
203 if ($type & self
::TAG
) {
209 $contactTag = CRM_Core_BAO_EntityTag
::getTag($id);
211 foreach ($contactTag as $tag) {
212 $defaults[$fName . '[' . $tag . ']'] = 1;
219 * This function sets the default values for the form. Note that in edit/view mode
220 * the default values are retrieved from the database
226 public static function setDefaultValues(&$form, &$defaults) {
227 $contactEditOptions = $form->get('contactEditOptions');
229 if ($form->_action
& CRM_Core_Action
::ADD
) {
230 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
231 // set group and tag defaults if any
233 $defaults['group'][$form->_gid
] = 1;
236 $defaults['tag'][$form->_tid
] = 1;
241 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
242 // set the group and tag ids
243 $groupElementType = 'checkbox';
244 if (CRM_Utils_System
::getClassName($form) == 'CRM_Contact_Form_Contact') {
245 $groupElementType = 'select';
247 self
::setDefaults($form->_contactId
, $defaults, self
::ALL
, NULL, $groupElementType);