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 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
137 $systems = $this->_get_systems('public');
139 if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
140 $this->params['named']['system_id'] = key($systems);
143 if (isset($this->params['named']['system_id']) && !empty($this->params['named']['system_id'])) {
144 $system = $this->Character->System->find('first', array(
145 'conditions' => array(
146 'System.id' => $this->params['named']['system_id'],
152 'System.public_flag',
156 if (empty($system) || !$this->check_public_flag2($system['System'])) {
157 $this->Session->setFlash(__('Invalid ID.', true));
158 $this->redirect(array('action'=>'index'));
161 /* if (isset($system['System']) && !empty($system['System']) && $system['System']['set_npc']) {
165 if (!empty($this->data)) {
166 $this->data['Character']['user_id'] = $this->user_id;
167 if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
168 $this->data['Character']['system_id'] = key($systems);
171 $this->Character->create();
172 if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
174 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
175 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
177 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
181 $this->set('systems', $systems);
182 $this->set('isOwner', false);
184 $this->set('title_for_layout', " ". __('Add Character', true));
187 function edit($id = null) {
188 if (!$id && empty($this->data)) {
189 $this->Session->setFlash(__('Invalid ID.', true));
190 $this->redirect(array('action'=>'index'));
194 unset($this->Character->System->hasMany['Profile']['fields']);
195 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
196 unset($this->Character->hasMany['CharacterProfileArchive']);
198 $character = $this->_get_character($id, array(), false);
199 if (!$this->isOwner($character['Character'], $this->user_id)) {
200 $this->Session->setFlash(__('No Permission', true));
201 $this->redirect(array('action'=>'index'));
205 if (!$this->check_public_flag($character['System'])) {
206 $this->redirect(array('action'=>'change_system', $id));
208 /* if ($character['System']['set_npc']) {
212 if (empty($this->data)) {
213 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
216 if (!empty($this->data)) {
217 $this->post_data = $this->data;
219 $this->data['clearCache'] = array(
220 'character_id' => $id,
221 'user_id' => $this->user_id,
222 'system_id' => $character['System']['id'],
226 if (isset($this->data['CharactersHasProfile'])) {
227 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
231 $this->_set_validate4characters_has_profile($character['System']['id']);
233 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
235 $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
237 if ($requiredCheck) {
239 $this->Character->CharactersHasProfile->deleteAll(array(
240 'CharactersHasProfile.character_id' => $id
244 $this->data['Character']['id'] = $id;
245 $this->Character->create();
247 if ($this->Character->saveAll($this->data, array(
249 'fieldList' => array_merge(
250 $this->Character->fields['edit'],
251 $this->Character->CharactersHasProfile->fields['add']
256 if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
257 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
260 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
261 $this->redirect(array('action'=>'view', $id));
265 $validate_error = current(current(current($this->Character->validationErrors)));
266 $this->Session->setFlash($validate_error);
269 $this->data = array_merge($character, $this->post_data);
270 $this->data['Character'] = $this->post_data['Character'];
271 $this->data['Character']['main_picture'] = $character['Character']['main_picture'];
272 $this->data['Character']['system_id'] = $character['Character']['system_id'];
273 $this->data['Character']['id'] = $id;
274 foreach ($this->data['System']['Profile'] as $k1 => $profile) {
275 if ($profile['profile_type'] == 'table') {
276 $colum_num = count($profile['CharactersHasProfile']);
277 } elseif ($profile['profile_type'] == 's-table') {
278 $colum_num = count($profile['CharactersHasProfile']);
279 } elseif ($profile['profile_type'] == 'checkbox') {
280 $colum_num = count($profile['ProfileSelect']);
281 } elseif ($profile['profile_type'] == 'm-input') {
282 $colum_num = count($profile['CharactersHasProfile']);
287 if ($colum_num == 0) {
288 if ($profile['profile_type'] == 's-table') {
289 $colum_num = count($profile['ProfileTable']) * count($profile['ProfileTable'][0]['ProfileTableStatic']);
290 } elseif ($profile['profile_type'] == 'table') {
291 $colum_num = count($profile['ProfileTable']) * 4;
292 } elseif ($profile['profile_type'] == 'checkbox') {
293 $colum_num = count($profile['ProfileSelect']);
294 } elseif ($profile['profile_type'] == 'm-input') {
301 for ($i=0; $i<$colum_num; $i++) {
302 if (!isset($profile['CharactersHasProfile'][$i])) {
303 $profile['CharactersHasProfile'][$i] = array('character_id' => $character['Character']['id']);
306 $postdata = array_shift($this->post_data['CharactersHasProfile']);
308 if (is_array($postdata['value'])) {
309 $postdata['value'] = $postdata['value'][0];
312 if (isset($postdata[$i])) {
313 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], $postdata[$i]);
315 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], (array)$postdata);
321 elseif (empty($this->data)) {
322 $this->data = $character;
323 $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
326 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
328 $this->set('isOwner', true);
330 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
333 function add_milti_profiles($id = null) {
334 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
335 $this->Session->setFlash(__('Invalid ID.', true));
336 $this->redirect(array('action'=>'index'));
340 $this->Character->System->hasMany['Profile']['fields'] = '';
341 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
342 unset($this->Character->hasMany['CharacterProfileArchive']);
344 $character = $this->_get_character($id);
345 if (!$this->isOwner($character['Character'], $this->user_id)) {
346 $this->Session->setFlash(__('No Permission', true));
347 $this->redirect(array('action'=>'index'));
350 if (!$this->check_public_flag($character['System'])) {
351 $this->redirect(array('action'=>'change_system', $id));
355 if (empty($character['System']['Profile'])) {
356 $this->Session->setFlash(__('Invalid Profile.', true));
357 $this->redirect(array('action'=>'view', $id));
360 $profile_id = $this->params['named']['profile_id'];
363 foreach ($character['System']['Profile'] as $profile) {
364 if ($profile['id'] == $profile_id) {
371 $this->Session->setFlash(__('No Profile.', true));
372 $this->redirect(array('action'=>'view', $id));
375 $character['System']['Profile'] = array();
376 $character['System']['Profile'][0] = $target;
378 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
380 if (!empty($this->data)) {
381 $this->data['clearCache'] = array(
382 'character_id' => $id,
383 'user_id' => $this->user_id,
384 'system_id' => $character['System']['id'],
387 $this->post_data = $this->data;
388 $this->data = array();
389 if (isset($this->post_data['Character'])) {
390 $this->data['Character'] = array_merge($character['Character'], $this->post_data['Character']);
392 $this->data['Character'] = $character['Character'];
394 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
395 $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
396 $this->data['Character']['modified'] = null;
399 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->post_data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
401 $this->_set_validate4characters_has_profile($character['System']['id']);
402 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
404 $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
406 if ($requiredCheck) {
408 $this->Character->CharactersHasProfile->deleteAll(array(
409 'CharactersHasProfile.profile_id' => $target['id'],
410 'CharactersHasProfile.character_id' => $id
414 $this->Character->create();
415 if ($this->Character->saveAll($this->data, array(
417 'fieldList' => array_merge(
418 $this->Character->CharactersHasProfile->fields['add']
423 if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
424 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
427 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
428 $this->redirect(array('action'=>'view', $id));
430 $this->data = array_merge($character, $this->data);
435 $validate_error = current(current(current($this->Character->validationErrors)));
436 $this->Session->setFlash($validate_error);
439 $this->data = array_merge($character, $this->data);
440 $this->data['Character']['id'] = $id;
441 unset($this->data['Character']['__Token']);
445 if (empty($this->data)) {
446 $this->data = $character;
448 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
450 $this->set('isOwner', true);
452 if (isset($this->params['named']['num'])) {
453 $form_nums = $this->params['named']['num'];
457 $this->set('form_nums', $form_nums);
459 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
462 function change_system($id = null) {
464 $this->Session->setFlash(__('Invalid ID.', true));
465 $this->redirect(array('action'=>'index'));
469 $this->Character->System->hasMany['Profile']['fields'] = '';
470 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
472 $character = $this->_get_character($id);
473 if (!$this->isOwner($character['Character'], $this->user_id)) {
474 $this->Session->setFlash(__('No Permission', true));
475 $this->redirect(array('action'=>'index'));
480 if (isset($character['System']) && !empty($character['System'])) {
482 if ($this->isOtherSystem($character['System']['id'])) {
483 $this->set('isChange', true);
485 } elseif ($this->check_public_flag($character['System'])) {
486 $this->redirect(array('action'=>'edit', $id));
489 $this->set('isChange', true);
493 if (!empty($this->data)) {
494 $this->data['clearCache'] = array(
495 'character_id' => $id,
496 'user_id' => $this->user_id,
497 'system_id' => $character['System']['id'],
500 $this->data['Character']['id'] = $id;
502 // validateはsystem_idのみ
503 $this->Cahracter['validate'] = array(
504 'system_id' => array(
505 'validSystemId' => array(
506 'rule' => array('validSystemId', true),
507 'allowEmpty' => false,
512 if ($this->Character->save(
516 'fieldList' => array('system_id'),
519 $this->Session->setFlash(__('The new system has been saved', true));
520 $this->redirect(array('action'=>'view', $id));
522 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
526 $this->set('isOwner', true);
527 $this->set('character', $character);
529 $systems = $this->_get_systems('public');
530 $this->set('systems', $systems);
532 $this->set('title_for_layout', " ". sprintf(__('%s Change System', true), $character['Character']['name']));
536 function set_status($id = null) {
537 if (!$id || !isset($this->params['named']['mode'])) {
538 $this->Session->setFlash(__('Invalid ID.', true));
539 $this->redirect(array('controller' => 'characters', 'action'=>'index'));
542 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
544 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
545 $this->Session->setFlash(__('Invalid URL.', true));
546 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
550 $new_character_picture = null;
551 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
552 if ($character['CharacterPicture']) {
553 foreach($character['CharacterPicture'] as $k => $v) {
554 if ($v['id'] == $this->params['named']['new_picture_id']) {
555 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
560 if (!$new_character_picture) {
561 $this->Session->setFlash(__('Invalid data.', true));
562 $this->redirect(array('controller' => 'character_pictures', 'action'=>'listview', $id));
567 $this->data['clearCache'] = array(
568 'character_id' => $id,
569 'user_id' => $this->user_id,
570 'system_id' => $character['Character']['system_id'],
574 $this->Character->id = $id;
575 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
576 $this->Character->save(
579 'fieldList' => array(
580 $this->params['named']['mode']
585 $this->Character->deleteCache4CharacterPicture($id);
588 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
589 $this->redirect(array('controller' => 'character_pictures', 'action' => 'listview', $id));
592 function delete($id = null) {
594 $this->Session->setFlash(__('Invalid id for Character', true));
595 $this->redirect(array('action'=>'index'));
598 if ($this->_delete($id)) {
599 $this->redirect(array('controller' => 'users', 'action'=>'index'));
602 $this->set('title_for_layout', " - ". __('Delete Character', true));
605 function admin_index($id = null) {
607 $this->_index($id, array('isAdmin' => true));
610 function admin_search($id = null) {
611 $this->search_cols['Character']['public_flag'] = array(
612 'name' => __('Public Flag', true),
614 'options' => CorePlus::set_publicflag($this->model_public_flags, array('all')),
615 'default' => 'public',
618 $this->_search($id, array('isAdmin' => true));
621 function admin_view($id = null) {
623 $this->Session->setFlash(__('Invalid Character.', true));
624 $this->redirect(array('action' => 'admin_index'));
627 $character = $this->_view($id, array(), true);
629 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
632 function admin_edit($id = null) {
633 if (!$id && empty($this->data)) {
634 $this->Session->setFlash(__('Invalid ID.', true));
635 $this->redirect(array('action'=>'index'));
639 $this->Character->System->hasMany['Profile']['fields'] = '';
640 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
641 unset($this->Character->hasMany['CharacterProfileArchive']);
643 $character = $this->_get_character($id, array(), true);
645 $this->Session->setFlash(__('No Character', true));
646 $this->redirect(array('action'=>'index'));
649 if (empty($this->data)) {
650 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
653 if (!empty($this->data)) {
655 $this->data['clearCache'] = array(
656 'character_id' => $id,
657 'user_id' => $this->user_id,
658 'system_id' => $character['System']['id'],
661 if (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_private') {
662 $this->data['Character']['public_flag'] = 'private';
663 } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_public') {
664 $this->data['Character']['public_flag'] = 'public';
665 } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'delete_confirm') {
666 $this->redirect(array('controller' => 'characters', 'action'=>'admin_delete', $id));
668 $this->Session->setFlash(sprintf(__('Invalid data.', true), $this->data['Character']['name']));
669 $this->redirect(array('action'=>'view', $id));
672 $this->data['Character']['id'] = $id;
673 $this->Character->create();
674 if ($this->Character->save($this->data, array(
676 'fieldList' => array(
681 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $character['Character']['name']));
682 $this->redirect(array('action'=>'view', $id));
685 $this->data = array_merge($character, $this->data);
686 $this->data['Character']['id'] = $id;
689 if (empty($this->data)) {
690 $this->data = $character;
691 $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
693 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
696 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
699 function admin_delete($id = null) {
701 $this->Session->setFlash(__('Invalid id for Character', true));
702 $this->redirect(array('action'=>'index'));
705 if ($this->_delete($id, array(), true)) {
706 $this->redirect(array('action'=>'index'));
709 $this->set('title_for_layout', " - ". __('Delete Character', true));
714 function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
715 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
717 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
722 $title = __('List of All Characters', true);
725 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
727 $this_system = $this->_getThisSystem($this->params['named']['system'], $isAdmin);
729 $conditions['Character.system_id'] = $this->params['named']['system'];
732 $profiles = $this->Character->System->Profile->find('all', array(
737 'Profile.profile_type',
738 'Profile.sort_order',
740 'conditions' => array(
741 'Profile.system_id' => $this->params['named']['system'],
742 'Profile.show_list' => true,
747 foreach ($profiles as $k => $v) {
748 if (isset($v['Profile']['sort_order']) && !empty($v['Profile']['sort_order'])) {
749 $sort_order_profile[$v['Profile']['id']] = $v['Profile']['sort_order'];
751 $sort_order_profile[$v['Profile']['id']] = 0;
754 $prof[$v['Profile']['id']] = array(
755 'id' => $v['Profile']['id'],
756 'name' => $v['Profile']['name'],
757 'key_name' => $v['Profile']['key_name'],
758 'profile_type' => $v['Profile']['profile_type'],
761 if (!empty($sort_order_profile)) {
762 $prof = $this->sort4sort_order($prof, $sort_order_profile);
765 foreach ($prof as $k => $v) {
766 $this->showlist_cols['Profile'][] = $v['id'];
769 $this_system['Profile'] = $prof;
771 $this_system = $this->_restore_html_system($this_system);
773 /* if ($this_system['System']['set_npc']) {
777 $title = $this_system['System']['name']. " ". $title;
779 $this->set('this_system', $this_system);
781 $contain = array_merge(array('System'), (array)$contain);
783 // $this->isNpc = true;
788 $user = $this->Character->User->find('first', array(
789 'conditions' => array('User.id' => $id),
798 $this->Session->setFlash(__('Invalid Id.', true));
799 $this->redirect(array('action'=>'index'));
802 $conditions['Character.user_id'] = $id;
803 if ($id == $this->user_id) {
804 unset($this->paginate['conditions']['Character.public_flag']);
807 $title = $user['User']['name']. " ". $title;
809 $contain = array_merge($contain, array('User'));
811 $this->set('userSet', $user);
814 if (!isset($this->params['named']['status'])) {
817 $status = $this->params['named']['status'];
823 $conditions['Character.status'] = $status;
827 unset($conditions['Character.status']);
830 $this->set('selected_status', $status);
832 $characters = $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
834 $this->set('characters', $characters);
836 $this->set('title_for_layout', " - ". $title);
839 function _search($id, $conditions = array(), $limit = 20) {
840 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
846 if (!isset($this->search_cols['Character'])) {
847 $this->search_cols['Character'] = array();
850 $system_conditions['System.public_flag'] = 'public';
851 if (isset($this->data['Character']['keyword']['system_id'])) {
852 if (!empty($this->data['Character']['keyword']['system_id'])) {
853 $this->passedArgs['system'] = $this->data['Character']['keyword']['system_id']['value'];
854 $system_id = $this->passedArgs['system'];
856 } elseif (isset($this->passedArgs['system'])) {
857 $system_id = $this->passedArgs['system'];
861 $systems = $this->_get_systems('public');
862 $systems = array_merge(array(
863 '0' => __('All', true),
866 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
867 'system_id' => array(
868 'name' => __('System', true),
870 'options' => $systems,
871 'default' => $system_id,
875 if (isset($this->passedArgs['status'])) {
876 $status = $this->passedArgs['status'];
881 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
883 'name' => __('Status', true),
885 'options' => CorePlus::set_status($this->model_status2),
886 'default' => $status,
890 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
892 'name' => __('Charater Name', true),
896 if ($this->site_configs['Character.NotesSearch']['value']) {
897 $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
899 'name' => __('Notes', true),
906 if (!empty($system_id)) {
907 $this_system = $this->_getThisSystem($system_id, $isAdmin);
909 $profiles = $this->Character->System->Profile->find('all', array(
914 'Profile.profile_type',
915 'Profile.sort_order',
917 'conditions' => array(
918 'Profile.system_id' => $system_id,
919 'Profile.search' => true,
924 foreach ($profiles as $k => $v) {
925 if (isset($v['Profile']['sort_order']) && !empty($v['Profile']['sort_order'])) {
926 $sort_order_profile[$v['Profile']['id']] = $v['Profile']['sort_order'];
928 $sort_order_profile[$v['Profile']['id']] = 0;
931 $prof[$v['Profile']['id']] = array(
932 'id' => $v['Profile']['id'],
933 'name' => $v['Profile']['name'],
934 // 'key_name' => $v['Profile']['key_name'],
935 // 'profile_type' => $v['Profile']['profile_type'],
938 if (!empty($sort_order_profile)) {
939 $prof = $this->sort4sort_order($prof, $sort_order_profile);
942 foreach ($prof as $k => $v) {
943 $this->search_cols['CharactersHasProfile'][$v['id']] = array(
944 'name' => $v['name'],
950 if (!empty($id) && ($this->user['User']['id'] == $id)) {
954 $this->set('search_cols', $this->search_cols);
956 if ($this->site_configs['Character.ListSearchNotes']['value'] || $this->site_configs['Character.NotesSearch']['value']) {
957 $this->fields = array_merge($this->fields, array('Character.notes'));
959 // $this->set('fields', $this->fields);
965 if (!empty($this->data)) {
966 if (isset($this->data['Character']['page']) && $this->data['Character']['page'] > 0) {
967 $page = $this->data['Character']['page'];
968 unset($this->data['Character']['page']);
972 $prev_type_set = false;
973 $new_condition = null;
974 foreach ($this->data as $model => $search_conditions) {
976 if (isset($search_conditions['keyword']) && !empty($search_conditions['keyword'])) {
977 foreach ($search_conditions['keyword'] as $field => $search_options) {
980 if ($field == 'public_flag') {
981 if (isset($conditions['isAdmin']) && !empty($conditions['isAdmin']) && $search_options['value'] == 'all') {
982 unset($this->paginate['conditions']['Character.public_flag']);
984 $conditions['Character.public_flag'] = $search_options['value'];
985 $conditions['public_force'] = true;
987 } elseif ($field == 'system_id') {
988 if (empty($search_options['value'])) {
989 if (isset($this->params['named']['system'])) {
990 unset($this->params['named']['system']);
993 $this->params['named']['system'] = $search_options['value'];
995 } elseif ($field == 'status') {
996 if ($search_options['value'] == 'all') {
997 $this->params['named']['status'] = 'all';
999 $this->params['named']['status'] = $search_options['value'];
1002 if (isset($search_options['value']) && !empty($search_options['value'])) {
1003 if (!empty($prev_type_set)) {
1004 $new_condition .= ' '. strtoupper($prev_type). ' ';
1005 $prev_type_set = false;
1008 if (isset($search_options['type']) && in_array(strtolower($search_options['type']), array('and', 'or'))) {
1009 $prev_type = strtoupper($search_options['type']);
1010 $prev_type_set = true;
1013 $new_condition .= $this->_create_search_sql($search_options['value'], $field, $model);
1017 case 'CharactersHasProfile':
1018 if (isset($search_options['value']) && !empty($search_options['value'])) {
1019 $conditions['profile_search'] = true;
1021 if (!empty($prev_type_set)) {
1022 $new_condition .= ' '. strtoupper($prev_type). ' ';
1023 $prev_type_set = false;
1026 if (isset($search_options['type']) && in_array(strtolower($search_options['type']), array('and', 'or'))) {
1027 $prev_type = strtoupper($search_options['type']);
1028 $prev_type_set = true;
1031 $new_condition .= $this->_create_search_sql($search_options['value'], 'value', $model, $field);
1035 $this->Session->setFlash(__('Invalid Data.', true));
1036 $this->redirect(array('action'=>'index'));
1043 if (empty($isAdmin)) {
1044 if (!empty($new_condition)) {
1045 $new_condition = '('. $new_condition. ')';
1046 $new_condition .= ' AND';
1048 if (isset($conditions['profile_search'])) {
1049 $new_condition .= ' CharactersHasProfile.public_flag = \'public\'';
1053 if (!empty($new_condition)) {
1054 $conditions['AND'][] = '('. $new_condition. ')';
1058 $this->_index($id, $conditions, $limit, array(), array(), array(), $page);
1061 function _view($id, $conditions = array(), $isAdmin = false) {
1062 $this->helpers[] = 'CharacterSheet';
1065 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
1066 $character = $this->Character->set_profiles2view($orig_character);
1068 Configure::write('isOwner', false);
1069 if ($isAdmin || CorePlus::isOwner($character['Character'], $this->user_id)) {
1070 Configure::write('isOwner', true);
1074 $this->set('systemValid', 'public');
1075 if (!$character['System']) {
1076 $this->set('systemValid', false);
1078 if (!$this->check_public_flag($character['System'])) {
1079 $this->set('systemValid', 'unpublic');
1081 /* if ($character['System']['set_npc']) {
1082 $this->isNpc = true;
1084 $character = $this->_restore_html_system($character);
1085 $character['System']['Profile'] = $this->_restore_html_characters_has_profiles($character['System']['Profile'], false);
1087 $this->set('title_for_layout', " - ". $character['Character']['name']);
1090 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
1091 $mode = $this->params['named']['mode'];
1094 $public_flg = false;
1098 foreach ($character['System']['CharacterSheet'] as $v) {
1099 if ($v['key_name'] == $mode) {
1108 $this->CharacterSheet = CorePlus::set_model('CharacterSheet');
1109 $characterSheet = $this->CharacterSheet->find('first', array(
1110 'conditions' => array('CharacterSheet.key_name' => $mode),
1114 if ($this->_checkCharaSheeOwner($characterSheet)) {
1115 $skin = $characterSheet['CharacterSheet'];
1123 $this->view = 'Theme';
1124 $this->theme = $mode;
1126 $this->set('skin', $skin);
1133 function _delete($id = null, $conditions = array(), $isAdmin = false) {
1135 $this->Session->setFlash(__('Invalid id for Character', true));
1136 $this->redirect(array('action'=>'index'));
1139 $character = $this->_view($id, $conditions, $isAdmin);
1140 if (empty($character)) {
1141 $this->Session->setFlash(__('No Character', true));
1142 $this->redirect(array('action'=>'index'));
1144 $this->set('character', $character);
1146 if (!$isAdmin && !$this->isOwner($character['Character'], $this->user_id)) {
1147 $this->Session->setFlash(__('No Permission', true));
1148 $this->redirect(array('action'=>'index'));
1150 $this->set('isOwner', true);
1152 if (!empty($this->data)) {
1154 $this->data['clearCache'] = array(
1155 'character_id' => $id,
1156 'user_id' => $this->user_id,
1157 'system_id' => $character['System']['id'],
1160 if ($this->data['Character']['confirm'] == 'yes') {
1162 $this->data['Character']['id'] = $id;
1163 $this->data['Character']['main_picture'] = null;
1164 $this->data['Character']['full_length'] = null;
1165 $this->data['Character']['deleted'] = true;
1166 $this->data['Character']['deleted_date'] = date('Y-m-d H:i:s');
1168 $this->Character->create();
1169 if ($this->Character->save(
1172 'validate' => false,
1173 'fieldList' => array(
1182 // CharactersHasProfiles
1183 $this->Character->CharactersHasProfile->updateAll(
1185 'CharactersHasProfile.public_flag' => "'private'",
1188 'CharactersHasProfile.character_id' => $id
1192 // CharacterProfileArchives
1193 $date = date('Y-m-d H:i:s');
1194 $this->Character->CharacterProfileArchive->updateAll(
1196 'CharacterProfileArchive.deleted' => true,
1197 'CharacterProfileArchive.deleted_date' => "'$date'",
1200 'CharacterProfileArchive.character_id' => $id
1205 if (isset($character['CharacterPicture']) && !empty($character['CharacterPicture'])) {
1206 foreach ($character['CharacterPicture'] as $picture) {
1207 $this->Character->CharacterPicture->delete($picture['id']);
1211 $this->Session->setFlash(__('Character deleted', true));
1215 $this->Session->setFlash(__('The Character could not be deleted.', true));
1224 function _get_character($id, $conditions = array(), $isAdmin = false)
1226 $character = $this->Character->get_character($id, $conditions, $isAdmin);
1228 if (empty($character)) {
1229 $this->Session->setFlash(__('No Character', true));
1230 $this->redirect(array('action'=>'index'));
1233 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
1234 $this->Session->setFlash(__('No Permission', true));
1235 $this->redirect(array('action'=>'index'));
1238 if (isset($character['System']['Profile'])) {
1239 $character['System'] = $this->_restore_html_profile($character['System']);
1245 // ProfileTableをCharacterHasProfilesのTableの形式にセット
1246 function _set_profile_table2characters_has_profiles($profile)
1248 if (!empty($profile[0]['CharactersHasProfile'])) {
1252 foreach($profile as $k => $v) {
1253 if (empty($v['ProfileTable'])) {
1258 $rows = count($v['CharactersHasProfile']);
1259 for($i=0; $i<$rows; $i++) {
1260 foreach($v['ProfileTable'] as $k2 => $v2) {
1261 if (isset($v2['CharactersHasProfile'][$i])) {
1262 $tmp[] = $v2['CharactersHasProfile'][$i];
1263 } elseif (isset($v['CharactersHasProfile'][$i])) {
1264 $tmp[] = $v['CharactersHasProfile'][$i];
1272 $profile[$k]['CharactersHasProfile'] = $tmp;
1274 if (isset($profile[$k]['ProfileTable'][0]['ProfileTableStatic']) && !empty($profile[$k]['ProfileTable'][0]['ProfileTableStatic'])) {
1275 $profile[$k]['ProfileTable'][0]['ProfileTableStatic'] = $this->_restore_html_profile_table_static($profile[$k]['ProfileTable'][0]['ProfileTableStatic']);
1281 // CharactersHasProfileの追加valiadte設定
1282 function _set_validate4characters_has_profile($system_id)
1284 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
1285 'rule' => array('validProfileId', $system_id),
1287 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
1288 'rule' => array('validProfileSelectId', $system_id),
1289 'allowEmpty' => true,
1291 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
1292 'rule' => array('validProfiletableId', $system_id),
1293 'allowEmpty' => true,
1298 function _restore_html_character($data, $nl2br = false) {
1299 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
1300 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
1301 $data['secret_notes'] = $this->{$this->modelClass}->restore_html($data['secret_notes'], false, false, false);
1303 $data['notes'] = str_replace('<br />', "\n", $data['notes']);
1304 $data['secret_notes'] = str_replace('<br />', "\n", $data['secret_notes']);
1310 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
1311 if (empty($profile)) {
1315 foreach($profile as $k => $v) {
1316 if (empty($v['CharactersHasProfile'])) {
1319 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
1324 function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
1329 foreach ($data as $k => $v) {
1330 if (isset($v['value'])) {
1331 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
1332 if ($nl2br && $profile_type == 'textarea') {
1333 $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);
1340 function _checkRequiredProfile($profile)
1342 if (empty($profile) || !isset($this->data['CharactersHasProfile'])) {
1346 $requireCheck = array();
1347 foreach($this->data['CharactersHasProfile'] as $k => $v) {
1348 if (!isset($requireCheck[$v['profile_id']])) {
1349 $requireCheck[$v['profile_id']] = null;
1352 if (isset($requireCheck[$v['profile_id']]['value']) && !empty($requireCheck[$v['profile_id']]['value'])) {
1356 if (empty($v['value'])) {
1360 if (isset($v['profile_table_static_id']) && !empty($v['profile_table_static_id'])) {
1361 if (!isset($requireCheck[$v['profile_id']]['profile_table_id']) || empty($requireCheck[$v['profile_id']]['profile_table_id'])) {
1362 $requireCheck[$v['profile_id']]['profile_table_id'] = $v['profile_table_id'];
1365 if ($requireCheck[$v['profile_id']]['profile_table_id'] == $v['profile_table_id']) {
1370 $requireCheck[$v['profile_id']]['value'] = $v['value'];
1374 foreach ($profile as $k => $v) {
1375 if ($v['required']) {
1376 if (!isset($requireCheck[$v['id']]) || empty($requireCheck[$v['id']]['value'])) {
1377 $errors[] = $v['name'];
1383 $this->Session->setFlash(sprintf(__('%s is required.', true), implode(',', $errors)));
1390 function _create_search_sql($keywords, $field, $model, $profile_id = null)
1395 if (empty($keywords) || !is_string($keywords) || empty($field)) {
1399 if (!isset($this->search_cols[$model]) ||
1400 ((empty($profile_id) && !array_key_exists($field, $this->search_cols[$model]))
1401 || (!empty($profile_id) && !array_key_exists($profile_id, $this->search_cols[$model])))) {
1405 $keywords = rawurldecode($keywords);
1406 $searchwords = explode(",", $keywords);
1407 foreach ($searchwords as $k2 => $searchword) {
1408 if (empty($searchword)) {
1412 if (!empty($result)) {
1413 $result .= ' '. strtoupper($type). ' ';
1418 $result .= '('. $model.'.'.$field. ' LIKE '. '\'%'. $searchword. '%\'';
1419 if (!empty($profile_id)) {
1420 $result .= ' AND CharactersHasProfile.profile_id = '. $profile_id;