PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/views/helpers/bc_baser.php

https://github.com/hashing/basercms
PHP | 1597 lines | 758 code | 191 blank | 648 comment | 185 complexity | 3405f8f4c1f20cb38189685a780f9d42 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Baserヘルパー
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package cake
  14. * @subpackage baser.app.view.helpers
  15. * @since baserCMS v 0.1.0
  16. * @version $Revision$
  17. * @modifiedby $LastChangedBy$
  18. * @lastmodified $Date$
  19. * @license http://basercms.net/license/index.html
  20. */
  21. /**
  22. * Include files
  23. */
  24. /**
  25. * Baserヘルパー
  26. *
  27. * @package cake
  28. * @subpackage baser.app.views.helpers
  29. */
  30. class BcBaserHelper extends AppHelper {
  31. /**
  32. * View
  33. *
  34. * @var View
  35. * @access protected
  36. */
  37. var $_view = null;
  38. /**
  39. * サイト基本設定
  40. *
  41. * @var array
  42. * @access public
  43. */
  44. var $siteConfig = array();
  45. /**
  46. * ヘルパー
  47. *
  48. * @var array
  49. * @access public
  50. */
  51. var $helpers = array(BC_HTML_HELPER, 'Javascript', 'Session', BC_XML_HELPER, BC_ARRAY_HELPER);
  52. /**
  53. * コンテンツ
  54. *
  55. * @var string
  56. * @access protected
  57. */
  58. var $_content = null;
  59. /**
  60. * カテゴリタイトル設定
  61. *
  62. * @var mixed
  63. * @access protected
  64. */
  65. var $_categoryTitleOn = true;
  66. /**
  67. * カテゴリタイトル
  68. *
  69. * @var mixed boolean Or string
  70. * @access protected
  71. */
  72. var $_categoryTitle = true;
  73. /**
  74. * ページモデル
  75. *
  76. * @var Page
  77. * @access public
  78. */
  79. var $Page = null;
  80. /**
  81. * アクセス制限設定モデル
  82. *
  83. * @var Permission
  84. * @access public
  85. */
  86. var $Permission = null;
  87. /**
  88. * Plugin Basers
  89. *
  90. * @var array
  91. * @access public
  92. */
  93. var $pluginBasers = array();
  94. /**
  95. * コンストラクタ
  96. *
  97. * @return void
  98. * @access public
  99. */
  100. function __construct() {
  101. $this->_view =& ClassRegistry::getObject('view');
  102. if(BC_INSTALLED && !Configure::read('BcRequest.isUpdater') && !Configure::read('BcRequest.isMaintenance')){
  103. if (ClassRegistry::isKeySet('Permission')) {
  104. $this->Permission = ClassRegistry::getObject('Permission');
  105. }else {
  106. $this->Permission = ClassRegistry::init('Permission');
  107. }
  108. if (ClassRegistry::isKeySet('Page')) {
  109. $this->Page = ClassRegistry::getObject('Page');
  110. }else {
  111. $this->Page = ClassRegistry::init('Page');
  112. }
  113. if (ClassRegistry::isKeySet('PageCategory')) {
  114. $this->PageCategory = ClassRegistry::getObject('PageCategory');
  115. }else {
  116. $this->PageCategory = ClassRegistry::init('PageCategory');
  117. }
  118. }
  119. if(BC_INSTALLED) {
  120. if(isset($this->_view->viewVars['siteConfig'])) {
  121. $this->siteConfig = $this->_view->viewVars['siteConfig'];
  122. }
  123. }
  124. if(BC_INSTALLED && !Configure::read('BcRequest.isUpdater') && !Configure::read('BcRequest.isMaintenance')){
  125. // プラグインのBaserヘルパを初期化
  126. $this->_initPluginBasers();
  127. }
  128. }
  129. /**
  130. * afterRender
  131. *
  132. * @return void
  133. * @access public
  134. */
  135. function afterRender() {
  136. parent::afterRender();
  137. // コンテンツをフックする
  138. $this->_content = ob_get_contents();
  139. }
  140. /**
  141. * メニューを取得する
  142. *
  143. * @param string $menuType
  144. * @return array $globalMenus
  145. * @access public
  146. */
  147. function getMenus () {
  148. if (ClassRegistry::init('GlobalMenu')) {
  149. if(!file_exists(CONFIGS.'database.php')) {
  150. return '';
  151. }
  152. $dbConfig = new DATABASE_CONFIG();
  153. if(!$dbConfig->baser) {
  154. return '';
  155. }
  156. $GlobalMenu = ClassRegistry::getObject('GlobalMenu');
  157. // エラーの際も呼び出される事があるので、テーブルが実際に存在するかチェックする
  158. $db =& ConnectionManager::getDataSource('baser');
  159. if ($db->isInterfaceSupported('listSources')) {
  160. $sources = $db->listSources();
  161. if (!is_array($sources) || in_array(strtolower($db->config['prefix'] . 'global_menus'), array_map('strtolower', $sources))) {
  162. if (empty($this->params['prefix'])) {
  163. $prefix = 'publish';
  164. } else {
  165. $prefix = $this->params['prefix'];
  166. }
  167. return $GlobalMenu->find('all', array('order' => 'sort'));
  168. }
  169. }
  170. }
  171. return '';
  172. }
  173. /**
  174. * タイトルをセットする
  175. *
  176. * @param string $title
  177. * @access public
  178. */
  179. function setTitle($title,$categoryTitleOn = null) {
  180. if(!is_null($categoryTitleOn)) {
  181. $this->_categoryTitleOn = $categoryTitleOn;
  182. }
  183. $this->_view->set('title',$title);
  184. }
  185. /**
  186. * キーワードをセットする
  187. *
  188. * @param string $title
  189. * @access public
  190. */
  191. function setKeywords($keywords) {
  192. $this->_view->set('keywords',$keywords);
  193. }
  194. /**
  195. * 説明文をセットする
  196. *
  197. * @param string $title
  198. * @access public
  199. */
  200. function setDescription($description) {
  201. $this->_view->set('description',$description);
  202. }
  203. /**
  204. * レイアウト用の変数をセットする
  205. * $view->set のラッパー
  206. *
  207. * @param string $title
  208. * @param mixed $value
  209. * @return void
  210. * @access public
  211. */
  212. function set($key,$value) {
  213. $this->_view->set($key,$value);
  214. }
  215. /**
  216. * タイトルへのカテゴリタイトル表示を設定
  217. * コンテンツごとの個別設定
  218. *
  219. * @param mixed $on boolean / 文字列(カテゴリ名として出力した文字を指定する)
  220. * @return void
  221. * @access public
  222. */
  223. function setCategoryTitle($on = true) {
  224. $this->_categoryTitle = $on;
  225. }
  226. /**
  227. * キーワードを取得する
  228. *
  229. * @return string $keyword
  230. * @return string
  231. * @access public
  232. */
  233. function getKeywords() {
  234. $keywords = '';
  235. if(!empty($this->_view->viewVars['keywords'])) {
  236. $keywords = $this->_view->viewVars['keywords'];
  237. }elseif(!empty($this->siteConfig['keyword'])) {
  238. $keywords = $this->siteConfig['keyword'];
  239. }
  240. return $keywords;
  241. }
  242. /**
  243. * 説明文を取得する
  244. *
  245. * @return string $description
  246. * @return string
  247. * @access public
  248. */
  249. function getDescription() {
  250. $description = '';
  251. if(!empty($this->_view->viewVars['description'])) {
  252. $description = $this->_view->viewVars['description'];
  253. }elseif(!empty($this->siteConfig['description'])) {
  254. $description = $this->siteConfig['description'];
  255. }
  256. return $description;
  257. }
  258. /**
  259. * タイトルを取得する
  260. * ページタイトルと直属のカテゴリ名が同じ場合は、ページ名を省略する
  261. *
  262. * @param string $separator
  263. * @param string $categoryTitleOn
  264. * @return string $description
  265. * @access public
  266. */
  267. function getTitle($separator='|',$categoryTitleOn = null) {
  268. $title = '';
  269. $crumbs = $this->getCrumbs($categoryTitleOn);
  270. if($crumbs){
  271. $crumbs = array_reverse($crumbs);
  272. foreach ($crumbs as $key => $crumb) {
  273. if($this->BcArray->first($crumbs, $key) && isset($crumbs[$key+1])) {
  274. if($crumbs[$key+1]['name'] == $crumb['name']) {
  275. continue;
  276. }
  277. }
  278. if($title){
  279. $title .= $separator;
  280. }
  281. $title .= $crumb['name'];
  282. }
  283. }
  284. // サイトタイトルを追加
  285. if ($title && !empty($this->siteConfig['name'])) {
  286. $title .= $separator;
  287. }
  288. if (!empty($this->siteConfig['name'])) {
  289. $title .= $this->siteConfig['name'];
  290. }
  291. return $title;
  292. }
  293. /**
  294. * パンくず用の配列を取得する
  295. * 基本的には、コントローラーの crumbs プロパティで設定した値を取得する仕様だが
  296. * 事前に setCategoryTitle メソッドで出力内容をカスタマイズする事ができる
  297. *
  298. * @param mixid $categoryTitleOn
  299. * @return array
  300. * @access public
  301. * @todo 処理内容がわかりにくいので変数名のリファクタリング要
  302. */
  303. function getCrumbs($categoryTitleOn = null){
  304. // ページカテゴリを追加
  305. if(!is_null($categoryTitleOn)) {
  306. $this->_categoryTitleOn = $categoryTitleOn;
  307. }
  308. $crumbs = array();
  309. if($this->_categoryTitleOn && $this->_categoryTitle) {
  310. if($this->_categoryTitle === true) {
  311. if($this->_view->viewVars['crumbs']){
  312. $crumbs = $this->_view->viewVars['crumbs'];
  313. }
  314. }else {
  315. if(is_array($this->_categoryTitle)){
  316. $crumbs = $this->_categoryTitle;
  317. }else{
  318. $crumbs = array($this->_categoryTitle=>'');
  319. }
  320. }
  321. }
  322. $contentsTitle = $this->getContentsTitle();
  323. if($contentsTitle) {
  324. $crumbs[] = array('name' => $contentsTitle, 'url' => '');
  325. }
  326. return $crumbs;
  327. }
  328. /**
  329. * コンテンツタイトルを取得する
  330. * @return string $description
  331. * @access public
  332. */
  333. function getContentsTitle() {
  334. $contentsTitle = '';
  335. // トップページの場合は、タイトルをサイト名だけにする
  336. if (!empty($this->_view->viewVars['contentsTitle'])) {
  337. $contentsTitle = $this->_view->viewVars['contentsTitle'];
  338. }elseif($this->_view->pageTitle) {
  339. $contentsTitle = $this->_view->pageTitle;
  340. }
  341. if ($this->_view->name != 'CakeError' && !empty($contentsTitle)) {
  342. return $contentsTitle;
  343. }
  344. }
  345. /**
  346. * コンテンツタイトルを出力する
  347. *
  348. * @return void
  349. * @access public
  350. */
  351. function contentsTitle() {
  352. echo $this->getContentsTitle();
  353. }
  354. /**
  355. * タイトルを出力する
  356. *
  357. * @param string $separator
  358. * @param string $categoryTitleOn
  359. * @return void
  360. * @access public
  361. */
  362. function title($separator='|',$categoryTitleOn = null) {
  363. echo '<title>'.strip_tags($this->getTitle($separator,$categoryTitleOn)).'</title>';
  364. }
  365. /**
  366. * メタキーワードタグを出力する
  367. *
  368. * @return void
  369. * @access public
  370. */
  371. function metaKeywords() {
  372. echo $this->BcHtml->meta('keywords',$this->getkeywords());
  373. }
  374. /**
  375. * メタディスクリプションを出力する
  376. *
  377. * @return void
  378. * @access public
  379. */
  380. function metaDescription() {
  381. echo $this->BcHtml->meta('description', strip_tags($this->getDescription()));
  382. }
  383. /**
  384. * RSSリンクタグを出力する
  385. *
  386. * @param string $title
  387. * @param string $link
  388. * @return void
  389. * @access public
  390. */
  391. function rss($title, $link) {
  392. echo $this->BcHtml->meta($title, $link, array('type' => 'rss'));
  393. }
  394. /**
  395. * トップページかどうか判断する
  396. *
  397. * @return boolean
  398. * @access public
  399. * @deprecated isHomeに統合する
  400. */
  401. function isTop() {
  402. return $this->isHome();
  403. }
  404. /**
  405. * トップページかどうか判断する
  406. *
  407. * @return boolean
  408. * @access public
  409. */
  410. function isHome() {
  411. return ($this->params['url']['url'] == '/' ||
  412. $this->params['url']['url'] == 'index' ||
  413. $this->params['url']['url'] == Configure::read('BcRequest.agentAlias').'/' ||
  414. $this->params['url']['url'] == Configure::read('BcRequest.agentAlias').'/index');
  415. }
  416. /**
  417. * webrootを出力する為だけのラッパー
  418. *
  419. * @return void
  420. * @access public
  421. */
  422. function root() {
  423. echo $this->getRoot();
  424. }
  425. /**
  426. * webrootを取得する為だけのラッパー
  427. *
  428. * @return string
  429. * @access public
  430. */
  431. function getRoot() {
  432. return $this->base.'/';
  433. }
  434. /**
  435. * ベースを考慮したURLを出力
  436. *
  437. * @param string $url
  438. * @param boolean $full
  439. * @return void
  440. * @access public
  441. */
  442. function url($url,$full = false) {
  443. echo $this->getUrl($url,$full);
  444. }
  445. /**
  446. * ベースを考慮したURLを取得
  447. *
  448. * @param string $url
  449. * @param boolean $full
  450. */
  451. function getUrl($url,$full = false) {
  452. return parent::url($url,$full);
  453. }
  454. /**
  455. * エレメントを取得する
  456. * View::elementを取得するだけのラッパー
  457. *
  458. * @param string $name
  459. * @param array $params
  460. * @param boolean $loadHelpers
  461. * @param boolean $subDir
  462. * @return string
  463. * @access public
  464. */
  465. function getElement($name, $params = array(), $loadHelpers = false, $subDir = true) {
  466. if(!empty($this->_view->subDir) && $subDir) {
  467. $name = $this->_view->subDir.DS.$name;
  468. $params['subDir'] = true;
  469. } else {
  470. $params['subDir'] = false;
  471. }
  472. return $this->_view->element($name, $params, $loadHelpers);
  473. }
  474. /**
  475. * エレメントを出力する
  476. * View::elementを出力するだけのラッパー
  477. *
  478. * @param string $name
  479. * @param array $params
  480. * @param boolean $loadHelpers
  481. * @return void
  482. * @access public
  483. */
  484. function element($name, $params = array(), $loadHelpers = false, $subDir = true) {
  485. echo $this->getElement($name, $params, $loadHelpers, $subDir);
  486. }
  487. /**
  488. * ヘッダーを出力する
  489. *
  490. * @param array $params
  491. * @param mixed $loadHelpers
  492. * @param boolean $subDir
  493. */
  494. function header($params = array(), $loadHelpers = false, $subDir = true) {
  495. $out = $this->getElement('header', $params, $loadHelpers, $subDir);
  496. echo $this->executeHook('baserHeader', $out);
  497. }
  498. /**
  499. * フッターを出力する
  500. *
  501. * @param array $params
  502. * @param mixed $loadHelpers
  503. * @param boolean $subDir
  504. * @return void
  505. * @access public
  506. */
  507. function footer($params = array(), $loadHelpers = false, $subDir = true) {
  508. $out = $this->getElement('footer', $params, $loadHelpers, $subDir);
  509. echo $this->executeHook('baserFooter', $out);
  510. }
  511. /**
  512. * ページネーションを出力する
  513. * [非推奨]
  514. * @param string $name
  515. * @param array $params
  516. * @param boolean $loadHelpers
  517. * @return void
  518. * @access public
  519. * @deprecated
  520. */
  521. function pagination($name = 'default', $params = array(), $loadHelpers = false, $subDir = true) {
  522. if(!$name) {
  523. $name = 'default';
  524. }
  525. $file = 'paginations'.DS.$name;
  526. echo $this->getElement($file,$params,$loadHelpers, $subDir);
  527. }
  528. /**
  529. * コンテンツを出力する
  530. * $content_for_layout を出力するだけのラッパー
  531. *
  532. * @return void
  533. * @access public
  534. */
  535. function content() {
  536. echo $this->_content;
  537. }
  538. /**
  539. * セッションメッセージをフラッシュするだけのラッパー
  540. *
  541. * @param array $key
  542. * @return void
  543. * @access public
  544. */
  545. function flash($key='flash') {
  546. if ($this->Session->check('Message.'.$key)) {
  547. $this->Session->flash($key);
  548. }
  549. }
  550. /**
  551. * スクリプトを出力する
  552. * $scripts_for_layout を出力する
  553. *
  554. * @return void
  555. * @access public
  556. */
  557. function scripts() {
  558. if(empty($this->params['admin']) && !empty($this->_view->viewVars['user']) && !Configure::read('BcRequest.agent')) {
  559. $publishTheme = $this->BcHtml->themeWeb;
  560. $this->BcHtml->themeWeb = 'themed/'.$this->siteConfig['admin_theme'].'/';
  561. $this->css('admin/toolbar', array('inline' => false));
  562. $this->BcHtml->themeWeb = $publishTheme;
  563. }
  564. echo join("\n\t", $this->_view->__scripts);
  565. }
  566. /**
  567. * ツールバーやCakeのデバッグ出力を表示
  568. *
  569. * @return void
  570. * @access public
  571. */
  572. function func() {
  573. if(empty($this->params['admin']) && !empty($this->_view->viewVars['user']) && !Configure::read('BcRequest.agent')) {
  574. $publishTheme = $this->_view->theme;
  575. $this->_view->theme = $this->siteConfig['admin_theme'];
  576. $this->element('admin/toolbar');
  577. $this->_view->theme = $publishTheme;
  578. }
  579. if (isset($this->_view->viewVars['cakeDebug']) && Configure::read() > 2) {
  580. $params = array('controller' => $this->_view->viewVars['cakeDebug']);
  581. echo View::element('dump', $params, false);
  582. }
  583. }
  584. /**
  585. * サブメニューをセットする
  586. *
  587. * @param array $submenus
  588. * @return void
  589. * @access public
  590. */
  591. function setSubMenus($submenus) {
  592. $this->_view->set('subMenuElements',$submenus);
  593. }
  594. /**
  595. * XMLヘッダを出力する
  596. *
  597. * @param array $attrib
  598. * @return void
  599. * @access public
  600. */
  601. function xmlHeader($attrib = array()) {
  602. if(empty($attrib['encoding']) && Configure::read('BcRequest.agent') == 'mobile'){
  603. $attrib['encoding'] = 'Shift-JIS';
  604. }
  605. echo $this->BcXml->header($attrib)."\n";
  606. }
  607. /**
  608. * アイコンタグを出力するだけのラッパー
  609. *
  610. * @return void
  611. * @access public
  612. */
  613. function icon() {
  614. echo $this->BcHtml->meta('icon');
  615. }
  616. /**
  617. * DOC TYPE を出力するだけのラッパー
  618. *
  619. * @param type $type
  620. * @return void
  621. * @access public
  622. */
  623. function docType($type = 'xhtml-trans') {
  624. echo $this->BcHtml->docType($type)."\n";
  625. }
  626. /**
  627. *
  628. * CSSタグを出力するだけのラッパー
  629. *
  630. * @param string $path
  631. * @param string $rel
  632. * @param array $htmlAttributes
  633. * @param boolean $inline
  634. * @return void
  635. * @access public
  636. */
  637. function css($path, $htmlAttributes = array(), $inline = true) {
  638. // Cake1.2系との互換対応
  639. if (isset($htmlAttributes['inline']) && $inline == true) {
  640. $inline = $htmlAttributes['inline'];
  641. }
  642. $rel = null;
  643. if(!empty($htmlAttributes['rel'])) {
  644. $rel = $htmlAttributes['rel'];
  645. }
  646. $ret = $this->BcHtml->css($path, $rel, $htmlAttributes, $inline);
  647. if($inline) {
  648. echo $ret;
  649. }
  650. }
  651. /**
  652. * Javascriptのlinkタグを出力するだけのラッパー
  653. *
  654. * @param boolean $url
  655. * @param boolean $inline
  656. * @return void
  657. * @access public
  658. */
  659. function js($url, $inline = true) {
  660. $ret = $this->Javascript->link($url, $inline);
  661. if($inline) {
  662. echo $ret;
  663. }
  664. }
  665. /**
  666. * imageタグを出力するだけのラッパー
  667. *
  668. * @param array $path
  669. * @param array $options
  670. * @return void
  671. * @access pub
  672. */
  673. function img($path, $options = array()) {
  674. echo $this->getImg($path, $options);
  675. }
  676. /**
  677. * imageタグを取得するだけのラッパー
  678. *
  679. * @param array $path
  680. * @param array $options
  681. * @return array
  682. * @access public
  683. */
  684. function getImg($path, $options = array()) {
  685. return $this->BcHtml->image($path, $options);
  686. }
  687. /**
  688. * aタグを表示するだけのラッパー関数
  689. *
  690. * @param string $title
  691. * @param string $url
  692. * @param array $htmlAttributes
  693. * @param boolean $confirmMessage
  694. * @param boolean $escapeTitle
  695. * @return void
  696. * @access public
  697. */
  698. function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = false) {
  699. echo $this->getLink($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
  700. }
  701. /**
  702. *
  703. */
  704. /**
  705. * aタグを取得するだけのラッパー
  706. *
  707. * @param string $title
  708. * @param string $url
  709. * @param array $htmlAttributes
  710. * @param boolean $confirmMessage
  711. * @param boolean $escapeTitle
  712. * @return string
  713. * @access public
  714. */
  715. function getLink($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = false) {
  716. $htmlAttributes = $this->executeHook('beforeBaserGetLink', $title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
  717. if(!empty($htmlAttributes['prefix'])) {
  718. if(!empty($this->params['prefix'])) {
  719. $url[$this->params['prefix']] = true;
  720. }
  721. unset($htmlAttributes['prefix']);
  722. }
  723. if(isset($htmlAttributes['forceTitle'])) {
  724. $forceTitle = $htmlAttributes['forceTitle'];
  725. unset($htmlAttributes['forceTitle']);
  726. }else {
  727. $forceTitle = false;
  728. }
  729. if(isset($htmlAttributes['ssl'])) {
  730. $ssl = true;
  731. unset($htmlAttributes['ssl']);
  732. }else {
  733. $ssl = false;
  734. }
  735. // 管理システムメニュー対策
  736. // プレフィックスが変更された場合も正常動作させる為
  737. // TODO メニューが廃止になったら削除
  738. if(!is_array($url)) {
  739. $url = preg_replace('/^\/admin\//', '/'.Configure::read('Routing.admin').'/', $url);
  740. }
  741. $url = $this->getUrl($url);
  742. $_url = preg_replace('/^'.preg_quote($this->base, '/').'\//', '/', $url);
  743. $enabled = true;
  744. // 認証チェック
  745. if(isset($this->Permission) && !empty($this->_view->viewVars['user']['user_group_id'])) {
  746. $userGroupId = $this->_view->viewVars['user']['user_group_id'];
  747. if(!$this->Permission->check($_url,$userGroupId)) {
  748. $enabled = false;
  749. }
  750. }
  751. // ページ公開チェック
  752. if(isset($this->Page) && empty($this->params['admin'])) {
  753. $adminPrefix = Configure::read('Routing.admin');
  754. if(isset($this->Page) && !preg_match('/^\/'.$adminPrefix.'/', $_url)) {
  755. if($this->Page->isPageUrl($_url) && !$this->Page->checkPublish($_url)) {
  756. $enabled = false;
  757. }
  758. }
  759. }
  760. if(!$enabled) {
  761. if($forceTitle) {
  762. return "<span>$title</span>";
  763. }else {
  764. return '';
  765. }
  766. }
  767. // 現在SSLのURLの場合、フルパスで取得
  768. if($this->isSSL() || $ssl) {
  769. $_url = preg_replace("/^\//", "", $_url);
  770. if(preg_match('/^admin\//', $_url)) {
  771. $admin = true;
  772. } else {
  773. $admin = false;
  774. }
  775. if(Configure::read('App.baseUrl')) {
  776. $_url = 'index.php/'.$_url;
  777. }
  778. if(!$ssl && !$admin) {
  779. $url = Configure::read('BcEnv.siteUrl').$_url;
  780. } else {
  781. $url = Configure::read('BcEnv.sslUrl').$_url;
  782. }
  783. } else {
  784. $url = $_url;
  785. }
  786. // Cake1.2系との互換対応
  787. if (isset($htmlAttributes['escape']) && $escapeTitle == true) {
  788. $escapeTitle = $htmlAttributes['escape'];
  789. }
  790. if(!$htmlAttributes) {
  791. $htmlAttributes = array();
  792. }
  793. $htmlAttributes = array_merge($htmlAttributes, array('escape' => $escapeTitle));
  794. $out = $this->BcHtml->link($title, $url, $htmlAttributes, $confirmMessage);
  795. return $this->executeHook('afterBaserGetLink', $url, $out);
  796. }
  797. /**
  798. * 現在がSSL通信か確認する
  799. *
  800. * @return boolean
  801. * @access public
  802. */
  803. function isSSL() {
  804. if(!empty($this->_view->viewVars['isSSL'])){
  805. return true;
  806. } else {
  807. return false;
  808. }
  809. }
  810. /**
  811. * charsetを出力するだけのラッパー
  812. *
  813. * @param string $charset
  814. * @return void
  815. * @access public
  816. */
  817. function charset($charset = null) {
  818. if(!$charset && Configure::read('BcRequest.agent') == 'mobile'){
  819. $charset = 'Shift-JIS';
  820. }
  821. echo $this->BcHtml->charset($charset);
  822. }
  823. /**
  824. * コピーライト用の年を出力する
  825. *
  826. * @param string 開始年
  827. * @return void
  828. * @access public
  829. */
  830. function copyYear($begin) {
  831. $year = date('Y');
  832. if($begin == $year) {
  833. echo $year;
  834. }else {
  835. echo $begin.' - '.$year;
  836. }
  837. }
  838. /**
  839. * ページ編集へのリンクを出力する
  840. *
  841. * @param string $id
  842. * @return void
  843. * @access public
  844. */
  845. function setPageEditLink($id) {
  846. if(empty($this->params['admin']) && !empty($this->_view->viewVars['user']) && !Configure::read('BcRequest.agent')) {
  847. $this->_view->viewVars['editLink'] = array('admin' => true, 'controller' => 'pages', 'action' => 'edit', $id);
  848. }
  849. }
  850. /**
  851. * 編集リンクを出力する
  852. *
  853. * @return void
  854. * @access public
  855. */
  856. function editLink() {
  857. if(!empty($this->_view->viewVars['editLink'])) {
  858. $this->link('編集する', $this->_view->viewVars['editLink'], array('class' => 'tool-menu'));
  859. }
  860. }
  861. /**
  862. * 編集リンクが存在するかチェックする
  863. *
  864. * @return boolean
  865. * @access public
  866. */
  867. function existsEditLink() {
  868. return (!empty($this->_view->viewVars['editLink']));
  869. }
  870. /**
  871. * 公開ページへのリンクを出力する
  872. *
  873. * @return void
  874. * @access public
  875. */
  876. function publishLink() {
  877. if(!empty($this->_view->viewVars['publishLink'])) {
  878. $this->link('公開ページ', $this->_view->viewVars['publishLink'], array('class' => 'tool-menu'));
  879. }
  880. }
  881. /**
  882. * 公開ページへのリンクが存在するかチェックする
  883. *
  884. * @return boolean
  885. * @access public
  886. */
  887. function existsPublishLink() {
  888. return (!empty($this->_view->viewVars['publishLink']));
  889. }
  890. /**
  891. * アップデート処理が必要かチェックする
  892. * TODO 別のヘルパに移動する
  893. * @return boolean
  894. * @access public
  895. */
  896. function checkUpdate() {
  897. $baserVerpoint = verpoint($this->_view->viewVars['baserVersion']);
  898. if(isset($this->siteConfig['version'])) {
  899. $siteVerpoint = verpoint($this->siteConfig['version']);
  900. }else {
  901. $siteVerpoint = 0;
  902. }
  903. if(!$baserVerpoint === false || $siteVerpoint === false) {
  904. return false;
  905. } else {
  906. return ($baserVerpoint > $siteVerpoint);
  907. }
  908. }
  909. /**
  910. * アップデート用のメッセージを出力する
  911. * TODO 別のヘルパーに移動する
  912. * @return void
  913. * @access public
  914. */
  915. function updateMessage() {
  916. $adminPrefix = Configure::read('Routing.admin');
  917. if($this->checkUpdate() && $this->params['controller'] != 'updaters') {
  918. $updateLink = $this->BcHtml->link('ここ',"/{$adminPrefix}/updaters");
  919. echo '<div id="UpdateMessage">WEBサイトのアップデートが完了していません。'.$updateLink.' からアップデートを完了させてください。</div>';
  920. }
  921. }
  922. /**
  923. * コンテンツ名を出力する
  924. *
  925. * @param boolean $detail
  926. * @return void
  927. * @access public
  928. */
  929. function contentsName($detail = false, $options = array()) {
  930. echo $this->getContentsName($detail);
  931. }
  932. /**
  933. * コンテンツ名を取得する
  934. * ・キャメルケースで取得
  935. * ・URLのコントローラー名までを取得
  936. * ・ページの場合は、カテゴリ名(カテゴリがない場合は Default)
  937. * ・トップページは、Home
  938. *
  939. * @param boolean $detail
  940. * @return string
  941. * @access public
  942. */
  943. function getContentsName($detail = false, $options = array()) {
  944. $options = array_merge(array(
  945. 'home' => 'Home',
  946. 'default' => 'Default',
  947. 'error' => 'Error',
  948. 'underscore'=> false),
  949. $options);
  950. extract($options);
  951. $prefix = '';
  952. $plugin = '';
  953. $controller = '';
  954. $action = '';
  955. $pass = '';
  956. $url0 = '';
  957. $url1 = '';
  958. $url2 = '';
  959. $aryUrl = array();
  960. if(!empty($this->params['prefix']) && Configure::read('BcRequest.agentPrefix') != $this->params['prefix']) {
  961. $prefix = h($this->params['prefix']);
  962. }
  963. if(!empty($this->params['plugin'])) {
  964. $plugin = h($this->params['plugin']);
  965. }
  966. $controller = h($this->params['controller']);
  967. if($prefix) {
  968. $action = str_replace($prefix.'_', '', h($this->params['action']));
  969. }else {
  970. $action = h($this->params['action']);
  971. }
  972. if(!empty($this->params['pass'])) {
  973. foreach($this->params['pass'] as $key => $value) {
  974. $pass[$key] = h($value);
  975. }
  976. }
  977. $url = split('/', h($this->params['url']['url']));
  978. if(Configure::read('BcRequest.agent')) {
  979. array_shift($url);
  980. }
  981. if(isset($url[0])) {
  982. $url0 = $url[0];
  983. }
  984. if(isset($url[1])) {
  985. $url1 = $url[1];
  986. }
  987. if(isset($url[2])) {
  988. $url2 = $url[2];
  989. }
  990. // 固定ページの場合
  991. if($controller=='pages' && $action=='display') {
  992. if(strpos($pass[0], 'pages/') !== false) {
  993. $pageUrl = str_replace('pages/','', $pass[0]);
  994. } else {
  995. $pageUrl = h($this->params['url']['url']);
  996. }
  997. if(preg_match('/\/$/', $pageUrl)) {
  998. $pageUrl .= 'index';
  999. }
  1000. $pageUrl = preg_replace('/\.html$/', '', $pageUrl);
  1001. $pageUrl = preg_replace('/^\//', '', $pageUrl);
  1002. $aryUrl = split('/',$pageUrl);
  1003. } else {
  1004. // プラグインルーティングの場合
  1005. if((($url1==''&&$action=='index')||($url1==$action)) && $url2!=$action && $plugin) {
  1006. $plugin = '';
  1007. $controller = $url0;
  1008. }
  1009. if($plugin) {
  1010. $controller = $plugin.'_'.$controller;
  1011. }
  1012. if($prefix) {
  1013. $controller = $prefix.'_'.$controller;
  1014. }
  1015. if($controller) {
  1016. $aryUrl[] = $controller;
  1017. }
  1018. if($action) {
  1019. $aryUrl[] = $action;
  1020. }
  1021. if($pass) {
  1022. $aryUrl = $aryUrl + $pass;
  1023. }
  1024. }
  1025. if ($this->_view->name == 'CakeError') {
  1026. $contentsName = $error;
  1027. } elseif(count($aryUrl) >= 2) {
  1028. if(!$detail) {
  1029. $contentsName = $aryUrl[0];
  1030. } else {
  1031. $contentsName = implode('_', $aryUrl);
  1032. }
  1033. } elseif(count($aryUrl) == 1 && $aryUrl[0] == 'index') {
  1034. $contentsName = $home;
  1035. } else {
  1036. if(!$detail) {
  1037. $contentsName = $default;
  1038. } else {
  1039. $contentsName = $aryUrl[0];
  1040. }
  1041. }
  1042. if($underscore) {
  1043. $contentsName = Inflector::underscore($contentsName);
  1044. } else {
  1045. $contentsName = Inflector::camelize($contentsName);
  1046. }
  1047. return $contentsName;
  1048. }
  1049. /**
  1050. * パンくずリストを出力する
  1051. * アクセス制限がかかっているリンクはテキストのみ表示する
  1052. *
  1053. * @param string $separator Text to separate crumbs.
  1054. * @param string $startText This will be the first crumb, if false it defaults to first crumb in array
  1055. * @return string
  1056. * @access public
  1057. */
  1058. function crumbs($separator = '&raquo;', $startText = false) {
  1059. if (!empty($this->BcHtml->_crumbs)) {
  1060. $out = array();
  1061. if ($startText) {
  1062. $out[] = $this->getLink($startText, '/');
  1063. }
  1064. foreach ($this->BcHtml->_crumbs as $crumb) {
  1065. if (!empty($crumb[1])) {
  1066. $out[] = $this->getLink($crumb[0], $crumb[1], $crumb[2]);
  1067. } else {
  1068. $out[] = $crumb[0];
  1069. }
  1070. }
  1071. echo $this->output(implode($separator, $out));
  1072. }
  1073. }
  1074. /**
  1075. * パンくずリストに要素を追加する
  1076. * アクセス制限がかかっているリンクの場合でもタイトルを表示できるオプションを付加
  1077. * $options に forceTitle を指定する事で表示しない設定も可能
  1078. *
  1079. * @param string $name Text for link
  1080. * @param string $link URL for link (if empty it won't be a link)
  1081. * @param mixed $options Link attributes e.g. array('id'=>'selected')
  1082. * @return void
  1083. * @access public
  1084. */
  1085. function addCrumb($name, $link = null, $options = null) {
  1086. $_options = array('forceTitle'=>true);
  1087. if($options) {
  1088. $options = am($_options,$options);
  1089. } else {
  1090. $options = $_options;
  1091. }
  1092. $this->BcHtml->_crumbs[] = array($name, $link, $options);
  1093. }
  1094. /**
  1095. * ページリストを取得する
  1096. *
  1097. * @param string $categoryId
  1098. * @return mixed boolean / array
  1099. * @access public
  1100. */
  1101. function getPageList($categoryId=null) {
  1102. if ($this->Page) {
  1103. $conditions = array('Page.status'=>1);
  1104. if($categoryId) {
  1105. $conditions['Page.page_category_id'] = $categoryId;
  1106. }
  1107. $this->Page->unbindModel(array('belongsTo'=>array('PageCategory')));
  1108. $pages = $this->Page->find('all',array('conditions'=>$conditions,
  1109. 'fields'=>array('title','url'),
  1110. 'order'=>'Page.sort'));
  1111. return Set::extract('/Page/.',$pages);
  1112. }else {
  1113. return false;
  1114. }
  1115. }
  1116. /**
  1117. *
  1118. */
  1119. /**
  1120. * ブラウザにキャッシュさせる為のヘッダーを出力する
  1121. *
  1122. * @param type $expire
  1123. * @param array $type
  1124. * @return void
  1125. * @access public
  1126. */
  1127. function cacheHeader($expire = DAY, $type='html') {
  1128. $contentType = array(
  1129. 'html' => 'text/html',
  1130. 'js' => 'text/javascript', 'css' => 'text/css',
  1131. 'gif' => 'image/gif', 'jpg' => 'image/jpeg', 'png' => 'image/png'
  1132. );
  1133. $fileModified = filemtime(WWW_ROOT.'index.php');
  1134. if(!$expire) {
  1135. $expire = strtotime(DAY);
  1136. } elseif(!is_numeric($expire)){
  1137. $expire = strtotime($expire);
  1138. }
  1139. header("Date: " . date("D, j M Y G:i:s ", $fileModified) . 'GMT');
  1140. header("Last-Modified: " . gmdate( "D, d M Y H:i:s", $fileModified) . " GMT");
  1141. header('Content-type: ' . $contentType[$type]);
  1142. header("Expires: " . gmdate("D, j M Y H:i:s", time() + $expire) . " GMT");
  1143. header('Cache-Control: max-age=' . $expire);
  1144. // Firefoxの場合は不要↓
  1145. //header("Cache-Control: cache");
  1146. header("Pragma: cache");
  1147. }
  1148. /**
  1149. * httpから始まるURLを取得する
  1150. *
  1151. * @param string $url
  1152. * @return string
  1153. * @access public
  1154. */
  1155. function getUri($url){
  1156. if(preg_match('/^http/is', $url)) {
  1157. return $url;
  1158. }else {
  1159. if(empty($_SERVER['HTTPS'])) {
  1160. $protocol = 'http';
  1161. }else {
  1162. $protocol = 'https';
  1163. }
  1164. return $protocol . '://'.$_SERVER['HTTP_HOST'].$this->getUrl($url);
  1165. }
  1166. }
  1167. /**
  1168. * プラグインのBaserヘルパを初期化する
  1169. * BaserHelperに定義されていないメソッドをプラグイン内のヘルパに定義する事で
  1170. * BaserHelperから呼び出せるようになる仕組みを提供する。
  1171. * コアからプラグインのヘルパメソッドをBaserHelper経由で直接呼び出せる為、
  1172. * コア側のコントローラーでいちいちヘルパの定義をしなくてよくなり、
  1173. * プラグインを導入しただけでテンプレート上でプラグインのメソッドが呼び出せるようになる。
  1174. * 例えばページ機能のWISIWIG内でプラグインのメソッドを書き込む事ができる。
  1175. *
  1176. * プラグインのBaserヘルパの命名規則:{プラグイン名}BaserHelper
  1177. * (呼びだし方)$bcBaser->feed(1);
  1178. *
  1179. * @return void
  1180. * @access public
  1181. */
  1182. function _initPluginBasers(){
  1183. $view = $this->_view;
  1184. $plugins = Configure::read('BcStatus.enablePlugins');
  1185. if(!$plugins) {
  1186. return;
  1187. }
  1188. $pluginBasers = array();
  1189. foreach($plugins as $plugin) {
  1190. $pluginName = Inflector::camelize($plugin);
  1191. if(App::import('Helper',$pluginName.'.'.$pluginName.'Baser')) {
  1192. $pluginBasers[] = $pluginName.'BaserHelper';
  1193. }
  1194. }
  1195. $vars = array(
  1196. 'base', 'webroot', 'here', 'params', 'action', 'data', 'themeWeb', 'plugin'
  1197. );
  1198. $c = count($vars);
  1199. foreach($pluginBasers as $key => $pluginBaser) {
  1200. $this->pluginBasers[$key] =& new $pluginBaser();
  1201. for ($j = 0; $j < $c; $j++) {
  1202. if(isset($view->{$vars[$j]})) {
  1203. $this->pluginBasers[$key]->{$vars[$j]} = $view->{$vars[$j]};
  1204. }
  1205. }
  1206. }
  1207. }
  1208. /**
  1209. * プラグインBaserヘルパ用マジックメソッド
  1210. * Baserヘルパに存在しないメソッドが呼ばれた際プラグインのBaserヘルパを呼び出す
  1211. *
  1212. * @param string $method
  1213. * @param array $params
  1214. * @return ixed
  1215. * @access protected
  1216. */
  1217. function call__($method, $params) {
  1218. foreach($this->pluginBasers as $pluginBaser){
  1219. if(method_exists($pluginBaser,$method)){
  1220. return call_user_func_array(array(&$pluginBaser, $method), $params);
  1221. }
  1222. }
  1223. }
  1224. /**
  1225. * 文字列を検索しマークとしてタグをつける
  1226. *
  1227. * @param string $search 検索文字列
  1228. * @param string $text 検索対象文字列
  1229. * @param string $name マーク用タグ
  1230. * @param array $attributes タグの属性
  1231. * @param boolean $escape エスケープ有無
  1232. * @return string $text 変換後文字列
  1233. * @access public
  1234. */
  1235. function mark($search, $text, $name = 'strong', $attributes = array(), $escape = false) {
  1236. if(!is_array($search)) {
  1237. $search = array($search);
  1238. }
  1239. foreach($search as $value) {
  1240. $text = str_replace($value, $this->BcHtml->tag($name, $value, $attributes, $escape), $text);
  1241. }
  1242. return $text;
  1243. }
  1244. /**
  1245. * サイトマップを出力する
  1246. *
  1247. * @param mixid $pageCategoryId / '' / 0
  1248. * @param string $recursive
  1249. * @return void
  1250. * @access public
  1251. */
  1252. function sitemap($pageCategoryId = null, $recursive = null) {
  1253. $pageList = $this->requestAction('/contents/get_page_list_recursive', array('pass' => array($pageCategoryId, $recursive)));
  1254. $params = array('pageList' => $pageList);
  1255. if(empty($_SESSION['Auth']['User'])) {
  1256. $params = am($params, array(
  1257. 'cache' => array(
  1258. 'time' => Configure::read('BcCache.defaultCachetime'),
  1259. 'key' => $pageCategoryId))
  1260. );
  1261. }
  1262. $this->element('sitemap', $params);
  1263. }
  1264. /**
  1265. * Flashを表示する
  1266. *
  1267. * @param string $path
  1268. * @param string $id
  1269. * @param int $width
  1270. * @param int $height
  1271. * @param array $options
  1272. * @return string
  1273. */
  1274. function swf($path, $id, $width, $height, $options = array()) {
  1275. $options = array_merge(array(
  1276. 'version' => 7,
  1277. 'script' => 'swfobject-2.2',
  1278. 'noflash' => '&nbsp;'
  1279. ), $options);
  1280. extract($options);
  1281. if(!preg_match('/^\//', $path)) {
  1282. $path = '/img/'.$path;
  1283. }
  1284. if(strpos($path, "\.") === false) {
  1285. $path .= '.swf';
  1286. }
  1287. $path = $this->webroot($path);
  1288. $path = 'http://projects.localhost:8888'.$path;
  1289. $out .= $this->js($script, true)."\n";
  1290. $out = <<< END_FLASH
  1291. <div id="{$id}">{$noflash}</div>
  1292. <script type="text/javascript">
  1293. swfobject.embedSWF("{$path}", "{$id}", "{$width}", "{$height}", "{$version}");
  1294. </script>
  1295. END_FLASH;
  1296. echo $out;
  1297. }
  1298. /**
  1299. * URLをリンクとして利用可能なURLに変換する
  1300. * ページの確認用URL取得に利用する
  1301. * /smartphone/about → /s/about
  1302. *
  1303. * @param string $url
  1304. * @param string $type mobile / smartphone
  1305. * @return string URL
  1306. */
  1307. function changePrefixToAlias($url, $type) {
  1308. $alias = Configure::read("BcAgent.{$type}.alias");
  1309. $prefix = Configure::read("BcAgent.{$type}.prefix");
  1310. return preg_replace('/^\/'.$prefix.'\//is', '/'.$alias.'/', $url);
  1311. }
  1312. /**
  1313. * 現在のログインユーザーが管理者グループかどうかチェックする
  1314. *
  1315. * @return boolean
  1316. * @access public
  1317. */
  1318. function isAdminUser($id = null) {
  1319. if(!$id && !empty($this->_view->viewVars['user']['user_group_id'])) {
  1320. $id = $this->_view->viewVars['user']['user_group_id'];
  1321. }
  1322. if($id == 1) {
  1323. return true;
  1324. } else {
  1325. return false;
  1326. }
  1327. }
  1328. /**
  1329. * 固定ページ判定
  1330. *
  1331. * @return boolean
  1332. * @access public
  1333. */
  1334. function isPage() {
  1335. return $this->Page->isPageUrl($this->getHere());
  1336. }
  1337. /**
  1338. * 現在のページの純粋なURLを取得
  1339. * スマートURLかどうか、サブフォルダかどうかに依存しないスラッシュから始まるURL
  1340. *
  1341. * @return string
  1342. * @access public
  1343. */
  1344. function getHere() {
  1345. return '/' . preg_replace('/^\//', '', $this->params['url']['url']);
  1346. }
  1347. /**
  1348. * ページカテゴリのトップ判定
  1349. *
  1350. * @return boolean
  1351. * @access public
  1352. */
  1353. function isCategoryTop() {
  1354. $url = $this->getHere();
  1355. $url = preg_replace('/^\//', '', $url);
  1356. if(preg_match('/\/$/', $url)) {
  1357. $url .= 'index';
  1358. }
  1359. if(preg_match('/\/index$/', $url)) {
  1360. $param = explode('/', $url);
  1361. if(count($param) >= 2) {
  1362. return true;
  1363. }
  1364. }
  1365. return false;
  1366. }
  1367. /**
  1368. * ページをエレメントとして読み込む
  1369. *
  1370. * ※ レイアウトは読み込まない
  1371. * @param int $id
  1372. */
  1373. function page($id, $params = array(), $options = array()) {
  1374. if(isset($this->_view->viewVars['pageRecursive']) && !$this->_view->viewVars['pageRecursive']) {
  1375. return;
  1376. }
  1377. $options = array_merge(array(
  1378. 'loadHelpers' => false,
  1379. 'subDir' => true,
  1380. 'recursive'=> true
  1381. ), $options);
  1382. extract($options);
  1383. $this->_view->viewVars['pageRecursive'] = $recursive;
  1384. // 現在のページの情報を退避
  1385. $currentId = null;
  1386. $description = $this->getDescription();
  1387. $title = $this->getContentsTitle();
  1388. if(!empty($this->_view->loaded['bcPage'])) {
  1389. $currentId = $this->_view->loaded['bcPage']->data['Page']['id'];
  1390. }
  1391. // urlを取得
  1392. $PageClass =& ClassRegistry::init('Page');
  1393. $page = $PageClass->find('first', array('conditions' => am(array('Page.id' => $id), $PageClass->getConditionAllowPublish()), 'recursive' => -1));
  1394. if($page) {
  1395. $url = '/../pages'.$PageClass->getPageUrl($page);
  1396. $this->element($url, $params, $loadHelpers = false, $subDir = true);
  1397. // 現在のページの情報に戻す
  1398. $this->setDescription($description);
  1399. $this->setTitle($title);
  1400. if($currentId) {
  1401. $this->setPageEditLink($currentId);
  1402. }
  1403. }
  1404. }
  1405. }
  1406. ?>