예제 #1
0
 public function testGetDisplayedValues()
 {
     $customGroup = Custom::createGroup(array(), 'Individual');
     $fieldsToCreate = array(array('data_type' => 'Country', 'html_type' => 'Select Country', 'tests' => array('UNITED STATES' => 1228, '' => NULL)), array('data_type' => 'StateProvince', 'html_type' => 'Multi-Select State/Province', 'tests' => array('' => 0, 'Alabama' => 1000, 'Alabama, Alaska' => array(1000, 1001))), array('data_type' => 'String', 'html_type' => 'Radio', 'option_values' => array('key' => 'KeyLabel'), 'tests' => array('KeyLabel' => 'key')), array('data_type' => 'String', 'html_type' => 'CheckBox', 'option_values' => array('key1' => 'Label1', 'key2' => 'Label2', 'key3' => 'Label3', 'key4' => 'Label4'), 'tests' => array('Label1' => array('key1'), 'Label2' => 'key2', 'Label2, Label3' => array('key2', 'key3'), 'Label3, Label4' => CRM_Utils_Array::implodePadded(array('key3', 'key4')), 'Label1, Label4' => array('key1' => 1, 'key4' => 1))), array('data_type' => 'Date', 'html_type' => 'Select Date', 'date_format' => 'd M yy', 'time_format' => 1, 'tests' => array('1 Jun 1999 1:30PM' => '1999-06-01 13:30', '' => '')));
     foreach ($fieldsToCreate as $num => $field) {
         $params = $field + array('label' => 'test field ' . $num, 'custom_group_id' => $customGroup->id);
         unset($params['tests']);
         $createdField = $this->callAPISuccess('customField', 'create', $params);
         foreach ($field['tests'] as $expected => $input) {
             $this->assertEquals($expected, CRM_Core_BAO_CustomField::displayValue($input, $createdField['id']));
         }
     }
     Custom::deleteGroup($customGroup);
 }
예제 #2
0
 /**
  * Build custom data view.
  *
  * @param CRM_Core_Form $form
  *   Page object.
  * @param array $groupTree
  * @param bool $returnCount
  *   True if customValue count needs to be returned.
  * @param int $gID
  * @param null $prefix
  * @param int $customValueId
  * @param int $entityId
  *
  * @return array|int
  */
 public static function buildCustomDataView(&$form, &$groupTree, $returnCount = FALSE, $gID = NULL, $prefix = NULL, $customValueId = NULL, $entityId = NULL)
 {
     $details = array();
     foreach ($groupTree as $key => $group) {
         if ($key === 'info') {
             continue;
         }
         foreach ($group['fields'] as $k => $properties) {
             $groupID = $group['id'];
             if (!empty($properties['customValue'])) {
                 foreach ($properties['customValue'] as $values) {
                     if (!empty($customValueId) && $customValueId != $values['id']) {
                         continue;
                     }
                     $details[$groupID][$values['id']]['title'] = CRM_Utils_Array::value('title', $group);
                     $details[$groupID][$values['id']]['name'] = CRM_Utils_Array::value('name', $group);
                     $details[$groupID][$values['id']]['help_pre'] = CRM_Utils_Array::value('help_pre', $group);
                     $details[$groupID][$values['id']]['help_post'] = CRM_Utils_Array::value('help_post', $group);
                     $details[$groupID][$values['id']]['collapse_display'] = CRM_Utils_Array::value('collapse_display', $group);
                     $details[$groupID][$values['id']]['collapse_adv_display'] = CRM_Utils_Array::value('collapse_adv_display', $group);
                     $details[$groupID][$values['id']]['fields'][$k] = array('field_title' => CRM_Utils_Array::value('label', $properties), 'field_type' => CRM_Utils_Array::value('html_type', $properties), 'field_data_type' => CRM_Utils_Array::value('data_type', $properties), 'field_value' => CRM_Core_BAO_CustomField::displayValue($values['data'], $properties['id'], $entityId), 'options_per_line' => CRM_Utils_Array::value('options_per_line', $properties));
                     // also return contact reference contact id if user has view all or edit all contacts perm
                     if ((CRM_Core_Permission::check('view all contacts') || CRM_Core_Permission::check('edit all contacts')) && $details[$groupID][$values['id']]['fields'][$k]['field_data_type'] == 'ContactReference') {
                         $details[$groupID][$values['id']]['fields'][$k]['contact_ref_id'] = CRM_Utils_Array::value('data', $values);
                     }
                 }
             } else {
                 $details[$groupID][0]['title'] = CRM_Utils_Array::value('title', $group);
                 $details[$groupID][0]['name'] = CRM_Utils_Array::value('name', $group);
                 $details[$groupID][0]['help_pre'] = CRM_Utils_Array::value('help_pre', $group);
                 $details[$groupID][0]['help_post'] = CRM_Utils_Array::value('help_post', $group);
                 $details[$groupID][0]['collapse_display'] = CRM_Utils_Array::value('collapse_display', $group);
                 $details[$groupID][0]['collapse_adv_display'] = CRM_Utils_Array::value('collapse_adv_display', $group);
                 $details[$groupID][0]['fields'][$k] = array('field_title' => CRM_Utils_Array::value('label', $properties));
             }
         }
     }
     if ($returnCount) {
         //return a single value count if group id is passed to function
         //else return a groupId and count mapped array
         if (!empty($gID)) {
             return count($details[$gID]);
         } else {
             $countValue = array();
             foreach ($details as $key => $value) {
                 $countValue[$key] = count($details[$key]);
             }
             return $countValue;
         }
     } else {
         $form->assign_by_ref("{$prefix}viewCustomData", $details);
         return $details;
     }
 }
예제 #3
0
 /**
  * Returns all the rows in the given offset and rowCount.
  *
  * @param string $action
  *   The action being performed.
  * @param int $offset
  *   The row number to start from.
  * @param int $rowCount
  *   The number of rows to return.
  * @param string $sort
  *   The sql string that describes the sort order.
  * @param string $output
  *   What should the result set include (web/email/csv).
  *
  * @param string $extraWhereClause
  *
  * @return int
  *   the total number of rows for this action
  */
 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL, $extraWhereClause = NULL)
 {
     $multipleFields = array('url');
     //$sort object processing for location fields
     if ($sort) {
         $vars = $sort->_vars;
         $varArray = array();
         foreach ($vars as $key => $field) {
             $field = $vars[$key];
             $fieldArray = explode('-', $field['name']);
             $fieldType = CRM_Utils_Array::value('2', $fieldArray);
             if (is_numeric(CRM_Utils_Array::value('1', $fieldArray))) {
                 if (!in_array($fieldType, $multipleFields)) {
                     $locationType = new CRM_Core_DAO_LocationType();
                     $locationType->id = $fieldArray[1];
                     $locationType->find(TRUE);
                     if ($fieldArray[0] == 'email' || $fieldArray[0] == 'im' || $fieldArray[0] == 'phone') {
                         $field['name'] = "`" . $locationType->name . "-" . $fieldArray[0] . "-1`";
                     } else {
                         $field['name'] = "`" . $locationType->name . "-" . $fieldArray[0] . "`";
                     }
                 } else {
                     $field['name'] = "`website-" . $fieldArray[1] . "-{$fieldType}`";
                 }
             }
             $varArray[$key] = $field;
         }
         $sort->_vars = $varArray;
     }
     $additionalWhereClause = 'contact_a.is_deleted = 0';
     if ($extraWhereClause) {
         $additionalWhereClause .= " AND {$extraWhereClause}";
     }
     $returnQuery = NULL;
     if ($this->_multiRecordTableName) {
         $returnQuery = TRUE;
     }
     $this->_query->_useGroupBy = TRUE;
     $result = $this->_query->searchQuery($offset, $rowCount, $sort, NULL, NULL, NULL, NULL, $returnQuery, $additionalWhereClause);
     if ($returnQuery) {
         $resQuery = preg_replace('/GROUP BY contact_a.id[\\s]+ORDER BY/', ' ORDER BY', $result);
         $result = CRM_Core_DAO::executeQuery($resQuery);
     }
     // process the result of the query
     $rows = array();
     // check if edit is configured in profile settings
     if ($this->_gid) {
         $editLink = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_edit_link');
     }
     //FIXME : make sure to handle delete separately. CRM-4418
     $mask = CRM_Core_Action::mask(array(CRM_Core_Permission::getPermission()));
     if ($editLink && $mask & CRM_Core_Permission::EDIT) {
         // do not allow edit for anon users in joomla frontend, CRM-4668
         $config = CRM_Core_Config::singleton();
         if (!$config->userFrameworkFrontend) {
             $this->_editLink = TRUE;
         }
     }
     $links = self::links($this->_map, $this->_editLink, $this->_linkToUF, $this->_profileIds);
     $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
     $names = array();
     static $skipFields = array('group', 'tag');
     foreach ($this->_fields as $key => $field) {
         // skip pseudo fields
         if (substr($key, 0, 9) == 'phone_ext') {
             continue;
         }
         if (!empty($field['in_selector']) && !in_array($key, $skipFields)) {
             if (strpos($key, '-') !== FALSE) {
                 $value = explode('-', $key);
                 $fieldName = CRM_Utils_Array::value(0, $value);
                 $id = CRM_Utils_Array::value(1, $value);
                 $type = CRM_Utils_Array::value(2, $value);
                 if (!in_array($fieldName, $multipleFields)) {
                     $locationTypeName = NULL;
                     if (is_numeric($id)) {
                         $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
                     } else {
                         if ($id == 'Primary') {
                             $locationTypeName = 1;
                         }
                     }
                     if (!$locationTypeName) {
                         continue;
                     }
                     $locationTypeName = str_replace(' ', '_', $locationTypeName);
                     if (in_array($fieldName, array('phone', 'im', 'email'))) {
                         if ($type) {
                             $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
                         } else {
                             $names[] = "{$locationTypeName}-{$fieldName}";
                         }
                     } else {
                         $names[] = "{$locationTypeName}-{$fieldName}";
                     }
                 } else {
                     $names[] = "website-{$id}-{$fieldName}";
                 }
             } elseif ($field['name'] == 'id') {
                 $names[] = 'contact_id';
             } else {
                 $names[] = $field['name'];
             }
         }
     }
     $multipleSelectFields = array('preferred_communication_method' => 1);
     $multiRecordTableId = NULL;
     if ($this->_multiRecordTableName) {
         $multiRecordTableId = "{$this->_multiRecordTableName}_id";
     }
     // we need to determine of overlay profile should be shown
     $showProfileOverlay = CRM_Core_BAO_UFGroup::showOverlayProfile();
     while ($result->fetch()) {
         $this->_query->convertToPseudoNames($result);
         if (isset($result->country)) {
             // the query returns the untranslated country name
             $i18n = CRM_Core_I18n::singleton();
             $result->country = $i18n->translate($result->country);
         }
         $row = array();
         $empty = TRUE;
         $row[] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id, $showProfileOverlay);
         if ($result->sort_name) {
             $row[] = $result->sort_name;
             $empty = FALSE;
         } else {
             continue;
         }
         foreach ($names as $name) {
             if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) {
                 $row[] = CRM_Core_BAO_CustomField::displayValue($result->{$name}, $cfID, $result->contact_id);
             } elseif (substr($name, -4) == '-url' && !empty($result->{$name})) {
                 $url = CRM_Utils_System::fixURL($result->{$name});
                 $typeId = substr($name, 0, -4) . "-website_type_id";
                 $typeName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Website', 'website_type_id', $result->{$typeId});
                 if ($typeName) {
                     $row[] = "<a href=\"{$url}\">{$result->{$name}} ({$typeName})</a>";
                 } else {
                     $row[] = "<a href=\"{$url}\">{$result->{$name}}</a>";
                 }
             } elseif ($name == 'preferred_language') {
                 $row[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $result->{$name});
             } elseif ($multipleSelectFields && array_key_exists($name, $multipleSelectFields)) {
                 $paramsNew = array($name => $result->{$name});
                 $name = array($name => array('newName' => $name, 'groupName' => $name));
                 CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE);
                 $row[] = $paramsNew[$key];
             } elseif (strpos($name, '-im')) {
                 if (!empty($result->{$name})) {
                     $providerId = $name . "-provider_id";
                     $providerName = CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_IM', 'provider_id', $result->{$providerId});
                     $row[] = $result->{$name} . " ({$providerName})";
                 } else {
                     $row[] = '';
                 }
             } elseif (strpos($name, '-phone-')) {
                 $phoneExtField = str_replace('phone', 'phone_ext', $name);
                 if (isset($result->{$phoneExtField})) {
                     $row[] = $result->{$name} . " (" . $result->{$phoneExtField} . ")";
                 } else {
                     $row[] = $result->{$name};
                 }
             } elseif (in_array($name, array('addressee', 'email_greeting', 'postal_greeting'))) {
                 $dname = $name . '_display';
                 $row[] = $result->{$dname};
             } elseif (in_array($name, array('birth_date', 'deceased_date'))) {
                 $row[] = CRM_Utils_Date::customFormat($result->{$name});
             } elseif (isset($result->{$name})) {
                 $row[] = $result->{$name};
             } else {
                 $row[] = '';
             }
             if (!empty($result->{$name})) {
                 $empty = FALSE;
             }
         }
         $newLinks = $links;
         $params = array('id' => $result->contact_id, 'gid' => implode(',', $this->_profileIds));
         // pass record id param to view url for multi record view
         if ($multiRecordTableId && $newLinks) {
             if ($result->{$multiRecordTableId}) {
                 if ($newLinks[CRM_Core_Action::VIEW]['url'] == 'civicrm/profile/view') {
                     $newLinks[CRM_Core_Action::VIEW]['qs'] .= "&multiRecord=view&recordId=%%recordId%%&allFields=1";
                     $params['recordId'] = $result->{$multiRecordTableId};
                 }
             }
         }
         if ($this->_linkToUF) {
             $ufID = CRM_Core_BAO_UFMatch::getUFId($result->contact_id);
             if (!$ufID) {
                 unset($newLinks[CRM_Core_Action::PROFILE]);
             } else {
                 $params['ufID'] = $ufID;
             }
         }
         $row[] = CRM_Core_Action::formLink($newLinks, $mask, $params, ts('more'), FALSE, 'profile.selector.row', 'Contact', $result->contact_id);
         if (!$empty) {
             $rows[] = $row;
         }
     }
     return $rows;
 }
예제 #4
0
    /**
     * @param $rows
     */
    private function _formatSurveyResponseData(&$rows)
    {
        $surveyIds = CRM_Utils_Array::value('survey_id_value', $this->_params);
        if (CRM_Utils_System::isNull($surveyIds) || empty($this->_params['fields']['survey_response'])) {
            return;
        }
        $surveyResponseFields = array();
        $surveyResponseFieldIds = array();
        foreach ($this->_columns as $tableName => $values) {
            if (!is_array($values['fields'])) {
                continue;
            }
            foreach ($values['fields'] as $name => $field) {
                if (!empty($field['isSurveyResponseField'])) {
                    $fldId = substr($name, 7);
                    $surveyResponseFields[$name] = "{$tableName}_{$name}";
                    $surveyResponseFieldIds[$fldId] = $fldId;
                }
            }
        }
        if (empty($surveyResponseFieldIds)) {
            return;
        }
        $hasResponseData = FALSE;
        foreach ($surveyResponseFields as $fldName) {
            foreach ($rows as $row) {
                if (!empty($row[$fldName])) {
                    $hasResponseData = TRUE;
                    break;
                }
            }
        }
        //do check respondent status.
        $statusId = CRM_Utils_Array::value('status_id_value', $this->_params);
        $respondentStatus = CRM_Utils_Array::value($statusId, self::$_surveyRespondentStatus);
        if (!$hasResponseData && $respondentStatus != 'Reserved') {
            return;
        }
        //start response data formatting.
        $query = '
    SELECT  cf.id,
            cf.data_type,
            cf.html_type,
            cg.table_name,
            cf.column_name,
            ov.value, ov.label,
            cf.option_group_id
      FROM  civicrm_custom_field cf
INNER JOIN  civicrm_custom_group cg ON ( cg.id = cf.custom_group_id )
 LEFT JOIN  civicrm_option_value ov ON ( cf.option_group_id = ov.option_group_id )
     WHERE  cf.id IN ( ' . implode(' , ', $surveyResponseFieldIds) . ' )
  Order By  ov.weight';
        $responseFields = array();
        $fieldValueMap = array();
        $properties = array('id', 'data_type', 'html_type', 'column_name', 'option_group_id');
        $responseField = CRM_Core_DAO::executeQuery($query);
        while ($responseField->fetch()) {
            $reponseFldName = $responseField->table_name . '_custom_' . $responseField->id;
            foreach ($properties as $prop) {
                $responseFields[$reponseFldName][$prop] = $responseField->{$prop};
            }
            if ($responseField->option_group_id) {
                //show value for print and pdf.
                $value = $responseField->label;
                if (in_array($this->_outputMode, array('print', 'pdf'))) {
                    $value = $responseField->value;
                }
                $fieldValueMap[$responseField->option_group_id][$responseField->value] = $value;
            }
        }
        $responseField->free();
        //actual data formatting.
        $hasData = FALSE;
        foreach ($rows as &$row) {
            if (!is_array($row)) {
                continue;
            }
            foreach ($row as $name => &$value) {
                if (!array_key_exists($name, $responseFields)) {
                    continue;
                }
                $hasData = TRUE;
                if ($respondentStatus == 'Reserved' && in_array($this->_outputMode, array('print', 'pdf'))) {
                    $optGrpId = CRM_Utils_Array::value('option_group_id', $responseFields[$name]);
                    $options = CRM_Utils_Array::value($optGrpId, $fieldValueMap, array());
                    $value = implode(' | ', array_keys($options));
                } else {
                    $value = CRM_Core_BAO_CustomField::displayValue($value, $responseFields[$name]['id']);
                }
            }
            if (!$hasData) {
                break;
            }
        }
    }
