-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSavePageBehavior.php
More file actions
403 lines (351 loc) · 11.4 KB
/
SavePageBehavior.php
File metadata and controls
403 lines (351 loc) · 11.4 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* Page Behavior
*
* @property Room $Room
* @property Page $ParentPage
* @property Box $Box
* @property Page $ChildPage
* @property Box $Box
* @property Container $Container
* @property Language $Language
*
* @copyright Copyright 2014, NetCommons Project
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('ModelBehavior', 'Model');
App::uses('Space', 'Rooms.Model');
/**
* Page Behavior
*
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @package NetCommons\Pages\Model
*/
class SavePageBehavior extends ModelBehavior {
/**
* beforeValidate is called before a model is validated, you can use this callback to
* add behavior validation rules into a models validate array. Returning false
* will allow you to make the validation fail.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False or null will abort the operation. Any other result will continue.
* @see Model::save()
*/
public function beforeValidate(Model $model, $options = array()) {
$model->loadModels([
'Page' => 'Pages.Page',
'Room' => 'Rooms.Room',
]);
if (! Hash::get($model->data, 'Page.room_id')) {
$model->data['Page']['room_id'] = Current::read('Room.id');
}
$model->data['Page']['is_container_fluid'] = false;
$slug = $model->data['Page']['slug'];
if (! isset($slug)) {
$slug = '';
}
$model->data['Page']['slug'] = $slug;
// ページのpermalinkは、ここで親ページのpermalink + 入力したpermalink=slug を連結する事で、初めてpermalink
// として機能する文字列になるため、必ず$model->getParentPermalink()を通す
//$permalink = Hash::get(
// $model->data,
// 'Page.permalink',
// $model->getParentPermalink($model->data['Page']) . '/' . $slug
//);
$permalink = $model->getParentPermalink($model->data['Page']) . '/' . $slug;
if (substr($permalink, 0, 1) === '/') {
$permalink = substr($permalink, 1);
}
$model->data['Page']['permalink'] = $permalink;
return parent::beforeValidate($model, $options);
}
/**
* afterValidate is called just after model data was validated, you can use this callback
* to perform any data cleanup or preparation if needed
*
* @param Model $model Model using this behavior
* @return mixed False will stop this event from being passed to other behaviors
*/
public function afterValidate(Model $model) {
$model->loadModels([
'PagesLanguage' => 'Pages.PagesLanguage',
]);
if (isset($model->data['PagesLanguage'])) {
$model->PagesLanguage->create(false);
$model->PagesLanguage->set($model->data['PagesLanguage']);
if (! $model->PagesLanguage->validates()) {
$model->validationErrors = Hash::merge(
$model->validationErrors, $model->PagesLanguage->validationErrors
);
return false;
}
}
return true;
}
/**
* afterSave is called after a model is saved.
*
* @param Model $model Model using this behavior
* @param bool $created True if this save created a new record
* @param array $options Options passed from Model::save().
* @return bool
* @throws InternalErrorException
* @see Model::save()
*/
public function afterSave(Model $model, $created, $options = array()) {
$model->loadModels([
'PagesLanguage' => 'Pages.PagesLanguage',
'Page' => 'Pages.Page',
]);
if (isset($model->data['PagesLanguage'])) {
$model->PagesLanguage->data['PagesLanguage']['page_id'] = $model->data['Page']['id'];
if (! $model->PagesLanguage->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$this->updateRoomName($model, $model->data);
}
if ($created) {
$result = $model->Page->savePageContainers($model->data);
$model->data = Hash::merge($model->data, $result);
$result = $model->Page->saveBox($model->data);
$model->data = Hash::merge($model->data, $result);
$model->Page->saveBoxesPageContainers($model->data);
}
return parent::afterSave($model, $created, $options);
}
/**
* Save page each association model
*
* #### Options
*
* - `atomic`: If true (default), will attempt to save all records in a single transaction.
* Should be set to false if database/table does not support transactions.
*
* @param Model $model Model using this behavior
* @param array $data request data
* @param array $options Options to use when saving record data, See $options above.
* @throws InternalErrorException
* @return mixed On success Model::$data if its not empty or true, false on failure
*/
public function savePage(Model $model, $data, $options = array()) {
$options = Hash::merge(array('atomic' => true), $options);
//トランザクションBegin
if ($options['atomic']) {
$model->begin();
}
//バリデーション
$model->set($data);
if (! $model->validates()) {
return false;
}
try {
if (! $page = $model->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if ($options['atomic']) {
$model->commit();
}
} catch (Exception $ex) {
if ($options['atomic']) {
$model->rollback($ex);
}
throw $ex;
}
return $page;
}
/**
* テーマ
*
* @param Model $model Model using this behavior
* @param array $data request data
* @throws InternalErrorException
* @return mixed True on success, false on failure
*/
public function saveTheme(Model $model, $data) {
//トランザクションBegin
$model->begin();
if (! $model->exists($data[$model->alias]['id'])) {
return false;
}
try {
$model->id = $data[$model->alias]['id'];
if (! $model->saveField('theme', $data[$model->alias]['theme'], array('callbacks' => false))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$model->commit();
} catch (Exception $ex) {
$model->rollback($ex);
}
return true;
}
/**
* 移動
*
* @param Model $model Model using this behavior
* @param array $data request data
* @return bool
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function saveMove(Model $model, $data) {
//トランザクションBegin
$model->begin();
if (! $model->exists($data[$model->alias]['id'])) {
return false;
}
try {
$model->id = $data[$model->alias]['id'];
if ($data[$model->alias]['type'] === 'up') {
$result = $model->moveUp($model->id, 1);
} elseif ($data[$model->alias]['type'] === 'down') {
$result = $model->moveDown($model->id, 1);
} elseif ($data[$model->alias]['type'] === 'top') {
$childCount = $model->childCount($data[$model->alias]['parent_id'], true);
$result = $model->moveUp($model->id, $childCount);
} elseif ($data[$model->alias]['type'] === 'bottom') {
$childCount = $model->childCount($data[$model->alias]['parent_id'], true);
$result = $model->moveDown($model->id, $childCount);
} elseif ($data[$model->alias]['type'] === 'move') {
//callbacksは必要
$permalink =
$model->getParentPermalink($data['Page']) . '/' . Current::read('Page.slug');
if (substr($permalink, 0, 1) === '/') {
$permalink = substr($permalink, 1);
}
$data[$model->alias]['permalink'] = $permalink;
$model->create(false);
$options = [
'validate' => false,
'fieldList' => ['parent_id', 'permalink'],
'callbacks' => true
];
$result = $model->save($data, $options);
} else {
$result = false;
}
if (! $result) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//パブリックスペースで、移動したものが先頭になった場合、Room.page_id_topを更新する
$this->__updatePageIdTopMove($model, $data);
$model->commit();
} catch (Exception $ex) {
$model->rollback($ex);
}
return true;
}
/**
* page_id_topの更新処理
*
* @param Model $model Model using this behavior
* @param array $data request data
* @return bool
* @throws InternalErrorException
*/
private function __updatePageIdTopMove(Model $model, $data) {
//パブリックスペースで、移動したものが先頭になった場合、Room.page_id_topを更新する
$first = $model->find('first', array(
'recursive' => -1,
'fields' => array('id'),
'conditions' => array(
'parent_id !=' => '',
),
'order' => array('sort_key' => 'asc'),
));
//if ($first['Page']['id'] === $data[$model->alias]['id']) {
$model->Room->id = Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID);
if (! $model->Room->saveField('page_id_top', $first['Page']['id'], ['callbacks' => false])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//}
return true;
}
/**
* page_id_topに対するルーム名を更新する
* ただし、page_id_topがSpace.page_id_topであれば、更新しない
*
* @param Model $model Model using this behavior
* @param array $page request data
* @return bool
* @throws InternalErrorException
*/
public function updateRoomName(Model $model, $page) {
$model->loadModels([
'Room' => 'Rooms.Room',
'RoomsLanguage' => 'Rooms.RoomsLanguage',
'Space' => 'Rooms.Space',
]);
if (! Hash::get($page, 'Page.id') ||
! Hash::get($page, 'Page.room_id') ||
! Hash::get($page, 'PagesLanguage.language_id')) {
return true;
}
if (! $this->hasEditableRoomNameByPage($model, Hash::get($page, 'Page.id'))) {
return true;
}
if (isset($model->data['PagesLanguage'])) {
$db = $model->getDataSource();
$pageName = $db->value($model->data['PagesLanguage']['name'], 'string');
$update = array(
$model->RoomsLanguage->alias . '.name' => $pageName,
);
$conditions = array(
$model->RoomsLanguage->alias . '.room_id' => Hash::get($page, 'Page.room_id'),
$model->RoomsLanguage->alias . '.language_id' => Hash::get($page, 'PagesLanguage.language_id'),
);
if (! $model->RoomsLanguage->updateAll($update, $conditions)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
return true;
}
/**
* page_id_topに対するルーム名を更新する
* ただし、page_id_topがSpace.page_id_topであれば、更新しない
*
* @param Model $model Model using this behavior
* @param int $pageId ページID
* @return bool
* @throws InternalErrorException
*/
public function hasEditableRoomNameByPage(Model $model, $pageId) {
$model->loadModels([
'Room' => 'Rooms.Room',
'RoomsLanguage' => 'Rooms.RoomsLanguage',
'Space' => 'Rooms.Space',
]);
if (! $pageId) {
return false;
}
return (bool)$model->find('count', array(
'recursive' => -1,
'joins' => array(
array(
'table' => $model->Room->table,
'alias' => $model->Room->alias,
'type' => 'INNER',
'conditions' => array(
$model->Room->alias . '.id' . ' = ' . $model->alias . '.room_id',
),
),
array(
'table' => $model->Space->table,
'alias' => $model->Space->alias,
'type' => 'INNER',
'conditions' => array(
$model->Space->alias . '.id' . ' = ' . $model->Room->alias . '.space_id',
),
),
),
'conditions' => [
$model->alias . '.id' => $pageId,
$model->Room->alias . '.page_id_top' . ' = ' . $model->alias . '.id',
$model->Space->alias . '.room_id_root' . ' != ' . $model->Room->alias . '.id',
$model->Space->alias . '.page_id_top' . ' != ' . $model->Room->alias . '.page_id_top',
]
));
}
}