示例#1
0
 /**
  * Function to support legacy format for groups and tags.
  *
  * @param string $id
  * @param array|int $values
  *
  */
 public static function legacyConvertFormValues($id, &$values)
 {
     $legacyElements = array('group', 'tag', 'contact_tags', 'contact_type', 'membership_type_id', 'membership_status_id');
     if (in_array($id, $legacyElements) && is_array($values)) {
         // prior to 4.7, formValues for some attributes (e.g. group, tag) are stored in array(id1 => 1, id2 => 1),
         // as per the recent Search fixes $values need to be in standard array(id1, id2) format
         CRM_Utils_Array::formatArrayKeys($values);
     }
 }
 /**
  * Lower-level logic for rendering a custom field value
  *
  * @param string|array $value
  * @param array $field
  * @param int|null $entityId
  *
  * @return string
  */
 private static function formatDisplayValue($value, $field, $entityId = NULL)
 {
     if (self::isSerialized($field) && !is_array($value)) {
         $value = CRM_Utils_Array::explodePadded($value);
     }
     // CRM-12989 fix
     if ($field['html_type'] == 'CheckBox') {
         CRM_Utils_Array::formatArrayKeys($value);
     }
     $display = is_array($value) ? implode(', ', $value) : (string) $value;
     switch ($field['html_type']) {
         case 'Select':
         case 'Autocomplete-Select':
         case 'Radio':
         case 'Select Country':
         case 'Select State/Province':
         case 'CheckBox':
         case 'AdvMulti-Select':
         case 'Multi-Select':
         case 'Multi-Select State/Province':
         case 'Multi-Select Country':
             if ($field['data_type'] == 'ContactReference' && $value) {
                 $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name');
             } elseif (is_array($value)) {
                 $v = array();
                 foreach ($value as $key => $val) {
                     $v[] = CRM_Utils_Array::value($val, $field['options']);
                 }
                 $display = implode(', ', $v);
             } else {
                 $display = CRM_Utils_Array::value($value, $field['options'], '');
             }
             break;
         case 'Select Date':
             $customFormat = NULL;
             // FIXME: Are there any legitimate reasons why $value would be an array?
             // Or should we throw an exception here if it is?
             $value = is_array($value) ? CRM_Utils_Array::first($value) : $value;
             $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats();
             $format = CRM_Utils_Array::value('date_format', $field);
             if ($format) {
                 if (array_key_exists($format, $actualPHPFormats)) {
                     $customTimeFormat = (array) $actualPHPFormats[$format];
                     switch (CRM_Utils_Array::value('time_format', $field)) {
                         case 1:
                             $customTimeFormat[] = 'g:iA';
                             break;
                         case 2:
                             $customTimeFormat[] = 'G:i';
                             break;
                         default:
                             // if time is not selected remove time from value
                             $value = substr($value, 0, 10);
                     }
                     $customFormat = implode(" ", $customTimeFormat);
                 }
             }
             $display = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat);
             break;
         case 'File':
             // In the context of displaying a profile, show file/image
             if ($value) {
                 if ($entityId) {
                     $url = self::getFileURL($entityId, $field['id']);
                     if ($url) {
                         $display = $url['file_url'];
                     }
                 } else {
                     // In other contexts show a paperclip icon
                     if (CRM_Utils_Rule::integer($value)) {
                         $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
                         $display = $icons[$value];
                     } else {
                         //CRM-18396, if filename is passed instead
                         $display = $value;
                     }
                 }
             }
             break;
         case 'TextArea':
             $display = nl2br($display);
             break;
         case 'Text':
             if ($field['data_type'] == 'Money' && isset($value)) {
                 //$value can also be an array(while using IN operator from search builder or api).
                 foreach ((array) $value as $val) {
                     $disp[] = CRM_Utils_Money::format($val);
                 }
                 $display = implode(', ', $disp);
             }
             break;
     }
     return $display;
 }
