static function &valuesByID($customFieldID, $optionGroupID = NULL)
 {
     if (!$optionGroupID) {
         $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldID, 'option_group_id');
     }
     $options = CRM_Core_OptionGroup::valuesByID($optionGroupID);
     CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE);
     return $options;
 }
 /**
  * Low-level option getter, rarely accessed directly.
  * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
  *
  * @param String $daoName
  * @param String $fieldName
  * @param Array $params
  * - name       string  name of the option group
  * - flip       boolean results are return in id => label format if false
  *                            if true, the results are reversed
  * - grouping   boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
  * - localize   boolean if true, localize the results before returning
  * - condition  string|array add condition(s) to the sql query - will be concatenated using 'AND'
  * - keyColumn  string the column to use for 'id'
  * - labelColumn string the column to use for 'label'
  * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
  * - onlyActive boolean return only the action option values
  * - fresh      boolean ignore cache entries and go back to DB
  * @param String $context: Context string
  *
  * @return Array on success, FALSE on error.
  *
  * @static
  */
 public static function get($daoName, $fieldName, $params = array(), $context = NULL)
 {
     CRM_Core_DAO::buildOptionsContext($context);
     $flip = !empty($params['flip']);
     // Merge params with defaults
     $params += array('grouping' => FALSE, 'localize' => FALSE, 'onlyActive' => $context == 'validate' || $context == 'get' ? FALSE : TRUE, 'fresh' => FALSE);
     // Custom fields are not in the schema
     if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
         $customField = new CRM_Core_DAO_CustomField();
         $customField->id = (int) substr($fieldName, 7);
         $customField->find(TRUE);
         $options = FALSE;
         if (!empty($customField->option_group_id)) {
             $options = CRM_Core_OptionGroup::valuesByID($customField->option_group_id, $flip, $params['grouping'], $params['localize'], CRM_Utils_Array::value('labelColumn', $params, 'label'), $params['onlyActive'], $params['fresh']);
         } else {
             if ($customField->data_type === 'StateProvince') {
                 $options = self::stateProvince();
             } elseif ($customField->data_type === 'Country') {
                 $options = $context == 'validate' ? self::countryIsoCode() : self::country();
             } elseif ($customField->data_type === 'Boolean') {
                 $options = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
             }
             $options = $options && $flip ? array_flip($options) : $options;
         }
         if ($options !== FALSE) {
             CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE);
         }
         $customField->free();
         return $options;
     }
     // Core field: load schema
     $dao = new $daoName();
     $fields = $dao->fields();
     $fieldKeys = $dao->fieldKeys();
     $dao->free();
     // Support "unique names" as well as sql names
     $fieldKey = $fieldName;
     if (empty($fields[$fieldKey])) {
         $fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
     }
     // If neither worked then this field doesn't exist. Return false.
     if (empty($fields[$fieldKey])) {
         return FALSE;
     }
     $fieldSpec = $fields[$fieldKey];
     // If the field is an enum, explode the enum definition and return the array.
     if (isset($fieldSpec['enumValues'])) {
         // use of a space after the comma is inconsistent in xml
         $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
         $output = explode(',', $enumStr);
         return array_combine($output, $output);
     } elseif (!empty($fieldSpec['pseudoconstant'])) {
         $pseudoconstant = $fieldSpec['pseudoconstant'];
         // Merge params with schema defaults
         $params += array('condition' => CRM_Utils_Array::value('condition', $pseudoconstant, array()), 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant), 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant));
         // Fetch option group from option_value table
         if (!empty($pseudoconstant['optionGroupName'])) {
             if ($context == 'validate') {
                 $params['labelColumn'] = 'name';
             }
             // Call our generic fn for retrieving from the option_value table
             return CRM_Core_OptionGroup::values($pseudoconstant['optionGroupName'], $flip, $params['grouping'], $params['localize'], $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL, $params['labelColumn'] ? $params['labelColumn'] : 'label', $params['onlyActive'], $params['fresh'], $params['keyColumn'] ? $params['keyColumn'] : 'value');
         }
         // Fetch options from other tables
         if (!empty($pseudoconstant['table'])) {
             // Normalize params so the serialized cache string will be consistent.
             CRM_Utils_Array::remove($params, 'flip', 'fresh');
             ksort($params);
             $cacheKey = $daoName . $fieldName . serialize($params);
             // Retrieve cached options
             if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
                 $output = self::$cache[$cacheKey];
             } else {
                 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']);
                 if (!class_exists($daoName)) {
                     return FALSE;
                 }
                 // Get list of fields for the option table
                 $dao = new $daoName();
                 $availableFields = array_keys($dao->fieldKeys());
                 $dao->free();
                 $select = "SELECT %1 AS id, %2 AS label";
                 $from = "FROM %3";
                 $wheres = array();
                 $order = "ORDER BY %2";
                 // Use machine name instead of label in validate context
                 if ($context == 'validate') {
                     if (!empty($pseudoconstant['nameColumn'])) {
                         $params['labelColumn'] = $pseudoconstant['nameColumn'];
                     } elseif (in_array('name', $availableFields)) {
                         $params['labelColumn'] = 'name';
                     }
                 }
                 // Condition param can be passed as an sql clause string or an array of clauses
                 if (!empty($params['condition'])) {
                     $wheres[] = implode(' AND ', (array) $params['condition']);
                 }
                 // onlyActive param will automatically filter on common flags
                 if (!empty($params['onlyActive'])) {
                     foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0) as $flag => $val) {
                         if (in_array($flag, $availableFields)) {
                             $wheres[] = "{$flag} = {$val}";
                         }
                     }
                 }
                 // Filter domain specific options
                 if (in_array('domain_id', $availableFields)) {
                     $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID();
                 }
                 $queryParams = array(1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), 2 => array($params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES));
                 // Add orderColumn param
                 if (!empty($params['orderColumn'])) {
                     $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES);
                     $order = "ORDER BY %4";
                 } elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
                     $order = '';
                 } elseif (in_array('weight', $availableFields)) {
                     $order = "ORDER BY weight";
                 }
                 $output = array();
                 $query = "{$select} {$from}";
                 if ($wheres) {
                     $query .= " WHERE " . implode($wheres, ' AND ');
                 }
                 $query .= ' ' . $order;
                 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
                 while ($dao->fetch()) {
                     $output[$dao->id] = $dao->label;
                 }
                 $dao->free();
                 // Localize results
                 if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') {
                     $I18nParams = array();
                     if ($pseudoconstant['table'] == 'civicrm_country') {
                         $I18nParams['context'] = 'country';
                     }
                     if ($pseudoconstant['table'] == 'civicrm_state_province') {
                         $I18nParams['context'] = 'province';
                     }
                     $i18n = CRM_Core_I18n::singleton();
                     $i18n->localizeArray($output, $I18nParams);
                     // Maintain sort by label
                     if ($order == "ORDER BY %2") {
                         CRM_Utils_Array::asort($output);
                     }
                 }
                 self::$cache[$cacheKey] = $output;
             }
             return $flip ? array_flip($output) : $output;
         }
     } elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
         $output = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
         return $flip ? array_flip($output) : $output;
     }
     // If we're still here, it's an error. Return FALSE.
     return FALSE;
 }
