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/
15 class CharactersController extends AppController {
17 var $name = 'Characters';
29 var $disableTokenActions = array();
30 var $post_data = array();
33 var $search_cols = array(
38 function beforeFilter() {
40 parent::beforeFilter();
43 $this->cacheAction = array(
44 'index' => Configure::read('Cache.expireShort'),
48 $this->AuthPlus->allow('view');
49 $this->AuthPlus->allow('index');
50 $this->AuthPlus->allow('search');
53 $this->set_public_flag4view();
54 $this->set_status4view();
57 function beforeRender()
59 parent::beforeRender();
65 function index($id = null) {
66 if (!empty($id) && $id == $this->user_id) {
67 $this->redirect(array_merge(array('action'=>'mycharacter'), $this->params['named']));
70 $this->set('isOwner', false);
75 function search($id = null) {
76 if (!empty($id) && $id == $this->user_id) {
77 $this->redirect(array_merge(array('action'=>'mysearch'), $this->params['named']));
80 $this->set('isOwner', false);
86 $this->set('isOwner', true);
88 $this->_search($this->user_id);
91 function mycharacter(){
94 if (empty($this->site_configs['Site.myHome']['value'])) {
95 $news = $this->get_news();
97 $this->set('news', $news);
99 $this->set('isOwner', true);
101 $this->_index($this->user_id);
104 function view($id = null) {
106 $this->Session->setFlash(__('Invalid Character.', true));
107 $this->redirect(array('action'=>'index'));
110 $character = $this->_view($id, array(), false);
112 if (!$this->check_public_flag($character['System'])) {
113 unset($character['System']['Profile']);
116 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
119 if ($this->isOwner($character['Character'], $this->user_id)) {
122 $this->set('isOwner', $isOwner);
124 // キャラ画像のpublic_flagチェック
125 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
126 foreach($character['CharacterPicture'] as $k => $v) {
127 if (!$this->check_public_flag2($v)) {
128 unset($character['CharacterPicture'][$k]);
133 // $character = array_merge($character, $this->Character->getTotalCount($id));
135 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
139 $systems = $this->_get_systems(1);
141 if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
142 $this->params['named']['system_id'] = key($systems);
145 if (isset($this->params['named']['system_id']) && !empty($this->params['named']['system_id'])) {
146 $system = $this->Character->System->find('first', array(
147 'conditions' => array(
148 'System.id' => $this->params['named']['system_id'],
154 'System.public_flag',
158 if (empty($system) || !$this->check_public_flag2($system['System'])) {
159 $this->Session->setFlash(__('Invalid ID.', true));
160 $this->redirect(array('action'=>'index'));
163 /* if (isset($system['System']) && !empty($system['System']) && $system['System']['set_npc']) {
167 if (!empty($this->data)) {
168 $this->data['Character']['user_id'] = $this->user_id;
169 if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
170 $this->data['Character']['system_id'] = key($systems);
173 $this->Character->create();
174 if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
176 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
177 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
179 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
183 $this->set('systems', $systems);
184 $this->set('isOwner', false);
186 $this->set('title_for_layout', " ". __('Add Character', true));
189 function edit($id = null) {
190 if (!$id && empty($this->data)) {
191 $this->Session->setFlash(__('Invalid ID.', true));
192 $this->redirect(array('action'=>'index'));
196 unset($this->Character->System->hasMany['Profile']['fields']);
197 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
198 unset($this->Character->hasMany['CharacterProfileArchive']);
200 $character = $this->_get_character($id, array(), false);
201 if (!$this->isOwner($character['Character'], $this->user_id)) {
202 $this->Session->setFlash(__('No Permission', true));
203 $this->redirect(array('action'=>'index'));
207 if (!$this->check_public_flag($character['System'])) {
208 $this->redirect(array('action'=>'change_system', $id));
210 /* if ($character['System']['set_npc']) {
214 if (empty($this->data)) {
215 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
218 if (!empty($this->data)) {
219 $this->post_data = $this->data;
221 $this->data['clearCache'] = array(
222 'character_id' => $id,
223 'user_id' => $this->user_id,
224 'system_id' => $character['System']['id'],
228 if (isset($this->data['CharactersHasProfile'])) {
229 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
233 $this->_set_validate4characters_has_profile($character['System']['id']);
235 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
237 $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
239 if ($requiredCheck) {
241 $this->Character->CharactersHasProfile->deleteAll(array(
242 'CharactersHasProfile.character_id' => $id
246 $this->data['Character']['id'] = $id;
247 $this->Character->create();
249 if ($this->Character->saveAll($this->data, array(
251 'fieldList' => array_merge(
252 $this->Character->fields['edit'],
253 $this->Character->CharactersHasProfile->fields['add']
258 if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
259 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
262 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
263 $this->redirect(array('action'=>'view', $id));
267 $validate_error = current(current(current($this->Character->validationErrors)));
268 $this->Session->setFlash($validate_error);
271 $this->data = array_merge($character, $this->post_data);
272 $this->data['Character'] = $this->post_data['Character'];
273 $this->data['Character']['main_picture'] = $character['Character']['main_picture'];
274 $this->data['Character']['system_id'] = $character['Character']['system_id'];
275 $this->data['Character']['id'] = $id;
276 foreach ($this->data['System']['Profile'] as $k1 => $profile) {
277 switch (CorePlus::get_profile_type($profile['profile_type'])) {
281 $colum_num = count($profile['CharactersHasProfile']);
284 $colum_num = count($profile['ProfileSelect']);
291 if ($colum_num == 0) {
292 switch (CorePlus::get_profile_type($profile['profile_type'])) {
294 $colum_num = count($profile['ProfileTable']) * count($profile['ProfileTable'][0]['ProfileTableStatic']);
297 $colum_num = count($profile['ProfileTable']) * 4;
300 $colum_num = count($profile['ProfileSelect']);
311 for ($i=0; $i<$colum_num; $i++) {
312 if (!isset($profile['CharactersHasProfile'][$i])) {
313 $profile['CharactersHasProfile'][$i] = array('character_id' => $character['Character']['id']);
316 $postdata = array_shift($this->post_data['CharactersHasProfile']);
318 if (is_array($postdata['value'])) {
319 $postdata['value'] = $postdata['value'][0];
322 if (isset($postdata[$i])) {
323 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], $postdata[$i]);
325 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], (array)$postdata);
331 elseif (empty($this->data)) {
332 $this->data = $character;
333 $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
336 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
338 $this->set('isOwner', true);
340 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
343 function add_multi_profiles($id = null) {
344 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
345 $this->Session->setFlash(__('Invalid ID.', true));
346 $this->redirect(array('action'=>'index'));
350 $this->Character->System->hasMany['Profile']['fields'] = '';
351 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
352 unset($this->Character->hasMany['CharacterProfileArchive']);
354 $character = $this->_get_character($id);
355 if (!$this->isOwner($character['Character'], $this->user_id)) {
356 $this->Session->setFlash(__('No Permission', true));
357 $this->redirect(array('action'=>'index'));
360 if (!$this->check_public_flag($character['System'])) {
361 $this->redirect(array('action'=>'change_system', $id));
365 if (empty($character['System']['Profile'])) {
366 $this->Session->setFlash(__('Invalid Profile.', true));
367 $this->redirect(array('action'=>'view', $id));
370 $profile_id = $this->params['named']['profile_id'];
373 foreach ($character['System']['Profile'] as $profile) {
374 if ($profile['id'] == $profile_id) {
381 $this->Session->setFlash(__('No Profile.', true));
382 $this->redirect(array('action'=>'view', $id));
385 $character['System']['Profile'] = array();
386 $character['System']['Profile'][0] = $target;
388 if (!in_array(CorePlus::get_profile_type($target['profile_type']), array(
393 $this->Session->setFlash(__('Invalid Profile.', true));
394 $this->redirect(array('action'=>'view', $id));
397 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
399 if (!empty($this->data)) {
400 $this->data['clearCache'] = array(
401 'character_id' => $id,
402 'user_id' => $this->user_id,
403 'system_id' => $character['System']['id'],
406 $this->post_data = $this->data;
407 $this->data = array();
408 if (isset($this->post_data['Character'])) {
409 $this->data['Character'] = array_merge($character['Character'], $this->post_data['Character']);
411 $this->data['Character'] = $character['Character'];
413 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
414 $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
415 $this->data['Character']['modified'] = null;
418 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->post_data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
421 $this->data['CharacterProfileArchive'] = $this->post_data['CharacterProfileArchive'];
424 $this->_set_validate4characters_has_profile($character['System']['id']);
425 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
427 $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
429 if ($requiredCheck) {
431 $this->Character->CharactersHasProfile->deleteAll(array(
432 'CharactersHasProfile.profile_id' => $target['id'],
433 'CharactersHasProfile.character_id' => $id
437 $this->Character->create();
438 if ($this->Character->saveAll($this->data, array(
440 'fieldList' => array_merge(
441 $this->Character->CharactersHasProfile->fields['add']
446 if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
447 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
450 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
451 $this->redirect(array('action'=>'view', $id));
453 $this->data = array_merge($character, $this->data);
458 $validate_error = current(current(current($this->Character->validationErrors)));
459 $this->Session->setFlash($validate_error);
462 $this->data = array_merge($character, $this->data);
463 $this->data['Character']['id'] = $id;
464 unset($this->data['Character']['__Token']);
468 if (empty($this->data)) {
469 $this->data = $character;
471 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
473 $this->set('isOwner', true);
475 if (isset($this->params['named']['num'])) {
476 $form_nums = $this->params['named']['num'];
480 $this->set('form_nums', $form_nums);
482 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
485 function change_system($id = null) {
487 $this->Session->setFlash(__('Invalid ID.', true));
488 $this->redirect(array('action'=>'index'));
492 $this->Character->System->hasMany['Profile']['fields'] = '';
493 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
495 $character = $this->_get_character($id);
496 if (!$this->isOwner($character['Character'], $this->user_id)) {
497 $this->Session->setFlash(__('No Permission', true));
498 $this->redirect(array('action'=>'index'));
503 if (isset($character['System']) && !empty($character['System'])) {
505 if ($this->isOtherSystem($character['System']['id'])) {
506 $this->set('isChange', true);
508 } elseif ($this->check_public_flag($character['System'])) {
509 $this->redirect(array('action'=>'edit', $id));
512 $this->set('isChange', true);
516 if (!empty($this->data)) {
517 $this->data['clearCache'] = array(
518 'character_id' => $id,
519 'user_id' => $this->user_id,
520 'system_id' => $character['System']['id'],
523 $this->data['Character']['id'] = $id;
525 // validateはsystem_idのみ
526 $this->Cahracter['validate'] = array(
527 'system_id' => array(
528 'validSystemId' => array(
529 'rule' => array('validSystemId', true),
530 'allowEmpty' => false,
535 if ($this->Character->save(
539 'fieldList' => array('system_id'),
542 $this->Session->setFlash(__('The new system has been saved', true));
543 $this->redirect(array('action'=>'view', $id));
545 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
549 $this->set('isOwner', true);
550 $this->set('character', $character);
552 $systems = $this->_get_systems(1);
553 $this->set('systems', $systems);
555 $this->set('title_for_layout', " ". sprintf(__('%s Change System', true), $character['Character']['name']));
559 function set_status($id = null) {
560 if (!$id || !isset($this->params['named']['mode'])) {
561 $this->Session->setFlash(__('Invalid ID.', true));
562 $this->redirect(array('controller' => 'characters', 'action'=>'index'));
565 $character = $this->_get_character4character_id($id, $this->user_id);
567 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
568 $this->Session->setFlash(__('Invalid URL.', true));
569 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
573 $new_character_picture = null;
574 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
575 if ($character['CharacterPicture']) {
576 foreach($character['CharacterPicture'] as $k => $v) {
577 if ($v['id'] == $this->params['named']['new_picture_id']) {
578 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
583 if (!$new_character_picture) {
584 $this->Session->setFlash(__('Invalid data.', true));
585 $this->redirect(array('controller' => 'character_pictures', 'action'=>'listview', $id));
590 $this->data['clearCache'] = array(
591 'character_id' => $id,
592 'user_id' => $this->user_id,
593 'system_id' => $character['Character']['system_id'],
597 $this->Character->id = $id;
598 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
599 $this->Character->save(
602 'fieldList' => array(
603 $this->params['named']['mode']
608 $this->Character->deleteCache4CharacterPicture($id);
611 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
612 $this->redirect(array('controller' => 'character_pictures', 'action' => 'listview', $id));
615 function delete($id = null) {
617 $this->Session->setFlash(__('Invalid id for Character', true));
618 $this->redirect(array('action'=>'index'));
621 if ($this->_delete($id)) {
622 $this->redirect(array('controller' => 'users', 'action'=>'index'));
625 $this->set('title_for_layout', " - ". __('Delete Character', true));
628 function admin_index($id = null) {
630 $this->_index($id, array('isAdmin' => true));
633 function admin_search($id = null) {
634 $this->search_cols['Character']['public_flag'] = array(
635 'name' => __('Public Flag', true),
637 'options' => CorePlus::set_publicflag($this->model_public_flags, array('all')),
641 $this->_search($id, array('isAdmin' => true));
644 function admin_view($id = null) {
646 $this->Session->setFlash(__('Invalid Character.', true));
647 $this->redirect(array('action' => 'admin_index'));
650 $character = $this->_view($id, array(), true);
652 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
655 function admin_edit($id = null) {
656 if (!$id && empty($this->data)) {
657 $this->Session->setFlash(__('Invalid ID.', true));
658 $this->redirect(array('action'=>'index'));
662 $this->Character->System->hasMany['Profile']['fields'] = '';
663 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
664 unset($this->Character->hasMany['CharacterProfileArchive']);
666 $character = $this->_get_character($id, array(), true);
668 $this->Session->setFlash(__('No Character', true));
669 $this->redirect(array('action'=>'index'));
672 if (empty($this->data)) {
673 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
676 if (!empty($this->data)) {
678 $this->data['clearCache'] = array(
679 'character_id' => $id,
680 'user_id' => $this->user_id,
681 'system_id' => $character['System']['id'],
684 if (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_private') {
685 $this->data['Character']['public_flag'] = 2;
686 } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_public') {
687 $this->data['Character']['public_flag'] = 1;
688 } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'delete_confirm') {
689 $this->redirect(array('controller' => 'characters', 'action'=>'admin_delete', $id));
691 $this->Session->setFlash(sprintf(__('Invalid data.', true), $this->data['Character']['name']));
692 $this->redirect(array('action'=>'view', $id));
695 $this->data['Character']['id'] = $id;
696 $this->Character->create();
697 if ($this->Character->save($this->data, array(
699 'fieldList' => array(
704 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $character['Character']['name']));
705 $this->redirect(array('action'=>'view', $id));
708 $this->data = array_merge($character, $this->data);
709 $this->data['Character']['id'] = $id;
712 if (empty($this->data)) {
713 $this->data = $character;
714 $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
716 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
719 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
722 function admin_delete($id = null) {
724 $this->Session->setFlash(__('Invalid id for Character', true));
725 $this->redirect(array('action'=>'index'));
728 if ($this->_delete($id, array(), true)) {
729 $this->redirect(array('action'=>'index'));
732 $this->set('title_for_layout', " - ". __('Delete Character', true));
737 function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
738 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
740 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
745 $title = __('List of All Characters', true);
748 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
750 $this_system = $this->_getThisSystem($this->params['named']['system'], $isAdmin);
752 $conditions['Character.system_id'] = $this->params['named']['system'];
755 $profiles = $this->Character->System->Profile->find('all', array(
760 'Profile.profile_type',
761 'Profile.sort_order',
763 'conditions' => array(
764 'Profile.system_id' => $this->params['named']['system'],
765 'Profile.show_list' => true,
770 foreach ($profiles as $k => $v) {
771 if (isset($v['Profile']['sort_order']) && !empty($v['Profile']['sort_order'])) {
772 $sort_order_profile[$v['Profile']['id']] = $v['Profile']['sort_order'];
774 $sort_order_profile[$v['Profile']['id']] = 0;
777 $prof[$v['Profile']['id']] = array(
778 'id' => $v['Profile']['id'],
779 'name' => $v['Profile']['name'],
780 'key_name' => $v['Profile']['key_name'],
781 'profile_type' => $v['Profile']['profile_type'],
784 if (!empty($sort_order_profile)) {
785 $prof = $this->sort4sort_order($prof, $sort_order_profile);
788 foreach ($prof as $k => $v) {
789 $this->showlist_cols['Profile'][] = $v['id'];
792 $this_system['Profile'] = $prof;
794 $this_system = $this->_restore_html_system($this_system);
796 /* if ($this_system['System']['set_npc']) {
800 $title = $this_system['System']['name']. " ". $title;
802 $this->set('this_system', $this_system);
804 $contain = array_merge(array('System'), (array)$contain);
806 // $this->isNpc = true;
811 $user = $this->Character->User->find('first', array(
812 'conditions' => array('User.id' => $id),
821 $this->Session->setFlash(__('Invalid Id.', true));
822 $this->redirect(array('action'=>'index'));
825 $conditions['Character.user_id'] = $id;
826 if ($id == $this->user_id) {
827 unset($this->paginate['conditions']['Character.public_flag']);
830 $title = $user['User']['name']. " ". $title;
832 $contain = array_merge($contain, array('User'));
834 $this->set('userSet', $user);
837 if (!isset($this->params['named']['status'])) {
840 $status = $this->params['named']['status'];
846 $conditions['Character.status'] = $status;
850 unset($conditions['Character.status']);
853 $this->set('selected_status', $status);
855 $characters = $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
857 $this->set('characters', $characters);
859 $this->set('title_for_layout', " - ". $title);
862 function _search($id, $conditions = array(), $limit = 20) {
863 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
869 if (!isset($this->search_cols['Character'])) {
870 $this->search_cols['Character'] = array();
873 $system_conditions['System.public_flag'] = 1;
874 if (isset($this->data['Character']['keyword']['system_id'])) {
875 if (!empty($this->data['Character']['keyword']['system_id'])) {
876 $this->passedArgs['system'] = $this->data['Character']['keyword']['system_id']['value'];
877 $system_id = $this->passedArgs['system'];
879 } elseif (isset($this->passedArgs['system'])) {
880 $system_id = $this->passedArgs['system'];
884 $systems = $this->_get_systems(1);
886 array(0 => __('All', true)) + $systems;
888 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
889 'system_id' => array(
890 'name' => __('System', true),
892 'options' => $systems,
893 'default' => $system_id,
897 if (isset($this->passedArgs['status'])) {
898 $status = $this->passedArgs['status'];
903 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
905 'name' => __('Status', true),
907 'options' => CorePlus::set_status($this->model_status2),
908 'default' => $status,
912 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
914 'name' => __('Charater Name', true),
918 if ($this->site_configs['Character.NotesSearch']['value']) {
919 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
921 'name' => __('Notes', true),
928 if (!empty($system_id)) {
929 $this_system = $this->_getThisSystem($system_id, $isAdmin);
931 $profiles = $this->Character->System->Profile->find('all', array(
936 'Profile.profile_type',
937 'Profile.sort_order',
939 'conditions' => array(
940 'Profile.system_id' => $system_id,
941 'Profile.search' => true,
946 foreach ($profiles as $k => $v) {
947 if (isset($v['Profile']['sort_order']) && !empty($v['Profile']['sort_order'])) {
948 $sort_order_profile[$v['Profile']['id']] = $v['Profile']['sort_order'];
950 $sort_order_profile[$v['Profile']['id']] = 0;
953 $prof[$v['Profile']['id']] = array(
954 'id' => $v['Profile']['id'],
955 'name' => $v['Profile']['name'],
956 // 'key_name' => $v['Profile']['key_name'],
957 // 'profile_type' => $v['Profile']['profile_type'],
960 if (!empty($sort_order_profile)) {
961 $prof = $this->sort4sort_order($prof, $sort_order_profile);
964 foreach ($prof as $k => $v) {
965 $this->search_cols['CharactersHasProfile'][$v['id']] = array(
966 'name' => $v['name'],
972 if (!empty($id) && ($this->user['User']['id'] == $id)) {
976 $this->set('search_cols', $this->search_cols);
978 if ($this->site_configs['Character.ListSearchNotes']['value'] || $this->site_configs['Character.NotesSearch']['value']) {
979 $this->fields = array_merge($this->fields, array('Character.notes'));
981 // $this->set('fields', $this->fields);
987 if (!empty($this->data)) {
988 if (isset($this->data['Character']['page']) && $this->data['Character']['page'] > 0) {
989 $page = $this->data['Character']['page'];
990 unset($this->data['Character']['page']);
994 $prev_type_set = false;
995 $new_condition = null;
996 foreach ($this->data as $model => $search_conditions) {
998 if (isset($search_conditions['keyword']) && !empty($search_conditions['keyword'])) {
999 foreach ($search_conditions['keyword'] as $field => $search_options) {
1002 if ($field == 'public_flag') {
1003 if (isset($conditions['isAdmin']) && !empty($conditions['isAdmin']) && $search_options['value'] == 'all') {
1004 unset($this->paginate['conditions']['Character.public_flag']);
1006 $conditions['Character.public_flag'] = $search_options['value'];
1007 $conditions['public_force'] = true;
1009 } elseif ($field == 'system_id') {
1010 if (empty($search_options['value'])) {
1011 if (isset($this->params['named']['system'])) {
1012 unset($this->params['named']['system']);
1015 $this->params['named']['system'] = $search_options['value'];
1017 } elseif ($field == 'status') {
1018 if ($search_options['value'] == 'all') {
1019 $this->params['named']['status'] = 'all';
1021 $this->params['named']['status'] = $search_options['value'];
1024 if (isset($search_options['value']) && !empty($search_options['value'])) {
1025 if (!empty($prev_type_set)) {
1026 $new_condition .= ' '. strtoupper($prev_type). ' ';
1027 $prev_type_set = false;
1030 if (isset($search_options['type']) && in_array(strtolower($search_options['type']), array('and', 'or'))) {
1031 $prev_type = strtoupper($search_options['type']);
1032 $prev_type_set = true;
1035 $new_condition .= $this->_create_search_sql($search_options['value'], $field, $model);
1039 case 'CharactersHasProfile':
1040 if (isset($search_options['value']) && !empty($search_options['value'])) {
1041 $conditions['profile_search'] = true;
1043 if (!empty($prev_type_set)) {
1044 $new_condition .= ' '. strtoupper($prev_type). ' ';
1045 $prev_type_set = false;
1048 if (isset($search_options['type']) && in_array(strtolower($search_options['type']), array('and', 'or'))) {
1049 $prev_type = strtoupper($search_options['type']);
1050 $prev_type_set = true;
1053 $new_condition .= $this->_create_search_sql($search_options['value'], 'value', $model, $field);
1057 $this->Session->setFlash(__('Invalid Data.', true));
1058 $this->redirect(array('action'=>'index'));
1065 if (empty($isAdmin)) {
1066 if (!empty($new_condition)) {
1067 $new_condition = '('. $new_condition. ')';
1069 if (isset($conditions['profile_search'])) {
1070 $new_condition .= ' AND';
1071 $new_condition .= ' CharactersHasProfile.public_flag = \'public\'';
1075 if (!empty($new_condition)) {
1076 $conditions['AND'][] = '('. $new_condition. ')';
1080 $this->_index($id, $conditions, $limit, array(), array(), array(), $page);
1083 function _view($id, $conditions = array(), $isAdmin = false) {
1084 $this->helpers[] = 'CharacterSheet';
1087 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
1088 $character = $this->Character->set_profiles2view($orig_character);
1090 Configure::write('isOwner', false);
1091 if ($isAdmin || CorePlus::isOwner($character['Character'], $this->user_id)) {
1092 Configure::write('isOwner', true);
1096 $this->set('systemValid', true);
1097 if (!$character['System']) {
1098 $this->set('systemValid', false);
1100 if (!$this->check_public_flag($character['System'])) {
1101 $this->set('systemValid', 'unpublic');
1103 /* if ($character['System']['set_npc']) {
1104 $this->isNpc = true;
1106 $character = $this->_restore_html_system($character);
1107 $character['System']['Profile'] = $this->_restore_html_characters_has_profiles($character['System']['Profile'], false);
1109 $this->set('title_for_layout', " - ". $character['Character']['name']);
1112 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
1113 $mode = $this->params['named']['mode'];
1116 $public_flg = false;
1120 foreach ($character['System']['CharacterSheet'] as $v) {
1121 if ($v['key_name'] == $mode) {
1130 $this->CharacterSheet = CorePlus::set_model('CharacterSheet');
1131 $characterSheet = $this->CharacterSheet->find('first', array(
1132 'conditions' => array('CharacterSheet.key_name' => $mode),
1136 if ($this->_checkCharaSheeOwner($characterSheet)) {
1137 $skin = $characterSheet['CharacterSheet'];
1145 $this->view = 'Theme';
1146 $this->theme = $mode;
1148 $this->set('skin', $skin);
1155 function _delete($id = null, $conditions = array(), $isAdmin = false) {
1157 $this->Session->setFlash(__('Invalid id for Character', true));
1158 $this->redirect(array('action'=>'index'));
1161 $character = $this->_view($id, $conditions, $isAdmin);
1162 if (empty($character)) {
1163 $this->Session->setFlash(__('No Character', true));
1164 $this->redirect(array('action'=>'index'));
1166 $this->set('character', $character);
1168 if (!$isAdmin && !$this->isOwner($character['Character'], $this->user_id)) {
1169 $this->Session->setFlash(__('No Permission', true));
1170 $this->redirect(array('action'=>'index'));
1172 $this->set('isOwner', true);
1174 if (!empty($this->data)) {
1176 $this->data['clearCache'] = array(
1177 'character_id' => $id,
1178 'user_id' => $this->user_id,
1179 'system_id' => $character['System']['id'],
1182 if ($this->data['Character']['confirm'] == 'yes') {
1184 $this->data['Character']['id'] = $id;
1185 $this->data['Character']['main_picture'] = null;
1186 $this->data['Character']['full_length'] = null;
1187 $this->data['Character']['deleted'] = true;
1188 $this->data['Character']['deleted_date'] = date('Y-m-d H:i:s');
1190 $this->Character->create();
1191 if ($this->Character->save(
1194 'validate' => false,
1195 'fieldList' => array(
1204 // CharactersHasProfiles
1205 $this->Character->CharactersHasProfile->updateAll(
1207 'CharactersHasProfile.public_flag' => "2",
1210 'CharactersHasProfile.character_id' => $id
1214 // CharacterProfileArchives
1215 $date = date('Y-m-d H:i:s');
1216 $this->Character->CharacterProfileArchive->updateAll(
1218 'CharacterProfileArchive.deleted' => true,
1219 'CharacterProfileArchive.deleted_date' => "'$date'",
1222 'CharacterProfileArchive.character_id' => $id
1227 if (isset($character['CharacterPicture']) && !empty($character['CharacterPicture'])) {
1228 foreach ($character['CharacterPicture'] as $picture) {
1229 $this->Character->CharacterPicture->delete($picture['id']);
1233 $this->Session->setFlash(__('Character deleted', true));
1237 $this->Session->setFlash(__('The Character could not be deleted.', true));
1246 function _get_character($id, $conditions = array(), $isAdmin = false)
1248 $character = $this->Character->get_character($id, $conditions, $isAdmin);
1250 if (empty($character)) {
1251 $this->Session->setFlash(__('No Character', true));
1252 $this->redirect(array('action'=>'index'));
1255 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
1256 $this->Session->setFlash(__('No Permission', true));
1257 $this->redirect(array('action'=>'index'));
1260 if (isset($character['System']['Profile'])) {
1261 $character['System'] = $this->_restore_html_profile($character['System']);
1267 // ProfileTableをCharacterHasProfilesのTableの形式にセット
1268 function _set_profile_table2characters_has_profiles($profile)
1270 if (!empty($profile[0]['CharactersHasProfile'])) {
1274 foreach($profile as $k => $v) {
1275 if (empty($v['ProfileTable'])) {
1280 $rows = count($v['CharactersHasProfile']);
1281 for($i=0; $i<$rows; $i++) {
1282 foreach($v['ProfileTable'] as $k2 => $v2) {
1283 if (isset($v2['CharactersHasProfile'][$i])) {
1284 $tmp[] = $v2['CharactersHasProfile'][$i];
1285 } elseif (isset($v['CharactersHasProfile'][$i])) {
1286 $tmp[] = $v['CharactersHasProfile'][$i];
1294 $profile[$k]['CharactersHasProfile'] = $tmp;
1296 if (isset($profile[$k]['ProfileTable'][0]['ProfileTableStatic']) && !empty($profile[$k]['ProfileTable'][0]['ProfileTableStatic'])) {
1297 $profile[$k]['ProfileTable'][0]['ProfileTableStatic'] = $this->_restore_html_profile_table_static($profile[$k]['ProfileTable'][0]['ProfileTableStatic']);
1303 // CharactersHasProfileの追加valiadte設定
1304 function _set_validate4characters_has_profile($system_id)
1306 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
1307 'rule' => array('validProfileId', $system_id),
1309 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
1310 'rule' => array('validProfileSelectId', $system_id),
1311 'allowEmpty' => true,
1313 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
1314 'rule' => array('validProfiletableId', $system_id),
1315 'allowEmpty' => true,
1320 function _restore_html_character($data, $nl2br = false) {
1321 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
1322 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
1323 $data['secret_notes'] = $this->{$this->modelClass}->restore_html($data['secret_notes'], false, false, false);
1325 $data['notes'] = str_replace('<br />', "\n", $data['notes']);
1326 $data['secret_notes'] = str_replace('<br />', "\n", $data['secret_notes']);
1332 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
1333 if (empty($profile)) {
1337 foreach($profile as $k => $v) {
1338 if (empty($v['CharactersHasProfile'])) {
1341 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
1346 function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
1351 foreach ($data as $k => $v) {
1352 if (isset($v['value'])) {
1353 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
1354 if ($nl2br && CorePlus::get_profile_type($profile_type) == 'textarea') {
1355 $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);
1362 function _checkRequiredProfile($profile)
1364 if (empty($profile) || !isset($this->data['CharactersHasProfile'])) {
1368 $requireCheck = array();
1369 foreach($this->data['CharactersHasProfile'] as $k => $v) {
1370 if (!isset($requireCheck[$v['profile_id']])) {
1371 $requireCheck[$v['profile_id']] = null;
1374 if (isset($requireCheck[$v['profile_id']]['value']) && !empty($requireCheck[$v['profile_id']]['value'])) {
1378 if (empty($v['value'])) {
1382 if (isset($v['profile_table_static_id']) && !empty($v['profile_table_static_id'])) {
1383 if (!isset($requireCheck[$v['profile_id']]['profile_table_id']) || empty($requireCheck[$v['profile_id']]['profile_table_id'])) {
1384 $requireCheck[$v['profile_id']]['profile_table_id'] = $v['profile_table_id'];
1387 if ($requireCheck[$v['profile_id']]['profile_table_id'] == $v['profile_table_id']) {
1392 $requireCheck[$v['profile_id']]['value'] = $v['value'];
1396 foreach ($profile as $k => $v) {
1397 if ($v['required']) {
1398 if (!isset($requireCheck[$v['id']]) || empty($requireCheck[$v['id']]['value'])) {
1399 $errors[] = $v['name'];
1405 $this->Session->setFlash(sprintf(__('%s is required.', true), implode(',', $errors)));
1412 function _create_search_sql($keywords, $field, $model, $profile_id = null)
1417 if (empty($keywords) || !is_string($keywords) || empty($field)) {
1421 if (!isset($this->search_cols[$model]) ||
1422 ((empty($profile_id) && !array_key_exists($field, $this->search_cols[$model]))
1423 || (!empty($profile_id) && !array_key_exists($profile_id, $this->search_cols[$model])))) {
1427 $keywords = rawurldecode($keywords);
1428 $searchwords = explode(",", $keywords);
1429 foreach ($searchwords as $k2 => $searchword) {
1430 if (empty($searchword)) {
1434 if (!empty($result)) {
1435 $result .= ' '. strtoupper($type). ' ';
1440 $result .= '('. $model.'.'.$field. ' LIKE '. '\'%'. $searchword. '%\'';
1441 if (!empty($profile_id)) {
1442 $result .= ' AND CharactersHasProfile.profile_id = '. $profile_id;