示例#3
0
 /**
  * Get display value.
  *
  * @param string $value
  * @param array $option
  * @param string $html_type
  * @param string $data_type
  * @param int $contactID
  * @param int $fieldID
  *
  * @return array|mixed|null|string
  */
 public static function getDisplayValueCommon($value, &$option, $html_type, $data_type, $contactID = NULL, $fieldID = NULL)
 {
     $display = $value;
     if ($fieldID && ($html_type == 'Radio' && $data_type != 'Boolean' || $html_type == 'Autocomplete-Select' && $data_type != 'ContactReference' || $html_type == 'Select' || $html_type == 'CheckBox' || $html_type == 'AdvMulti-Select' || $html_type == 'Multi-Select')) {
         CRM_Utils_Hook::customFieldOptions($fieldID, $option);
     }
     switch ($html_type) {
         case 'Radio':
             if ($data_type == 'Boolean') {
                 $options = array('No', 'Yes');
             } else {
                 $options = $option;
             }
             if (is_array($value)) {
                 $display = NULL;
                 foreach ($value as $data) {
                     $display .= $display ? ', ' . $options[$data] : $options[$data];
                 }
             } else {
                 $display = CRM_Utils_Array::value($value, $options);
             }
             break;
         case 'Autocomplete-Select':
             if ($data_type == 'ContactReference' && $value) {
                 $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name');
             } elseif (is_array($value)) {
                 $display = NULL;
                 foreach ($value as $data) {
                     $display .= $display ? ', ' . $option[$data] : $option[$data];
                 }
             } else {
                 $display = CRM_Utils_Array::value($value, $option);
             }
             break;
         case 'Select':
             if (is_array($value)) {
                 $display = NULL;
                 foreach ($value as $data) {
                     $display .= $display ? ', ' . $option[$data] : $option[$data];
                 }
             } else {
                 $display = CRM_Utils_Array::value($value, $option);
             }
             break;
         case 'CheckBox':
         case 'AdvMulti-Select':
         case 'Multi-Select':
             if (is_array($value)) {
                 if ($html_type == 'CheckBox') {
                     // CRM-12989 fix
                     CRM_Utils_Array::formatArrayKeys($value);
                 }
                 $checkedData = $value;
             } else {
                 $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1));
                 if ($html_type == 'CheckBox') {
                     $newData = array();
                     foreach ($checkedData as $v) {
                         $v = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, '', $v);
                         $newData[] = $v;
                     }
                     $checkedData = $newData;
                 }
             }
             $v = array();
             foreach ($checkedData as $key => $val) {
                 $v[] = CRM_Utils_Array::value($val, $option);
             }
             if (!empty($v)) {
                 $display = implode(', ', $v);
             }
             break;
         case 'Select Date':
             if (is_array($value)) {
                 foreach ($value as $key => $val) {
                     $display[$key] = CRM_Utils_Date::customFormat($val);
                 }
             } else {
                 // remove time element display if time is not set
                 if (empty($option['attributes']['time_format'])) {
                     $value = substr($value, 0, 10);
                 }
                 $display = CRM_Utils_Date::customFormat($value);
             }
             break;
         case 'Select State/Province':
         case 'Multi-Select State/Province':
         case 'Select Country':
         case 'Multi-Select Country':
             if (strstr($html_type, 'State/Province')) {
                 $option = CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE);
             } else {
                 $option = CRM_Core_PseudoConstant::country(FALSE, FALSE);
             }
             // process multi-select state/country field values
             if (!is_array($value)) {
                 $value = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
             }
             $display = NULL;
             foreach ($value as $data) {
                 $display .= $display && !empty($option[$data]) ? ', ' . $option[$data] : $option[$data];
             }
             break;
         case 'File':
             // In the context of displaying a profile, show file/image
             if ($contactID && $value) {
                 $url = self::getFileURL($contactID, $fieldID, $value);
                 if ($url) {
                     $display = $url['file_url'];
                 }
             } elseif ($value) {
                 $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
                 $display = $icons[$value];
             }
             break;
         case 'TextArea':
             if (empty($value)) {
                 $display = '';
             } else {
                 $display = is_array($value) ? nl2br(implode(', ', $value)) : nl2br($value);
             }
             break;
         case 'Link':
         case 'Text':
             if (empty($value)) {
                 $display = '';
             } else {
                 $display = is_array($value) ? implode(', ', $value) : $value;
             }
     }
     return $display ? $display : $value;
 }
