-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTopicsController.php
More file actions
202 lines (174 loc) · 5.15 KB
/
TopicsController.php
File metadata and controls
202 lines (174 loc) · 5.15 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
<?php
/**
* Topics Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('TopicsAppController', 'Topics.Controller');
/**
* Topics Controller
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Topics\Controller
*/
class TopicsController extends TopicsAppController {
/**
* 使用するComponent
*
* @var array
*/
public $components = array(
'Paginator',
);
/**
* 使用するModel
*
* @var array
*/
public $uses = array(
'Topics.Topic',
'Topics.TopicFrameSetting',
'Topics.TopicFramesRoom',
'Topics.TopicFramesPlugin',
'Topics.TopicFramesBlock',
);
/**
* 使用するHelpers
*
* @var array
*/
public $helpers = array(
'NetCommons.DisplayNumber',
'Topics.Topics',
'Rss',
'Text'
);
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
$topicFrameSetting = $this->TopicFrameSetting->getTopicFrameSetting();
$this->set('topicFrameSetting', $topicFrameSetting['TopicFrameSetting']);
if ($this->request->is('xml')) {
$this->viewVars['topicFrameSetting']['display_type'] = TopicFrameSetting::DISPLAY_TYPE_FLAT;
$this->Components->unload('Pages.PageLayout');
}
}
/**
* index
*
* @return void
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function index() {
$topicFrameSetting = $this->viewVars['topicFrameSetting'];
$displayType = $this->viewVars['topicFrameSetting']['display_type'];
$conditions = array();
$days = isset($this->params['named']['days'])
? $this->params['named']['days']
: null;
if ($days) {
$date = new DateTime();
$date->sub(new DateInterval('P' . $days . 'D'));
$period = $date->format('Y-m-d H:i:s');
$conditions['Topic.publish_start >='] = $period;
}
if ($displayType === TopicFrameSetting::DISPLAY_TYPE_ROOMS) {
//ルームごとに表示する
$conditionsByRoom = [];
if (isset($this->request->query['room_id']) &&
! empty($this->request->query['room_id'])) {
$conditionsByRoom['Room.id'] = $this->request->query['room_id'];
}
$rooms = $this->TopicFramesRoom->getRooms(
['TopicFrameSetting' => $topicFrameSetting], $conditionsByRoom
);
$this->set('rooms', $rooms);
$topics = array();
$roomIds = array_keys($rooms);
foreach ($roomIds as $roomId) {
$conditions['Topic.room_id'] = $roomId;
$topics[$roomId] = $this->__getTopics(
['TopicFrameSetting' => $topicFrameSetting], $conditions
);
}
$this->set('topics', $topics);
$this->view = 'index_rooms';
} elseif ($displayType === TopicFrameSetting::DISPLAY_TYPE_PLUGIN) {
//プラグインごとに表示する
$conditionsByPlugin = [];
if (isset($this->request->query['plugin_key']) &&
! empty($this->request->query['plugin_key'])) {
$conditionsByPlugin['Plugin.key'] = $this->request->query['plugin_key'];
}
$plugins = $this->TopicFramesPlugin->getPlugins(
['TopicFrameSetting' => $topicFrameSetting], $conditionsByPlugin
);
$this->set('plugins', $plugins);
$topics = array();
$pluginKeys = array_keys($plugins);
foreach ($pluginKeys as $pluginKey) {
$conditions['Plugin.key'] = $pluginKey;
$topics[$pluginKey] = $this->__getTopics(
['TopicFrameSetting' => $topicFrameSetting], $conditions
);
}
$this->set('topics', $topics);
$this->view = 'index_plugins';
} else {
//フラット表示
$result = $this->__getTopics(['TopicFrameSetting' => $topicFrameSetting], $conditions);
$this->set('topics', $result['topics']);
$this->set('paging', $result['paging']);
$this->view = 'index';
}
if (! $this->viewVars['topics']) {
$this->view = 'not_found';
}
}
/**
* プラグインデータ取得
*
* @param array $topicFrameSetting TopicFrameSettingデータ
* @param array $conditions 条件配列
* @return array
*/
private function __getTopics($topicFrameSetting, $conditions) {
$options = $this->TopicFrameSetting->getQueryOptions($topicFrameSetting, $conditions);
$status = isset($this->params['named']['status'])
? $this->params['named']['status']
: 0;
$this->Paginator->settings = array(
'Topic' => $this->Topic->getQueryOptions($status, $options),
);
$maxPage = isset($this->params['named']['page'])
? $this->params['named']['page']
: '1';
$startPage = isset($this->params['named']['startPage'])
? $this->params['named']['startPage']
: $maxPage;
$topics = array();
for ($page = $startPage; $page <= $maxPage; $page++) {
$this->params['named'] = Hash::insert($this->params['named'], 'page', $page);
$result = $this->Paginator->paginate('Topic');
foreach ($result as $topic) {
unset($topic['Topic']['search_contents']);
$topics[] = $topic;
}
}
$paging = $this->request['paging'];
unset($paging['Topic']['order']);
unset($paging['Topic']['options']);
unset($paging['Topic']['paramType']);
return array('topics' => $topics, 'paging' => $paging['Topic']);
}
}