| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 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 | * build form elements. |
| 46 | * params object $form object of the form |
| 47 | * |
| 48 | * @param CRM_Core_Form $form |
| 49 | * The form object that we are operating on. |
| 50 | * @param int $contactId |
| 51 | * Contact id. |
| 52 | * @param int $type |
| 53 | * What components are we interested in. |
| 54 | * @param bool $visibility |
| 55 | * Visibility of the field. |
| 56 | * @param null $isRequired |
| 57 | * @param string $groupName |
| 58 | * If used for building group block. |
| 59 | * @param string $tagName |
| 60 | * If used for building tag block. |
| 61 | * @param string $fieldName |
| 62 | * This is used in batch profile(i.e to build multiple blocks). |
| 63 | * |
| 64 | * @param string $groupElementType |
| 65 | * |
| 66 | */ |
| 67 | public static function buildQuickForm( |
| 68 | &$form, |
| 69 | $contactId = 0, |
| 70 | $type = self::ALL, |
| 71 | $visibility = FALSE, |
| 72 | $isRequired = NULL, |
| 73 | $groupName = 'Group(s)', |
| 74 | $tagName = 'Tag(s)', |
| 75 | $fieldName = NULL, |
| 76 | $groupElementType = 'checkbox' |
| 77 | ) { |
| 78 | if (!isset($form->_tagGroup)) { |
| 79 | $form->_tagGroup = array(); |
| 80 | } |
| 81 | |
| 82 | // NYSS 5670 |
| 83 | if (!$contactId && !empty($form->_contactId)) { |
| 84 | $contactId = $form->_contactId; |
| 85 | } |
| 86 | |
| 87 | $type = (int) $type; |
| 88 | if ($type & self::GROUP) { |
| 89 | |
| 90 | $fName = 'group'; |
| 91 | if ($fieldName) { |
| 92 | $fName = $fieldName; |
| 93 | } |
| 94 | |
| 95 | $groupID = isset($form->_grid) ? $form->_grid : NULL; |
| 96 | if ($groupID && $visibility) { |
| 97 | $ids = array($groupID => $groupID); |
| 98 | } |
| 99 | else { |
| 100 | if ($visibility) { |
| 101 | $group = CRM_Core_PseudoConstant::allGroup(); |
| 102 | } |
| 103 | else { |
| 104 | $group = CRM_Core_PseudoConstant::group(); |
| 105 | } |
| 106 | $ids = $group; |
| 107 | } |
| 108 | |
| 109 | if ($groupID || !empty($group)) { |
| 110 | $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids); |
| 111 | |
| 112 | $attributes['skiplabel'] = TRUE; |
| 113 | $elements = array(); |
| 114 | $groupsOptions = array(); |
| 115 | foreach ($groups as $id => $group) { |
| 116 | // make sure that this group has public visibility |
| 117 | if ($visibility && |
| 118 | $group['visibility'] == 'User and User Admin Only' |
| 119 | ) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | if ($groupElementType == 'select') { |
| 124 | $groupsOptions[$id] = $group['title']; |
| 125 | } |
| 126 | else { |
| 127 | $form->_tagGroup[$fName][$id]['description'] = $group['description']; |
| 128 | $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if ($groupElementType == 'select' && !empty($groupsOptions)) { |
| 133 | $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE, |
| 134 | array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2') |
| 135 | ); |
| 136 | $form->assign('groupCount', count($groupsOptions)); |
| 137 | } |
| 138 | |
| 139 | if ($groupElementType == 'checkbox' && !empty($elements)) { |
| 140 | $form->addGroup($elements, $fName, $groupName, ' <br />'); |
| 141 | $form->assign('groupCount', count($elements)); |
| 142 | if ($isRequired) { |
| 143 | $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required'); |
| 144 | } |
| 145 | } |
| 146 | $form->assign('groupElementType', $groupElementType); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if ($type & self::TAG) { |
| 151 | $fName = 'tag'; |
| 152 | if ($fieldName) { |
| 153 | $fName = $fieldName; |
| 154 | } |
| 155 | $form->_tagGroup[$fName] = 1; |
| 156 | |
| 157 | // get the list of all the categories |
| 158 | $tags = new CRM_Core_BAO_Tag(); |
| 159 | $tree = $tags->getTree('civicrm_contact', TRUE); |
| 160 | // let's not load jstree if there are not children. This also fixes blank |
| 161 | // display at the beginning of checkboxes |
| 162 | $form->assign('loadjsTree', FALSE); |
| 163 | if (!empty(CRM_Utils_Array::retrieveValueRecursive($tree, 'children'))) { |
| 164 | // CODE FROM CRM/Tag/Form/Tag.php // |
| 165 | CRM_Core_Resources::singleton() |
| 166 | ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE) |
| 167 | ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header'); |
| 168 | $form->assign('loadjsTree', TRUE); |
| 169 | } |
| 170 | |
| 171 | $elements = array(); |
| 172 | self::climbtree($form, $tree, $elements); |
| 173 | |
| 174 | $form->addGroup($elements, $fName, $tagName, '<br />'); |
| 175 | $form->assign('tagCount', count($elements)); |
| 176 | $form->assign('tree', $tree); |
| 177 | $form->assign('tag', $tree); |
| 178 | $form->assign('entityID', $contactId); |
| 179 | $form->assign('entityTable', 'civicrm_contact'); |
| 180 | |
| 181 | if ($isRequired) { |
| 182 | $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required'); |
| 183 | } |
| 184 | |
| 185 | // build tag widget |
| 186 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); |
| 187 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE); |
| 188 | } |
| 189 | $form->assign('tagGroup', $form->_tagGroup); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Climb tree. |
| 194 | * |
| 195 | * @param $form |
| 196 | * @param $tree |
| 197 | * @param $elements |
| 198 | * |
| 199 | * @return mixed |
| 200 | */ |
| 201 | public static function climbtree($form, $tree, &$elements) { |
| 202 | foreach ($tree as $tagID => $varValue) { |
| 203 | $tagAttribute = array( |
| 204 | 'onclick' => "return changeRowColor(\"rowidtag_$tagID\")", |
| 205 | 'id' => "tag_{$tagID}", |
| 206 | ); |
| 207 | |
| 208 | $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute); |
| 209 | |
| 210 | if (array_key_exists('children', $varValue)) { |
| 211 | self::climbtree($form, $varValue['children'], $elements); |
| 212 | } |
| 213 | } |
| 214 | return $elements; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Set defaults for relevant form elements. |
| 219 | * |
| 220 | * @param int $id |
| 221 | * The contact id. |
| 222 | * @param array $defaults |
| 223 | * The defaults array to store the values in. |
| 224 | * @param int $type |
| 225 | * What components are we interested in. |
| 226 | * @param string $fieldName |
| 227 | * This is used in batch profile(i.e to build multiple blocks). |
| 228 | * |
| 229 | * @param string $groupElementType |
| 230 | * |
| 231 | * @return void |
| 232 | */ |
| 233 | public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') { |
| 234 | $type = (int ) $type; |
| 235 | if ($type & self::GROUP) { |
| 236 | $fName = 'group'; |
| 237 | if ($fieldName) { |
| 238 | $fName = $fieldName; |
| 239 | } |
| 240 | |
| 241 | $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE); |
| 242 | if ($contactGroup) { |
| 243 | foreach ($contactGroup as $group) { |
| 244 | if ($groupElementType == 'select') { |
| 245 | $defaults[$fName][] = $group['group_id']; |
| 246 | } |
| 247 | else { |
| 248 | $defaults[$fName . '[' . $group['group_id'] . ']'] = 1; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if ($type & self::TAG) { |
| 255 | $fName = 'tag'; |
| 256 | if ($fieldName) { |
| 257 | $fName = $fieldName; |
| 258 | } |
| 259 | |
| 260 | $contactTag = CRM_Core_BAO_EntityTag::getTag($id); |
| 261 | if ($contactTag) { |
| 262 | foreach ($contactTag as $tag) { |
| 263 | $defaults[$fName . '[' . $tag . ']'] = 1; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Set default values for the form. Note that in edit/view mode |
| 271 | * the default values are retrieved from the database |
| 272 | * |
| 273 | * |
| 274 | * @param CRM_Core_Form $form |
| 275 | * @param $defaults |
| 276 | * |
| 277 | * @return void |
| 278 | */ |
| 279 | public static function setDefaultValues(&$form, &$defaults) { |
| 280 | $contactEditOptions = $form->get('contactEditOptions'); |
| 281 | |
| 282 | if ($form->_action & CRM_Core_Action::ADD) { |
| 283 | if (array_key_exists('TagsAndGroups', $contactEditOptions)) { |
| 284 | // set group and tag defaults if any |
| 285 | if ($form->_gid) { |
| 286 | $defaults['group'][$form->_gid] = 1; |
| 287 | } |
| 288 | if ($form->_tid) { |
| 289 | $defaults['tag'][$form->_tid] = 1; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | else { |
| 294 | if (array_key_exists('TagsAndGroups', $contactEditOptions)) { |
| 295 | // set the group and tag ids |
| 296 | $groupElementType = 'checkbox'; |
| 297 | if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') { |
| 298 | $groupElementType = 'select'; |
| 299 | } |
| 300 | self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | } |