-
Notifications
You must be signed in to change notification settings - Fork 128
/
BcBaserHelper.php
executable file
·2423 lines (2192 loc) · 73.9 KB
/
BcBaserHelper.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
/**
* BcBaserHelper
*
* 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 Baser.View.Helper
* @since baserCMS v 0.1.0
* @license http://basercms.net/license/index.html
*/
/**
* Include files
*/
App::uses('AppHelper', 'View/Helper');
App::uses('BcAuthComponent', 'Controller/Component');
/**
* BcBaserHelper
*
* テーマより利用される事を前提としたヘルパーで、テーマで必要となる機能をひと通り提供する。
*
* @package Baser.View.Helper
* @property BcHtmlHelper $BcHtml BcHtmlヘルパ
* @property JsHelper $Js Jsヘルパ
* @property SessionHelper $Session Sessionヘルパ
* @property BcXmlHelper $BcXml BcXmlヘルパ
* @property BcArrayHelper $BcArray BcArrayヘルパ
* @property BcPageHelper $BcPage BcPageヘルパ
*/
class BcBaserHelper extends AppHelper {
/**
* サイト基本設定データ
*
* @var array
*/
public $siteConfig = array();
/**
* ヘルパー
*
* @var array
*/
public $helpers = array('BcHtml', 'Js', 'Session', 'BcXml', 'BcArray', 'BcPage');
/**
* ページモデル
*
* 一度初期化した後に再利用し、処理速度を向上する為にコンストラクタでセットする。
*
* @var Page
*/
protected $_Page = null;
/**
* アクセス制限設定モデル
*
* 一度初期化した後に再利用し、処理速度を向上する為にコンストラクタでセットする。
*
* @var Permission
*/
protected $_Permission = null;
/**
* カテゴリタイトル設定
*
* パンくず用の配列を取得する際、カテゴリのタイトルを取得するかどうかの判定を保持
*
* @var mixed boolean or null
*/
protected $_categoryTitleOn = true;
/**
* カテゴリタイトル
*
* @var mixed boolean or string
*/
protected $_categoryTitle = true;
/**
* BcBaserHelper を拡張するプラグインのヘルパ
*
* BcBaserHelper::_initPluginBasers() で自動的に初期化される。
*
* @var array
*/
protected $_pluginBasers = array();
/**
* コンストラクタ
*
* @param View $View ビュークラス
* @param array $settings ヘルパ設定値(BcBaserHelper では利用していない)
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
// モデルクラスをセット
// 一度初期化した後に再利用し、処理速度を向上する為にコンストラクタでセットしておく
if ($this->_View && BC_INSTALLED && !Configure::read('BcRequest.isUpdater') && !Configure::read('BcRequest.isMaintenance')) {
// DBに接続できない場合、CakePHPのエラーメッセージが表示されてしまう為、 try を利用
try {
$this->_Permission = ClassRegistry::init('Permission');
$this->_Page = ClassRegistry::init('Page');
} catch (Exception $ex) {
}
}
// サイト基本設定データをセット
if (BC_INSTALLED || isConsole()) {
$this->siteConfig = $this->_View->get('siteConfig', array());
}
// プラグインのBaserヘルパを初期化
if (BC_INSTALLED && !Configure::read('BcRequest.isUpdater') && !Configure::read('BcRequest.isMaintenance')) {
$this->_initPluginBasers();
}
}
/**
* メニューのデータを取得する
*
* 配列で全件取得する
*
* @return array|false メニューデータ、または、false
*/
public function getMenus() {
$Menu = ClassRegistry::init('Menu');
if ($Menu) {
return $Menu->find('all', array('order' => 'sort'));
}
return false;
}
/**
* タイトルを設定する
*
* @param string $title タイトル
* @param mixed $categoryTitleOn カテゴリのタイトルを含むかどうか
* @return void
*/
public function setTitle($title, $categoryTitleOn = null) {
if (!is_null($categoryTitleOn)) {
$this->_categoryTitleOn = $categoryTitleOn;
}
$this->_View->pageTitle = $title;
}
/**
* meta タグのキーワードを設定する
*
* @param string $keywords キーワード(複数の場合はカンマで区切る)
* @return void
*/
public function setKeywords($keywords) {
$this->_View->set('keywords', $keywords);
}
/**
* meta タグの説明文を設定する
*
* @param string $description 説明文
* @return void
*/
public function setDescription($description) {
$this->_View->set('description', $description);
}
/**
* レイアウトで利用する為の変数を設定する
*
* View::set() のラッパー
*
* @param string $key 変数名
* @param mixed $value 値
* @return void
*/
public function set($key, $value) {
$this->_View->set($key, $value);
}
/**
* タイトルへのカテゴリタイトルの出力有無を設定する
*
* コンテンツごとに個別設定をする為に利用する。
* パンくずにも影響する。
*
* @param bool|string|array $on true を指定した場合は、コントローラーで指定した crumbs を参照し、
* 文字列を指定した場合には、その文字列をカテゴリとして利用する。
* パンくずにリンクをつける場合には、配列で指定する。
* (例) array('name' => '会社案内', 'url' => '/company/index')
* @return void
*/
public function setCategoryTitle($on = true) {
$this->_categoryTitle = $on;
}
/**
* meta タグ用のキーワードを取得する
*
* @return string meta タグ用のキーワード
*/
public function getKeywords() {
$keywords = $this->_View->get('keywords');
if (!empty($keywords)) {
return $keywords;
}
if(!empty($this->siteConfig['keyword'])) {
return $this->siteConfig['keyword'];
}
return '';
}
/**
* meta タグ用のページ説明文を取得する
*
* @return string meta タグ用の説明文
*/
public function getDescription() {
$description = $this->_View->get('description');
if (!empty($description)) {
return $description;
}
if(!empty($this->siteConfig['description'])) {
return $this->siteConfig['description'];
}
return '';
}
/**
* タイトルタグを取得する
*
* ページタイトルと直属のカテゴリ名が同じ場合は、ページ名を省略する
* version 3.0.10 より第2引数 $categoryTitleOn は、 $options にまとめられました。
* 後方互換のために第2引数に配列型以外を指定された場合は、 $categoryTitleOn として取り扱います。
*
* @param string $separator 区切り文字
* @param array $options
* `categoryTitleOn` カテゴリタイトルを表示するかどうか boolean で指定 (初期値 : null)
* `tag` (boolean) false でタグを削除するかどうか (初期値 : true)
* `allowableTags` tagが falseの場合、削除しないタグを指定できる。詳しくは、php strip_tags のドキュメントを参考してください。 (初期値 : '')
* @return string メタタグ用のタイトルを返す
*/
public function getTitle($separator = '|', $options = array()) {
if(! is_array($options)){
$categoryTitleOn = $options;
unset($options);
$options['categoryTitleOn'] = $categoryTitleOn ;
}
$options = array_merge(array(
'categoryTitleOn' => null,
'tag' => true,
'allowableTags' => ''
), $options);
$title = array();
$crumbs = $this->getCrumbs($options['categoryTitleOn']);
if ($crumbs) {
$crumbs = array_reverse($crumbs);
foreach ($crumbs as $key => $crumb) {
if ($this->BcArray->first($crumbs, $key) && isset($crumbs[$key + 1])) {
if ($crumbs[$key + 1]['name'] == $crumb['name']) {
continue;
}
}
if(!$options['tag']){
$title[] = strip_tags($crumb['name'], $options['allowableTags']);
} else {
$title[] = $crumb['name'];
}
}
}
// サイトタイトルを追加
if (!empty($this->siteConfig['name'])) {
if(!$options['tag']){
$title[] = strip_tags($this->siteConfig['name'], $options['allowableTags']);
} else {
$title[] = $this->siteConfig['name'];
}
}
return implode($separator, $title);
}
/**
* パンくず用の配列を取得する
*
* 基本的には、コントローラーの crumbs プロパティで設定した値を取得する仕様だが
* 事前に setCategoryTitle メソッドで出力内容をカスタマイズする事ができる
*
* @param mixed $categoryTitleOn 親カテゴリの階層を表示するかどうか
* @return array パンくず用の配列
* @todo
* HTMLレンダリングも含めた状態で取得できる、HtmlHelper::getCrumbs() とメソッド名が
* 同じで、 処理内容がわかりにくいので変数名のリファクタリング要。
* ただし、BcBaserHelper::getCrumbs() は、テーマで利用されている可能性が高いので、
* 後方互換を考慮する必要がある。
*/
public function getCrumbs($categoryTitleOn = null) {
// ページカテゴリを追加
if (!is_null($categoryTitleOn)) {
$this->_categoryTitleOn = $categoryTitleOn;
}
// 親となるパンくずを取得
$crumbs = array();
if ($this->_categoryTitleOn) {
// true の場合は、コントローラーで設定された crumbs を取得
if ($this->_categoryTitle === true) {
$crumbs = $this->_View->get('crumbs', array());
} elseif (is_array($this->_categoryTitle)) {
$crumbs[] = $this->_categoryTitle;
} elseif ($this->_categoryTitle) {
$crumbs[] = array('name' => $this->_categoryTitle, 'url' => '');
}
}
// カレントのページを追加
$contentsTitle = $this->getContentsTitle();
if ($contentsTitle) {
$crumbs[] = array('name' => $contentsTitle, 'url' => '');
}
return $crumbs;
}
/**
* コンテンツタイトルを取得する
*
* @return string コンテンツタイトル
*/
public function getContentsTitle() {
if (empty($this->_View->pageTitle)) {
return '';
}
return $this->_View->pageTitle;
}
/**
* コンテンツのタイトルを出力する
*
* @return void
*/
public function contentsTitle() {
echo h($this->getContentsTitle());
}
/**
* タイトルタグを出力する
*
* @param string $separator 区切り文字
* @param string $categoryTitleOn カテゴリを表示するかどうか boolean で指定
* @return void
*/
public function title($separator = '|', $categoryTitleOn = null) {
echo '<title>' . strip_tags($this->getTitle($separator, $categoryTitleOn)) . "</title>\n";
}
/**
* キーワード用のメタタグを出力する
*
* @return void
*/
public function metaKeywords() {
echo $this->BcHtml->meta('keywords', $this->getkeywords()) . "\n";
}
/**
* ページ説明文用のメタタグを出力する
*
* @return void
*/
public function metaDescription() {
echo $this->BcHtml->meta('description', strip_tags($this->getDescription())) . "\n";
}
/**
* RSSフィードのリンクタグを出力する
*
* @param string $title RSSのタイトル
* @param string $link RSSのURL
* @return void
*/
public function rss($title, $link) {
echo $this->BcHtml->meta($title, $link, array('type' => 'rss')) . "\n";
}
/**
* トップページかどうか判断する
*
* @return bool
* @deprecated isHomeに統合する
*/
public function isTop() {
trigger_error(deprecatedMessage('ヘルパーメソッド:BcBaserHelper::isTop()', '3.0.6', '3.1.0', '$this->BcBaser->isHome() を利用してください。'), E_USER_DEPRECATED);
return $this->isHome();
}
/**
* 現在のページがトップページかどうかを判定する
*
* @return bool
*/
public function isHome() {
if (!Configure::read('BcRequest.agentAlias')) {
return (
$this->request->url == false ||
$this->request->url == 'index'
);
} else {
return (
$this->request->url == Configure::read('BcRequest.agentAlias') . '/' ||
$this->request->url == Configure::read('BcRequest.agentAlias') . '/index'
);
}
}
/**
* baserCMSが設置されているパスを出力する
*
* BcBaserHelper::getRoot() をラッピングして出力するだけの処理
*
* @return void
*/
public function root() {
echo $this->getRoot();
}
/**
* baserCMSが設置されているパスを取得する
*
* 画像タグやリンクタグを出力する際に、baserCMSの設置フォルダに
* 依存せずパスを出力する為に利用する。
*
* 《利用例》
* <img src="<?php echo $this->BcBaser->root() ?>img/test.png" />
*
* 《basercmsというフォルダに設置している場合の取得例》
* /basercms/
*
* 《basercmsというフォルダに設置し、スマートURLオフの場合の取得例》
* /basercms/index.php/
*
* @return string
*/
public function getRoot() {
return $this->request->base . '/';
}
/**
* baserCMSの設置フォルダを考慮したURLを出力する
*
* 《利用例》
* <a href="<?php $this->BcBaser->getUrl('/about') ?>">会社概要</a>
*
* @param mixed $url baserCMS設置フォルダからの絶対URL、もしくは配列形式のURL情報
* 省略した場合には、PC用のトップページのURLを出力する
* @param bool $full httpから始まるURLを取得するかどうか
* @param bool $sessionId セションIDを付加するかどうか
* @return void
*/
public function url($url = null, $full = false, $sessionId = true) {
echo $this->getUrl($url, $full, $sessionId);
}
/**
* baserCMSの設置フォルダを考慮したURLを取得する
*
* 《利用例》
* <a href="<?php echo $this->BcBaser->getUrl('/about') ?>">会社概要</a>
*
* @param mixed $url baserCMS設置フォルダからの絶対URL、もしくは配列形式のURL情報
* 省略した場合には、PC用のトップページのURLを取得する
* @param bool $full httpから始まるURLを取得するかどうか
* @param bool $sessionId セションIDを付加するかどうか
* @return string URL
*/
public function getUrl($url = null, $full = false, $sessionId = true) {
return parent::url($url, $full, $sessionId);
}
/**
* エレメントテンプレートのレンダリング結果を取得する
*
* @param string $name エレメント名
* @param array $data エレメントで参照するデータ
* @param array $options オプションのパラメータ
* `subDir` (boolean) エレメントのパスについてプレフィックスによるサブディレクトリを追加するかどうか
* ※ その他のパラメータについては、View::element() を参照
* @return string エレメントのレンダリング結果
*/
public function getElement($name, $data = array(), $options = array()) {
$options = array_merge(array(
'subDir' => true
), $options);
if (isset($options['plugin']) && !$options['plugin']) {
unset($options['plugin']);
}
/*** beforeElement ***/
$event = $this->dispatchEvent('beforeElement', array(
'name' => $name,
'data' => $data,
'options' => $options
), array('layer' => 'View', 'class' => '', 'plugin' => ''));
if ($event !== false) {
$options = ($event->result === null || $event->result === true) ? $event->data['options'] : $event->result;
}
/*** Controller.beforeElement ***/
$event = $this->dispatchEvent('beforeElement', array(
'name' => $name,
'data' => $data,
'options' => $options
), array('layer' => 'View', 'class' => $this->_View->name));
if ($event !== false) {
$options = ($event->result === null || $event->result === true) ? $event->data['options'] : $event->result;
}
if ($options['subDir'] === false) {
if (!$this->_subDir && $this->_View->subDir) {
$this->_subDir = $this->_View->subDir;
}
$this->_View->subDir = null;
} else {
if ($this->_subDir) {
$this->_View->subDir = $this->_subDir;
}
}
$out = $this->_View->element($name, $data, $options);
/*** afterElement ***/
$event = $this->dispatchEvent('afterElement', array(
'name' => $name,
'out' => $out
), array('layer' => 'View', 'class' => '', 'plugin' => ''));
if ($event !== false) {
$out = ($event->result === null || $event->result === true) ? $event->data['out'] : $event->result;
}
/*** Controller.afterElement ***/
$event = $this->dispatchEvent('afterElement', array(
'name' => $name,
'out' => $out
), array('layer' => 'View', 'class' => $this->_View->name));
if ($event !== false) {
$out = ($event->result === null || $event->result === true) ? $event->data['out'] : $event->result;
}
return $out;
}
/**
* エレメントテンプレートを出力する
*
* @param string $name エレメント名
* @param array $data エレメントで参照するデータ
* @param array $options オプションのパラメータ
* `subDir` (boolean) エレメントのパスについてプレフィックスによるサブディレクトリを追加するかどうか
* ※ その他のパラメータについては、View::element() を参照
* @return void
*/
public function element($name, $data = array(), $options = array()) {
$options = array_merge(array(
'subDir' => true
), $options);
echo $this->getElement($name, $data, $options);
}
/**
* ヘッダーテンプレートを出力する
*
* @param array $data エレメントで参照するデータ
* @param array $options オプションのパラメータ
* `subDir` (boolean) エレメントのパスについてプレフィックスによるサブディレクトリを追加するかどうか
* ※ その他のパラメータについては、View::element() を参照
* @return void
*/
public function header($data = array(), $options = array()) {
$options = array_merge(array(
'subDir' => true
), $options);
$out = $this->getElement('header', $data, $options);
/*** header ***/
$event = $this->dispatchEvent('header', array(
'out' => $out
), array('layer' => 'View', 'class' => '', 'plugin' => ''));
if ($event !== false) {
$out = ($event->result === null || $event->result === true) ? $event->data['out'] : $event->result;
}
/*** Controller.header ***/
$event = $this->dispatchEvent('header', array(
'out' => $out
), array('layer' => 'View', 'class' => $this->_View->name));
if ($event !== false) {
$out = ($event->result === null || $event->result === true) ? $event->data['out'] : $event->result;
}
echo $out;
}
/**
* フッターテンプレートを出力する
*
* @param array $data エレメントで参照するデータ
* @param array $options オプションのパラメータ
* `subDir` (boolean) エレメントのパスについてプレフィックスによるサブディレクトリを追加するかどうか
* ※ その他のパラメータについては、View::element() を参照
* @return void
*/
public function footer($data = array(), $options = array()) {
$options = array_merge(array(
'subDir' => true
), $options);
$out = $this->getElement('footer', $data, $options);
/*** footer ***/
$event = $this->dispatchEvent('footer', array(
'out' => $out
), array('layer' => 'View', 'class' => '', 'plugin' => ''));
if ($event) {
$out = ($event->result === null || $event->result === true) ? $event->data['out'] : $event->result;
}
/*** Controller.footer ***/
$event = $this->dispatchEvent('footer', array(
'out' => $out
), array('layer' => 'View', 'class' => $this->_View->name));
if ($event) {
$out = ($event->result === null || $event->result === true) ? $event->data['out'] : $event->result;
}
echo $out;
}
/**
* ページネーションを出力する
*
* @param string $name
* @param array $data ページネーションで参照するデータ
* @param array $options オプションのパラメータ
* `subDir` (boolean) エレメントのパスについてプレフィックスによるサブディレクトリを追加するかどうか
* ※ その他のパラメータについては、View::element() を参照
* @return void
*/
public function pagination($name = 'default', $data = array(), $options = array()) {
$options = array_merge(array(
'subDir' => true
), $options);
if (!$name) {
$name = 'default';
}
$file = 'paginations' . DS . $name;
echo $this->getElement($file, $data, $options);
}
/**
* コンテンツ本体を出力する
*
* レイアウトテンプレートで利用する
*
* @return void
*/
public function content() {
/*** beforeContent ***/
$this->dispatchEvent('contentHeader', null, array('layer' => 'View', 'class' => '', 'plugin' => ''));
/*** Controller.beforeContent ***/
$this->dispatchEvent('contentHeader', null, array('layer' => 'View', 'class' => $this->_View->name));
echo $this->_View->fetch('content');
/*** afterContent ***/
$event = $this->dispatchEvent('contentFooter', null, array('layer' => 'View', 'class' => '', 'plugin' => ''));
/*** Controller.afterContent ***/
$event = $this->dispatchEvent('contentFooter', null, array('layer' => 'View', 'class' => $this->_View->name));
}
/**
* セッションに保存したメッセージを出力する
*
* メールフォームのエラーメッセージ等を出力します。
*
* @param string $key 出力するメッセージのキー(初期状態では省略してよいです)
* @return void
*/
public function flash($key = 'flash') {
if ($this->Session->check('Message.' . $key)) {
echo '<div id="MessageBox">';
echo $this->Session->flash($key);
echo '</div>';
}
}
/**
* コンテンツ内で設定した CSS や javascript をレイアウトテンプレートに出力し、ログイン中の場合、ツールバー用のCSSも出力する
* また、テーマ用のCSSが存在する場合には出力する
*
* 利用する際は、</head>タグの直前あたりに記述する。
* コンテンツ内で、レイアウトテンプレートへの出力を設定する場合には、inline オプションを false にする
*
* 《利用例》
* $this->BcBaser->css('admin/layout', array('inline' => false));
* $this->BcBaser->js('admin/startup', false);
*
* @return void
*/
public function scripts() {
$currentPrefix = $this->_View->get('currentPrefix');
$authPrefix = Configure::read('BcAuthPrefix.' . $currentPrefix);
$toolbar = true;
if (isset($authPrefix['toolbar'])) {
$toolbar = $authPrefix['toolbar'];
}
$scripts = $this->_View->fetch('meta') . $this->_View->fetch('css') . $this->_View->fetch('script');
// TODO CakePHP では、 scripts_for_layout は deprecated となっているが後方互換の為残しておく
// baserCMS 4系で除外予定
$scripts .= str_replace($scripts, '', $this->_View->get('scripts_for_layout'));
echo $scripts;
// ### ツールバー用CSS出力
// 《表示条件》
// - プレビューでない
// - auth prefix の設定で、利用するように定義されている
// - モバイルでない
// - Query String で、toolbar=false に定義されていない
// - 管理画面でない
// - ログインしている
if (empty($this->_View->viewVars['preview']) && $toolbar && !Configure::read('BcRequest.agent')) {
if (!isset($this->request->query['toolbar']) || ($this->request->query['toolbar'] !== false && $this->request->query['toolbar'] !== 'false')) {
if (empty($this->request->params['admin']) && !empty($this->_View->viewVars['user'])) {
$this->css('admin/toolbar');
}
}
}
// ### テーマ用CSS出力
// 《表示条件》
// - インストーラーではない
// - /files/theme_configs/config.css が存在する
if (!BcUtil::isAdminSystem() && $this->params['controller'] != 'installations' && file_exists(WWW_ROOT . 'files' . DS . 'theme_configs' . DS . 'config.css')) {
$this->css('/files/theme_configs/config');
}
}
/**
* ツールバーエレメントや CakePHP のデバッグ出力を表示
*
* 利用する際は、</body> タグの直前あたりに記述する。
*
* @return void
*/
public function func() {
$currentPrefix = $this->_View->get('currentPrefix');
$authPrefix = Configure::read('BcAuthPrefix.' . $currentPrefix);
$toolbar = true;
if (isset($authPrefix['toolbar'])) {
$toolbar = $authPrefix['toolbar'];
}
// ### ツールバーエレメント出力
// 《表示条件》
// - プレビューでない
// - auth prefix の設定で、利用するように定義されている
// - モバイルでない
// - Query String で、toolbar=false に定義されていない
// - 管理画面でない
// - ログインしている
if (empty($this->_View->viewVars['preview']) && $toolbar && !Configure::read('BcRequest.agent')) {
if (!isset($this->request->query['toolbar']) || ($this->request->query['toolbar'] !== false && $this->request->query['toolbar'] !== 'false')) {
if (empty($this->request->params['admin']) && !empty($this->_View->viewVars['user'])) {
$this->element('admin/toolbar', array(), array('subDir' => false));
}
}
}
// デバッグ
if (Configure::read('debug') >= 2) {
$this->element('template_dump', array(), array('subDir' => false));
$this->element('sql_dump', array(), array('subDir' => false));
}
}
/**
* サブメニューを設定する(管理画面用)
*
* @param array $submenus サブメニューエレメント名を配列で指定
* @return void
*/
public function setSubMenus($submenus) {
$this->_View->set('subMenuElements', $submenus);
}
/**
* XMLヘッダタグを出力する
*
* @param array $attrib 属性
* @return void
*/
public function xmlHeader($attrib = array()) {
if (empty($attrib['encoding']) && Configure::read('BcRequest.agent') == 'mobile') {
$attrib['encoding'] = 'Shift-JIS';
}
echo $this->BcXml->header($attrib) . "\n";
}
/**
* アイコン(favicon)タグを出力する
*
* @return void
*/
public function icon() {
echo $this->BcHtml->meta('icon') . "\n";
}
/**
* ドキュメントタイプを指定するタグを出力する
*
* @param string $type 出力ドキュメントタイプの文字列(初期値 : 'xhtml-trans')
* @return void
*/
public function docType($type = 'xhtml-trans') {
echo $this->BcHtml->docType($type) . "\n";
}
/**
* CSS タグを出力する
*
* 《利用例》
* $this->BcBaser->css('admin/import')
*
* @param string $path CSSファイルのパス(css フォルダからの相対パス)拡張子は省略可
* @param mixed $options オプション
* (配列の場合)
* - `rel` : rel属性(初期値 : 'stylesheet')
* - `inline` : コンテンツ内にCSSを出力するかどうか(初期値 : true)
* ※ その他のパラメータについては、HtmlHelper::css() を参照。
* ※ false を指定した場合、inline が false となる。
* @return void
*/
public function css($path, $options = array()) {
if ($options === false) {
$options['inline'] = false;
}
$options = array_merge(array(
'rel' => 'stylesheet',
'inline' => true
), $options);
$rel = $options['rel'];
unset($options['rel']);
$result = $this->BcHtml->css($path, $rel, $options);
if ($options['inline']) {
echo $result;
}
}
/**
* Javascript タグを出力する
*
* @param string|array $url Javascriptのパス(js フォルダからの相対パス)拡張子は省略可
* @param bool $inline コンテンツ内に Javascript を出力するかどうか(初期値 : true)
* @return void
*/
public function js($url, $inline = true) {
$result = $this->BcHtml->script($url, array('inline' => $inline));
if ($inline) {
echo $result;
}
}
/**
* 画像タグを出力する
*
* @param array $path 画像のパス(img フォルダからの相対パス)
* @param array $options オプション(主にHTML属性)
* ※ パラメータについては、HtmlHelper::image() を参照。
* @return void
*/
public function img($path, $options = array()) {
echo $this->getImg($path, $options);
}
/**
* 画像タグを取得する
*
* @param string $path 画像のパス(img フォルダからの相対パス)
* @param array $options オプション(主にHTML属性)
* ※ パラメータについては、HtmlHelper::image() を参照。
* @return string 画像タグ
*/
public function getImg($path, $options = array()) {
return $this->BcHtml->image($path, $options);
}
/**
* アンカータグを出力する
*
* @param string $title タイトル
* @param mixed $url オプション(初期値 : null)
* @param array $htmlAttributes オプション(初期値 : array())
* - `escape` : タイトルをエスケープするかどうか(初期値 : false)
* - `prefix` : URLにプレフィックスをつけるかどうか(初期値 : false)
* - `forceTitle` : 許可されていないURLの際にタイトルを強制的に出力するかどうか(初期値 : false)
* - `ssl` : SSL用のURLをして出力するかどうか(初期値 : false)
* ※ その他のパラメータについては、HtmlHelper::link() を参照。
* @param bool $confirmMessage 確認メッセージ(初期値 : false)
* リンクをクリックした際に確認メッセージが表示され、はいをクリックした場合のみ遷移する
* @return void
*/
public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false) {
echo $this->getLink($title, $url, $htmlAttributes, $confirmMessage);
}
/**
* アンカータグを取得する
*
* @param string $title タイトル
* @param mixed $url オプション(初期値 : null)
* @param array $options オプション(初期値 : array())
* - `escape` : タイトルをエスケープするかどうか(初期値 : false)
* - `prefix` : URLにプレフィックスをつけるかどうか(初期値 : false)
* - `forceTitle` : 許可されていないURLの際にタイトルを強制的に出力するかどうか(初期値 : false)
* - `ssl` : SSL用のURLをして出力するかどうか(初期値 : false)
* ※ その他のパラメータについては、HtmlHelper::image() を参照。
* @param bool $confirmMessage 確認メッセージ(初期値 : false)
* リンクをクリックした際に確認メッセージが表示され、はいをクリックした場合のみ遷移する
* @return string
*/
public function getLink($title, $url = null, $options = array(), $confirmMessage = false) {
$adminAlias = Configure::read('BcAuthPrefix.admin.alias');
if (!is_array($options)) {
$options = array($options);
}
$options = array_merge(array(
'escape' => false,
'prefix' => false,
'forceTitle' => false,
'ssl' => false
), $options);
/*** beforeGetLink ***/
$event = $this->dispatchEvent('beforeGetLink', array(
'title' => $title,
'url' => $url,
'options' => $options,
'confirmMessage' => $confirmMessage
), array('class' => 'Html', 'plugin' => ''));
if ($event !== false) {
$options = ($event->result === null || $event->result === true) ? $event->data['options'] : $event->result;
}
if ($options['prefix']) {
if (!empty($this->request->params['prefix']) && is_array($url)) {
$url[$this->request->params['prefix']] = true;
}
}
$forceTitle = $options['forceTitle'];
$ssl = $options['ssl'];
unset($options['prefix']);
unset($options['forceTitle']);
unset($options['ssl']);
// 管理システムメニュー対策
// プレフィックスが変更された場合も正常動作させる為
// TODO メニューが廃止になったら削除
if (!is_array($url)) {
$prefixes = Configure::read('Routing.prefixes');
$url = preg_replace('/^\/' . $adminAlias . '\//', '/' . $prefixes[0] . '/', $url);
}