3 class CharactersController extends AppController {
5 var $name = 'Characters';
17 var $actionMapPlus = array(
18 'add_milti_profiles' => 'update',
19 'change_system' => 'update',
20 'set_status' => 'update',
23 var $disableTokenActions = array();
24 var $post_data = array();
28 function beforeFilter() {
30 parent::beforeFilter();
33 $this->AuthPlus->allow('view');
34 $this->AuthPlus->allow('index');
37 function beforeRender()
39 parent::beforeRender();
41 $this->set_public_flag4view();
42 $this->set_status4view();
48 function index($id = null) {
52 function view($id = null) {
53 $this->view = 'Theme';
54 $this->theme = 'example';
57 $this->Session->setFlash(__('Invalid Character.', true));
58 $this->redirect(array('action'=>'index'));
62 'Character.id' => $id,
63 'Character.deleted' => 0,
66 $character = $this->_view($id, $conditions, false);
68 if (!$this->check_public_flag($character['System'])) {
69 unset($character['System']['Profile']);
72 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
75 if ($this->isOwner($character['Character'], $this->user_id)) {
78 $this->set('isOwner', $isOwner);
80 // キャラ画像のpublic_flagチェック
81 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
82 foreach($character['CharacterPicture'] as $k => $v) {
83 if (!$this->check_public_flag2($v)) {
84 unset($character['CharacterPicture'][$k]);
89 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
93 $systems = $this->_get_systems();
95 if (!empty($this->data)) {
96 $this->data['Character']['user_id'] = $this->user_id;
98 $this->Character->create();
99 if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
100 $this->Session->setFlash(__('The Character has been saved', true));
101 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
103 $this->Session->setFlash(__('The Character could not be saved. Please, try again.', true));
107 $this->set('systems', $systems);
108 $this->set('isOwner', false);
111 function edit($id = null) {
112 if (!$id && empty($this->data)) {
113 $this->Session->setFlash(__('Invalid ID.', true));
114 $this->redirect(array('action'=>'index'));
119 'Character.id' => $id,
120 'Character.deleted' => 0,
122 $this->Character->System->hasMany['Profile']['fields'] = '';
123 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
125 $character = $this->_get_character($id, $conditions);
126 if (!$this->isOwner($character['Character'], $this->user_id)) {
127 $this->Session->setFlash(__('No Permission', true));
128 $this->redirect(array('action'=>'index'));
132 if (!$this->check_public_flag($character['System'])) {
133 $this->redirect(array('action'=>'change_system', $id));
136 if (empty($this->data)) {
137 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
140 if (!empty($this->data)) {
141 $this->post_data = $this->data;
144 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
147 $this->_set_validate4characters_has_profile($character['System']['id']);
150 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
153 $this->Character->CharactersHasProfile->deleteAll(array(
154 'CharactersHasProfile.character_id' => $id
159 $this->data['Character']['id'] = $id;
160 $this->Character->create();
162 if ($this->Character->saveAll($this->data, array(
164 'fieldList' => array_merge(
165 $this->Character->fields['edit'],
166 $this->Character->CharactersHasProfile->fields['add']
169 $this->Session->setFlash(__('The Character has been saved.', true));
171 $this->redirect(array('action'=>'view', $id));
175 $this->data = $character;
176 $this->data['Character'] = $this->post_data['Character'];
177 $this->data['Character']['id'] = $id;
179 foreach($this->data['System']['Profile'] as $k1 => $profile) {
180 foreach($profile['CharactersHasProfile'] as $k2 => $v) {
181 $postdata = array_shift($this->post_data['CharactersHasProfile']);
182 if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
183 array_unshift($this->post_data['CharactersHasProfile'], $postdata);
186 if (is_array($postdata['value'])) {
187 $postdata['value'] = $postdata['value'][0];
189 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
190 if (isset($postdata['profile_select_id'])) {
191 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
197 if (empty($this->data)) {
198 $this->data = $character;
199 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
201 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
203 $this->set('isOwner', true);
206 function add_milti_profiles($id = null) {
207 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
208 $this->Session->setFlash(__('Invalid ID.', true));
209 $this->redirect(array('action'=>'index'));
214 'Character.id' => $id,
215 'Character.deleted' => 0,
217 $this->Character->System->hasMany['Profile']['fields'] = '';
218 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
220 $character = $this->_get_character($id, $conditions);
221 if (!$this->isOwner($character['Character'], $this->user_id)) {
222 $this->Session->setFlash(__('No Permission', true));
223 $this->redirect(array('action'=>'index'));
226 if (!$this->check_public_flag($character['System'])) {
227 $this->redirect(array('action'=>'change_system', $id));
231 if (empty($character['System']['Profile'])) {
232 $this->Session->setFlash(__('Invalid Profile.', true));
233 $this->redirect(array('action'=>'view', $id));
236 $profile_id = $this->params['named']['profile_id'];
238 if (empty($this->data)) {
240 foreach ($character['System']['Profile'] as $profile) {
241 if ($profile['id'] == $profile_id) {
248 $this->Session->setFlash(__('No Profile.', true));
249 $this->redirect(array('action'=>'view', $id));
252 $character['System']['Profile'] = array();
253 $character['System']['Profile'][0] = $target;
255 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
258 if (!empty($this->data)) {
259 $this->post_data = $this->data;
261 $this->data['Character'] = $character['Character'];
262 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
263 $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
264 $this->data['Character']['modified'] = null;
267 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
270 $this->_set_validate4characters_has_profile($character['System']['id']);
273 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
276 $this->Character->CharactersHasProfile->deleteAll(array(
277 'CharactersHasProfile.character_id' => $id
281 $this->Character->create();
282 if ($this->Character->saveAll($this->data, array(
284 'fieldList' => array_merge(
285 $this->Character->CharactersHasProfile->fields['add']
288 $this->Session->setFlash(__('The Character has been saved.', true));
290 $this->redirect(array('action'=>'view', $id));
294 $this->data = $character;
295 $this->data['Character'] = $this->post_data['Character'];
296 $this->data['Character']['id'] = $id;
300 if (empty($this->data)) {
301 $this->data = $character;
302 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
305 $this->set('isOwner', true);
308 function change_system($id = null) {
310 $this->Session->setFlash(__('Invalid ID.', true));
311 $this->redirect(array('action'=>'index'));
316 'Character.id' => $id,
317 'Character.deleted' => 0,
319 $this->Character->System->hasMany['Profile']['fields'] = '';
320 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
322 $character = $this->_get_character($id, $conditions);
323 if (!$this->isOwner($character['Character'], $this->user_id)) {
324 $this->Session->setFlash(__('No Permission', true));
325 $this->redirect(array('action'=>'index'));
329 if (isset($character['System']) && !empty($character['System'])) {
331 if ($this->check_public_flag($character['System'])) {
332 $this->redirect(array('action'=>'edit', $id));
335 $this->set('isChange', true);
339 if (!empty($this->data)) {
340 $this->data['Character']['id'] = $id;
342 // validateはsystem_idのみ
343 $this->Cahracter['validate'] = array(
344 'system_id' => array(
345 'validSystemId' => array(
346 'rule' => array('validSystemId', true),
347 'allowEmpty' => false,
352 if ($this->Character->save(
356 'fieldList' => array('system_id'),
359 $this->Session->setFlash(__('The new system has been saved', true));
361 $this->redirect(array('action'=>'view', $id));
363 $this->Session->setFlash(__('The new system could not be saved. Please, try again.', true));
367 $this->set('isOwner', true);
368 $this->set('character', $character);
370 $systems = $this->_get_systems();
371 $this->set('systems', $systems);
375 function set_status($id = null) {
376 if (!$id || !isset($this->params['named']['mode'])) {
377 $this->Session->setFlash(__('Invalid ID.', true));
378 $this->redirect(array('controller' => 'characters', 'action'=>'index'));
381 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
383 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
384 $this->Session->setFlash(__('Invalid URL.', true));
385 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
389 $new_character_picture = null;
390 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
391 if ($character['CharacterPicture']) {
392 foreach($character['CharacterPicture'] as $k => $v) {
393 if ($v['id'] == $this->params['named']['new_picture_id']) {
394 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
399 if (!$new_character_picture) {
400 $this->Session->setFlash(__('Invalid data.', true));
401 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
406 $this->Character->id = $id;
407 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
408 $this->Character->save(
411 'fieldList' => array(
412 $this->params['named']['mode']
417 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
418 $this->redirect(array('controller' => 'character_pictures', 'action' => 'index', $id));
421 function delete($id = null) {
423 $this->Session->setFlash(__('Invalid id for Character', true));
424 $this->redirect(array('action'=>'index'));
427 $character = $this->Character->read(null, $id);
428 if (empty($character)) {
429 $this->Session->setFlash(__('No Character', true));
430 $this->redirect(array('action'=>'index'));
433 if (!$this->isOwner($character['Character'], $this->user_id)) {
434 $this->Session->setFlash(__('No Permission', true));
435 $this->redirect(array('action'=>'index'));
438 if ($this->Character->del($id)) {
439 $this->Session->setFlash(__('Character deleted', true));
440 $this->redirect(array('action'=>'index'));
443 $this->set('isOwner', true);
447 function admin_index($id = null) {
448 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
449 $this->_index($id, array('isAdmin' => true));
452 function admin_view($id = null) {
454 $this->Session->setFlash(__('Invalid Character.', true));
455 $this->redirect(array('action' => 'admin_index'));
459 'Character.id' => $id,
460 'Character.deleted' => 0,
463 $character = $this->_view($id, $conditions, true);
465 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
468 function admin_delete($id = null) {
470 $this->Session->setFlash(__('Invalid id for Character', true));
471 $this->redirect(array('action'=>'index'));
473 if ($this->Character->del($id)) {
474 $this->Session->setFlash(__('Character deleted', true));
475 $this->redirect(array('action'=>'index'));
481 function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
482 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
487 unset($conditions['isAdmin']);
490 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
492 $conditions['System.id'] = $this->params['named']['system'];
493 if ($isAdmin === false) {
494 $conditions['System.public_flag'] = 'public';
497 $this_system = $this->Character->System->find('first', array(
498 'conditions' => $conditions,
502 if (!isset($this_system['System'])) {
503 $this->Session->setFlash(__('Invalid System.', true));
504 $this->redirect(array('controller' => 'systems', 'action' => 'index'));
507 $this->set('this_system', $this_system);
509 $contain = array_merge(array('System'), (array)$contain);
515 $conditions = array('User.id' => $id);
517 $user = $this->Character->User->find('first', array(
518 'conditions' => $conditions,
527 $this->Session->setFlash(__('Invalid Id.', true));
528 $this->redirect(array('action'=>'index'));
531 $contain = array_merge($contain, array('User'));
533 $this->set('userSet', $user);
536 if ($user['User']['id'] == $this->user_id) {
541 $conditions = array();
543 $conditions['Character.user_id'] = $id;
545 if ($isAdmin === true) {
546 $conditions['isAdmin'] = true;
548 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
549 $conditions['Character.system_id'] = $this->params['named']['system'];
551 $characters = $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
553 $this->set('characters', $characters);
555 $this->set('isOwner', false);
556 $this->set('isOwner_userSet', $isOwner);
559 function _view($id, $conditions, $isAdmin = false) {
562 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
563 if (!empty($orig_character['System']['Profile'])) {
564 $new_profile = array();
565 foreach ($orig_character['System']['Profile'] as $k => $profile) {
566 $new_profile[$profile['key_name']]['id'] = $profile['id'];
567 $new_profile[$profile['key_name']]['name'] = $profile['name'];
568 $new_profile[$profile['key_name']]['profile_type'] = $profile['profile_type'];
569 if (!empty($profile['ProfileTable'])) {
570 $new_profile[$profile['key_name']]['ProfileTable'] = $profile['ProfileTable'];
572 if ($profile['profile_type'] == 's-table' && empty($profile['CharactersHasProfile'])) {
573 if (isset($profile['ProfileTable'][0]['ProfileTableStatic'])) {
574 foreach ($profile['ProfileTable'][0]['ProfileTableStatic'] as $k2 => $v2) {
575 $new_profile[$profile['key_name']]['CharactersHasProfile'][] = $this->_set_blank4static_table($profile['ProfileTable'][0]['ProfileTableStatic'][$k2]['title'], $profile['ProfileTable'][0]['profile_id'], $profile['ProfileTable'][0]['id']);
576 for ($i=1;$i<count($profile['ProfileTable']);$i++) {
577 $new_profile[$profile['key_name']]['CharactersHasProfile'][] = $this->_set_blank4static_table(null, $profile['ProfileTable'][$i]['profile_id'], $profile['ProfileTable'][$i]['id']);
582 $new_profile[$profile['key_name']]['CharactersHasProfile'] = null;
585 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
589 $orig_character['System']['Profile'] = $new_profile;
593 'Character' => $orig_character['Character'],
594 'CharacterPicture' => $orig_character['CharacterPicture'],
595 'System' => $orig_character['System'],
596 'User' => $orig_character['User']
599 if (!$character['Character']) {
600 $this->Session->setFlash(__('Invalid Character.', true));
601 $this->redirect(array('action'=>'index'));
604 $this->set('systemValid', 'public');
605 if (!$character['System']) {
606 $this->set('systemValid', false);
608 if (!$this->check_public_flag($character['System'])) {
609 $this->set('systemValid', 'unpublic');
617 function _get_character($id, $conditions, $isAdmin = false)
619 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $id;
620 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
621 unset($this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
623 $character = $this->Character->find('first', array(
624 'conditions' => $conditions,
627 'CharacterPicture' => array(
633 'ProfileTable' => array(
634 'ProfileTableStatic',
636 'CharactersHasProfile',
643 if (empty($character)) {
644 $this->Session->setFlash(__('No Character', true));
645 $this->redirect(array('action'=>'index'));
648 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
649 $this->Session->setFlash(__('No Permission', true));
650 $this->redirect(array('action'=>'index'));
656 // ProfileTableをCharacterHasProfilesのTableの形式にセット
657 function _set_profile_table2characters_has_profiles($profile)
659 if (!empty($profile[0]['CharactersHasProfile'])) {
663 foreach($profile as $k => $v) {
664 if (empty($v['ProfileTable'])) {
668 $rows = count($v['CharactersHasProfile']);
669 for($i=0; $i<$rows; $i++) {
670 foreach($v['ProfileTable'] as $k2 => $v2) {
671 if (isset($v2['CharactersHasProfile'][$i])) {
672 $tmp[] = $v2['CharactersHasProfile'][$i];
678 $profile[$k]['CharactersHasProfile'] = $tmp;
684 /* POSTのCharactersHasProfileを処理 */
685 function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
687 $prev_profile_id = null;
688 $profileTable_tmp = array();
690 foreach ($characters_has_profiles as $k => $v) {
691 if (!empty($character_id)) {
692 $characters_has_profiles[$k]['character_id'] = $character_id;
696 if (is_array($v['value'])) {
697 $characters_has_profiles[$k]['value'] = $v['value'][0];
701 // table: 一時保管して全項目空の場合行削除
702 if (CorePlus::is_valid($v, 'profile_table_id')) {
703 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
706 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($v, $k);
708 } elseif (empty($v['value'])) {
709 unset($characters_has_profiles[$k]);
713 // table: 空行削除、不正profile_table_idデータ削除
714 if (!empty($profileTable_tmp)) {
715 if (!isset($this->Character->ProfileTable)) {
716 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
718 foreach($profileTable_tmp as $profile_id => $v) {
719 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
720 'conditions' => array('ProfileTable.profile_id' => $profile_id),
721 'fields' => array('ProfileTable.id'),
722 'order' => array('ProfileTable.sort_order' => 'asc'),
726 foreach ($v as $i => $profile_tables) {
728 $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
729 if (!empty($diff_keys)) {
730 foreach($diff_keys as $dvalue) {
731 unset($characters_has_profiles[$dvalue[1]]);
737 foreach($profile_table_columns as $k => $columns) {
738 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
743 if ($delete_flg === true) {
744 foreach ($profile_tables as $delk) {
745 unset($characters_has_profiles[$delk[1]]);
753 if (!is_null($profile_id) && !empty($now_data)) {
756 foreach($now_data as $k => $v) {
757 if ($v['id'] == $profile_id) {
758 $v['CharactersHasProfile'] = $characters_has_profiles;
761 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
763 if (isset($tmp[$i]['id'])) {
764 unset($tmp[$i]['id']);
766 if (isset($tmp[$i]['character_id'])) {
767 unset($tmp[$i]['character_id']);
774 $characters_has_profiles = $tmp;
777 return $characters_has_profiles;
780 // CharactersHasProfileの追加valiadte設定
781 function _set_validate4characters_has_profile($system_id)
783 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
784 'rule' => array('validProfileId', $system_id),
786 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
787 'rule' => array('validProfileSelectId', $system_id),
788 'allowEmpty' => true,
790 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
791 'rule' => array('validProfiletableId', $system_id),
792 'allowEmpty' => true,
797 function _restore_html_character($data, $nl2br = false) {
798 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
799 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
801 $data['notes'] = nl2br($data['notes']);
807 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
808 if (empty($profile)) {
812 foreach($profile as $k => $v) {
813 if (empty($v['CharactersHasProfile'])) {
816 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br);
821 function __restore_html_characters_has_profiles($data, $nl2br = false) {
826 foreach ($data as $k => $v) {
827 if (isset($v['value'])) {
828 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
837 /* status設定をview用にセット */
838 function set_status4view()
840 $model_status = $this->get_status();
842 $this->set('status', $model_status);
844 function get_status()
846 return $this->Character->status;
849 function _set_blank4static_table($value, $profile_id, $profile_table_id)
852 'profile_id' => $profile_id,
853 'profile_table_id' => $profile_table_id,
855 'public_flag' => 'public',
861 function _get_systems($public_flag = 'public')
863 $conditions = array();
864 if (!empty($public_flag)) {
865 $conditions['System.public_flag'] = $public_flag;
868 if (!isset($this->System)) {
869 $this->System = CorePlus::set_model('System');
872 $systems = $this->System->find('list', array(
873 'conditions' => $conditions,
876 if (!empty($systems)) {
877 foreach ($systems as $k => $v) {
878 $systems[$k] = $this->{$this->modelClass}->restore_html($v);