Example #3
0
 static function buildOption($field, &$options)
 {
     $options['attributes'] = array('label' => $field['label'], 'data_type' => $field['data_type'], 'html_type' => $field['html_type']);
     $optionGroupID = null;
     if ($field['html_type'] == 'CheckBox' || $field['html_type'] == 'Radio' || $field['html_type'] == 'Select' || $field['html_type'] == 'AdvMulti-Select' || $field['html_type'] == 'Multi-Select' || $field['html_type'] == 'Autocomplete-Select' && $field['data_type'] != 'ContactReference') {
         if ($field['option_group_id']) {
             $optionGroupID = $field['option_group_id'];
         } else {
             if ($field['data_type'] != 'Boolean') {
                 CRM_Core_Error::fatal();
             }
         }
     }
     // build the cache for custom values with options (label => value)
     if ($optionGroupID != null) {
         $query = "\nSELECT label, value\n  FROM civicrm_option_value\n WHERE option_group_id = {$optionGroupID}\n";
         $dao =& CRM_Core_DAO::executeQuery($query);
         while ($dao->fetch()) {
             if ($field['data_type'] == 'Int' || $field['data_type'] == 'Float') {
                 $num = round($dao->value, 2);
                 $options["{$num}"] = $dao->label;
             } else {
                 $options[$dao->value] = $dao->label;
             }
         }
         require_once 'CRM/Utils/Hook.php';
         CRM_Utils_Hook::customFieldOptions($field['id'], $options);
     }
 }
 /**
  * @param $field
  * @param $options
  *
  * @throws Exception
  */
 public static function buildOption($field, &$options)
 {
     // Fixme - adding anything but options to the $options array is a bad idea
     // What if an option had the key 'attributes'?
     $options['attributes'] = array('label' => $field['label'], 'data_type' => $field['data_type'], 'html_type' => $field['html_type']);
     $optionGroupID = NULL;
     if ($field['html_type'] == 'CheckBox' || $field['html_type'] == 'Radio' || $field['html_type'] == 'Select' || $field['html_type'] == 'AdvMulti-Select' || $field['html_type'] == 'Multi-Select' || $field['html_type'] == 'Autocomplete-Select' && $field['data_type'] != 'ContactReference') {
         if ($field['option_group_id']) {
             $optionGroupID = $field['option_group_id'];
         } elseif ($field['data_type'] != 'Boolean') {
             CRM_Core_Error::fatal();
         }
     }
     // build the cache for custom values with options (label => value)
     if ($optionGroupID != NULL) {
         $query = "\nSELECT label, value\n  FROM civicrm_option_value\n WHERE option_group_id = {$optionGroupID}\n";
         $dao = CRM_Core_DAO::executeQuery($query);
         while ($dao->fetch()) {
             if ($field['data_type'] == 'Int' || $field['data_type'] == 'Float') {
                 $num = round($dao->value, 2);
                 $options["{$num}"] = $dao->label;
             } else {
                 $options[$dao->value] = $dao->label;
             }
         }
         CRM_Utils_Hook::customFieldOptions($field['id'], $options);
     }
 }
 /**
  * Class constructor.
  *
  * Takes in a set of custom field ids andsets up the data structures to
  * generate a query
  *
  * @param array $ids
  *   The set of custom field ids.
  *
  * @param bool $contactSearch
  * @param array $locationSpecificFields
  */
 public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array())
 {
     $this->_ids =& $ids;
     $this->_locationSpecificCustomFields = $locationSpecificFields;
     $this->_select = array();
     $this->_element = array();
     $this->_tables = array();
     $this->_whereTables = array();
     $this->_where = array();
     $this->_qill = array();
     $this->_options = array();
     $this->_fields = array();
     $this->_contactSearch = $contactSearch;
     if (empty($this->_ids)) {
         return;
     }
     // initialize the field array
     $tmpArray = array_keys($this->_ids);
     $idString = implode(',', $tmpArray);
     $query = "\nSELECT f.id, f.label, f.data_type,\n       f.html_type, f.is_search_range,\n       f.option_group_id, f.custom_group_id,\n       f.column_name, g.table_name,\n       f.date_format,f.time_format\n  FROM civicrm_custom_field f,\n       civicrm_custom_group g\n WHERE f.custom_group_id = g.id\n   AND g.is_active = 1\n   AND f.is_active = 1\n   AND f.id IN ( {$idString} )";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         // get the group dao to figure which class this custom field extends
         $extends = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id, 'extends');
         if (array_key_exists($extends, self::$extendsMap)) {
             $extendsTable = self::$extendsMap[$extends];
         } elseif (in_array($extends, CRM_Contact_BAO_ContactType::subTypes())) {
             // if $extends is a subtype, refer contact table
             $extendsTable = self::$extendsMap['Contact'];
         }
         $this->_fields[$dao->id] = array('id' => $dao->id, 'label' => $dao->label, 'extends' => $extendsTable, 'data_type' => $dao->data_type, 'html_type' => $dao->html_type, 'is_search_range' => $dao->is_search_range, 'column_name' => $dao->column_name, 'table_name' => $dao->table_name, 'option_group_id' => $dao->option_group_id);
         // store it in the options cache to make things easier
         // during option lookup
         $this->_options[$dao->id] = array();
         $this->_options[$dao->id]['attributes'] = array('label' => $dao->label, 'data_type' => $dao->data_type, 'html_type' => $dao->html_type);
         $optionGroupID = NULL;
         $htmlTypes = array('CheckBox', 'Radio', 'Select', 'Multi-Select', 'AdvMulti-Select', 'Autocomplete-Select');
         if (in_array($dao->html_type, $htmlTypes) && $dao->data_type != 'ContactReference') {
             if ($dao->option_group_id) {
                 $optionGroupID = $dao->option_group_id;
             } elseif ($dao->data_type != 'Boolean') {
                 $errorMessage = ts("The custom field %1 is corrupt. Please delete and re-build the field", array(1 => $dao->label));
                 CRM_Core_Error::fatal($errorMessage);
             }
         } elseif ($dao->html_type == 'Select Date') {
             $this->_options[$dao->id]['attributes']['date_format'] = $dao->date_format;
             $this->_options[$dao->id]['attributes']['time_format'] = $dao->time_format;
         }
         // build the cache for custom values with options (label => value)
         if ($optionGroupID != NULL) {
             $query = "\nSELECT label, value\n  FROM civicrm_option_value\n WHERE option_group_id = {$optionGroupID}\n";
             $option = CRM_Core_DAO::executeQuery($query);
             while ($option->fetch()) {
                 $dataType = $this->_fields[$dao->id]['data_type'];
                 if ($dataType == 'Int' || $dataType == 'Float') {
                     $num = round($option->value, 2);
                     $this->_options[$dao->id]["{$num}"] = $option->label;
                 } else {
                     $this->_options[$dao->id][$option->value] = $option->label;
                 }
             }
             $options = $this->_options[$dao->id];
             //unset attributes to avoid confussion
             unset($options['attributes']);
             CRM_Utils_Hook::customFieldOptions($dao->id, $options, FALSE);
         }
     }
 }
 /** 
  * Format custom value according to data, view mode
  * @param array $values associated array of custom values
  * @param array $field associated array
  * @param boolean $dncOptionPerLine true if optionPerLine should not be consider
  *
  */
 static function formatCustomValues(&$values, &$field, $dncOptionPerLine = false)
 {
     $value = $values['data'];
     //changed isset CRM-4601
     if (CRM_Utils_System::isNull($value)) {
         return;
     }
     $htmlType = CRM_Utils_Array::value('html_type', $field);
     $dataType = CRM_Utils_Array::value('data_type', $field);
     $option_group_id = CRM_Utils_Array::value('option_group_id', $field);
     $timeFormat = CRM_Utils_Array::value('time_format', $field);
     $optionPerLine = CRM_Utils_Array::value('options_per_line', $field);
     $freezeString = "";
     $freezeStringChecked = "";
     switch ($dataType) {
         case 'Date':
             $customTimeFormat = '';
             $customFormat = null;
             if ($timeFormat == 1) {
                 $customTimeFormat = '%l:%M %P';
             } else {
                 if ($timeFormat == 2) {
                     $customTimeFormat = '%H:%M';
                 }
             }
             $supportableFormats = array('mm/dd' => "%B %E%f {$customTimeFormat}", 'dd-mm' => "%E%f %B {$customTimeFormat}", 'yy' => "%Y {$customTimeFormat}");
             if ($format = CRM_Utils_Array::value('date_format', $field)) {
                 if (array_key_exists($format, $supportableFormats)) {
                     $customFormat = $supportableFormats["{$format}"];
                 }
             }
             $retValue = CRM_Utils_Date::customFormat($value, $customFormat);
             break;
         case 'Boolean':
             if ($value == '1') {
                 $retValue = $freezeStringChecked . ts('Yes') . "\n";
             } else {
                 $retValue = $freezeStringChecked . ts('No') . "\n";
             }
             break;
         case 'Link':
             if ($value) {
                 $retValue = CRM_Utils_System::formatWikiURL($value);
             }
             break;
         case 'File':
             $retValue = $values;
             break;
         case 'ContactReference':
             if (CRM_Utils_Array::value('data', $values)) {
                 $retValue = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $values['data'], 'display_name');
             }
             break;
         case 'Memo':
             $retValue = $value;
             break;
         case 'Float':
             if ($htmlType == 'Text') {
                 $retValue = (double) $value;
                 break;
             }
         case 'Money':
             if ($htmlType == 'Text') {
                 require_once 'CRM/Utils/Money.php';
                 $retValue = CRM_Utils_Money::format($value, null, '%a');
                 break;
             }
         case 'String':
         case 'Int':
             if (in_array($htmlType, array('Text', 'TextArea'))) {
                 $retValue = $value;
                 break;
             }
         case 'StateProvince':
         case 'Country':
             //added check for Multi-Select in the below if-statement
             $customData[] = $value;
             //form custom data for multiple-valued custom data
             switch ($htmlType) {
                 case 'Multi-Select Country':
                 case 'Select Country':
                     $customData = $value;
                     if (!is_array($value)) {
                         $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                     }
                     $query = "\n                    SELECT id as value, name as label  \n                    FROM civicrm_country";
                     $coDAO = CRM_Core_DAO::executeQuery($query);
                     break;
                 case 'Select State/Province':
                 case 'Multi-Select State/Province':
                     $customData = $value;
                     if (!is_array($value)) {
                         $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                     }
                     $query = "\n                    SELECT id as value, name as label  \n                    FROM civicrm_state_province";
                     $coDAO = CRM_Core_DAO::executeQuery($query);
                     break;
                 case 'Select':
                     $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                     if ($option_group_id) {
                         $query = "\n                        SELECT label, value\n                        FROM civicrm_option_value\n                        WHERE option_group_id = %1\n                        ORDER BY weight ASC, label ASC";
                         $params = array(1 => array($option_group_id, 'Integer'));
                         $coDAO = CRM_Core_DAO::executeQuery($query, $params);
                     }
                     break;
                 case 'CheckBox':
                 case 'AdvMulti-Select':
                 case 'Multi-Select':
                     $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                 default:
                     if ($option_group_id) {
                         $query = "\n                        SELECT label, value\n                        FROM civicrm_option_value\n                        WHERE option_group_id = %1\n                        ORDER BY weight ASC, label ASC";
                         $params = array(1 => array($option_group_id, 'Integer'));
                         $coDAO = CRM_Core_DAO::executeQuery($query, $params);
                     }
             }
             $options = array();
             if (is_object($coDAO)) {
                 while ($coDAO->fetch()) {
                     $options[$coDAO->value] = $coDAO->label;
                 }
             } else {
                 CRM_Core_Error::fatal(ts('You have hit issue CRM-4716. Please post a report with as much detail as possible on the CiviCRM forums. You can truncate civicr_cache to get around this problem'));
             }
             require_once 'CRM/Utils/Hook.php';
             CRM_Utils_Hook::customFieldOptions($field['id'], $options, false);
             $retValue = null;
             foreach ($options as $optionValue => $optionLabel) {
                 //to show only values that are checked
                 if (in_array((string) $optionValue, $customData)) {
                     $checked = in_array($optionValue, $customData) ? $freezeStringChecked : $freezeString;
                     if (!$optionPerLine || $dncOptionPerLine) {
                         if ($retValue) {
                             $retValue .= ", ";
                         }
                         $retValue .= $checked . $optionLabel;
                     } else {
                         $retValue[] = $checked . $optionLabel;
                     }
                 }
             }
             break;
     }
     //special case for option per line formatting
     if ($optionPerLine > 1 && is_array($retValue)) {
         $rowCounter = 0;
         $fieldCounter = 0;
         $displayValues = array();
         $displayString = null;
         foreach ($retValue as $val) {
             if ($displayString) {
                 $displayString .= ", ";
             }
             $displayString .= $val;
             $rowCounter++;
             $fieldCounter++;
             if ($rowCounter == $optionPerLine || $fieldCounter == count($retValue)) {
                 $displayValues[] = $displayString;
                 $displayString = null;
                 $rowCounter = 0;
             }
         }
         $retValue = $displayValues;
     }
     $retValue = isset($retValue) ? $retValue : null;
     return $retValue;
 }
