This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadBehavior.php
235 lines (206 loc) · 6.7 KB
/
UploadBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace App\Model\Behavior;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\Routing\Router;
/**
* Class UploadBehavior
*
* 上传行为类
* 在Model加入以下代码,uploadFields会在上传时对此字段进行格式化,去除域名信息并保存
$this->addBehavior('Upload', [
'uploadFields' => [
'icon'
]
]);
* $this->{Model}->find('uploadFormatter') 可对字段进行full域名格式化
*
*
* 自定义库的配置
* app.php中
*
'CustomLibConfig' => [
// 上传行为类
'UploadBehavior' => [
'enabled' => true // 是否开启,若使用oss等请关闭此类
]
]
*
*/
class UploadBehavior extends Behavior
{
/**
* Default config
*
* These are merged with user-provided config when the behavior is used.
*
* events - an event-name keyed array of which fields to update, and when, for a given event
* possible values for when a field will be updated are "always", "new" or "existing", to set
* the field value always, only when a new record or only when an existing record.
*
* refreshTimestamp - if true (the default) the timestamp used will be the current time when
* the code is executed, to set to an explicit date time value - set refreshTimetamp to false
* and call setTimestamp() on the behavior class before use.
*
* @var array
*/
protected $_defaultConfig = [
'implementedFinders' => [
'uploadFormatter' => 'findUploadFormatter'
],
'implementedMethods' => [
'uploadTouch' => 'touch',
],
'events' => [
'Model.beforeSave' => [],
],
'uploadFields' => [], // 格式化字段
'delimiter' => ',', // 多个信息的分隔符
'enabled' => true // 是否开启,可在app.php中配置
];
/**
* Initialize hook
*
* If events are specified - do *not* merge them with existing events,
* overwrite the events to listen on
*
* @param array $config The config for this behavior.
* @return void
*/
public function initialize(array $config)
{
$this->setConfig('enabled', Configure::read('CustomLibConfig.UploadBehavior.enabled') ? : false, false);
if (isset($config['events'])) {
$this->setConfig('events', $config['events'], false);
}
}
/**
* There is only one event handler, it can be configured to be called for any event
*
* @param \Cake\Event\Event $event Event instance.
* @param \Cake\Datasource\EntityInterface $entity Entity instance.
* @throws \UnexpectedValueException if a field's when value is misdefined
* @return true Returns true irrespective of the behavior logic, the save will not be prevented.
* @throws \UnexpectedValueException When the value for an event is not 'always', 'new' or 'existing'
*/
public function handleEvent(Event $event, EntityInterface $entity)
{
$fields = $this->_config['uploadFields'];
foreach ($fields as $field) {
$this->_updateField($entity, $field);
}
return true;
}
/**
* implementedEvents
*
* The implemented events of this behavior depend on configuration
*
* @return array
*/
public function implementedEvents()
{
return array_fill_keys(array_keys($this->_config['events']), 'handleEvent');
}
/**
* Touch an entity
*
* Bumps timestamp fields for an entity. For any fields configured to be updated
* "always" or "existing", update the timestamp value. This method will overwrite
* any pre-existing value.
*
* @param \Cake\Datasource\EntityInterface $entity Entity instance.
* @param string $eventName Event name.
* @return bool true if a field is updated, false if no action performed
*/
public function touch(EntityInterface $entity, $eventName = 'Model.beforeSave')
{
$events = $this->_config['events'];
if (empty($events[$eventName])) {
return false;
}
$fields = $this->_config['uploadFields'];
$return = false;
foreach ($fields as $field) {
$return = true;
$this->_updateField($entity, $field);
}
return $return;
}
/**
* Update a field, if it hasn't been updated already
*
* @param \Cake\Datasource\EntityInterface $entity Entity instance.
* @param string $field Field name
* @return void
*/
protected function _updateField($entity, $field)
{
if (!$this->_config['enabled']) {
return;
}
if (!$entity->isDirty($field)) {
return;
}
$columnType = $this->getTable()->getSchema()->getColumnType($field);
if (!$columnType) {
return;
}
$original = $entity->get($field);
if (empty($original)) {
return;
}
$fullUrl = Router::fullBaseUrl();
$base = Router::getRequest()->getAttribute('base') ? : Configure::read('App.base');
$original = str_replace($fullUrl . $base, '', $original);
$entity->set($field, $original);
}
/**
* 用户基础 format
* @param $results
* @return mixed
*/
public function _uploadFormatter($results)
{
if (!$this->_config['enabled']) {
return $results;
}
$fields = $this->_config['uploadFields'];
if (empty($fields)) {
return $results;
}
$delimiter = $this->_config['delimiter'];
return $results->map(function ($row) use ($fields, $delimiter) {
$options = ['setter' => false, 'guard' => false];
foreach ($fields as $field) {
if (isset($row[$field]) && !empty($row[$field])) {
$arr = explode($delimiter, $row[$field]);
$newArr = [];
foreach ($arr as $item) {
$newArr[] = Router::url($item, true);
}
$row->set($field, implode($delimiter, $newArr), $options);
}
}
$row->clean();
return $row;
});
}
/**
* 格式化查询
* $query->find('uploadFormatter');
*
*
* @param \Cake\ORM\Query $query The original query to modify
* @param array $options Options
* @return \Cake\ORM\Query
*/
public function findUploadFormatter(Query $query, array $options)
{
return $query
->formatResults([$this, '_uploadFormatter'], $query::PREPEND);
}
}