Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1068851
in-portal
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jul 19, 3:27 AM
Size
4 KB
Mime Type
text/x-diff
Expires
Mon, Jul 21, 3:27 AM (18 h, 59 m)
Engine
blob
Format
Raw Data
Handle
691854
Attached To
rINP In-Portal
in-portal
View Options
Index: branches/5.2.x/core/kernel/utility/formatters/options_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/options_formatter.php (revision 16647)
+++ branches/5.2.x/core/kernel/utility/formatters/options_formatter.php (revision 16648)
@@ -1,116 +1,132 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2011 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kOptionsFormatter extends kFormatter {
/**
* Formats value of a given field
*
* @param string $value
* @param string $field_name
* @param kDBItem|kDBList $object
* @param string $format
* @return string
*/
function Format($value, $field_name, &$object, $format=null)
{
if ( is_null($value) ) {
return '';
}
$field_options = $object->GetFieldOptions($field_name);
if (!array_key_exists('options', $field_options) || !is_array($field_options['options'])) {
trigger_error('Options not defined for <strong>'.$object->Prefix.'</strong> field <strong>'.$field_name.'</strong>', E_USER_WARNING);
return $value;
}
$options = $field_options['options'];
$use_phrases = array_key_exists('use_phrases', $field_options) ? $field_options['use_phrases'] : false;
- if (strpos($value, '|') !== false) {
- // multiple checkboxes OR multiselect
- $values = Array ();
- $values_unsorted = explode('|', substr($value, 1, -1) );
+ if ( strpos($value, '|') !== false ) {
+ // Multiple checkboxes OR multiselect.
+ $sorted_values = array();
+ $selected_values = explode('|', substr($value, 1, -1));
// 1. sort values using options order from unit config
- $key_indexes = array_keys($options);
- foreach ($values_unsorted as $value) {
- $values[ array_search($value, $key_indexes) ] = $value;
+ $existing_values = array_keys($options);
+ $non_existing_selected_values = array_diff($selected_values, $existing_values);
+
+ if ( $non_existing_selected_values ) {
+ $selected_values = array_diff($selected_values, $non_existing_selected_values);
+
+ foreach ( $selected_values as $unsorted_value ) {
+ $sorted_values[array_search($unsorted_value, $existing_values)] = $unsorted_value;
+ }
+
+ ksort($sorted_values);
+ $sorted_values = array_merge($sorted_values, $non_existing_selected_values);
+ }
+ else {
+ foreach ( $selected_values as $unsorted_value ) {
+ $sorted_values[array_search($unsorted_value, $existing_values)] = $unsorted_value;
+ }
+
+ ksort($sorted_values);
}
- ksort($values);
// 2. convert values to titles
- $labels = Array ();
- foreach ($values as $value) {
- $label = $this->formatOption($value, $options, $use_phrases);
- if ($label) {
+ $labels = array();
+
+ foreach ( $sorted_values as $sorted_value ) {
+ $label = $this->formatOption($sorted_value, $options, $use_phrases);
+
+ if ( $label ) {
$labels[] = $label;
}
}
return implode($format, $labels);
}
- else {
- return $this->formatOption($value, $options, $use_phrases);
- }
+
+ return $this->formatOption($value, $options, $use_phrases);
}
function formatOption($value, $options, $use_phrases = true)
{
$label = getArrayValue($options, $value);
if ($label !== false) {
// option_id found in options array
return $use_phrases ? $this->Application->Phrase($label) : $label;
}
else {
// option_id not found
return "$value" == "0" ? '' : $value;
}
}
/**
* Performs basic type validation on form field value
*
* @param mixed $value
* @param string $field_name
* @param kDBItem $object
* @return mixed
* @access public
*/
public function Parse($value, $field_name, &$object)
{
if ( $value == '' ) {
return NULL;
}
$found = $option_key = false;
$options = $object->GetFieldOptions($field_name);
$use_phrases = getArrayValue($options, 'use_phrases');
foreach ($options['options'] as $option_key => $option_value) {
if ( $use_phrases ) {
$option_value = $this->Application->Phrase($option_value);
}
if ( "$option_value" === "$value" ) {
$found = true;
break;
}
}
return $found ? $option_key : $value;
}
-}
\ No newline at end of file
+}
Event Timeline
Log In to Comment