예제 #5
0
파일: Merger.php 프로젝트: agroknow/mermix
 /**
  * Based on the provided two contact_ids and a set of tables, move the belongings of the
  * other contact to the main one - be it Location / CustomFields or Contact .. related info.
  * A superset of moveContactBelongings() function.
  *
  * @param int $mainId
  *   Main contact with whom merge has to happen.
  * @param int $otherId
  *   Duplicate contact which would be deleted after merge operation.
  *
  * @param $migrationInfo
  *
  * @return bool
  */
 public static function moveAllBelongings($mainId, $otherId, $migrationInfo)
 {
     if (empty($migrationInfo)) {
         return FALSE;
     }
     $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
     $relTables = CRM_Dedupe_Merger::relTables();
     $moveTables = $locBlocks = $tableOperations = array();
     foreach ($migrationInfo as $key => $value) {
         if ($value == $qfZeroBug) {
             $value = '0';
         }
         if ((in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) || substr($key, 0, 12) == 'move_custom_') && $value != NULL) {
             $submitted[substr($key, 5)] = $value;
         } elseif (substr($key, 0, 14) == 'move_location_' and $value != NULL) {
             $locField = explode('_', $key);
             $fieldName = $locField[2];
             $fieldCount = $locField[3];
             // Set up the operation type (add/overwrite)
             // Ignore operation for websites
             // @todo Tidy this up
             $operation = 0;
             if ($fieldName != 'website') {
                 $operation = CRM_Utils_Array::value('operation', $migrationInfo['location'][$fieldName][$fieldCount]);
             }
             // default operation is overwrite.
             if (!$operation) {
                 $operation = 2;
             }
             $locBlocks[$fieldName][$fieldCount]['operation'] = $operation;
         } elseif (substr($key, 0, 15) == 'move_rel_table_' and $value == '1') {
             $moveTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']);
             if (array_key_exists('operation', $migrationInfo)) {
                 foreach ($relTables[substr($key, 5)]['tables'] as $table) {
                     if (array_key_exists($key, $migrationInfo['operation'])) {
                         $tableOperations[$table] = $migrationInfo['operation'][$key];
                     }
                 }
             }
         }
     }
     // **** Do location related migration.
     // @todo Handle OpenID (not currently in API).
     if (!empty($locBlocks)) {
         $locationBlocks = self::getLocationBlockInfo();
         $primaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mainId, array('is_primary' => 1));
         $billingBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mainId, array('is_billing' => 1));
         foreach ($locBlocks as $name => $block) {
             if (!is_array($block) || CRM_Utils_System::isNull($block)) {
                 continue;
             }
             $daoName = 'CRM_Core_DAO_' . $locationBlocks[$name]['label'];
             $primaryDAOId = array_key_exists($name, $primaryBlockIds) ? array_pop($primaryBlockIds[$name]) : NULL;
             $billingDAOId = array_key_exists($name, $billingBlockIds) ? array_pop($billingBlockIds[$name]) : NULL;
             foreach ($block as $blkCount => $values) {
                 // For the block which belongs to other-contact, link the location block to main-contact
                 $otherBlockDAO = new $daoName();
                 $otherBlockDAO->contact_id = $mainId;
                 // Get the ID of this block on the 'other' contact, otherwise skip
                 $otherBlockId = CRM_Utils_Array::value('id', $migrationInfo['other_details']['location_blocks'][$name][$blkCount]);
                 if (!$otherBlockId) {
                     continue;
                 }
                 $otherBlockDAO->id = $otherBlockId;
                 // Add/update location and type information from the form, if applicable
                 if ($locationBlocks[$name]['hasLocation']) {
                     $locTypeId = CRM_Utils_Array::value('locTypeId', $migrationInfo['location'][$name][$blkCount]);
                     $otherBlockDAO->location_type_id = $locTypeId;
                 }
                 if ($locationBlocks[$name]['hasType']) {
                     $typeTypeId = CRM_Utils_Array::value('typeTypeId', $migrationInfo['location'][$name][$blkCount]);
                     $otherBlockDAO->{$locationBlocks[$name]['hasType']} = $typeTypeId;
                 }
                 // Get main block ID
                 $mainBlockId = CRM_Utils_Array::value('mainContactBlockId', $migrationInfo['location'][$name][$blkCount], 0);
                 // if main contact already has primary & billing, set the flags to 0.
                 if ($primaryDAOId) {
                     $otherBlockDAO->is_primary = 0;
                 }
                 if ($billingDAOId) {
                     $otherBlockDAO->is_billing = 0;
                 }
                 $operation = CRM_Utils_Array::value('operation', $values, 2);
                 // overwrite - need to delete block which belongs to main-contact.
                 if (!empty($mainBlockId) && $operation == 2) {
                     $deleteDAO = new $daoName();
                     $deleteDAO->id = $mainBlockId;
                     $deleteDAO->find(TRUE);
                     // if we about to delete a primary / billing block, set the flags for new block
                     // that we going to assign to main-contact
                     if ($primaryDAOId && $primaryDAOId == $deleteDAO->id) {
                         $otherBlockDAO->is_primary = 1;
                     }
                     if ($billingDAOId && $billingDAOId == $deleteDAO->id) {
                         $otherBlockDAO->is_billing = 1;
                     }
                     $deleteDAO->delete();
                     $deleteDAO->free();
                 }
                 $otherBlockDAO->update();
                 $otherBlockDAO->free();
             }
         }
     }
     // **** Do tables related migrations
     if (!empty($moveTables)) {
         CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId, $moveTables, $tableOperations);
         unset($moveTables, $tableOperations);
     }
     // **** Do contact related migrations
     CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId);
     // FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact()
     $names['gender'] = array('newName' => 'gender_id', 'groupName' => 'gender');
     $names['individual_prefix'] = array('newName' => 'prefix_id', 'groupName' => 'individual_prefix');
     $names['individual_suffix'] = array('newName' => 'suffix_id', 'groupName' => 'individual_suffix');
     $names['communication_style'] = array('newName' => 'communication_style_id', 'groupName' => 'communication_style');
     $names['addressee'] = array('newName' => 'addressee_id', 'groupName' => 'addressee');
     $names['email_greeting'] = array('newName' => 'email_greeting_id', 'groupName' => 'email_greeting');
     $names['postal_greeting'] = array('newName' => 'postal_greeting_id', 'groupName' => 'postal_greeting');
     CRM_Core_OptionGroup::lookupValues($submitted, $names, TRUE);
     // fix custom fields so they're edible by createProfileContact()
     static $treeCache = array();
     if (!array_key_exists($migrationInfo['main_details']['contact_type'], $treeCache)) {
         $treeCache[$migrationInfo['main_details']['contact_type']] = CRM_Core_BAO_CustomGroup::getTree($migrationInfo['main_details']['contact_type'], CRM_Core_DAO::$_nullObject, NULL, -1);
     }
     $cgTree =& $treeCache[$migrationInfo['main_details']['contact_type']];
     $cFields = array();
     foreach ($cgTree as $key => $group) {
         if (!isset($group['fields'])) {
             continue;
         }
         foreach ($group['fields'] as $fid => $field) {
             $cFields[$fid]['attributes'] = $field;
         }
     }
     if (!isset($submitted)) {
         $submitted = array();
     }
     foreach ($submitted as $key => $value) {
         if (substr($key, 0, 7) == 'custom_') {
             $fid = (int) substr($key, 7);
             if (empty($cFields[$fid])) {
                 continue;
             }
             $htmlType = $cFields[$fid]['attributes']['html_type'];
             switch ($htmlType) {
                 case 'File':
                     $customFiles[] = $fid;
                     unset($submitted["custom_{$fid}"]);
                     break;
                 case 'Select Country':
                 case 'Select State/Province':
                     $submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
                     break;
                 case 'CheckBox':
                 case 'AdvMulti-Select':
                 case 'Multi-Select':
                 case 'Multi-Select Country':
                 case 'Multi-Select State/Province':
                     // Merge values from both contacts for multivalue fields, CRM-4385
                     // get the existing custom values from db.
                     $customParams = array('entityID' => $mainId, $key => TRUE);
                     $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
                     if (!empty($customfieldValues[$key])) {
                         $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
                         if (is_array($existingValue) && !empty($existingValue)) {
                             $mergeValue = $submmtedCustomValue = array();
                             if ($value) {
                                 $submmtedCustomValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                             }
                             //hack to remove null and duplicate values from array.
                             foreach (array_merge($submmtedCustomValue, $existingValue) as $k => $v) {
                                 if ($v != '' && !in_array($v, $mergeValue)) {
                                     $mergeValue[] = $v;
                                 }
                             }
                             //keep state and country as array format.
                             //for checkbox and m-select format w/ VALUE_SEPARATOR
                             if (in_array($htmlType, array('CheckBox', 'Multi-Select', 'AdvMulti-Select'))) {
                                 $submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $mergeValue) . CRM_Core_DAO::VALUE_SEPARATOR;
                             } else {
                                 $submitted[$key] = $mergeValue;
                             }
                         }
                     } elseif (in_array($htmlType, array('Multi-Select Country', 'Multi-Select State/Province'))) {
                         //we require submitted values should be in array format
                         if ($value) {
                             $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                             //hack to remove null values from array.
                             $mergeValue = array();
                             foreach ($mergeValueArray as $k => $v) {
                                 if ($v != '') {
                                     $mergeValue[] = $v;
                                 }
                             }
                             $submitted[$key] = $mergeValue;
                         }
                     }
                     break;
                 default:
                     break;
             }
         }
     }
     // **** Do file custom fields related migrations
     // FIXME: move this someplace else (one of the BAOs) after discussing
     // where to, and whether CRM_Core_BAO_File::deleteFileReferences() shouldn't actually,
     // like, delete a file...
     if (!isset($customFiles)) {
         $customFiles = array();
     }
     foreach ($customFiles as $customId) {
         list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($customId);
         // get the contact_id -> file_id mapping
         $fileIds = array();
         $sql = "SELECT entity_id, {$columnName} AS file_id FROM {$tableName} WHERE entity_id IN ({$mainId}, {$otherId})";
         $dao = CRM_Core_DAO::executeQuery($sql);
         while ($dao->fetch()) {
             $fileIds[$dao->entity_id] = $dao->file_id;
         }
         $dao->free();
         // delete the main contact's file
         if (!empty($fileIds[$mainId])) {
             CRM_Core_BAO_File::deleteFileReferences($fileIds[$mainId], $mainId, $customId);
         }
         // move the other contact's file to main contact
         //NYSS need to INSERT or UPDATE depending on whether main contact has an existing record
         if (CRM_Core_DAO::singleValueQuery("SELECT id FROM {$tableName} WHERE entity_id = {$mainId}")) {
             $sql = "UPDATE {$tableName} SET {$columnName} = {$fileIds[$otherId]} WHERE entity_id = {$mainId}";
         } else {
             $sql = "INSERT INTO {$tableName} ( entity_id, {$columnName} ) VALUES ( {$mainId}, {$fileIds[$otherId]} )";
         }
         CRM_Core_DAO::executeQuery($sql);
         if (CRM_Core_DAO::singleValueQuery("\n        SELECT id\n        FROM civicrm_entity_file\n        WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$otherId]}")) {
             $sql = "\n          UPDATE civicrm_entity_file\n          SET entity_id = {$mainId}\n          WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$otherId]}";
         } else {
             $sql = "\n          INSERT INTO civicrm_entity_file ( entity_table, entity_id, file_id )\n          VALUES ( '{$tableName}', {$mainId}, {$fileIds[$otherId]} )";
         }
         CRM_Core_DAO::executeQuery($sql);
     }
     // move view only custom fields CRM-5362
     $viewOnlyCustomFields = array();
     foreach ($submitted as $key => $value) {
         $fid = (int) substr($key, 7);
         if (array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view'])) {
             $viewOnlyCustomFields[$key] = $value;
         }
     }
     // special case to set values for view only, CRM-5362
     if (!empty($viewOnlyCustomFields)) {
         $viewOnlyCustomFields['entityID'] = $mainId;
         CRM_Core_BAO_CustomValueTable::setValues($viewOnlyCustomFields);
     }
     if (CRM_Core_Permission::check('merge duplicate contacts') && CRM_Core_Permission::check('delete contacts')) {
         // if ext id is submitted then set it null for contact to be deleted
         if (!empty($submitted['external_identifier'])) {
             $query = "UPDATE civicrm_contact SET external_identifier = null WHERE id = {$otherId}";
             CRM_Core_DAO::executeQuery($query);
         }
         civicrm_api3('contact', 'delete', array('id' => $otherId));
         CRM_Core_BAO_PrevNextCache::deleteItem($otherId);
     }
     // FIXME: else part
     // else {
     //  CRM_Core_Session::setStatus( ts('Do not have sufficient permission to delete duplicate contact.') );
     // }
     // CRM-15681 merge sub_types
     if ($other_sub_types = CRM_Utils_array::value('contact_sub_type', $migrationInfo['other_details'])) {
         if ($main_sub_types = CRM_Utils_array::value('contact_sub_type', $migrationInfo['main_details'])) {
             $submitted['contact_sub_type'] = array_unique(array_merge($main_sub_types, $other_sub_types));
         } else {
             $submitted['contact_sub_type'] = $other_sub_types;
         }
     }
     // **** Update contact related info for the main contact
     if (!empty($submitted)) {
         $submitted['contact_id'] = $mainId;
         //update current employer field
         if ($currentEmloyerId = CRM_Utils_Array::value('current_employer_id', $submitted)) {
             if (!CRM_Utils_System::isNull($currentEmloyerId)) {
                 $submitted['current_employer'] = $submitted['current_employer_id'];
             } else {
                 $submitted['current_employer'] = '';
             }
             unset($submitted['current_employer_id']);
         }
         //CRM-14312 include prefix/suffix from mainId if not overridden for proper construction of display/sort name
         if (!isset($submitted['prefix_id']) && !empty($migrationInfo['main_details']['prefix_id'])) {
             $submitted['prefix_id'] = $migrationInfo['main_details']['prefix_id'];
         }
         if (!isset($submitted['suffix_id']) && !empty($migrationInfo['main_details']['suffix_id'])) {
             $submitted['suffix_id'] = $migrationInfo['main_details']['suffix_id'];
         }
         CRM_Contact_BAO_Contact::createProfileContact($submitted, CRM_Core_DAO::$_nullArray, $mainId);
     }
     CRM_Utils_Hook::post('merge', 'Contact', $mainId, CRM_Core_DAO::$_nullObject);
     self::createMergeActivities($mainId, $otherId);
     return TRUE;
 }