Example #7
0
 static function &valuesByID($customFieldID, $optionGroupID = null)
 {
     if (!$optionGroupID) {
         $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldID, 'option_group_id');
     }
     require_once 'CRM/Core/OptionGroup.php';
     $options =& CRM_Core_OptionGroup::valuesByID($optionGroupID);
     require_once 'CRM/Utils/Hook.php';
     CRM_Utils_Hook::customFieldOptions($customFieldID, $options, false);
     return $options;
 }
Example #8
0
 /**
  * Add custom data to the columns.
  *
  * @param bool $addFields
  * @param array $permCustomGroupIds
  */
 public function addCustomDataToColumns($addFields = TRUE, $permCustomGroupIds = array())
 {
     if (empty($this->_customGroupExtends)) {
         return;
     }
     if (!is_array($this->_customGroupExtends)) {
         $this->_customGroupExtends = array($this->_customGroupExtends);
     }
     $customGroupWhere = '';
     if (!empty($permCustomGroupIds)) {
         $customGroupWhere = "cg.id IN (" . implode(',', $permCustomGroupIds) . ") AND";
     }
     $sql = "\nSELECT cg.table_name, cg.title, cg.extends, cf.id as cf_id, cf.label,\n       cf.column_name, cf.data_type, cf.html_type, cf.option_group_id, cf.time_format\nFROM   civicrm_custom_group cg\nINNER  JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id\nWHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND\n      {$customGroupWhere}\n      cg.is_active = 1 AND\n      cf.is_active = 1 AND\n      cf.is_searchable = 1\nORDER BY cg.weight, cf.weight";
     $customDAO = CRM_Core_DAO::executeQuery($sql);
     $curTable = NULL;
     while ($customDAO->fetch()) {
         if ($customDAO->table_name != $curTable) {
             $curTable = $customDAO->table_name;
             $curFields = $curFilters = array();
             // dummy dao object
             $this->_columns[$curTable]['dao'] = 'CRM_Contact_DAO_Contact';
             $this->_columns[$curTable]['extends'] = $customDAO->extends;
             $this->_columns[$curTable]['grouping'] = $customDAO->table_name;
             $this->_columns[$curTable]['group_title'] = $customDAO->title;
             foreach (array('fields', 'filters', 'group_bys') as $colKey) {
                 if (!array_key_exists($colKey, $this->_columns[$curTable])) {
                     $this->_columns[$curTable][$colKey] = array();
                 }
             }
         }
         $fieldName = 'custom_' . $customDAO->cf_id;
         if ($addFields) {
             // this makes aliasing work in favor
             $curFields[$fieldName] = array('name' => $customDAO->column_name, 'title' => $customDAO->label, 'dataType' => $customDAO->data_type, 'htmlType' => $customDAO->html_type);
         }
         if ($this->_customGroupFilters) {
             // this makes aliasing work in favor
             $curFilters[$fieldName] = array('name' => $customDAO->column_name, 'title' => $customDAO->label, 'dataType' => $customDAO->data_type, 'htmlType' => $customDAO->html_type);
         }
         switch ($customDAO->data_type) {
             case 'Date':
                 // filters
                 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_DATE;
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_DATE;
                 // CRM-6946, show time part for datetime date fields
                 if ($customDAO->time_format) {
                     $curFields[$fieldName]['type'] = CRM_Utils_Type::T_TIMESTAMP;
                 }
                 break;
             case 'Boolean':
                 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_SELECT;
                 $curFilters[$fieldName]['options'] = array('' => ts('- select -'), 1 => ts('Yes'), 0 => ts('No'));
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_INT;
                 break;
             case 'Int':
                 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_INT;
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_INT;
                 break;
             case 'Money':
                 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_MONEY;
                 break;
             case 'Float':
                 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_FLOAT;
                 break;
             case 'String':
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
                 if (!empty($customDAO->option_group_id)) {
                     if (in_array($customDAO->html_type, array('Multi-Select', 'AdvMulti-Select', 'CheckBox'))) {
                         $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT_SEPARATOR;
                     } else {
                         $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT;
                     }
                     if ($this->_customGroupFilters) {
                         $curFilters[$fieldName]['options'] = array();
                         $ogDAO = CRM_Core_DAO::executeQuery("SELECT ov.value, ov.label FROM civicrm_option_value ov WHERE ov.option_group_id = %1 ORDER BY ov.weight", array(1 => array($customDAO->option_group_id, 'Integer')));
                         while ($ogDAO->fetch()) {
                             $curFilters[$fieldName]['options'][$ogDAO->value] = $ogDAO->label;
                         }
                         CRM_Utils_Hook::customFieldOptions($customDAO->cf_id, $curFilters[$fieldName]['options'], FALSE);
                     }
                 }
                 break;
             case 'StateProvince':
                 if (in_array($customDAO->html_type, array('Multi-Select State/Province'))) {
                     $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT_SEPARATOR;
                 } else {
                     $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT;
                 }
                 $curFilters[$fieldName]['options'] = CRM_Core_PseudoConstant::stateProvince();
                 break;
             case 'Country':
                 if (in_array($customDAO->html_type, array('Multi-Select Country'))) {
                     $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT_SEPARATOR;
                 } else {
                     $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT;
                 }
                 $curFilters[$fieldName]['options'] = CRM_Core_PseudoConstant::country();
                 break;
             case 'ContactReference':
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
                 $curFilters[$fieldName]['name'] = 'display_name';
                 $curFilters[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
                 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
                 $curFields[$fieldName]['name'] = 'display_name';
                 $curFields[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
                 break;
             default:
                 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
                 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
         }
         if (!array_key_exists('type', $curFields[$fieldName])) {
             $curFields[$fieldName]['type'] = CRM_Utils_Array::value('type', $curFilters[$fieldName], array());
         }
         if ($addFields) {
             $this->_columns[$curTable]['fields'] = array_merge($this->_columns[$curTable]['fields'], $curFields);
         }
         if ($this->_customGroupFilters) {
             $this->_columns[$curTable]['filters'] = array_merge($this->_columns[$curTable]['filters'], $curFilters);
         }
         if ($this->_customGroupGroupBy) {
             $this->_columns[$curTable]['group_bys'] = array_merge($this->_columns[$curTable]['group_bys'], $curFields);
         }
     }
 }
 /**
  * @param string $context
  * @return array|bool
  */
 public function getOptions($context = NULL)
 {
     CRM_Core_DAO::buildOptionsContext($context);
     if (!$this->id) {
         return FALSE;
     }
     if (!$this->data_type || !$this->custom_group_id) {
         $this->find(TRUE);
     }
     if (!empty($this->option_group_id)) {
         $options = CRM_Core_OptionGroup::valuesByID($this->option_group_id, FALSE, FALSE, FALSE, 'label', !($context == 'validate' || $context == 'get'));
     } elseif ($this->data_type === 'StateProvince') {
         $options = CRM_Core_Pseudoconstant::stateProvince();
     } elseif ($this->data_type === 'Country') {
         $options = $context == 'validate' ? CRM_Core_Pseudoconstant::countryIsoCode() : CRM_Core_Pseudoconstant::country();
     } elseif ($this->data_type === 'Boolean') {
         $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
     } else {
         return FALSE;
     }
     CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
     CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, array('context' => $context));
     return $options;
 }
Example #10
0
 /**
  * Format custom value according to data, view mode
  *
  * @param array $values associated array of custom values
  * @param array $field associated array
  * @param boolean $dncOptionPerLine true if optionPerLine should not be consider
  *
  */
 static function formatCustomValues(&$values, &$field, $dncOptionPerLine = FALSE)
 {
     $value = $values['data'];
     //changed isset CRM-4601
     if (CRM_Utils_System::isNull($value)) {
         return;
     }
     $htmlType = CRM_Utils_Array::value('html_type', $field);
     $dataType = CRM_Utils_Array::value('data_type', $field);
     $option_group_id = CRM_Utils_Array::value('option_group_id', $field);
     $timeFormat = CRM_Utils_Array::value('time_format', $field);
     $optionPerLine = CRM_Utils_Array::value('options_per_line', $field);
     $freezeString = "";
     $freezeStringChecked = "";
     switch ($dataType) {
         case 'Date':
             $customTimeFormat = '';
             $customFormat = NULL;
             switch ($timeFormat) {
                 case 1:
                     $customTimeFormat = '%l:%M %P';
                     break;
                 case 2:
                     $customTimeFormat = '%H:%M';
                     break;
                 default:
                     // if time is not selected remove time from value
                     $value = substr($value, 0, 10);
             }
             $supportableFormats = array('mm/dd' => "%B %E%f {$customTimeFormat}", 'dd-mm' => "%E%f %B {$customTimeFormat}", 'yy' => "%Y {$customTimeFormat}", 'M yy' => "%b %Y {$customTimeFormat}", 'yy-mm' => "%Y-%m {$customTimeFormat}");
             if ($format = CRM_Utils_Array::value('date_format', $field)) {
                 if (array_key_exists($format, $supportableFormats)) {
                     $customFormat = $supportableFormats["{$format}"];
                 }
             }
             $retValue = CRM_Utils_Date::customFormat($value, $customFormat);
             break;
         case 'Boolean':
             if ($value == '1') {
                 $retValue = $freezeStringChecked . ts('Yes') . "\n";
             } else {
                 $retValue = $freezeStringChecked . ts('No') . "\n";
             }
             break;
         case 'Link':
             if ($value) {
                 $retValue = CRM_Utils_System::formatWikiURL($value);
             }
             break;
         case 'File':
             $retValue = $values;
             break;
         case 'ContactReference':
             if (CRM_Utils_Array::value('data', $values)) {
                 $retValue = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $values['data'], 'display_name');
             }
             break;
         case 'Memo':
             $retValue = $value;
             break;
         case 'Float':
             if ($htmlType == 'Text') {
                 $retValue = (double) $value;
                 break;
             }
         case 'Money':
             if ($htmlType == 'Text') {
                 $retValue = CRM_Utils_Money::format($value, NULL, '%a');
                 break;
             }
         case 'String':
         case 'Int':
             if (in_array($htmlType, array('Text', 'TextArea'))) {
                 $retValue = $value;
                 break;
             }
             // note that if its not text / textarea, the code falls thru and executes
             // the below case also
         // note that if its not text / textarea, the code falls thru and executes
         // the below case also
         case 'StateProvince':
         case 'Country':
             $options = array();
             $coDAO = NULL;
             //added check for Multi-Select in the below if-statement
             $customData[] = $value;
             //form custom data for multiple-valued custom data
             switch ($htmlType) {
                 case 'Multi-Select Country':
                 case 'Select Country':
                     $customData = $value;
                     if (!is_array($value)) {
                         $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                     }
                     $query = "\n                    SELECT id as value, name as label\n                    FROM civicrm_country";
                     $coDAO = CRM_Core_DAO::executeQuery($query);
                     break;
                 case 'Select State/Province':
                 case 'Multi-Select State/Province':
                     $customData = $value;
                     if (!is_array($value)) {
                         $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                     }
                     $query = "\n                    SELECT id as value, name as label\n                    FROM civicrm_state_province";
                     $coDAO = CRM_Core_DAO::executeQuery($query);
                     break;
                 case 'Select':
                     $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                     if ($option_group_id) {
                         $options = CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($option_group_id);
                     }
                     break;
                 case 'CheckBox':
                 case 'AdvMulti-Select':
                 case 'Multi-Select':
                     $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
                 default:
                     if ($option_group_id) {
                         $options = CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($option_group_id);
                     }
             }
             if (is_object($coDAO)) {
                 while ($coDAO->fetch()) {
                     if ($dataType == 'Country') {
                         // NB: using ts() on a variable here is OK, since the value is pre-determined, not variable
                         // and already extracted to .pot files.
                         $options[$coDAO->value] = ts($coDAO->label, array('context' => 'country'));
                     } elseif ($dataType == 'StateProvince') {
                         $options[$coDAO->value] = ts($coDAO->label, array('context' => 'province'));
                     } else {
                         $options[$coDAO->value] = $coDAO->label;
                     }
                 }
             }
             CRM_Utils_Hook::customFieldOptions($field['id'], $options, FALSE);
             $retValue = NULL;
             foreach ($options as $optionValue => $optionLabel) {
                 if ($dataType == 'Money') {
                     foreach ($customData as $k => $v) {
                         $customData[] = CRM_Utils_Money::format($v, NULL, '%a');
                     }
                 }
                 //to show only values that are checked
                 if (in_array((string) $optionValue, $customData)) {
                     $checked = in_array($optionValue, $customData) ? $freezeStringChecked : $freezeString;
                     if (!$optionPerLine || $dncOptionPerLine) {
                         if ($retValue) {
                             $retValue .= ", ";
                         }
                         $retValue .= $checked . $optionLabel;
                     } else {
                         $retValue[] = $checked . $optionLabel;
                     }
                 }
             }
             break;
     }
     //special case for option per line formatting
     if ($optionPerLine > 1 && is_array($retValue)) {
         $rowCounter = 0;
         $fieldCounter = 0;
         $displayValues = array();
         $displayString = '';
         foreach ($retValue as $val) {
             if ($displayString) {
                 $displayString .= ", ";
             }
             $displayString .= $val;
             $rowCounter++;
             $fieldCounter++;
             if ($rowCounter == $optionPerLine || $fieldCounter == count($retValue)) {
                 $displayValues[] = $displayString;
                 $displayString = '';
                 $rowCounter = 0;
             }
         }
         $retValue = $displayValues;
     }
     $retValue = isset($retValue) ? $retValue : NULL;
     return $retValue;
 }