Index: branches/RC/core/units/general/custom_fields.php =================================================================== diff -u -N -r10860 -r11178 --- branches/RC/core/units/general/custom_fields.php (.../custom_fields.php) (revision 10860) +++ branches/RC/core/units/general/custom_fields.php (.../custom_fields.php) (revision 11178) @@ -12,27 +12,37 @@ * * @param string $values_list * @param string $separator + * @param bool $parse_sqls * @return Array */ - function GetValuesHash($values_list, $separator = VALUE_LIST_SEPARATOR) + function GetValuesHash($values_list, $separator = VALUE_LIST_SEPARATOR, $parse_sqls = true) { - $optionValuesStr = trim($this->ParseConfigSQL($values_list), $separator); - if (!$optionValuesStr) { + $values_list = trim($this->ParseConfigSQL($values_list, $separator, $parse_sqls), $separator); + + if (!$values_list) { // no options, then return empty array return Array(); } - $optionValuesTmp = explode($separator, $optionValuesStr); + $optionValuesTmp = explode($separator, $values_list); $optionValues = Array(); - if (substr_count($optionValuesStr, '=') != count($optionValuesTmp)) { - trigger_error('Invalid symbol in ValueList field [' . substr($optionValuesStr, 0, 100) . ' ...]' , E_USER_NOTICE); + if (substr_count($values_list, '=') != count($optionValuesTmp)) { + if ($this->Application->isDebugMode()) { + $this->Application->Debugger->appendTrace(); + } + + trigger_error('Invalid symbol in ValueList field [' . substr($values_list, 0, 100) . ' ...]' , E_USER_NOTICE); return Array (); } foreach ($optionValuesTmp as $optionValue) { - list($key, $val) = explode('=', $optionValue); + list ($key, $val) = explode('=', $optionValue); $val = (substr($val,0,1) == '+') ? substr($val, 1) : $this->Application->Phrase($val); + + if (substr($key, 0, 3) == 'SQL') { + $val = base64_decode( str_replace('_', '=', $val) ); + } $optionValues[$key] = $val; } return $optionValues; @@ -42,10 +52,11 @@ * Replace SQL's in valueList with appropriate queried values * * @param string $valueString + * @param string $separator * @return string * @todo Apply refactoring to embedded vars stuff */ - function ParseConfigSQL($valueString) + function ParseConfigSQL($valueString, $separator = VALUE_LIST_SEPARATOR, $parse_sqls = true) { $string = trim( str_replace(Array('', '%3$s'), Array (TABLE_PREFIX, $this->Application->GetVar('m_lang')), $valueString) ); @@ -80,27 +91,41 @@ $i = 0; $sql_count = count($regs[0]); while ($i < $sql_count) { - $string = str_replace(''.$regs[2][$i].'', $this->QueryConfigSQL($regs[2][$i], $regs[1][$i]), $string); + if ($parse_sqls) { + $replacement = $this->_queryConfigSQL($regs[2][$i], $regs[1][$i], $separator); + } + else { + $sql = base64_encode(''.$regs[2][$i].''); + $replacement = 'SQL' . $i . '=+' . str_replace('=', '_', $sql); + } + + $string = str_replace(''.$regs[2][$i].'', $replacement, $string); $i++; } - $string = preg_replace('/(,){2,}/', ',', $string); // trim trailing commas inside string + + $string = preg_replace('/[' . preg_quote($separator, '/') . ']+/', $separator, $string); // trim trailing separators inside string } return $string; } - function QueryConfigSQL($sql, $plus = '') + /** + * Transforms given sql into value list string + * + * @param string $sql + * @param string $plus + * @param string $separator + * @return string + */ + function _queryConfigSQL($sql, $plus = '', $separator = VALUE_LIST_SEPARATOR) { - $valArray = $this->Conn->Query($sql); - for($i=0; $iConn->Query($sql); + foreach ($values as $index => $value) { + $values[$index] = $value['OptionValue'] . '=' . $plus . $value['OptionName']; } - return implode(',', $valArray); - } - + return implode($separator, $values); + } } ?> \ No newline at end of file Index: branches/RC/core/units/custom_fields/custom_fields_event_handler.php =================================================================== diff -u -N -r10913 -r11178 --- branches/RC/core/units/custom_fields/custom_fields_event_handler.php (.../custom_fields_event_handler.php) (revision 10913) +++ branches/RC/core/units/custom_fields/custom_fields_event_handler.php (.../custom_fields_event_handler.php) (revision 11178) @@ -165,13 +165,18 @@ $custom_field_helper =& $this->Application->recallObject('InpCustomFieldsHelper'); /* @var $custom_field_helper InpCustomFieldsHelper */ - $options = $custom_field_helper->GetValuesHash( $object->GetDBField('ValueList') ); + $options = $custom_field_helper->GetValuesHash( $object->GetDBField('ValueList'), VALUE_LIST_SEPARATOR, false ); $records = Array (); + + $option_key = key($options); + if ($option_key === '' || $option_key == 0) { + // remove 1st empty option, and add it later, when options will be saved, but allow string option keys + unset($options[$option_key]); // keep index, don't use array_unshift! + } + foreach ($options as $option_key => $option_title) { - if ($option_key > 0) { - $records[] = Array ('OptionKey' => $option_key, 'OptionTitle' => $option_title); - } + $records[] = Array ('OptionKey' => $option_key, 'OptionTitle' => $option_title); } $minput_helper =& $this->Application->recallObject('MInputHelper'); Index: branches/RC/core/admin_templates/custom_fields/custom_fields_edit.tpl =================================================================== diff -u -N -r10913 -r11178 --- branches/RC/core/admin_templates/custom_fields/custom_fields_edit.tpl (.../custom_fields_edit.tpl) (revision 10913) +++ branches/RC/core/admin_templates/custom_fields/custom_fields_edit.tpl (.../custom_fields_edit.tpl) (revision 11178) @@ -2,9 +2,9 @@ - + - +