예제 #6
0
 /**
  * Get the list the export fields.
  *
  * @param int $selectAll
  *   User preference while export.
  * @param array $ids
  *   Contact ids.
  * @param array $params
  *   Associated array of fields.
  * @param string $order
  *   Order by clause.
  * @param array $fields
  *   Associated array of fields.
  * @param array $moreReturnProperties
  *   Additional return fields.
  * @param int $exportMode
  *   Export mode.
  * @param string $componentClause
  *   Component clause.
  * @param string $componentTable
  *   Component table.
  * @param bool $mergeSameAddress
  *   Merge records if they have same address.
  * @param bool $mergeSameHousehold
  *   Merge records if they belong to the same household.
  *
  * @param array $exportParams
  * @param string $queryOperator
  *
  */
 public static function exportComponents($selectAll, $ids, $params, $order = NULL, $fields = NULL, $moreReturnProperties = NULL, $exportMode = CRM_Export_Form_Select::CONTACT_EXPORT, $componentClause = NULL, $componentTable = NULL, $mergeSameAddress = FALSE, $mergeSameHousehold = FALSE, $exportParams = array(), $queryOperator = 'AND')
 {
     $returnProperties = array();
     $paymentFields = $selectedPaymentFields = FALSE;
     $relationField = NULL;
     $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
     // Warning - this imProviders var is used in a somewhat fragile way - don't rename it
     // without manually testing the export of IM provider still works.
     $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
     $contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE, 'name', FALSE);
     $queryMode = self::getQueryMode($exportMode);
     if ($fields) {
         //construct return properties
         $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         $locationTypeFields = array('street_address', 'supplemental_address_1', 'supplemental_address_2', 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'state_province', 'country', 'phone', 'email', 'im');
         foreach ($fields as $key => $value) {
             $phoneTypeId = $imProviderId = NULL;
             $relationshipTypes = $fieldName = CRM_Utils_Array::value(1, $value);
             if (!$fieldName) {
                 continue;
             }
             // get phoneType id and IM service provider id separately
             if ($fieldName == 'phone') {
                 $phoneTypeId = CRM_Utils_Array::value(3, $value);
             } elseif ($fieldName == 'im') {
                 $imProviderId = CRM_Utils_Array::value(3, $value);
             }
             if (array_key_exists($relationshipTypes, $contactRelationshipTypes)) {
                 if (!empty($value[2])) {
                     $relationField = CRM_Utils_Array::value(2, $value);
                     if (trim(CRM_Utils_Array::value(3, $value))) {
                         $relLocTypeId = CRM_Utils_Array::value(3, $value);
                     } else {
                         $relLocTypeId = 'Primary';
                     }
                     if ($relationField == 'phone') {
                         $relPhoneTypeId = CRM_Utils_Array::value(4, $value);
                     } elseif ($relationField == 'im') {
                         $relIMProviderId = CRM_Utils_Array::value(4, $value);
                     }
                 } elseif (!empty($value[4])) {
                     $relationField = CRM_Utils_Array::value(4, $value);
                     $relLocTypeId = CRM_Utils_Array::value(5, $value);
                     if ($relationField == 'phone') {
                         $relPhoneTypeId = CRM_Utils_Array::value(6, $value);
                     } elseif ($relationField == 'im') {
                         $relIMProviderId = CRM_Utils_Array::value(6, $value);
                     }
                 }
             }
             $contactType = CRM_Utils_Array::value(0, $value);
             $locTypeId = CRM_Utils_Array::value(2, $value);
             if ($relationField) {
                 if (in_array($relationField, $locationTypeFields) && is_numeric($relLocTypeId)) {
                     if ($relPhoneTypeId) {
                         $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['phone-' . $relPhoneTypeId] = 1;
                     } elseif ($relIMProviderId) {
                         $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['im-' . $relIMProviderId] = 1;
                     } else {
                         $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]][$relationField] = 1;
                     }
                     $relPhoneTypeId = $relIMProviderId = NULL;
                 } else {
                     $returnProperties[$relationshipTypes][$relationField] = 1;
                 }
             } elseif (is_numeric($locTypeId)) {
                 if ($phoneTypeId) {
                     $returnProperties['location'][$locationTypes[$locTypeId]]['phone-' . $phoneTypeId] = 1;
                 } elseif ($imProviderId) {
                     $returnProperties['location'][$locationTypes[$locTypeId]]['im-' . $imProviderId] = 1;
                 } else {
                     $returnProperties['location'][$locationTypes[$locTypeId]][$fieldName] = 1;
                 }
             } else {
                 //hack to fix component fields
                 //revert mix of event_id and title
                 if ($fieldName == 'event_id') {
                     $returnProperties['event_id'] = 1;
                 } elseif ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT && array_key_exists($fieldName, self::componentPaymentFields())) {
                     $selectedPaymentFields = TRUE;
                     $paymentTableId = 'participant_id';
                     $returnProperties[$fieldName] = 1;
                 } else {
                     $returnProperties[$fieldName] = 1;
                 }
             }
         }
         $defaultExportMode = self::defaultReturnProperty($exportMode);
         if ($defaultExportMode) {
             $returnProperties[$defaultExportMode] = 1;
         }
     } else {
         $primary = TRUE;
         $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE);
         foreach ($fields as $key => $var) {
             if ($key && substr($key, 0, 6) != 'custom') {
                 //for CRM=952
                 $returnProperties[$key] = 1;
             }
         }
         if ($primary) {
             $returnProperties['location_type'] = 1;
             $returnProperties['im_provider'] = 1;
             $returnProperties['phone_type_id'] = 1;
             $returnProperties['provider_id'] = 1;
             $returnProperties['current_employer'] = 1;
         }
         $extraProperties = self::defineExtraProperties($queryMode);
         $paymentFields = $extraProperties['paymentFields'];
         $extraReturnProperties = $extraProperties['extraReturnProperties'];
         $paymentTableId = $extraProperties['paymentTableId'];
         if ($queryMode != CRM_Contact_BAO_Query::MODE_CONTACTS) {
             $componentReturnProperties = CRM_Contact_BAO_Query::defaultReturnProperties($queryMode);
             if ($queryMode == CRM_Contact_BAO_Query::MODE_CONTRIBUTE) {
                 // soft credit columns are not automatically populated, because contribution search doesn't require them by default
                 $componentReturnProperties = array_merge($componentReturnProperties, CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE));
             }
             $returnProperties = array_merge($returnProperties, $componentReturnProperties);
             if (!empty($extraReturnProperties)) {
                 $returnProperties = array_merge($returnProperties, $extraReturnProperties);
             }
             // unset non exportable fields for components
             $nonExpoFields = array('groups', 'tags', 'notes', 'contribution_status_id', 'pledge_status_id', 'pledge_payment_status_id');
             foreach ($nonExpoFields as $value) {
                 unset($returnProperties[$value]);
             }
         }
     }
     if ($mergeSameAddress) {
         //make sure the addressee fields are selected
         //while using merge same address feature
         $returnProperties['addressee'] = 1;
         $returnProperties['postal_greeting'] = 1;
         $returnProperties['email_greeting'] = 1;
         $returnProperties['street_name'] = 1;
         $returnProperties['household_name'] = 1;
         $returnProperties['street_address'] = 1;
         $returnProperties['city'] = 1;
         $returnProperties['state_province'] = 1;
         // some columns are required for assistance incase they are not already present
         $exportParams['merge_same_address']['temp_columns'] = array();
         $tempColumns = array('id', 'master_id', 'state_province_id', 'postal_greeting_id', 'addressee_id');
         foreach ($tempColumns as $column) {
             if (!array_key_exists($column, $returnProperties)) {
                 $returnProperties[$column] = 1;
                 $column = $column == 'id' ? 'civicrm_primary_id' : $column;
                 $exportParams['merge_same_address']['temp_columns'][$column] = 1;
             }
         }
     }
     if (!$selectAll && $componentTable && !empty($exportParams['additional_group'])) {
         // If an Additional Group is selected, then all contacts in that group are
         // added to the export set (filtering out duplicates).
         $query = "\nINSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_contact gc WHERE gc.group_id = {$exportParams['additional_group']} ON DUPLICATE KEY UPDATE {$componentTable}.contact_id = gc.contact_id";
         CRM_Core_DAO::executeQuery($query);
     }
     if ($moreReturnProperties) {
         // fix for CRM-7066
         if (!empty($moreReturnProperties['group'])) {
             unset($moreReturnProperties['group']);
             $moreReturnProperties['groups'] = 1;
         }
         $returnProperties = array_merge($returnProperties, $moreReturnProperties);
     }
     $exportParams['postal_mailing_export']['temp_columns'] = array();
     if ($exportParams['exportOption'] == 2 && isset($exportParams['postal_mailing_export']) && CRM_Utils_Array::value('postal_mailing_export', $exportParams['postal_mailing_export']) == 1) {
         $postalColumns = array('is_deceased', 'do_not_mail', 'street_address', 'supplemental_address_1');
         foreach ($postalColumns as $column) {
             if (!array_key_exists($column, $returnProperties)) {
                 $returnProperties[$column] = 1;
                 $exportParams['postal_mailing_export']['temp_columns'][$column] = 1;
             }
         }
     }
     // rectify params to what proximity search expects if there is a value for prox_distance
     // CRM-7021
     if (!empty($params)) {
         CRM_Contact_BAO_ProximityQuery::fixInputParams($params);
     }
     $query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL, FALSE, FALSE, $queryMode, FALSE, TRUE, TRUE, NULL, $queryOperator);
     //sort by state
     //CRM-15301
     $query->_sort = $order;
     list($select, $from, $where, $having) = $query->query();
     if ($mergeSameHousehold == 1) {
         if (empty($returnProperties['id'])) {
             $returnProperties['id'] = 1;
         }
         //also merge Head of Household
         $relationKeyMOH = CRM_Utils_Array::key('Household Member of', $contactRelationshipTypes);
         $relationKeyHOH = CRM_Utils_Array::key('Head of Household for', $contactRelationshipTypes);
         foreach ($returnProperties as $key => $value) {
             if (!array_key_exists($key, $contactRelationshipTypes)) {
                 $returnProperties[$relationKeyMOH][$key] = $value;
                 $returnProperties[$relationKeyHOH][$key] = $value;
             }
         }
         unset($returnProperties[$relationKeyMOH]['location_type']);
         unset($returnProperties[$relationKeyMOH]['im_provider']);
         unset($returnProperties[$relationKeyHOH]['location_type']);
         unset($returnProperties[$relationKeyHOH]['im_provider']);
     }
     $allRelContactArray = $relationQuery = array();
     foreach ($contactRelationshipTypes as $rel => $dnt) {
         if ($relationReturnProperties = CRM_Utils_Array::value($rel, $returnProperties)) {
             $allRelContactArray[$rel] = array();
             // build Query for each relationship
             $relationQuery[$rel] = new CRM_Contact_BAO_Query(NULL, $relationReturnProperties, NULL, FALSE, FALSE, $queryMode);
             list($relationSelect, $relationFrom, $relationWhere, $relationHaving) = $relationQuery[$rel]->query();
             list($id, $direction) = explode('_', $rel, 2);
             // identify the relationship direction
             $contactA = 'contact_id_a';
             $contactB = 'contact_id_b';
             if ($direction == 'b_a') {
                 $contactA = 'contact_id_b';
                 $contactB = 'contact_id_a';
             }
             if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
                 $relIDs = $ids;
             } elseif ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
                 $sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
                 $query = "SELECT contact_id FROM civicrm_activity_contact\n                              WHERE activity_id IN ( " . implode(',', $ids) . ") AND\n                              record_type_id = {$sourceID}";
                 $dao = CRM_Core_DAO::executeQuery($query);
                 while ($dao->fetch()) {
                     $relIDs[] = $dao->contact_id;
                 }
             } else {
                 $component = self::exportComponent($exportMode);
                 if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
                     $relIDs = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
                 } else {
                     $relIDs = CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
                 }
             }
             $relationshipJoin = $relationshipClause = '';
             if (!$selectAll && $componentTable) {
                 $relationshipJoin = " INNER JOIN {$componentTable} ctTable ON ctTable.contact_id = {$contactA}";
             } elseif (!empty($relIDs)) {
                 $relID = implode(',', $relIDs);
                 $relationshipClause = " AND crel.{$contactA} IN ( {$relID} )";
             }
             $relationFrom = " {$relationFrom}\n                INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_a.id AND crel.relationship_type_id = {$id}\n                {$relationshipJoin} ";
             //check for active relationship status only
             $today = date('Ymd');
             $relationActive = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
             $relationWhere = " WHERE contact_a.is_deleted = 0 {$relationshipClause} {$relationActive}";
             $relationGroupBy = " GROUP BY crel.{$contactA}";
             $relationSelect = "{$relationSelect}, {$contactA} as refContact ";
             $relationQueryString = "{$relationSelect} {$relationFrom} {$relationWhere} {$relationHaving} {$relationGroupBy}";
             $allRelContactDAO = CRM_Core_DAO::executeQuery($relationQueryString);
             while ($allRelContactDAO->fetch()) {
                 //FIX Me: Migrate this to table rather than array
                 // build the array of all related contacts
                 $allRelContactArray[$rel][$allRelContactDAO->refContact] = clone $allRelContactDAO;
             }
             $allRelContactDAO->free();
         }
     }
     // make sure the groups stuff is included only if specifically specified
     // by the fields param (CRM-1969), else we limit the contacts outputted to only
     // ones that are part of a group
     if (!empty($returnProperties['groups'])) {
         $oldClause = "( contact_a.id = civicrm_group_contact.contact_id )";
         $newClause = " ( {$oldClause} AND ( civicrm_group_contact.status = 'Added' OR civicrm_group_contact.status IS NULL ) )";
         // total hack for export, CRM-3618
         $from = str_replace($oldClause, $newClause, $from);
     }
     if (!$selectAll && $componentTable) {
         $from .= " INNER JOIN {$componentTable} ctTable ON ctTable.contact_id = contact_a.id ";
     } elseif ($componentClause) {
         if (empty($where)) {
             $where = "WHERE {$componentClause}";
         } else {
             $where .= " AND {$componentClause}";
         }
     }
     // CRM-13982 - check if is deleted
     $excludeTrashed = TRUE;
     foreach ($params as $value) {
         if ($value[0] == 'contact_is_deleted') {
             $excludeTrashed = FALSE;
         }
     }
     $trashClause = $excludeTrashed ? "contact_a.is_deleted != 1" : "( 1 )";
     if (empty($where)) {
         $where = "WHERE {$trashClause}";
     } else {
         $where .= " AND {$trashClause}";
     }
     $queryString = "{$select} {$from} {$where} {$having}";
     $groupBy = self::getGroupBy($exportMode, $queryMode, $returnProperties, $query);
     $queryString .= $groupBy;
     // always add contact_a.id to the ORDER clause
     // so the order is deterministic
     //CRM-15301
     if (strpos('contact_a.id', $order) === FALSE) {
         $order .= ", contact_a.id";
     }
     if ($order) {
         list($field, $dir) = explode(' ', $order, 2);
         $field = trim($field);
         if (!empty($returnProperties[$field])) {
             //CRM-15301
             $queryString .= " ORDER BY {$order}";
         }
     }
     $multipleSelectFields = array('preferred_communication_method' => 1);
     $addPaymentHeader = FALSE;
     $paymentDetails = array();
     if ($paymentFields || $selectedPaymentFields) {
         // get payment related in for event and members
         $paymentDetails = CRM_Contribute_BAO_Contribution::getContributionDetails($exportMode, $ids);
         //get all payment headers.
         // If we haven't selected specific payment fields, load in all the
         // payment headers.
         if (!$selectedPaymentFields) {
             $paymentHeaders = self::componentPaymentFields();
             if (!empty($paymentDetails)) {
                 $addPaymentHeader = TRUE;
             }
         } else {
             $paymentHeaders = array();
         }
         $nullContributionDetails = array_fill_keys(array_keys($paymentHeaders), NULL);
     }
     $componentDetails = array();
     $setHeader = TRUE;
     $rowCount = self::EXPORT_ROW_COUNT;
     $offset = 0;
     // we write to temp table often to avoid using too much memory
     $tempRowCount = 100;
     $count = -1;
     // for CRM-3157 purposes
     $i18n = CRM_Core_I18n::singleton();
     list($outputColumns, $headerRows, $sqlColumns, $metadata) = self::getExportStructureArrays($returnProperties, $query, $phoneTypes, $imProviders, $contactRelationshipTypes, $relationQuery, $selectedPaymentFields);
     $limitReached = FALSE;
     while (!$limitReached) {
         $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}";
         $dao = CRM_Core_DAO::executeQuery($limitQuery);
         // If this is less than our limit by the end of the iteration we do not need to run the query again to
         // check if some remain.
         $rowsThisIteration = 0;
         while ($dao->fetch()) {
             $count++;
             $rowsThisIteration++;
             $row = array();
             //convert the pseudo constants
             // CRM-14398 there is problem in this architecture that is not easily solved. For now we are using the cloned
             // temporary iterationDAO object to get around it.
             // the issue is that the convertToPseudoNames function is adding additional properties (e.g for campaign) to the DAO object
             // these additional properties are NOT reset when the $dao cycles through the while loop
             // nor are they overwritten as they are not in the loop
             // the convertToPseudoNames will not adequately over-write them either as it doesn't 'kick-in' unless the
             // relevant property is set.
             // It may be that a long-term fix could be introduced there - however, it's probably necessary to figure out how to test the
             // export class before tackling a better architectural fix
             $iterationDAO = clone $dao;
             $query->convertToPseudoNames($iterationDAO);
             //first loop through output columns so that we return what is required, and in same order.
             $relationshipField = 0;
             foreach ($outputColumns as $field => $value) {
                 // add im_provider to $dao object
                 if ($field == 'im_provider' && property_exists($iterationDAO, 'provider_id')) {
                     $iterationDAO->im_provider = $iterationDAO->provider_id;
                 }
                 //build row values (data)
                 $fieldValue = NULL;
                 if (property_exists($iterationDAO, $field)) {
                     $fieldValue = $iterationDAO->{$field};
                     // to get phone type from phone type id
                     if ($field == 'phone_type_id' && isset($phoneTypes[$fieldValue])) {
                         $fieldValue = $phoneTypes[$fieldValue];
                     } elseif ($field == 'provider_id' || $field == 'im_provider') {
                         $fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders);
                     } elseif ($field == 'master_id') {
                         $masterAddressId = NULL;
                         if (isset($iterationDAO->master_id)) {
                             $masterAddressId = $iterationDAO->master_id;
                         }
                         // get display name of contact that address is shared.
                         $fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId, $iterationDAO->contact_id);
                     }
                 }
                 if ($field == 'id') {
                     $row[$field] = $iterationDAO->contact_id;
                     // special case for calculated field
                 } elseif ($field == 'source_contact_id') {
                     $row[$field] = $iterationDAO->contact_id;
                 } elseif ($field == 'pledge_balance_amount') {
                     $row[$field] = $iterationDAO->pledge_amount - $iterationDAO->pledge_total_paid;
                     // special case for calculated field
                 } elseif ($field == 'pledge_next_pay_amount') {
                     $row[$field] = $iterationDAO->pledge_next_pay_amount + $iterationDAO->pledge_outstanding_amount;
                 } elseif (array_key_exists($field, $contactRelationshipTypes)) {
                     $relDAO = CRM_Utils_Array::value($iterationDAO->contact_id, $allRelContactArray[$field]);
                     $relationQuery[$field]->convertToPseudoNames($relDAO);
                     foreach ($value as $relationField => $relationValue) {
                         if (is_object($relDAO) && property_exists($relDAO, $relationField)) {
                             $fieldValue = $relDAO->{$relationField};
                             if ($relationField == 'phone_type_id') {
                                 $fieldValue = $phoneTypes[$relationValue];
                             } elseif ($relationField == 'provider_id') {
                                 $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders);
                             } elseif (is_object($relDAO) && in_array($relationField, array('email_greeting', 'postal_greeting', 'addressee'))) {
                                 //special case for greeting replacement
                                 $fldValue = "{$relationField}_display";
                                 $fieldValue = $relDAO->{$fldValue};
                             }
                         } elseif (is_object($relDAO) && $relationField == 'state_province') {
                             $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
                         } elseif (is_object($relDAO) && $relationField == 'country') {
                             $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
                         } else {
                             $fieldValue = '';
                         }
                         $field = $field . '_';
                         if (array_key_exists($relationField, $multipleSelectFields)) {
                             $param = array($relationField => $fieldValue);
                             $names = array($relationField => array('newName' => $relationField, 'groupName' => $relationField));
                             CRM_Core_OptionGroup::lookupValues($param, $names, FALSE);
                             $fieldValue = $param[$relationField];
                         }
                         if (is_object($relDAO) && $relationField == 'id') {
                             $row[$field . $relationField] = $relDAO->contact_id;
                         } elseif (is_array($relationValue) && $relationField == 'location') {
                             foreach ($relationValue as $ltype => $val) {
                                 foreach (array_keys($val) as $fld) {
                                     $type = explode('-', $fld);
                                     $fldValue = "{$ltype}-" . $type[0];
                                     if (!empty($type[1])) {
                                         $fldValue .= "-" . $type[1];
                                     }
                                     // CRM-3157: localise country, region (both have ‘country’ context)
                                     // and state_province (‘province’ context)
                                     switch (TRUE) {
                                         case !is_object($relDAO):
                                             $row[$field . '_' . $fldValue] = '';
                                             break;
                                         case in_array('country', $type):
                                         case in_array('world_region', $type):
                                             $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->{$fldValue}, array('context' => 'country'));
                                             break;
                                         case in_array('state_province', $type):
                                             $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->{$fldValue}, array('context' => 'province'));
                                             break;
                                         default:
                                             $row[$field . '_' . $fldValue] = $relDAO->{$fldValue};
                                             break;
                                     }
                                 }
                             }
                         } elseif (isset($fieldValue) && $fieldValue != '') {
                             //check for custom data
                             if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) {
                                 $row[$field . $relationField] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
                             } else {
                                 //normal relationship fields
                                 // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
                                 switch ($relationField) {
                                     case 'country':
                                     case 'world_region':
                                         $row[$field . $relationField] = $i18n->crm_translate($fieldValue, array('context' => 'country'));
                                         break;
                                     case 'state_province':
                                         $row[$field . $relationField] = $i18n->crm_translate($fieldValue, array('context' => 'province'));
                                         break;
                                     default:
                                         $row[$field . $relationField] = $fieldValue;
                                         break;
                                 }
                             }
                         } else {
                             // if relation field is empty or null
                             $row[$field . $relationField] = '';
                         }
                     }
                 } elseif (isset($fieldValue) && $fieldValue != '') {
                     //check for custom data
                     if ($cfID = CRM_Core_BAO_CustomField::getKeyID($field)) {
                         $row[$field] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
                     } elseif (array_key_exists($field, $multipleSelectFields)) {
                         //option group fixes
                         $paramsNew = array($field => $fieldValue);
                         if ($field == 'test_tutoring') {
                             $name = array($field => array('newName' => $field, 'groupName' => 'test'));
                             // for  readers group
                         } elseif (substr($field, 0, 4) == 'cmr_') {
                             $name = array($field => array('newName' => $field, 'groupName' => substr($field, 0, -3)));
                         } else {
                             $name = array($field => array('newName' => $field, 'groupName' => $field));
                         }
                         CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE);
                         $row[$field] = $paramsNew[$field];
                     } elseif (in_array($field, array('email_greeting', 'postal_greeting', 'addressee'))) {
                         //special case for greeting replacement
                         $fldValue = "{$field}_display";
                         $row[$field] = $iterationDAO->{$fldValue};
                     } else {
                         //normal fields with a touch of CRM-3157
                         switch ($field) {
                             case 'country':
                             case 'world_region':
                                 $row[$field] = $i18n->crm_translate($fieldValue, array('context' => 'country'));
                                 break;
                             case 'state_province':
                                 $row[$field] = $i18n->crm_translate($fieldValue, array('context' => 'province'));
                                 break;
                             case 'gender':
                             case 'preferred_communication_method':
                             case 'preferred_mail_format':
                             case 'communication_style':
                                 $row[$field] = $i18n->crm_translate($fieldValue);
                                 break;
                             default:
                                 if (isset($metadata[$field])) {
                                     // No I don't know why we do it this way & whether we could
                                     // make better use of pseudoConstants.
                                     if (!empty($metadata[$field]['context'])) {
                                         $row[$field] = $i18n->crm_translate($fieldValue, $metadata[$field]);
                                         break;
                                     }
                                     if (!empty($metadata[$field]['pseudoconstant'])) {
                                         // This is not our normal syntax for pseudoconstants but I am a bit loath to
                                         // call an external function until sure it is not increasing php processing given this
                                         // may be iterated 100,000 times & we already have the $imProvider var loaded.
                                         // That can be next refactor...
                                         // Yes - definitely feeling hatred for this bit of code - I know you will beat me up over it's awfulness
                                         // but I have to reach a stable point....
                                         $varName = $metadata[$field]['pseudoconstant']['var'];
                                         $labels = ${$varName};
                                         $row[$field] = $labels[$fieldValue];
                                         break;
                                     }
                                 }
                                 $row[$field] = $fieldValue;
                                 break;
                         }
                     }
                 } elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) {
                     $paymentData = CRM_Utils_Array::value($iterationDAO->{$paymentTableId}, $paymentDetails);
                     $payFieldMapper = array('componentPaymentField_total_amount' => 'total_amount', 'componentPaymentField_contribution_status' => 'contribution_status', 'componentPaymentField_payment_instrument' => 'pay_instru', 'componentPaymentField_transaction_id' => 'trxn_id', 'componentPaymentField_received_date' => 'receive_date');
                     $row[$field] = CRM_Utils_Array::value($payFieldMapper[$field], $paymentData, '');
                 } else {
                     // if field is empty or null
                     $row[$field] = '';
                 }
             }
             // add payment headers if required
             if ($addPaymentHeader && $paymentFields) {
                 // @todo rather than do this for every single row do it before the loop starts.
                 // where other header definitions take place.
                 $headerRows = array_merge($headerRows, $paymentHeaders);
                 foreach (array_keys($paymentHeaders) as $paymentHdr) {
                     self::sqlColumnDefn($query, $sqlColumns, $paymentHdr);
                 }
             }
             if ($setHeader) {
                 $exportTempTable = self::createTempTable($sqlColumns);
             }
             //build header only once
             $setHeader = FALSE;
             // If specific payment fields have been selected for export, payment
             // data will already be in $row. Otherwise, add payment related
             // information, if appropriate.
             if ($addPaymentHeader) {
                 if (!$selectedPaymentFields) {
                     if ($paymentFields) {
                         $paymentData = CRM_Utils_Array::value($row[$paymentTableId], $paymentDetails);
                         if (!is_array($paymentData) || empty($paymentData)) {
                             $paymentData = $nullContributionDetails;
                         }
                         $row = array_merge($row, $paymentData);
                     } elseif (!empty($paymentDetails)) {
                         $row = array_merge($row, $nullContributionDetails);
                     }
                 }
             }
             //remove organization name for individuals if it is set for current employer
             if (!empty($row['contact_type']) && $row['contact_type'] == 'Individual' && array_key_exists('organization_name', $row)) {
                 $row['organization_name'] = '';
             }
             // add component info
             // write the row to a file
             $componentDetails[] = $row;
             // output every $tempRowCount rows
             if ($count % $tempRowCount == 0) {
                 self::writeDetailsToTable($exportTempTable, $componentDetails, $sqlColumns);
                 $componentDetails = array();
             }
         }
         $dao->free();
         if ($rowsThisIteration < self::EXPORT_ROW_COUNT) {
             $limitReached = TRUE;
         }
         $offset += $rowCount;
     }
     if ($exportTempTable) {
         self::writeDetailsToTable($exportTempTable, $componentDetails, $sqlColumns);
         // do merge same address and merge same household processing
         if ($mergeSameAddress) {
             self::mergeSameAddress($exportTempTable, $headerRows, $sqlColumns, $exportParams);
         }
         // merge the records if they have corresponding households
         if ($mergeSameHousehold) {
             self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyMOH);
             self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyHOH);
         }
         // if postalMailing option is checked, exclude contacts who are deceased, have
         // "Do not mail" privacy setting, or have no street address
         if (isset($exportParams['postal_mailing_export']['postal_mailing_export']) && $exportParams['postal_mailing_export']['postal_mailing_export'] == 1) {
             self::postalMailingFormat($exportTempTable, $headerRows, $sqlColumns, $exportMode);
         }
         // call export hook
         CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode);
         // In order to be able to write a unit test against this function we need to suppress
         // the csv writing. In future hopefully the csv writing & the main processing will be in separate functions.
         if (empty($exportParams['suppress_csv_for_testing'])) {
             self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode);
         }
         // delete the export temp table and component table
         $sql = "DROP TABLE IF EXISTS {$exportTempTable}";
         CRM_Core_DAO::executeQuery($sql);
         // Do not exit in test context.
         if (empty($exportParams['suppress_csv_for_testing'])) {
             CRM_Utils_System::civiExit();
         }
     } else {
         CRM_Core_Error::fatal(ts('No records to export'));
     }
 }
