Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
UWfile.php
1 <?php
2 
3 class UWfile {
4 
5  /**
6  * @var array
7  * @name widget parametrs
8  */
9  public $params = array('path'=>'assets');
10 
11  /**
12  * Widget initialization
13  * @return array
14  */
15  public function init() {
16  return array(
17  'name'=>__CLASS__,
18  'label'=>UserModule::t('File field'),
19  'fieldType'=>array('VARCHAR'),
20  'params'=>$this->params,
21  'paramsLabels' => array(
22  'path'=>UserModule::t('Upload path'),
23  ),
24  'other_validator'=>array(
25  'file'=>array(
26  'allowEmpty'=>array('','false','true'),
27  'maxFiles'=>'',
28  'maxSize'=>'',
29  'minSize'=>'',
30  'tooLarge'=>'',
31  'tooMany'=>'',
32  'tooSmall'=>'',
33  'types'=>'',
34  'wrongType'=>'',
35  ),
36  ),
37  );
38  }
39 
40  /**
41  * @param $value
42  * @param $model
43  * @param $field_varname
44  * @return string
45  */
46  public function setAttributes($value,$model,$field_varname) {
47  $value = CUploadedFile::getInstance($model,$field_varname);
48 
49  if ($value) {
50  $old_file = $model->getAttribute($field_varname);
51  $file_name = $this->params['path'].'/'.$value->name;
52  if (file_exists($file_name)) {
53  $file_name = str_replace('.'.$value->extensionName,'-'.time().'.'.$value->extensionName,$file_name);
54  }
55  if ($model->validate()) {
56  if ($old_file&&file_exists($old_file))
57  unlink($old_file);
58  $value->saveAs($file_name);
59  }
60  $value = $file_name;
61  } else {
62  if (isset($_POST[get_class($model)]['uwfdel'][$field_varname])&&$_POST[get_class($model)]['uwfdel'][$field_varname]) {
63  $old_file = $model->getAttribute($field_varname);
64  if ($old_file&&file_exists($old_file))
65  unlink($old_file);
66  $value='';
67  } else {
68  $value = $model->getAttribute($field_varname);
69  }
70  }
71  return $value;
72  }
73 
74  /**
75  * @param $value
76  * @return string
77  */
78  public function viewAttribute($model,$field) {
79  $file = $model->getAttribute($field->varname);
80  if ($file) {
81  $file = Yii::app()->baseUrl.'/'.$file;
82  return CHtml::link($file,$file);
83  } else
84  return '';
85  }
86 
87  /**
88  * @param $value
89  * @return string
90  */
91  public function editAttribute($model,$field,$params=array()) {
92  if (!isset($params['options'])) $params['options'] = array();
93  $options = $params['options'];
94  unset($params['options']);
95 
96  return CHtml::activeFileField($model,$field->varname,$params)
97  .(($model->getAttribute($field->varname))?'<br/>'.CHtml::activeCheckBox($model,'[uwfdel]'.$field->varname,$params)
98  .' '.CHtml::activeLabelEx($model,'[uwfdel]'.$field->varname,array('label'=>UserModule::t('Delete file'),'style'=>'display:inline;')):'')
99  ;
100  }
101 
102 }