示例#4
0
 /**
  * Takes an associative array and creates a contact object.
  *
  * The function extracts all the params it needs to initialize the create a
  * contact object. the params array could contain additional unused name/value
  * pairs
  *
  * @param array $params
  *   (reference) an assoc array of name/value pairs.
  *
  * @return CRM_Contact_BAO_Contact|CRM_Core_Error|NULL
  *   Created or updated contact object or error object.
  *   (error objects are being phased out in favour of exceptions)
  */
 public static function add(&$params)
 {
     $contact = new CRM_Contact_DAO_Contact();
     if (empty($params)) {
         return NULL;
     }
     // Fix for validate contact sub type CRM-5143.
     if (isset($params['contact_sub_type'])) {
         if (empty($params['contact_sub_type'])) {
             $params['contact_sub_type'] = 'null';
         } else {
             if (!CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type'], TRUE)) {
                 // we'll need to fix tests to handle this
                 // CRM-7925
                 CRM_Core_Error::fatal(ts('The Contact Sub Type does not match the Contact type for this record'));
             }
             $params['contact_sub_type'] = CRM_Utils_Array::implodePadded($params['contact_sub_type']);
         }
     } else {
         // Reset the value.
         // CRM-101XX.
         $params['contact_sub_type'] = 'null';
     }
     // Fixed contact source.
     if (isset($params['contact_source'])) {
         $params['source'] = $params['contact_source'];
     }
     // Fix for preferred communication method.
     $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params, '');
     if ($prefComm && is_array($prefComm)) {
         unset($params['preferred_communication_method']);
         CRM_Utils_Array::formatArrayKeys($prefComm);
         $prefComm = CRM_Utils_Array::implodePadded($prefComm);
     }
     $contact->preferred_communication_method = $prefComm;
     $allNull = $contact->copyValues($params);
     $contact->id = CRM_Utils_Array::value('contact_id', $params);
     if ($contact->contact_type == 'Individual') {
         $allNull = FALSE;
         // Format individual fields.
         CRM_Contact_BAO_Individual::format($params, $contact);
     } elseif ($contact->contact_type == 'Household') {
         if (isset($params['household_name'])) {
             $allNull = FALSE;
             $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, '');
         }
     } elseif ($contact->contact_type == 'Organization') {
         if (isset($params['organization_name'])) {
             $allNull = FALSE;
             $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, '');
         }
     }
     $privacy = CRM_Utils_Array::value('privacy', $params);
     if ($privacy && is_array($privacy) && !empty($privacy)) {
         $allNull = FALSE;
         foreach (self::$_commPrefs as $name) {
             $contact->{$name} = CRM_Utils_Array::value($name, $privacy, FALSE);
         }
     }
     // Since hash was required, make sure we have a 0 value for it (CRM-1063).
     // @todo - does this mean we can remove this block?
     // Fixed in 1.5 by making hash optional, only do this in create mode, not update.
     if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) {
         $allNull = FALSE;
         $contact->hash = md5(uniqid(rand(), TRUE));
     }
     // Even if we don't need $employerId, it's important to call getFieldValue() before
     // the contact is saved because we want the existing value to be cached.
     // createCurrentEmployerRelationship() needs the old value not the updated one. CRM-10788
     $employerId = empty($contact->id) ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id');
     if (!$allNull) {
         $contact->save();
         CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id);
     }
     if ($contact->contact_type == 'Individual' && (isset($params['current_employer']) || isset($params['employer_id']))) {
         // Create current employer.
         $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params);
         $newContact = FALSE;
         if (empty($params['contact_id'])) {
             $newContact = TRUE;
         }
         if ($newEmployer) {
             CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact);
         } else {
             if ($employerId) {
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId);
             }
         }
     }
     // Update cached employer name.
     if ($contact->contact_type == 'Organization') {
         CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id);
     }
     return $contact;
 }