예제 #7
0
 /**
  * Based on the provided two contact_ids and a set of tables, move the belongings of the
  * other contact to the main one - be it Location / CustomFields or Contact .. related info.
  * A superset of moveContactBelongings() function.
  *
  * @param int $mainId
  *   Main contact with whom merge has to happen.
  * @param int $otherId
  *   Duplicate contact which would be deleted after merge operation.
  *
  * @param $migrationInfo
  *
  * @param bool $checkPermissions
  *   Respect logged in user permissions.
  *
  * @return bool
  */
 public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $checkPermissions = TRUE)
 {
     if (empty($migrationInfo)) {
         return FALSE;
     }
     $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
     $relTables = CRM_Dedupe_Merger::relTables();
     $moveTables = $locationMigrationInfo = $tableOperations = array();
     foreach ($migrationInfo as $key => $value) {
         if ($value == $qfZeroBug) {
             $value = '0';
         }
         if ((in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) || substr($key, 0, 12) == 'move_custom_') && $value != NULL) {
             $submitted[substr($key, 5)] = $value;
         } elseif (substr($key, 0, 14) == 'move_location_' and $value != NULL) {
             $locationMigrationInfo[$key] = $value;
         } elseif (substr($key, 0, 15) == 'move_rel_table_' and $value == '1') {
             $moveTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']);
             if (array_key_exists('operation', $migrationInfo)) {
                 foreach ($relTables[substr($key, 5)]['tables'] as $table) {
                     if (array_key_exists($key, $migrationInfo['operation'])) {
                         $tableOperations[$table] = $migrationInfo['operation'][$key];
                     }
                 }
             }
         }
     }
     self::mergeLocations($mainId, $otherId, $locationMigrationInfo, $migrationInfo);
     // **** Do tables related migrations
     if (!empty($moveTables)) {
         CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId, $moveTables, $tableOperations);
         unset($moveTables, $tableOperations);
     }
     // **** Do contact related migrations
     CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId);
     // FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact()
     $names['gender'] = array('newName' => 'gender_id', 'groupName' => 'gender');
     $names['individual_prefix'] = array('newName' => 'prefix_id', 'groupName' => 'individual_prefix');
     $names['individual_suffix'] = array('newName' => 'suffix_id', 'groupName' => 'individual_suffix');
     $names['communication_style'] = array('newName' => 'communication_style_id', 'groupName' => 'communication_style');
     $names['addressee'] = array('newName' => 'addressee_id', 'groupName' => 'addressee');
     $names['email_greeting'] = array('newName' => 'email_greeting_id', 'groupName' => 'email_greeting');
     $names['postal_greeting'] = array('newName' => 'postal_greeting_id', 'groupName' => 'postal_greeting');
     CRM_Core_OptionGroup::lookupValues($submitted, $names, TRUE);
     // fix custom fields so they're edible by createProfileContact()
     static $treeCache = array();
     if (!array_key_exists($migrationInfo['main_details']['contact_type'], $treeCache)) {
         $treeCache[$migrationInfo['main_details']['contact_type']] = CRM_Core_BAO_CustomGroup::getTree($migrationInfo['main_details']['contact_type'], CRM_Core_DAO::$_nullObject, NULL, -1);
     }
     $cgTree =& $treeCache[$migrationInfo['main_details']['contact_type']];
     $cFields = array();
     foreach ($cgTree as $key => $group) {
         if (!isset($group['fields'])) {
             continue;
         }
         foreach ($group['fields'] as $fid => $field) {
             $cFields[$fid]['attributes'] = $field;
         }
     }
     if (!isset($submitted)) {
         $submitted = array();
     }
     foreach ($submitted as $key => $value) {
         if (substr($key, 0, 7) == 'custom_') {
             $fid = (int) substr($key, 7);
             if (empty($cFields[$fid])) {
                 continue;
             }
             $htmlType = $cFields[$fid]['attributes']['html_type'];
             switch ($htmlType) {
                 case 'File':
                     $customFiles[] = $fid;
                     unset($submitted["custom_{$fid}"]);
                     break;
                 case 'Select Country':
                 case 'Select State/Province':
                     $submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
                     break;
                 case 'Select Date':
                     if ($cFields[$fid]['attributes']['is_view']) {
                         $submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
                     }
                     break;
                 case 'CheckBox':
                 case 'AdvMulti-Select':
                 case 'Multi-Select':
                 case 'Multi-Select Country':
                 case 'Multi-Select State/Province':
                     // Merge values from both contacts for multivalue fields, CRM-4385
                     // get the existing custom values from db.
                     $customParams = array('entityID' => $mainId, $key => TRUE);
                     $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
                     if (!empty($customfieldValues[$key])) {
                         $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
                         if (is_array($existingValue) && !empty($existingValue)) {
                             $mergeValue = $submmtedCustomValue = array();
                             if ($value == 'null') {
                                 // CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
                                 $submitted[$key] = $value;
                             } else {
                                 if ($value) {
                                     $submmtedCustomValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                                 }
                                 //hack to remove null and duplicate values from array.
                                 foreach (array_merge($submmtedCustomValue, $existingValue) as $k => $v) {
                                     if ($v != '' && !in_array($v, $mergeValue)) {
                                         $mergeValue[] = $v;
                                     }
                                 }
                                 //keep state and country as array format.
                                 //for checkbox and m-select format w/ VALUE_SEPARATOR
                                 if (in_array($htmlType, array('CheckBox', 'Multi-Select', 'AdvMulti-Select'))) {
                                     $submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $mergeValue) . CRM_Core_DAO::VALUE_SEPARATOR;
                                 } else {
                                     $submitted[$key] = $mergeValue;
                                 }
                             }
                         }
                     } elseif (in_array($htmlType, array('Multi-Select Country', 'Multi-Select State/Province'))) {
                         //we require submitted values should be in array format
                         if ($value) {
                             $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                             //hack to remove null values from array.
                             $mergeValue = array();
                             foreach ($mergeValueArray as $k => $v) {
                                 if ($v != '') {
                                     $mergeValue[] = $v;
                                 }
                             }
                             $submitted[$key] = $mergeValue;
                         }
                     }
                     break;
                 default:
                     break;
             }
         }
     }
     // **** Do file custom fields related migrations
     // FIXME: move this someplace else (one of the BAOs) after discussing
     // where to, and whether CRM_Core_BAO_File::deleteFileReferences() shouldn't actually,
     // like, delete a file...
     if (!isset($customFiles)) {
         $customFiles = array();
     }
     foreach ($customFiles as $customId) {
         list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($customId);
         // get the contact_id -> file_id mapping
         $fileIds = array();
         $sql = "SELECT entity_id, {$columnName} AS file_id FROM {$tableName} WHERE entity_id IN ({$mainId}, {$otherId})";
         $dao = CRM_Core_DAO::executeQuery($sql);
         while ($dao->fetch()) {
             $fileIds[$dao->entity_id] = $dao->file_id;
         }
         $dao->free();
         // delete the main contact's file
         if (!empty($fileIds[$mainId])) {
             CRM_Core_BAO_File::deleteFileReferences($fileIds[$mainId], $mainId, $customId);
         }
         // move the other contact's file to main contact
         //NYSS need to INSERT or UPDATE depending on whether main contact has an existing record
         if (CRM_Core_DAO::singleValueQuery("SELECT id FROM {$tableName} WHERE entity_id = {$mainId}")) {
             $sql = "UPDATE {$tableName} SET {$columnName} = {$fileIds[$otherId]} WHERE entity_id = {$mainId}";
         } else {
             $sql = "INSERT INTO {$tableName} ( entity_id, {$columnName} ) VALUES ( {$mainId}, {$fileIds[$otherId]} )";
         }
         CRM_Core_DAO::executeQuery($sql);
         if (CRM_Core_DAO::singleValueQuery("\n        SELECT id\n        FROM civicrm_entity_file\n        WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$otherId]}")) {
             $sql = "\n          UPDATE civicrm_entity_file\n          SET entity_id = {$mainId}\n          WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$otherId]}";
         } else {
             $sql = "\n          INSERT INTO civicrm_entity_file ( entity_table, entity_id, file_id )\n          VALUES ( '{$tableName}', {$mainId}, {$fileIds[$otherId]} )";
         }
         CRM_Core_DAO::executeQuery($sql);
     }
     // move view only custom fields CRM-5362
     $viewOnlyCustomFields = array();
     foreach ($submitted as $key => $value) {
         $fid = (int) substr($key, 7);
         if (array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view'])) {
             $viewOnlyCustomFields[$key] = $value;
         }
     }
     // special case to set values for view only, CRM-5362
     if (!empty($viewOnlyCustomFields)) {
         $viewOnlyCustomFields['entityID'] = $mainId;
         CRM_Core_BAO_CustomValueTable::setValues($viewOnlyCustomFields);
     }
     if (!$checkPermissions || CRM_Core_Permission::check('merge duplicate contacts') && CRM_Core_Permission::check('delete contacts')) {
         // if ext id is submitted then set it null for contact to be deleted
         if (!empty($submitted['external_identifier'])) {
             $query = "UPDATE civicrm_contact SET external_identifier = null WHERE id = {$otherId}";
             CRM_Core_DAO::executeQuery($query);
         }
         civicrm_api3('contact', 'delete', array('id' => $otherId));
     }
     // CRM-15681 merge sub_types
     if ($other_sub_types = CRM_Utils_Array::value('contact_sub_type', $migrationInfo['other_details'])) {
         if ($main_sub_types = CRM_Utils_Array::value('contact_sub_type', $migrationInfo['main_details'])) {
             $submitted['contact_sub_type'] = array_unique(array_merge($main_sub_types, $other_sub_types));
         } else {
             $submitted['contact_sub_type'] = $other_sub_types;
         }
     }
     // **** Update contact related info for the main contact
     if (!empty($submitted)) {
         $submitted['contact_id'] = $mainId;
         //update current employer field
         if ($currentEmloyerId = CRM_Utils_Array::value('current_employer_id', $submitted)) {
             if (!CRM_Utils_System::isNull($currentEmloyerId)) {
                 $submitted['current_employer'] = $submitted['current_employer_id'];
             } else {
                 $submitted['current_employer'] = '';
             }
             unset($submitted['current_employer_id']);
         }
         //CRM-14312 include prefix/suffix from mainId if not overridden for proper construction of display/sort name
         if (!isset($submitted['prefix_id']) && !empty($migrationInfo['main_details']['prefix_id'])) {
             $submitted['prefix_id'] = $migrationInfo['main_details']['prefix_id'];
         }
         if (!isset($submitted['suffix_id']) && !empty($migrationInfo['main_details']['suffix_id'])) {
             $submitted['suffix_id'] = $migrationInfo['main_details']['suffix_id'];
         }
         CRM_Contact_BAO_Contact::createProfileContact($submitted, CRM_Core_DAO::$_nullArray, $mainId);
     }
     CRM_Utils_Hook::post('merge', 'Contact', $mainId, CRM_Core_DAO::$_nullObject);
     self::createMergeActivities($mainId, $otherId);
     return TRUE;
 }
예제 #8
0
 /**
  * Generate the where clause and also the english language equivalent.
  */
 public function where()
 {
     foreach ($this->_ids as $id => $values) {
         // Fixed for Issue CRM 607
         if (CRM_Utils_Array::value($id, $this->_fields) === NULL || !$values) {
             continue;
         }
         $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
         foreach ($values as $tuple) {
             list($name, $op, $value, $grouping, $wildcard) = $tuple;
             $field = $this->_fields[$id];
             $fieldName = "{$field['table_name']}.{$field['column_name']}";
             $isSerialized = CRM_Core_BAO_CustomField::isSerialized($field);
             // fix $value here to escape sql injection attacks
             $qillValue = NULL;
             if (!is_array($value)) {
                 $value = CRM_Core_DAO::escapeString(trim($value));
                 $qillValue = CRM_Core_BAO_CustomField::displayValue($value, $id);
             } elseif (count($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
                 $op = key($value);
                 $qillValue = strstr($op, 'NULL') ? NULL : CRM_Core_BAO_CustomField::displayValue($value[$op], $id);
             } else {
                 $op = strstr($op, 'IN') ? $op : 'IN';
                 $qillValue = CRM_Core_BAO_CustomField::displayValue($value, $id);
             }
             $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
             switch ($field['data_type']) {
                 case 'String':
                 case 'StateProvince':
                 case 'Country':
                     if ($field['is_search_range'] && is_array($value)) {
                         //didn't found any field under any of these three data-types as searchable by range
                     } else {
                         // fix $value here to escape sql injection attacks
                         if (!is_array($value)) {
                             if ($field['data_type'] == 'String') {
                                 $value = CRM_Utils_Type::escape($strtolower($value), 'String');
                             } else {
                                 $value = CRM_Utils_Type::escape($value, 'Integer');
                             }
                         } elseif ($isSerialized) {
                             if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
                                 $op = key($value);
                                 $value = $value[$op];
                             }
                             $value = implode(',', (array) $value);
                         }
                         // CRM-14563,CRM-16575 : Special handling of multi-select custom fields
                         if ($isSerialized && !empty($value) && !strstr($op, 'NULL') && !strstr($op, 'LIKE')) {
                             $sp = CRM_Core_DAO::VALUE_SEPARATOR;
                             if (strstr($op, 'IN')) {
                                 $value = str_replace(",", "{$sp}|{$sp}", $value);
                                 $value = str_replace('(', '[[.left-parenthesis.]]', $value);
                                 $value = str_replace(')', '[[.right-parenthesis.]]', $value);
                             }
                             $op = strstr($op, '!') || strstr($op, 'NOT') ? 'NOT RLIKE' : 'RLIKE';
                             $value = $sp . $value . $sp;
                             if (!$wildcard) {
                                 foreach (explode("|", $value) as $val) {
                                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $val, 'String');
                                 }
                             } else {
                                 $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
                             }
                         } else {
                             //FIX for custom data query fired against no value(NULL/NOT NULL)
                             $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
                         }
                         $this->_qill[$grouping][] = $field['label'] . " {$qillOp} {$qillValue}";
                     }
                     break;
                 case 'ContactReference':
                     $label = $value ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name') : '';
                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
                     $this->_qill[$grouping][] = $field['label'] . " {$qillOp} {$label}";
                     break;
                 case 'Int':
                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
                     $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));
                     break;
                 case 'Boolean':
                     if (!is_array($value)) {
                         if (strtolower($value) == 'yes' || strtolower($value) == strtolower(ts('Yes'))) {
                             $value = 1;
                         } else {
                             $value = (int) $value;
                         }
                         $value = $value == 1 ? 1 : 0;
                         $qillValue = $value ? 'Yes' : 'No';
                     }
                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Integer');
                     $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));
                     break;
                 case 'Link':
                 case 'Memo':
                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
                     $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));
                     break;
                 case 'Money':
                     $value = CRM_Utils_Array::value($op, (array) $value, $value);
                     if (is_array($value)) {
                         foreach ($value as $key => $val) {
                             $value[$key] = CRM_Utils_Rule::cleanMoney($value[$key]);
                         }
                     } else {
                         $value = CRM_Utils_Rule::cleanMoney($value);
                     }
                 case 'Float':
                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'Float');
                     $this->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $field['label'], 2 => $qillOp, 3 => $qillValue));
                     break;
                 case 'Date':
                     $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op, $value, 'String');
                     list($qillOp, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $field['label'], $value, $op, array(), CRM_Utils_Type::T_DATE);
                     $this->_qill[$grouping][] = "{$field['label']} {$qillOp} '{$qillVal}'";
                     break;
                 case 'File':
                     if ($op == 'IS NULL' || $op == 'IS NOT NULL' || $op == 'IS EMPTY' || $op == 'IS NOT EMPTY') {
                         switch ($op) {
                             case 'IS EMPTY':
                                 $op = 'IS NULL';
                                 break;
                             case 'IS NOT EMPTY':
                                 $op = 'IS NOT NULL';
                                 break;
                         }
                         $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldName, $op);
                         $this->_qill[$grouping][] = $field['label'] . " {$qillOp} ";
                     }
                     break;
             }
         }
     }
 }
예제 #9
0
 /**
  * Build badges parameters before actually creating badges.
  *
  * @param array $params
  *   Associated array of submitted values.
  * @param CRM_Core_Form $form
  */
 public static function buildBadges(&$params, &$form)
 {
     // get name badge layout info
     $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
     // split/get actual field names from token and individual contact image URLs
     $returnProperties = array();
     if (!empty($layoutInfo['data']['token'])) {
         foreach ($layoutInfo['data']['token'] as $index => $value) {
             $element = '';
             if ($value) {
                 $token = CRM_Utils_Token::getTokens($value);
                 if (key($token) == 'contact') {
                     $element = $token['contact'][0];
                 } elseif (key($token) == 'event') {
                     $element = $token['event'][0];
                     //FIX ME - we need to standardize event token names
                     if (substr($element, 0, 6) != 'event_') {
                         $element = 'event_' . $element;
                     }
                 } elseif (key($token) == 'participant') {
                     $element = $token['participant'][0];
                 }
                 // build returnproperties for query
                 $returnProperties[$element] = 1;
             }
             // add actual field name to row element
             $layoutInfo['data']['rowElements'][$index] = $element;
         }
     }
     // add additional required fields for query execution
     $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL');
     foreach ($additionalFields as $field) {
         $returnProperties[$field] = 1;
     }
     if ($form->_single) {
         $queryParams = NULL;
     } else {
         $queryParams = $form->get('queryParams');
     }
     $query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT);
     list($select, $from, $where, $having) = $query->query();
     if (empty($where)) {
         $where = "WHERE {$form->_componentClause}";
     } else {
         $where .= " AND {$form->_componentClause}";
     }
     $sortOrder = NULL;
     if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
         $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
         if (!empty($sortOrder)) {
             $sortOrder = " ORDER BY {$sortOrder}";
         }
     }
     $queryString = "{$select} {$from} {$where} {$having} {$sortOrder}";
     $dao = CRM_Core_DAO::executeQuery($queryString);
     $rows = array();
     while ($dao->fetch()) {
         $query->convertToPseudoNames($dao);
         $rows[$dao->participant_id] = array();
         foreach ($returnProperties as $key => $dontCare) {
             $value = isset($dao->{$key}) ? $dao->{$key} : NULL;
             // Format custom fields
             if (strstr($key, 'custom_') && isset($value)) {
                 $value = CRM_Core_BAO_CustomField::displayValue($value, substr($key, 7), $dao->contact_id);
             }
             $rows[$dao->participant_id][$key] = $value;
         }
     }
     $eventBadgeClass = new CRM_Badge_BAO_Badge();
     $eventBadgeClass->createLabels($rows, $layoutInfo);
 }
