PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/plugins/feed/controllers/feed_controller.php

https://github.com/hashing/basercms
PHP | 316 lines | 159 code | 36 blank | 121 comment | 33 complexity | 24c76b22046c98712a5aea73a468f781 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.feed.controllers
  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. * Include files
  22. */
  23. /**
  24. * フィードコントローラー
  25. *
  26. * @package baser.plugins.feed.controllers
  27. */
  28. class FeedController extends FeedAppController {
  29. /**
  30. * クラス名
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $name = 'Feed';
  36. /**
  37. * コンポーネント
  38. * @var array
  39. * @access public
  40. */
  41. var $components = array('RequestHandler');
  42. /**
  43. * モデル
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. var $uses = array("Feed.FeedConfig","Feed.FeedDetail","Feed.RssEx");
  49. /**
  50. * ヘルパー
  51. *
  52. * @var array
  53. * @access public
  54. */
  55. var $helpers = array('Cache',BC_TEXT_HELPER,'Feed.Feed', BC_ARRAY_HELPER);
  56. /**
  57. * beforeFilter
  58. *
  59. * @return void
  60. * @access public
  61. */
  62. function beforeFilter() {
  63. /* 認証設定 */
  64. $this->BcAuth->allow('index', 'mobile_index', 'smartphone_index', 'ajax', 'smartphone_ajax');
  65. parent::beforeFilter();
  66. }
  67. /**
  68. * [PUBLIC] フィードを一覧表示する
  69. *
  70. * @param int $id
  71. * @return void
  72. * @access public
  73. */
  74. function index($id) {
  75. $this->navis = array();
  76. // IDの指定がなかった場合はエラーとする
  77. if(!$id) {
  78. $this->render('error');
  79. return;
  80. }
  81. // feed設定データ取得
  82. $feedConfig = $this->FeedConfig->read(null,$id);
  83. $feedDetails = $this->FeedDetail->find('all', array('conditions' => array("FeedDetail.feed_config_id" => $id)));
  84. // データが取得できなかった場合はエラーとする
  85. if(!$feedConfig||!$feedDetails) {
  86. $this->render('error');
  87. return;
  88. }
  89. $cachetime = 0;
  90. $itemExists = false;
  91. foreach($feedDetails as $feedDetail) {
  92. // フィードを取得する
  93. if(strpos($feedDetail['FeedDetail']['category_filter'],'|') !== false) {
  94. $categoryFilter = explode('|',$feedDetail['FeedDetail']['category_filter']);
  95. }else {
  96. $categoryFilter = $feedDetail['FeedDetail']['category_filter'];
  97. }
  98. $url = '';
  99. if(strpos($feedDetail['FeedDetail']['url'],'http')!==false) {
  100. $url = $feedDetail['FeedDetail']['url'];
  101. }else {
  102. if(empty($_SERVER['HTTPS'])) {
  103. $protocol = 'http';
  104. }else {
  105. $protocol = 'https';
  106. }
  107. if($protocol) {
  108. $url = $protocol . '://'.$_SERVER['HTTP_HOST'].$this->base.$feedDetail['FeedDetail']['url'];
  109. }
  110. }
  111. $feed = $this->RssEx->findAll($url ,null, $feedDetail['FeedDetail']['cache_time'] ,$categoryFilter);
  112. $feeds[] = $feed;
  113. if($cachetime < (strtotime($feedDetail['FeedDetail']['cache_time'])-time())) {
  114. $cachetime = (strtotime($feedDetail['FeedDetail']['cache_time'])-time());
  115. }
  116. if($feed['Items']) {
  117. $itemExists = true;
  118. }
  119. }
  120. // データが取得できなかった場合はレンダリングして終了
  121. if(!$itemExists) {
  122. $this->render($feedConfig['FeedConfig']['template']);
  123. return;
  124. }
  125. // フィードタイトルをtitle_noとしてインデックス番号に変換する
  126. if($feedConfig['FeedConfig']['feed_title_index']) {
  127. $titleIndex = explode("|",$feedConfig['FeedConfig']['feed_title_index']);
  128. foreach($feeds as $key => $feed) {
  129. foreach($titleIndex as $key2 => $title) {
  130. if($title == $feed['Channel']['title']['value']) {
  131. foreach($feed['Items'] as $key3 => $item) {
  132. $feeds[$key]['Items'][$key3]['feed_title_no']['value'] = $key2+1;
  133. $feeds[$key]['Items'][$key3]['feed_title']['value'] = $title;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // アイテムをマージ
  140. $items = array();
  141. foreach($feeds as $feed) {
  142. if(!empty($feed['Items'])) {
  143. $items = array_merge($items,$feed['Items']);
  144. }
  145. }
  146. // カテゴリをcategory_noとしてインデックス番号に変換する
  147. if($feedConfig['FeedConfig']['category_index']) {
  148. $categoryIndex = explode("|",$feedConfig['FeedConfig']['category_index']);
  149. foreach($items as $key => $item) {
  150. foreach($categoryIndex as $key2 => $category) {
  151. if($category == $item['category']['value']) {
  152. $items[$key]['category_no']['value'] = $key2+1;
  153. }
  154. }
  155. }
  156. }
  157. // 日付を秒数に変換
  158. foreach($items as $key => $item) {
  159. if(!empty($item['pubDate']['value']))
  160. $items[$key]['timestamp'] = strtotime($item['pubDate']['value']);
  161. }
  162. // 日付で並び替え
  163. usort($items, array($this, "_sortDescByTimestamp"));
  164. // 件数で絞り込み
  165. $items = array_slice($items, 0, $feedConfig['FeedConfig']['display_number']);
  166. /* キャッシュを設定 */
  167. if(!isset($_SESSION['Auth']['User'])) {
  168. $this->cacheAction =$cachetime;
  169. }
  170. $this->set('cachetime', $cachetime);
  171. $this->set('items',$items);
  172. $this->render($feedConfig['FeedConfig']['template']);
  173. }
  174. /**
  175. * [MOBILE] フィードを一覧表示する
  176. *
  177. * @param int $id
  178. * @return void
  179. * @access public
  180. */
  181. function mobile_index($id) {
  182. $this->setAction('index',$id);
  183. }
  184. /**
  185. * [SMARTPHONE] フィードを一覧表示する
  186. *
  187. * @param int $id
  188. * @return void
  189. * @access public
  190. */
  191. function smartphone_index($id) {
  192. $this->setAction('index',$id);
  193. }
  194. /**
  195. * [PUBLIC] フィードをAJAXで読み込む為のJavascriptを生成する
  196. *
  197. * @param int $id
  198. * @return void
  199. * @access public
  200. */
  201. function ajax($id) {
  202. if(strpos($id,'.js') !== false) {
  203. $id = str_replace('.js','',$id);
  204. }
  205. Configure::write('debug', 0);
  206. $this->cacheAction = Configure::read('BcCache.defaultCachetime');
  207. $this->layout = "ajax";
  208. // idを設定
  209. $this->set('id',$id);
  210. }
  211. /**
  212. * [PUBLIC] フィードをAJAXで読み込む為のJavascriptを生成する
  213. *
  214. * @param int $id
  215. * @return void
  216. * @access public
  217. */
  218. function smartphone_ajax($id) {
  219. $this->setAction('ajax',$id);
  220. }
  221. /**
  222. * タイムスタンプを元に降順に並び替える
  223. *
  224. * @param array $a
  225. * @param array $b
  226. * @return array
  227. * @access protected
  228. */
  229. function _sortDescByTimestamp($a, $b) {
  230. if ($a['timestamp'] == $b['timestamp']) {
  231. return 0;
  232. }
  233. return ($a['timestamp'] > $b['timestamp']) ? -1 : 1;
  234. }
  235. /*
  236. * バブルソート
  237. *
  238. * @param array $val = ソートする配列
  239. * @param string $flag = ソート対象の配列要素
  240. * @param string $order = ソートの昇順・降順 デフォルトは昇順
  241. * @return array 並び替え後の配列
  242. * @access protected
  243. */
  244. function _bsort(&$val, $flag = "", $order = "ASC") {
  245. for($i=0;$i<count($val)-1;$i++) {
  246. for($j=count($val)-1;$j>$i;$j--) {
  247. if($flag) {
  248. if($order=="DESC") {
  249. if($val[$j]["".$flag.""]>$val[$j-1]["".$flag.""]) {
  250. $t=$val[$j];
  251. $val[$j]=$val[$j-1];
  252. $val[$j-1]=$t;
  253. }
  254. } else {
  255. if($val[$j]["".$flag.""]<$val[$j-1]["".$flag.""]) {
  256. $t=$val[$j];
  257. $val[$j]=$val[$j-1];
  258. $val[$j-1]=$t;
  259. }
  260. }
  261. } else {
  262. if($order=="DESC") {
  263. if($val[$j]>$val[$j-1]) {
  264. $t=$val[$j];
  265. $val[$j]=$val[$j-1];
  266. $val[$j-1]=$t;
  267. }
  268. } else {
  269. if($val[$j]<$val[$j-1]) {
  270. $t=$val[$j];
  271. $val[$j]=$val[$j-1];
  272. $val[$j-1]=$t;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. ?>