Index: branches/RC/core/kernel/utility/formatters/upload_formatter.php =================================================================== diff -u -r9251 -r9253 --- branches/RC/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 9251) +++ branches/RC/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 9253) @@ -142,86 +142,108 @@ return $ret; } - function Format($value, $field_name, &$object, $format=null) + function getSingleFormat($format) { - if ( is_null($value) ) return ''; + $single_mapping = Array ( + 'file_urls' => 'full_url', + 'file_sizes' => 'file_size', + 'files_resized' => 'resize', + ); + return $single_mapping[$format]; + } + + /** + * Return formatted file url,path or size (or same for multiple files) + * + * @param string $value + * @param string $field_name + * @param kDBItem $object + * @param string $format + * @return string + */ + function Format($value, $field_name, &$object, $format = null) + { + if (is_null($value)) { + return ''; + } + $options = $object->GetFieldOptions($field_name); - if ( isset($format) ) $options['format'] = $format; + if (!isset($format)) { + $format = isset($options['format']) ? $options['format'] : false; + } - if ($format == 'file_urls' || $format == 'file_names' || $format == 'file_sizes' || substr($format, 0, 13) == 'files_resized') { - $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath; - $files = explode('|', $value); - $urls = array(); - $names = array(); - if ($value) { // if value string was not empty - foreach ($files as $a_file) { - if (isset($options['direct_links']) && $options['direct_links']) { - $urls[] = rtrim($this->Application->BaseURL(), '/').$upload_dir.$a_file; - } - else { - $urls[] = $this->Application->HREF('', '', array('no_amp'=>1, 'pass'=>'m,'.$object->Prefix, $object->Prefix.'_event'=>'OnViewFile', 'file'=>$a_file, 'field'=>$field_name)); - } - $names[] = $a_file; - $sizes[] = filesize(FULL_PATH.$upload_dir.$a_file); - } + if ($format && preg_match('/(file_urls|file_names|file_sizes|files_resized)(.*)/', $format, $regs)) { + if (!$value || $format == 'file_names') { + // storage format matches display format OR no value + return $value; } - if (preg_match('/files_resized:([\d]*)x([\d]*)/', $format, $regs)) { - $resized = Array(); - foreach ($files as $a_file) { - $resized[] = $this->resizeImage(FULL_PATH.$upload_dir.$a_file, $regs[1], $regs[2]); - } - return implode('|', $resized); - } + $ret = Array (); + $files = explode('|', $value); + $format = $this->getSingleFormat($regs[1]).$regs[2]; - switch ($format) { - case 'file_urls': - return implode('|', $urls); - break; - - case 'file_names': - return implode('|', $names); - break; - - case 'file_sizes': - return implode('|', $sizes); - break; + foreach ($files as $a_file) { + $ret[] = $this->GetFormatted($a_file, $field_name, $object, $format); } - return implode('|', $format == 'file_urls' ? $urls : $names); + + return implode('|', $ret); } $tc_value = $this->TypeCast($value, $options); if( ($tc_value === false) || ($tc_value != $value) ) return $value; // for leaving badly formatted date on the form - return $this->GetFormatted($tc_value, $options); + $object->Fields[$field_name]['direct_links'] = true; // for case, when non-swf uploader is used + return $this->GetFormatted($tc_value, $field_name, $object, $format); } - function GetFormatted($tc_value, &$options) + /** + * Return formatted file url,path or size + * + * @param string $value + * @param string $field_name + * @param kDBItem $object + * @param string $format + * @return string + */ + function GetFormatted($value, $field_name, &$object, $format = null) { - if (isset($options['format'])) { - $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath; + if (!$format) { + return $value; + } - if (preg_match('/resize:([\d]*)x([\d]*)/', $options['format'], $regs)) { - return $this->resizeImage(FULL_PATH.$upload_dir.$tc_value, $regs[1], $regs[2]); - } + $options = $object->GetFieldOptions($field_name); + $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath; - switch ($options['format']) { - case 'full_url': - return rtrim($this->Application->BaseURL(), '/').$upload_dir.$tc_value; - break; + if (preg_match('/resize:([\d]*)x([\d]*)/', $format, $regs)) { + return $this->resizeImage(FULL_PATH.$upload_dir.$value, $regs[1], $regs[2]); + } - case 'full_path': - return FULL_PATH.$upload_dir.$tc_value; - break; + switch ($format) { + case 'full_url': + if (isset($options['direct_links']) && $options['direct_links']) { + return rtrim($this->Application->BaseURL(), '/').$upload_dir.$value; + } + else { + $url_params = Array ( + 'no_amp' => 1, 'pass' => 'm,'.$object->Prefix, + $object->Prefix.'_event' => 'OnViewFile', + 'file' => $value, 'field' => $field_name + ); + return $this->Application->HREF('', '', $url_params); + } + break; - default: - return sprintf($options['format'], $tc_value); - break; - } + case 'full_path': + return FULL_PATH.$upload_dir.$value; + break; + + case 'file_size': + return filesize(FULL_PATH.$upload_dir.$value); + break; } - return $tc_value; + return sprintf($format, $value); } function resizeImage($src_image, $max_width, $max_height) @@ -284,17 +306,17 @@ parent::kUploadFormatter(); } - function GetFormatted($tc_value, &$options) + function GetFormatted($value, $field_name, &$object, $format = null) { - if (isset($options['format']) && ($options['format'] == 'img_size')) { + if ($format == 'img_size') { $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath; - $img_path = FULL_PATH.'/'.$upload_dir.$tc_value; + $img_path = FULL_PATH.'/'.$upload_dir.$value; $image_info = @getimagesize($img_path); return ' width="'.$image_info[0].'" height="'.$image_info[1].'"'; } - return parent::GetFormatted($tc_value, $options); + return parent::GetFormatted($value, $field_name, $object, $format); } } \ No newline at end of file