5 * @copyright Copyright 2010, Cake. (http://trpgtools-onweb.sourceforge.jp/)
7 * @package TRPG Data Bank
9 * @author Cake <cake_67@users.sourceforge.jp>
10 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
11 * @link http://trpgtools-onweb.sourceforge.jp/
18 App::import('Vendor', 'pear_ini');
20 class AppController extends Controller
22 var $user = array('User' => array(
27 var $isAdministrator = false;
30 var $site_configs = array();
32 var $components = array(
35 'Cakeplus.HtmlEscape',
58 var $conditions = array(
59 'Character.public_flag' => 'public',
60 'Character.deleted' => 0
64 'Character.system_id',
67 'Character.main_picture',
68 'Character.sort_order',
70 'Character.public_flag',
73 var $contain = array();
76 'Character.modified' => 'DESC',
78 var $paginate = array(
81 'conditions' => array(
82 'Character.public_flag' => 'public',
83 'Character.deleted' => 0
87 'Character.modified' => 'DESC',
92 var $showlist_cols = array(
95 var $cacheAction = array();
97 // POSTのTokenチェックをしないアクション
98 var $disableTokenActions = array();
100 var $model_public_flags = array(
103 var $model_status = array(
106 var $model_status2 = array(
109 function beforeFilter()
112 // Session->setFlashのメッセージがある場合、キャッシュしない
113 $message = $this->Session->read('Message.flash');
114 if (!empty($message['message']) && Configure::read('Cache.check')) {
115 Configure::write('Cache.check', false);
118 if (isset($this->Benchmark) && is_object($this->Benchmark)) {
119 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' .' beforeFilterStart');
122 parent::beforeFilter();
124 $this->user['User']['name'] = __('Guest', true);
127 $this->disableCache();
129 if (isset($this->params['prefix'])) {
130 if ($this->params['prefix'] == Configure::read('Routing.base_prefixes')) {
131 $this->isAdmin = true;
132 $this->layout = 'admin_'.$this->layout;
135 $this->set('admin', $this->isAdmin);
138 $this->site_configs = CorePlus::set_db_settings();
139 $this->set('site_configs', $this->site_configs);
141 if ($this->AuthPlus) {
143 // $this->AuthPlus->actionPath = 'controllers/';
144 // $this->AuthPlus->authorize = 'crud';
145 $this->AuthPlus->authorize = 'orig';
147 $this->user_id = $this->AuthPlus->user('id');
148 $user = $this->getUser($this->user_id);
153 if (in_array($this->AuthPlus->user('group_id'), array(1,2,3))) {
154 $this->isAdministrator = true;
158 $this->Token->checkToken();
161 $this->AuthPlus->loginAction = '/users/login';
162 $this->AuthPlus->loginRedirect = Configure::read('Routing.basePath');
163 $this->AuthPlus->logoutRedirect = Configure::read('Routing.basePath');
164 if ($this->isAdmin) {
165 $this->AuthPlus->loginRedirect = '/admin/users/index';
168 $this->set('user', $this->user);
169 $this->set('isAdministrator', $this->isAdministrator);
172 function beforeRender()
174 if (isset($this->Benchmark) && is_object($this->Benchmark)) {
175 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' . ' beforeRenderStart');
178 parent::beforeRender();
181 function afterFilter()
183 if (isset($this->Benchmark) && is_object($this->Benchmark)) {
184 $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' . ' afterFilterStart');
186 parent::afterFilter();
190 /* public_flag設定をview用にセット */
191 function set_public_flag4view()
193 $this->model_public_flags = $this->get_public_flag();
195 $this->set('public_flags', $this->model_public_flags);
197 function get_public_flag()
199 return $this->{$this->modelClass}->public_flag;
201 function check_public_flag($data, $key = null)
203 return CorePlus::check_public_flag($data, $key);
205 function check_public_flag2($data, $key = null)
207 return CorePlus::check_public_flag2($data, $key);
210 /* status設定をview用にセット */
211 function set_status4view()
213 $this->model_status = $this->get_status();
215 $this->set('status', $this->model_status);
217 $this->model_status2 = array_merge($this->model_status, array('all'));
219 $this->set('status2', $this->model_status2);
221 function get_status()
223 if (isset($this->Character)) {
224 return $this->Character->status;
225 } elseif (isset($this->CharacterProfileArchive)) {
226 return $this->CharacterProfileArchive->Character->status;
233 function isOwner($data, $user_id)
235 return CorePlus::isOwner($data, $user_id);
239 function getUser($id, $isAdmin=false, $isDelete=false) {
244 if (!isset($this->User)) {
245 $this->User = CorePlus::set_model('User');
251 if ($isAdmin === true) {
252 $conditions['User.group_id'] = array(
253 Configure::read('Group.admin'),
254 Configure::read('Group.subadmin'),
255 Configure::read('Group.watcher'),
256 Configure::read('Group.member'),
257 Configure::read('Group.locked'),
258 Configure::read('Group.pre'),
261 $conditions['User.group_id'] = array(
262 Configure::read('Group.admin'),
263 Configure::read('Group.subadmin'),
264 Configure::read('Group.watcher'),
265 Configure::read('Group.member'),
266 Configure::read('Group.locked'),
279 if ($isAdmin === true) {
280 $fields = array_merge($fields, array(
291 if ($this->name == 'Users') {
292 $contain = array_merge($contain, array(
293 'CharacterSheet' => array(
299 if ($id == $this->user_id || $isAdmin === true) {
300 unset($this->User->hasMany['CharacterSheet']['conditions']['CharacterSheet.public_flag']);
302 $this->User->hasMany['CharacterSheet']['conditions']['CharacterSheet.public_flag'] = 'public';
305 if ($isAdmin === true || $isDelete === true) {
306 $contain = array_merge($contain, array(
310 unset($this->User->hasMany['Character']['conditions']['Character.public_flag']);
311 unset($this->User->hasMany['Character']['limit']);
314 if (empty($contain)) {
317 $recursive = Set::countDim($contain, true);
320 $user = $this->User->find('first', array(
321 'conditions' => $conditions,
323 'recursive' => $recursive,
324 'contain' => $contain,
331 function _getThisSystem($id, $isAdmin = false)
335 static $prev_isAdmin;
337 if (!empty($this_system)
338 && (!empty($prev_id) && $id == $prev_id)
339 && ($isAdmin == $prev_isAdmin)) {
344 $prev_isAdmin = $isAdmin;
345 $system_conditions['System.id'] = $id;
346 if ($isAdmin === false) {
347 $system_conditions['System.public_flag'] = 'public';
350 $this_system = $this->{$this->modelClass}->System->find('first', array(
351 'conditions' => $system_conditions,
355 if (!isset($this_system['System'])) {
356 $this->Session->setFlash(__('Invalid System.', true));
357 $this->redirect(array('action' => 'index'));
364 function _check_demo()
366 if (Configure::read('Mode.Demo')) {
367 if ($_SESSION['Auth']['User']['username'] == 'guest') {
368 $this->Session->setFlash(__('Unavailable for GUEST.', true));
370 if ($this->site_configs['User.newUserRegist']['value']) {
371 $this->redirect(array('controller' => 'users', 'action'=>'add'));
374 $this->redirect(array('action'=>'index'));
381 function _restore_html_system($data, $nl2br = false) {
382 if (isset($data['System']['name']) && !empty($data['System']['name'])) {
383 $data['System']['name'] = $this->{$this->modelClass}->restore_html($data['System']['name'], false, false, false);
385 if (isset($data['System']['copyright']) && !empty($data['System']['copyright'])) {
386 $data['System']['copyright'] = $this->{$this->modelClass}->restore_html($data['System']['copyright'], false, false, false);
388 if (isset($data['System']['url']) && !empty($data['System']['url'])) {
389 $data['System']['url'] = $this->{$this->modelClass}->restore_html($data['System']['url'], false, false, false);
391 if (isset($data['System']['detail']) && !empty($data['System']['detail'])) {
392 $data['System']['detail'] = $this->{$this->modelClass}->restore_html($data['System']['detail'], false, false, false);
394 $data['System']['detail'] = str_replace('<br />', "\n", $data['System']['detail']);
397 if (isset($data['System']['ad']) && !empty($data['System']['ad'])) {
398 $data['System']['ad'] = $this->{$this->modelClass}->restore_html($data['System']['ad'], false, false, false);
401 if (isset($data['Profile']) && !empty($data['Profile'])) {
402 $data = $this->_restore_html_profile($data);
408 function _get_systems($public_flag = 'public', $conditions = array(), $fields = array())
410 if (!isset($this->System)) {
411 $this->System = CorePlus::set_model('System');
414 if (empty($public_flag)) {
415 if (isset($conditions['System.public_flag'])) {
416 unset($conditions['System.public_flag']);
419 $conditions = array_merge(
422 'System.public_flag' => $public_flag,
428 $fields = array_merge(
437 $systems = $this->System->find('all', array(
438 'conditions' => $conditions,
443 if (!empty($systems)) {
444 $systems = $this->_restore_html_get_systems($systems, $public_flag);
452 function _set_conditions_characters4user_id($user_id = null, $conditions = array(), $fields = array(), $contain = array(), $order = array())
454 if (!empty($this->showlist_cols)) {
455 $this->contain = array_merge($this->contain, array('CharactersHasProfile'));
456 if (isset($this->showlist_cols['Profile']) && !empty($this->showlist_cols['Profile'])) {
457 $this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.profile_id'] = $this->showlist_cols['Profile'];
461 if (!empty($user_id)) {
462 $conditions = array_merge((array)$conditions,
464 'Character.user_id' => $user_id,
470 $conditions = array_merge($this->conditions, (array)$conditions);
471 if ((isset($conditions['isAdmin']) && !empty($conditions['isAdmin']))
472 || (!empty($user_id) && ($user_id == $this->user_id))
473 || (isset($conditions['public_force']) && !empty($conditions['public_force']))) {
474 if (isset($this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.public_flag'])) {
475 unset($this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.public_flag']);
477 unset($conditions['Character.public_flag']);
478 unset($conditions['CharactersHasProfile.public_flag']);
480 if (isset($conditions['profile_search'])) {
481 $this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.public_flag'] = 'public';
483 $conditions['Character.public_flag'] = 'public';
484 /* if (!isset($conditions['CharactersHasProfile.public_flag']) || empty($conditions['CharactersHasProfile.public_flag'])) {
485 $conditions['CharactersHasProfile.public_flag'] = 'public';
488 unset($conditions['isAdmin']);
489 unset($conditions['public_force']);
491 $fields = array_merge($this->fields,
495 $contain = array_merge($this->contain, (array)$contain);
496 $contain = array_unique($contain);
497 if (empty($contain)) {
500 $recursive = Set::countDim($contain);
503 $order = array_merge($this->order, (array)$order);
505 return array($conditions, $fields, $contain, $recursive, $order);
508 function _get_characters_list4user_id($user_id = null, $conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
510 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
512 if ($this->modelClass != 'Character') {
513 $this->Character = CorePlus::set_model('Character');
516 return $this->Character->find('all', array(
517 'conditions' => $conditions,
520 'recursive' => $recursive,
521 'contain' => $contain,
526 function _get_characters_page4user_id($user_id = null, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1)
528 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
530 if (!isset($this->Character)) {
531 $this->Character = CorePlus::set_model('Character');
535 if ($key = array_search('System.copyright', $this->Character->belongsTo['System']['fields'])) {
536 unset($this->Character->belongsTo['System']['fields'][$key]);
539 $this->paginate['Character'] = array(
541 'conditions' => $conditions,
543 'contain' => $contain,
544 'recursive' => $recursive,
549 return $this->paginate('Character');
554 function _get_character4character_id($character_id, $user_id = null, $public_flag = null, $is_deleted = 0)
556 if (!isset($this->Character)) {
557 $this->Character = CorePlus::set_model('Character');
561 'Character.id' => $character_id,
562 'Character.deleted' => $is_deleted,
565 $conditions['Character.user_id'] = $user_id;
568 unset($this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
570 $conditions['Character.public_flag'] = $public_flag;
571 $this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag'] = $public_flag;
574 $character = $this->Character->find('first', array(
575 'conditions' => $conditions,
577 'CharacterPicture' => array(
583 if (empty($character)) {
584 $this->Session->setFlash(__('Invalid Character.', true));
585 $this->redirect(array('controller' => 'characters', 'action'=>'index'));
590 function check_character_picture_max($character_pictures_num)
592 if (!$max_num = intval($this->site_configs['Character.maxPictures']['value'])) {
596 if ($max_num <= $character_pictures_num) {
604 function _restore_html_user($data, $nl2br = false) {
605 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
607 if (isset($data['notes']) && !empty($data['notes'])) {
608 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
610 $data['notes'] = str_replace('<br />', "\n", $data['notes']);
615 $data = $this->decrypt_mail($data);
619 function _restore_html_news($data, $nl2br = false) {
620 $data['title'] = $this->{$this->modelClass}->restore_html($data['title'], false, false, false);
622 if (isset($data['value']) && !empty($data['value'])) {
623 $data['value'] = $this->{$this->modelClass}->restore_html($data['value'], false, false, false);
625 $data['value'] = str_replace('<br />', "\n", $data['value']);
633 function decrypt_mail($data)
635 if (isset($data['pcmail']) && !empty($data['pcmail'])) {
636 $data['pcmail'] = $this->Crypt->decrypt($data['pcmail']);
641 /* Profile系 htmlRestore */
642 function _restore_html_profile($data) {
643 if (isset($data['Profile']['name']) && !empty($data['Profile']['name'])) {
644 $data['Profile']['name'] = $this->{$this->modelClass}->restore_html($data['Profile']['name'], false, false, false);
647 if (isset($data['Profile'][0]['ProfileSelect'])) {
648 foreach ($data['Profile'] as $k => $v) {
649 $data['Profile'][$k] = $this->__restore_html_profile($v);
651 $data['Profile'] = $this->_restore_html_profiles($data['Profile']);
653 } elseif(isset($data['Profile']['ProfileSelect'])) {
654 $data['Profile'] = $this->__restore_html_profile($data['Profile']);
659 function __restore_html_profile($data) {
660 if (isset($data['ProfileSelect']) && !empty($data['ProfileSelect'])) {
661 $data['ProfileSelect'] = $this->_restore_html_profile_select($data['ProfileSelect']);
663 if (isset($data['ProfileTable']) && !empty($data['ProfileTable'])) {
664 $data['ProfileTable'] = $this->_restore_html_profile_table($data['ProfileTable']);
665 if (isset($data['ProfileTable'][0]['ProfileTableStatic']) && !empty($data['ProfileTable'][0]['ProfileTableStatic'])) {
666 $data['ProfileTable'][0]['ProfileTableStatic'] = $this->_restore_html_profile_table_static($data['ProfileTable'][0]['ProfileTableStatic']);
672 function _restore_html_profiles($data) {
673 $sort_order = array();
674 foreach ($data as $k => $v) {
675 if (isset($v['value']) && !empty($v['value'])) {
676 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
679 if (isset($v['sort_order']) && !empty($v['sort_order'])) {
680 $sort_order[$k] = $v['sort_order'];
686 $data = $this->sort4sort_order($data, $sort_order);
690 function _restore_html_get_systems($data, $public_flag = array()) {
691 $sort_order = array();
692 foreach ($data as $k => $v) {
693 $restored[$this->{$this->modelClass}->restore_html($v['System']['name'])] = $v['System']['id'];
695 if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value'] && !empty($public_flag)) {
699 if (isset($v['System']['sort_order']) && !empty($v['System']['sort_order'])) {
700 $sort_order[$v['System']['id']] = $v['System']['sort_order'];
702 $sort_order[$v['System']['id']] = 0;
706 $restored = $this->sort4sort_order($restored, $sort_order);
708 return array_flip($restored);
710 function _restore_html_profile_select($data) {
711 $sort_order = array();
712 foreach ($data as $k => $v) {
713 if (isset($v['value']) && !empty($v['value'])) {
714 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
717 if (isset($v['sort_order']) && !empty($v['sort_order'])) {
718 $sort_order[$k] = $v['sort_order'];
724 $data = $this->sort4sort_order($data, $sort_order);
728 function _restore_html_profile_table($data) {
729 foreach ($data as $k => $v) {
730 if (isset($v['value']) && !empty($v['value'])) {
731 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
734 if (isset($v['sort_order']) && !empty($v['sort_order'])) {
735 $sort_order[$k] = $v['sort_order'];
741 $data = $this->sort4sort_order($data, $sort_order);
745 function _restore_html_profile_table_static($data) {
746 if (empty($data) || !is_array($data)) {
750 foreach ($data as $k => $v) {
751 if (isset($v['title']) && !empty($v['title'])) {
752 $data[$k]['title'] = $this->{$this->modelClass}->restore_html($v['title'], false, false, false);
755 if (isset($v['sort_order']) && !empty($v['sort_order'])) {
756 $sort_order[$k] = $v['sort_order'];
762 $data = $this->sort4sort_order($data, $sort_order);
767 function sort4sort_order($data, $sort_order)
769 if (empty($data) || empty($sort_order)) {
772 array_multisort($sort_order, SORT_ASC, $data);
777 function _checkCharaSheeOwner($characterSheet)
779 if (!isset($characterSheet['CharacterSheet']) || empty($characterSheet['CharacterSheet']) || !isset($characterSheet['CharacterSheet']['user_id']) || empty($characterSheet['CharacterSheet']['user_id'])) {
783 if ($characterSheet['CharacterSheet']['user_id'] == $this->user_id) {
790 function checkEditOtherSystem($system_id = null)
792 if ($this->isOtherSystem($system_id)) {
793 $this->Session->setFlash(__('Other System cannot have ANY PROFILE SETTINGS.', true));
794 $this->redirect(array('controller' => 'systems', 'action'=>'view', $system_id));
797 function isOtherSystem($system_id = null)
799 if (empty($system_id)) {
803 if (empty($this->site_configs['System.otherSetting']['value']) || $this->site_configs['System.otherSetting']['value'] != $system_id) {
811 /* POSTのCharactersHasProfileを処理 */
812 function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array(), $allow_blank = false)
814 $prev_profile_id = null;
815 $profileTable_tmp = array();
818 foreach ($characters_has_profiles as $k => $v) {
819 if (!empty($character_id)) {
820 $characters_has_profiles[$k]['character_id'] = $character_id;
823 if (is_array($v['value'])) {
824 $characters_has_profiles[$k]['value'] = $v['value'][0];
828 $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
829 $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
831 if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
832 // $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
833 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
836 // 空の値処理, public_flag
837 // table: 一時保管して全項目空の場合行削除
838 if (CorePlus::is_valid($v, 'profile_table_id')) {
839 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
845 if ($public == null && isset($characters_has_profiles[$k]['public_flag'])) {
846 $public = $v['public_flag'];
848 if ($v['profile_table_static_id']) {
849 $characters_has_profiles[$k]['public_flag'] = 'public';
852 $characters_has_profiles[$k]['public_flag'] = $public;
855 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
856 // $allow_blank = falseならそれ以外は削除
857 } elseif (empty($v['value']) && empty($allow_blank)) {
858 unset($characters_has_profiles[$k]);
862 // table: 空行削除、不正profile_table_idデータ削除
863 if (!empty($profileTable_tmp)) {
864 if (!isset($this->Character->ProfileTable)) {
865 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
867 foreach($profileTable_tmp as $profile_id => $v) {
868 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
869 'conditions' => array('ProfileTable.profile_id' => $profile_id),
870 'fields' => array('ProfileTable.id'),
871 'order' => array('ProfileTable.sort_order' => 'asc'),
875 foreach ($v as $i => $profile_tables) {
877 $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
878 if (!empty($diff_keys)) {
879 foreach($diff_keys as $dvalue) {
880 unset($characters_has_profiles[$dvalue[1]]);
886 foreach($profile_table_columns as $k2 => $columns) {
887 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
892 if ($delete_flg === true) {
893 foreach ($profile_tables as $delk) {
894 unset($characters_has_profiles[$delk[1]]);
902 if (!is_null($profile_id) && !empty($now_data)) {
905 foreach($now_data as $k => $v) {
906 if ($v['id'] == $profile_id) {
907 $v['CharactersHasProfile'] = $characters_has_profiles;
910 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
912 if (isset($tmp[$i]['id'])) {
913 unset($tmp[$i]['id']);
915 if (isset($tmp[$i]['character_id'])) {
916 unset($tmp[$i]['character_id']);
923 $characters_has_profiles = $tmp;
926 return $characters_has_profiles;
930 function get_news($limit = 5) {
931 $this->News = CorePlus::set_model('News');
933 $news = $this->News->find('all', array(
934 'conditions' => array(
935 'News.public_flag' => 'public',
936 'News.end_date > ' => date('Y-m-d 00:00:00', time()),
941 'News.date' => 'desc'
946 foreach($news as $k => $v) {
947 $news[$k]['News'] = $this->_restore_html_news($v['News']);
960 * 文字コード変換 SJISWin->UTF-8
962 function convertEncodeSjis2Utf8(&$str, $key)
964 $str = mb_convert_encoding($str, 'UTF-8', 'SJIS-Win');