forked from baserproject/basercms
/
BcPageHelper.php
executable file
·387 lines (334 loc) · 9.71 KB
/
BcPageHelper.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
<?php
/**
* ページヘルパー
*
* baserCMS : Based Website Development Project <http://basercms.net>
* Copyright 2008 - 2014, baserCMS Users Community <http://sites.google.com/site/baserusers/>
*
* @copyright Copyright 2008 - 2014, baserCMS Users Community
* @link http://basercms.net baserCMS Project
* @package Baser.View.Helper
* @since baserCMS v 0.1.0
* @license http://basercms.net/license/index.html
*/
App::uses('Helper', 'View');
/**
* ページヘルパー
*
* @package Baser.View.Helper
*/
class BcPageHelper extends Helper {
/**
* ページモデル
*
* @var Page
* @access public
*/
public $Page = null;
/**
* data
* @var array
* @access public
*/
public $data = array();
/**
* ヘルパー
*
* @var array
*/
public $helpers = array('BcBaser');
/**
* construct
*
* @param object $View
* @return void
*/
public function __construct(View $View) {
parent::__construct($View);
if (ClassRegistry::isKeySet('Page')) {
$this->Page = ClassRegistry::getObject('Page');
} else {
$this->Page = ClassRegistry::init('Page', 'Model');
}
}
/**
* beforeRender
*
* @param string $viewFile (継承もとで利用中) The view file that is going to be rendered
* @return void
*/
public function beforeRender($viewFile) {
//if ($this->request->params['controller'] == 'pages' && ($this->request->params['action'] == 'display' || $this->request->params['action'] == 'smartphone_display') && isset($this->request->params['pass'][0])) {
if ($this->request->params['controller'] == 'pages' && preg_match('/(^|_)display$/', $this->request->params['action']) && isset($this->request->params['pass'][0])) {
// @TODO ページ機能が.html拡張子なしに統合できたらコメントアウトされたものに切り替える
//$this->request->data = $this->Page->findByUrl('/'.impload('/',$this->request->params['pass'][0]));
$param = Configure::read('BcRequest.pureUrl');
if ($param && preg_match('/\/$/is', $param)) {
$param .= 'index';
}
if (Configure::read('BcRequest.agent')) {
$agentPrefix = Configure::read('BcRequest.agentPrefix');
if(empty($this->BcBaser->siteConfig['linked_pages_' . $agentPrefix])) {
$param = $agentPrefix . '/' . $param;
}
}
$param = preg_replace("/\.html$/", '', $param);
$this->request->data = $this->Page->findByUrl('/' . $param);
}
}
/**
* ページ機能用URLを取得する
*
* @param array $page
* @return string
*/
public function getUrl($page) {
return $this->Page->getPageUrl($page);
}
/**
* 現在のページが所属するカテゴリデータを取得する
*
* @return array 失敗すると getCategory() は FALSE を返します。
*/
public function getCategory() {
if (!empty($this->request->data['PageCategory']['id'])) {
return $this->request->data['PageCategory'];
} else {
return false;
}
}
/**
* 現在のページが所属する親のカテゴリを取得する
*
* @param boolean $top 親カテゴリが存在するかどうか、 オプションのパラメータ、初期値はオプションのパラメータ、初期値は false
* @return array
*/
public function getParentCategory($top = false) {
$category = $this->getCategory();
if (empty($category['id'])) {
return false;
}
if ($top) {
$path = $this->Page->PageCategory->getPath($category['id']);
if ($path) {
$parent = $path[0];
} else {
return false;
}
} else {
$parent = $this->Page->PageCategory->getParentNode($category['id']);
}
return $parent;
}
/**
* ページリストを取得する
*
* @param int $pageCategoryId
* @param int $recursive
* @return array
*/
public function getPageList($pageCategoryId, $recursive = null) {
return $this->requestAction('/contents/get_page_list_recursive', array('pass' => array($pageCategoryId, $recursive)));
}
/**
* カテゴリ名を取得する
*
* @return mixed string / false
*/
public function getCategoryName() {
$category = $this->getCategory();
if ($category['name']) {
return $category['name'];
} else {
return false;
}
}
/**
* 公開状態を取得する
*
* @param array データリスト
* @return boolean 公開状態
*/
public function allowPublish($data) {
if (isset($data['Page'])) {
$data = $data['Page'];
}
$allowPublish = (int) $data['status'];
// 期限を設定している場合に条件に該当しない場合は強制的に非公開とする
if (($data['publish_begin'] != 0 && $data['publish_begin'] >= date('Y-m-d H:i:s')) ||
($data['publish_end'] != 0 && $data['publish_end'] <= date('Y-m-d H:i:s'))) {
$allowPublish = false;
}
return $allowPublish;
}
/**
* ページカテゴリ間の次の記事へのリンクを取得する
*
* @param array $post
* @param string $title
* @param array $attributes
* @return void コンテンツナビが無効の場合のみ、空文字を返す
*/
public function nextLink($title = '', $attributes = array()) {
if (!$this->contensNaviAvailable()) {
return '';
}
if (ClassRegistry::isKeySet('Page')) {
$PageClass = ClassRegistry::getObject('Page');
} else {
$PageClass = ClassRegistry::init('Page');
}
$_attributes = array('class' => 'next-link', 'arrow' => ' ≫');
$attributes = am($_attributes, $attributes);
$arrow = $attributes['arrow'];
unset($attributes['arrow']);
$conditions = am(array(
'Page.sort >' => $this->request->data['Page']['sort'],
'Page.page_category_id' => $this->request->data['Page']['page_category_id']
), $PageClass->getConditionAllowPublish());
$nextPost = $PageClass->find('first', array(
'conditions' => $conditions,
'fields' => array('title', 'url'),
'order' => 'sort',
'recursive' => -1,
'cache' => false
));
if ($nextPost) {
if (!$title) {
$title = $nextPost['Page']['title'] . $arrow;
}
$this->BcBaser->link($title, preg_replace('/^\/mobile/', '/m', $nextPost['Page']['url']), $attributes);
}
}
/**
* ページカテゴリ間の前の記事へのリンクを取得する
*
* @param array $post
* @param string $title
* @param array $attributes
* @return void
*/
public function prevLink($title = '', $attributes = array()) {
if (!$this->contensNaviAvailable()) {
return '';
}
if (ClassRegistry::isKeySet('Page')) {
$PageClass = ClassRegistry::getObject('Page');
} else {
$PageClass = ClassRegistry::init('Page');
}
$_attributes = array('class' => 'prev-link', 'arrow' => '≪ ');
$attributes = am($_attributes, $attributes);
$arrow = $attributes['arrow'];
unset($attributes['arrow']);
$conditions = am(array(
'Page.sort <' => $this->request->data['Page']['sort'],
'Page.page_category_id' => $this->request->data['Page']['page_category_id']
), $PageClass->getConditionAllowPublish());
$nextPost = $PageClass->find('first', array(
'conditions' => $conditions,
'fields' => array('title', 'url'),
'order' => 'sort DESC',
'recursive' => -1,
'cache' => false
));
if ($nextPost) {
if (!$title) {
$title = $arrow . $nextPost['Page']['title'];
}
$this->BcBaser->link($title, preg_replace('/^\/mobile/', '/m', $nextPost['Page']['url']), $attributes);
}
}
/**
* コンテンツナビ有効チェック
*
* @return boolean
*/
public function contensNaviAvailable() {
if (empty($this->request->data['Page']['page_category_id']) || empty($this->request->data['PageCategory']['contents_navi'])) {
return false;
} else {
return true;
}
}
/**
* 固定ページのコンテンツを出力する
*
* @return void
*/
public function content() {
$agent = '';
if (Configure::read('BcRequest.agentPrefix')) {
$agent = Configure::read('BcRequest.agentPrefix');
}
$path = $this->_View->getVar('pagePath');
if ($agent) {
$url = '/' . implode('/', $this->request->params['pass']);
$linked = $this->Page->isLinked($agent, $url);
if (!$linked) {
$path = $agent . DS . $path;
}
}
echo $this->_View->evaluate(getViewPath() . 'Pages' . DS . $path . '.php', $this->_View->viewVars);
}
/**
* テンプレートを取得
* セレクトボックスのソースとして利用
*
* @param string $type layout or content
* @param string $agent '' or mobile or smartphone
* @return array
*/
public function getTemplates($type = 'layout', $agent = '') {
$agentPrefix = '';
if ($agent) {
$agentPrefix = Configure::read('BcAgent.' . $agent . '.prefix');
}
$siteConfig = Configure::read('BcSite');
$themePath = WWW_ROOT . 'theme' . DS . $siteConfig['theme'] . DS;
$viewPaths = array_merge(array($themePath), App::path('View'));
$ext = Configure::read('BcApp.templateExt');
$templates = array();
foreach ($viewPaths as $viewPath) {
$templatePath = '';
switch ($type) {
case 'layout':
if (!$agentPrefix) {
$templatePath = $viewPath . 'Layouts' . DS;
} else {
$templatePath = $viewPath . 'Layouts' . DS . $agentPrefix . DS;
}
break;
case 'content':
if (!$agentPrefix) {
$templatePath = $viewPath . 'Pages' . DS . 'templates' . DS;
} else {
$templatePath = $viewPath . 'Pages' . DS . $agentPrefix . DS . 'templates' . DS;
}
break;
}
if (!$templatePath) {
continue;
}
$Folder = new Folder($templatePath);
$files = $Folder->read(true, true);
if ($files[1]) {
foreach ($files[1] as $file) {
if (preg_match('/(.+)' . preg_quote($ext) . '$/', $file, $matches)) {
if (!in_array($matches[1], $templates)) {
$templates[] = $matches[1];
}
}
}
}
}
if ($templates) {
return array_combine($templates, $templates);
} else {
return array();
}
}
public function treeList($datas, $recursive = 0) {
return $this->BcBaser->getElement('pages/index_tree_list', array('datas' => $datas, 'recursive' => $recursive));
}
}