PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/controllers/pages_controller.php

https://github.com/hashing/basercms
PHP | 1156 lines | 703 code | 156 blank | 297 comment | 158 complexity | 23379f8eee34b7c82a89ad7717fc58c2 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.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. * 固定ページコントローラー
  22. *
  23. * @package cake
  24. * @subpackage cake.baser.controllers
  25. */
  26. class PagesController extends AppController {
  27. /**
  28. * コントローラー名
  29. *
  30. * @var string
  31. * @access public
  32. */
  33. var $name = 'Pages';
  34. /**
  35. * ヘルパー
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. var $helpers = array(
  41. 'Html', BC_GOOGLEMAPS_HELPER, BC_XML_HELPER, BC_TEXT_HELPER,
  42. BC_FREEZE_HELPER, BC_CKEDITOR_HELPER, BC_PAGE_HELPER
  43. );
  44. /**
  45. * コンポーネント
  46. *
  47. * @var array
  48. * @access public
  49. */
  50. var $components = array('BcAuth','Cookie','BcAuthConfigure', 'BcEmail');
  51. /**
  52. * モデル
  53. *
  54. * @var array
  55. * @access public
  56. */
  57. var $uses = array('Page', 'PageCategory');
  58. /**
  59. * beforeFilter
  60. *
  61. * @return void
  62. * @access public
  63. */
  64. function beforeFilter() {
  65. parent::beforeFilter();
  66. // 認証設定
  67. $this->BcAuth->allow('display','mobile_display', 'smartphone_display');
  68. if(!empty($this->params['admin'])){
  69. $this->crumbs = array(array('name' => '固定ページ管理', 'url' => array('controller' => 'pages', 'action' => 'index')));
  70. }
  71. $user = $this->BcAuth->user();
  72. $userModel = $this->getUserModel();
  73. $newCatAddable = $this->PageCategory->checkNewCategoryAddable(
  74. $user[$userModel]['user_group_id'],
  75. $this->checkRootEditable()
  76. );
  77. $this->set('newCatAddable', $newCatAddable);
  78. }
  79. /**
  80. * [ADMIN] ページリスト
  81. *
  82. * @return void
  83. * @access public
  84. */
  85. function admin_index() {
  86. /* 画面情報設定 */
  87. $default = array('named' => array('num' => $this->siteConfigs['admin_list_num'], 'sortmode' => 0),
  88. 'Page' => array('page_category_id' => '', 'page_type' => 1));
  89. $this->setViewConditions('Page', array('default' => $default));
  90. // 並び替えモードの場合は、強制的にsortフィールドで並び替える
  91. if($this->passedArgs['sortmode']) {
  92. $this->passedArgs['sort'] = 'sort';
  93. $this->passedArgs['direction'] = 'asc';
  94. }
  95. // 検索条件
  96. $conditions = $this->_createAdminIndexConditions($this->data);
  97. $this->paginate = array(
  98. 'conditions' => $conditions,
  99. 'fields' => array(),
  100. 'order' =>'Page.sort',
  101. 'limit' => $this->passedArgs['num']
  102. );
  103. $datas = $this->paginate('Page');
  104. $this->set('datas',$datas);
  105. $this->_setAdminIndexViewData();
  106. if($this->RequestHandler->isAjax() || !empty($this->params['url']['ajax'])) {
  107. Configure::write('debug', 0);
  108. $this->render('ajax_index');
  109. return;
  110. }
  111. /* 表示設定 */
  112. if(!isset($this->data['Page']['page_type'])) {
  113. $this->data['Page']['page_type'] = 1;
  114. }
  115. $pageCategories = array('' => '指定しない', 'noncat' => 'カテゴリなし');
  116. $_pageCategories = $this->getCategorySource($this->data['Page']['page_type']);
  117. if($_pageCategories) {
  118. $pageCategories += $_pageCategories;
  119. }
  120. $this->set('search', 'pages_index');
  121. $this->set('pageCategories', $pageCategories);
  122. $this->subMenuElements = array('pages','page_categories');
  123. $this->pageTitle = '固定ページ一覧';
  124. $this->search = 'pages_index';
  125. $this->help = 'pages_index';
  126. }
  127. /**
  128. * [ADMIN] 固定ページ情報登録
  129. *
  130. * @return void
  131. * @access public
  132. */
  133. function admin_add() {
  134. if(empty($this->data)) {
  135. $this->data = $this->Page->getDefaultValue();
  136. $this->data['Page']['page_type'] = 1;
  137. }else {
  138. /* 登録処理 */
  139. $this->data['Page']['url'] = $this->Page->getPageUrl($this->data);
  140. $this->Page->create($this->data);
  141. if($this->data['Page']['page_type'] == 2 && !$this->data['Page']['page_category_id']) {
  142. $this->data['Page']['page_category_id'] = $this->PageCategory->getAgentId('mobile');
  143. } elseif($this->data['Page']['page_type'] == 3 && !$this->data['Page']['page_category_id']) {
  144. $this->data['Page']['page_category_id'] = $this->PageCategory->getAgentId('smartphone');
  145. }
  146. if($this->Page->validates()) {
  147. if($this->Page->save($this->data,false)) {
  148. // キャッシュを削除する
  149. if($this->Page->allowedPublish($this->data['Page']['status'], $this->data['Page']['publish_begin'], $this->data['Page']['publish_end'])) {
  150. clearViewCache();
  151. }
  152. // 完了メッセージ
  153. $message = '固定ページ「'.$this->data['Page']['name'].'」を追加しました。';
  154. $this->Session->setFlash($message);
  155. $this->Page->saveDbLog($message);
  156. // afterPageAdd
  157. $this->executeHook('afterPageAdd');
  158. // 編集画面にリダイレクト
  159. $id = $this->Page->getInsertID();
  160. $this->redirect(array('controller' => 'pages', 'action' => 'edit', $id));
  161. }else {
  162. $this->Session->setFlash('保存中にエラーが発生しました。');
  163. }
  164. }else {
  165. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  166. }
  167. }
  168. /* 表示設定 */
  169. $categories = $this->getCategorySource($this->data['Page']['page_type'], array('empty' => '指定しない', 'own' => true));
  170. $this->set('categories', $categories);
  171. $this->set('editable', true);
  172. $this->set('previewId', 'add_'.mt_rand(0, 99999999));
  173. $this->set('reflectMobile', Configure::read('BcApp.mobile'));
  174. $this->set('reflectSmartphone', Configure::read('BcApp.smartphone'));
  175. $this->set('users', $this->Page->getControlSource('user_id'));
  176. $this->set('ckEditorOptions1', array('useDraft' => true, 'draftField' => 'draft', 'disableDraft' => true, 'width' => 'auto'));
  177. $this->subMenuElements = array('pages','page_categories');
  178. $this->set('rootMobileId', $this->PageCategory->getAgentId('mobile'));
  179. $this->set('rootSmartphoneId', $this->PageCategory->getAgentId('smartphone'));
  180. $this->pageTitle = '新規固定ページ登録';
  181. $this->help = 'pages_form';
  182. $this->render('form');
  183. }
  184. /**
  185. * [ADMIN] 固定ページ情報編集
  186. *
  187. * @param int $id (page_id)
  188. * @return void
  189. * @access public
  190. */
  191. function admin_edit($id) {
  192. /* 除外処理 */
  193. if(!$id && empty($this->data)) {
  194. $this->Session->setFlash('無効なIDです。');
  195. $this->redirect(array('action' => 'index'));
  196. }
  197. if(empty($this->data)) {
  198. $this->data = $this->Page->read(null, $id);
  199. $this->data['Page']['contents_tmp'] = $this->data['Page']['contents'];
  200. $mobileIds = $this->PageCategory->getAgentCategoryIds('mobile');
  201. $smartphoneIds = $this->PageCategory->getAgentCategoryIds('smartphone');
  202. if(in_array($this->data['Page']['page_category_id'], $mobileIds)) {
  203. $this->data['Page']['page_type'] = 2;
  204. } elseif(in_array($this->data['Page']['page_category_id'], $smartphoneIds)) {
  205. $this->data['Page']['page_type'] = 3;
  206. } else {
  207. $this->data['Page']['page_type'] = 1;
  208. }
  209. }else {
  210. $before = $this->Page->read(null, $id);
  211. if(empty($this->data['Page']['page_type'])) {
  212. $this->data['Page']['page_type'] = 1;
  213. }
  214. /* 更新処理 */
  215. if($this->data['Page']['page_type'] == 2 && !$this->data['Page']['page_category_id']) {
  216. $this->data['Page']['page_category_id'] = $this->PageCategory->getAgentId('mobile');
  217. } elseif($this->data['Page']['page_type'] == 3 && !$this->data['Page']['page_category_id']) {
  218. $this->data['Page']['page_category_id'] = $this->PageCategory->getAgentId('smartphone');
  219. }
  220. $this->data['Page']['url'] = $this->Page->getPageUrl($this->data);
  221. $this->Page->set($this->data);
  222. if($this->Page->validates()) {
  223. if($this->Page->save($this->data,false)) {
  224. // タイトル、URL、公開状態が更新された場合、全てビューキャッシュを削除する
  225. $beforeStatus = $this->Page->allowedPublish($before['Page']['status'], $before['Page']['publish_begin'], $before['Page']['publish_end']);
  226. $afterStatus = $this->Page->allowedPublish($this->data['Page']['status'], $this->data['Page']['publish_begin'], $this->data['Page']['publish_end']);
  227. if($beforeStatus != $afterStatus || $before['Page']['title'] != $this->data['Page']['title'] || $before['Page']['url'] != $this->data['Page']['url']) {
  228. clearViewCache();
  229. } else {
  230. clearViewCache($this->data['Page']['url']);
  231. }
  232. // 完了メッセージ
  233. $message = '固定ページ「'.$this->data['Page']['name'].'」を更新しました。';
  234. $this->Session->setFlash($message);
  235. $this->Page->saveDbLog($message);
  236. // afterPageEdit
  237. $this->executeHook('afterPageEdit');
  238. // 同固定ページへリダイレクト
  239. $this->redirect(array('action' => 'edit', $id));
  240. }else {
  241. $this->Session->setFlash('保存中にエラーが発生しました。');
  242. }
  243. }else {
  244. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  245. }
  246. }
  247. /* 表示設定 */
  248. $currentOwnerId = '';
  249. $currentPageCategoryId = '';
  250. if(!empty($this->data['PageCategory']['id'])) {
  251. $currentPageCategoryId = $this->data['PageCategory']['id'];
  252. }
  253. $categories = $this->getCategorySource($this->data['Page']['page_type'], array(
  254. 'currentOwnerId' => $currentOwnerId,
  255. 'currentPageCategoryId' => $currentPageCategoryId,
  256. 'own' => true,
  257. 'empty' => '指定しない'
  258. ));
  259. $url = $this->convertViewUrl($this->data['Page']['url']);
  260. if($this->data['Page']['url']) {
  261. $this->set('publishLink', $url);
  262. }
  263. $this->set('categories', $categories);
  264. $this->set('editable', $this->checkCurrentEditable($currentPageCategoryId, $currentOwnerId));
  265. $this->set('previewId', $this->data['Page']['id']);
  266. $this->set('reflectMobile', Configure::read('BcApp.mobile'));
  267. $this->set('reflectSmartphone', Configure::read('BcApp.smartphone'));
  268. $this->set('users', $this->Page->getControlSource('user_id'));
  269. $this->set('ckEditorOptions1', array('useDraft' => true, 'draftField' => 'draft', 'disableDraft' => false, 'width' => 'auto'));
  270. $this->set('url', $url);
  271. $this->set('mobileExists',$this->Page->agentExists('mobile', $this->data));
  272. $this->set('smartphoneExists',$this->Page->agentExists('smartphone', $this->data));
  273. $this->set('rootMobileId', $this->PageCategory->getAgentId('mobile'));
  274. $this->set('rootSmartphoneId', $this->PageCategory->getAgentId('smartphone'));
  275. $this->subMenuElements = array('pages','page_categories');
  276. if(!empty($this->data['Page']['title'])) {
  277. $this->pageTitle = '固定ページ情報編集:'.$this->data['Page']['title'];
  278. } else {
  279. $this->pageTitle = '固定ページ情報編集:'.Inflector::Classify($this->data['Page']['name']);
  280. }
  281. $this->help = 'pages_form';
  282. $this->render('form');
  283. }
  284. /**
  285. * DBに保存されているURLをビュー用のURLに変換する
  286. *
  287. * @param string $url
  288. * @return string
  289. */
  290. function convertViewUrl($url) {
  291. $url = preg_replace('/\/index$/', '/', $url);
  292. if(preg_match('/^\/'.Configure::read('BcAgent.mobile.prefix').'\//is', $url)) {
  293. $url = preg_replace('/^\/'.Configure::read('BcAgent.mobile.prefix').'\//is', '/'.Configure::read('BcAgent.mobile.alias').'/', $url);
  294. } elseif(preg_match('/^\/'.Configure::read('BcAgent.smartphone.prefix').'\//is', $url)) {
  295. $url = preg_replace('/^\/'.Configure::read('BcAgent.smartphone.prefix').'\//is', '/'.Configure::read('BcAgent.smartphone.alias').'/', $url);
  296. }
  297. return $url;
  298. }
  299. /**
  300. * [ADMIN] 固定ページ情報削除
  301. *
  302. * @param int $id (page_id)
  303. * @return void
  304. * @access public
  305. * @deprecated admin_ajax_delete で Ajax化
  306. */
  307. function admin_delete($id = null) {
  308. /* 除外処理 */
  309. if(!$id) {
  310. $this->Session->setFlash('無効なIDです。');
  311. $this->redirect(array('action' => 'index'));
  312. }
  313. // メッセージ用にデータを取得
  314. $page = $this->Page->read(null, $id);
  315. /* 削除処理 */
  316. if($this->Page->del($id)) {
  317. // 完了メッセージ
  318. $message = '固定ページ: '.$page['Page']['name'].' を削除しました。';
  319. $this->Session->setFlash($message);
  320. $this->Page->saveDbLog($message);
  321. }else {
  322. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  323. }
  324. $this->redirect(array('action' => 'index'));
  325. }
  326. /**
  327. * [ADMIN] 固定ページファイルを登録する
  328. *
  329. * @return void
  330. * @access public
  331. */
  332. function admin_entry_page_files() {
  333. // 現在のテーマの固定ページファイルのパスを取得
  334. $pagesPath = getViewPath().'pages';
  335. $result = $this->Page->entryPageFiles($pagesPath);
  336. clearViewCache();
  337. $message = $result['all'].' ページ中 '.$result['insert'].' ページの新規登録、 '. $result['update'].' ページの更新に成功しました。';
  338. $this->Session->setFlash($message);
  339. $this->redirect(array('action' => 'index'));
  340. }
  341. /**
  342. * [ADMIN] 固定ページファイルを登録する
  343. *
  344. * @return void
  345. * @access public
  346. */
  347. function admin_write_page_files() {
  348. if($this->Page->createAllPageTemplate()){
  349. $this->Session->setFlash('固定ページテンプレートの書き出しに成功しました。');
  350. } else {
  351. $this->Session->setFlash('固定ページテンプレートの書き出しに失敗しました。<br />表示できないページは固定ページ管理より更新処理を行ってください。');
  352. }
  353. clearViewCache();
  354. $this->redirect(array('action' => 'index'));
  355. }
  356. /**
  357. * ビューを表示する
  358. *
  359. * @param mixed
  360. * @return void
  361. * @access public
  362. */
  363. function display() {
  364. $path = func_get_args();
  365. $ext = '';
  366. if(preg_match('/^pages/', $path[0])) {
  367. // .htmlの拡張子がついている場合、$pathが正常に取得できないので取得しなおす
  368. // 1.5.9 以前との互換性の為残しておく
  369. $url = str_replace('pages','',$path[0]);
  370. if($url == 'index.html') {
  371. $url = '/index.html';
  372. }
  373. $params = Router::parse(str_replace('.html','',$path[0]));
  374. $path = $params['pass'];
  375. $this->params['pass'] = $path;
  376. $ext = '.html';
  377. } else {
  378. $url = '/'.implode('/', $path);
  379. }
  380. // モバイルディレクトリへのアクセスは Not Found
  381. if(isset($path[0]) && ($path[0]==Configure::read('BcAgent.mobile.prefix') || $path[0]==Configure::read('BcAgent.smartphone.prefix'))){
  382. $this->notFound();
  383. }
  384. $count = count($path);
  385. if (!$count) {
  386. $this->redirect('/');
  387. }
  388. $page = $subpage = $title = null;
  389. if (!empty($path[0])) {
  390. $page = $path[0];
  391. }
  392. if (!empty($path[1])) {
  393. $subpage = $path[1];
  394. }
  395. if (!empty($path[$count - 1])) {
  396. $title = Inflector::humanize($path[$count - 1]);
  397. }
  398. // 公開制限を確認
  399. // 1.5.10 で、拡張子なしを標準に変更
  400. // 拡張子なしの場合は、route.phpで認証がかかる為、ここでは処理を行わない
  401. // 1.5.9 以前との互換性の為残しておく
  402. if(($ext)) {
  403. if(!$this->Page->checkPublish($url)) {
  404. return $this->notFound();
  405. }
  406. }
  407. // キャッシュ設定
  408. if(!isset($_SESSION['Auth']['User'])){
  409. $this->helpers[] = 'Cache';
  410. $this->cacheAction = $this->Page->getCacheTime($url);
  411. }
  412. // ナビゲーションを取得
  413. $this->crumbs = $this->_getCrumbs($url);
  414. $path[count($path)-1] .= $ext;
  415. $this->subMenuElements = array('default');
  416. $this->set(compact('page', 'subpage', 'title'));
  417. $this->render(join('/', $path));
  418. }
  419. /**
  420. * パンくずナビ用の配列を取得する
  421. *
  422. * @param string $url
  423. * @return array
  424. * @access protected
  425. */
  426. function _getCrumbs($url) {
  427. if(Configure::read('BcRequest.agent')) {
  428. $url = '/'.Configure::read('BcRequest.agentAlias').$url;
  429. }
  430. // 直属のカテゴリIDを取得
  431. $pageCategoryId = $this->Page->field('page_category_id', array('Page.url' => $url));
  432. // 関連カテゴリを取得(関連固定ページも同時に取得)
  433. $pageCategorires = array();
  434. if($pageCategoryId) {
  435. $pageCategorires = $this->Page->PageCategory->getPath($pageCategoryId, array('PageCategory.name', 'PageCategory.title'), 1);
  436. }
  437. $crumbs = array();
  438. if($pageCategorires) {
  439. // index 固定ページの有無によりリンクを判別
  440. foreach($pageCategorires as $pageCategory) {
  441. if(!empty($pageCategory['Page'])) {
  442. $categoryUrl = '';
  443. foreach($pageCategory['Page'] as $page) {
  444. if($page['name'] == 'index') {
  445. $categoryUrl = $page['url'];
  446. break;
  447. }
  448. }
  449. if($categoryUrl) {
  450. $crumbs[] = array('name' => $pageCategory['PageCategory']['title'], 'url' => $categoryUrl);
  451. } else {
  452. $crumbs[] = array('name' => $pageCategory['PageCategory']['title'], 'url' => '');
  453. }
  454. }
  455. }
  456. }
  457. return $crumbs;
  458. }
  459. /**
  460. * [MOBILE] ビューを表示する
  461. *
  462. * @param mixed
  463. * @return void
  464. * @access public
  465. */
  466. function mobile_display() {
  467. $path = func_get_args();
  468. call_user_func_array( array( &$this, 'display' ), $path );
  469. }
  470. /**
  471. * [SMARTPHONE] ビューを表示する
  472. *
  473. * @param mixed
  474. * @return void
  475. * @access public
  476. */
  477. function smartphone_display() {
  478. $path = func_get_args();
  479. call_user_func_array( array( &$this, 'display' ), $path );
  480. }
  481. /**
  482. * [ADMIN] 固定ページをプレビュー
  483. *
  484. * @param mixed $id (blog_post_id)
  485. * @return void
  486. * @access public
  487. */
  488. function admin_create_preview($id) {
  489. if(isset($this->data['Page'])) {
  490. $page = $this->data;
  491. if(empty($page['Page']['page_category_id']) && $page['Page']['page_type'] == 2) {
  492. $page['Page']['page_category_id'] = $this->Page->PageCategory->getAgentId('mobile');
  493. }elseif(empty($page['Page']['page_category_id']) && $page['Page']['page_type'] == 3) {
  494. $page['Page']['page_category_id'] = $this->Page->PageCategory->getAgentId('smartphone');
  495. }
  496. $page['Page']['url'] = $this->Page->getPageUrl($page);
  497. } else {
  498. $conditions = array('Page.id' => $id);
  499. $page = $this->Page->find($conditions);
  500. }
  501. if(!$page) {
  502. echo false;
  503. exit();
  504. }
  505. Cache::write('page_preview_'.$id, $page);
  506. $settings = Configure::read('BcAgent');
  507. foreach($settings as $key => $setting) {
  508. if(preg_match('/^\/'.$setting['prefix'].'\//is', $page['Page']['url'])){
  509. Configure::write('BcRequest.agent', $key);
  510. Configure::write('BcRequest.agentPrefix', $setting['prefix']);
  511. Configure::write('BcRequest.agentAlias', $setting['alias']);
  512. break;
  513. }
  514. }
  515. // 一時ファイルとしてビューを保存
  516. // タグ中にPHPタグが入る為、ファイルに保存する必要がある
  517. $contents = $this->Page->addBaserPageTag(null, $page['Page']['contents'], $page['Page']['title'],$page['Page']['description']);
  518. $path = TMP.'pages_preview_'.$id.$this->ext;
  519. $file = new File($path);
  520. $file->open('w');
  521. $file->append($contents);
  522. $file->close();
  523. unset($file);
  524. @chmod($path, 0666);
  525. echo true;
  526. exit();
  527. }
  528. /**
  529. * プレビューを表示する
  530. *
  531. * @return void
  532. * @access public
  533. */
  534. function admin_preview($id){
  535. $page = Cache::read('page_preview_'.$id);
  536. $settings = Configure::read('BcAgent');
  537. foreach($settings as $key => $setting) {
  538. if(preg_match('/^\/'.$setting['prefix'].'\//is', $page['Page']['url'])){
  539. Configure::write('BcRequest.agent', $key);
  540. Configure::write('BcRequest.agentPrefix', $setting['prefix']);
  541. Configure::write('BcRequest.agentAlias', $setting['alias']);
  542. break;
  543. }
  544. }
  545. if(Configure::read('BcRequest.agent')){
  546. $this->layoutPath = Configure::read('BcAgent.'.Configure::read('BcRequest.agent').'.prefix');
  547. if(Configure::read('BcRequest.agent') == 'mobile') {
  548. $this->helpers[] = BC_MOBILE_HELPER;
  549. }
  550. } else {
  551. $this->layoutPath = '';
  552. }
  553. $this->subDir = '';
  554. $this->params['prefix'] = '';
  555. $this->params['admin'] = '';
  556. $this->params['controller'] = 'pages';
  557. $this->params['action'] = 'display';
  558. $this->params['url']['url'] = preg_replace('/^\//i','',preg_replace('/^\/mobile\//is','/m/',$page['Page']['url']));
  559. $this->crumbs = $this->_getCrumbs($this->params['url']['url']);
  560. $this->theme = $this->siteConfigs['theme'];
  561. $this->render('display',null,TMP.'pages_preview_'.$id.$this->ext);
  562. @unlink(TMP.'pages_preview_'.$id.$this->ext);
  563. Cache::delete('page_preview_'.$id);
  564. }
  565. /**
  566. * 並び替えを更新する [AJAX]
  567. *
  568. * @access public
  569. * @return boolean
  570. */
  571. function admin_ajax_update_sort () {
  572. if($this->data){
  573. $this->setViewConditions('Page', array('action' => 'admin_index'));
  574. $conditions = $this->_createAdminIndexConditions($this->data);
  575. $this->Page->fileSave = false;
  576. $this->Page->contentSaving = false;
  577. if($this->Page->changeSort($this->data['Sort']['id'],$this->data['Sort']['offset'],$conditions)){
  578. clearViewCache();
  579. clearDataCache();
  580. echo true;
  581. }else{
  582. $this->ajaxError(500, '一度リロードしてから再実行してみてください。');
  583. }
  584. }else{
  585. $this->ajaxError(500, '無効な処理です。');
  586. }
  587. exit();
  588. }
  589. /**
  590. * 管理画面固定ページ一覧の検索条件を取得する
  591. *
  592. * @param array $data
  593. * @return string
  594. * @access protected
  595. */
  596. function _createAdminIndexConditions($data){
  597. /* 条件を生成 */
  598. $conditions = array();
  599. $pageCategoryId = '';
  600. // 固定ページカテゴリ
  601. if(isset($data['Page']['page_category_id'])) {
  602. $pageCategoryId = $data['Page']['page_category_id'];
  603. }
  604. $name = '';
  605. $pageType = 1;
  606. if(isset($data['Page']['name'])) {
  607. $name = $data['Page']['name'];
  608. }
  609. if(isset($data['Page']['page_type'])) {
  610. $pageType = $data['Page']['page_type'];
  611. }
  612. unset($data['_Token']);
  613. unset($data['Page']['name']);
  614. unset($data['Page']['page_category_id']);
  615. unset($data['Sort']);
  616. unset($data['Page']['open']);
  617. unset($data['Page']['page_type']);
  618. if($pageType == 1 && !$pageCategoryId) {
  619. $pageCategoryId = 'pconly';
  620. }
  621. if($pageType == 2 && !$pageCategoryId) {
  622. $pageCategoryId = $this->PageCategory->getAgentId('mobile');
  623. }
  624. if($pageType == 3 && !$pageCategoryId) {
  625. $pageCategoryId = $this->PageCategory->getAgentId('smartphone');
  626. }
  627. // 条件指定のないフィールドを解除
  628. foreach($data['Page'] as $key => $value) {
  629. if($value === '') {
  630. unset($data['Page'][$key]);
  631. }
  632. }
  633. if($data['Page']) {
  634. $conditions = $this->postConditions($data);
  635. }
  636. if(isset($data['Page'])){
  637. $data = $data['Page'];
  638. }
  639. // 固定ページカテゴリ
  640. if(!empty($pageCategoryId)) {
  641. if($pageCategoryId == 'pconly') {
  642. // PCのみ
  643. $agentCategoryIds = am($this->PageCategory->getAgentCategoryIds('mobile'), $this->PageCategory->getAgentCategoryIds('smartphone'));
  644. if($agentCategoryIds) {
  645. $conditions['or'] = array('not'=>array('Page.page_category_id' => $agentCategoryIds),
  646. array('Page.page_category_id'=>null));
  647. } else {
  648. $conditions['or'] = array(array('Page.page_category_id'=>null));
  649. }
  650. } elseif($pageCategoryId != 'noncat') {
  651. // カテゴリ指定
  652. // 子カテゴリも検索条件に入れる
  653. $pageCategoryIds = array($pageCategoryId);
  654. $children = $this->PageCategory->children($pageCategoryId);
  655. if($children) {
  656. foreach($children as $child) {
  657. $pageCategoryIds[] = $child['PageCategory']['id'];
  658. }
  659. }
  660. $conditions['Page.page_category_id'] = $pageCategoryIds;
  661. } elseif($pageCategoryId == 'noncat') {
  662. //カテゴリなし
  663. if($pageType == 1) {
  664. $conditions['or'] = array(array('Page.page_category_id' => ''),array('Page.page_category_id'=>NULL));
  665. } elseif($pageType == 2) {
  666. $conditions['Page.page_category_id'] = $this->PageCategory->getAgentId('mobile');
  667. } elseif($pageType == 3) {
  668. $conditions['Page.page_category_id'] = $this->PageCategory->getAgentId('smartphone');
  669. }
  670. }
  671. } else {
  672. if(!Configure::read('BcApp.mobile') || !Configure::read('BcApp.smartphone')) {
  673. $conditions['or'] = array(
  674. array('Page.page_category_id' => ''),
  675. array('Page.page_category_id' => NULL));
  676. }
  677. if(!Configure::read('BcApp.mobile')) {
  678. $conditions['or'][] = array('Page.page_category_id <>' => $this->PageCategory->getAgentId('mobile'));
  679. }
  680. if(!Configure::read('BcApp.smartphone')) {
  681. $conditions['or'][] = array('Page.page_category_id <>' => $this->PageCategory->getAgentId('smartphone'));
  682. }
  683. }
  684. if($name) {
  685. $conditions['and']['or'] = array(
  686. 'Page.name LIKE' => '%'.$name.'%',
  687. 'Page.title LIKE' => '%'.$name.'%'
  688. );
  689. }
  690. return $conditions;
  691. }
  692. /**
  693. * PC用のカテゴリIDを元にモバイルページが作成する権限があるかチェックする
  694. *
  695. * @param int $type
  696. * @param int $id
  697. * @return boolean
  698. * @access public
  699. */
  700. function admin_check_agent_page_addable($type, $id = null) {
  701. $user = $this->BcAuth->user();
  702. $userModel = $this->getUserModel();
  703. $userGroupId = $user[$userModel]['user_group_id'];
  704. $result = false;
  705. while(true) {
  706. $agentId = $this->PageCategory->getAgentRelativeId($type, $id);
  707. if($agentId) {
  708. if($agentId == 1 || $agentId == 2) {
  709. $ownerId = $this->siteConfigs['root_owner_id'];
  710. } else {
  711. $pageCategory = $this->PageCategory->find('first', array(
  712. 'conditions'=> array('PageCategory.id' => $agentId),
  713. 'field' => array('owner_id')
  714. ));
  715. $ownerId = $pageCategory['PageCategory']['owner_id'];
  716. }
  717. if($ownerId) {
  718. if($userGroupId == $ownerId) {
  719. $result = true;
  720. } else {
  721. $result = false;
  722. }
  723. } else {
  724. $result = true;
  725. }
  726. break;
  727. }
  728. $pageCategory = $this->PageCategory->find('first', array(
  729. 'conditions'=> array('PageCategory.id' => $id),
  730. 'field' => array('parent_id')
  731. ));
  732. $id = $pageCategory['PageCategory']['parent_id'];
  733. }
  734. if($result) {
  735. echo 1;
  736. }
  737. exit();
  738. }
  739. /**
  740. * [AJAX] カテゴリリスト用のデータを取得する
  741. *
  742. * @param int $type
  743. * @param boolean $empty
  744. * @return array
  745. * @access public
  746. */
  747. function admin_ajax_category_source($type) {
  748. $categorySource = $this->getCategorySource($type, $this->data['Option']);
  749. $this->set('categorySource', $categorySource);
  750. }
  751. /**
  752. * カテゴリリスト用のデータを取得する
  753. *
  754. * @param int $type
  755. * @param int $options
  756. * @param boolean $empty
  757. * @return array
  758. * @access public
  759. */
  760. function getCategorySource($type, $options = array()) {
  761. $editable = true;
  762. if(isset($options['currentPageCategoryId']) && isset($options['currentOwnerId'])) {
  763. $editable = $this->checkCurrentEditable($options['currentPageCategoryId'], $options['currentOwnerId']);
  764. }
  765. $mobileId = $this->Page->PageCategory->getAgentId('mobile');
  766. $smartphoneId = $this->Page->PageCategory->getAgentId('smartphone');
  767. switch($type) {
  768. case '1': // PC
  769. $parentId = '';
  770. $excludeParentId = array($mobileId, $smartphoneId);
  771. break;
  772. case '2': // モバイル
  773. $parentId = $mobileId;
  774. $excludeParentId = '';
  775. break;
  776. case '3': // スマホ
  777. $parentId = $smartphoneId;
  778. $excludeParentId = '';
  779. break;
  780. default:
  781. $parentId = '';
  782. $excludeParentId = '';
  783. }
  784. $_options = array(
  785. 'rootEditable' => $this->checkRootEditable(),
  786. 'pageEditable' => $editable,
  787. 'agentRoot' => false,
  788. 'parentId' => $parentId,
  789. 'excludeParentId' => $excludeParentId
  790. );
  791. $_options['currentPageCategoryId'] = 58;
  792. if(isset($options['currentPageCategoryId'])) {
  793. $_options['pageCategoryId'] = $options['currentPageCategoryId'];
  794. }
  795. if(!empty($options['excludeParentId'])) {
  796. if($_options['excludeParentId']) {
  797. $_options['excludeParentId'][] = $options['excludeParentId'];
  798. } else {
  799. $_options['excludeParentId'] = $options['excludeParentId'];
  800. }
  801. }
  802. if(isset($options['empty'])) {
  803. $_options['empty'] = $options['empty'];
  804. }
  805. if(!empty($options['own'])) {
  806. $user = $this->BcAuth->user();
  807. $userModel = $this->getUserModel();
  808. $_options['userGroupId'] = $user[$userModel]['user_group_id'];
  809. }
  810. return $this->Page->getControlSource('page_category_id', $_options);
  811. }
  812. /**
  813. * 現在のページが書込可能かチェックする
  814. *
  815. * @param int $pageCategoryId
  816. * @param int $ownerId
  817. * @return boolean
  818. * @access public
  819. */
  820. function checkCurrentEditable($pageCategoryId, $ownerId) {
  821. $user = $this->BcAuth->user();
  822. $userModel = $this->getUserModel();
  823. $editable = false;
  824. if(!$pageCategoryId) {
  825. $currentCatOwner = $this->siteConfigs['root_owner_id'];
  826. } else {
  827. $currentCatOwner = $ownerId;
  828. }
  829. return ($currentCatOwner == $user[$userModel]['user_group_id'] ||
  830. $user[$userModel]['user_group_id'] == 1 || !$currentCatOwner);
  831. }
  832. /**
  833. * 一括削除
  834. *
  835. * @param array $ids
  836. * @return boolean
  837. * @access protected
  838. */
  839. function _batch_del($ids) {
  840. if($ids) {
  841. foreach($ids as $id) {
  842. $data = $this->Page->read(null, $id);
  843. if($this->Page->del($id)) {
  844. $this->Page->saveDbLog('固定ページ: '.$data['Page']['name'].' を削除しました。');
  845. }
  846. }
  847. }
  848. return true;
  849. }
  850. /**
  851. * [ADMIN] 無効状態にする(AJAX)
  852. *
  853. * @param string $blogContentId
  854. * @param string $blogPostId beforeFilterで利用
  855. * @param string $blogCommentId
  856. * @return void
  857. * @access public
  858. */
  859. function admin_ajax_unpublish($id) {
  860. if(!$id) {
  861. $this->ajaxError(500, '無効な処理です。');
  862. }
  863. if($this->_changeStatus($id, false)) {
  864. exit(true);
  865. } else {
  866. $this->ajaxError(500, $this->Page->validationErrors);
  867. }
  868. exit();
  869. }
  870. /**
  871. * [ADMIN] 有効状態にする(AJAX)
  872. *
  873. * @param string $blogContentId
  874. * @param string $blogPostId beforeFilterで利用
  875. * @param string $blogCommentId
  876. * @return void
  877. * @access public
  878. */
  879. function admin_ajax_publish($id) {
  880. if(!$id) {
  881. $this->ajaxError(500, '無効な処理です。');
  882. }
  883. if($this->_changeStatus($id, true)) {
  884. exit(true);
  885. } else {
  886. $this->ajaxError(500, $this->Page->validationErrors);
  887. }
  888. exit();
  889. }
  890. /**
  891. * 一括公開
  892. *
  893. * @param array $ids
  894. * @return boolean
  895. * @access protected
  896. */
  897. function _batch_publish($ids) {
  898. if($ids) {
  899. foreach($ids as $id) {
  900. $this->_changeStatus($id, true);
  901. }
  902. }
  903. return true;
  904. }
  905. /**
  906. * 一括非公開
  907. *
  908. * @param array $ids
  909. * @return boolean
  910. * @access protected
  911. */
  912. function _batch_unpublish($ids) {
  913. if($ids) {
  914. foreach($ids as $id) {
  915. $this->_changeStatus($id, false);
  916. }
  917. }
  918. return true;
  919. }
  920. /**
  921. * ステータスを変更する
  922. *
  923. * @param int $id
  924. * @param boolean $status
  925. * @return boolean
  926. */
  927. function _changeStatus($id, $status) {
  928. $statusTexts = array(0 => '非公開', 1 => '公開');
  929. $data = $this->Page->find('first', array('conditions' => array('Page.id' => $id), 'recursive' => -1));
  930. $data['Page']['status'] = $status;
  931. if($status) {
  932. $data['Page']['publish_begin'] = '';
  933. $data['Page']['publish_end'] = '';
  934. }
  935. $this->Page->set($data);
  936. if($this->Page->save()) {
  937. clearViewCache($data['Page']['url']);
  938. $statusText = $statusTexts[$status];
  939. $this->Page->saveDbLog('固定ページ「'.$data['Page']['name'].'」 を'.$statusText.'にしました。');
  940. return true;
  941. } else {
  942. return false;
  943. }
  944. }
  945. /**
  946. * [ADMIN] 固定ページ情報削除
  947. *
  948. * @param int $id (page_id)
  949. * @return void
  950. * @access public
  951. */
  952. function admin_ajax_delete($id = null) {
  953. if(!$id) {
  954. $this->ajaxError(500, '無効な処理です。');
  955. }
  956. $page = $this->Page->read(null, $id);
  957. if($this->Page->del($id)) {
  958. clearViewCache($page['Page']['url']);
  959. $this->Page->saveDbLog('固定ページ: '.$page['Page']['name'].' を削除しました。');
  960. echo true;
  961. }
  962. exit();
  963. }
  964. /**
  965. * [ADMIN] 固定ページコピー
  966. *
  967. * @param int $id
  968. * @return void
  969. * @access public
  970. */
  971. function admin_ajax_copy($id = null) {
  972. $result = $this->Page->copy($id);
  973. if($result) {
  974. $result['Page']['id'] = $this->Page->getInsertID();
  975. $this->setViewConditions('Page', array('action' => 'admin_index'));
  976. $this->_setAdminIndexViewData();
  977. ClassRegistry::removeObject('View'); // Page 保存時に requestAction で 固定ページテンプレート生成用に初期化される為
  978. $this->set('data', $result);
  979. } else {
  980. $this->ajaxError(500, $this->Page->validationErrors);
  981. }
  982. }
  983. /**
  984. * 一覧の表示用データをセットする
  985. *
  986. * @return void
  987. * @access protected
  988. */
  989. function _setAdminIndexViewData() {
  990. $user = $this->BcAuth->user();
  991. $allowOwners = array();
  992. if(!empty($user)) {
  993. $allowOwners = array('', $user['User']['user_group_id']);
  994. }
  995. $this->set('users', $this->Page->getControlSource('user_id'));
  996. $this->set('allowOwners', $allowOwners);
  997. $this->set('sortmode', $this->passedArgs['sortmode']);
  998. }
  999. }
  1000. ?>