PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/plugins/blog/controllers/blog_comments_controller.php

https://github.com/hashing/basercms
PHP | 454 lines | 213 code | 50 blank | 191 comment | 39 complexity | f608fd81b874ea81c59c1d99d3f6b5e2 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.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.blog.controllers
  27. */
  28. class BlogCommentsController extends BlogAppController {
  29. /**
  30. * クラス名
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $name = 'BlogComments';
  36. /**
  37. * モデル
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $uses = array('Blog.BlogCategory', 'Blog.BlogComment', 'Blog.BlogPost');
  43. /**
  44. * ヘルパー
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $helpers = array();
  50. /**
  51. * コンポーネント
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $components = array('BcAuth','Cookie','BcAuthConfigure','RequestHandler','BcEmail','Security','BcCaptcha');
  57. /**
  58. * ぱんくずナビ
  59. *
  60. * @var string
  61. * @access public
  62. */
  63. var $crumbs = array(
  64. array('name' => 'プラグイン管理', 'url' => array('plugin' => '', 'controller' => 'plugins', 'action' => 'index')),
  65. array('name' => 'ブログ管理', 'url' => array('controller' => 'blog_contents', 'action' => 'index'))
  66. );
  67. /**
  68. * サブメニューエレメント
  69. *
  70. * @var array
  71. * @access public
  72. */
  73. var $subMenuElements = array();
  74. /**
  75. * beforeFilter
  76. *
  77. * @return void
  78. * @access public
  79. */
  80. function beforeFilter() {
  81. parent::beforeFilter();
  82. $this->BcAuth->allow('add','captcha', 'smartphone_add', 'smartphone_captcha');
  83. $crumbs = array();
  84. if(!empty($this->params['pass'][1])) {
  85. $dbDatas = $this->BlogPost->read(null,$this->params['pass'][1]);
  86. if(!$dbDatas) {
  87. $this->notFound();
  88. }
  89. $this->blogPost['BlogPost'] = $dbDatas['BlogPost'];
  90. $this->blogContent['BlogContent'] = $dbDatas['BlogContent'];
  91. $crumbs[] = array('name' => $this->blogContent['BlogContent']['title'].'管理', 'url' => array('controller' => 'blog_posts', 'action' => 'index', $this->blogContent['BlogContent']['id']));
  92. $crumbs[] = array('name' => $this->blogPost['BlogPost']['name'], 'url' => array('controller' => 'blog_posts', 'action' => 'edit', $this->blogContent['BlogContent']['id'], $this->blogPost['BlogPost']['id']));
  93. }elseif(!empty($this->params['pass'][0])) {
  94. $dbDatas = $this->BlogPost->BlogContent->read(null,$this->params['pass'][0]);
  95. $this->blogContent['BlogContent'] = $dbDatas['BlogContent'];
  96. $crumbs[] = array('name' => $this->blogContent['BlogContent']['title'].'管理', 'url' => array('controller' => 'blog_posts', 'action' => 'index', $this->blogContent['BlogContent']['id']));
  97. }
  98. $this->crumbs = am($this->crumbs,$crumbs);
  99. if(!empty($this->params['prefix']) && $this->params['prefix']=='admin') {
  100. $this->subMenuElements = array('blog_posts','blog_categories','blog_common');
  101. }
  102. if(empty($this->params['admin'])) {
  103. $this->Security->enabled = true;
  104. $this->Security->requireAuth('add');
  105. }
  106. }
  107. /**
  108. * beforeRender
  109. *
  110. * @return void
  111. * @access public
  112. */
  113. function beforeRender() {
  114. parent::beforeRender();
  115. $this->set('blogContent',$this->blogContent);
  116. }
  117. /**
  118. * [ADMIN] ブログを一覧表示する
  119. *
  120. * @return void
  121. * @access public
  122. */
  123. function admin_index($blogContentId,$blogPostId=null) {
  124. if(!$blogContentId || empty($this->blogContent['BlogContent'])) {
  125. $this->Session->setFlash('無効な処理です。');
  126. $this->redirect(array('controller' => 'blog_contents', 'action' => 'index'));
  127. }
  128. /* 検索条件 */
  129. if($blogPostId) {
  130. $conditions['BlogComment.blog_post_id'] = $blogPostId;
  131. $this->pageTitle = '記事 ['.$this->blogPost['BlogPost']['name'].'] のコメント一覧';
  132. }else {
  133. $conditions['BlogComment.blog_content_id'] = $blogContentId;
  134. $this->pageTitle = 'ブログ ['.$this->blogContent['BlogContent']['title'].'] のコメント一覧';
  135. }
  136. /* 画面情報設定 */
  137. $default = array('named' => array('num' => $this->siteConfigs['admin_list_num']));
  138. $this->setViewConditions('BlogPost', array('group' => $blogContentId, 'default' => $default));
  139. // データを取得
  140. $this->paginate = array('conditions'=>$conditions,
  141. 'fields'=> array(),
  142. 'order' => 'BlogComment.id',
  143. 'limit' => $this->passedArgs['num']
  144. );
  145. $dbDatas = $this->paginate('BlogComment');
  146. $this->set('dbDatas',$dbDatas);
  147. $this->help = 'blog_comments_index';
  148. }
  149. /**
  150. * [ADMIN] 一括削除
  151. *
  152. * @param int $blogContentId
  153. * @param int $blogPostId
  154. * @param int $id
  155. * @return void
  156. * @access public
  157. */
  158. function _batch_del($ids) {
  159. if($ids) {
  160. foreach($ids as $id) {
  161. $this->_del($id);
  162. }
  163. }
  164. return true;
  165. }
  166. /**
  167. * [ADMIN] 削除処理 (ajax)
  168. *
  169. * @param int $blogContentId
  170. * @param int $blogPostId
  171. * @param int $id
  172. * @return void
  173. * @access public
  174. */
  175. function admin_ajax_delete($blogContentId, $blogPostId, $id = null) {
  176. /* 除外処理 */
  177. if(!$id) {
  178. $this->ajaxError(500, '無効な処理です。');
  179. }
  180. if($this->_del($id)){
  181. exit(true);
  182. }else{
  183. exit();
  184. }
  185. }
  186. /**
  187. * 削除処理
  188. *
  189. * @param int $blogContentId
  190. * @param int $blogPostId
  191. * @param int $id
  192. * @return void
  193. * @access public
  194. */
  195. function _del($id = null) {
  196. /* 削除処理 */
  197. if($this->BlogComment->del($id)) {
  198. if(isset($this->blogPost['BlogPost']['name'])) {
  199. $message = '記事「'.$this->blogPost['BlogPost']['name'].'」へのコメントを削除しました。';
  200. }else {
  201. $message = '記事「'.$this->blogContent['BlogContent']['title'].'」へのコメントを削除しました。';
  202. }
  203. $this->BlogComment->saveDbLog($message);
  204. return true;
  205. }else {
  206. return false;
  207. }
  208. }
  209. /**
  210. * [ADMIN] 削除処理
  211. *
  212. * @param int $blogContentId
  213. * @param int $blogPostId
  214. * @param int $id
  215. * @return void
  216. * @access public
  217. */
  218. function admin_delete($blogContentId,$blogPostId,$id = null) {
  219. /* 除外処理 */
  220. if(!$blogContentId || !$id) {
  221. $this->notFound();
  222. }
  223. /* 削除処理 */
  224. if($this->BlogComment->del($id)) {
  225. if(isset($this->blogPost['BlogPost']['name'])) {
  226. $message = '記事「'.$this->blogPost['BlogPost']['name'].'」へのコメントを削除しました。';
  227. }else {
  228. $message = '記事「'.$this->blogContent['BlogContent']['title'].'」へのコメントを削除しました。';
  229. }
  230. $this->Session->setFlash($message);
  231. $this->BlogComment->saveDbLog($message);
  232. }else {
  233. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  234. }
  235. if($blogPostId) {
  236. $this->redirect(array('action' => 'index', $blogContentId, $blogPostId));
  237. }else {
  238. $this->redirect(array('action' => 'index', $blogContentId));
  239. }
  240. }
  241. /**
  242. * [ADMIN] 無効状態にする(AJAX)
  243. *
  244. * @param string $blogContentId
  245. * @param string $blogPostId beforeFilterで利用
  246. * @param string $blogCommentId
  247. * @return void
  248. * @access public
  249. */
  250. function admin_ajax_unpublish($blogContentId, $blogPostId, $id) {
  251. if(!$id) {
  252. $this->ajaxError(500, '無効な処理です。');
  253. }
  254. if($this->_changeStatus($id, false)) {
  255. clearViewCache();
  256. exit(true);
  257. } else {
  258. $this->ajaxError(500, $this->BlogComment->validationErrors);
  259. }
  260. exit();
  261. }
  262. /**
  263. * [ADMIN] 有効状態にする(AJAX)
  264. *
  265. * @param string $blogContentId
  266. * @param string $blogPostId beforeFilterで利用
  267. * @param string $blogCommentId
  268. * @return void
  269. * @access public
  270. */
  271. function admin_ajax_publish($blogContentId, $blogPostId, $id) {
  272. if(!$id) {
  273. $this->ajaxError(500, '無効な処理です。');
  274. }
  275. if($this->_changeStatus($id, true)) {
  276. clearViewCache();
  277. exit(true);
  278. } else {
  279. $this->ajaxError(500, $this->BlogComment->validationErrors);
  280. }
  281. exit();
  282. }
  283. /**
  284. * 一括公開
  285. *
  286. * @param array $ids
  287. * @return boolean
  288. * @access protected
  289. */
  290. function _batch_publish($ids) {
  291. if($ids) {
  292. foreach($ids as $id) {
  293. $this->_changeStatus($id, true);
  294. }
  295. }
  296. clearViewCache();
  297. return true;
  298. }
  299. /**
  300. * 一括非公開
  301. *
  302. * @param array $ids
  303. * @return boolean
  304. * @access protected
  305. */
  306. function _batch_unpublish($ids) {
  307. if($ids) {
  308. foreach($ids as $id) {
  309. $this->_changeStatus($id, false);
  310. }
  311. }
  312. clearViewCache();
  313. return true;
  314. }
  315. /**
  316. * ステータスを変更する
  317. *
  318. * @param int $id
  319. * @param boolean $status
  320. * @return boolean
  321. */
  322. function _changeStatus($id, $status) {
  323. $statusTexts = array(0 => '公開状態', 1 => '非公開状態');
  324. $data = $this->BlogComment->find('first', array('conditions' => array('BlogComment.id' => $id), 'recursive' => -1));
  325. $data['BlogComment']['status'] = $status;
  326. $this->BlogComment->set($data);
  327. if($this->BlogComment->save()) {
  328. $statusText = $statusTexts[$status];
  329. if(isset($this->blogPost['BlogPost']['name'])) {
  330. $message = '記事「'.$this->blogPost['BlogPost']['name'].'」へのコメントを'.$statusText.'に設定しました。';
  331. }else {
  332. $message = '記事「'.$this->blogContent['BlogContent']['title'].'」へのコメントを'.$statusText.'に設定しました。';
  333. }
  334. $this->BlogComment->saveDbLog($message);
  335. return true;
  336. } else {
  337. return false;
  338. }
  339. }
  340. /**
  341. * [AJAX] ブログコメントを登録する
  342. *
  343. * @param string $blogContentId
  344. * @param string $blogPostId
  345. * @return boolean
  346. * @access public
  347. */
  348. function add($blogContentId,$blogPostId) {
  349. if(!$this->data || !$blogContentId || !$blogPostId || empty($this->blogContent) || !$this->blogContent['BlogContent']['comment_use']) {
  350. $this->notFound();
  351. }else {
  352. // 画像認証を行う
  353. $captchaResult = true;
  354. if($this->blogContent['BlogContent']['auth_captcha']){
  355. $captchaResult = $this->BcCaptcha->check($this->data['BlogComment']['auth_captcha']);
  356. if(!$captchaResult){
  357. $this->set('dbData',false);
  358. return false;
  359. } else {
  360. unset($this->data['BlogComment']['auth_captcha']);
  361. }
  362. }
  363. $result = $this->BlogComment->add($this->data,$blogContentId,$blogPostId,$this->blogContent['BlogContent']['comment_approve']);
  364. if($result && $captchaResult) {
  365. $this->_sendComment();
  366. $this->set('dbData',$result['BlogComment']);
  367. }else{
  368. $this->set('dbData',false);
  369. }
  370. }
  371. }
  372. /**
  373. * [AJAX] ブログコメントを登録する
  374. *
  375. * @param string $blogContentId
  376. * @param string $blogPostId
  377. * @return boolean
  378. * @access public
  379. */
  380. function smartphone_add($blogContentId,$blogPostId) {
  381. $this->setAction('add', $blogContentId, $blogPostId);
  382. }
  383. /**
  384. * 認証用のキャプチャ画像を表示する
  385. *
  386. * @return void
  387. * @access public
  388. */
  389. function captcha()
  390. {
  391. $this->BcCaptcha->render();
  392. }
  393. /**
  394. * [SMARTPHONE] 認証用のキャプチャ画像を表示する
  395. *
  396. * @return void
  397. * @access public
  398. */
  399. function smartphone_captcha()
  400. {
  401. $this->BcCaptcha->render();
  402. }
  403. }
  404. ?>