/
Content.php
1901 lines (1818 loc) · 57.4 KB
/
Content.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* baserCMS : Based Website Development Project <http://basercms.net>
* Copyright (c) baserCMS Users Community <http://basercms.net/community/>
*
* @copyright Copyright (c) baserCMS Users Community
* @link http://basercms.net baserCMS Project
* @package Baser.Model
* @since baserCMS v 4.0.0
* @license http://basercms.net/license/index.html
*/
/**
* コンテンツモデル
*
* @package Baser.Model
* @property Site $Site
*/
class Content extends AppModel {
/**
* ビヘイビア
*
* @var array
*/
public $actsAs = [
'Tree' => ['level' => 'level'],
'BcCache',
'SoftDelete',
'BcUpload' => [
'saveDir' => "contents",
'fields' => [
'eyecatch' => [
'type' => 'image',
'namefield' => 'id',
'nameadd' => true,
'nameformat' => '%08d',
'subdirDateFormat' => 'Y/m',
//'imageresize' => array('width' => '800', 'height' => '800'),
'imagecopy' => [
'thumb' => ['suffix' => '_thumb', 'width' => '300', 'height' => '300'],
'medium'=> ['suffix' => '_midium', 'width' => '800', 'height' => '800']
]
]
]
]
];
/**
* Belongs To
*
* @var array
*/
public $belongsTo = [
'Site' => [
'className' => 'Site',
'foreignKey' => 'site_id'
],
'User' => [
'className' => 'User',
'foreignKey' => 'author_id'
]
];
/**
* バリデーション
*
* @var array
*/
public $validate = [
'id' => [
['rule' => 'numeric', 'on' => 'update', 'message' => 'IDに不正な値が利用されています。']
],
'name' => [
['rule' => ['notBlank'],
'message' => 'URLを入力してください。'],
['rule' => ['bcUtileUrlencodeBlank'],
'message' => 'URLはスペース、全角スペース及び、指定の記号(\\\'|`^"(){}[];/?:@&=+$,%<>#!)だけの名前は付けられません。'],
['rule' => ['notBlank'],
'message' => 'スラッグを入力してください。'],
['rule' => ['maxLength', 2083],
'message' => 'タイトルは230文字以内で入力してください。'],
['rule' => ['duplicateRelatedSiteContent'],
'message' => '連携しているサブサイトでスラッグが重複するコンテンツが存在します。重複するコンテンツのスラッグ名を先に変更してください。']
],
'title' => [
['rule' => ['bcUtileUrlencodeBlank'],
'message' => 'タイトルはスペース、全角スペース及び、指定の記号(\\\'|`^"(){}[];/?:@&=+$,%<>#!)だけの名前は付けられません。'],
['rule' => ['notBlank'],
'message' => 'タイトルを入力してください。'],
['rule' => ['maxLength', 230],
'message' => 'タイトルは230文字以内で入力してください。'],
],
'self_publish_begin' => [
['rule' => ['checkDate'],
'message' => '公開開始日に不正な文字列が入っています。']
],
'self_publish_end' => [
['rule' => ['checkDate'],
'message' => '公開終了日に不正な文字列が入っています。'],
['rule' => ['checkDateAfterThan', 'self_publish_begin'],
'message' => '公開終了日は、公開開始日より新しい日付で入力してください。'],
],
'created_date' => [
['rule' => ['checkDate'],
'message' => '作成日に不正な文字列が入っています。'],
],
'modified_date' => [
['rule' => ['checkDate'],
'message' => '更新日に不正な文字列が入っています。']
],
];
/**
* 関連データを更新する
*
* @var bool
*/
public $updatingRelated = true;
/**
* システムデータを更新する
*
* @var bool
*/
public $updatingSystemData = true;
/**
* 保存前の親ID
*
* IDの変更比較に利用
*
* @var null
*/
public $beforeSaveParentId = null;
/**
* 削除時の削除対象レコード
*
* afterDelete で利用する為、beforeDelete で取得し保存する
* @var []
*/
private $__deleteTarget;
/**
* Implemented Events
*
* beforeDelete の優先順位を SoftDeleteBehaviorより高くする為に調整
*
* @return array
*/
public function implementedEvents() {
return [
'Model.beforeFind' => ['callable' => 'beforeFind', 'passParams' => true],
'Model.afterFind' => ['callable' => 'afterFind', 'passParams' => true],
'Model.beforeValidate' => ['callable' => 'beforeValidate', 'passParams' => true],
'Model.afterValidate' => ['callable' => 'afterValidate'],
'Model.beforeSave' => ['callable' => 'beforeSave', 'passParams' => true],
'Model.afterSave' => ['callable' => 'afterSave', 'passParams' => true],
'Model.beforeDelete' => ['callable' => 'beforeDelete', 'passParams' => true, 'priority' => 1],
'Model.afterDelete' => ['callable' => 'afterDelete'],
];
}
/**
* サイト設定にて、エイリアスを利用してメインサイトと自動連携するオプションを利用時に、
* 関連するサブサイトで、関連コンテンツを作成する際、同階層に重複名称のコンテンツがないか確認する
*
* - 新規の際は、存在するだけでエラー
* - 編集の際は、main_site_content_id が自身のIDでない、alias_id が自身のIDでない場合エラー
*
* @param $check
* @return bool
*/
public function duplicateRelatedSiteContent($check) {
$name = $check[key($check)];
if(!$this->Site->isMain($this->data['Content']['site_id'])) {
return true;
}
$parents = $this->getPath($this->data['Content']['parent_id'], ['name'], -1);
$parents = Hash::extract($parents, "{n}.Content.name");
unset($parents[0]);
if($this->data['Content']['site_id']) {
unset($parents[1]);
}
$baseUrl = '/';
if($parents) {
$baseUrl = '/' . implode('/', $parents) . '/';
}
$sites = $this->Site->find('all', ['conditions' => ['Site.main_site_id' => $this->data['Content']['site_id'], 'relate_main_site' => true]]);
// URLを取得
$urlAry = [];
foreach($sites as $site) {
$prefix = $site['Site']['name'];
if($site['Site']['alias']) {
$prefix = $site['Site']['alias'];
}
$urlAry[] = '/' . $prefix . $baseUrl . $name;
}
$conditions = ['Content.url' => $urlAry];
if(!empty($this->data['Content']['id'])) {
$conditions = array_merge($conditions, [
['or' => ['Content.alias_id <>' => $this->data['Content']['id'], 'Content.alias_id' => null]],
['or' => ['Content.main_site_content_id <>' => $this->data['Content']['id'], 'Content.main_site_content_id' => null]]
]);
}
if($this->find('count', ['conditions' => $conditions])) {
return false;
}
return true;
}
/**
* Before Validate
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
*/
public function beforeValidate($options = []) {
// コンテンツ一覧にて、コンテンツを登録した直後のリネーム処理までは新規追加とみなして処理を行う為、$create で判定させる
$create = false;
if(empty($this->data['Content']['id']) || !empty($options['firstCreate'])) {
$create = true;
}
// タイトルは強制的に255文字でカット
if(!empty($this->data['Content']['title'])) {
$this->data['Content']['title'] = mb_substr($this->data['Content']['title'], 0, 254, 'UTF-8');
}
if($create) {
// IEのURL制限が2083文字の為、全て全角文字を想定し231文字でカット
if(!isset($this->data['Content']['name'])) {
$this->data['Content']['name'] = BcUtil::urlencode(mb_substr($this->data['Content']['title'], 0, 230, 'UTF-8'));
}
if(!isset($this->data['Content']['self_status'])) {
$this->data['Content']['self_status'] = false;
}
if(!isset($this->data['Content']['self_publish_begin'])) {
$this->data['Content']['self_publish_begin'] = null;
}
if(!isset($this->data['Content']['self_publish_end'])) {
$this->data['Content']['self_publish_end'] = null;
}
if(!isset($this->data['Content']['deleted'])) {
$this->data['Content']['deleted'] = false;
}
if(!isset($this->data['Content']['created_date'])) {
$this->data['Content']['created_date'] = date('Y-m-d H:i:s');
}
if(!isset($this->data['Content']['site_root'])) {
$this->data['Content']['site_root'] = 0;
}
if(!isset($this->data['Content']['exclude_search'])) {
$this->data['Content']['exclude_search'] = 0;
}
if(!isset($this->data['Content']['author_id'])) {
$user = BcUtil::loginUser('admin');
$this->data['Content']['author_id'] = $user['id'];
}
} else {
if(empty($this->data['Content']['modified_date'])) {
$this->data['Content']['modified_date'] = date('Y-m-d H:i:s');
}
if(isset($this->data['Content']['name'])) {
$this->data['Content']['name'] = BcUtil::urlencode(mb_substr($this->data['Content']['name'], 0, 230, 'UTF-8'));
}
if($this->data['Content']['id'] == 1) {
unset($this->validate['name']);
}
}
// name の 重複チェック&リネーム
if(!empty($this->data['Content']['name'])) {
$contentId = null;
if(!empty($this->data['Content']['id'])) {
$contentId = $this->data['Content']['id'];
}
$this->data['Content']['name'] = $this->getUniqueName($this->data['Content']['name'], $this->data['Content']['parent_id'], $contentId);
}
return true;
}
/**
* After Validate
*/
public function afterValidate() {
parent::afterValidate();
// 新規追加の際、name は、title より自動設定される為、バリデーションエラーが発生してもエラーメッセージを表示しない
if(empty($this->data['Content']['id']) && !empty($this->validationErrors['name'])) {
unset($this->validationErrors['name']);
}
}
/**
* 一意の name 値を取得する
*
* @param string $name name フィールドの値
* @return string
*/
public function getUniqueName($name, $parentId, $contentId = null) {
// 先頭が同じ名前のリストを取得し、後方プレフィックス付きのフィールド名を取得する
$conditions = [
'Content.name LIKE' => $name . '%',
'Content.parent_id' => $parentId
];
if($contentId) {
$conditions['Content.id <>'] = $contentId;
}
$datas = $this->find('all', ['conditions' => $conditions, 'fields' => ['name'], 'order' => "Content.name", 'recursive' => -1]);
$datas = Hash::extract($datas, "{n}.Content.name");
$numbers = [];
if ($datas) {
foreach($datas as $data) {
$lastPrefix = preg_replace('/^' . preg_quote($name, '/') . '/', '', $data);
if(!$lastPrefix) {
$numbers[1] = 1;
} elseif (preg_match("/^_([0-9]+)$/s", $lastPrefix, $matches)) {
$numbers[$matches[1]] = true;
}
}
if($numbers) {
$prefixNo = 1;
while(true) {
if(!isset($numbers[$prefixNo])) {
break;
}
$prefixNo++;
}
if($prefixNo == 1) {
return $name;
} else {
return $name . '_' . ($prefixNo);
}
} else {
return $name;
}
} else {
return $name;
}
}
/**
* Before Save
*
* @param array $options
* @return bool
*/
public function beforeSave($options = []) {
if(!empty($this->data['Content']['id'])) {
$this->beforeSaveParentId = $this->field('parent_id', ['Content.id' => $this->data['Content']['id']]);
}
return parent::beforeSave($options);
}
/**
* After Save
*
* @param bool $created
* @param array $options
* @return void
*/
public function afterSave($created, $options = []) {
parent::afterSave($created, $options);
$this->deleteAssocCache($this->data);
if($this->updatingSystemData) {
$this->updateSystemData($this->data);
}
if($this->updatingRelated) {
// ゴミ箱から戻す場合、 type の定義がないが問題なし
if(!empty($this->data['Content']['type']) && $this->data['Content']['type'] == 'ContentFolder') {
$this->updateChildren($this->data['Content']['id']);
}
$this->updateRelateSubSiteContent($this->data);
if(!empty($this->data['Content']['parent_id']) && $this->beforeSaveParentId != $this->data['Content']['parent_id']) {
$SiteConfig = ClassRegistry::init('SiteConfig');
$SiteConfig->updateContentsSortLastModified();
$this->beforeSaveParentId = null;
}
}
}
/**
* 関連するコンテンツ本体のデータキャッシュを削除する
* @param $data
*/
public function deleteAssocCache($data) {
if(empty($data['Content']['plugin']) || empty($data['Content']['type'])) {
$data = $this->find('first', ['fields' => ['Content.plugin', 'Content.type'], 'conditions' => ['Content.id' => $data['Content']['id']], 'recursive' => -1]);
}
$assoc = $data['Content']['type'];
if($data['Content']['plugin'] != 'Core') {
if(!CakePlugin::loaded($data['Content']['plugin'])) {
return;
}
$assoc = $data['Content']['plugin'] . '.' . $assoc;
}
$AssocModel = ClassRegistry::init($assoc);
if($AssocModel && !empty($AssocModel->actsAs) && in_array('BcCache', $AssocModel->actsAs)) {
if($AssocModel->Behaviors->hasMethod('delCache')) {
$AssocModel->delCache();
}
}
}
/**
* Before Delete
*
* 論理削除の場合、
* @param bool $cascade
* @return bool
*/
public function beforeDelete($cascade = true) {
if(!parent::beforeDelete($cascade)) {
return false;
}
$data = $this->find('first', [
'conditions' => [$this->alias . '.id' => $this->id]
]);
$this->__deleteTarget = $data;
if(!$this->softDelete(null)) {
return true;
}
return true;
}
/**
* After Delete
*
* 関連コンテンツのキャッシュを削除する
*/
public function afterDelete() {
parent::afterDelete();
$data = $this->__deleteTarget;
$this->__deleteTarget = null;
if($data) {
$this->deleteRelateSubSiteContent($data);
$this->deleteAlias($data);
}
$this->deleteAssocCache($data);
}
/**
* 自データのエイリアスを削除する
*
* 全サイトにおけるエイリアスを全て削除
*
* @param $data
*/
public function deleteAlias($data) {
//自身がエイリアスか確認し、エイリアスの場合は終了
if($data['Content']['alias_id']) {
return;
}
$contents = $this->find('all', [
'fields' => ['Content.id'],
'conditions' => [
'Content.alias_id' => $data['Content']['id']
], 'recursive' => -1]);
if(!$contents) {
return;
}
foreach($contents as $content) {
$softDelete = $this->softDelete(null);
$this->softDelete(false);
$this->removeFromTree($content['Content']['id'], true);
$this->softDelete($softDelete);
}
$this->data = $data;
$this->id = $data['Content']['id'];
}
/**
* メインサイトの場合、連携設定がされている子サイトのエイリアス削除する
*
* @param $data
*/
public function deleteRelateSubSiteContent($data) {
// 自身がエイリアスか確認し、エイリアスの場合は終了
if($data['Content']['alias_id']) {
return;
}
// メインサイトか確認し、メインサイトでない場合は終了
if(!$this->Site->isMain($data['Content']['site_id'])) {
return;
}
// 連携設定となっている小サイトを取得
$sites = $this->Site->find('all', ['conditions' => ['Site.main_site_id' => $data['Content']['site_id'], 'relate_main_site' => true], 'recursive' => -1]);
if(!$sites) {
return;
}
// 同階層に同名のコンテンツがあるか確認
foreach($sites as $site) {
$content = $this->find('first', ['conditions' => [
'Content.site_id' => $site['Site']['id'],
'Content.main_site_content_id' => $data['Content']['id']
], 'recursive' => -1]);
if($content) {
// 存在する場合は、自身のエイリアスかどうか確認し削除する
if($content['Content']['alias_id'] == $data['Content']['id']) {
$softDelete = $this->softDelete(null);
$this->softDelete(false);
$this->removeFromTree($content['Content']['id'], true);
$this->softDelete($softDelete);
} elseif($content['Content']['type'] == 'ContentFolder') {
$this->updateChildren($content['Content']['id']);
}
}
}
$this->data = $data;
$this->id = $data['Content']['id'];
}
/**
* メインサイトの場合、連携設定がされている子サイトのエイリアスを追加・更新する
*
* @param $data
*/
public function updateRelateSubSiteContent($data) {
// 他のデータを更新する為、一旦退避
$dataTmp = $this->data;
$idTmp = $this->id;
// 自身がエイリアスか確認し、エイリアスの場合は終了
if(!empty($data['Content']['alias_id']) || !isset($data['Content']['site_id'])) {
return true;
}
$isContentFolder = false;
if(!empty($data['Content']['type']) && $data['Content']['type'] == 'ContentFolder') {
$isContentFolder = true;
}
// メインサイトか確認し、メインサイトでない場合は終了
if(!$this->Site->isMain($data['Content']['site_id'])) {
return true;
}
// 連携設定となっている小サイトを取得
$sites = $this->Site->find('all', ['conditions' => ['Site.main_site_id' => $data['Content']['site_id'], 'relate_main_site' => true]]);
if(!$sites) {
return true;
}
$_data = $this->find('first', ['conditions' => ['Content.id' => $data['Content']['id']], 'recursive' => -1]);
if($_data) {
$data = ['Content' => array_merge($_data['Content'], $data['Content'])];
}
// URLが空の場合はゴミ箱へ移動する処理の為、連携更新を行わない
if(!$data['Content']['url']) {
return true;
}
$CreateModel = $this;
if($isContentFolder) {
$CreateModel = ClassRegistry::init('ContentFolder');
}
$pureUrl = $this->pureUrl($data['Content']['url'], $data['Content']['site_id']);
// 同階層に同名のコンテンツがあるか確認
$result = true;
foreach($sites as $site) {
if(!$site['Site']['status']) {
continue;
}
$url = $pureUrl;
$prefix = $this->Site->getPrefix($site);
if($prefix) {
$url = '/' . $prefix . $url;
}
$content = $this->find('first', ['conditions' => [
'Content.site_id' => $site['Site']['id'],
'or' => [
'Content.main_site_content_id' => $data['Content']['id'],
'Content.url' => $url
]
], 'recursive' => -1]);
if($content) {
// 存在する場合は、自身のエイリアスかどうか確認し、エイリアスの場合は、公開状態とタイトル、説明文、アイキャッチ、更新日を更新
// フォルダの場合も更新する
if($content['Content']['alias_id'] == $data['Content']['id'] || ($content['Content']['type'] == 'ContentFolder' && $isContentFolder)) {
$content['Content']['name'] = urldecode($data['Content']['name']);
$content['Content']['title'] = $data['Content']['title'];
$content['Content']['description'] = $data['Content']['description'];
$content['Content']['self_status'] = $data['Content']['self_status'];
$content['Content']['self_publish_begin'] = $data['Content']['self_publish_begin'];
$content['Content']['self_publish_end'] = $data['Content']['self_publish_end'];
$content['Content']['created_date'] = $data['Content']['created_date'];
$content['Content']['modified_date'] = $data['Content']['modified_date'];
$content['Content']['exclude_search'] = $data['Content']['exclude_search'];
if(!empty($data['Content']['eyecatch'])) {
$content['Content']['eyecatch'] = $data['Content']['eyecatch'];
}
$url = $data['Content']['url'];
if($content['Content']['type'] == 'ContentFolder') {
$url = preg_replace('/\/[^\/]+\/$/', '/', $url);
}
$content['Content']['parent_id'] = $this->copyContentFolderPath($url, $site['Site']['id']);
} else {
$content['Content']['name'] = urldecode($data['Content']['name']);
}
if(!$this->save($content)) {
$result = false;
}
} else {
// 存在しない場合はエイリアスを作成
// フォルダの場合は実体として作成
$content = $data;
unset($content['Content']['id']);
unset($content['Content']['name']);
unset($content['Content']['url']);
unset($content['Content']['lft']);
unset($content['Content']['rght']);
unset($content['Content']['created_date']);
unset($content['Content']['modified_date']);
unset($content['Content']['created']);
unset($content['Content']['modified']);
unset($content['Content']['layout_template']);
$content['Content']['name'] = $data['Content']['name'];
$content['Content']['main_site_content_id'] = $data['Content']['id'];
$content['Content']['site_id'] = $site['Site']['id'];
$url = $data['Content']['url'];
if($content['Content']['type'] == 'ContentFolder') {
$url = preg_replace('/\/[^\/]+\/$/', '/', $url);
unset($content['Content']['entity_id']);
} else {
$content['Content']['alias_id'] = $data['Content']['id'];
}
$content['Content']['parent_id'] = $this->copyContentFolderPath($url, $site['Site']['id']);
$CreateModel->create($content);
if(!$CreateModel->save()) {
$result = false;
}
}
}
// 退避したデータを戻す
$this->data = $dataTmp;
$this->id = $idTmp;
return $result;
}
/**
* サブサイトのプレフィックスがついていない純粋なURLを取得
*
* @param string $url
* @param int $siteId
* @return mixed
*/
public function pureUrl($url, $siteId) {
$site = BcSite::findById($siteId);
return $site->getPureUrl($url);
}
/**
* Content data を作成して保存する
*
* @param array $content
* @param string $plugin
* @param string $type
* @param int $entityId
*/
public function createContent($content, $plugin, $type, $entityId = null, $validate = true) {
if(isset($content['Content'])) {
$content = $content['Content'];
}
$content['plugin'] = $plugin;
$content['type'] = $type;
$content['entity_id'] = $entityId;
if(!isset($content['deleted'])) {
$content['deleted'] = false;
}
if(!isset($content['site_root'])) {
$content['site_root'] = 0;
}
if(!isset($content['exclude_search'])) {
$content['exclude_search'] = 0;
}
if(!isset($content['created_date'])) {
$content['created_date'] = date('Y-m-d H:i:s');
}
$this->create($content);
return $this->save(null, $validate);
}
/**
* コンテンツデータよりURLを生成する
*
* @param int $id コンテンツID
* @param string $plugin プラグイン
* @param string $type タイプ
* @return mixed URL | false
*/
public function createUrl($id, $plugin = null, $type = null) {
$id = (int) $id;
// @deprecated 5.0.0 since 4.0.2 $plugin / $type の引数は不要
if(!$id) {
return false;
} elseif($id == 1) {
$url = '/';
} else {
// サイト全体のURLを変更する場合、TreeBehavior::getPath() を利用するとかなりの時間がかかる為、
// DataSource::query() を利用する
$db = $this->getDataSource();
$sql = "SELECT lft, rght FROM {$this->tablePrefix}contents AS Content WHERE id = {$id} AND deleted = " . $db->value(false, 'boolean');
$content = $db->query($sql, false);
if(!$content) {
return false;
}
if(isset($content[0]['Content'])) {
$content = $content[0]['Content'];
} else {
$content = $content[0][0];
}
$sql = "SELECT name, plugin, type FROM {$this->tablePrefix}contents AS Content " .
"WHERE lft <= {$db->value($content['lft'], 'integer')} AND rght >= {$db->value($content['rght'], 'integer')} AND deleted = " . $db->value(false, 'boolean') . " " .
"ORDER BY lft ASC";
$parents = $db->query($sql, false);
unset($parents[0]);
if(!$parents) {
return false;
}
$names = [];
$content = null;
foreach($parents as $parent) {
if(isset($parent['Content'])) {
$parent = $parent['Content'];
} else {
$parent = $parent[0];
}
$names[] = $parent['name'];
$content = $parent;
}
$plugin = $content['plugin'];
$type = $content['type'];
$url = '/' . implode('/', $names);
$setting = $omitViewAction = Configure::read('BcContents.items.' . $plugin . '.' . $type);
if($type == 'ContentFolder' || empty($setting['omitViewAction'])) {
$url .= '/';
}
}
return $url;
}
/**
* システムデータを更新する
*
* URL / 公開状態 / メインサイトの関連コンテンツID
*
* @param array $data
* @return mixed
*/
public function updateSystemData($data) {
if(empty($data['Content']['name'])) {
if($data['Content']['id'] != 1) {
return false;
}
}
$site = $this->Site->find('first', ['conditions' => ['Site.id' => $data['Content']['site_id']]]);
// URLを更新
$data['Content']['url'] = $this->createUrl($data['Content']['id'], $data['Content']['plugin'], $data['Content']['type']);
// 親フォルダの公開状態に合わせて公開状態を更新(自身も含める)
if(isset($data['Content']['self_status'])) {
$data['Content']['status'] = $data['Content']['self_status'];
}
// null の場合、isset で判定できないので array_key_exists を利用
if(array_key_exists('self_publish_begin', $data['Content'])) {
$data['Content']['publish_begin'] = $data['Content']['self_publish_begin'];
}
if(array_key_exists('self_publish_end', $data['Content'])) {
$data['Content']['publish_end'] = $data['Content']['self_publish_end'];
}
if(!empty($data['Content']['parent_id'])) {
$parent = $this->find('first', [
'fields' => ['name', 'status', 'publish_begin', 'publish_end'],
'conditions' => ['Content.id' => $data['Content']['parent_id']],
'recursive' => -1
]);
if(!$parent['Content']['status'] || $parent['Content']['publish_begin'] || $parent['Content']['publish_begin']) {
$data['Content']['status'] = $parent['Content']['status'];
$data['Content']['publish_begin'] = $parent['Content']['publish_begin'];
$data['Content']['publish_end'] = $parent['Content']['publish_end'];
}
}
// 主サイトの関連コンテンツIDを更新
if($site) {
// 主サイトの同一階層のコンテンツを特定
$prefix = $site['Site']['name'];
if($site['Site']['alias']) {
$prefix = $site['Site']['alias'];
}
$url = preg_replace('/^\/' . preg_quote($prefix, '/') . '\//', '/', $data['Content']['url']);
$mainSitePrefix = $this->Site->getPrefix($site['Site']['main_site_id']);
if($mainSitePrefix) {
$url = '/' . $mainSitePrefix . $url;
}
$mainSiteContentId = $this->field('id', [
'site_id' => $site['Site']['main_site_id'],
'url' => $url
]);
// main_site_content_id を更新
if($mainSiteContentId) {
$data['Content']['main_site_content_id'] = $mainSiteContentId;
} else {
$data['Content']['main_site_content_id'] = null;
}
}
$data = $this->save($data, ['validate' => false, 'callbacks' => false]);
$this->data = $data;
return (bool) ($data);
}
/**
* ID を指定して公開状態かどうか判定する
*
* @param $id
* @return bool
*/
public function isPublishById($id) {
$conditions = array_merge(['Content.id' => $id], $this->getConditionAllowPublish());
return (bool) $this->find('first', ['conditions' => $conditions, 'recursive' => -1]);
}
/**
* 子ノードのURLを全て更新する
*
* @param $id
* @return bool
*/
public function updateChildren($id) {
// 他のデータを更新する為一旦退避
$dataTmp = $this->data;
$idTmp = $this->id;
$children = $this->children($id, false, null, 'Content.lft');
$result = true;
if($children) {
foreach($children as $child) {
if(!$this->updateSystemData($child)) {
$result = false;
}
}
}
// 退避したデータを戻す
$this->data = $dataTmp;
$this->id = $idTmp;
return $result;
}
/**
* タイプよりコンテンツを取得する
*
* @param string $type 例)Blog.BlogContent
* @param int $entityId
* @return array
*/
public function findByType($type, $entityId = null) {
list($plugin, $type) = pluginSplit($type);
if(!$plugin) {
$plugin = 'Core';
}
$conditions = [
'Content.plugin' => $plugin,
'Content.type' => $type,
'Content.alias_id' => null
];
if($entityId) {
$conditions['Content.entity_id'] = $entityId;
}
return $this->find('first', ['conditions' => $conditions, 'order' => ['Content.id']]);
}
/**
* コンテンツフォルダーのリストを取得
* コンボボックス用
*
* @param int $siteId
* @param array $options
* @return array|bool
*/
public function getContentFolderList($siteId = null, $options = []) {
$options = array_merge([
'excludeId' => null
], $options);
$conditions = [
'type' => 'ContentFolder',
'alias_id' => null
];
if(!is_null($siteId)) {
$conditions['site_id'] = $siteId;
}
if($options['excludeId']) {
$conditions['id <>'] = $options['excludeId'];
}
if(!empty($options['conditions'])) {
$conditions = array_merge($conditions, $options['conditions']);
}
$folders = $this->generateTreeList($conditions);
if($folders) {
return $this->convertTreeList($folders);
}
return false;
}
/**
* ツリー構造のデータを コンボボックスのデータ用に変換する
* @param $nodes
* @return array
*/
public function convertTreeList($nodes) {
if(!$nodes) {
return [];
}
foreach ($nodes as $key => $value) {
if (preg_match("/^([_]+)/i", $value, $matches)) {
$value = preg_replace("/^[_]+/i", '', $value);
$prefix = str_replace('_', ' ', $matches[1]);
$value = $prefix . '└' . $value;
}
$nodes[$key] = $value;
}
return $nodes;
}
/**
* ツリー構造より論理削除する
*
* @param $id
* @return bool
*/
public function softDeleteFromTree($id) {
$this->softDelete(true);
$this->Behaviors->unload('BcCache');
$this->Behaviors->unload('BcUpload');
$result = $this->deleteRecursive($id);
$this->Behaviors->load('BcCache');
$this->Behaviors->load('BcUpload');
$this->delAssockCache();
return $result;
}
/**
* 再帰的に削除
*
* エイリアスの場合
*
* @param $id
* @return bool
*/
public function deleteRecursive($id) {
if(!$id) {
return false;
}
$children = $this->children($id, true);
$result = true;
if($children) {
foreach($children as $child) {
if(!$this->deleteRecursive($child['Content']['id'])) {
$result = false;
}
}
}
if($result) {
$content = $this->find('first', [
'conditions' => ['Content.id' => $id],
'recursive' => -1
]);
if(empty($content['Content']['alias_id'])) {
// エイリアス以外の場合
// 一旦階層構造から除外しリセットしてゴミ箱に移動(論理削除)
$content['Content']['parent_id'] = null;
$content['Content']['url'] = '';
$content['Content']['status'] = false;
$content['Content']['self_status'] = false;
unset($content['Content']['lft']);
unset($content['Content']['rght']);
$this->updatingSystemData = false;
// ここでは callbacks を false にすると lft rght が更新されないので callbacks は true に設定する(default: true)
$this->clear();
$this->save($content, ['validate' => false]);
$this->updatingSystemData = true;
$result = $this->delete($id);
// =====================================================================
// 通常の削除の際、afterDelete で、関連コンテンツのキャッシュを削除しているが、
// 論理削除の場合、afterDelete が呼ばれない為、ここで削除する
// =====================================================================
$this->deleteAssocCache($content);
return $result;
} else {
// エイリアスの場合、直接削除
$softDelete = $this->softDelete(null);
$this->softDelete(false);
$result = $this->removeFromTree($content['Content']['id'], true);
$this->softDelete($softDelete);
return $result;
}
}
return false;