1 <?php
2
3 class UWfile {
4
5 6 7 8
9 public $params = array('path'=>'assets');
10
11 private $_file_instance = NULL;
12 private $_old_file_path = '';
13 private $_new_file_path = '';
14
15 16 17 18
19 public function init() {
20 return array(
21 'name'=>__CLASS__,
22 'label'=>UserModule::t('File field'),
23 'fieldType'=>array('VARCHAR'),
24 'params'=>$this->params,
25 'paramsLabels' => array(
26 'path'=>UserModule::t('Upload path'),
27 ),
28 'other_validator'=>array(
29 'file'=>array(
30 'allowEmpty'=>array('','false','true'),
31 'maxFiles'=>'',
32 'maxSize'=>'',
33 'minSize'=>'',
34 'tooLarge'=>'',
35 'tooMany'=>'',
36 'tooSmall'=>'',
37 'types'=>'',
38 'wrongType'=>'',
39 'safe'=>array('true','false'),
40 ),
41 ),
42 );
43 }
44
45 46 47 48 49 50
51 public function setAttributes($value,$model,$field_varname) {
52 $this->_new_file_path = $this->_old_file_path = $model->getAttribute($field_varname);
53
54 if ($this->_file_instance = CUploadedFile::getInstance($model,$field_varname)){
55
56 $model->getEventHandlers('onAfterSave')->insertAt(0,array($this, 'processFile'));
57 $file_name = str_replace(' ', '-', $this->_file_instance->name);
58 $this->_new_file_path = $this->params['path'].'/';
59
60 if ($this->_old_file_path){
61 $this->_new_file_path = pathinfo($this->_old_file_path, PATHINFO_DIRNAME).'/';
62 } else {
63 $this->_new_file_path .= $this->unique_dir($this->_new_file_path).'/';
64 }
65
66 $this->_new_file_path .= $file_name;
67
68 } else {
69 if (isset($_POST[get_class($model)]['uwfdel'][$field_varname])&&$_POST[get_class($model)]['uwfdel'][$field_varname]){
70 $model->onAfterSave = array($this, 'processFile');
71 $path = '';
72 }
73 }
74
75 return $this->_new_file_path;
76 }
77
78 79 80 81
82 public function viewAttribute($model,$field) {
83 $file = $model->getAttribute($field->varname);
84 if ($file) {
85 $file = Yii::app()->baseUrl.'/'.$file;
86 return CHtml::link(pathinfo($file, PATHINFO_FILENAME),$file);
87 } else
88 return '';
89 }
90
91 92 93 94
95 public function editAttribute($model,$field,$params=array()) {
96 if (!isset($params['options'])) $params['options'] = array();
97 $options = $params['options'];
98 unset($params['options']);
99
100 return CHtml::activeFileField($model,$field->varname,$params)
101 .(($model->getAttribute($field->varname))?'<br/>'.CHtml::activeCheckBox($model,'[uwfdel]'.$field->varname,$params)
102 .' '.CHtml::activeLabelEx($model,'[uwfdel]'.$field->varname,array('label'=>UserModule::t('Delete file'),'style'=>'display:inline;')):'')
103 ;
104 }
105
106 public function processFile($event){
107
108 $model = $event->sender;
109
110 if ($this->_old_file_path && file_exists($this->_old_file_path)){
111 unlink($this->_old_file_path);
112 $files = scandir(pathinfo($this->_old_file_path, PATHINFO_DIRNAME));
113 if (empty($files[2])){
114
115 rmdir(pathinfo($this->_old_file_path, PATHINFO_DIRNAME));
116 }
117
118 }
119 if ($this->_file_instance){
120 if (!is_dir(pathinfo($this->_new_file_path, PATHINFO_DIRNAME))){
121 mkdir(pathinfo($this->_new_file_path, PATHINFO_DIRNAME), 0777, TRUE);
122 }
123 $this->_file_instance->saveAs($this->_new_file_path);
124 }
125 }
126
127 private function unique_dir($base_path='')
128 {
129 $unique_dir = $this->random_string();
130
131 while (is_dir($base_path . $unique_dir)) {
132 $unique_dir = $this->random_string();
133 }
134
135 return $unique_dir;
136 }
137
138 private function random_string($max = 20){
139 $string = '';
140 $chars = "abcdefghijklmnopqrstuvwxwz0123456789_-ABCDEGFHIJKLMNOPQRSTUVW";
141 for($i = 0; $i < $max; $i++){
142 $rand_key = mt_rand(0, strlen($chars));
143 $string .= substr($chars, $rand_key, 1);
144 }
145 return str_shuffle($string);
146 }
147
148 }