1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenrizbes\UploadableBehavior\Behavior; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The UploadableBehavior helps you handle uploaded files with Propel |
7
|
|
|
* |
8
|
|
|
* Class UploadableBehavior |
9
|
|
|
* @package Fenrizbes\UploadableBehavior\Behavior |
10
|
|
|
*/ |
11
|
|
|
class UploadableBehavior extends \Behavior |
12
|
|
|
{ |
13
|
|
|
protected $parameters = array( |
14
|
|
|
'columns' => 'file', |
15
|
|
|
'use_origin_fileName' => false, //использовать колоки для хранения оригинального имени файла |
16
|
|
|
|
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
protected $columns; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns an array with configured columns' names |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
protected function getColumns() |
27
|
|
|
{ |
28
|
|
|
if (is_null($this->columns)) { |
29
|
|
|
$this->columns = array(); |
30
|
|
|
|
31
|
|
|
foreach (explode(',', $this->getParameter('columns')) as $column) { |
32
|
|
|
$this->columns[] = trim($column); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->columns; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Получение имени колоки для хранения оригинального имени файла |
41
|
|
|
* @param $column |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
protected function getOriginalNameColumn($column) |
45
|
|
|
{ |
46
|
|
|
return trim($column) . "_original_name"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns a setter's name for a column |
51
|
|
|
* |
52
|
|
|
* @param $column |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
protected function getColumnSetter($column) |
56
|
|
|
{ |
57
|
|
|
return 'set'. $this->getTable()->getColumn($column)->getPhpName(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a getter's name for a column |
62
|
|
|
* |
63
|
|
|
* @param $column |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function getColumnGetter($column) |
67
|
|
|
{ |
68
|
|
|
return 'get'. $this->getTable()->getColumn($column)->getPhpName(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function modifyTable() |
75
|
|
|
{ |
76
|
|
|
$table = $this->getTable(); |
77
|
|
|
|
78
|
|
|
foreach ($this->getColumns() as $column) { |
79
|
|
|
if (!$table->hasColumn($column)) { |
80
|
|
|
$table->addColumn(array( |
81
|
|
|
'name' => $column, |
82
|
|
|
'type' => 'VARCHAR', |
83
|
|
|
'size' => 255 |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
// добавление колонок для хранения оригинального имения файла |
87
|
|
|
$origColumn = $this->getOriginalNameColumn($column); |
88
|
|
|
|
89
|
|
|
if ($this->getParameter('use_origin_fileName') && !$table->hasColumn($origColumn)) { |
90
|
|
|
$table->addColumn(array( |
91
|
|
|
'name' => $origColumn, |
92
|
|
|
'type' => 'VARCHAR', |
93
|
|
|
'size' => 255 |
94
|
|
|
)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Constructs methods |
101
|
|
|
* |
102
|
|
|
* @param $builder |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function objectMethods($builder) |
106
|
|
|
{ |
107
|
|
|
$script = ''; |
108
|
|
|
|
109
|
|
|
$script .= $this->renderTemplate('getUploadRoot'); |
110
|
|
|
|
111
|
|
|
$script .= $this->renderTemplate('getUploadDir', array( |
112
|
|
|
'class_name' => $builder->getStubObjectBuilder()->getClassname() |
113
|
|
|
)); |
114
|
|
|
|
115
|
|
|
$script .= $this->renderTemplate('makeFileName'); |
116
|
|
|
|
117
|
|
|
$script .= $this->renderTemplate('moveUploadedFile'); |
118
|
|
|
|
119
|
|
|
$script .= $this->renderSetOriginalFileName(); |
120
|
|
|
|
121
|
|
|
return $script; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/* Добавление метода для задания оригинального имени файла |
125
|
|
|
* |
126
|
|
|
*/ |
127
|
|
|
protected function renderSetOriginalFileName() |
128
|
|
|
{ |
129
|
|
|
$columnsSetters = array(); |
130
|
|
|
if ($this->getParameter('use_origin_fileName')) { |
131
|
|
|
foreach ($this->getColumns() as $column) { |
132
|
|
|
$columnsSetters[$column] = $this->getColumnSetter($this->getOriginalNameColumn($column)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (count($columnsSetters)) { |
136
|
|
|
return $this->renderTemplate("setOriginalFileName", array('columnsSetters' => $columnsSetters)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
return ''; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Checks if need to move any uploaded files |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function preSave() |
148
|
|
|
{ |
149
|
|
|
$script = ''; |
150
|
|
|
|
151
|
|
|
foreach ($this->getColumns() as $column) { |
152
|
|
|
$script .= $this->renderTemplate('preSave', array( |
153
|
|
|
'column' => $this->getTable()->getColumn($column)->getFullyQualifiedName(), |
154
|
|
|
'setter' => $this->getColumnSetter($column), |
155
|
|
|
'getter' => $this->getColumnGetter($column) |
156
|
|
|
)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $script; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|