PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/controllers/page_categories_controller.php

https://github.com/hashing/basercms
PHP | 511 lines | 305 code | 54 blank | 152 comment | 59 complexity | 37b9d8d47187197f5fa3424e89c70388 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 PageCategoriesController extends AppController {
  27. /**
  28. * Controller name
  29. *
  30. * @var string
  31. * @access public
  32. */
  33. var $name = 'PageCategories';
  34. /**
  35. * Default helper
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. var $helpers = array(BC_TEXT_HELPER, BC_FORM_HELPER, BC_ARRAY_HELPER);
  41. /**
  42. * This controller does not use a model
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. var $uses = array('PageCategory');
  48. /**
  49. * コンポーネント
  50. *
  51. * @var array
  52. * @access public
  53. */
  54. var $components = array('BcAuth','Cookie','BcAuthConfigure');
  55. /**
  56. * パンくず
  57. * @var array
  58. * @access public
  59. */
  60. var $crumbs = array(array('name' => '固定ページ管理', 'url' => array('controller' => 'pages', 'action' => 'index')));
  61. /**
  62. * beforeFilter
  63. *
  64. * @return void
  65. * @access public
  66. */
  67. function beforeFilter() {
  68. parent::beforeFilter();
  69. $user = $this->BcAuth->user();
  70. $userModel = $this->getUserModel();
  71. $newCatAddable = $this->PageCategory->checkNewCategoryAddable(
  72. $user[$userModel]['user_group_id'],
  73. $this->checkRootEditable()
  74. );
  75. $this->set('newCatAddable', $newCatAddable);
  76. }
  77. /**
  78. * [ADMIN] 固定ページカテゴリーリスト
  79. *
  80. * @return void
  81. * @access public
  82. */
  83. function admin_index() {
  84. if(!Configure::read('BcApp.mobile')) {
  85. $this->data['PageCategory']['type'] = 'pc';
  86. }
  87. $default = array('PageCategory' => array('type'=>'pc'));
  88. $this->setViewConditions('PageCategory', array('default' => $default));
  89. $mobileId = $this->PageCategory->getAgentId('mobile');
  90. $smartphoneId = $this->PageCategory->getAgentId('smartphone');
  91. $ids = array();
  92. $conditions = array();
  93. if($this->data['PageCategory']['type'] == 'pc' || empty($this->data['PageCategory']['type'])) {
  94. $children = am($this->PageCategory->children($mobileId, false, array('PageCategory.id')), $this->PageCategory->children($smartphoneId, false, array('PageCategory.id')));
  95. if($children) {
  96. $ids = am($ids, Set::extract('/PageCategory/id', $children));
  97. }
  98. $ids = am(array($mobileId, $smartphoneId), $ids);
  99. $conditions = array('NOT' => array('PageCategory.id' => $ids));
  100. } elseif($this->data['PageCategory']['type'] == 'mobile') {
  101. $children = am($this->PageCategory->children($mobileId, false, array('PageCategory.id')));
  102. if($children) {
  103. $ids = am($ids, Set::extract('/PageCategory/id', $children));
  104. }
  105. if($ids) {
  106. $conditions = array(array('PageCategory.id' => $ids));
  107. }
  108. } elseif($this->data['PageCategory']['type'] == 'smartphone') {
  109. $children = am($this->PageCategory->children($smartphoneId, false, array('PageCategory.id')));
  110. if($children) {
  111. $ids = am($ids, Set::extract('/PageCategory/id', $children));
  112. }
  113. if($ids) {
  114. $conditions = array(array('PageCategory.id' => $ids));
  115. }
  116. }
  117. $datas = array();
  118. if($conditions) {
  119. $_dbDatas = $this->PageCategory->generatetreelist($conditions);
  120. $datas = array();
  121. foreach($_dbDatas as $key => $dbData) {
  122. $category = $this->PageCategory->find('first', array('conditions' => array('PageCategory.id'=>$key), 'recursive' => -1));
  123. if(preg_match("/^([_]+)/i",$dbData,$matches)) {
  124. $prefix = str_replace('_','&nbsp&nbsp&nbsp',$matches[1]);
  125. $category['PageCategory']['title'] = $prefix.'└'.$category['PageCategory']['title'];
  126. $category['PageCategory']['depth'] = strlen($matches[1]);
  127. } else {
  128. $category['PageCategory']['depth'] = 0;
  129. }
  130. $datas[] = $category;
  131. }
  132. }
  133. $this->_setAdminIndexViewData();
  134. $this->set('datas', $datas);
  135. if($this->RequestHandler->isAjax() || !empty($this->params['url']['ajax'])) {
  136. $this->render('ajax_index');
  137. return;
  138. }
  139. $pageType = array();
  140. if(Configure::read('BcApp.mobile') || Configure::read('BcApp.smartphone')) {
  141. $pageType = array('pc' => 'PC');
  142. }
  143. if(Configure::read('BcApp.mobile')) {
  144. $pageType['mobile'] = 'モバイル';
  145. }
  146. if(Configure::read('BcApp.smartphone')) {
  147. $pageType['smartphone'] = 'スマートフォン';
  148. }
  149. if($pageType) {
  150. $this->search = 'page_categories_index';
  151. }
  152. $this->help = 'page_categories_index';
  153. $this->set('pageType', $pageType);
  154. /* 表示設定 */
  155. $this->subMenuElements = array('pages','page_categories');
  156. $this->pageTitle = '固定ページカテゴリー一覧';
  157. $this->search = 'page_categories_index';
  158. $this->help = 'page_categories_index';
  159. }
  160. /**
  161. * [ADMIN] 固定ページカテゴリー情報登録
  162. *
  163. * @return void
  164. * @access public
  165. */
  166. function admin_add() {
  167. if(empty($this->data)) {
  168. $this->data = array('PageCategory' => array('contents_navi' => false, 'page_category_type' => 1));
  169. } else {
  170. if(!$this->data['PageCategory']['parent_id']) {
  171. switch ($this->data['PageCategory']['page_category_type']) {
  172. case 1:
  173. $this->data['PageCategory']['parent_id'] = '';
  174. break;
  175. case 2:
  176. $this->data['PageCategory']['parent_id'] = $this->PageCategory->getAgentId('mobile');
  177. break;
  178. case 3:
  179. $this->data['PageCategory']['parent_id'] = $this->PageCategory->getAgentId('smartphone');
  180. break;
  181. }
  182. }
  183. unset($this->data['PageCategory']['page_category_type']);
  184. /* 登録処理 */
  185. $this->PageCategory->create($this->data);
  186. if($this->PageCategory->validates()) {
  187. if($this->PageCategory->save($this->data,false)) {
  188. $message = '固定ページカテゴリー「'.$this->data['PageCategory']['name'].'」を追加しました。';
  189. if(ini_get('safe_mode')) {
  190. $message .= '<br />機能制限のセーフモードで動作しているので、手動で次のフォルダ内に追加したカテゴリと同階層のフォルダを作成し、書込権限を与える必要があります。<br />'.
  191. WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.'pages'.DS;
  192. }
  193. $this->Session->setFlash($message);
  194. $this->PageCategory->saveDbLog('固定ページカテゴリー「'.$this->data['PageCategory']['name'].'」を追加しました。');
  195. $this->redirect(array('controller' => 'page_categories', 'action' => 'index'));
  196. }else {
  197. $this->Session->setFlash('保存中にエラーが発生しました。');
  198. }
  199. }else {
  200. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  201. }
  202. }
  203. /* 表示設定 */
  204. $user = $this->BcAuth->user();
  205. $userModel = $this->getUserModel();
  206. $parents = $this->PageCategory->getControlSource('parent_id', array(
  207. 'ownerId' => $user[$userModel]['user_group_id']
  208. ));
  209. if($this->checkRootEditable()) {
  210. if($parents) {
  211. $parents = array('' => '指定しない') + $parents;
  212. } else {
  213. $parents = array('' => '指定しない');
  214. }
  215. } else {
  216. $mobileId = $this->PageCategory->getAgentId('mobile');
  217. if(isset($parents[$mobileId])) {
  218. unset($parents[$mobileId]);
  219. }
  220. $smartphoneId = $this->PageCategory->getAgentId('smartphone');
  221. if(isset($parents[$smartphoneId])) {
  222. unset($parents[$smartphoneId]);
  223. }
  224. }
  225. $this->set('parents', $parents);
  226. $this->subMenuElements = array('pages','page_categories');
  227. $this->pageTitle = '新規固定ページカテゴリー登録';
  228. $this->help = 'page_categories_form';
  229. $this->render('form');
  230. }
  231. /**
  232. * [ADMIN] 固定ページカテゴリー情報編集
  233. *
  234. * @param int page_id
  235. * @return void
  236. * @access public
  237. */
  238. function admin_edit($id) {
  239. /* 除外処理 */
  240. if(!$id && empty($this->data)) {
  241. $this->Session->setFlash('無効なIDです。');
  242. $this->redirect(array('action' => 'index'));
  243. }
  244. if(empty($this->data)) {
  245. $this->data = $this->PageCategory->read(null, $id);
  246. $this->data['PageCategory']['page_category_type'] = $this->PageCategory->getType($this->data['PageCategory']['id']);
  247. }else {
  248. if(!$this->data['PageCategory']['parent_id']) {
  249. switch ($this->data['PageCategory']['page_category_type']) {
  250. case 1:
  251. $this->data['PageCategory']['parent_id'] = '';
  252. break;
  253. case 2:
  254. $this->data['PageCategory']['parent_id'] = $this->PageCategory->getAgentId('mobile');
  255. break;
  256. case 3:
  257. $this->data['PageCategory']['parent_id'] = $this->PageCategory->getAgentId('smartphone');
  258. break;
  259. }
  260. }
  261. unset($this->data['PageCategory']['page_category_type']);
  262. /* 更新処理 */
  263. $this->PageCategory->set($this->data);
  264. if($this->PageCategory->validates()) {
  265. if($this->PageCategory->save($this->data,false)) {
  266. $this->Session->setFlash('固定ページカテゴリー「'.$this->data['PageCategory']['name'].'」を更新しました。');
  267. $this->PageCategory->saveDbLog('固定ページカテゴリー「'.$this->data['PageCategory']['name'].'」を更新しました。');
  268. $this->redirect(array('action' => 'index'));
  269. }else {
  270. $this->Session->setFlash('保存中にエラーが発生しました。');
  271. }
  272. }else {
  273. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  274. }
  275. }
  276. $indexPage = array();
  277. if(isset($this->data['Page'])){
  278. foreach($this->data['Page'] as $page){
  279. if($page['name']=='index'){
  280. $indexPage['url'] = preg_replace('/^\/mobile\//is', '/m/', $page['url']);
  281. $indexPage['status'] = $page['status'];
  282. break;
  283. }
  284. }
  285. }
  286. $this->set('indexPage',$indexPage);
  287. /* 表示設定 */
  288. $user = $this->BcAuth->user();
  289. $userModel = $this->getUserModel();
  290. $mobileId = $this->PageCategory->getAgentId();
  291. $parents = $this->PageCategory->getControlSource('parent_id', array(
  292. 'excludeParentId' => $this->data['PageCategory']['id'],
  293. 'ownerId' => $user[$userModel]['user_group_id']
  294. ));
  295. if($this->checkRootEditable()) {
  296. if($parents) {
  297. $parents = array('' => '指定しない') + $parents;
  298. } else {
  299. $parents = array('' => '指定しない');
  300. }
  301. } elseif(isset($parents[$mobileId])) {
  302. unset($parents[$mobileId]);
  303. }
  304. $this->set('parents', $parents);
  305. $this->subMenuElements = array('pages','page_categories');
  306. $this->pageTitle = '固定ページカテゴリー情報編集:'.$this->data['PageCategory']['title'];
  307. $this->help = 'page_categories_form';
  308. $this->render('form');
  309. }
  310. /**
  311. * [ADMIN] 固定ページカテゴリー情報削除
  312. *
  313. * @param int page_id
  314. * @return void
  315. * @access public
  316. */
  317. function admin_delete($id = null) {
  318. /* 除外処理 */
  319. if(!$id) {
  320. $this->Session->setFlash('無効なIDです。');
  321. $this->redirect(array('action' => 'index'));
  322. }
  323. // メッセージ用にデータを取得
  324. $page = $this->PageCategory->read(null, $id);
  325. /* 削除処理 */
  326. if($this->PageCategory->del($id)) {
  327. $this->Session->setFlash('固定ページカテゴリー: '.$page['PageCategory']['name'].' を削除しました。');
  328. $this->PageCategory->saveDbLog('固定ページカテゴリー「'.$page['PageCategory']['name'].'」を削除しました。');
  329. }else {
  330. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  331. }
  332. $this->redirect(array('action' => 'index'));
  333. }
  334. /**
  335. * カテゴリの並び替え順を上げる
  336. *
  337. * @param type $id
  338. * @return void
  339. * @access public
  340. * @deprecated admin_ajax_up に移行
  341. */
  342. function admin_up($id) {
  343. $this->PageCategory->moveup($id);
  344. $this->redirect(array('controller' => 'page_categories', 'action' => 'index', "#" => 'Row'.$id));
  345. }
  346. /**
  347. * カテゴリの並び替え順を下げる
  348. *
  349. * @param type $id
  350. * @return void
  351. * @access public
  352. * @deprecated admin_ajax_down に移行
  353. */
  354. function admin_down($id) {
  355. $this->PageCategory->movedown($id);
  356. $this->redirect(array('controller' => 'page_categories', 'action' => 'index', "#" => 'Row'.$id));
  357. }
  358. /**
  359. * [ADMIN] 固定ページカテゴリー削除
  360. *
  361. * @param int $id 固定ページカテゴリーID
  362. * @return void
  363. * @access public
  364. */
  365. function admin_ajax_delete($id = null) {
  366. if(!$id) {
  367. $this->ajaxError(500, '無効な処理です。');
  368. }
  369. $data = $this->PageCategory->read(null, $id);
  370. if($this->PageCategory->del($id)) {
  371. $this->PageCategory->saveDbLog('固定ページ: '.$data['PageCategory']['name'].' を削除しました。');
  372. echo true;
  373. }
  374. exit();
  375. }
  376. /**
  377. * [ADMIN] 固定ページカテゴリーコピー
  378. *
  379. * @param int $id 固定ページカテゴリID
  380. * @return void
  381. * @access public
  382. */
  383. function admin_ajax_copy($id = null) {
  384. $result = $this->PageCategory->copy($id);
  385. if($result) {
  386. $result['PageCategory']['id'] = $this->PageCategory->getInsertID();
  387. $this->_setAdminIndexViewData();
  388. $this->set('data', $result);
  389. } else {
  390. $this->ajaxError(500, $this->PageCategory->validationErrors);
  391. }
  392. }
  393. /**
  394. * 一覧の表示用データをセットする
  395. *
  396. * @return void
  397. * @access protected
  398. */
  399. function _setAdminIndexViewData() {
  400. $allowOwners = array();
  401. if(isset($user['user_group_id'])) {
  402. $allowOwners = array('', $user['user_group_id']);
  403. }
  404. $this->set('allowOwners', $allowOwners);
  405. $this->set('owners', $this->PageCategory->getControlSource('owner_id'));
  406. }
  407. /**
  408. * 固定ページカテゴリ一括削除
  409. *
  410. * @param array $ids
  411. * @return boolean
  412. * @access protected
  413. */
  414. function _batch_del($ids) {
  415. if($ids) {
  416. foreach($ids as $id) {
  417. $data = $this->PageCategory->read(null, $id);
  418. if($this->PageCategory->del($id)) {
  419. $this->PageCategory->saveDbLog('固定ページカテゴリー: '.$data['PageCategory']['name'].' を削除しました。');
  420. }
  421. }
  422. }
  423. return true;
  424. }
  425. /**
  426. * [ADMIN] カテゴリの並び替え順を上げる
  427. *
  428. * @param int $id
  429. * @return void
  430. * @access public
  431. */
  432. function admin_ajax_up($id) {
  433. if($this->PageCategory->moveup($id)) {
  434. echo true;
  435. } else {
  436. $this->ajaxError(500, '一度リロードしてから再実行してみてください。');
  437. }
  438. exit();
  439. }
  440. /**
  441. * [ADMIN] カテゴリの並び替え順を下げる
  442. *
  443. * @param int $id
  444. * @return void
  445. * @access public
  446. * @deprecated
  447. */
  448. function admin_ajax_down($id) {
  449. if($this->PageCategory->movedown($id)) {
  450. echo true;
  451. } else {
  452. $this->ajaxError(500, '一度リロードしてから再実行してみてください。');
  453. }
  454. exit();
  455. }
  456. }
  457. ?>