PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/plugins/blog/views/helpers/blog.php

https://github.com/hashing/basercms
PHP | 738 lines | 373 code | 82 blank | 283 comment | 68 complexity | 519554bcaa81769457be34140e45c6c7 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ブログヘルパー
  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 baser.plugins.blog.views.helpers
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * ブログヘルパー
  22. * @package baser.plugins.blog.views.helpers
  23. */
  24. class BlogHelper extends AppHelper {
  25. /**
  26. * view
  27. *
  28. * @var View
  29. * @access protected
  30. */
  31. var $_view = null;
  32. /**
  33. * ヘルパー
  34. *
  35. * @var array
  36. * @access public
  37. */
  38. var $helpers = array('Html', BC_TIME_HELPER, BC_BASER_HELPER);
  39. /**
  40. * ブログカテゴリモデル
  41. *
  42. * @var BlogCategory
  43. * @access public
  44. */
  45. var $BlogCategory = null;
  46. /**
  47. * コンストラクタ
  48. *
  49. * @return void
  50. * @access public
  51. */
  52. function __construct() {
  53. $this->_view =& ClassRegistry::getObject('view');
  54. $this->_setBlogContent();
  55. }
  56. /**
  57. * ブログコンテンツデータをセットする
  58. *
  59. * @param int $blogContentId
  60. * @return void
  61. * @access protected
  62. */
  63. function _setBlogContent($blogContentId = null) {
  64. if(isset($this->blogContent) && !$blogContentId) {
  65. return;
  66. }
  67. if($blogContentId) {
  68. $BlogContent = ClassRegistry::getObject('BlogContent');
  69. $BlogContent->expects(array());
  70. $this->blogContent = Set::extract('BlogContent', $BlogContent->read(null, $blogContentId));
  71. } elseif(isset($this->_view->viewVars['blogContent'])) {
  72. $this->blogContent = $this->_view->viewVars['blogContent']['BlogContent'];
  73. }
  74. }
  75. /**
  76. * タイトルを表示する
  77. *
  78. * @return void
  79. * @access public
  80. */
  81. function title() {
  82. echo $this->getTitle();
  83. }
  84. /**
  85. * タイトルを取得する
  86. *
  87. * @return string
  88. * @access public
  89. */
  90. function getTitle() {
  91. return $this->blogContent['title'];
  92. }
  93. /**
  94. * ブログの説明文を取得する
  95. *
  96. * @return string
  97. * @access public
  98. */
  99. function getDescription() {
  100. return $this->blogContent['description'];
  101. }
  102. /**
  103. * ブログの説明文を表示する
  104. *
  105. * @return void
  106. * @access public
  107. */
  108. function description() {
  109. echo $this->getDescription();
  110. }
  111. /**
  112. * ブログの説明文が指定されているかどうか
  113. *
  114. * @return boolean
  115. * @access public
  116. */
  117. function descriptionExists() {
  118. if(!empty($this->blogContent['description'])) {
  119. return true;
  120. }else {
  121. return false;
  122. }
  123. }
  124. /**
  125. * 記事のタイトルを表示する
  126. *
  127. * @param array $post
  128. * @return void
  129. */
  130. function postTitle($post, $link = true) {
  131. echo $this->getPostTitle($post, $link);
  132. }
  133. /**
  134. * 記事タイトルを取得する
  135. *
  136. * @param array $post
  137. * @param boolean $link
  138. * @return string
  139. * @access public
  140. */
  141. function getPostTitle($post, $link) {
  142. if($link) {
  143. return $this->getPostLink($post, $post['BlogPost']['name']);
  144. } else {
  145. return $post['BlogPost']['name'];
  146. }
  147. }
  148. /**
  149. * 記事へのリンクを取得する
  150. *
  151. * @param array $post
  152. * @param string $title
  153. * @param array $options
  154. * @return string
  155. * @access public
  156. */
  157. function getPostLink($post, $title, $options = array()) {
  158. $this->_setBlogContent($post['BlogPost']['blog_content_id']);
  159. $url = array('admin'=>false,'plugin'=>'','controller'=>$this->blogContent['name'],'action'=>'archives', $post['BlogPost']['no']);
  160. return $this->BcBaser->getLink($title, $url, $options);
  161. }
  162. /**
  163. * 記事へのリンクを出力する
  164. *
  165. * @param array $post
  166. * @param string $title
  167. * @return void
  168. * @access public
  169. */
  170. function postLink($post, $title, $options = array()) {
  171. echo $this->getPostLink($post, $title, $options);
  172. }
  173. /**
  174. * コンテンツを表示する
  175. *
  176. * @param array $post
  177. * @param mixied boolean / string $moreLink
  178. * @return void
  179. * @access public
  180. */
  181. function postContent($post,$moreText = true, $moreLink = false, $cut = false) {
  182. echo $this->getPostContent($post, $moreText, $moreLink, $cut);
  183. }
  184. /**
  185. * コンテンツデータを取得する
  186. *
  187. * @param array $post
  188. * @param mixied boolean / string $moreLink
  189. * @return string
  190. * @access public
  191. */
  192. function getPostContent($post,$moreText = true, $moreLink = false, $cut = false) {
  193. if($moreLink === true) {
  194. $moreText = '≫ 続きを読む';
  195. }elseif($moreLink !== false) {
  196. $moreText = $moreLink;
  197. }
  198. $out = '<div class="post-body">'.$post['BlogPost']['content'].'</div>';
  199. if($moreLink && trim($post['BlogPost']['detail']) && trim($post['BlogPost']['detail']) != "<br>") {
  200. $out .= '<p class="more">'.$this->Html->link($moreText, array('admin'=>false,'plugin'=>'', 'controller'=>$this->blogContent['name'],'action'=>'archives', $post['BlogPost']['no'],'#'=>'post-detail'), null,null,false).'</p>';
  201. }elseif($moreText && $post['BlogPost']['detail']) {
  202. $out .= '<div id="post-detail">'.$post['BlogPost']['detail'].'</div>';
  203. }
  204. if($cut) {
  205. $out = mb_substr(strip_tags($out), 0, $cut, 'UTF-8');
  206. }
  207. return $out;
  208. }
  209. /**
  210. * カテゴリを出力する
  211. * @param array $post
  212. * @return void
  213. * @access puublic
  214. */
  215. function category($post, $options = array()) {
  216. echo $this->getCategory($post, $options);
  217. }
  218. /**
  219. * カテゴリを取得する
  220. *
  221. * @param array $post
  222. * @return string
  223. */
  224. function getCategory($post, $options = array()) {
  225. if(!empty($post['BlogCategory']['name'])) {
  226. $options = am(array('link' => true), $options);
  227. $link = false;
  228. if($options['link']) {
  229. $link = true;
  230. }
  231. unset($options['link']);
  232. if($link) {
  233. if(!isset($this->Html)){
  234. $this->Html = new HtmlHelper();
  235. }
  236. return $this->Html->link($post['BlogCategory']['title'],$this->getCategoryUrl($post['BlogCategory']['id'], $options),$options,null,false);
  237. } else {
  238. return $post['BlogCategory']['title'];
  239. }
  240. }else {
  241. return '';
  242. }
  243. }
  244. /**
  245. * タグを出力する
  246. *
  247. * @param array $post
  248. * @param string $separator
  249. * @return void
  250. * @access public
  251. */
  252. function tag($post, $separator = ' , ') {
  253. echo $this->getTag($post, $separator);
  254. }
  255. /**
  256. * タグを取得する
  257. *
  258. * @param array $post
  259. * @param string $separator
  260. * @return void
  261. * @access public
  262. */
  263. function getTag($post, $separator = ' , ') {
  264. $tagLinks = array();
  265. if(!empty($post['BlogTag'])) {
  266. foreach($post['BlogTag'] as $tag) {
  267. $url = array('admin'=>false,'plugin'=>'','controller'=>$this->blogContent['name'],'action'=>'archives', 'tag', $tag['name']);
  268. $tagLinks[] = $this->BcBaser->getLink($tag['name'], $url);
  269. }
  270. }
  271. if($tagLinks) {
  272. return implode($separator, $tagLinks);
  273. } else {
  274. return '';
  275. }
  276. }
  277. /**
  278. * カテゴリのURLを取得する
  279. * [注意] リンク関数でラップする前提の為、ベースURLは考慮されない
  280. *
  281. * @param string $blogCategoyId
  282. * @return void
  283. */
  284. function getCategoryUrl($blogCategoryId, $options = array()) {
  285. $options = array_merge(array(
  286. 'named' => array()
  287. ), $options);
  288. extract($options);
  289. if (!isset($this->BlogCategory)) {
  290. $this->BlogCategory =& ClassRegistry::init('BlogCategory','Model');
  291. }
  292. $categoryPath = $this->BlogCategory->getPath($blogCategoryId);
  293. $blogContentId = $categoryPath[0]['BlogCategory']['blog_content_id'];
  294. $this->_setBlogContent($blogContentId);
  295. $blogContentName = $this->blogContent['name'];
  296. $path = array('category');
  297. if($categoryPath) {
  298. foreach($categoryPath as $category) {
  299. $path[] = $category['BlogCategory']['name'];
  300. }
  301. }
  302. if($named) {
  303. $path = array_merge($path, $named);
  304. }
  305. $url = Router::url(am(array('admin'=>false,'plugin'=>'','controller'=>$blogContentName,'action'=>'archives'), $path));
  306. $baseUrl = preg_replace('/\/$/', '', BC_BASE_URL);
  307. return preg_replace('/^'.preg_quote($baseUrl, '/').'/', '', $url);
  308. }
  309. /**
  310. * 登録日
  311. *
  312. * @param array $post
  313. * @param string $format
  314. * @return void
  315. * @access public
  316. */
  317. function postDate($post,$format = 'Y/m/d') {
  318. echo $this->getPostDate($post, $format);
  319. }
  320. /**
  321. * 登録日
  322. *
  323. * @param array $post
  324. * @param string $format
  325. * @return void
  326. * @access public
  327. */
  328. function getPostDate($post,$format = 'Y/m/d') {
  329. return $this->BcTime->format($format,$post['BlogPost']['posts_date']);
  330. }
  331. /**
  332. * 投稿者を出力
  333. *
  334. * @param array $post
  335. * @return void
  336. * @access public
  337. */
  338. function author($post) {
  339. $author = '';
  340. if(!empty($post['User']['real_name_1'])) {
  341. $author .= $post['User']['real_name_1'];
  342. }
  343. if(!empty($post['User']['real_name_2'])) {
  344. $author .= " ".$post['User']['real_name_2'];
  345. }
  346. echo $author;
  347. }
  348. /**
  349. * カテゴリーリストを取得する
  350. *
  351. * @param $categories
  352. * @param $depth
  353. * @return string
  354. * @access public
  355. */
  356. function getCategoryList($categories,$depth=3, $count = false, $options = array()) {
  357. return $this->_getCategoryList($categories,$depth, 1, $count, $options);
  358. }
  359. /**
  360. * カテゴリーリストを取得する
  361. *
  362. * @param $categories
  363. * @param $depth
  364. * @return string
  365. * @access public
  366. */
  367. function _getCategoryList($categories, $depth=3, $current=1, $count = false, $options = array()) {
  368. if($depth < $current) {
  369. return '';
  370. }
  371. if($categories) {
  372. $out = '<ul class="depth-'.$current.'">';
  373. $current++;
  374. foreach($categories as $category) {
  375. if($count && isset($category['BlogCategory']['count'])) {
  376. $category['BlogCategory']['title'] .= '('.$category['BlogCategory']['count'].')';
  377. }
  378. $url = $this->getCategoryUrl($category['BlogCategory']['id']);
  379. $url = preg_replace('/^\//', '', $url);
  380. if($this->_view->params['url']['url'] == $url) {
  381. $class = ' class="current"';
  382. } elseif(!empty($this->_view->params['named']['category']) && $this->_view->params['named']['category'] == $category['BlogCategory']['name']) {
  383. $class = ' class="selected"';
  384. } else {
  385. $class = '';
  386. }
  387. $out .= '<li'.$class.'>'.$this->getCategory($category, $options);
  388. if(!empty($category['BlogCategory']['children'])) {
  389. $out.= $this->_getCategoryList($category['BlogCategory']['children'],$depth,$current, $count, $options);
  390. }
  391. $out.='</li>';
  392. }
  393. $out .= '</ul>';
  394. return $out;
  395. }else {
  396. return '';
  397. }
  398. }
  399. /**
  400. * ブログ編集ページへのリンクを出力【非推奨】
  401. *
  402. * @param string $id
  403. * @return void
  404. * @access public
  405. * @deprecated ツールバーに移行
  406. */
  407. function editPost($blogContentId,$blogPostId) {
  408. if(empty($this->params['admin']) && !empty($this->_view->viewVars['user']) && !Configure::read('BcRequest.agent')) {
  409. echo '<div class="edit-link">'.$this->BcBaser->getLink('≫ 編集する', array('admin' => true, 'prefix' => 'blog', 'controller' => 'blog_posts', 'action' => 'edit', $blogContentId, $blogPostId), array('target' => '_blank')).'</div>';
  410. }
  411. }
  412. /**
  413. * 前の記事へのリンクを取得する
  414. *
  415. * @param array $post
  416. * @param string $title
  417. * @param array $htmlAttributes
  418. * @return void
  419. * @access pulic
  420. */
  421. function prevLink($post,$title='',$htmlAttributes = array()) {
  422. if(ClassRegistry::isKeySet('BlogPost')) {
  423. $BlogPost = ClassRegistry::getObject('BlogPost');
  424. } else {
  425. $BlogPost = ClassRegistry::init('BlogPost');
  426. }
  427. $_htmlAttributes = array('class'=>'prev-link','arrow'=>'≪ ');
  428. $htmlAttributes = am($_htmlAttributes,$htmlAttributes);
  429. $arrow = $htmlAttributes['arrow'];
  430. unset($htmlAttributes['arrow']);
  431. $BlogPost =& ClassRegistry::getObject('BlogPost');
  432. $conditions = array();
  433. $conditions['BlogPost.posts_date <'] = $post['BlogPost']['posts_date'];
  434. $conditions["BlogPost.blog_content_id"] = $post['BlogPost']['blog_content_id'];
  435. $conditions = am($conditions, $BlogPost->getConditionAllowPublish());
  436. // 毎秒抽出条件が違うのでキャッシュしない
  437. $prevPost = $BlogPost->find('first', array(
  438. 'conditions' => $conditions,
  439. 'fields' => array('no','name'),
  440. 'order' => 'posts_date DESC',
  441. 'recursive' => 0,
  442. 'cache' => false
  443. ));
  444. if($prevPost) {
  445. $no = $prevPost['BlogPost']['no'];
  446. if(!$title) {
  447. $title = $arrow.$prevPost['BlogPost']['name'];
  448. }
  449. $this->BcBaser->link($title, array('admin'=>false,'plugin'=>'', 'controller'=>$this->blogContent['name'],'action'=>'archives', $no),$htmlAttributes);
  450. }
  451. }
  452. /**
  453. * 次の記事へのリンクを取得する
  454. *
  455. * @param array $post
  456. * @return void
  457. * @access public
  458. */
  459. function nextLink($post,$title='',$htmlAttributes = array()) {
  460. if(ClassRegistry::isKeySet('BlogPost')) {
  461. $BlogPost = ClassRegistry::getObject('BlogPost');
  462. } else {
  463. $BlogPost = ClassRegistry::init('BlogPost');
  464. }
  465. $_htmlAttributes = array('class'=>'next-link','arrow'=>' ≫');
  466. $htmlAttributes = am($_htmlAttributes,$htmlAttributes);
  467. $arrow = $htmlAttributes['arrow'];
  468. unset($htmlAttributes['arrow']);
  469. $BlogPost =& ClassRegistry::getObject('BlogPost');
  470. $conditions = array();
  471. $conditions['BlogPost.posts_date >'] = $post['BlogPost']['posts_date'];
  472. $conditions["BlogPost.blog_content_id"] = $post['BlogPost']['blog_content_id'];
  473. $conditions = am($conditions, $BlogPost->getConditionAllowPublish());
  474. // 毎秒抽出条件が違うのでキャッシュしない
  475. $nextPost = $BlogPost->find('first', array(
  476. 'conditions' => $conditions,
  477. 'fields' => array('no','name'),
  478. 'order' => 'posts_date',
  479. 'recursive' => 0,
  480. 'cache' => false
  481. ));
  482. if($nextPost) {
  483. $no = $nextPost['BlogPost']['no'];
  484. if(!$title) {
  485. $title = $nextPost['BlogPost']['name'].$arrow;
  486. }
  487. $this->BcBaser->link($title, array('admin'=>false,'plugin'=>'','mobile'=>false,'controller'=>$this->blogContent['name'],'action'=>'archives', $no),$htmlAttributes);
  488. }
  489. }
  490. /**
  491. * レイアウトテンプレートを取得
  492. * コンボボックスのソースとして利用
  493. * TODO 別のヘルパに移動
  494. * @return array
  495. * @access public
  496. */
  497. function getLayoutTemplates() {
  498. $templatesPathes = array();
  499. if($this->BcBaser->siteConfig['theme']){
  500. $templatesPathes[] = WWW_ROOT.'themed'.DS.$this->BcBaser->siteConfig['theme'].DS.'layouts'.DS;
  501. }
  502. $templatesPathes[] = APP . 'plugins' . DS . 'blog'.DS.'views'.DS.'layouts'.DS;
  503. $templatesPathes = am($templatesPathes,array(BASER_PLUGINS.'blog'.DS.'views'.DS.'layouts'.DS,
  504. BASER_VIEWS.'layouts'.DS));
  505. $_templates = array();
  506. foreach($templatesPathes as $templatesPath){
  507. $folder = new Folder($templatesPath);
  508. $files = $folder->read(true, true);
  509. $foler = null;
  510. if($files[1]){
  511. if($_templates){
  512. $_templates = am($_templates,$files[1]);
  513. }else{
  514. $_templates = $files[1];
  515. }
  516. }
  517. }
  518. $templates = array();
  519. foreach($_templates as $template){
  520. $ext = Configure::read('BcApp.templateExt');
  521. if($template != 'installations'.$ext){
  522. $template = basename($template, $ext);
  523. $templates[$template] = $template;
  524. }
  525. }
  526. return $templates;
  527. }
  528. /**
  529. * ブログテンプレートを取得
  530. * コンボボックスのソースとして利用
  531. * TODO 別のヘルパに移動
  532. * @return array
  533. * @access public
  534. */
  535. function getBlogTemplates() {
  536. $templatesPathes = array();
  537. if($this->BcBaser->siteConfig['theme']){
  538. $templatesPathes[] = WWW_ROOT.'themed'.DS.$this->BcBaser->siteConfig['theme'].DS.'blog'.DS;
  539. }
  540. $templatesPathes[] = APP . 'plugins' . DS . 'blog'.DS.'views'.DS.'blog'.DS;
  541. $templatesPathes[] = BASER_PLUGINS.'blog'.DS.'views'.DS.'blog'.DS;
  542. $_templates = array();
  543. foreach($templatesPathes as $templatePath){
  544. $folder = new Folder($templatePath);
  545. $files = $folder->read(true, true);
  546. $foler = null;
  547. if($files[0]){
  548. if($_templates){
  549. $_templates = am($_templates,$files[0]);
  550. }else{
  551. $_templates = $files[0];
  552. }
  553. }
  554. }
  555. $templates = array();
  556. foreach($_templates as $template){
  557. if($template != 'rss' && $template != 'mobile'){
  558. $templates[$template] = $template;
  559. }
  560. }
  561. return $templates;
  562. }
  563. /**
  564. * 公開状態を取得する
  565. *
  566. * @param array $data データリスト
  567. * @return boolean 公開状態
  568. * @access public
  569. */
  570. function allowPublish($data){
  571. if(ClassRegistry::isKeySet('BlogPost')) {
  572. $BlogPost = ClassRegistry::getObject('BlogPost');
  573. } else {
  574. $BlogPost = ClassRegistry::init('BlogPost');
  575. }
  576. return $BlogPost->allowPublish($data);
  577. }
  578. /**
  579. * 記事中の画像を出力する
  580. *
  581. * @param array $post
  582. * @param array $options
  583. * @return void
  584. * @access public
  585. */
  586. function postImg($post, $options = array()) {
  587. echo $this->getPostImg($post, $options);
  588. }
  589. /**
  590. * 記事中の画像を取得する
  591. *
  592. * @param array $post
  593. * @param array $options
  594. * @return void
  595. * @access public
  596. */
  597. function getPostImg($post, $options = array()) {
  598. $this->_setBlogContent($post['BlogPost']['blog_content_id']);
  599. $_options = array('num' => 1, 'link' => true, 'alt' => $post['BlogPost']['name']);
  600. $options = am($_options, $options);
  601. extract($options);
  602. unset($options['num']);
  603. unset($options['link']);
  604. $contents = $post['BlogPost']['content'].$post['BlogPost']['detail'];
  605. $pattern = '/<img.*?src="([^"]+)"[^>]*>/is';
  606. if(!preg_match_all($pattern, $contents, $matches)){
  607. return '';
  608. }
  609. if(isset($matches[1][$num-1])) {
  610. $url = $matches[1][$num-1];
  611. $url = preg_replace('/^'.preg_quote($this->base, '/').'/', '', $url);
  612. $img = $this->BcBaser->getImg($url, $options);
  613. if($link) {
  614. return $this->BcBaser->getLink($img, $url = array('admin'=>false,'plugin'=>'','controller'=>$this->blogContent['name'],'action'=>'archives', $post['BlogPost']['no']));
  615. } else {
  616. return $img;
  617. }
  618. } else {
  619. return '';
  620. }
  621. }
  622. /**
  623. * 記事の本文、詳細の中で指定したIDの中のHTMLを取得する
  624. *
  625. * @param array $post
  626. * @param string $id
  627. * @return string
  628. * @access public
  629. */
  630. function getHtmlById($post, $id) {
  631. $content = $post['BlogPost']['content'].$post['BlogPost']['detail'];
  632. $values = array();
  633. $pattern = '/<([^\s]+)\s[^>]*?id="'.$id.'"[^>]*>(.*?)<\/\1>/is';
  634. if(preg_match($pattern, $content, $matches)){
  635. return $matches[2];
  636. }else{
  637. return '';
  638. }
  639. }
  640. /**
  641. * 親カテゴリを取得する
  642. *
  643. * @param array $post
  644. * @return array $parentCategory
  645. * @access public
  646. */
  647. function getParentCategory($post) {
  648. if(empty($post['BlogCategory']['id'])) {
  649. return null;
  650. }
  651. $BlogCategory = ClassRegistry::init('Blog.BlogCategory');
  652. return $BlogCategory->getparentnode($post['BlogCategory']['id']);
  653. }
  654. }
  655. ?>