| 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 | class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic { |
| 36 | |
| 37 | /** |
| 38 | * The action links that we need to display for the browse screen |
| 39 | * |
| 40 | * @var array |
| 41 | * @static |
| 42 | */ |
| 43 | static $_links = NULL; |
| 44 | |
| 45 | protected $_fields = NULL; |
| 46 | |
| 47 | protected $_profileId = NULL; |
| 48 | |
| 49 | protected $_contactId = NULL; |
| 50 | |
| 51 | protected $_customGroupTitle = NULL; |
| 52 | |
| 53 | protected $_pageViewType = NULL; |
| 54 | |
| 55 | protected $_contactType = NULL; |
| 56 | /** |
| 57 | * Get BAO Name |
| 58 | * |
| 59 | * @return string Classname of BAO. |
| 60 | */ |
| 61 | function getBAOName() { |
| 62 | return ''; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get action Links |
| 67 | * |
| 68 | * @return array (reference) of action links |
| 69 | */ |
| 70 | function &links() { |
| 71 | if (!(self::$_links[$this->_pageViewType])) { |
| 72 | // helper variable for nicer formatting |
| 73 | $links = array(); |
| 74 | |
| 75 | $view = array_search(CRM_Core_Action::VIEW, CRM_Core_Action::$_names); |
| 76 | $update = array_search(CRM_Core_Action::UPDATE, CRM_Core_Action::$_names); |
| 77 | $delete = array_search(CRM_Core_Action::DELETE, CRM_Core_Action::$_names); |
| 78 | |
| 79 | // names and titles |
| 80 | $links[CRM_Core_Action::VIEW] = array( |
| 81 | 'name' => ts('View'), |
| 82 | 'title' => ts('View %1', array( 1 => $this->_customGroupTitle . ' record')), |
| 83 | ); |
| 84 | |
| 85 | $links[CRM_Core_Action::UPDATE] = array( |
| 86 | 'name' => ts('Edit'), |
| 87 | 'title' => ts('Edit %1', array( 1 => $this->_customGroupTitle . ' record')), |
| 88 | ); |
| 89 | |
| 90 | $links[CRM_Core_Action::DELETE] = array( |
| 91 | 'name' => ts('Delete'), |
| 92 | 'title' => ts('Delete %1', array( 1 => $this->_customGroupTitle . ' record')), |
| 93 | ); |
| 94 | |
| 95 | // urls and queryStrings |
| 96 | if ($this->_pageViewType == 'profileDataView') { |
| 97 | $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/profile/view'; |
| 98 | $links[CRM_Core_Action::VIEW]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$view}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%"; |
| 99 | |
| 100 | $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/profile/edit'; |
| 101 | $links[CRM_Core_Action::UPDATE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$update}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%"; |
| 102 | |
| 103 | $links[CRM_Core_Action::DELETE]['url'] = 'civicrm/profile/edit'; |
| 104 | $links[CRM_Core_Action::DELETE]['qs'] = "reset=1&id=%%id%%&recordId=%%recordId%%&gid=%%gid%%&multiRecord={$delete}&snippet=1&context=multiProfileDialog&onPopupClose=%%onPopupClose%%"; |
| 105 | |
| 106 | } |
| 107 | elseif ($this->_pageViewType == 'customDataView') { |
| 108 | // custom data specific view links |
| 109 | $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd'; |
| 110 | $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&snippet=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&multiRecordDisplay=single'; |
| 111 | |
| 112 | // custom data specific update links |
| 113 | $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit'; |
| 114 | $links[CRM_Core_Action::UPDATE]['qs'] = 'reset=1&snippet=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%cgcount%%&multiRecordDisplay=single'; |
| 115 | // NOTE : links for DELETE action for customDataView is handled in browse |
| 116 | |
| 117 | // copy action |
| 118 | $links[CRM_Core_Action::COPY] = array( |
| 119 | 'name' => ts('Copy'), |
| 120 | 'title' => ts('Copy %1', array( 1 => $this->_customGroupTitle . ' record')), |
| 121 | 'url' => 'civicrm/contact/view/cd/edit', |
| 122 | 'qs' => 'reset=1&snippet=1&type=%%type%%&groupID=%%groupID%%&entityID=%%entityID%%&cgcount=%%newCgCount%%&multiRecordDisplay=single©ValueId=%%cgcount%%' |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | self::$_links[$this->_pageViewType] = $links; |
| 127 | } |
| 128 | return self::$_links[$this->_pageViewType]; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Run the page |
| 133 | * |
| 134 | * This method is called after the page is created. It checks for the type |
| 135 | * of action and executes that action. Finally it calls the parent's run |
| 136 | * method. |
| 137 | * |
| 138 | * @return void |
| 139 | * @access public |
| 140 | * |
| 141 | */ |
| 142 | function run() { |
| 143 | // get the requested action, default to 'browse' |
| 144 | $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, FALSE); |
| 145 | $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this); |
| 146 | |
| 147 | // assign vars to templates |
| 148 | $this->assign('action', $action); |
| 149 | $profileId = CRM_Utils_Request::retrieve('profileId', 'Positive', $this, FALSE); |
| 150 | if (!is_array($profileId) && is_numeric($profileId)) { |
| 151 | $this->_profileId = $profileId; |
| 152 | } |
| 153 | |
| 154 | $this->_contactId = CRM_Utils_Request::retrieve('contactId', 'Positive', $this, FALSE); |
| 155 | $this->_pageViewType = CRM_Utils_Request::retrieve('pageViewType', 'Positive', $this, FALSE, 'profileDataView'); |
| 156 | $this->_customGroupId = CRM_Utils_Request::retrieve('customGroupId', 'Positive', $this, FALSE, 0); |
| 157 | $this->_contactType = CRM_Utils_Request::retrieve('contactType', 'String', $this, FALSE); |
| 158 | if ($action & CRM_Core_Action::BROWSE) { |
| 159 | //browse |
| 160 | $this->browse(); |
| 161 | return; |
| 162 | } |
| 163 | // parent run |
| 164 | return parent::run(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Browse the listing |
| 169 | * |
| 170 | * @return void |
| 171 | * @access public |
| 172 | */ |
| 173 | function browse() { |
| 174 | $dateFields = NULL; |
| 175 | $dateFieldsVals = NULL; |
| 176 | if ($this->_pageViewType == 'profileDataView' && $this->_profileId) { |
| 177 | $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, |
| 178 | NULL, NULL, |
| 179 | FALSE, NULL, |
| 180 | FALSE, |
| 181 | NULL, |
| 182 | CRM_Core_Permission::EDIT |
| 183 | ); |
| 184 | $multiRecordFields = array( ); |
| 185 | $fieldIDs = NULL; |
| 186 | $result = NULL; |
| 187 | $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE); |
| 188 | |
| 189 | $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields)); |
| 190 | $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id'); |
| 191 | $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); |
| 192 | if (!$reached) { |
| 193 | $this->assign('contactId', $this->_contactId); |
| 194 | $this->assign('gid', $this->_profileId); |
| 195 | } |
| 196 | $this->assign('reachedMax', $reached); |
| 197 | |
| 198 | if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) { |
| 199 | $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing); |
| 200 | } |
| 201 | } |
| 202 | elseif ($this->_pageViewType == 'customDataView') { |
| 203 | // require custom group id for _pageViewType of customDataView |
| 204 | $customGroupId = $this->_customGroupId; |
| 205 | $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); |
| 206 | if (!$reached) { |
| 207 | $this->assign('contactId', $this->_contactId); |
| 208 | $this->assign('customGroupId', $customGroupId); |
| 209 | $this->assign('ctype', $this->_contactType); |
| 210 | } |
| 211 | $this->assign('reachedMax', $reached); |
| 212 | // custom group info : this consists of the field title of group fields |
| 213 | $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE); |
| 214 | // field ids of fields in_selector for the custom group id provided |
| 215 | $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']); |
| 216 | // field labels for headers |
| 217 | $fieldLabels = $groupDetail[$customGroupId]['fields']; |
| 218 | |
| 219 | // from the above customGroupInfo we can get $this->_customGroupTitle |
| 220 | $this->_customGroupTitle = $groupDetail[$customGroupId]['title']; |
| 221 | } |
| 222 | if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) { |
| 223 | $options = array( ); |
| 224 | $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format'); |
| 225 | foreach ($fieldIDs as $key => $fieldID) { |
| 226 | $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID; |
| 227 | $param = array('id' => $fieldIDs[$key]); |
| 228 | $returnValues = array( ); |
| 229 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities); |
| 230 | if ($returnValues['data_type'] == 'Date') { |
| 231 | $dateFields[$fieldIDs[$key]] = 1; |
| 232 | } |
| 233 | |
| 234 | $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]); |
| 235 | if (!empty($optionValuePairs)) { |
| 236 | foreach ($optionValuePairs as $optionPairs) { |
| 237 | $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label']; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type']; |
| 242 | $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type']; |
| 243 | |
| 244 | $options[$fieldIDs[$key]]['attributes']['format'] = |
| 245 | $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues); |
| 246 | $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues); |
| 247 | } |
| 248 | |
| 249 | // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView) |
| 250 | $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE); |
| 251 | |
| 252 | if ($this->_pageViewType == 'profileDataView') { |
| 253 | if (!empty($fieldIDs)) { |
| 254 | //get the group info of multi rec fields in listing view |
| 255 | $fieldInput = $fieldIDs; |
| 256 | $fieldIdInput = $fieldIDs[0]; |
| 257 | } |
| 258 | else { |
| 259 | //if no listing fields exist, take the group title for display |
| 260 | $nonListingFieldIds = array_keys($multiRecordFields); |
| 261 | $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]); |
| 262 | $fieldIdInput = $singleField; |
| 263 | $singleField = array($singleField); |
| 264 | $fieldInput = $singleField; |
| 265 | } |
| 266 | $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput); |
| 267 | $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle']; |
| 268 | } |
| 269 | // $cgcount is defined before 'if' condition as enitiy may have no record |
| 270 | // and $cgcount is used to build new record url |
| 271 | $cgcount = 1; |
| 272 | if ($result && !empty($result)) { |
| 273 | $links = self::links(); |
| 274 | if ($this->_pageViewType == 'profileDataView') { |
| 275 | $pageCheckSum = $this->get('pageCheckSum'); |
| 276 | if ($pageCheckSum) { |
| 277 | foreach ($links as $key => $link) { |
| 278 | $links[$key] = $link['qs'] . "&cs=%%cs%%"; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | $linkAction = array_sum(array_keys($this->links())); |
| 283 | |
| 284 | if ($reached) { |
| 285 | unset($links[CRM_Core_Action::COPY]); |
| 286 | } |
| 287 | $newCgCount = (!$reached) ? count($result) + 1 : NULL; |
| 288 | foreach ($result as $recId => &$value) { |
| 289 | foreach ($value as $fieldId => &$val) { |
| 290 | if (is_numeric($fieldId)) { |
| 291 | $customValue = &$val; |
| 292 | if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) { |
| 293 | // formated date capture value capture |
| 294 | $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); |
| 295 | } |
| 296 | else { |
| 297 | // assign to $result |
| 298 | $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); |
| 299 | } |
| 300 | if (!$customValue) { |
| 301 | $customValue = ""; |
| 302 | } |
| 303 | |
| 304 | $op = NULL; |
| 305 | if ($this->_pageViewType == 'profileDataView') { |
| 306 | $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, |
| 307 | 'id' => $this->_contactId, 'onPopupClose' => $this->_onPopupClose); |
| 308 | $op = 'profile.multiValue.row'; |
| 309 | } |
| 310 | else { |
| 311 | // different set of url params |
| 312 | $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId; |
| 313 | $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId; |
| 314 | $actionParams['recId'] = $recId; |
| 315 | $actionParams['type'] = $this->_contactType; |
| 316 | $actionParams['cgcount'] = $cgcount; |
| 317 | $actionParams['newCgCount'] = $newCgCount; |
| 318 | |
| 319 | // DELETE action links |
| 320 | $redirectUrl = CRM_Core_Session::singleton()->readUserContext(); |
| 321 | $links[CRM_Core_Action::DELETE]['url'] = '#'; |
| 322 | $links[CRM_Core_Action::DELETE]['extra'] = " onclick=' var ru = \"{$redirectUrl}\"; showDeleteInDialog({$recId}, {$this->_customGroupId}, {$this->_contactId}, ru)' "; |
| 323 | $links[CRM_Core_Action::DELETE]['class'] = 'ignore-jshref'; |
| 324 | } |
| 325 | if (!empty($pageCheckSum)) { |
| 326 | $actionParams['cs'] = $pageCheckSum; |
| 327 | } |
| 328 | |
| 329 | $value['action'] = CRM_Core_Action::formLink( |
| 330 | $links, |
| 331 | $linkAction, |
| 332 | $actionParams, |
| 333 | ts('more'), |
| 334 | FALSE, |
| 335 | $op, |
| 336 | 'customValue', |
| 337 | $fieldId // not ideal, but the one thing not sent in $actionParams |
| 338 | ); |
| 339 | } |
| 340 | } |
| 341 | $cgcount++; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | $headers = array( ); |
| 347 | if (!empty($fieldIDs)) { |
| 348 | foreach ($fieldIDs as $fieldID) { |
| 349 | $headers[$fieldID] = ($this->_pageViewType == 'profileDataView') ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label']; |
| 350 | } |
| 351 | } |
| 352 | $this->assign('dateFields', $dateFields); |
| 353 | $this->assign('dateFieldsVals',$dateFieldsVals); |
| 354 | $this->assign('cgcount', $cgcount); |
| 355 | $this->assign('customGroupTitle', $this->_customGroupTitle); |
| 356 | $this->assign('headers', $headers); |
| 357 | $this->assign('records', $result); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Get name of edit form |
| 362 | * |
| 363 | * @return string classname of edit form |
| 364 | */ |
| 365 | function editForm() { |
| 366 | return ''; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Get edit form name |
| 371 | * |
| 372 | * @return string name of this page |
| 373 | */ |
| 374 | function editName() { |
| 375 | return ''; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get user context |
| 380 | * |
| 381 | * @return string user context |
| 382 | */ |
| 383 | function userContext($mode = NULL) { |
| 384 | return ''; |
| 385 | } |
| 386 | } |
| 387 | |