/** * Run the page. * * This method is called after the page is created. It checks for the * type of action and executes that action. * * @return void */ public function run() { $template = CRM_Core_Smarty::singleton(); if ($this->_id && $this->_gid) { // first check that id is part of the limit group id, CRM-4822 $limitListingsGroupsID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'limit_listings_group_id'); $config = CRM_Core_Config::singleton(); if ($limitListingsGroupsID) { if (!CRM_Contact_BAO_GroupContact::isContactInGroup($this->_id, $limitListingsGroupsID)) { CRM_Utils_System::setTitle(ts('Profile View - Permission Denied')); return CRM_Core_Session::setStatus(ts('You do not have permission to view this contact record. Contact the site administrator if you need assistance.'), ts('Permission Denied'), 'error'); } } $session = CRM_Core_Session::singleton(); $userID = $session->get('userID'); $this->_isPermissionedChecksum = $allowPermission = FALSE; $permissionType = CRM_Core_Permission::VIEW; if (CRM_Core_Permission::check('administer users') || CRM_Core_Permission::check('view all contacts') || CRM_Contact_BAO_Contact_Permission::allow($this->_id)) { $allowPermission = TRUE; } if ($this->_id != $userID) { // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228 if ($config->userFrameworkFrontend) { $this->_isPermissionedChecksum = CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum($this->_id, $this, FALSE); if (!$this->_isPermissionedChecksum) { $this->_isPermissionedChecksum = $allowPermission; } } else { $this->_isPermissionedChecksum = CRM_Contact_BAO_Contact_Permission::validateChecksumContact($this->_id, $this, FALSE); } } // CRM-10853 // Users with create or edit permission should be allowed to view their own profile if ($this->_id == $userID || $this->_isPermissionedChecksum) { if (!CRM_Core_Permission::check('profile view')) { if (CRM_Core_Permission::check('profile create') || CRM_Core_Permission::check('profile edit')) { $this->_skipPermission = TRUE; } } } // make sure we dont expose all fields based on permission $admin = FALSE; if (!$config->userFrameworkFrontend && $allowPermission || $this->_id == $userID || $this->_isPermissionedChecksum) { $admin = TRUE; } $values = array(); $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileIds, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, $this->_restrict, $this->_skipPermission, NULL, $permissionType); if ($this->_multiRecord & CRM_Core_Action::VIEW && $this->_recordId && !$this->_allFields) { CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields); $fields = $multiRecordFields; } if ($this->_isContactActivityProfile && $this->_gid) { $errors = CRM_Profile_Form::validateContactActivityProfile($this->_activityId, $this->_id, $this->_gid); if (!empty($errors)) { CRM_Core_Error::fatal(array_pop($errors)); } } //reformat fields array foreach ($fields as $name => $field) { // also eliminate all formatting fields if (CRM_Utils_Array::value('field_type', $field) == 'Formatting') { unset($fields[$name]); } // make sure that there is enough permission to expose this field if (!$admin && $field['visibility'] == 'User and User Admin Only') { unset($fields[$name]); } } if ($this->_isContactActivityProfile) { $contactFields = $activityFields = array(); foreach ($fields as $fieldName => $field) { if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { $activityFields[$fieldName] = $field; } else { $contactFields[$fieldName] = $field; } } CRM_Core_BAO_UFGroup::getValues($this->_id, $contactFields, $values); if ($this->_activityId) { CRM_Core_BAO_UFGroup::getValues(NULL, $activityFields, $values, TRUE, array(array('activity_id', '=', $this->_activityId, 0, 0))); } } else { $customWhereClause = NULL; if ($this->_multiRecord & CRM_Core_Action::VIEW && $this->_recordId) { if ($this->_allFields) { $copyFields = $fields; CRM_Core_BAO_UFGroup::shiftMultiRecordFields($copyFields, $multiRecordFields); $fieldKey = key($multiRecordFields); } else { $fieldKey = key($fields); } if ($fieldID = CRM_Core_BAO_CustomField::getKeyID($fieldKey)) { $tableColumnGroup = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); $columnName = "{$tableColumnGroup[0]}.id"; $customWhereClause = $columnName . ' = ' . $this->_recordId; } } CRM_Core_BAO_UFGroup::getValues($this->_id, $fields, $values, TRUE, NULL, FALSE, $customWhereClause); } // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl $profileFields = array(); $labels = array(); foreach ($fields as $name => $field) { //CRM-14338 // Create a unique, non-empty index for each field. $index = $field['title']; if ($index === '') { $index = ' '; } while (array_key_exists($index, $labels)) { $index .= ' '; } $labels[$index] = preg_replace('/\\s+|\\W+/', '_', $name); } foreach ($values as $title => $value) { $profileFields[$labels[$title]] = array('label' => $title, 'value' => $value); } $template->assign_by_ref('row', $values); $template->assign_by_ref('profileFields', $profileFields); } $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name'); $this->assign('ufGroupName', $name); CRM_Utils_Hook::viewProfile($name); if (strtolower($name) == 'summary_overlay') { $template->assign('overlayProfile', TRUE); } if ($this->_multiRecord & CRM_Core_Action::VIEW && $this->_recordId && !$this->_allFields) { $fieldDetail = reset($fields); $fieldId = CRM_Core_BAO_CustomField::getKeyID($fieldDetail['name']); $customGroupDetails = CRM_Core_BAO_CustomGroup::getGroupTitles(array($fieldId)); $multiRecTitle = $customGroupDetails[$fieldId]['groupTitle']; } else { $title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'title'); } //CRM-4131. $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name'); if ($displayName) { $session = CRM_Core_Session::singleton(); $config = CRM_Core_Config::singleton(); if ($session->get('userID') && CRM_Core_Permission::check('access CiviCRM') && CRM_Contact_BAO_Contact_Permission::allow($session->get('userID'), CRM_Core_Permission::VIEW) && !$config->userFrameworkFrontend) { $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "action=view&reset=1&cid={$this->_id}", TRUE); $this->assign('displayName', $displayName); $displayName = "<a href=\"{$contactViewUrl}\">{$displayName}</a>"; } $title .= ' - ' . $displayName; } $title = isset($multiRecTitle) ? ts('View %1 Record', array(1 => $multiRecTitle)) : $title; CRM_Utils_System::setTitle($title); // invoke the pagRun hook, CRM-3906 CRM_Utils_Hook::pageRun($this); return trim($template->fetch($this->getHookedTemplateFileName())); }
/** * Pre processing work done here. * * gets session variables for table name, id of entity in table, type of entity and stores them. * * @param * * @return void */ public function preProcess() { $this->_id = $this->get('id'); $this->_profileIds = $this->get('profileIds'); $this->_grid = CRM_Utils_Request::retrieve('grid', 'Integer', $this); $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); //unset from session when $_GET doesn't have it //except when the form is submitted if (empty($_POST)) { if (!array_key_exists('multiRecord', $_GET)) { $this->set('multiRecord', NULL); } if (!array_key_exists('recordId', $_GET)) { $this->set('recordId', NULL); } } $this->_session = CRM_Core_Session::singleton(); $this->_currentUserID = $this->_session->get('userID'); if ($this->_mode == self::MODE_EDIT) { //specifies the action being done on a multi record field $multiRecordAction = CRM_Utils_Request::retrieve('multiRecord', 'String', $this); $this->_multiRecord = !is_numeric($multiRecordAction) ? CRM_Core_Action::resolve($multiRecordAction) : $multiRecordAction; if ($this->_multiRecord) { $this->set('multiRecord', $this->_multiRecord); } if ($this->_multiRecord && !in_array($this->_multiRecord, array(CRM_Core_Action::UPDATE, CRM_Core_Action::ADD, CRM_Core_Action::DELETE))) { CRM_Core_Error::fatal(ts('Proper action not specified for this custom value record profile')); } } $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate'); $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0)); if (count($gids) > 1 && !$this->_profileIds && empty($this->_profileIds)) { if (!empty($gids)) { foreach ($gids as $pfId) { $this->_profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive'); } } // check if we are rendering mixed profiles if (CRM_Core_BAO_UFGroup::checkForMixProfiles($this->_profileIds)) { CRM_Core_Error::fatal(ts('You cannot combine profiles of multiple types.')); } // for now consider 1'st profile as primary profile and validate it // i.e check for profile type etc. // FIX ME: validations for other than primary $this->_gid = $this->_profileIds[0]; $this->set('gid', $this->_gid); $this->set('profileIds', $this->_profileIds); } if (!$this->_gid) { $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0); $this->set('gid', $this->_gid); } $this->_activityId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET'); if (is_numeric($this->_activityId)) { $latestRevisionId = CRM_Activity_BAO_Activity::getLatestActivityId($this->_activityId); if ($latestRevisionId) { $this->_activityId = $latestRevisionId; } } $this->_isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($this->_gid); //get values for ufGroupName, captch and dupe update. if ($this->_gid) { $dao = new CRM_Core_DAO_UFGroup(); $dao->id = $this->_gid; if ($dao->find(TRUE)) { $this->_isUpdateDupe = $dao->is_update_dupe; $this->_isAddCaptcha = $dao->add_captcha; $this->_ufGroup = (array) $dao; } $dao->free(); if (!CRM_Utils_Array::value('is_active', $this->_ufGroup)) { CRM_Core_Error::fatal(ts('The requested profile (gid=%1) is inactive or does not exist.', array(1 => $this->_gid))); } } $this->assign('ufGroupName', $this->_ufGroup['name']); $gids = empty($this->_profileIds) ? $this->_gid : $this->_profileIds; // if we dont have a gid use the default, else just use that specific gid if (($this->_mode == self::MODE_REGISTER || $this->_mode == self::MODE_CREATE) && !$this->_gid) { $this->_ctype = CRM_Utils_Request::retrieve('ctype', 'String', $this, FALSE, 'Individual', 'REQUEST'); $this->_fields = CRM_Core_BAO_UFGroup::getRegistrationFields($this->_action, $this->_mode, $this->_ctype); } elseif ($this->_mode == self::MODE_SEARCH) { $this->_fields = CRM_Core_BAO_UFGroup::getListingFields($this->_action, CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY, FALSE, $gids, TRUE, NULL, $this->_skipPermission, CRM_Core_Permission::SEARCH); } else { $this->_fields = CRM_Core_BAO_UFGroup::getFields($gids, FALSE, NULL, NULL, NULL, FALSE, NULL, $this->_skipPermission, NULL, $this->_action == CRM_Core_Action::ADD ? CRM_Core_Permission::CREATE : CRM_Core_Permission::EDIT); $multiRecordFieldListing = FALSE; //using selector for listing of multirecord fields if ($this->_mode == self::MODE_EDIT && $this->_gid) { CRM_Core_BAO_UFGroup::shiftMultiRecordFields($this->_fields, $this->_multiRecordFields); if ($this->_multiRecord) { if ($this->_multiRecord != CRM_Core_Action::ADD) { $this->_recordId = CRM_Utils_Request::retrieve('recordId', 'Positive', $this); } else { $this->_recordId = NULL; $this->set('recordId', NULL); } //record id is necessary for _multiRecord view and update/edit action if (!$this->_recordId && ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE)) { CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) requires record id while performing this action', array(1 => $this->_gid))); } elseif (empty($this->_multiRecordFields)) { CRM_Core_Error::fatal(ts('No Multi-Record Fields configured for this profile (gid=%1)', array(1 => $this->_gid))); } $fieldId = CRM_Core_BAO_CustomField::getKeyID(key($this->_multiRecordFields)); $customGroupDetails = CRM_Core_BAO_CustomGroup::getGroupTitles(array($fieldId)); $this->_customGroupTitle = $customGroupDetails[$fieldId]['groupTitle']; $this->_customGroupId = $customGroupDetails[$fieldId]['groupID']; if ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE) { //record exists check foreach ($this->_multiRecordFields as $key => $field) { $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); } $getValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); if (array_key_exists($this->_recordId, $getValues)) { $this->_recordExists = TRUE; } else { $this->_recordExists = FALSE; if ($this->_multiRecord & CRM_Core_Action::UPDATE) { CRM_Core_Session::setStatus(ts('Note: The record %1 doesnot exists. Upon save a new record will be create', array(1 => $this->_recordId)), ts('Record doesnot exist'), 'alert'); } } } if ($this->_multiRecord & CRM_Core_Action::ADD) { $this->_maxRecordLimit = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupDetails[$fieldId]['groupID'], $this->_id); if ($this->_maxRecordLimit) { CRM_Core_Session::setStatus(ts('You cannot add a new record as maximum allowed limit is reached'), ts('Sorry'), 'error'); } } } elseif (!empty($this->_multiRecordFields) && (!$this->_multiRecord || !in_array($this->_multiRecord, array(CRM_Core_Action::DELETE, CRM_Core_Action::UPDATE)))) { CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header'); //multirecord listing page $multiRecordFieldListing = TRUE; $page = new CRM_Profile_Page_MultipleRecordFieldsListing(); $cs = $this->get('cs'); $page->set('pageCheckSum', $cs); $page->set('contactId', $this->_id); $page->set('profileId', $this->_gid); $page->set('action', CRM_Core_Action::BROWSE); $page->set('multiRecordFieldListing', $multiRecordFieldListing); $page->run(); } } $this->assign('multiRecordFieldListing', $multiRecordFieldListing); // is profile double-opt in? if (!empty($this->_fields['group']) && CRM_Core_BAO_UFGroup::isProfileDoubleOptin()) { $emailField = FALSE; foreach ($this->_fields as $name => $values) { if (substr($name, 0, 6) == 'email-') { $emailField = TRUE; } } if (!$emailField) { $status = ts("Email field should be included in profile if you want to use Group(s) when Profile double-opt in process is enabled."); $this->_session->setStatus($status); } } //transferring all the multi-record custom fields in _fields if ($this->_multiRecord && !empty($this->_multiRecordFields)) { $this->_fields = $this->_multiRecordFields; $this->_multiRecordProfile = TRUE; } elseif ($this->_multiRecord && empty($this->_multiRecordFields)) { CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); } } if (!is_array($this->_fields)) { CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); } }
/** * Format custom field name. * * Combine group and field name to avoid conflict. * * @param array $fields * * @return array */ public function formatCustomFieldName(&$fields) { //CRM-2676, replacing the conflict for same custom field name from different custom group. $fieldIds = $formattedFieldNames = array(); foreach ($fields as $key => $value) { if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { $fieldIds[] = $customFieldId; } } if (!empty($fieldIds) && is_array($fieldIds)) { $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldIds); if (!empty($groupTitles)) { foreach ($groupTitles as $fId => $values) { $key = "custom_{$fId}"; $groupTitle = $values['groupTitle']; $formattedFieldNames[$key] = $fields[$key] . ' :: ' . $groupTitle; } } } return $formattedFieldNames; }
/** * Test getGroupTitles() */ public function testgetGroupTitles() { $customGrouptitle = 'Test Custom Group'; $groupParams = array('title' => $customGrouptitle, 'name' => 'test_custom_group', 'style' => 'Tab', 'extends' => 'Individual', 'weight' => 10, 'is_active' => 1, 'version' => 3); $customGroup = Custom::createGroup($groupParams); $customGroupId = $customGroup->id; $customFieldLabel = 'Test Custom Field'; $fieldParams = array('custom_group_id' => $customGroupId, 'label' => $customFieldLabel, 'html_type' => 'Text', 'data_type' => 'String', 'is_required' => 1, 'is_searchable' => 0, 'is_active' => 1, 'version' => 3); $customField = Custom::createField($fieldParams); $customFieldId = $customField->id; $params = array($customFieldId); $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params); $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Custom Group', 'Check Group Title.'); Custom::deleteGroup($customGroup); }
/** * Test getGroupTitles() */ public function testGetGroupTitles() { $groupParams = array('title' => 'Test Group', 'name' => 'test_custom_group', 'style' => 'Tab', 'extends' => 'Individual', 'weight' => 10, 'is_active' => 1); $customGroup = $this->customGroupCreate($groupParams); $fieldParams = array('label' => 'Custom Field', 'html_type' => 'Text', 'data_type' => 'String', 'is_required' => 1, 'is_searchable' => 0, 'is_active' => 1, 'custom_group_id' => $customGroup['id']); $customField = $this->customFieldCreate($fieldParams); $customFieldId = $customField['id']; $params = array($customFieldId); $groupTitles = CRM_Core_BAO_CustomGroup::getGroupTitles($params); $this->assertEquals($groupTitles[$customFieldId]['groupTitle'], 'Test Group', 'Check Group Title.'); $this->customGroupDelete($customGroup['id']); }
/** * Browse the listing. * * @return void */ public function browse() { $dateFields = NULL; $cgcount = 0; $attributes = array(); $dateFieldsVals = NULL; if ($this->_pageViewType == 'profileDataView' && $this->_profileId) { $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::EDIT); $multiRecordFields = array(); $fieldIDs = NULL; $result = NULL; $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE); $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields)); $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id'); $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); if (!$reached) { $this->assign('contactId', $this->_contactId); $this->assign('gid', $this->_profileId); } $this->assign('reachedMax', $reached); if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) { $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing); } } elseif ($this->_pageViewType == 'customDataView') { // require custom group id for _pageViewType of customDataView $customGroupId = $this->_customGroupId; $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); if (!$reached) { $this->assign('contactId', $this->_contactId); $this->assign('customGroupId', $customGroupId); $this->assign('ctype', $this->_contactType); } $this->assign('reachedMax', $reached); // custom group info : this consists of the field title of group fields $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE); // field ids of fields in_selector for the custom group id provided $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']); // field labels for headers $fieldLabels = $groupDetail[$customGroupId]['fields']; // from the above customGroupInfo we can get $this->_customGroupTitle $this->_customGroupTitle = $groupDetail[$customGroupId]['title']; } if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) { $options = array(); $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format', 'default_value', 'is_required'); foreach ($fieldIDs as $key => $fieldID) { $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID; $param = array('id' => $fieldIDs[$key]); $returnValues = array(); CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities); if ($returnValues['data_type'] == 'Date') { $dateFields[$fieldIDs[$key]] = 1; $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats(); $dateFormat = (array) CRM_Utils_Array::value($returnValues['date_format'], $actualPHPFormats); $timeFormat = CRM_Utils_Array::value('time_format', $returnValues); } $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]); if (!empty($optionValuePairs)) { foreach ($optionValuePairs as $optionPairs) { $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label']; } } $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type']; $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type']; $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']); $options[$fieldIDs[$key]]['attributes']['default_value'] = CRM_Utils_Array::value('default_value', $returnValues); $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues); $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues); } // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView) $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE); if ($this->_pageViewType == 'profileDataView') { if (!empty($fieldIDs)) { //get the group info of multi rec fields in listing view $fieldInput = $fieldIDs; $fieldIdInput = $fieldIDs[0]; } else { //if no listing fields exist, take the group title for display $nonListingFieldIds = array_keys($multiRecordFields); $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]); $fieldIdInput = $singleField; $singleField = array($singleField); $fieldInput = $singleField; } $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput); $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle']; } // $cgcount is defined before 'if' condition as enitiy may have no record // and $cgcount is used to build new record url $cgcount = 1; if ($result && !empty($result)) { $links = self::links(); if ($this->_pageViewType == 'profileDataView') { $pageCheckSum = $this->get('pageCheckSum'); if ($pageCheckSum) { foreach ($links as $key => $link) { $links[$key] = $link['qs'] . "&cs=%%cs%%"; } } } $linkAction = array_sum(array_keys($this->links())); if ($reached) { unset($links[CRM_Core_Action::COPY]); } $newCgCount = !$reached ? count($result) + 1 : NULL; foreach ($result as $recId => &$value) { foreach ($value as $fieldId => &$val) { if (is_numeric($fieldId)) { $customValue =& $val; if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) { // formated date capture value capture $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); //set date and time format switch ($timeFormat) { case 1: $dateFormat[1] = 'g:iA'; break; case 2: $dateFormat[1] = 'G:i'; break; default: // if time is not selected remove time from value $result[$recId][$fieldId] = substr($result[$recId][$fieldId], 0, 10); } $result[$recId][$fieldId] = CRM_Utils_Date::processDate($result[$recId][$fieldId], NULL, FALSE, implode(" ", $dateFormat)); } else { // assign to $result $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); } // FIXME: getDisplayValue should always return a string so why is this necessary? if (!$customValue && $customValue !== '0') { $customValue = ""; } // Set field attributes to support crmEditable // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable $fieldAttributes = array('class' => "crmf-custom_{$fieldId}_{$recId}"); $editable = FALSE; if ($linkAction & CRM_Core_Action::UPDATE) { $spec = $options[$fieldId]['attributes']; switch ($spec['html_type']) { case 'Text': // Other data types like money would require some extra validation // FIXME: crmEditable currently does not support any validation rules :( $supportedDataTypes = array('Float', 'String', 'Int'); $editable = in_array($spec['data_type'], $supportedDataTypes); break; case 'TextArea': $editable = TRUE; $fieldAttributes['data-type'] = 'textarea'; break; case 'Radio': case 'Select': case 'Select Country': case 'Select State/Province': $editable = TRUE; $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select'; if (!$spec['is_required']) { $fieldAttributes['data-empty-option'] = ts('- none -'); } break; } } if ($editable) { $fieldAttributes['class'] .= ' crm-editable'; } $attributes[$fieldId][$recId] = $fieldAttributes; $op = NULL; if ($this->_pageViewType == 'profileDataView') { $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, 'id' => $this->_contactId); $op = 'profile.multiValue.row'; } else { // different set of url params $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId; $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId; $actionParams['recId'] = $recId; $actionParams['type'] = $this->_contactType; $actionParams['cgcount'] = $cgcount; $actionParams['newCgCount'] = $newCgCount; // DELETE action links $deleteData = array('valueID' => $recId, 'groupID' => $this->_customGroupId, 'contactId' => $this->_contactId, 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue')); $links[CRM_Core_Action::DELETE]['url'] = '#'; $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"'; $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row'; } if (!empty($pageCheckSum)) { $actionParams['cs'] = $pageCheckSum; } $value['action'] = CRM_Core_Action::formLink($links, $linkAction, $actionParams, ts('more'), FALSE, $op, 'customValue', $fieldId); } } $cgcount++; } } } $headers = array(); if (!empty($fieldIDs)) { foreach ($fieldIDs as $fieldID) { $headers[$fieldID] = $this->_pageViewType == 'profileDataView' ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label']; } } $this->assign('dateFields', $dateFields); $this->assign('dateFieldsVals', $dateFieldsVals); $this->assign('cgcount', $cgcount); $this->assign('customGroupTitle', $this->_customGroupTitle); $this->assign('headers', $headers); $this->assign('records', $result); $this->assign('attributes', $attributes); }
/** * Browse the listing * * @return void * @access public */ function browse() { if ($this->_profileId) { $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::EDIT); $multiRecordFields = array(); $fieldIDs = NULL; $result = NULL; $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE); $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields)); $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id'); $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); if (!$reached) { $this->assign('contactId', $this->_contactId); $this->assign('gid', $this->_profileId); } $this->assign('reachedMax', $reached); if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) { $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing); } } if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) { $options = array(); $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format'); foreach ($fieldIDs as $key => $fieldID) { $fieldIDs[$key] = CRM_Core_BAO_CustomField::getKeyID($fieldID); $param = array('id' => $fieldIDs[$key]); $returnValues = array(); CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities); $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]); if (!empty($optionValuePairs)) { foreach ($optionValuePairs as $optionPairs) { $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label']; } } $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type']; $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type']; $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues); $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues); } $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE); if (!empty($fieldIDs)) { //get the group info of multi rec fields in listing view $fieldInput = $fieldIDs; $fieldIdInput = $fieldIDs[0]; } else { //if no listing fields exist, take the group title for display $nonListingFieldIds = array_keys($multiRecordFields); $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]); $fieldIdInput = $singleField; $singleField = array($singleField); $fieldInput = $singleField; } $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput); $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle']; if ($result && !empty($result)) { $links = self::links(); $pageCheckSum = $this->get('pageCheckSum'); if ($pageCheckSum) { foreach ($links as $key => $link) { $links[$key] = $link['qs'] . "&cs=%%cs%%"; } } $linkAction = array_sum(array_keys($this->links())); foreach ($result as $recId => &$value) { foreach ($value as $fieldId => &$val) { if (is_numeric($fieldId)) { $customValue =& $val; $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); if (!$customValue) { $customValue = ""; } $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, 'id' => $this->_contactId, 'onPopupClose' => $this->_onPopupClose); if ($pageCheckSum) { $actionParams['cs'] = $pageCheckSum; } $value['action'] = CRM_Core_Action::formLink($links, $linkAction, $actionParams, ts('more'), FALSE, 'profile.multiValue.row', 'customValue', $fieldId); } } } } } $headers = array(); if (!empty($fieldIDs)) { foreach ($fieldIDs as $fieldID) { $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel']; } } $this->assign('customGroupTitle', $this->_customGroupTitle); $this->assign('headers', $headers); $this->assign('records', $result); }
/** * Browse the listing * * @return void * @access public */ function browse() { $dateFields = NULL; $cgcount = 0; $dateFieldsVals = NULL; if ($this->_pageViewType == 'profileDataView' && $this->_profileId) { $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::EDIT); $multiRecordFields = array(); $fieldIDs = NULL; $result = NULL; $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE); $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields)); $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id'); $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); if (!$reached) { $this->assign('contactId', $this->_contactId); $this->assign('gid', $this->_profileId); } $this->assign('reachedMax', $reached); if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) { $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing); } } elseif ($this->_pageViewType == 'customDataView') { // require custom group id for _pageViewType of customDataView $customGroupId = $this->_customGroupId; $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId); if (!$reached) { $this->assign('contactId', $this->_contactId); $this->assign('customGroupId', $customGroupId); $this->assign('ctype', $this->_contactType); } $this->assign('reachedMax', $reached); // custom group info : this consists of the field title of group fields $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE); // field ids of fields in_selector for the custom group id provided $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']); // field labels for headers $fieldLabels = $groupDetail[$customGroupId]['fields']; // from the above customGroupInfo we can get $this->_customGroupTitle $this->_customGroupTitle = $groupDetail[$customGroupId]['title']; } if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) { $options = array(); $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format'); foreach ($fieldIDs as $key => $fieldID) { $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID; $param = array('id' => $fieldIDs[$key]); $returnValues = array(); CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities); if ($returnValues['data_type'] == 'Date') { $dateFields[$fieldIDs[$key]] = 1; } $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]); if (!empty($optionValuePairs)) { foreach ($optionValuePairs as $optionPairs) { $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label']; } } $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type']; $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type']; $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues); $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues); } // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView) $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE); if ($this->_pageViewType == 'profileDataView') { if (!empty($fieldIDs)) { //get the group info of multi rec fields in listing view $fieldInput = $fieldIDs; $fieldIdInput = $fieldIDs[0]; } else { //if no listing fields exist, take the group title for display $nonListingFieldIds = array_keys($multiRecordFields); $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]); $fieldIdInput = $singleField; $singleField = array($singleField); $fieldInput = $singleField; } $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput); $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle']; } // $cgcount is defined before 'if' condition as enitiy may have no record // and $cgcount is used to build new record url $cgcount = 1; if ($result && !empty($result)) { $links = self::links(); if ($this->_pageViewType == 'profileDataView') { $pageCheckSum = $this->get('pageCheckSum'); if ($pageCheckSum) { foreach ($links as $key => $link) { $links[$key] = $link['qs'] . "&cs=%%cs%%"; } } } $linkAction = array_sum(array_keys($this->links())); if ($reached) { unset($links[CRM_Core_Action::COPY]); } $newCgCount = !$reached ? count($result) + 1 : NULL; foreach ($result as $recId => &$value) { foreach ($value as $fieldId => &$val) { if (is_numeric($fieldId)) { $customValue =& $val; if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) { // formated date capture value capture $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); } else { // assign to $result $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options); } if (!$customValue) { $customValue = ""; } $op = NULL; if ($this->_pageViewType == 'profileDataView') { $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, 'id' => $this->_contactId); $op = 'profile.multiValue.row'; } else { // different set of url params $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId; $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId; $actionParams['recId'] = $recId; $actionParams['type'] = $this->_contactType; $actionParams['cgcount'] = $cgcount; $actionParams['newCgCount'] = $newCgCount; // DELETE action links $deleteData = array('valueID' => $recId, 'groupID' => $this->_customGroupId, 'contactId' => $this->_contactId, 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue')); $links[CRM_Core_Action::DELETE]['url'] = '#'; $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"'; $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row'; } if (!empty($pageCheckSum)) { $actionParams['cs'] = $pageCheckSum; } $value['action'] = CRM_Core_Action::formLink($links, $linkAction, $actionParams, ts('more'), FALSE, $op, 'customValue', $fieldId); } } $cgcount++; } } } $headers = array(); if (!empty($fieldIDs)) { foreach ($fieldIDs as $fieldID) { $headers[$fieldID] = $this->_pageViewType == 'profileDataView' ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label']; } } $this->assign('dateFields', $dateFields); $this->assign('dateFieldsVals', $dateFieldsVals); $this->assign('cgcount', $cgcount); $this->assign('customGroupTitle', $this->_customGroupTitle); $this->assign('headers', $headers); $this->assign('records', $result); }