DestinationPath) { $this->FullPath = FULL_PATH.$this->DestinationPath; } parent::kBase(); } //function Parse($value, $options, &$errors) function Parse($value, $field_name, &$object) { $ret = ''; $options = $object->GetFieldOptions($field_name); if(getArrayValue($options, 'upload_dir')) { $this->DestinationPath = $options['upload_dir']; $this->FullPath = FULL_PATH.$this->DestinationPath; } if (getArrayValue($value, 'upload') && getArrayValue($value, 'error') == UPLOAD_ERR_NO_FILE) { // file was not uploaded this time, but was uploaded before, then use previously uploaded file (from db) return getArrayValue($value, 'upload'); } if ( is_array($value) && $value['size'] ) { if ( is_array($value) && $value['error'] === UPLOAD_ERR_OK ) { if ( getArrayValue($options, 'allowed_types') && !in_array($value['type'], $options['allowed_types']) ) { $object->FieldErrors[$field_name]['pseudo'] = 'bad_file_format'; } elseif ( $value['size'] > ($options['max_size'] ? $options['max_size'] : MAX_UPLOAD_SIZE) ) { $object->FieldErrors[$field_name]['pseudo'] = 'bad_file_size'; } elseif ( !is_writable($this->FullPath) ) { $object->FieldErrors[$field_name]['pseudo'] = 'cant_save_file'; } else { $real_name = $this->ValidateFileName($this->FullPath, $value['name']); $file_name = $this->FullPath.$real_name; if ( !move_uploaded_file($value['tmp_name'], $file_name) ) { $object->FieldErrors[$field_name]['pseudo'] = 'cant_save_file'; } else { @chmod($file_name, 0666); if(getArrayValue($options, 'size_field')) { $object->SetDBField($options['size_field'], $value['size']); } if(getArrayValue($options, 'orig_name_field')) { $object->SetDBField($options['orig_name_field'], $value['name']); } if(getArrayValue($options, 'content_type_field')) { $object->SetDBField($options['content_type_field'], $value['type']); } $ret = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath.$real_name; // delete previous file, when new file is uploaded under same field /*$previous_file = isset($value['upload']) ? $value['upload'] : false; if ($previous_file && file_exists($this->FullPath.$previous_file)) { unlink($this->FullPath.$previous_file); }*/ } } } else { $object->FieldErrors[$field_name]['pseudo'] = 'cant_save_file'; } } else { if(getArrayValue($options, 'required')) { $object->FieldErrors[$field_name]['pseudo'] = 'required'; } } if ($value['error'] && !( $value['error'] == UPLOAD_ERR_NO_FILE ) && !$object->FieldErrors[$field_name]['pseudo']) { $object->FieldErrors[$field_name]['pseudo'] = 'cant_save_file'; } return $ret; } function Format($value, $field_name, &$object, $format=null) { if ( is_null($value) ) return ''; $options = $object->GetFieldOptions($field_name); if ( isset($format) ) $options['format'] = $format; $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); } function GetFormatted($tc_value, &$options) { if (isset($options['format'])) { switch ($options['format']) { case 'full_url': $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath; return rtrim($this->Application->BaseURL(), '/').$upload_dir.$tc_value; break; default: return sprintf($options['format'], $tc_value); break; } } return $tc_value; } function ValidateFileName($path, $name) { $parts = pathinfo($name); $ext = '.'.$parts['extension']; $filename = substr($parts['basename'], 0, -strlen($ext)); $new_name = $filename.$ext; while ( file_exists($path.'/'.$new_name) ) { if ( preg_match('/('.preg_quote($filename, '/').'_)([0-9]*)('.preg_quote($ext, '/').')/', $new_name, $regs) ) { $new_name = $regs[1].($regs[2]+1).$regs[3]; } else { $new_name = $filename.'_1'.$ext; } } return $new_name; } } class kPictureFormatter extends kUploadFormatter { function kPictureFormatter() { $this->NakeLookupPath = IMAGES_PATH; $this->DestinationPath = IMAGES_PENDING_PATH; parent::kUploadFormatter(); } function GetFormatted($tc_value, &$options) { if (isset($options['format']) && ($options['format'] == 'img_size')) { $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath; $img_path = FULL_PATH.'/'.$upload_dir.$tc_value; $image_info = @getimagesize($img_path); return ' width="'.$image_info[0].'" height="'.$image_info[1].'"'; } return parent::GetFormatted($tc_value, $options); } }