| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Contact_Form_Edit_TagsAndGroups { |
| 36 | |
| 37 | /** |
| 38 | * Constant to determine which forms we are generating |
| 39 | * |
| 40 | * Used by both profile and edit contact |
| 41 | */ |
| 42 | CONST GROUP = 1, TAG = 2, ALL = 3; |
| 43 | |
| 44 | /** |
| 45 | * This function is to build form elements |
| 46 | * params object $form object of the form |
| 47 | * |
| 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) |
| 56 | * |
| 57 | * @param string $groupElementType |
| 58 | * |
| 59 | * @static |
| 60 | * @access public |
| 61 | */ |
| 62 | static function buildQuickForm(&$form, |
| 63 | $contactId = 0, |
| 64 | $type = self::ALL, |
| 65 | $visibility = FALSE, |
| 66 | $isRequired = NULL, |
| 67 | $groupName = 'Group(s)', |
| 68 | $tagName = 'Tag(s)', |
| 69 | $fieldName = NULL, |
| 70 | $groupElementType = 'checkbox' |
| 71 | ) { |
| 72 | if (!isset($form->_tagGroup)) { |
| 73 | $form->_tagGroup = array(); |
| 74 | } |
| 75 | |
| 76 | // NYSS 5670 |
| 77 | if (!$contactId && !empty($form->_contactId)) { |
| 78 | $contactId = $form->_contactId; |
| 79 | } |
| 80 | |
| 81 | $type = (int) $type; |
| 82 | if ($type & self::GROUP) { |
| 83 | |
| 84 | $fName = 'group'; |
| 85 | if ($fieldName) { |
| 86 | $fName = $fieldName; |
| 87 | } |
| 88 | |
| 89 | $groupID = isset($form->_grid) ? $form->_grid : NULL; |
| 90 | if ($groupID && $visibility) { |
| 91 | $ids = array($groupID => $groupID); |
| 92 | } |
| 93 | else { |
| 94 | if ($visibility) { |
| 95 | $group = CRM_Core_PseudoConstant::allGroup(); |
| 96 | } |
| 97 | else { |
| 98 | $group = CRM_Core_PseudoConstant::group(); |
| 99 | } |
| 100 | $ids = $group; |
| 101 | } |
| 102 | |
| 103 | if ($groupID || !empty($group)) { |
| 104 | $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids); |
| 105 | |
| 106 | $attributes['skiplabel'] = TRUE; |
| 107 | $elements = array(); |
| 108 | $groupsOptions = array(); |
| 109 | foreach ($groups as $id => $group) { |
| 110 | // make sure that this group has public visibility |
| 111 | if ($visibility && |
| 112 | $group['visibility'] == 'User and User Admin Only' |
| 113 | ) { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | if ($groupElementType == 'select') { |
| 118 | $groupsOptions[$id] = $group['title']; |
| 119 | } |
| 120 | else { |
| 121 | $form->_tagGroup[$fName][$id]['description'] = $group['description']; |
| 122 | $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes); |
| 123 | } |
| 124 | } |
| 125 | |
| 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') |
| 129 | ); |
| 130 | $form->assign('groupCount', count($groupsOptions)); |
| 131 | } |
| 132 | |
| 133 | if ($groupElementType == 'checkbox' && !empty($elements)) { |
| 134 | $form->addGroup($elements, $fName, $groupName, ' <br />'); |
| 135 | $form->assign('groupCount', count($elements)); |
| 136 | if ($isRequired) { |
| 137 | $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required'); |
| 138 | } |
| 139 | } |
| 140 | $form->assign('groupElementType', $groupElementType); |
| 141 | } |
| 142 | } |
| 143 | |
| 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'); |
| 149 | |
| 150 | $fName = 'tag'; |
| 151 | if ($fieldName) { |
| 152 | $fName = $fieldName; |
| 153 | } |
| 154 | $form->_tagGroup[$fName] = 1; |
| 155 | |
| 156 | // get the list of all the categories |
| 157 | $tags = new CRM_Core_BAO_Tag(); |
| 158 | $tree = $tags->getTree('civicrm_contact', TRUE); |
| 159 | |
| 160 | $elements = array(); |
| 161 | self::climbtree($form, $tree, $elements); |
| 162 | |
| 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'); |
| 169 | |
| 170 | if ($isRequired) { |
| 171 | $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required'); |
| 172 | } |
| 173 | |
| 174 | // build tag widget |
| 175 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); |
| 176 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE); |
| 177 | } |
| 178 | $form->assign('tagGroup', $form->_tagGroup); |
| 179 | } |
| 180 | |
| 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}", |
| 186 | ); |
| 187 | |
| 188 | $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute); |
| 189 | |
| 190 | if (array_key_exists('children', $varValue)) { |
| 191 | self::climbtree($form, $varValue['children'], $elements); |
| 192 | } |
| 193 | } |
| 194 | return $elements; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Set defaults for relevant form elements |
| 199 | * |
| 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) |
| 204 | * |
| 205 | * @param string $groupElementType |
| 206 | * |
| 207 | * @return void |
| 208 | * @access public |
| 209 | * @static |
| 210 | */ |
| 211 | static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') { |
| 212 | $type = (int ) $type; |
| 213 | if ($type & self::GROUP) { |
| 214 | $fName = 'group'; |
| 215 | if ($fieldName) { |
| 216 | $fName = $fieldName; |
| 217 | } |
| 218 | |
| 219 | $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE); |
| 220 | if ($contactGroup) { |
| 221 | foreach ($contactGroup as $group) { |
| 222 | if ($groupElementType == 'select') { |
| 223 | $defaults[$fName][] = $group['group_id']; |
| 224 | } |
| 225 | else { |
| 226 | $defaults[$fName . '[' . $group['group_id'] . ']'] = 1; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if ($type & self::TAG) { |
| 233 | $fName = 'tag'; |
| 234 | if ($fieldName) { |
| 235 | $fName = $fieldName; |
| 236 | } |
| 237 | |
| 238 | $contactTag = CRM_Core_BAO_EntityTag::getTag($id); |
| 239 | if ($contactTag) { |
| 240 | foreach ($contactTag as $tag) { |
| 241 | $defaults[$fName . '[' . $tag . ']'] = 1; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Set default values for the form. Note that in edit/view mode |
| 249 | * the default values are retrieved from the database |
| 250 | * |
| 251 | * @access public |
| 252 | * |
| 253 | * @param CRM_Core_Form $form |
| 254 | * @param $defaults |
| 255 | * |
| 256 | * @return void |
| 257 | */ |
| 258 | public static function setDefaultValues(&$form, &$defaults) { |
| 259 | $contactEditOptions = $form->get('contactEditOptions'); |
| 260 | |
| 261 | if ($form->_action & CRM_Core_Action::ADD) { |
| 262 | if (array_key_exists('TagsAndGroups', $contactEditOptions)) { |
| 263 | // set group and tag defaults if any |
| 264 | if ($form->_gid) { |
| 265 | $defaults['group'][$form->_gid] = 1; |
| 266 | } |
| 267 | if ($form->_tid) { |
| 268 | $defaults['tag'][$form->_tid] = 1; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | else { |
| 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'; |
| 278 | } |
| 279 | self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |