| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Create a page for displaying Custom Sets. |
| 38 | * |
| 39 | * Heart of this class is the run method which checks |
| 40 | * for action type and then displays the appropriate |
| 41 | * page. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Custom_Page_Group extends CRM_Core_Page { |
| 45 | |
| 46 | /** |
| 47 | * The action links that we need to display for the browse screen |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | private static $_actionLinks; |
| 52 | |
| 53 | /** |
| 54 | * Get the action links for this page. |
| 55 | * |
| 56 | * @param null |
| 57 | * |
| 58 | * @return array array of action links that we need to display for the browse screen |
| 59 | * @access public |
| 60 | */ |
| 61 | function &actionLinks() { |
| 62 | // check if variable _actionsLinks is populated |
| 63 | if (!isset(self::$_actionLinks)) { |
| 64 | self::$_actionLinks = array( |
| 65 | CRM_Core_Action::BROWSE => array( |
| 66 | 'name' => ts('View and Edit Custom Fields'), |
| 67 | 'url' => 'civicrm/admin/custom/group/field', |
| 68 | 'qs' => 'reset=1&action=browse&gid=%%id%%', |
| 69 | 'title' => ts('View and Edit Custom Fields'), |
| 70 | ), |
| 71 | CRM_Core_Action::PREVIEW => array( |
| 72 | 'name' => ts('Preview'), |
| 73 | 'url' => 'civicrm/admin/custom/group', |
| 74 | 'qs' => 'action=preview&reset=1&id=%%id%%', |
| 75 | 'title' => ts('Preview Custom Data Set'), |
| 76 | ), |
| 77 | CRM_Core_Action::UPDATE => array( |
| 78 | 'name' => ts('Settings'), |
| 79 | 'url' => 'civicrm/admin/custom/group', |
| 80 | 'qs' => 'action=update&reset=1&id=%%id%%', |
| 81 | 'title' => ts('Edit Custom Set'), |
| 82 | ), |
| 83 | CRM_Core_Action::DISABLE => array( |
| 84 | 'name' => ts('Disable'), |
| 85 | 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_CustomGroup' . '\',\'' . 'enable-disable' . '\' );"', |
| 86 | 'ref' => 'disable-action', |
| 87 | 'title' => ts('Disable Custom Set'), |
| 88 | ), |
| 89 | CRM_Core_Action::ENABLE => array( |
| 90 | 'name' => ts('Enable'), |
| 91 | 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_CustomGroup' . '\',\'' . 'disable-enable' . '\' );"', |
| 92 | 'ref' => 'enable-action', |
| 93 | 'title' => ts('Enable Custom Set'), |
| 94 | ), |
| 95 | CRM_Core_Action::DELETE => array( |
| 96 | 'name' => ts('Delete'), |
| 97 | 'url' => 'civicrm/admin/custom/group', |
| 98 | 'qs' => 'action=delete&reset=1&id=%%id%%', |
| 99 | 'title' => ts('Delete Custom Set'), |
| 100 | ), |
| 101 | ); |
| 102 | } |
| 103 | return self::$_actionLinks; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Run the page. |
| 108 | * |
| 109 | * This method is called after the page is created. It checks for the |
| 110 | * type of action and executes that action. |
| 111 | * Finally it calls the parent's run method. |
| 112 | * |
| 113 | * @param null |
| 114 | * |
| 115 | * @return void |
| 116 | * @access public |
| 117 | * |
| 118 | */ |
| 119 | function run() { |
| 120 | // get the requested action |
| 121 | $action = CRM_Utils_Request::retrieve('action', 'String', |
| 122 | // default to 'browse' |
| 123 | $this, FALSE, 'browse' |
| 124 | ); |
| 125 | |
| 126 | if ($action & CRM_Core_Action::DELETE) { |
| 127 | $session = CRM_Core_Session::singleton(); |
| 128 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/', 'action=browse')); |
| 129 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteGroup', "Delete Cutom Set", NULL); |
| 130 | $id = CRM_Utils_Request::retrieve('id', 'Positive', |
| 131 | $this, FALSE, 0 |
| 132 | ); |
| 133 | $controller->set('id', $id); |
| 134 | $controller->setEmbedded(TRUE); |
| 135 | $controller->process(); |
| 136 | $controller->run(); |
| 137 | } |
| 138 | // assign vars to templates |
| 139 | $this->assign('action', $action); |
| 140 | $id = CRM_Utils_Request::retrieve('id', 'Positive', |
| 141 | $this, FALSE, 0 |
| 142 | ); |
| 143 | |
| 144 | // what action to take ? |
| 145 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { |
| 146 | $this->edit($id, $action); |
| 147 | } |
| 148 | elseif ($action & CRM_Core_Action::PREVIEW) { |
| 149 | $this->preview($id); |
| 150 | } |
| 151 | else { |
| 152 | // finally browse the custom groups |
| 153 | $this->browse(); |
| 154 | } |
| 155 | // parent run |
| 156 | return parent::run(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * edit custom group |
| 161 | * |
| 162 | * @param int $id custom group id |
| 163 | * @param string $action the action to be invoked |
| 164 | * |
| 165 | * @return void |
| 166 | * @access public |
| 167 | */ |
| 168 | function edit($id, $action) { |
| 169 | // create a simple controller for editing custom data |
| 170 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Group', ts('Custom Set'), $action); |
| 171 | |
| 172 | // set the userContext stack |
| 173 | $session = CRM_Core_Session::singleton(); |
| 174 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/', 'action=browse')); |
| 175 | $controller->set('id', $id); |
| 176 | $controller->setEmbedded(TRUE); |
| 177 | $controller->process(); |
| 178 | $controller->run(); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Preview custom group |
| 183 | * |
| 184 | * @param int $id custom group id |
| 185 | * |
| 186 | * @return void |
| 187 | * @access public |
| 188 | */ |
| 189 | function preview($id) { |
| 190 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), NULL); |
| 191 | $session = CRM_Core_Session::singleton(); |
| 192 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group', 'action=browse')); |
| 193 | $controller->set('groupId', $id); |
| 194 | $controller->setEmbedded(TRUE); |
| 195 | $controller->process(); |
| 196 | $controller->run(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Browse all custom data groups. |
| 201 | * |
| 202 | * @param string $action the action to be invoked |
| 203 | * |
| 204 | * @return void |
| 205 | * @access public |
| 206 | */ |
| 207 | function browse($action = NULL) { |
| 208 | // get all custom groups sorted by weight |
| 209 | $customGroup = array(); |
| 210 | $dao = new CRM_Core_DAO_CustomGroup(); |
| 211 | $dao->is_reserved = FALSE; |
| 212 | $dao->orderBy('weight, title'); |
| 213 | $dao->find(); |
| 214 | |
| 215 | while ($dao->fetch()) { |
| 216 | $customGroup[$dao->id] = array(); |
| 217 | CRM_Core_DAO::storeValues($dao, $customGroup[$dao->id]); |
| 218 | // form all action links |
| 219 | $action = array_sum(array_keys($this->actionLinks())); |
| 220 | |
| 221 | // update enable/disable links depending on custom_group properties. |
| 222 | if ($dao->is_active) { |
| 223 | $action -= CRM_Core_Action::ENABLE; |
| 224 | } |
| 225 | else { |
| 226 | $action -= CRM_Core_Action::DISABLE; |
| 227 | } |
| 228 | $customGroup[$dao->id]['order'] = $customGroup[$dao->id]['weight']; |
| 229 | $customGroup[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, |
| 230 | array('id' => $dao->id) |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | $customGroupExtends = CRM_Core_SelectValues::customGroupExtends(); |
| 235 | foreach ($customGroup as $key => $array) { |
| 236 | CRM_Core_DAO_CustomGroup::addDisplayEnums($customGroup[$key]); |
| 237 | $customGroup[$key]['extends_display'] = $customGroupExtends[$customGroup[$key]['extends']]; |
| 238 | } |
| 239 | |
| 240 | //fix for Displaying subTypes |
| 241 | $subTypes = array(); |
| 242 | |
| 243 | $subTypes['Activity'] = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); |
| 244 | $subTypes['Contribution'] = CRM_Contribute_PseudoConstant::financialType( ); |
| 245 | $subTypes['Membership'] = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE); |
| 246 | $subTypes['Event'] = CRM_Core_OptionGroup::values('event_type'); |
| 247 | $subTypes['Grant'] = CRM_Core_OptionGroup::values('grant_type'); |
| 248 | $subTypes['Campaign'] = CRM_Campaign_PseudoConstant::campaignType(); |
| 249 | $subTypes['Participant'] = array(); |
| 250 | $subTypes['ParticipantRole'] = CRM_Core_OptionGroup::values('participant_role');; |
| 251 | $subTypes['ParticipantEventName'] = CRM_Event_PseudoConstant::event(); |
| 252 | $subTypes['ParticipantEventType'] = CRM_Core_OptionGroup::values('event_type'); |
| 253 | $subTypes['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL); |
| 254 | $subTypes['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL); |
| 255 | $subTypes['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL); |
| 256 | |
| 257 | |
| 258 | $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Individual'); |
| 259 | $relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Organization'); |
| 260 | $relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Household'); |
| 261 | |
| 262 | $allRelationshipType = array(); |
| 263 | $allRelationshipType = array_merge($relTypeInd, $relTypeOrg); |
| 264 | $allRelationshipType = array_merge($allRelationshipType, $relTypeHou); |
| 265 | |
| 266 | //adding subtype specific relationships CRM-5256 |
| 267 | $relSubType = CRM_Contact_BAO_ContactType::subTypeInfo(); |
| 268 | foreach ($relSubType as $subType => $val) { |
| 269 | $subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'], |
| 270 | FALSE, 'label', TRUE, $subType |
| 271 | ); |
| 272 | $allRelationshipType = array_merge($allRelationshipType, $subTypeRelationshipTypes); |
| 273 | } |
| 274 | |
| 275 | $subTypes['Relationship'] = $allRelationshipType; |
| 276 | |
| 277 | $cSubTypes = CRM_Core_Component::contactSubTypes(); |
| 278 | $contactSubTypes = array(); |
| 279 | foreach ($cSubTypes as $key => $value) { |
| 280 | $contactSubTypes[$key] = $key; |
| 281 | } |
| 282 | |
| 283 | $subTypes['Contact'] = $contactSubTypes; |
| 284 | |
| 285 | CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($subTypes); |
| 286 | |
| 287 | foreach ($customGroup as $key => $values) { |
| 288 | $subValue = CRM_Utils_Array::value('extends_entity_column_value', $customGroup[$key]); |
| 289 | $subName = CRM_Utils_Array::value('extends_entity_column_id', $customGroup[$key]); |
| 290 | $type = CRM_Utils_Array::value('extends', $customGroup[$key]); |
| 291 | if ($subValue) { |
| 292 | $subValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 293 | substr($subValue, 1, -1) |
| 294 | ); |
| 295 | $colValue = NULL; |
| 296 | foreach ($subValue as $sub) { |
| 297 | if ($sub) { |
| 298 | if ($type == 'Participant') { |
| 299 | if ($subName == 1) { |
| 300 | $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantRole'][$sub] : $subTypes['ParticipantRole'][$sub]; |
| 301 | } |
| 302 | elseif ($subName == 2) { |
| 303 | $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantEventName'][$sub] : $subTypes['ParticipantEventName'][$sub]; |
| 304 | } |
| 305 | elseif ($subName == 3) { |
| 306 | $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantEventType'][$sub] : $subTypes['ParticipantEventType'][$sub]; |
| 307 | } |
| 308 | } |
| 309 | elseif ($type == 'Relationship') { |
| 310 | $colValue = $colValue ? $colValue . ', ' . $subTypes[$type][$sub . '_a_b'] : $subTypes[$type][$sub . '_a_b']; |
| 311 | if (isset($subTypes[$type][$sub . '_b_a'])) { |
| 312 | $colValue = $colValue ? $colValue . ', ' . $subTypes[$type][$sub . '_b_a'] : $subTypes[$type][$sub . '_b_a']; |
| 313 | } |
| 314 | } |
| 315 | else { |
| 316 | $colValue = $colValue ? ($colValue . (isset($subTypes[$type][$sub]) ? ', ' . $subTypes[$type][$sub] : '')) : (isset($subTypes[$type][$sub]) ? $subTypes[$type][$sub] : ''); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | $customGroup[$key]["extends_entity_column_value"] = $colValue; |
| 321 | } |
| 322 | else { |
| 323 | if (is_array(CRM_Utils_Array::value($type, $subTypes))) { |
| 324 | $customGroup[$key]["extends_entity_column_value"] = ts("Any"); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group', "reset=1&action=browse"); |
| 330 | CRM_Utils_Weight::addOrder($customGroup, 'CRM_Core_DAO_CustomGroup', |
| 331 | 'id', $returnURL |
| 332 | ); |
| 333 | |
| 334 | $this->assign('rows', $customGroup); |
| 335 | } |
| 336 | } |
| 337 | |