예제 #10
0
 /**
  * Build the array for display the profile fields.
  *
  * @param array $params
  *   Key value.
  * @param int $gid
  *   Profile Id.
  * @param array $groupTitle
  *   Profile Group Title.
  * @param array $values
  *   Formatted array of key value.
  *
  * @param array $profileFields
  */
 public static function displayProfile(&$params, $gid, &$groupTitle, &$values, &$profileFields = array())
 {
     if ($gid) {
         $config = CRM_Core_Config::singleton();
         $session = CRM_Core_Session::singleton();
         $contactID = $session->get('userID');
         if ($contactID) {
             if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $contactID)) {
                 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW);
             }
         } else {
             $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::ADD);
         }
         foreach ($fields as $v) {
             if (!empty($v['groupTitle'])) {
                 $groupTitle['groupTitle'] = $v['groupTitle'];
                 break;
             }
         }
         $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
         //start of code to set the default values
         foreach ($fields as $name => $field) {
             $customVal = '';
             $skip = FALSE;
             // skip fields that should not be displayed separately
             if ($field['skipDisplay']) {
                 continue;
             }
             $index = $field['title'];
             if ($name === 'organization_name') {
                 $values[$index] = $params[$name];
             }
             if ('state_province' == substr($name, 0, 14)) {
                 if ($params[$name]) {
                     $values[$index] = CRM_Core_PseudoConstant::stateProvince($params[$name]);
                 } else {
                     $values[$index] = '';
                 }
             } elseif ('date' == substr($name, -4)) {
                 $values[$index] = CRM_Utils_Date::customFormat(CRM_Utils_Date::processDate($params[$name]), $config->dateformatFull);
             } elseif ('country' == substr($name, 0, 7)) {
                 if ($params[$name]) {
                     $values[$index] = CRM_Core_PseudoConstant::country($params[$name]);
                 } else {
                     $values[$index] = '';
                 }
             } elseif ('county' == substr($name, 0, 6)) {
                 if ($params[$name]) {
                     $values[$index] = CRM_Core_PseudoConstant::county($params[$name]);
                 } else {
                     $values[$index] = '';
                 }
             } elseif (in_array(substr($name, 0, -3), array('gender', 'prefix', 'suffix', 'communication_style'))) {
                 $values[$index] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $name, $params[$name]);
             } elseif (in_array($name, array('addressee', 'email_greeting', 'postal_greeting'))) {
                 $filterCondition = array('greeting_type' => $name);
                 $greeting = CRM_Core_PseudoConstant::greeting($filterCondition);
                 $values[$index] = $greeting[$params[$name]];
             } elseif ($name === 'preferred_communication_method') {
                 $communicationFields = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
                 $compref = array();
                 $pref = $params[$name];
                 if (is_array($pref)) {
                     foreach ($pref as $k => $v) {
                         if ($v) {
                             $compref[] = $communicationFields[$k];
                         }
                     }
                 }
                 $values[$index] = implode(',', $compref);
             } elseif ($name == 'contact_sub_type') {
                 $values[$index] = implode(', ', $params[$name]);
             } elseif ($name == 'group') {
                 $groups = CRM_Contact_BAO_GroupContact::getGroupList();
                 $title = array();
                 foreach ($params[$name] as $gId => $dontCare) {
                     if ($dontCare) {
                         $title[] = $groups[$gId];
                     }
                 }
                 $values[$index] = implode(', ', $title);
             } elseif ($name == 'tag') {
                 $entityTags = $params[$name];
                 $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
                 $title = array();
                 if (is_array($entityTags)) {
                     foreach ($entityTags as $tagId => $dontCare) {
                         $title[] = $allTags[$tagId];
                     }
                 }
                 $values[$index] = implode(', ', $title);
             } elseif ('participant_role_id' == $name or 'participant_role' == $name) {
                 $roles = CRM_Event_PseudoConstant::participantRole();
                 $values[$index] = $roles[$params[$name]];
             } elseif ('participant_status_id' == $name or 'participant_status' == $name) {
                 $status = CRM_Event_PseudoConstant::participantStatus();
                 $values[$index] = $status[$params[$name]];
             } elseif (substr($name, -11) == 'campaign_id') {
                 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($params[$name]);
                 $values[$index] = CRM_Utils_Array::value($params[$name], $campaigns);
             } elseif (strpos($name, '-') !== FALSE) {
                 list($fieldName, $id) = CRM_Utils_System::explode('-', $name, 2);
                 $detailName = str_replace(' ', '_', $name);
                 if (in_array($fieldName, array('state_province', 'country', 'county'))) {
                     $values[$index] = $params[$detailName];
                     $idx = $detailName . '_id';
                     $values[$index] = $params[$idx];
                 } elseif ($fieldName == 'im') {
                     $providerName = NULL;
                     if ($providerId = $detailName . '-provider_id') {
                         $providerName = CRM_Utils_Array::value($params[$providerId], $imProviders);
                     }
                     if ($providerName) {
                         $values[$index] = $params[$detailName] . " (" . $providerName . ")";
                     } else {
                         $values[$index] = $params[$detailName];
                     }
                 } elseif ($fieldName == 'phone') {
                     $phoneExtField = str_replace('phone', 'phone_ext', $detailName);
                     if (isset($params[$phoneExtField])) {
                         $values[$index] = $params[$detailName] . " (" . $params[$phoneExtField] . ")";
                     } else {
                         $values[$index] = $params[$detailName];
                     }
                 } else {
                     $values[$index] = $params[$detailName];
                 }
             } else {
                 if (substr($name, 0, 7) === 'do_not_' or substr($name, 0, 3) === 'is_') {
                     if ($params[$name]) {
                         $values[$index] = '[ x ]';
                     }
                 } else {
                     if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) {
                         $query = "\nSELECT html_type, data_type\nFROM   civicrm_custom_field\nWHERE  id = {$cfID}\n";
                         $dao = CRM_Core_DAO::executeQuery($query);
                         $dao->fetch();
                         $htmlType = $dao->html_type;
                         if ($htmlType == 'File') {
                             $values[$index] = $params[$index];
                         } else {
                             if ($dao->data_type == 'Int' || $dao->data_type == 'Boolean') {
                                 $v = $params[$name];
                                 if (!CRM_Utils_System::isNull($v)) {
                                     $customVal = (int) $v;
                                 }
                             } elseif ($dao->data_type == 'Float') {
                                 $customVal = (double) $params[$name];
                             } elseif ($dao->data_type == 'Date') {
                                 //@todo note the currently we are using default date time formatting. Since you can select/set
                                 // different date and time format specific to custom field we should consider fixing this
                                 // sometime in the future
                                 $customVal = $displayValue = CRM_Utils_Date::customFormat(CRM_Utils_Date::processDate($params[$name]), $config->dateformatFull);
                                 if (!empty($params[$name . '_time'])) {
                                     $customVal = $displayValue = CRM_Utils_Date::customFormat(CRM_Utils_Date::processDate($params[$name], $params[$name . '_time']), $config->dateformatDatetime);
                                 }
                                 $skip = TRUE;
                             } else {
                                 $customVal = $params[$name];
                             }
                             //take the custom field options
                             $returnProperties = array($name => 1);
                             $query = new CRM_Contact_BAO_Query($params, $returnProperties, $fields);
                             if (!$skip) {
                                 $displayValue = CRM_Core_BAO_CustomField::displayValue($customVal, $cfID);
                             }
                             //Hack since we dont have function to check empty.
                             //FIXME in 2.3 using crmIsEmptyArray()
                             $customValue = TRUE;
                             if (is_array($customVal) && is_array($displayValue)) {
                                 $customValue = array_diff($customVal, $displayValue);
                             }
                             //use difference of arrays
                             if (empty($customValue) || !$customValue) {
                                 $values[$index] = '';
                             } else {
                                 $values[$index] = $displayValue;
                             }
                         }
                     } elseif ($name == 'home_URL' && !empty($params[$name])) {
                         $url = CRM_Utils_System::fixURL($params[$name]);
                         $values[$index] = "<a href=\"{$url}\">{$params[$name]}</a>";
                     } elseif (in_array($name, array('birth_date', 'deceased_date', 'participant_register_date'))) {
                         $values[$index] = CRM_Utils_Date::customFormat(CRM_Utils_Date::format($params[$name]));
                     } else {
                         $values[$index] = CRM_Utils_Array::value($name, $params);
                     }
                 }
             }
             $profileFields[$name] = $field;
         }
     }
 }
예제 #11
0
 /**
  * Returns all the rows in the given offset and rowCount.
  *
  * @param string $action
  *   The action being performed.
  * @param int $offset
  *   The row number to start from.
  * @param int $rowCount
  *   The number of rows to return.
  * @param string $sort
  *   The sql string that describes the sort order.
  * @param string $output
  *   What should the result set include (web/email/csv).
  *
  * @return int
  *   the total number of rows for this action
  */
 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL)
 {
     $config = CRM_Core_Config::singleton();
     if (($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN) && $this->_formValues['radio_ts'] == 'ts_sel') {
         $includeContactIds = TRUE;
     } else {
         $includeContactIds = FALSE;
     }
     // note the formvalues were given by CRM_Contact_Form_Search to us
     // and contain the search criteria (parameters)
     // note that the default action is basic
     if ($rowCount) {
         $cacheKey = $this->buildPrevNextCache($sort);
         $result = $this->_query->getCachedContacts($cacheKey, $offset, $rowCount, $includeContactIds);
     } else {
         $result = $this->_query->searchQuery($offset, $rowCount, $sort, FALSE, $includeContactIds);
     }
     // process the result of the query
     $rows = array();
     $permissions = array(CRM_Core_Permission::getPermission());
     if (CRM_Core_Permission::check('delete contacts')) {
         $permissions[] = CRM_Core_Permission::DELETE;
     }
     $mask = CRM_Core_Action::mask($permissions);
     // mask value to hide map link if there are not lat/long
     $mapMask = $mask & 4095;
     if ($this->_searchContext == 'smog') {
         $gc = CRM_Core_SelectValues::groupContactStatus();
     }
     if ($this->_ufGroupID) {
         $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         $names = array();
         static $skipFields = array('group', 'tag');
         foreach ($this->_fields as $key => $field) {
             if (!empty($field['in_selector']) && !in_array($key, $skipFields)) {
                 if (strpos($key, '-') !== FALSE) {
                     list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $key, 3);
                     if ($id == 'Primary') {
                         $locationTypeName = 1;
                     } elseif ($fieldName == 'url') {
                         $locationTypeName = "website-{$id}";
                     } else {
                         $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
                         if (!$locationTypeName) {
                             continue;
                         }
                     }
                     $locationTypeName = str_replace(' ', '_', $locationTypeName);
                     if (in_array($fieldName, array('phone', 'im', 'email'))) {
                         if ($type) {
                             $names[] = "{$locationTypeName}-{$fieldName}-{$type}";
                         } else {
                             $names[] = "{$locationTypeName}-{$fieldName}";
                         }
                     } else {
                         $names[] = "{$locationTypeName}-{$fieldName}";
                     }
                 } else {
                     $names[] = $field['name'];
                 }
             }
         }
         $names[] = "status";
     } elseif (!empty($this->_returnProperties)) {
         $names = self::makeProperties($this->_returnProperties);
     } else {
         $names = self::$_properties;
     }
     $multipleSelectFields = array('preferred_communication_method' => 1);
     $links = self::links($this->_context, $this->_contextMenu, $this->_key);
     //check explicitly added contact to a Smart Group.
     $groupID = CRM_Utils_Array::value('group', $this->_formValues);
     $pseudoconstants = array();
     // for CRM-3157 purposes
     if (in_array('world_region', $names)) {
         $pseudoconstants['world_region'] = array('dbName' => 'worldregion_id', 'values' => CRM_Core_PseudoConstant::worldRegion());
     }
     $seenIDs = array();
     while ($result->fetch()) {
         $row = array();
         $this->_query->convertToPseudoNames($result);
         // the columns we are interested in
         foreach ($names as $property) {
             if ($property == 'status') {
                 continue;
             }
             if ($cfID = CRM_Core_BAO_CustomField::getKeyID($property)) {
                 $row[$property] = CRM_Core_BAO_CustomField::displayValue($result->{$property}, $cfID, $result->contact_id);
             } elseif ($multipleSelectFields && array_key_exists($property, $multipleSelectFields)) {
                 $key = $property;
                 $paramsNew = array($key => $result->{$property});
                 $name = array($key => array('newName' => $key, 'groupName' => $key));
                 CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE);
                 $row[$key] = $paramsNew[$key];
             } elseif (strpos($property, '-im')) {
                 $row[$property] = $result->{$property};
                 if (!empty($result->{$property})) {
                     $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
                     $providerId = $property . "-provider_id";
                     $providerName = $imProviders[$result->{$providerId}];
                     $row[$property] = $result->{$property} . " ({$providerName})";
                 }
             } elseif (in_array($property, array('addressee', 'email_greeting', 'postal_greeting'))) {
                 $greeting = $property . '_display';
                 $row[$property] = $result->{$greeting};
             } elseif (isset($pseudoconstants[$property])) {
                 $row[$property] = CRM_Utils_Array::value($result->{$pseudoconstants[$property]['dbName']}, $pseudoconstants[$property]['values']);
             } elseif (strpos($property, '-url') !== FALSE) {
                 $websiteUrl = '';
                 $websiteKey = str_replace('-url', '', $property);
                 $propertyArray = explode('-', $property);
                 $websiteFld = $websiteKey . '-' . array_pop($propertyArray);
                 if (!empty($result->{$websiteFld})) {
                     $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
                     $websiteType = $websiteTypes[$result->{"{$websiteKey}-website_type_id"}];
                     $websiteValue = $result->{$websiteFld};
                     $websiteUrl = "<a href=\"{$websiteValue}\">{$websiteValue}  ({$websiteType})</a>";
                 }
                 $row[$property] = $websiteUrl;
             } else {
                 $row[$property] = isset($result->{$property}) ? $result->{$property} : NULL;
             }
         }
         if (!empty($result->postal_code_suffix)) {
             $row['postal_code'] .= "-" . $result->postal_code_suffix;
         }
         if ($output != CRM_Core_Selector_Controller::EXPORT && $this->_searchContext == 'smog') {
             if (empty($result->status) && $groupID) {
                 $contactID = $result->contact_id;
                 if ($contactID) {
                     $gcParams = array('contact_id' => $contactID, 'group_id' => $groupID);
                     $gcDefaults = array();
                     CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_GroupContact', $gcParams, $gcDefaults);
                     if (empty($gcDefaults)) {
                         $row['status'] = ts('Smart');
                     } else {
                         $row['status'] = $gc[$gcDefaults['status']];
                     }
                 } else {
                     $row['status'] = NULL;
                 }
             } else {
                 $row['status'] = $gc[$result->status];
             }
         }
         if ($output != CRM_Core_Selector_Controller::EXPORT) {
             $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
             if (!empty($this->_formValues['deleted_contacts']) && CRM_Core_Permission::check('access deleted contacts')) {
                 $links = array(array('name' => ts('View'), 'url' => 'civicrm/contact/view', 'qs' => 'reset=1&cid=%%id%%', 'class' => 'no-popup', 'title' => ts('View Contact Details')), array('name' => ts('Restore'), 'url' => 'civicrm/contact/view/delete', 'qs' => 'reset=1&cid=%%id%%&restore=1', 'title' => ts('Restore Contact')));
                 if (CRM_Core_Permission::check('delete contacts')) {
                     $links[] = array('name' => ts('Delete Permanently'), 'url' => 'civicrm/contact/view/delete', 'qs' => 'reset=1&cid=%%id%%&skip_undelete=1', 'title' => ts('Permanently Delete Contact'));
                 }
                 $row['action'] = CRM_Core_Action::formLink($links, NULL, array('id' => $result->contact_id), ts('more'), FALSE, 'contact.selector.row', 'Contact', $result->contact_id);
             } elseif (is_numeric(CRM_Utils_Array::value('geo_code_1', $row)) || !empty($row['city']) && CRM_Utils_Array::value('state_province', $row)) {
                 $row['action'] = CRM_Core_Action::formLink($links, $mask, array('id' => $result->contact_id), ts('more'), FALSE, 'contact.selector.row', 'Contact', $result->contact_id);
             } else {
                 $row['action'] = CRM_Core_Action::formLink($links, $mapMask, array('id' => $result->contact_id), ts('more'), FALSE, 'contact.selector.row', 'Contact', $result->contact_id);
             }
             // allow components to add more actions
             CRM_Core_Component::searchAction($row, $result->contact_id);
             $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id);
             $row['contact_type_orig'] = $result->contact_sub_type ? $result->contact_sub_type : $result->contact_type;
             $row['contact_sub_type'] = $result->contact_sub_type ? CRM_Contact_BAO_ContactType::contactTypePairs(FALSE, $result->contact_sub_type, ', ') : $result->contact_sub_type;
             $row['contact_id'] = $result->contact_id;
             $row['sort_name'] = $result->sort_name;
             if (array_key_exists('id', $row)) {
                 $row['id'] = $result->contact_id;
             }
         }
         // Dedupe contacts
         if (in_array($row['contact_id'], $seenIDs) === FALSE) {
             $seenIDs[] = $row['contact_id'];
             $rows[] = $row;
         }
     }
     return $rows;
 }
 /**
  * Browse the listing.
  *
  * @return void
  */
 public function browse()
 {
     $dateFields = NULL;
     $cgcount = 0;
     $attributes = $headerAttr = 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', 'is_view');
         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']['is_view'] = CRM_Utils_Array::value('is_view', $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);
         }
         $DTparams = !empty($this->_DTparams) ? $this->_DTparams : NULL;
         // 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, $DTparams);
         $resultCount = !empty($result['count']) ? $result['count'] : count($result);
         $sortedResult = !empty($result['sortedResult']) ? $result['sortedResult'] : array();
         unset($result['count']);
         unset($result['sortedResult']);
         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 ? $resultCount + 1 : NULL;
             if (!empty($DTparams)) {
                 $this->_total = $resultCount;
                 $cgcount = $DTparams['offset'] + 1;
             }
             foreach ($result as $recId => &$value) {
                 foreach ($value as $fieldId => &$val) {
                     if (is_numeric($fieldId)) {
                         $customValue =& $val;
                         if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
                             // formatted date capture value capture
                             $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::displayValue($customValue, $fieldId);
                             //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::displayValue($customValue, $fieldId);
                         }
                         // 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 (!$options[$fieldId]['attributes']['is_view'] && $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 {
                             $headerAttr[$fieldId]['dataType'] = CRM_Utils_Array::value('data-type', $fieldAttributes);
                             $headerAttr[$fieldId]['dataEmptyOption'] = CRM_Utils_Array::value('data-empty-option', $fieldAttributes);
                             // 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'] = empty($DTparams['sort']) ? $cgcount : $sortedResult[$recId];
                             $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'];
             $headerAttr[$fieldID]['columnName'] = $fieldLabels[$fieldID]['column_name'];
         }
     }
     $this->assign('dateFields', $dateFields);
     $this->assign('dateFieldsVals', $dateFieldsVals);
     $this->assign('cgcount', $cgcount);
     $this->assign('contactId', $this->_contactId);
     $this->assign('contactType', $this->_contactType);
     $this->assign('customGroupTitle', $this->_customGroupTitle);
     $this->assign('headers', $headers);
     $this->assign('headerAttr', $headerAttr);
     $this->assign('records', $result);
     $this->assign('attributes', $attributes);
     return array($result, $attributes);
 }