示例#5
0
 /**
  * Set defaults.
  *
  * @param array $groupTree
  * @param array $defaults
  * @param bool $viewMode
  * @param bool $inactiveNeeded
  * @param int $action
  */
 public static function setDefaults(&$groupTree, &$defaults, $viewMode = FALSE, $inactiveNeeded = FALSE, $action = CRM_Core_Action::NONE)
 {
     foreach ($groupTree as $id => $group) {
         if (!isset($group['fields'])) {
             continue;
         }
         foreach ($group['fields'] as $field) {
             if (CRM_Utils_Array::value('element_value', $field) !== NULL) {
                 $value = $field['element_value'];
             } elseif (CRM_Utils_Array::value('default_value', $field) !== NULL && ($action != CRM_Core_Action::UPDATE || !array_key_exists('element_value', $field))) {
                 $value = $viewMode ? NULL : $field['default_value'];
             } else {
                 continue;
             }
             if (empty($field['element_name'])) {
                 continue;
             }
             $elementName = $field['element_name'];
             switch ($field['html_type']) {
                 case 'Multi-Select':
                 case 'AdvMulti-Select':
                 case 'CheckBox':
                     $defaults[$elementName] = array();
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($field['id'], $inactiveNeeded);
                     if ($viewMode) {
                         $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1));
                         if (isset($value)) {
                             foreach ($customOption as $customValue => $customLabel) {
                                 if (in_array($customValue, $checkedData)) {
                                     if ($field['html_type'] == 'CheckBox') {
                                         $defaults[$elementName][$customValue] = 1;
                                     } else {
                                         $defaults[$elementName][$customValue] = $customValue;
                                     }
                                 } else {
                                     $defaults[$elementName][$customValue] = 0;
                                 }
                             }
                         }
                     } else {
                         if (isset($field['customValue']['data'])) {
                             $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($field['customValue']['data'], 1, -1));
                             foreach ($customOption as $val) {
                                 if (in_array($val['value'], $checkedData)) {
                                     if ($field['html_type'] == 'CheckBox') {
                                         $defaults[$elementName][$val['value']] = 1;
                                     } else {
                                         $defaults[$elementName][$val['value']] = $val['value'];
                                     }
                                 } else {
                                     $defaults[$elementName][$val['value']] = 0;
                                 }
                             }
                         } else {
                             if (is_array($value) && count($value)) {
                                 CRM_Utils_Array::formatArrayKeys($value);
                                 $checkedValue = $value;
                             } else {
                                 $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1));
                             }
                             foreach ($customOption as $val) {
                                 if (in_array($val['value'], $checkedValue)) {
                                     if ($field['html_type'] == 'CheckBox') {
                                         $defaults[$elementName][$val['value']] = 1;
                                     } else {
                                         $defaults[$elementName][$val['value']] = $val['value'];
                                     }
                                 }
                             }
                         }
                     }
                     break;
                 case 'Multi-Select Country':
                 case 'Multi-Select State/Province':
                     if (isset($value)) {
                         $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                         foreach ($checkedValue as $val) {
                             if ($val) {
                                 $defaults[$elementName][$val] = $val;
                             }
                         }
                     }
                     break;
                 case 'Select Country':
                     if ($value) {
                         $defaults[$elementName] = $value;
                     } else {
                         $config = CRM_Core_Config::singleton();
                         $defaults[$elementName] = $config->defaultContactCountry;
                     }
                     break;
                 default:
                     if ($field['data_type'] == "Float") {
                         $defaults[$elementName] = (double) $value;
                     } elseif ($field['data_type'] == 'Money' && $field['html_type'] == 'Text') {
                         $defaults[$elementName] = CRM_Utils_Money::format($value, NULL, '%a');
                     } else {
                         $defaults[$elementName] = $value;
                     }
             }
         }
     }
 }