/
BlogPost.php
executable file
·694 lines (625 loc) · 18.4 KB
/
BlogPost.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
<?php
/**
* 記事モデル
*
* baserCMS : Based Website Development Project <http://basercms.net>
* Copyright 2008 - 2015, baserCMS Users Community <http://sites.google.com/site/baserusers/>
*
* @copyright Copyright 2008 - 2015, baserCMS Users Community
* @link http://basercms.net baserCMS Project
* @package Blog.Model
* @since baserCMS v 0.1.0
* @license http://basercms.net/license/index.html
*/
/**
* Include files
*/
App::uses('BlogAppModel', 'Blog.Model');
/**
* 記事モデル
*
* @package Blog.Model
*/
class BlogPost extends BlogAppModel {
/**
* クラス名
*
* @var string
* @access public
*/
public $name = 'BlogPost';
/**
* 検索テーブルへの保存可否
*
* @var boolean
* @access public
*/
public $contentSaving = true;
/**
* ビヘイビア
*
* @var array
* @access public
*/
public $actsAs = array(
'BcContentsManager',
'BcCache',
'BcUpload' => array(
'subdirDateFormat' => 'Y/m/',
'fields' => array(
'eye_catch' => array(
'type' => 'image',
'namefield' => 'no',
'nameformat' => '%08d'
)
)
)
);
/**
* belongsTo
*
* @var array
* @access public
*/
public $belongsTo = array(
'BlogCategory' => array(
'className' => 'Blog.BlogCategory',
'foreignKey' => 'blog_category_id'),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'),
'BlogContent' => array(
'className' => 'Blog.BlogContent',
'foreignKey' => 'blog_content_id')
);
/**
* hasMany
*
* @var array
* @access public
*/
public $hasMany = array(
'BlogComment' => array(
'className' => 'Blog.BlogComment',
'order' => 'created',
'foreignKey' => 'blog_post_id',
'dependent' => true,
'exclusive' => false,
'finderQuery' => '')
);
/**
* HABTM
*
* @var array
* @access public
*/
public $hasAndBelongsToMany = array(
'BlogTag' => array(
'className' => 'Blog.BlogTag',
'joinTable' => 'blog_posts_blog_tags',
'foreignKey' => 'blog_post_id',
'associationForeignKey' => 'blog_tag_id',
'conditions' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderQuery' => '',
'deleteQuery' => ''
));
/**
* validate
*
* @var array
* @access public
*/
public $validate = array(
'name' => array(
array('rule' => array('notEmpty'),
'message' => 'タイトルを入力してください。',
'required' => true),
array('rule' => array('maxLength', 255),
'message' => 'タイトルは255文字以内で入力してください。')
),
'detail' => array(
array('rule' => array('maxByte', 64000),
'message' => '本稿欄に保存できるデータ量を超えています。')
),
'detail_draft' => array(
array('rule' => array('maxByte', 64000),
'message' => '草稿欄に保存できるデータ量を超えています。')
),
'publish_begin' => array(
array('rule' => array('checkDate'),
'message' => '公開開始日の形式が不正です。')
),
'publish_end' => array(
array('rule' => array('checkDate'),
'message' => '公開終了日の形式が不正です。')
),
'posts_date' => array(
array('rule' => array('notEmpty'),
'message' => '投稿日を入力してください。',
'required' => true),
array('rule' => array('checkDate'),
'message' => '投稿日の形式が不正です。')
),
'user_id' => array(
array('rule' => array('notEmpty'),
'message' => '投稿者を選択してください。')
)
);
/**
* アップロードビヘイビアの設定
*
* @param int $id ブログコンテンツID
*/
public function setupUpload($id) {
$sizes = array('thumb', 'mobile_thumb');
$data = $this->BlogContent->find('first', array('conditions' => array('BlogContent.id' => $id)));
$data = $this->BlogContent->constructEyeCatchSize($data);
$data = $data['BlogContent'];
$imagecopy = array();
foreach ($sizes as $size) {
if (!isset($data['eye_catch_size_' . $size . '_width']) || !isset($data['eye_catch_size_' . $size . '_height'])) {
continue;
}
$imagecopy[$size] = array('suffix' => '__' . $size);
$imagecopy[$size]['width'] = $data['eye_catch_size_' . $size . '_width'];
$imagecopy[$size]['height'] = $data['eye_catch_size_' . $size . '_height'];
}
$settings = $this->Behaviors->BcUpload->settings['BlogPost'];
if (empty($settings['saveDir']) || !preg_match('/^' . preg_quote("blog" . DS . $data['name'], '/') . '\//', $settings['saveDir'])) {
$settings['saveDir'] = "blog" . DS . $data['name'] . DS . "blog_posts";
}
$settings['fields']['eye_catch']['imagecopy'] = $imagecopy;
$this->Behaviors->attach('BcUpload', $settings);
}
/**
* 初期値を取得する
*
* @return array $authUser 初期値データ
* @access public
*/
public function getDefaultValue($authUser) {
$data[$this->name]['user_id'] = $authUser['id'];
$data[$this->name]['posts_date'] = date('Y/m/d H:i:s');
$data[$this->name]['status'] = 0;
return $data;
}
/**
* ブログの月別一覧を取得する
*
* @param int $blogContentId ブログコンテンツID
* @param array $options オプション
* @return array 月別リストデータ
* @access public
*/
public function getPostedDates($blogContentId, $options) {
$options = array_merge(array(
'category' => false,
'limit' => false,
'viewCount' => false,
'type' => 'month' // month Or year
), $options);
extract($options);
$conditions = array('BlogPost.blog_content_id' => $blogContentId);
$conditions = am($conditions, $this->getConditionAllowPublish());
// TODO CSVDBではGROUP BYが実装されていない為、取り急ぎPHPで処理
/* $dates = $this->find('all',array('fields'=>array('YEAR(posts_date) as year','MONTH(posts_date) as month','COUNT(id)' as count),
$conditions,
'group'=>array('YEAR(posts_date)','MONTH(posts_date)')))); */
if ($category) {
$recursive = 1;
$this->unbindModel(array(
'belongsTo' => array('User', 'BlogContent'),
'hasAndBelongsToMany' => array('BlogTag')
));
} else {
$recursive = -1;
}
// 毎秒抽出条件が違うのでキャッシュしない
$posts = $this->find('all', array(
'conditions' => $conditions,
'order' => 'BlogPost.posts_date DESC',
'recursive' => $recursive,
'cache' => false
));
$dates = array();
$counter = 0;
foreach ($posts as $post) {
$exists = false;
$_date = array();
$year = date('Y', strtotime($post['BlogPost']['posts_date']));
$month = date('m', strtotime($post['BlogPost']['posts_date']));
$categoryId = $post['BlogPost']['blog_category_id'];
foreach ($dates as $key => $date) {
if (!$category || $date['BlogCategory']['id'] == $categoryId) {
if ($type == 'year' && $date['year'] == $year) {
$exists = true;
}
if ($type == 'month' && $date['year'] == $year && $date['month'] == $month) {
$exists = true;
}
}
if ($exists) {
if ($viewCount) {
$dates[$key]['count'] ++;
}
break;
}
}
if (!$exists) {
if ($type == 'year') {
$_date['year'] = $year;
} elseif ($type == 'month') {
$_date['year'] = $year;
$_date['month'] = $month;
}
if ($category) {
$_date['BlogCategory']['id'] = $categoryId;
$_date['BlogCategory']['name'] = $post['BlogCategory']['name'];
$_date['BlogCategory']['title'] = $post['BlogCategory']['title'];
}
if ($viewCount) {
$_date['count'] = 1;
}
$dates[] = $_date;
$counter++;
}
if ($limit !== false && $limit <= $counter) {
break;
}
}
return $dates;
}
/**
* カレンダー用に指定した月で記事の投稿がある日付のリストを取得する
*
* @param int $blogContentId ブログコンテンツID
* @param int $year 年
* @param int $month 月
* @return array
* @access public
*/
public function getEntryDates($blogContentId, $year, $month) {
$entryDates = $this->find('all', array(
'fields' => array('BlogPost.posts_date'),
'conditions' => $this->_getEntryDatesConditions($blogContentId, $year, $month),
'recursive' => -1,
'cache' => false
));
$entryDates = Hash::extract($entryDates, '{n}.BlogPost.posts_date');
foreach ($entryDates as $key => $entryDate) {
$entryDates[$key] = date('Y-m-d', strtotime($entryDate));
}
return $entryDates;
}
/**
* 投稿者の一覧を取得する
*
* @param int $blogContentId ブログコンテンツID
* @param array $options オプション
* @return array
*/
public function getAuthors($blogContentId, $options) {
$options = array_merge(array(
'viewCount' => false
), $options);
extract($options);
$users = $this->User->find('all', array('recursive' => -1, array('order' => 'User.id'), 'fields' => array(
'User.id', 'User.name', 'User.real_name_1', 'User.real_name_2', 'User.nickname'
)));
$availableUsers = array();
foreach ($users as $key => $user) {
$count = $this->find('count', array('conditions' => array_merge(array(
'BlogPost.user_id' => $user['User']['id'],
'BlogPost.blog_content_id' => $blogContentId
), $this->getConditionAllowPublish())));
if ($count) {
if ($viewCount) {
$user['count'] = $count;
}
$availableUsers[] = $user;
}
}
return $availableUsers;
}
/**
* 指定した月の記事が存在するかチェックする
*
* @param int $blogContentId
* @param int $year
* @param int $month
* @return boolean
*/
public function existsEntry($blogContentId, $year, $month) {
if ($this->find('first', array(
'fields' => array('BlogPost.id'),
'conditions' => $this->_getEntryDatesConditions($blogContentId, $year, $month),
'recursive' => -1,
'cache' => false
))) {
return true;
} else {
return false;
}
}
/**
* 年月を指定した検索条件を生成
* データベースごとに構文が違う
*
* @param int $blogContentId
* @param int $year
* @param int $month
* @return string
* @access private
*/
protected function _getEntryDatesConditions($blogContentId, $year, $month) {
$dbConfig = new DATABASE_CONFIG();
$datasource = $dbConfig->plugin['datasource'];
switch ($datasource) {
case 'Database/BcMysql':
case 'Database/BcCsv':
if (!empty($year)) {
$conditions["YEAR(`BlogPost`.`posts_date`)"] = $year;
} else {
$conditions["YEAR(`BlogPost`.`posts_date`)"] = date('Y');
}
if (!empty($month)) {
$conditions["MONTH(`BlogPost`.`posts_date`)"] = $month;
} else {
$conditions["MONTH(`BlogPost`.`posts_date`)"] = date('m');
}
break;
case 'Database/BcPostgres':
if (!empty($year)) {
$conditions["date_part('year', \"BlogPost\".\"posts_date\") ="] = $year;
} else {
$conditions["date_part('year', \"BlogPost\".\"posts_date\") ="] = date('Y');
}
if (!empty($month)) {
$conditions["date_part('month', \"BlogPost\".\"posts_date\") ="] = $month;
} else {
$conditions["date_part('month', \"BlogPost\".\"posts_date\") ="] = date('m');
}
break;
case 'Database/BcSqlite':
if (!empty($year)) {
$conditions["strftime('%Y',BlogPost.posts_date)"] = $year;
} else {
$conditions["strftime('%Y',BlogPost.posts_date)"] = date('Y');
}
if (!empty($month)) {
$conditions["strftime('%m',BlogPost.posts_date)"] = sprintf('%02d', $month);
} else {
$conditions["strftime('%m',BlogPost.posts_date)"] = date('m');
}
break;
}
$conditions = am($conditions, array('BlogPost.blog_content_id' => $blogContentId), $this->getConditionAllowPublish());
return $conditions;
}
/**
* コントロールソースを取得する
*
* @param string $field フィールド名
* @param array $options
* @return array コントロールソース
* @access public
*/
public function getControlSource($field, $options = array()) {
switch ($field) {
case 'blog_category_id':
extract($options);
$catOption = array('blogContentId' => $blogContentId);
$isSuperAdmin = false;
if (!empty($userGroupId)) {
if (!isset($blogCategoryId)) {
$blogCategoryId = '';
}
if ($userGroupId == 1) {
$isSuperAdmin = true;
}
// 現在のページが編集不可の場合、現在表示しているカテゴリも取得する
if (!$postEditable && $blogCategoryId) {
$catOption['conditions'] = array('OR' => array('BlogCategory.id' => $blogCategoryId));
}
// super admin でない場合は、管理許可のあるカテゴリのみ取得
if (!$isSuperAdmin) {
$catOption['ownerId'] = $userGroupId;
}
if ($postEditable && !$rootEditable && !$isSuperAdmin) {
unset($empty);
}
}
$categories = $this->BlogCategory->getControlSource('parent_id', $catOption);
// 「指定しない」追加
if (isset($empty)) {
if ($categories) {
$categories = array('' => $empty) + $categories;
} else {
$categories = array('' => $empty);
}
}
$controlSources['blog_category_id'] = $categories;
break;
case 'user_id':
$controlSources['user_id'] = $this->User->getUserList($options);
break;
case 'blog_tag_id':
$controlSources['blog_tag_id'] = $this->BlogTag->find('list');
break;
}
if (isset($controlSources[$field])) {
return $controlSources[$field];
} else {
return false;
}
}
/**
* 公開状態を取得する
*
* @param array データリスト
* @return boolean 公開状態
* @access public
*/
public function allowPublish($data) {
if (isset($data['BlogPost'])) {
$data = $data['BlogPost'];
}
$allowPublish = (int)$data['status'];
if ($data['publish_begin'] == '0000-00-00 00:00:00') {
$data['publish_begin'] = null;
}
if ($data['publish_end'] == '0000-00-00 00:00:00') {
$data['publish_end'] = null;
}
// 期限を設定している場合に条件に該当しない場合は強制的に非公開とする
if (($data['publish_begin'] && $data['publish_begin'] >= date('Y-m-d H:i:s')) ||
($data['publish_end'] && $data['publish_end'] <= date('Y-m-d H:i:s'))) {
$allowPublish = false;
}
return $allowPublish;
}
/**
* 公開済の conditions を取得
*
* @return array
* @access public
*/
public function getConditionAllowPublish() {
$conditions[$this->alias . '.status'] = true;
$conditions[] = array('or' => array(array($this->alias . '.publish_begin <=' => date('Y-m-d H:i:s')),
array($this->alias . '.publish_begin' => null),
array($this->alias . '.publish_begin' => '0000-00-00 00:00:00')));
$conditions[] = array('or' => array(array($this->alias . '.publish_end >=' => date('Y-m-d H:i:s')),
array($this->alias . '.publish_end' => null),
array($this->alias . '.publish_end' => '0000-00-00 00:00:00')));
return $conditions;
}
/**
* 公開状態の記事を取得する
*
* @param array $options
* @return array
* @access public
*/
public function getPublishes($options) {
if (!empty($options['conditions'])) {
$options['conditions'] = array_merge($this->getConditionAllowPublish(), $options['conditions']);
} else {
$options['conditions'] = $this->getConditionAllowPublish();
}
// 毎秒抽出条件が違うのでキャッシュしない
$options['cache'] = false;
$datas = $this->find('all', $options);
return $datas;
}
/**
* afterSave
*
* @param boolean $created
* @return boolean
* @access public
*/
public function afterSave($created, $options = array()) {
// 検索用テーブルへの登録・削除
if ($this->contentSaving && !$this->data['BlogPost']['exclude_search']) {
$this->saveContent($this->createContent($this->data));
} else {
if (!empty($this->data['BlogPost']['id'])) {
$this->deleteContent($this->data['BlogPost']['id']);
} elseif (!empty($this->id)) {
$this->deleteContent($this->id);
} else {
$this->cakeError('Not found pk-value in BlogPost.');
}
}
}
/**
* 検索用データを生成する
*
* @param array $data
* @return array
* @access public
*/
public function createContent($data) {
if (isset($data['BlogPost'])) {
$data = $data['BlogPost'];
}
$_data = array();
$_data['Content']['type'] = 'ブログ';
$_data['Content']['model_id'] = $this->id;
$_data['Content']['category'] = '';
if (!empty($data['blog_category_id'])) {
$BlogCategory = ClassRegistry::init('Blog.BlogCategory');
$categoryPath = $BlogCategory->getPath($data['blog_category_id'], array('title'));
if ($categoryPath) {
$_data['Content']['category'] = $categoryPath[0]['BlogCategory']['title'];
}
}
$_data['Content']['title'] = $data['name'];
$_data['Content']['detail'] = $data['content'] . ' ' . $data['detail'];
$PluginContent = ClassRegistry::init('PluginContent');
$_data['Content']['url'] = '/' . $PluginContent->field('name', array('PluginContent.content_id' => $data['blog_content_id'], 'plugin' => 'blog')) . '/archives/' . $data['no'];
$_data['Content']['status'] = $this->allowPublish($data);
return $_data;
}
/**
* beforeDelete
*
* @return boolean
* @access public
*/
public function beforeDelete($cascade = true) {
return $this->deleteContent($this->id);
}
/**
* コピーする
*
* @param int $id
* @param array $data
* @return mixed page Or false
*/
public function copy($id = null, $data = array()) {
$data = array();
if ($id) {
$data = $this->find('first', array('conditions' => array('BlogPost.id' => $id), 'recursive' => 1));
}
if (!empty($_SESSION['Auth']['User'])) {
$data['BlogPost']['user_id'] = $_SESSION['Auth']['User']['id'];
}
$data['BlogPost']['name'] .= '_copy';
$data['BlogPost']['no'] = $this->getMax('no', array('BlogPost.blog_content_id' => $data['BlogPost']['blog_content_id'])) + 1;
$data['BlogPost']['status'] = '0'; // TODO intger の為 false では正常に保存できない(postgreSQLで確認)
unset($data['BlogPost']['id']);
unset($data['BlogPost']['created']);
unset($data['BlogPost']['modified']);
// 一旦退避(afterSaveでリネームされてしまうのを避ける為)
$eyeCatch = $data['BlogPost']['eye_catch'];
unset($data['BlogPost']['eye_catch']);
if (!empty($data['BlogTag'])) {
foreach ($data['BlogTag'] as $key => $tag) {
$data['BlogTag'][$key] = $tag['id'];
}
}
$this->create($data);
$result = $this->save();
if ($result) {
if ($eyeCatch) {
$data['BlogPost']['id'] = $this->getLastInsertID();
$data['BlogPost']['eye_catch'] = $eyeCatch;
$this->set($data);
$this->renameToFieldBasename(true); // 内部でリネームされたデータが再セットされる
$result = $this->save();
}
return $result;
} else {
if (isset($this->validationErrors['name'])) {
return $this->copy(null, $data);
} else {
return false;
}
}
}
}