예제 #13
0
 /**
  * Process the form after the input has been submitted and validated.
  */
 public function postProcess()
 {
     $fv = $this->controller->exportValues($this->_name);
     $config = CRM_Core_Config::singleton();
     $locName = NULL;
     //get the address format sequence from the config file
     $mailingFormat = Civi::settings()->get('mailing_format');
     $sequence = CRM_Utils_Address::sequence($mailingFormat);
     foreach ($sequence as $v) {
         $address[$v] = 1;
     }
     if (array_key_exists('postal_code', $address)) {
         $address['postal_code_suffix'] = 1;
     }
     //build the returnproperties
     $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
     $mailingFormat = Civi::settings()->get('mailing_format');
     $mailingFormatProperties = array();
     if ($mailingFormat) {
         $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat);
         $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
     }
     //we should not consider addressee for data exists, CRM-6025
     if (array_key_exists('addressee', $mailingFormatProperties)) {
         unset($mailingFormatProperties['addressee']);
     }
     $customFormatProperties = array();
     if (stristr($mailingFormat, 'custom_')) {
         foreach ($mailingFormatProperties as $token => $true) {
             if (substr($token, 0, 7) == 'custom_') {
                 if (empty($customFormatProperties[$token])) {
                     $customFormatProperties[$token] = $mailingFormatProperties[$token];
                 }
             }
         }
     }
     if (!empty($customFormatProperties)) {
         $returnProperties = array_merge($returnProperties, $customFormatProperties);
     }
     if (isset($fv['merge_same_address'])) {
         // we need first name/last name for summarising to avoid spillage
         $returnProperties['first_name'] = 1;
         $returnProperties['last_name'] = 1;
     }
     $individualFormat = FALSE;
     /*
      * CRM-8338: replace ids of household members with the id of their household
      * so we can merge labels by household.
      */
     if (isset($fv['merge_same_household'])) {
         $this->mergeContactIdsByHousehold();
         $individualFormat = TRUE;
     }
     //get the contacts information
     $params = array();
     if (!empty($fv['location_type_id'])) {
         $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         $locName = $locType[$fv['location_type_id']];
         $location = array('location' => array("{$locName}" => $address));
         $returnProperties = array_merge($returnProperties, $location);
         $params[] = array('location_type', '=', array(1 => $fv['location_type_id']), 0, 0);
     } else {
         $returnProperties = array_merge($returnProperties, $address);
     }
     $rows = array();
     foreach ($this->_contactIds as $key => $contactID) {
         $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0);
     }
     // fix for CRM-2651
     if (!empty($fv['do_not_mail'])) {
         $params[] = array('do_not_mail', '=', 0, 0, 0);
     }
     // fix for CRM-2613
     $params[] = array('is_deceased', '=', 0, 0, 0);
     $custom = array();
     foreach ($returnProperties as $name => $dontCare) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         if ($cfID) {
             $custom[] = $cfID;
         }
     }
     //get the total number of contacts to fetch from database.
     $numberofContacts = count($this->_contactIds);
     $query = new CRM_Contact_BAO_Query($params, $returnProperties);
     $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
     $messageToken = CRM_Utils_Token::getTokens($mailingFormat);
     // also get all token values
     CRM_Utils_Hook::tokenValues($details[0], $this->_contactIds, NULL, $messageToken, 'CRM_Contact_Form_Task_Label');
     $tokens = array();
     CRM_Utils_Hook::tokens($tokens);
     $tokenFields = array();
     foreach ($tokens as $category => $catTokens) {
         foreach ($catTokens as $token => $tokenName) {
             $tokenFields[] = $token;
         }
     }
     foreach ($this->_contactIds as $value) {
         foreach ($custom as $cfID) {
             if (isset($details[0][$value]["custom_{$cfID}"])) {
                 $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($details[0][$value]["custom_{$cfID}"], $cfID);
             }
         }
         $contact = CRM_Utils_Array::value($value, $details['0']);
         if (is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         // we need to remove all the "_id"
         unset($contact['contact_id']);
         if ($locName && !empty($contact[$locName])) {
             // If location type is not primary, $contact contains
             // one more array as "$contact[$locName] = array( values... )"
             if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
                 continue;
             }
             $contact = array_merge($contact, $contact[$locName]);
             unset($contact[$locName]);
             if (!empty($contact['county_id'])) {
                 unset($contact['county_id']);
             }
             foreach ($contact as $field => $fieldValue) {
                 $rows[$value][$field] = $fieldValue;
             }
             $valuesothers = array();
             $paramsothers = array('contact_id' => $value);
             $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers);
             if (!empty($fv['location_type_id'])) {
                 foreach ($valuesothers as $vals) {
                     if (CRM_Utils_Array::value('location_type_id', $vals) == CRM_Utils_Array::value('location_type_id', $fv)) {
                         foreach ($vals as $k => $v) {
                             if (in_array($k, array('email', 'phone', 'im', 'openid'))) {
                                 if ($k == 'im') {
                                     $rows[$value][$k] = $v['1']['name'];
                                 } else {
                                     $rows[$value][$k] = $v['1'][$k];
                                 }
                                 $rows[$value][$k . '_id'] = $v['1']['id'];
                             }
                         }
                     }
                 }
             }
         } else {
             if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
                 continue;
             }
             if (!empty($contact['addressee_display'])) {
                 $contact['addressee_display'] = trim($contact['addressee_display']);
             }
             if (!empty($contact['addressee'])) {
                 $contact['addressee'] = $contact['addressee_display'];
             }
             // now create the rows for generating mailing labels
             foreach ($contact as $field => $fieldValue) {
                 $rows[$value][$field] = $fieldValue;
             }
         }
     }
     if (isset($fv['merge_same_address'])) {
         CRM_Core_BAO_Address::mergeSameAddress($rows);
         $individualFormat = TRUE;
     }
     // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
     foreach ($rows as $id => $row) {
         if ($commMethods = CRM_Utils_Array::value('preferred_communication_method', $row)) {
             $val = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $commMethods));
             $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
             $temp = array();
             foreach ($val as $vals) {
                 $temp[] = $comm[$vals];
             }
             $row['preferred_communication_method'] = implode(', ', $temp);
         }
         $row['id'] = $id;
         $formatted = CRM_Utils_Address::format($row, 'mailing_format', FALSE, TRUE, $individualFormat, $tokenFields);
         // CRM-2211: UFPDF doesn't have bidi support; use the PECL fribidi package to fix it.
         // On Ubuntu (possibly Debian?) be aware of http://pecl.php.net/bugs/bug.php?id=12366
         // Due to FriBidi peculiarities, this can't be called on
         // a multi-line string, hence the explode+implode approach.
         if (function_exists('fribidi_log2vis')) {
             $lines = explode("\n", $formatted);
             foreach ($lines as $i => $line) {
                 $lines[$i] = fribidi_log2vis($line, FRIBIDI_AUTO, FRIBIDI_CHARSET_UTF8);
             }
             $formatted = implode("\n", $lines);
         }
         $rows[$id] = array($formatted);
     }
     //call function to create labels
     self::createLabel($rows, $fv['label_name']);
     CRM_Utils_System::civiExit(1);
 }
예제 #14
0
 /**
  * Get the rows for the labels.
  *
  * @param $contactIDs
  * @param int $locationTypeID
  * @param bool $respectDoNotMail
  * @param $mergeSameAddress
  * @param bool $mergeSameHousehold
  *   UNUSED.
  *
  * @return array
  *   Array of rows for labels
  */
 public static function getRows($contactIDs, $locationTypeID, $respectDoNotMail, $mergeSameAddress, $mergeSameHousehold)
 {
     $locName = NULL;
     //get the address format sequence from the config file
     $addressReturnProperties = CRM_Contact_Form_Task_LabelCommon::getAddressReturnProperties();
     //build the return properties
     $returnProperties = array('display_name' => 1, 'contact_type' => 1, 'prefix_id' => 1);
     $mailingFormat = Civi::settings()->get('mailing_format');
     $mailingFormatProperties = array();
     if ($mailingFormat) {
         $mailingFormatProperties = CRM_Utils_Token::getReturnProperties($mailingFormat);
         $returnProperties = array_merge($returnProperties, $mailingFormatProperties);
     }
     $customFormatProperties = array();
     if (stristr($mailingFormat, 'custom_')) {
         foreach ($mailingFormatProperties as $token => $true) {
             if (substr($token, 0, 7) == 'custom_') {
                 if (empty($customFormatProperties[$token])) {
                     $customFormatProperties[$token] = $mailingFormatProperties[$token];
                 }
             }
         }
     }
     $returnProperties = array_merge($returnProperties, $customFormatProperties);
     if ($mergeSameAddress) {
         // we need first name/last name for summarising to avoid spillage
         $returnProperties['first_name'] = 1;
         $returnProperties['last_name'] = 1;
     }
     //get the contacts information
     $params = $custom = array();
     foreach ($contactIDs as $key => $contactID) {
         $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0);
     }
     // fix for CRM-2651
     if (!empty($respectDoNotMail['do_not_mail'])) {
         $params[] = array('do_not_mail', '=', 0, 0, 0);
     }
     // fix for CRM-2613
     $params[] = array('is_deceased', '=', 0, 0, 0);
     if ($locationTypeID) {
         $locType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
         $locName = $locType[$locationTypeID];
         $location = array('location' => array("{$locName}" => $addressReturnProperties));
         $returnProperties = array_merge($returnProperties, $location);
         $params[] = array('location_type', '=', array($locationTypeID => 1), 0, 0);
     } else {
         $returnProperties = array_merge($returnProperties, $addressReturnProperties);
     }
     foreach ($returnProperties as $name) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         if ($cfID) {
             $custom[] = $cfID;
         }
     }
     //get the total number of contacts to fetch from database.
     $numberofContacts = count($contactIDs);
     //this does the same as calling civicrm_api3('contact, get, array('id' => array('IN' => $this->_contactIds)
     // except it also handles multiple locations
     $query = new CRM_Contact_BAO_Query($params, $returnProperties);
     $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
     $messageToken = CRM_Utils_Token::getTokens($mailingFormat);
     $details = $details[0];
     $tokenFields = CRM_Contact_Form_Task_LabelCommon::getTokenData($details);
     foreach ($contactIDs as $value) {
         foreach ($custom as $cfID) {
             if (isset($details[$value]["custom_{$cfID}"])) {
                 $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($details[$value]["custom_{$cfID}"], $cfID);
             }
         }
         $contact = CRM_Utils_Array::value($value, $details);
         if (is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         // we need to remove all the "_id"
         unset($contact['contact_id']);
         if ($locName && !empty($contact[$locName])) {
             // If location type is not primary, $contact contains
             // one more array as "$contact[$locName] = array( values... )"
             if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
                 continue;
             }
             unset($contact[$locName]);
             if (!empty($contact['county_id'])) {
                 unset($contact['county_id']);
             }
             foreach ($contact as $field => $fieldValue) {
                 $rows[$value][$field] = $fieldValue;
             }
             $valuesothers = array();
             $paramsothers = array('contact_id' => $value);
             $valuesothers = CRM_Core_BAO_Location::getValues($paramsothers, $valuesothers);
             if ($locationTypeID) {
                 foreach ($valuesothers as $vals) {
                     if (CRM_Utils_Array::value('location_type_id', $vals) == $locationTypeID) {
                         foreach ($vals as $k => $v) {
                             if (in_array($k, array('email', 'phone', 'im', 'openid'))) {
                                 if ($k == 'im') {
                                     $rows[$value][$k] = $v['1']['name'];
                                 } else {
                                     $rows[$value][$k] = $v['1'][$k];
                                 }
                                 $rows[$value][$k . '_id'] = $v['1']['id'];
                             }
                         }
                     }
                 }
             }
         } else {
             if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) {
                 continue;
             }
             if (!empty($contact['addressee_display'])) {
                 $contact['addressee_display'] = trim($contact['addressee_display']);
             }
             if (!empty($contact['addressee'])) {
                 $contact['addressee'] = $contact['addressee_display'];
             }
             // now create the rows for generating mailing labels
             foreach ($contact as $field => $fieldValue) {
                 $rows[$value][$field] = $fieldValue;
             }
         }
     }
     // sigh couldn't extract out tokenfields yet
     return array($rows, $tokenFields);
 }
예제 #15
0
 /**
  * Given a contact id and a field set, return the values from the db
  * for this contact
  *
  * @param int $cid
  * @param array $fields
  *   The profile fields of interest.
  * @param array $values
  *   The values for the above fields.
  * @param bool $searchable
  *   Searchable or not.
  * @param array $componentWhere
  *   Component condition.
  * @param bool $absolute
  *   Return urls in absolute form (useful when sending an email).
  * @param null $additionalWhereClause
  */
 public static function getValues($cid, &$fields, &$values, $searchable = TRUE, $componentWhere = NULL, $absolute = FALSE, $additionalWhereClause = NULL)
 {
     if (empty($cid) && empty($componentWhere)) {
         return NULL;
     }
     // get the contact details (hier)
     $returnProperties = CRM_Contact_BAO_Contact::makeHierReturnProperties($fields);
     $params = $cid ? array(array('contact_id', '=', $cid, 0, 0)) : array();
     // add conditions specified by components. eg partcipant_id etc
     if (!empty($componentWhere)) {
         $params = array_merge($params, $componentWhere);
     }
     $query = new CRM_Contact_BAO_Query($params, $returnProperties, $fields);
     $details = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $additionalWhereClause);
     if (!$details->fetch()) {
         return;
     }
     $query->convertToPseudoNames($details);
     $config = CRM_Core_Config::singleton();
     $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
     $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
     $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
     $multipleFields = array('url');
     //start of code to set the default values
     foreach ($fields as $name => $field) {
         // fix for CRM-3962
         if ($name == 'id') {
             $name = 'contact_id';
         }
         // skip fields that should not be displayed separately
         if (!empty($field['skipDisplay'])) {
             continue;
         }
         // Create a unique, non-empty index for each field.
         $index = $field['title'];
         if ($index === '') {
             $index = ' ';
         }
         while (array_key_exists($index, $values)) {
             $index .= ' ';
         }
         $params[$index] = $values[$index] = '';
         $customFieldName = NULL;
         // hack for CRM-665
         if (isset($details->{$name}) || $name == 'group' || $name == 'tag') {
             // to handle gender / suffix / prefix
             if (in_array(substr($name, 0, -3), array('gender', 'prefix', 'suffix'))) {
                 $params[$index] = $details->{$name};
                 $values[$index] = $details->{$name};
             } elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)) {
                 $dname = $name . '_display';
                 $values[$index] = $details->{$dname};
                 $name = $name . '_id';
                 $params[$index] = $details->{$name};
             } elseif (in_array($name, array('state_province', 'country', 'county'))) {
                 $values[$index] = $details->{$name};
                 $idx = $name . '_id';
                 $params[$index] = $details->{$idx};
             } elseif ($name === 'preferred_communication_method') {
                 $communicationFields = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
                 $compref = array();
                 $pref = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details->{$name});
                 foreach ($pref as $k) {
                     if ($k) {
                         $compref[] = $communicationFields[$k];
                     }
                 }
                 $params[$index] = $details->{$name};
                 $values[$index] = implode(',', $compref);
             } elseif ($name === 'preferred_language') {
                 $params[$index] = $details->{$name};
                 $values[$index] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $details->{$name});
             } elseif ($name == 'group') {
                 $groups = CRM_Contact_BAO_GroupContact::getContactGroup($cid, 'Added', NULL, FALSE, TRUE);
                 $title = $ids = array();
                 foreach ($groups as $g) {
                     // CRM-8362: User and User Admin visibility groups should be included in display if user has
                     // VIEW permission on that group
                     $groupPerm = CRM_Contact_BAO_Group::checkPermission($g['group_id'], $g['title']);
                     if ($g['visibility'] != 'User and User Admin Only' || CRM_Utils_Array::key(CRM_Core_Permission::VIEW, $groupPerm)) {
                         $title[] = $g['title'];
                         if ($g['visibility'] == 'Public Pages') {
                             $ids[] = $g['group_id'];
                         }
                     }
                 }
                 $values[$index] = implode(', ', $title);
                 $params[$index] = implode(',', $ids);
             } elseif ($name == 'tag') {
                 $entityTags = CRM_Core_BAO_EntityTag::getTag($cid);
                 $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
                 $title = array();
                 foreach ($entityTags as $tagId) {
                     $title[] = $allTags[$tagId];
                 }
                 $values[$index] = implode(', ', $title);
                 $params[$index] = implode(',', $entityTags);
             } elseif ($name == 'activity_status_id') {
                 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
                 $values[$index] = $activityStatus[$details->{$name}];
                 $params[$index] = $details->{$name};
             } elseif ($name == 'activity_date_time') {
                 $values[$index] = CRM_Utils_Date::customFormat($details->{$name});
                 $params[$index] = $details->{$name};
             } elseif ($name == 'contact_sub_type') {
                 $contactSubTypeNames = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details->{$name});
                 if (!empty($contactSubTypeNames)) {
                     $contactSubTypeLabels = array();
                     // get all contact subtypes
                     $allContactSubTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
                     // build contact subtype labels array
                     foreach ($contactSubTypeNames as $cstName) {
                         if ($cstName) {
                             $contactSubTypeLabels[] = $allContactSubTypes[$cstName]['label'];
                         }
                     }
                     $values[$index] = implode(',', $contactSubTypeLabels);
                 }
                 $params[$index] = $details->{$name};
             } else {
                 if (substr($name, 0, 7) === 'do_not_' || substr($name, 0, 3) === 'is_') {
                     if ($details->{$name}) {
                         $values[$index] = '[ x ]';
                     }
                 } else {
                     if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) {
                         $htmlType = $field['html_type'];
                         // field_type is only set when we are retrieving profile values
                         // when sending email, we call the same function to get custom field
                         // values etc, i.e. emulating a profile
                         $fieldType = CRM_Utils_Array::value('field_type', $field);
                         if ($htmlType == 'File') {
                             $entityId = $cid;
                             if (!$cid && $fieldType == 'Activity' && !empty($componentWhere[0][2])) {
                                 $entityId = $componentWhere[0][2];
                             }
                             $fileURL = CRM_Core_BAO_CustomField::getFileURL($entityId, $cfID, NULL, $absolute, $additionalWhereClause);
                             $params[$index] = $values[$index] = $fileURL['file_url'];
                         } else {
                             $customVal = NULL;
                             if (isset($dao) && property_exists($dao, 'data_type') && ($dao->data_type == 'Int' || $dao->data_type == 'Boolean')) {
                                 $customVal = (int) $details->{$name};
                             } elseif (isset($dao) && property_exists($dao, 'data_type') && $dao->data_type == 'Float') {
                                 $customVal = (double) $details->{$name};
                             } elseif (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $details->{$name}))) {
                                 $customVal = $details->{$name};
                             }
                             //CRM-4582
                             if (CRM_Utils_System::isNull($customVal)) {
                                 continue;
                             }
                             $params[$index] = $customVal;
                             $values[$index] = CRM_Core_BAO_CustomField::displayValue($customVal, $cfID);
                             if ($field['data_type'] == 'ContactReference') {
                                 $params[$index] = $values[$index];
                             }
                             if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'is_search_range')) {
                                 $customFieldName = "{$name}_from";
                             }
                         }
                     } elseif ($name == 'image_URL') {
                         list($width, $height) = getimagesize(CRM_Utils_String::unstupifyUrl($details->{$name}));
                         list($thumbWidth, $thumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($width, $height);
                         $image_URL = '<img src="' . $details->{$name} . '" height= ' . $thumbHeight . ' width= ' . $thumbWidth . '  />';
                         $values[$index] = "<a href='#' onclick='contactImagePopUp(\"{$details->{$name}}\", {$width}, {$height});'>{$image_URL}</a>";
                     } elseif (in_array($name, array('birth_date', 'deceased_date', 'membership_start_date', 'membership_end_date', 'join_date'))) {
                         $values[$index] = CRM_Utils_Date::customFormat($details->{$name});
                         $params[$index] = CRM_Utils_Date::isoToMysql($details->{$name});
                     } else {
                         $dao = '';
                         if ($index == 'Campaign') {
                             $dao = 'CRM_Campaign_DAO_Campaign';
                         } elseif ($index == 'Contribution Page') {
                             $dao = 'CRM_Contribute_DAO_ContributionPage';
                         }
                         if ($dao) {
                             $value = CRM_Core_DAO::getFieldValue($dao, $details->{$name}, 'title');
                         } else {
                             $value = $details->{$name};
                         }
                         $values[$index] = $value;
                     }
                 }
             }
         } elseif (strpos($name, '-') !== FALSE) {
             list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $name, 3);
             if (!in_array($fieldName, $multipleFields)) {
                 if ($id == 'Primary') {
                     // fix for CRM-1543
                     // not sure why we'd every use Primary location type id
                     // we need to fix the source if we are using it
                     // $locationTypeName = CRM_Contact_BAO_Contact::getPrimaryLocationType( $cid );
                     $locationTypeName = 1;
                 } else {
                     $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
                 }
                 if (!$locationTypeName) {
                     continue;
                 }
                 $detailName = "{$locationTypeName}-{$fieldName}";
                 $detailName = str_replace(' ', '_', $detailName);
                 if (in_array($fieldName, array('phone', 'im', 'email', 'openid'))) {
                     if ($type) {
                         $detailName .= "-{$type}";
                     }
                 }
                 if (in_array($fieldName, array('state_province', 'country', 'county'))) {
                     $values[$index] = $details->{$detailName};
                     $idx = $detailName . '_id';
                     $params[$index] = $details->{$idx};
                 } elseif ($fieldName == 'im') {
                     $providerId = $detailName . '-provider_id';
                     if (isset($imProviders[$details->{$providerId}])) {
                         $values[$index] = $details->{$detailName} . " (" . $imProviders[$details->{$providerId}] . ")";
                     } else {
                         $values[$index] = $details->{$detailName};
                     }
                     $params[$index] = $details->{$detailName};
                 } elseif ($fieldName == 'phone') {
                     $phoneExtField = str_replace('phone', 'phone_ext', $detailName);
                     if (isset($details->{$phoneExtField})) {
                         $values[$index] = $details->{$detailName} . " (" . $details->{$phoneExtField} . ")";
                     } else {
                         $values[$index] = $details->{$detailName};
                     }
                     $params[$index] = $details->{$detailName};
                 } else {
                     $values[$index] = $params[$index] = $details->{$detailName};
                 }
             } else {
                 $detailName = "website-{$id}-{$fieldName}";
                 $url = CRM_Utils_System::fixURL($details->{$detailName});
                 if ($details->{$detailName}) {
                     $websiteTypeId = "website-{$id}-website_type_id";
                     $websiteType = $websiteTypes[$details->{$websiteTypeId}];
                     $values[$index] = "<a href=\"{$url}\">{$details->{$detailName}} ( {$websiteType} )</a>";
                 } else {
                     $values[$index] = '';
                 }
             }
         }
         if (CRM_Utils_Array::value('visibility', $field) == 'Public Pages and Listings' && CRM_Core_Permission::check('profile listings and forms')) {
             if (CRM_Utils_System::isNull($params[$index])) {
                 $params[$index] = $values[$index];
             }
             if (!isset($params[$index])) {
                 continue;
             }
             if (!$customFieldName) {
                 $fieldName = $field['name'];
             } else {
                 $fieldName = $customFieldName;
             }
             $url = NULL;
             if (CRM_Core_BAO_CustomField::getKeyID($field['name'])) {
                 $htmlType = $field['html_type'];
                 if ($htmlType == 'Link') {
                     $url = $params[$index];
                 } elseif (in_array($htmlType, array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Multi-Select State/Province', 'Multi-Select Country'))) {
                     $valSeperator = CRM_Core_DAO::VALUE_SEPARATOR;
                     $selectedOptions = explode($valSeperator, $params[$index]);
                     foreach ($selectedOptions as $key => $multiOption) {
                         if ($multiOption) {
                             $url[] = CRM_Utils_System::url('civicrm/profile', 'reset=1&force=1&gid=' . $field['group_id'] . '&' . urlencode($fieldName) . '=' . urlencode($multiOption));
                         }
                     }
                 } else {
                     $url = CRM_Utils_System::url('civicrm/profile', 'reset=1&force=1&gid=' . $field['group_id'] . '&' . urlencode($fieldName) . '=' . urlencode($params[$index]));
                 }
             } else {
                 $url = CRM_Utils_System::url('civicrm/profile', 'reset=1&force=1&gid=' . $field['group_id'] . '&' . urlencode($fieldName) . '=' . urlencode($params[$index]));
             }
             if ($url && !empty($values[$index]) && $searchable) {
                 if (is_array($url) && !empty($url)) {
                     $links = array();
                     $eachMultiValue = explode(', ', $values[$index]);
                     foreach ($eachMultiValue as $key => $valueLabel) {
                         $links[] = '<a href="' . $url[$key] . '">' . $valueLabel . '</a>';
                     }
                     $values[$index] = implode(', ', $links);
                 } else {
                     $values[$index] = '<a href="' . $url . '">' . $values[$index] . '</a>';
                 }
             }
         }
     }
 }
