3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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,
61 $type = CRM_Contact_Form_Edit_TagsandGroups
::ALL
,
64 $groupName = 'Group(s)',
68 if (!isset($form->_tagGroup
)) {
69 $form->_tagGroup
= array();
73 if (!$contactId && !empty($form->_contactId
)) {
74 $contactId = $form->_contactId
;
78 if ($type & CRM_Contact_Form_Edit_TagsandGroups
::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;
104 foreach ($groups as $id => $group) {
105 // make sure that this group has public visibility
107 $group['visibility'] == 'User and User Admin Only'
111 $form->_tagGroup
[$fName][$id]['description'] = $group['description'];
112 $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
115 if (!empty($elements)) {
116 $form->addGroup($elements, $fName, $groupName, ' <br />');
117 $form->assign('groupCount', count($elements));
119 $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
125 if ($type & CRM_Contact_Form_Edit_TagsandGroups
::TAG
) {
130 $form->_tagGroup
[$fName] = 1;
132 $tag = CRM_Core_BAO_Tag
::getTags();
134 foreach ($tag as $id => $name) {
135 $elements[] = $form->createElement('checkbox', $id, NULL, $name);
137 if (!empty($elements)) {
138 $form->addGroup($elements, $fName, $tagName, '<br />');
139 $form->assign('tagCount', count($elements));
143 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
147 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
149 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
151 $form->assign('tagGroup', $form->_tagGroup
);
155 * set defaults for relevant form elements
157 * @param int $id the contact id
158 * @param array $defaults the defaults array to store the values in
159 * @param int $type what components are we interested in
160 * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
166 static function setDefaults($id, &$defaults, $type = CRM_Contact_Form_Edit_TagsandGroups
::ALL
, $fieldName = NULL) {
167 $type = (int ) $type;
168 if ($type & self
::GROUP
) {
174 $contactGroup = CRM_Contact_BAO_GroupContact
::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
176 foreach ($contactGroup as $group) {
177 $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
182 if ($type & self
::TAG
) {
188 $contactTag = CRM_Core_BAO_EntityTag
::getTag($id);
190 foreach ($contactTag as $tag) {
191 $defaults[$fName . '[' . $tag . ']'] = 1;
198 * This function sets the default values for the form. Note that in edit/view mode
199 * the default values are retrieved from the database
205 public static function setDefaultValues(&$form, &$defaults) {
206 $contactEditOptions = $form->get('contactEditOptions');
207 if ($form->_action
& CRM_Core_Action
::ADD
) {
208 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
209 // set group and tag defaults if any
211 $defaults['group'][$form->_gid
] = 1;
214 $defaults['tag'][$form->_tid
] = 1;
219 if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
220 // set the group and tag ids
221 self
::setDefaults($form->_contactId
, $defaults, self
::ALL
);