-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPage.php
More file actions
490 lines (445 loc) · 12.6 KB
/
Page.php
File metadata and controls
490 lines (445 loc) · 12.6 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
/**
* Page Model
*
* @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('PagesAppModel', 'Pages.Model');
App::uses('Current', 'NetCommons.Utility');
App::uses('Space', 'Rooms.Model');
/**
* Page Model
*
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @package NetCommons\Pages\Model
*/
class Page extends PagesAppModel {
/**
* パブリックスペースのページID
*
* @var string
*/
const PUBLIC_ROOT_PAGE_ID = '1';
/**
* プライベートスペースのページID
*
* @var string
*/
const PRIVATE_ROOT_PAGE_ID = '2';
/**
* グループスペースのページID
*
* @var string
*/
const ROOM_ROOT_PAGE_ID = '3';
/**
* ホームのソートキー
*
* @var string
*/
const HOME_SORT_KEY = '~00000001-00000001';
/**
* TreeParser
* __constructでセットする
*
* @var array
*/
public static $treeParser;
/**
* Default behaviors
*
* @var array
*/
public $actsAs = array(
'Pages.PagesTree',
'Pages.SavePage',
'Pages.GetPage',
'Pages.PageAssociations',
'ThemeSettings.Theme',
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Room' => array(
'className' => 'Rooms.Room',
'foreignKey' => 'room_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'ParentPage' => array(
'className' => 'Pages.Page',
'foreignKey' => 'parent_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'ChildPage' => array(
'className' => 'Pages.Page',
'foreignKey' => 'parent_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'PageContainer' => array(
'className' => 'Pages.PageContainer',
'foreignKey' => 'page_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
/**
* Spaceモデルをバインドしているかどうか
*
* @var array
*/
protected $_isSpaceBind = false;
/**
* Constructor. Binds the model's database table to the object.
*
* @param bool|int|string|array $id Set this ID for this model on startup,
* can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
* @see Model::__construct()
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
self::$treeParser = chr(9);
}
/**
* Called before each find operation. Return false if you want to halt the find
* call, otherwise return the (modified) query data.
*
* @param array $query Data used to execute this query, i.e. conditions, order, etc.
* @return mixed true if the operation should continue, false if it should abort; or, modified
* $query to continue with new $query
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforefind
*/
public function beforeFind($query) {
if (Hash::get($query, 'recursive') > -1 && isset($this->belongsTo['Room'])) {
$this->bindModel(array(
'belongsTo' => array(
'Space' => array(
'className' => 'Rooms.Space',
'foreignKey' => false,
'conditions' => array(
'Room.space_id = Space.id',
),
'fields' => '',
'order' => ''
),
)
), true);
$this->Room->useDbConfig = $this->useDbConfig;
$this->Space->useDbConfig = $this->useDbConfig;
}
return true;
}
/**
* Called after each find operation. Can be used to modify any results returned by find().
* Return value should be the (modified) results.
*
* @param mixed $results The results of the find operation
* @param bool $primary Whether this model is being queried directly (vs. being queried as an association)
* @return mixed Result of the find operation
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function afterFind($results, $primary = false) {
foreach ($results as $i => $result) {
if (!isset($result['Space'])) {
break;
}
if ($result['Space']['permalink']) {
$results[$i]['Page']['full_permalink'] = $result['Space']['permalink'] . '/';
} else {
$results[$i]['Page']['full_permalink'] = '';
}
$results[$i]['Page']['full_permalink'] .= $result['Page']['permalink'];
}
return $results;
}
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$themes = $this->getThemes();
$themeNames = [];
foreach ($themes as $theme) {
$themeNames[] = $theme['name'];
}
$this->validate = ValidateMerge::merge($this->validate, array(
'slug' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('pages', 'Slug')),
'required' => true
),
'validPermalink' => array(
'rule' => array('validPermalink'),
'message' => sprintf(
__d('pages', 'Use of %s is prohibited. Please enter a different entry.'),
__d('pages', 'Slug')
),
'allowEmpty' => false,
'required' => true,
),
),
'permalink' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('pages', 'Slug')),
'required' => true
),
'isUniquePermalink' => array(
'rule' => array('isUniquePermalink'),
'message' => sprintf(__d('net_commons', '%s is already in use.'), __d('pages', 'Slug')),
),
'validPermalink' => array(
'rule' => array('validPermalink'),
'message' => sprintf(
__d('pages', 'Use of %s is prohibited. Please enter a different entry.'),
__d('pages', 'Slug')
),
'allowEmpty' => false,
'required' => true,
),
),
'root_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'is_container_fluid' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'theme' => array(
'inList' => array(
'rule' => array('inList', $themeNames),
'message' => __d('net_commons', 'Invalid request.'),
),
),
));
return parent::beforeValidate($options);
}
/**
* パーマリンクバリデーション
*
* @param array $check チェック値
* @return bool
*/
public function validPermalink($check) {
$value = array_shift($check);
//NGワード
// 「%」「 」「#」「<」「>」「+」「\」「"」「'」「&」「?」「=」「~」「:」「;」「,」「$」「@」
// 「^/(最初にスラッシュ)」「./」「/.」「.$(最後にドット)」「^.(最初にドット)」
// 「|」「]」「[」「!」「(」「)」「*」
$pattern = '/(%| |#|<|>|\+|\\\\|\"|\'|&|\?|=|~|:|;|,|\$|@' .
'|^\/|\/$|\.\/|\/\.|\.$|^\.|\||\]|\[|\!|\(|\)|\*)/';
return !(bool)preg_match($pattern, $value);
}
/**
* パーマリンクバリデーション
*
* @param array $fields チェック値
* @return bool
*/
public function isUniquePermalink($fields) {
if (!empty($this->id)) {
$fields[$this->primaryKey . ' !='] = $this->id;
}
if (isset($this->data[$this->alias]['root_id'])) {
$fields['root_id'] = $this->data[$this->alias]['root_id'];
}
return !$this->find('count', array('conditions' => $fields, 'recursive' => -1));
}
/**
* Called after each successful save operation.
*
* @param bool $created True if this save created a new record
* @param array $options Options passed from Model::save().
* @return void
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#aftersave
* @see Model::save()
* @throws InternalErrorException
*/
public function afterSave($created, $options = array()) {
if (Hash::get($this->data, 'Page.id') &&
Hash::get($this->data, 'Page.permalink')) {
$chidren = $this->children(
Hash::get($this->data, 'Page.id'), false, array('Page.id', 'Page.permalink', 'Page.slug')
);
$data = $this->data;
foreach ($chidren as $child) {
$this->id = $child[$this->alias]['id'];
$permalink = Hash::get($data, 'Page.permalink') . '/' . $child[$this->alias]['slug'];
if ($permalink !== $child[$this->alias]['permalink']) {
if (! $this->saveField('permalink', $permalink, array('callbacks' => false))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}
$this->data = $data;
}
}
/**
* ページの作成
*
* @return array ページデータ
*/
public function createPage() {
$this->loadModels([
'PagesLanguage' => 'Pages.PagesLanguage',
]);
$slug = 'page_' . date('YmdHis');
$result = Hash::merge(
$this->create(array(
'id' => null,
'slug' => $slug,
'permalink' => $slug,
'room_id' => Current::read('Room.id'),
'root_id' => Hash::get(Current::read('Page'), 'root_id', Current::read('Page.id')),
'parent_id' => Current::read('Page.id'),
)),
$this->PagesLanguage->create(array(
'id' => null,
'language_id' => Current::read('Language.id'),
'name' => sprintf(__d('pages', 'New page %s'), date('YmdHis')),
))
);
return $result;
}
/**
* ページデータの存在チェック
*
* @param int $pageId ページID
* @param int $roomId ルームID
* @param int $parentRoomId 親ルームID
* @return bool
*/
public function existPage($pageId, $roomId = null, $parentRoomId = null) {
if ($roomId && $parentRoomId && $roomId !== $parentRoomId) {
$result = $this->find('count', array(
'recursive' => 0,
'conditions' => array(
'Page.id' => $pageId,
'Page.room_id' => $roomId,
'Room.parent_id' => $parentRoomId,
),
));
} elseif ($roomId) {
$result = $this->find('count', array(
'recursive' => -1,
'conditions' => array(
'Page.id' => $pageId,
'Page.room_id' => $roomId
),
));
} else {
$result = $this->find('count', array(
'recursive' => -1,
'conditions' => array(
'Page.id' => $pageId,
'Page.room_id' => Current::read('Room.id')
),
));
}
return (bool)$result;
}
/**
* Delete page each association model
* - `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 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 deletePage($data, $options = array()) {
$this->loadModels([
'PagesLanguage' => 'Pages.PagesLanguage',
]);
$options = Hash::merge(array('atomic' => true), $options);
//トランザクションBegin
if ($options['atomic']) {
$this->begin();
}
try {
//Pageの削除
if (! $this->delete($data[$this->alias]['id'])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$conditions = array($this->PagesLanguage->alias . '.page_id' => $data[$this->alias]['id']);
if (! $this->PagesLanguage->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//Container関連の削除
$this->deleteContainers($data[$this->alias]['id']);
//Box関連の削除
$this->deleteBoxes($data[$this->alias]['id']);
if ($options['atomic']) {
$this->commit();
}
return true;
} catch (Exception $ex) {
if ($options['atomic']) {
$this->rollback($ex);
}
throw $ex;
}
}
}