예제 #16
0
 /**
  * @param int $clientID
  * @param $activityDAO
  * @param $activityTypeInfo
  *
  * @return array|null
  */
 public function getCustomData($clientID, $activityDAO, &$activityTypeInfo)
 {
     list($typeValues, $options, $sql) = $this->getActivityTypeCustomSQL($activityTypeInfo['id'], '%Y-%m-%d');
     $params = array(1 => array($activityDAO->id, 'Integer'));
     $customGroups = array();
     foreach ($sql as $tableName => $sqlClause) {
         $dao = CRM_Core_DAO::executeQuery($sqlClause, $params);
         if ($dao->fetch()) {
             $customGroup = array();
             foreach ($typeValues[$tableName] as $columnName => $typeValue) {
                 if (CRM_Utils_Array::value('type', $typeValue) == 'Date') {
                     $value = $dao->{$columnName};
                 } else {
                     $value = CRM_Core_BAO_CustomField::displayValue($dao->{$columnName}, $typeValue['fieldID']);
                 }
                 if ($value) {
                     // Note: this is already taken care in getDisplayValue above, but sometimes
                     // strings like '^A^A' creates problem. So to fix this special case -
                     if (strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
                         $value = trim($value, CRM_Core_DAO::VALUE_SEPARATOR);
                     }
                     if (CRM_Utils_Array::value('type', $typeValue) == 'String' || CRM_Utils_Array::value('type', $typeValue) == 'Memo') {
                         $value = $this->redact($value);
                     } elseif (CRM_Utils_Array::value('type', $typeValue) == 'File') {
                         $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $typeValue, 'entity_table');
                         $value = CRM_Core_BAO_File::attachmentInfo($tableName, $activityDAO->id);
                     } elseif (CRM_Utils_Array::value('type', $typeValue) == 'Link') {
                         $value = CRM_Utils_System::formatWikiURL($value);
                     }
                 }
                 //$typeValue
                 $customGroup[] = array('label' => $typeValue['label'], 'value' => $value, 'type' => $typeValue['type'], 'fieldID' => $typeValue['fieldID']);
             }
             $customGroups[$dao->groupTitle] = $customGroup;
         }
     }
     return empty($customGroups) ? NULL : $customGroups;
 }
예제 #17
0
 /**
  * Alter the way in which custom data fields are displayed.
  *
  * @param array $rows
  */
 public function alterCustomDataDisplay(&$rows)
 {
     // custom code to alter rows having custom values
     if (empty($this->_customGroupExtends)) {
         return;
     }
     $customFieldIds = array();
     foreach ($this->_params['fields'] as $fieldAlias => $value) {
         if ($fieldId = CRM_Core_BAO_CustomField::getKeyID($fieldAlias)) {
             $customFieldIds[$fieldAlias] = $fieldId;
         }
     }
     if (empty($customFieldIds)) {
         return;
     }
     // skip for type date and ContactReference since date format is already handled
     $query = "\nSELECT cg.table_name, cf.id\nFROM  civicrm_custom_field cf\nINNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id\nWHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND\n      cg.is_active = 1 AND\n      cf.is_active = 1 AND\n      cf.is_searchable = 1 AND\n      cf.data_type   NOT IN ('ContactReference', 'Date') AND\n      cf.id IN (" . implode(",", $customFieldIds) . ")";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $customFields[$dao->table_name . '_custom_' . $dao->id] = $dao->id;
     }
     $dao->free();
     $entryFound = FALSE;
     foreach ($rows as $rowNum => $row) {
         foreach ($row as $tableCol => $val) {
             if (array_key_exists($tableCol, $customFields)) {
                 $rows[$rowNum][$tableCol] = CRM_Core_BAO_CustomField::displayValue($val, $customFields[$tableCol]);
                 $entryFound = TRUE;
             }
         }
         // skip looking further in rows, if first row itself doesn't
         // have the column we need
         if (!$entryFound) {
             break;
         }
     }
 }
예제 #18
0
 /**
  * Gives required details of contacts in an indexed array format so we
  * can iterate in a nice loop and do token evaluation
  *
  * @param $contactIDs
  * @param array $returnProperties
  *   Of required properties.
  * @param bool $skipOnHold Don't return on_hold contact info also.
  *   Don't return on_hold contact info also.
  * @param bool $skipDeceased Don't return deceased contact info.
  *   Don't return deceased contact info.
  * @param array $extraParams
  *   Extra params.
  * @param array $tokens
  *   The list of tokens we've extracted from the content.
  * @param null $className
  * @param int $jobID
  *   The mailing list jobID - this is a legacy param.
  *
  * @return array
  */
 public static function getTokenDetails($contactIDs, $returnProperties = NULL, $skipOnHold = TRUE, $skipDeceased = TRUE, $extraParams = NULL, $tokens = array(), $className = NULL, $jobID = NULL)
 {
     if (empty($contactIDs)) {
         // putting a fatal here so we can track if/when this happens
         CRM_Core_Error::fatal();
     }
     // @todo this functions needs unit tests.
     $params = array();
     foreach ($contactIDs as $key => $contactID) {
         $params[] = array(CRM_Core_Form::CB_PREFIX . $contactID, '=', 1, 0, 0);
     }
     // fix for CRM-2613
     if ($skipDeceased) {
         $params[] = array('is_deceased', '=', 0, 0, 0);
     }
     //fix for CRM-3798
     if ($skipOnHold) {
         $params[] = array('on_hold', '=', 0, 0, 0);
     }
     if ($extraParams) {
         $params = array_merge($params, $extraParams);
     }
     // if return properties are not passed then get all return properties
     if (empty($returnProperties)) {
         $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()), array('display_name', 'checksum', 'contact_id'));
         foreach ($fields as $key => $val) {
             // The unavailable fields are not available as tokens, do not have a one-2-one relationship
             // with contacts and are expensive to resolve.
             // @todo see CRM-17253 - there are some other fields (e.g note) that should be excluded
             // and upstream calls to this should populate return properties.
             $unavailableFields = array('group', 'tag');
             if (!in_array($val, $unavailableFields)) {
                 $returnProperties[$val] = 1;
             }
         }
     }
     $custom = array();
     foreach ($returnProperties as $name => $dontCare) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         if ($cfID) {
             $custom[] = $cfID;
         }
     }
     //get the total number of contacts to fetch from database.
     $numberofContacts = count($contactIDs);
     $query = new CRM_Contact_BAO_Query($params, $returnProperties);
     $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
     $contactDetails =& $details[0];
     foreach ($contactIDs as $key => $contactID) {
         if (array_key_exists($contactID, $contactDetails)) {
             if (CRM_Utils_Array::value('preferred_communication_method', $returnProperties) == 1 && array_key_exists('preferred_communication_method', $contactDetails[$contactID])) {
                 $pcm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
                 // communication Preference
                 $contactPcm = explode(CRM_Core_DAO::VALUE_SEPARATOR, $contactDetails[$contactID]['preferred_communication_method']);
                 $result = array();
                 foreach ($contactPcm as $key => $val) {
                     if ($val) {
                         $result[$val] = $pcm[$val];
                     }
                 }
                 $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $result);
             }
             foreach ($custom as $cfID) {
                 if (isset($contactDetails[$contactID]["custom_{$cfID}"])) {
                     $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($contactDetails[$contactID]["custom_{$cfID}"], $cfID);
                 }
             }
             // special case for greeting replacement
             foreach (array('email_greeting', 'postal_greeting', 'addressee') as $val) {
                 if (!empty($contactDetails[$contactID][$val])) {
                     $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
                 }
             }
         }
     }
     // also call a hook and get token details
     CRM_Utils_Hook::tokenValues($details[0], $contactIDs, $jobID, $tokens, $className);
     return $details;
 }
