HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
UWfile.php
Go to the documentation of this file.
1 <?php
2 
3 class UWfile
4 {
9  public $params = array('path' => 'assets');
10 
16  public function init()
17  {
18  return array(
19  'name' => __CLASS__,
20  'label' => UserModule::t('File field'),
21  'fieldType' => array('VARCHAR'),
22  'params' => $this->params,
23  'paramsLabels' => array(
24  'path' => UserModule::t('Upload path'),
25  ),
26  'other_validator' => array(
27  'file' => array(
28  'allowEmpty' => array('','false','true'),
29  'maxFiles' => '',
30  'maxSize' => '',
31  'minSize' => '',
32  'tooLarge' => '',
33  'tooMany' => '',
34  'tooSmall' => '',
35  'types' => '',
36  'wrongType' => '',
37  ),
38  ),
39  );
40  }
41 
49  public function setAttributes($value, $model, $field_varname)
50  {
51  $value = CUploadedFile::getInstance($model, $field_varname);
52 
53  if ($value) {
54  $old_file = $model->getAttribute($field_varname);
55  $file_name = $this->params['path'].'/'.$value->name;
56  if (file_exists($file_name)) {
57  $file_name = str_replace('.'.$value->extensionName, '-'.time().'.'.$value->extensionName, $file_name);
58  }
59  if ($model->validate()) {
60  if ($old_file && file_exists($old_file)) {
61  unlink($old_file);
62  }
63  $value->saveAs($file_name);
64  }
65  $value = $file_name;
66  } else {
67  if (isset($_POST[get_class($model)]['uwfdel'][$field_varname]) && $_POST[get_class($model)]['uwfdel'][$field_varname]) {
68  $old_file = $model->getAttribute($field_varname);
69  if ($old_file && file_exists($old_file)) {
70  unlink($old_file);
71  }
72  $value = '';
73  } else {
74  $value = $model->getAttribute($field_varname);
75  }
76  }
77 
78  return $value;
79  }
80 
86  public function viewAttribute($model, $field)
87  {
88  $file = $model->getAttribute($field->varname);
89  if ($file) {
90  $file = Yii::app()->baseUrl.'/'.$file;
91 
92  return CHtml::link($file, $file);
93  } else {
94  return '';
95  }
96  }
97 
103  public function editAttribute($model, $field, $params = array())
104  {
105  if (!isset($params['options'])) {
106  $params['options'] = array();
107  }
108  $options = $params['options'];
109  unset($params['options']);
110 
111  return CHtml::activeFileField($model, $field->varname, $params)
112  .(($model->getAttribute($field->varname)) ? '<br/>'.CHtml::activeCheckBox($model, '[uwfdel]'.$field->varname, $params)
113  .' '.CHtml::activeLabelEx($model, '[uwfdel]'.$field->varname, array('label' => UserModule::t('Delete file'), 'style' => 'display:inline;')) : '')
114  ;
115  }
116 }