PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/plugins/feed/controllers/feed_configs_controller.php

https://github.com/hashing/basercms
PHP | 393 lines | 170 code | 63 blank | 160 comment | 25 complexity | f62a88464f152f91f1daaedda0e4e265 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 FeedConfigsController extends FeedAppController {
  29. /**
  30. * クラス名
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $name = 'FeedConfigs';
  36. /**
  37. * モデル
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $uses = array("Feed.FeedConfig","Feed.FeedDetail","Feed.RssEx");
  43. /**
  44. * ヘルパー
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $helpers = array(BC_TEXT_HELPER, BC_TIME_HELPER, BC_FORM_HELPER, 'Feed.Feed');
  50. /**
  51. * コンポーネント
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $components = array('BcAuth','Cookie','BcAuthConfigure');
  57. /**
  58. * ぱんくずナビ
  59. *
  60. * @var array
  61. * @access public
  62. */
  63. var $crumbs = array(
  64. array('name' => 'プラグイン管理', 'url' => array('plugin' => '', 'controller' => 'plugins', 'action' => 'index')),
  65. array('name' => 'フィード管理', 'url' => array('controller' => 'feed_configs', 'action' => 'index'))
  66. );
  67. /**
  68. * サブメニューエレメント
  69. *
  70. * @var array
  71. * @access public
  72. */
  73. var $subMenuElements = array();
  74. /**
  75. * before_filter
  76. *
  77. * @return void
  78. * @access public
  79. */
  80. function beforeFilter() {
  81. parent::beforeFilter();
  82. if($this->params['prefix'] == 'admin') {
  83. $this->subMenuElements = array('feed_common');
  84. }
  85. }
  86. /**
  87. * [ADMIN] 一覧表示
  88. *
  89. * @return void
  90. * @access public
  91. */
  92. function admin_index() {
  93. // データを取得
  94. $this->paginate = array('conditions'=>array(),
  95. 'fields'=>array(),
  96. 'order'=>'FeedConfig.id',
  97. 'limit'=>10
  98. );
  99. $feedConfigs = $this->paginate('FeedConfig');
  100. if($feedConfigs) {
  101. $this->set('feedConfigs',$feedConfigs);
  102. }
  103. // 表示設定
  104. $this->pageTitle = 'フィード設定一覧';
  105. $this->help = 'feed_configs_index';
  106. }
  107. /**
  108. * [ADMIN] 登録
  109. *
  110. * @return void
  111. * @access public
  112. */
  113. function admin_add() {
  114. if(empty($this->data)) {
  115. $this->data = $this->FeedConfig->getDefaultValue();
  116. }else {
  117. $this->FeedConfig->create($this->data);
  118. // データを保存
  119. if($this->FeedConfig->save()) {
  120. $id = $this->FeedConfig->getLastInsertId();
  121. $this->Session->setFlash('フィード「'.$this->data['FeedConfig']['name'].'」を追加しました。');
  122. $this->FeedConfig->saveDbLog('フィード「'.$this->data['FeedConfig']['name'].'」を追加しました。');
  123. $this->redirect(array('controller' => 'feed_configs', 'action' => 'edit', $id, '#' => 'headFeedDetail'));
  124. }else {
  125. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  126. }
  127. }
  128. // 表示設定
  129. $this->pageTitle = '新規フィード設定登録';
  130. $this->help = 'feed_configs_form';
  131. $this->render('form');
  132. }
  133. /**
  134. * [ADMIN] 編集
  135. *
  136. * @param int $id
  137. * @return void
  138. * @access public
  139. */
  140. function admin_edit($id) {
  141. if(!$id && empty($this->data)) {
  142. $this->Session->setFlash('無効なIDです。');
  143. $this->redirect(array('action' => 'index'));
  144. }
  145. if(empty($this->data)) {
  146. $this->data = $this->FeedConfig->read(null, $id);
  147. $this->set('feedConfig',$this->data);
  148. }else {
  149. // データを保存
  150. if($this->FeedConfig->save($this->data)) {
  151. $this->_clearCache($this->data['FeedConfig']['id']);
  152. $this->Session->setFlash('フィード「'.$this->data['FeedConfig']['name'].'」を更新しました。');
  153. $this->FeedConfig->saveDbLog('フィード「'.$this->data['FeedConfig']['name'].'」を更新しました。');
  154. if($this->data['FeedConfig']['edit_template']){
  155. $this->redirectEditTemplate($this->data['FeedConfig']['template']);
  156. }else{
  157. $this->redirect(array('action' => 'index'));
  158. }
  159. }else {
  160. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  161. }
  162. }
  163. // 表示設定
  164. $this->subMenuElements = am($this->subMenuElements,array('feed_details'));
  165. $this->pageTitle = 'フィード設定編集';
  166. $this->help = 'feed_configs_form';
  167. $this->render('form');
  168. }
  169. /**
  170. * テンプレート編集画面にリダイレクトする
  171. *
  172. * @param string $template
  173. * @return void
  174. * @access public
  175. */
  176. function redirectEditTemplate($template){
  177. $path = 'feed'.DS.$template.$this->ext;
  178. $target = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$path;
  179. $sorces = array(BASER_PLUGINS.'mail'.DS.'views'.DS.$path);
  180. if($this->siteConfigs['theme']){
  181. if(!file_exists($target)){
  182. foreach($sorces as $source){
  183. if(file_exists($source)){
  184. $folder = new Folder();
  185. $folder->create(dirname($target), 0777);
  186. copy($source,$target);
  187. chmod($target,0666);
  188. break;
  189. }
  190. }
  191. }
  192. $this->redirect(array('plugin' => null, 'mail' => false, 'prefix' => false, 'controller' => 'theme_files', 'action' => 'edit', $this->siteConfigs['theme'], 'etc', $path));
  193. }else{
  194. $this->Session->setFlash('現在、「テーマなし」の場合、管理画面でのテンプレート編集はサポートされていません。');
  195. $this->redirect(array('action' => 'index'));
  196. }
  197. }
  198. /**
  199. * [ADMIN] 削除 (ajax)
  200. *
  201. * @param int $id
  202. * @return void
  203. * @access public
  204. */
  205. function _batch_del($ids) {
  206. if($ids) {
  207. foreach($ids as $id) {
  208. // メッセージ用にデータを取得
  209. $feedConfig = $this->FeedConfig->read(null, $id);
  210. // 削除実行
  211. if($this->FeedConfig->del($id)) {
  212. $this->FeedConfig->saveDbLog('フィード「'.$feedConfig['FeedConfig']['name'].'」を削除しました。');
  213. }
  214. }
  215. }
  216. return true;
  217. }
  218. /**
  219. * [ADMIN] 削除 (ajax)
  220. *
  221. * @param int $id
  222. * @return void
  223. * @access public
  224. */
  225. function admin_ajax_delete($id = null) {
  226. if(!$id) {
  227. $this->ajaxError(500, '無効な処理です。');
  228. }
  229. // メッセージ用にデータを取得
  230. $feedConfig = $this->FeedConfig->read(null, $id);
  231. // 削除実行
  232. if($this->FeedConfig->del($id)) {
  233. $this->FeedConfig->saveDbLog('フィード「'.$feedConfig['FeedConfig']['name'].'」を削除しました。');
  234. exit(true);
  235. }
  236. exit();
  237. }
  238. /**
  239. * [ADMIN] 削除
  240. *
  241. * @param int $id
  242. * @return void
  243. * @access public
  244. */
  245. function admin_delete($id = null) {
  246. if(!$id) {
  247. $this->Session->setFlash('無効なIDです。');
  248. $this->redirect(array('action' => 'index'));
  249. return;
  250. }
  251. // メッセージ用にデータを取得
  252. $feedConfig = $this->FeedConfig->read(null, $id);
  253. // 削除実行
  254. if($this->FeedConfig->del($id)) {
  255. $this->Session->setFlash($feedConfig['FeedConfig']['name'].' を削除しました。');
  256. $this->FeedConfig->saveDbLog('フィード「'.$feedConfig['FeedConfig']['name'].'」を削除しました。');
  257. }else {
  258. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  259. }
  260. $this->redirect(array('action' => 'index'));
  261. }
  262. /**
  263. * 読み込んだフィードをプレビュー表示する
  264. *
  265. * @param string $id
  266. * @return void
  267. * @access public
  268. */
  269. function admin_preview($id) {
  270. if(!$id) $this->notFound();
  271. $this->pageTitle = 'プレビュー:'.$this->FeedConfig->field('name',array('FeedConfig.id'=>$id));
  272. $this->set('id',$id);
  273. }
  274. /**
  275. * フィードのキャッシュを全て削除する
  276. *
  277. * @return void
  278. * @access public
  279. */
  280. function admin_delete_cache() {
  281. $this->_clearCache();
  282. $this->Session->setFlash('フィードのキャッシュを削除しました。');
  283. $this->redirect(array('controller' => 'feed_configs', 'action' => 'index'));
  284. }
  285. /**
  286. * フィードのキャッシュを削除する(requestAction用)
  287. *
  288. * @param string $feedConfigId
  289. * @param string $url
  290. * @return void
  291. * @access protected
  292. */
  293. function admin_clear_cache($feedConfigId = '', $url = '') {
  294. $this->_clearCache($feedConfigId, $url);
  295. }
  296. /**
  297. * フィードのキャッシュを削除する
  298. * TODO 第2引き数がない場合、全てのRSSのキャッシュを削除してしまう仕様となっているので
  299. * RSSキャッシュ保存名をURLのハッシュ文字列ではなく、feed_detail_idを元にした文字列に変更し、
  300. * feed_detail_idで指定して削除できるようにする
  301. *
  302. * @param string $feedConfigId
  303. * @param string $url
  304. * @return void
  305. * @access protected
  306. */
  307. function _clearCache($feedConfigId = '', $url = '') {
  308. if($feedConfigId) {
  309. clearViewCache('/feed/index/'.$feedConfigId);
  310. clearViewCache('/feed/ajax/'.$feedConfigId);
  311. clearViewCache('/feed/cachetime/'.$feedConfigId);
  312. } else {
  313. clearViewCache('/feed/index');
  314. clearViewCache('/feed/ajax');
  315. clearViewCache('/feed/cachetime');
  316. }
  317. if($url) {
  318. if(strpos($url,'http')===false) {
  319. // 実際のキャッシュではSSLを利用しているかどうかわからないので、両方削除する
  320. clearCache($this->RssEx->__createCacheHash('', 'http://'.$_SERVER['HTTP_HOST'].$this->base.$url), 'views', '.rss');
  321. clearCache($this->RssEx->__createCacheHash('', 'https://'.$_SERVER['HTTP_HOST'].$this->base.$url), 'views', '.rss');
  322. }else {
  323. clearCache($this->RssEx->__createCacheHash('', $url),'views','.rss');
  324. }
  325. } else {
  326. clearViewCache(null,'rss');
  327. }
  328. }
  329. }
  330. ?>