예제 #19
0
 /**
  * Process the form submission.
  */
 public function postProcess()
 {
     // get the submitted form values.
     $params = $this->controller->exportValues($this->_name);
     if ($this->_action & CRM_Core_Action::DELETE) {
         if (CRM_Utils_Array::value('delete_participant', $params) == 2) {
             $additionalId = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id);
             $participantLinks = CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId);
         }
         if (CRM_Utils_Array::value('delete_participant', $params) == 1) {
             $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id);
             foreach ($additionalIds as $value) {
                 CRM_Event_BAO_Participant::deleteParticipant($value);
             }
         }
         CRM_Event_BAO_Participant::deleteParticipant($this->_id);
         CRM_Core_Session::setStatus(ts('Selected participant was deleted successfully.'), ts('Record Deleted'), 'success');
         if (!empty($participantLinks)) {
             $status = ts('The following participants no longer have an event fee recorded. You can edit their registration and record a replacement contribution by clicking the links below:') . '<br/>' . $participantLinks;
             CRM_Core_Session::setStatus($status, ts('Group Payment Deleted'));
         }
         return;
     }
     // When adding a single contact, the formRule prevents you from adding duplicates
     // (See above in formRule()). When adding more than one contact, the duplicates are
     // removed automatically and the user receives one notification.
     if ($this->_action & CRM_Core_Action::ADD) {
         $event_id = $this->_eventId;
         if (empty($event_id) && !empty($params['event_id'])) {
             $event_id = $params['event_id'];
         }
         if (!$this->_single && !empty($event_id)) {
             $duplicateContacts = 0;
             while (list($k, $dupeCheckContactId) = each($this->_contactIds)) {
                 // Eliminate contacts that have already been assigned to this event.
                 $dupeCheck = new CRM_Event_BAO_Participant();
                 $dupeCheck->contact_id = $dupeCheckContactId;
                 $dupeCheck->event_id = $event_id;
                 $dupeCheck->find(TRUE);
                 if (!empty($dupeCheck->id)) {
                     $duplicateContacts++;
                     unset($this->_contactIds[$k]);
                 }
             }
             if ($duplicateContacts > 0) {
                 $msg = ts("%1 contacts have already been assigned to this event. They were not added a second time.", array(1 => $duplicateContacts));
                 CRM_Core_Session::setStatus($msg);
             }
             if (count($this->_contactIds) == 0) {
                 CRM_Core_Session::setStatus(ts("No participants were added."));
                 return;
             }
             // We have to re-key $this->_contactIds so each contact has the same
             // key as their corresponding record in the $participants array that
             // will be created below.
             $this->_contactIds = array_values($this->_contactIds);
         }
     }
     $participantStatus = CRM_Event_PseudoConstant::participantStatus();
     // set the contact, when contact is selected
     if (!empty($params['contact_id'])) {
         $this->_contactId = $params['contact_id'];
     }
     if ($this->_priceSetId && ($isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'))) {
         $this->_quickConfig = $isQuickConfig;
     }
     if ($this->_id) {
         $params['id'] = $this->_id;
     }
     $config = CRM_Core_Config::singleton();
     if ($this->_isPaidEvent) {
         $contributionParams = array();
         $lineItem = array();
         $additionalParticipantDetails = array();
         if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
             $eventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'start_date');
             if ($eventStartDate) {
                 $contributionParams['revenue_recognition_date'] = date('Ymd', strtotime($eventStartDate));
             }
         }
         if ($this->_id && $this->_action & CRM_Core_Action::UPDATE && $this->_paymentId) {
             $participantBAO = new CRM_Event_BAO_Participant();
             $participantBAO->id = $this->_id;
             $participantBAO->find(TRUE);
             $contributionParams['total_amount'] = $participantBAO->fee_amount;
             $params['discount_id'] = NULL;
             //re-enter the values for UPDATE mode
             $params['fee_level'] = $params['amount_level'] = $participantBAO->fee_level;
             $params['fee_amount'] = $participantBAO->fee_amount;
             if (isset($params['priceSetId'])) {
                 $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id);
             }
             //also add additional participant's fee level/priceset
             if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) {
                 $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id);
                 $hasLineItems = CRM_Utils_Array::value('priceSetId', $params, FALSE);
                 $additionalParticipantDetails = CRM_Event_BAO_Participant::getFeeDetails($additionalIds, $hasLineItems);
             }
         } else {
             //check if discount is selected
             if (!empty($params['discount_id'])) {
                 $discountId = $params['discount_id'];
             } else {
                 $discountId = $params['discount_id'] = 'null';
             }
             //lets carry currency, CRM-4453
             $params['fee_currency'] = $config->defaultCurrency;
             CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[0]);
             //CRM-11529 for quick config backoffice transactions
             //when financial_type_id is passed in form, update the
             //lineitems with the financial type selected in form
             $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $params);
             $isPaymentRecorded = CRM_Utils_Array::value('record_contribution', $params);
             if ($isPaymentRecorded && $this->_quickConfig && $submittedFinancialType) {
                 foreach ($lineItem[0] as &$values) {
                     $values['financial_type_id'] = $submittedFinancialType;
                 }
             }
             $params['fee_level'] = $params['amount_level'];
             $contributionParams['total_amount'] = $params['amount'];
             if ($this->_quickConfig && !empty($params['total_amount']) && $params['status_id'] != array_search('Partially paid', $participantStatus)) {
                 $params['fee_amount'] = $params['total_amount'];
             } else {
                 //fix for CRM-3086
                 $params['fee_amount'] = $params['amount'];
             }
         }
         if (isset($params['priceSetId'])) {
             if (!empty($lineItem[0])) {
                 $this->set('lineItem', $lineItem);
                 $this->_lineItem = $lineItem;
                 $lineItem = array_merge($lineItem, $additionalParticipantDetails);
                 $participantCount = array();
                 foreach ($lineItem as $k) {
                     foreach ($k as $v) {
                         if (CRM_Utils_Array::value('participant_count', $v) > 0) {
                             $participantCount[] = $v['participant_count'];
                         }
                     }
                 }
             }
             if (isset($participantCount)) {
                 $this->assign('pricesetFieldsCount', $participantCount);
             }
             $this->assign('lineItem', empty($lineItem[0]) || $this->_quickConfig ? FALSE : $lineItem);
         } else {
             $this->assign('amount_level', $params['amount_level']);
         }
     }
     $this->_params = $params;
     $amountOwed = NULL;
     if (isset($params['amount'])) {
         $amountOwed = $params['amount'];
         unset($params['amount']);
     }
     $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']);
     $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params), CRM_Utils_Array::value('receive_date_time', $params));
     $params['contact_id'] = $this->_contactId;
     // overwrite actual payment amount if entered
     if (!empty($params['total_amount'])) {
         $contributionParams['total_amount'] = CRM_Utils_Array::value('total_amount', $params);
     }
     // Retrieve the name and email of the current user - this will be the FROM for the receipt email
     $userName = CRM_Core_Session::singleton()->getLoggedInContactDisplayName();
     if ($this->_contactId) {
         list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
     }
     //modify params according to parameter used in create
     //participant method (addParticipant)
     $this->_params['participant_status_id'] = $params['status_id'];
     $this->_params['participant_role_id'] = is_array($params['role_id']) ? $params['role_id'] : explode(',', $params['role_id']);
     $this->_params['participant_register_date'] = $params['register_date'];
     $roleIdWithSeparator = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['participant_role_id']);
     if ($this->_mode) {
         if (!$this->_isPaidEvent) {
             CRM_Core_Error::fatal(ts('Selected Event is not Paid Event '));
         }
         $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'title');
         // set source if not set
         if (empty($params['source'])) {
             $this->_params['participant_source'] = ts('Offline Registration for Event: %2 by: %1', array(1 => $userName, 2 => $eventTitle));
         } else {
             $this->_params['participant_source'] = $params['source'];
         }
         $this->_params['description'] = $this->_params['participant_source'];
         $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], $this->_mode);
         $now = date('YmdHis');
         $fields = array();
         // set email for primary location.
         $fields['email-Primary'] = 1;
         $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail;
         $params['register_date'] = $now;
         // now set the values for the billing location.
         foreach ($this->_fields as $name => $dontCare) {
             $fields[$name] = 1;
         }
         // also add location name to the array
         $params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params);
         $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
         $fields["address_name-{$this->_bltID}"] = 1;
         $fields["email-{$this->_bltID}"] = 1;
         $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type');
         $nameFields = array('first_name', 'middle_name', 'last_name');
         foreach ($nameFields as $name) {
             $fields[$name] = 1;
             if (array_key_exists("billing_{$name}", $params)) {
                 $params[$name] = $params["billing_{$name}"];
                 $params['preserveDBName'] = TRUE;
             }
         }
         $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype);
     }
     if (!empty($this->_params['participant_role_id'])) {
         $customFieldsRole = array();
         foreach ($this->_params['participant_role_id'] as $roleKey) {
             $customFieldsRole = CRM_Utils_Array::crmArrayMerge(CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $roleKey, $this->_roleCustomDataTypeID), $customFieldsRole);
         }
         $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, CRM_Utils_Array::value('event_id', $params), $this->_eventNameCustomDataTypeID);
         $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $this->_eventTypeId, $this->_eventTypeCustomDataTypeID);
         $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE));
         $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields);
         $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields);
         $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, 'Participant');
     }
     //do cleanup line  items if participant edit the Event Fee.
     if (($this->_lineItem || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) {
         CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant');
     }
     if ($this->_mode) {
         // add all the additional payment params we need
         $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]);
         $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
         $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params);
         $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params);
         $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
         $this->_params['amount'] = $params['fee_amount'];
         $this->_params['amount_level'] = $params['amount_level'];
         $this->_params['currencyID'] = $config->defaultCurrency;
         $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
         // at this point we've created a contact and stored its address etc
         // all the payment processors expect the name and address to be in the
         // so we copy stuff over to first_name etc.
         $paymentParams = $this->_params;
         if (!empty($this->_params['send_receipt'])) {
             $paymentParams['email'] = $this->_contributorEmail;
         }
         // The only reason for merging in the 'contact_id' rather than ensuring it is set
         // is that this patch is being done around the time of the stable release
         // so more conservative approach is called for.
         // In fact the use of $params and $this->_params & $this->_contactId vs $contactID
         // needs rationalising.
         $mapParams = array_merge(array('contact_id' => $contactID), $this->_params);
         CRM_Core_Payment_Form::mapParams($this->_bltID, $mapParams, $paymentParams, TRUE);
         $payment = $this->_paymentProcessor['object'];
         // CRM-15622: fix for incorrect contribution.fee_amount
         $paymentParams['fee_amount'] = NULL;
         $result = $payment->doPayment($paymentParams);
         if (is_a($result, 'CRM_Core_Error')) {
             CRM_Core_Error::displaySessionError($result);
             CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/participant', "reset=1&action=add&cid={$this->_contactId}&context=participant&mode={$this->_mode}"));
         }
         if ($result) {
             $this->_params = array_merge($this->_params, $result);
         }
         $this->_params['receive_date'] = $now;
         if (!empty($this->_params['send_receipt'])) {
             $this->_params['receipt_date'] = $now;
         } else {
             $this->_params['receipt_date'] = NULL;
         }
         $this->set('params', $this->_params);
         $this->assign('trxn_id', $result['trxn_id']);
         $this->assign('receive_date', CRM_Utils_Date::processDate($this->_params['receive_date']));
         //add contribution record
         $this->_params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'financial_type_id');
         $this->_params['mode'] = $this->_mode;
         //add contribution record
         $contributions[] = $contribution = CRM_Event_Form_Registration_Confirm::processContribution($this, $this->_params, $result, $contactID, FALSE);
         // add participant record
         $participants = array();
         if (!empty($this->_params['role_id']) && is_array($this->_params['role_id'])) {
             $this->_params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['role_id']);
         }
         //CRM-15372 patch to fix fee amount replacing amount
         $this->_params['fee_amount'] = $this->_params['amount'];
         $participants[] = CRM_Event_Form_Registration::addParticipant($this, $contactID);
         //add custom data for participant
         CRM_Core_BAO_CustomValueTable::postProcess($this->_params, 'civicrm_participant', $participants[0]->id, 'Participant');
         //add participant payment
         $paymentParticipant = array('participant_id' => $participants[0]->id, 'contribution_id' => $contribution->id);
         $ids = array();
         CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids);
         $this->_contactIds[] = $this->_contactId;
     } else {
         $participants = array();
         if ($this->_single) {
             if ($params['role_id']) {
                 $params['role_id'] = $roleIdWithSeparator;
             } else {
                 $params['role_id'] = 'NULL';
             }
             $participants[] = CRM_Event_BAO_Participant::create($params);
         } else {
             foreach ($this->_contactIds as $contactID) {
                 $commonParams = $params;
                 $commonParams['contact_id'] = $contactID;
                 if ($commonParams['role_id']) {
                     $commonParams['role_id'] = $commonParams['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']);
                 } else {
                     $commonParams['role_id'] = 'NULL';
                 }
                 $participants[] = CRM_Event_BAO_Participant::create($commonParams);
             }
         }
         if (isset($params['event_id'])) {
             $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'title');
         }
         if ($this->_single) {
             $this->_contactIds[] = $this->_contactId;
         }
         $contributions = array();
         if (!empty($params['record_contribution'])) {
             if (!empty($params['id'])) {
                 if ($this->_onlinePendingContributionId) {
                     $ids['contribution'] = $this->_onlinePendingContributionId;
                 } else {
                     $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $params['id'], 'contribution_id', 'participant_id');
                 }
             }
             unset($params['note']);
             //build contribution params
             if (!$this->_onlinePendingContributionId) {
                 if (empty($params['source'])) {
                     $contributionParams['source'] = ts('%1 : Offline registration (by %2)', array(1 => $eventTitle, 2 => $userName));
                 } else {
                     $contributionParams['source'] = $params['source'];
                 }
             }
             $contributionParams['currency'] = $config->defaultCurrency;
             $contributionParams['non_deductible_amount'] = 'null';
             $contributionParams['receipt_date'] = !empty($params['send_receipt']) ? CRM_Utils_Array::value('receive_date', $params) : 'null';
             $recordContribution = array('contact_id', 'financial_type_id', 'payment_instrument_id', 'trxn_id', 'contribution_status_id', 'receive_date', 'check_number', 'campaign_id');
             foreach ($recordContribution as $f) {
                 $contributionParams[$f] = CRM_Utils_Array::value($f, $params);
                 if ($f == 'trxn_id') {
                     $this->assign('trxn_id', $contributionParams[$f]);
                 }
             }
             //insert financial type name in receipt.
             $this->assign('financialTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id']));
             // legacy support
             $this->assign('contributionTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id']));
             $contributionParams['skipLineItem'] = 1;
             if ($this->_id) {
                 $contributionParams['contribution_mode'] = 'participant';
                 $contributionParams['participant_id'] = $this->_id;
             }
             // Set is_pay_later flag for back-office offline Pending status contributions
             if ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) {
                 $contributionParams['is_pay_later'] = 1;
             } elseif ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) {
                 $contributionParams['is_pay_later'] = 0;
             }
             if ($params['status_id'] == array_search('Partially paid', $participantStatus)) {
                 if (!$amountOwed && $this->_action & CRM_Core_Action::UPDATE) {
                     $amountOwed = $params['fee_amount'];
                 }
                 // if multiple participants are link, consider contribution total amount as the amount Owed
                 if ($this->_id && CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) {
                     $amountOwed = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $ids['contribution'], 'total_amount');
                 }
                 // CRM-13964 partial_payment_total
                 if ($amountOwed > $params['total_amount']) {
                     // the owed amount
                     $contributionParams['partial_payment_total'] = $amountOwed;
                     // the actual amount paid
                     $contributionParams['partial_amount_pay'] = $params['total_amount'];
                 }
             }
             if (CRM_Utils_Array::value('tax_amount', $this->_params)) {
                 $contributionParams['tax_amount'] = $this->_params['tax_amount'];
             }
             if ($this->_single) {
                 if (empty($ids)) {
                     $ids = array();
                 }
                 $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids);
             } else {
                 $ids = array();
                 foreach ($this->_contactIds as $contactID) {
                     $contributionParams['contact_id'] = $contactID;
                     $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids);
                 }
             }
             //insert payment record for this participation
             if (empty($ids['contribution'])) {
                 foreach ($this->_contactIds as $num => $contactID) {
                     $ppDAO = new CRM_Event_DAO_ParticipantPayment();
                     $ppDAO->participant_id = $participants[$num]->id;
                     $ppDAO->contribution_id = $contributions[$num]->id;
                     $ppDAO->save();
                 }
             }
             // next create the transaction record
             $transaction = new CRM_Core_Transaction();
             // CRM-11124
             if ($this->_params['discount_id']) {
                 CRM_Event_BAO_Participant::createDiscountTrxn($this->_eventId, $contributionParams, NULL, CRM_Price_BAO_PriceSet::parseFirstPriceSetValueIDFromParams($this->_params));
             }
             $transaction->commit();
         }
     }
     // also store lineitem stuff here
     if ($this->_lineItem & $this->_action & CRM_Core_Action::ADD || $this->_lineItem && CRM_Core_Action::UPDATE && !$this->_paymentId) {
         foreach ($this->_contactIds as $num => $contactID) {
             foreach ($this->_lineItem as $key => $value) {
                 if (is_array($value) && $value != 'skip') {
                     foreach ($value as $lineKey => $line) {
                         //10117 update the line items for participants if contribution amount is recorded
                         if ($this->_quickConfig && !empty($params['total_amount']) && $params['status_id'] != array_search('Partially paid', $participantStatus)) {
                             $line['unit_price'] = $line['line_total'] = $params['total_amount'];
                             if (!empty($params['tax_amount'])) {
                                 $line['unit_price'] = $line['unit_price'] - $params['tax_amount'];
                                 $line['line_total'] = $line['line_total'] - $params['tax_amount'];
                             }
                         }
                         $lineItem[$this->_priceSetId][$lineKey] = $line;
                     }
                     CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant');
                     CRM_Contribute_BAO_Contribution::addPayments($value, $contributions);
                 }
             }
         }
     }
     $updateStatusMsg = NULL;
     //send mail when participant status changed, CRM-4326
     if ($this->_id && $this->_statusId && $this->_statusId != CRM_Utils_Array::value('status_id', $params) && !empty($params['is_notify'])) {
         $updateStatusMsg = CRM_Event_BAO_Participant::updateStatusMessage($this->_id, $params['status_id'], $this->_statusId);
     }
     $sent = array();
     $notSent = array();
     if (!empty($params['send_receipt'])) {
         if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
             $receiptFrom = $params['from_email_address'];
         }
         $this->assign('module', 'Event Registration');
         //use of the message template below requires variables in different format
         $event = $events = array();
         $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title');
         //get all event details.
         CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties);
         $event = $events[$params['event_id']];
         unset($event['start_date']);
         unset($event['end_date']);
         $role = CRM_Event_PseudoConstant::participantRole();
         $participantRoles = CRM_Utils_Array::value('role_id', $params);
         if (is_array($participantRoles)) {
             $selectedRoles = array();
             foreach ($participantRoles as $roleId) {
                 $selectedRoles[] = $role[$roleId];
             }
             $event['participant_role'] = implode(', ', $selectedRoles);
         } else {
             $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role);
         }
         $event['is_monetary'] = $this->_isPaidEvent;
         if ($params['receipt_text']) {
             $event['confirm_email_text'] = $params['receipt_text'];
         }
         $this->assign('isAmountzero', 1);
         $this->assign('event', $event);
         $this->assign('isShowLocation', $event['is_show_location']);
         if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
             $locationParams = array('entity_id' => $params['event_id'], 'entity_table' => 'civicrm_event');
             $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
             $this->assign('location', $location);
         }
         $status = CRM_Event_PseudoConstant::participantStatus();
         if ($this->_isPaidEvent) {
             $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
             if (!$this->_mode) {
                 if (isset($params['payment_instrument_id'])) {
                     $this->assign('paidBy', CRM_Utils_Array::value($params['payment_instrument_id'], $paymentInstrument));
                 }
             }
             $this->assign('totalAmount', $contributionParams['total_amount']);
             if (isset($contributionParams['partial_payment_total'])) {
                 // balance amount
                 $balanceAmount = $contributionParams['partial_payment_total'] - $contributionParams['partial_amount_pay'];
                 $this->assign('balanceAmount', $balanceAmount);
             }
             $this->assign('isPrimary', 1);
             $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
         }
         if ($this->_mode) {
             if (!empty($params['billing_first_name'])) {
                 $name = $params['billing_first_name'];
             }
             if (!empty($params['billing_middle_name'])) {
                 $name .= " {$params['billing_middle_name']}";
             }
             if (!empty($params['billing_last_name'])) {
                 $name .= " {$params['billing_last_name']}";
             }
             $this->assign('billingName', $name);
             // assign the address formatted up for display
             $addressParts = array("street_address-{$this->_bltID}", "city-{$this->_bltID}", "postal_code-{$this->_bltID}", "state_province-{$this->_bltID}", "country-{$this->_bltID}");
             $addressFields = array();
             foreach ($addressParts as $part) {
                 list($n, $id) = explode('-', $part);
                 if (isset($this->_params['billing_' . $part])) {
                     $addressFields[$n] = $this->_params['billing_' . $part];
                 }
             }
             $this->assign('address', CRM_Utils_Address::format($addressFields));
             $date = CRM_Utils_Date::format($params['credit_card_exp_date']);
             $date = CRM_Utils_Date::mysqlToIso($date);
             $this->assign('credit_card_exp_date', $date);
             $this->assign('credit_card_number', CRM_Utils_System::mungeCreditCard($params['credit_card_number']));
             $this->assign('credit_card_type', $params['credit_card_type']);
             // The concept of contributeMode is deprecated.
             $this->assign('contributeMode', 'direct');
             $this->assign('isAmountzero', 0);
             $this->assign('is_pay_later', 0);
             $this->assign('isPrimary', 1);
         }
         $this->assign('register_date', $params['register_date']);
         if ($params['receive_date']) {
             $this->assign('receive_date', $params['receive_date']);
         }
         $participant = array(array('participant_id', '=', $participants[0]->id, 0, 0));
         // check whether its a test drive ref CRM-3075
         if (!empty($this->_defaultValues['is_test'])) {
             $participant[] = array('participant_test', '=', 1, 0, 0);
         }
         $template = CRM_Core_Smarty::singleton();
         $customGroup = array();
         //format submitted data
         foreach ($params['custom'] as $fieldID => $values) {
             foreach ($values as $fieldValue) {
                 $customFields[$fieldID]['id'] = $fieldID;
                 $formattedValue = CRM_Core_BAO_CustomField::displayValue($fieldValue['value'], $fieldID, $participants[0]->id);
                 $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace('&nbsp;', '', $formattedValue);
             }
         }
         foreach ($this->_contactIds as $num => $contactID) {
             // Retrieve the name and email of the contact - this will be the TO for receipt email
             list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID);
             $this->_contributorDisplayName = $this->_contributorDisplayName == ' ' ? $this->_contributorEmail : $this->_contributorDisplayName;
             $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
             if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) {
                 $this->assign('isOnWaitlist', TRUE);
             }
             $this->assign('customGroup', $customGroup);
             $this->assign('contactID', $contactID);
             $this->assign('participantID', $participants[$num]->id);
             $this->_id = $participants[$num]->id;
             if ($this->_isPaidEvent) {
                 // fix amount for each of participants ( for bulk mode )
                 $eventAmount = array();
                 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
                 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
                 $totalTaxAmount = 0;
                 //add dataArray in the receipts in ADD and UPDATE condition
                 $dataArray = array();
                 if ($this->_action & CRM_Core_Action::ADD) {
                     $line = $lineItem[0];
                 } elseif ($this->_action & CRM_Core_Action::UPDATE) {
                     $line = $this->_values['line_items'];
                 }
                 if ($invoicing) {
                     foreach ($line as $key => $value) {
                         if (isset($value['tax_amount'])) {
                             $totalTaxAmount += $value['tax_amount'];
                             if (isset($dataArray[(string) $value['tax_rate']])) {
                                 $dataArray[(string) $value['tax_rate']] = $dataArray[(string) $value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value);
                             } else {
                                 $dataArray[(string) $value['tax_rate']] = CRM_Utils_Array::value('tax_amount', $value);
                             }
                         }
                     }
                     $this->assign('totalTaxAmount', $totalTaxAmount);
                     $this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings));
                     $this->assign('dataArray', $dataArray);
                 }
                 if (!empty($additionalParticipantDetails)) {
                     $params['amount_level'] = preg_replace('//', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName;
                 }
                 $eventAmount[$num] = array('label' => preg_replace('//', '', $params['amount_level']), 'amount' => $params['fee_amount']);
                 //as we are using same template for online & offline registration.
                 //So we have to build amount as array.
                 $eventAmount = array_merge($eventAmount, $additionalParticipantDetails);
                 $this->assign('amount', $eventAmount);
             }
             $sendTemplateParams = array('groupName' => 'msg_tpl_workflow_event', 'valueName' => 'event_offline_receipt', 'contactId' => $contactID, 'isTest' => !empty($this->_defaultValues['is_test']), 'PDFFilename' => ts('confirmation') . '.pdf');
             // try to send emails only if email id is present
             // and the do-not-email option is not checked for that contact
             if ($this->_contributorEmail and !$this->_toDoNotEmail) {
                 $sendTemplateParams['from'] = $receiptFrom;
                 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
                 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
                 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails);
                 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails);
             }
             //send email with pdf invoice
             $template = CRM_Core_Smarty::singleton();
             $taxAmt = $template->get_template_vars('dataArray');
             $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id');
             $prefixValue = Civi::settings()->get('contribution_invoice_settings');
             $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
             if (count($taxAmt) > 0 && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) {
                 $sendTemplateParams['isEmailPdf'] = TRUE;
                 $sendTemplateParams['contributionId'] = $contributionId;
             }
             list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
             if ($mailSent) {
                 $sent[] = $contactID;
                 foreach ($participants as $ids => $values) {
                     if ($values->contact_id == $contactID) {
                         $values->details = CRM_Utils_Array::value('receipt_text', $params);
                         CRM_Activity_BAO_Activity::addActivity($values, 'Email');
                         break;
                     }
                 }
             } else {
                 $notSent[] = $contactID;
             }
         }
     }
     // set the participant id if it is not set
     if (!$this->_id) {
         $this->_id = $participants[0]->id;
     }
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contributorDisplayName));
         if (!empty($params['send_receipt']) && count($sent)) {
             $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', array(1 => $this->_contributorEmail));
         }
         if ($updateStatusMsg) {
             $statusMsg = "{$statusMsg} {$updateStatusMsg}";
         }
     } elseif ($this->_action & CRM_Core_Action::ADD) {
         if ($this->_single) {
             $statusMsg = ts('Event registration for %1 has been added.', array(1 => $this->_contributorDisplayName));
             if (!empty($params['send_receipt']) && count($sent)) {
                 $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', array(1 => $this->_contributorEmail));
             }
         } else {
             $statusMsg = ts('Total Participant(s) added to event: %1.', array(1 => count($this->_contactIds)));
             if (count($notSent) > 0) {
                 $statusMsg .= ' ' . ts('Email has NOT been sent to %1 contact(s) - communication preferences specify DO NOT EMAIL OR valid Email is NOT present. ', array(1 => count($notSent)));
             } elseif (isset($params['send_receipt'])) {
                 $statusMsg .= ' ' . ts('A confirmation email has been sent to ALL participants');
             }
         }
     }
     CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
     $session = CRM_Core_Session::singleton();
     $buttonName = $this->controller->getButtonName();
     if ($this->_context == 'standalone') {
         if ($buttonName == $this->getButtonName('upload', 'new')) {
             $urlParams = 'reset=1&action=add&context=standalone';
             if ($this->_mode) {
                 $urlParams .= '&mode=' . $this->_mode;
             }
             if ($this->_eID) {
                 $urlParams .= '&eid=' . $this->_eID;
             }
             $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams));
         } else {
             $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactId}&selectedChild=participant"));
         }
     } elseif ($buttonName == $this->getButtonName('upload', 'new')) {
         $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}"));
     }
 }
예제 #20
0
 /**
  * @param $row
  */
 public function alterRow(&$row)
 {
     foreach ($row as $fieldName => &$field) {
         if (strpos($fieldName, 'custom_') === 0) {
             $field = CRM_Core_BAO_CustomField::displayValue($field, $fieldName);
         }
     }
 }