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) {
54 $this->Session->setFlash(__('Invalid Character.', true));
55 $this->redirect(array('action'=>'index'));
58 $character = $this->_view($id, array(), false);
60 if (!$this->check_public_flag($character['System'])) {
61 unset($character['System']['Profile']);
64 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
67 if ($this->isOwner($character['Character'], $this->user_id)) {
70 $this->set('isOwner', $isOwner);
72 // キャラ画像のpublic_flagチェック
73 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
74 foreach($character['CharacterPicture'] as $k => $v) {
75 if (!$this->check_public_flag2($v)) {
76 unset($character['CharacterPicture'][$k]);
81 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
85 $systems = $this->_get_systems();
87 if (!empty($this->data)) {
88 $this->data['Character']['user_id'] = $this->user_id;
90 $this->Character->create();
91 if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
95 if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 1) {
96 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
99 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
100 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
102 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
106 $this->set('systems', $systems);
107 $this->set('isOwner', false);
110 function edit($id = null) {
111 if (!$id && empty($this->data)) {
112 $this->Session->setFlash(__('Invalid ID.', true));
113 $this->redirect(array('action'=>'index'));
117 $this->Character->System->hasMany['Profile']['fields'] = '';
118 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
120 $character = $this->_get_character($id, array(), false, array('System' => 'Ptofile'));
121 if (!$this->isOwner($character['Character'], $this->user_id)) {
122 $this->Session->setFlash(__('No Permission', true));
123 $this->redirect(array('action'=>'index'));
127 if (!$this->check_public_flag($character['System'])) {
128 $this->redirect(array('action'=>'change_system', $id));
131 if (empty($this->data)) {
132 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
135 if (!empty($this->data)) {
136 $this->post_data = $this->data;
138 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
141 $this->_set_validate4characters_has_profile($character['System']['id']);
142 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
145 $this->Character->CharactersHasProfile->deleteAll(array(
146 'CharactersHasProfile.character_id' => $id
150 $this->data['Character']['id'] = $id;
151 $this->Character->create();
153 if ($this->Character->saveAll($this->data, array(
155 'fieldList' => array_merge(
156 $this->Character->fields['edit'],
157 $this->Character->CharactersHasProfile->fields['add']
163 if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 1) {
164 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
167 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
168 $this->redirect(array('action'=>'view', $id));
172 $this->data = $character;
173 $this->data['Character'] = $this->post_data['Character'];
174 $this->data['Character']['id'] = $id;
176 foreach($this->data['System']['Profile'] as $k1 => $profile) {
177 foreach($profile['CharactersHasProfile'] as $k2 => $v) {
178 $postdata = array_shift($this->post_data['CharactersHasProfile']);
179 if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
180 array_unshift($this->post_data['CharactersHasProfile'], $postdata);
183 if (is_array($postdata['value'])) {
184 $postdata['value'] = $postdata['value'][0];
186 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
187 if (isset($postdata['profile_select_id'])) {
188 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
194 if (empty($this->data)) {
195 $this->data = $character;
196 $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
198 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
200 $this->set('isOwner', true);
203 function add_milti_profiles($id = null) {
204 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
205 $this->Session->setFlash(__('Invalid ID.', true));
206 $this->redirect(array('action'=>'index'));
210 $this->Character->System->hasMany['Profile']['fields'] = '';
211 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
213 $character = $this->_get_character($id);
214 if (!$this->isOwner($character['Character'], $this->user_id)) {
215 $this->Session->setFlash(__('No Permission', true));
216 $this->redirect(array('action'=>'index'));
219 if (!$this->check_public_flag($character['System'])) {
220 $this->redirect(array('action'=>'change_system', $id));
224 if (empty($character['System']['Profile'])) {
225 $this->Session->setFlash(__('Invalid Profile.', true));
226 $this->redirect(array('action'=>'view', $id));
229 $profile_id = $this->params['named']['profile_id'];
231 if (empty($this->data)) {
233 foreach ($character['System']['Profile'] as $profile) {
234 if ($profile['id'] == $profile_id) {
241 $this->Session->setFlash(__('No Profile.', true));
242 $this->redirect(array('action'=>'view', $id));
245 $character['System']['Profile'] = array();
246 $character['System']['Profile'][0] = $target;
248 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
251 if (!empty($this->data)) {
252 $this->post_data = $this->data;
254 $this->data['Character'] = $character['Character'];
255 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
256 $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
257 $this->data['Character']['modified'] = null;
260 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
263 $this->_set_validate4characters_has_profile($character['System']['id']);
264 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
267 $this->Character->CharactersHasProfile->deleteAll(array(
268 'CharactersHasProfile.character_id' => $id
272 $this->Character->create();
273 if ($this->Character->saveAll($this->data, array(
275 'fieldList' => array_merge(
276 $this->Character->CharactersHasProfile->fields['add']
282 if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 1) {
283 $this->Character->saveCharacterProfile($this->Character->id, $this->data);
286 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
288 $this->redirect(array('action'=>'view', $id));
292 $this->data = $character;
293 $this->data['Character'] = $this->post_data['Character'];
294 $this->data['Character']['id'] = $id;
298 if (empty($this->data)) {
299 $this->data = $character;
300 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
303 $this->set('isOwner', true);
306 function change_system($id = null) {
308 $this->Session->setFlash(__('Invalid ID.', true));
309 $this->redirect(array('action'=>'index'));
313 $this->Character->System->hasMany['Profile']['fields'] = '';
314 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
316 $character = $this->_get_character($id);
317 if (!$this->isOwner($character['Character'], $this->user_id)) {
318 $this->Session->setFlash(__('No Permission', true));
319 $this->redirect(array('action'=>'index'));
323 if (isset($character['System']) && !empty($character['System'])) {
325 if ($this->check_public_flag($character['System'])) {
326 $this->redirect(array('action'=>'edit', $id));
329 $this->set('isChange', true);
333 if (!empty($this->data)) {
334 $this->data['Character']['id'] = $id;
336 // validateはsystem_idのみ
337 $this->Cahracter['validate'] = array(
338 'system_id' => array(
339 'validSystemId' => array(
340 'rule' => array('validSystemId', true),
341 'allowEmpty' => false,
346 if ($this->Character->save(
350 'fieldList' => array('system_id'),
353 $this->Session->setFlash(__('The new system has been saved', true));
355 $this->redirect(array('action'=>'view', $id));
357 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
361 $this->set('isOwner', true);
362 $this->set('character', $character);
364 $systems = $this->_get_systems();
365 $this->set('systems', $systems);
369 function set_status($id = null) {
370 if (!$id || !isset($this->params['named']['mode'])) {
371 $this->Session->setFlash(__('Invalid ID.', true));
372 $this->redirect(array('controller' => 'characters', 'action'=>'index'));
375 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
377 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
378 $this->Session->setFlash(__('Invalid URL.', true));
379 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
383 $new_character_picture = null;
384 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
385 if ($character['CharacterPicture']) {
386 foreach($character['CharacterPicture'] as $k => $v) {
387 if ($v['id'] == $this->params['named']['new_picture_id']) {
388 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
393 if (!$new_character_picture) {
394 $this->Session->setFlash(__('Invalid data.', true));
395 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
400 $this->Character->id = $id;
401 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
402 $this->Character->save(
405 'fieldList' => array(
406 $this->params['named']['mode']
411 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
412 $this->redirect(array('controller' => 'character_pictures', 'action' => 'index', $id));
415 function delete($id = null) {
417 $this->Session->setFlash(__('Invalid id for Character', true));
418 $this->redirect(array('action'=>'index'));
421 $character = $this->Character->read(null, $id);
422 if (empty($character)) {
423 $this->Session->setFlash(__('No Character', true));
424 $this->redirect(array('action'=>'index'));
427 if (!$this->isOwner($character['Character'], $this->user_id)) {
428 $this->Session->setFlash(__('No Permission', true));
429 $this->redirect(array('action'=>'index'));
432 if ($this->Character->del($id)) {
433 $this->Session->setFlash(__('Character deleted', true));
434 $this->redirect(array('action'=>'index'));
437 $this->set('isOwner', true);
441 function admin_index($id = null) {
442 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
443 $this->_index($id, array('isAdmin' => true));
446 function admin_view($id = null) {
448 $this->Session->setFlash(__('Invalid Character.', true));
449 $this->redirect(array('action' => 'admin_index'));
452 $character = $this->_view($id, array(), true);
454 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
457 function admin_delete($id = null) {
459 $this->Session->setFlash(__('Invalid id for Character', true));
460 $this->redirect(array('action'=>'index'));
462 if ($this->Character->del($id)) {
463 $this->Session->setFlash(__('Character deleted', true));
464 $this->redirect(array('action'=>'index'));
470 function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
471 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
476 unset($conditions['isAdmin']);
479 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
481 $conditions['System.id'] = $this->params['named']['system'];
482 if ($isAdmin === false) {
483 $conditions['System.public_flag'] = 'public';
486 $this_system = $this->Character->System->find('first', array(
487 'conditions' => $conditions,
491 if (!isset($this_system['System'])) {
492 $this->Session->setFlash(__('Invalid System.', true));
493 $this->redirect(array('controller' => 'systems', 'action' => 'index'));
495 $this_system = $this->_restore_html_system($this_system);
497 $this->set('this_system', $this_system);
499 $contain = array_merge(array('System'), (array)$contain);
505 $conditions = array('User.id' => $id);
507 $user = $this->Character->User->find('first', array(
508 'conditions' => $conditions,
517 $this->Session->setFlash(__('Invalid Id.', true));
518 $this->redirect(array('action'=>'index'));
521 $contain = array_merge($contain, array('User'));
523 $this->set('userSet', $user);
526 if ($user['User']['id'] == $this->user_id) {
531 $conditions = array();
533 $conditions['Character.user_id'] = $id;
535 if ($isAdmin === true) {
536 $conditions['isAdmin'] = true;
538 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
539 $conditions['Character.system_id'] = $this->params['named']['system'];
541 $characters = $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
543 $this->set('characters', $characters);
545 $this->set('isOwner', false);
546 $this->set('isOwner_userSet', $isOwner);
549 function _view($id, $conditions = array(), $isAdmin = false) {
550 $this->helpers[] = 'CharacterSheet';
553 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
554 $this->view = 'Theme';
555 $this->theme = $this->params['named']['mode'];
559 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
561 $character = $this->Character->set_profiles2view($orig_character);
564 $this->set('systemValid', 'public');
565 if (!$character['System']) {
566 $this->set('systemValid', false);
568 if (!$this->check_public_flag($character['System'])) {
569 $this->set('systemValid', 'unpublic');
571 $character = $this->_restore_html_system($character);
579 function _get_character($id, $conditions = array(), $isAdmin = false)
581 $character = $this->Character->get_character($id, $conditions, $isAdmin);
583 if (empty($character)) {
584 $this->Session->setFlash(__('No Character', true));
585 $this->redirect(array('action'=>'index'));
588 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
589 $this->Session->setFlash(__('No Permission', true));
590 $this->redirect(array('action'=>'index'));
596 // ProfileTableをCharacterHasProfilesのTableの形式にセット
597 function _set_profile_table2characters_has_profiles($profile)
599 if (!empty($profile[0]['CharactersHasProfile'])) {
603 foreach($profile as $k => $v) {
604 if (empty($v['ProfileTable'])) {
608 $rows = count($v['CharactersHasProfile']);
609 for($i=0; $i<$rows; $i++) {
610 foreach($v['ProfileTable'] as $k2 => $v2) {
611 if (isset($v2['CharactersHasProfile'][$i])) {
612 $tmp[] = $v2['CharactersHasProfile'][$i];
613 } elseif (isset($v['CharactersHasProfile'][$i])) {
614 $tmp[] = $v['CharactersHasProfile'][$i];
622 $profile[$k]['CharactersHasProfile'] = $tmp;
628 /* POSTのCharactersHasProfileを処理 */
629 function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
631 $prev_profile_id = null;
632 $profileTable_tmp = array();
634 foreach ($characters_has_profiles as $k => $v) {
635 if (!empty($character_id)) {
636 $characters_has_profiles[$k]['character_id'] = $character_id;
639 if (is_array($v['value'])) {
640 $characters_has_profiles[$k]['value'] = $v['value'][0];
644 $characters_has_profiles[$k]['value'] = Sanitize::html($characters_has_profiles[$k]['value']);
645 $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
646 $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
648 if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
649 $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
650 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
652 $characters_has_profiles[$k]['value'] = Sanitize::stripAll($characters_has_profiles[$k]['value']);
655 // table: 一時保管して全項目空の場合行削除
656 if (CorePlus::is_valid($v, 'profile_table_id')) {
657 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
660 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
662 } elseif (empty($v['value'])) {
663 unset($characters_has_profiles[$k]);
667 // table: 空行削除、不正profile_table_idデータ削除
668 if (!empty($profileTable_tmp)) {
669 if (!isset($this->Character->ProfileTable)) {
670 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
672 foreach($profileTable_tmp as $profile_id => $v) {
673 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
674 'conditions' => array('ProfileTable.profile_id' => $profile_id),
675 'fields' => array('ProfileTable.id'),
676 'order' => array('ProfileTable.sort_order' => 'asc'),
680 foreach ($v as $i => $profile_tables) {
682 $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
683 if (!empty($diff_keys)) {
684 foreach($diff_keys as $dvalue) {
685 unset($characters_has_profiles[$dvalue[1]]);
691 foreach($profile_table_columns as $k => $columns) {
692 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
697 if ($delete_flg === true) {
698 foreach ($profile_tables as $delk) {
699 unset($characters_has_profiles[$delk[1]]);
707 if (!is_null($profile_id) && !empty($now_data)) {
710 foreach($now_data as $k => $v) {
711 if ($v['id'] == $profile_id) {
712 $v['CharactersHasProfile'] = $characters_has_profiles;
715 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
717 if (isset($tmp[$i]['id'])) {
718 unset($tmp[$i]['id']);
720 if (isset($tmp[$i]['character_id'])) {
721 unset($tmp[$i]['character_id']);
728 $characters_has_profiles = $tmp;
731 return $characters_has_profiles;
734 // CharactersHasProfileの追加valiadte設定
735 function _set_validate4characters_has_profile($system_id)
737 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
738 'rule' => array('validProfileId', $system_id),
740 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
741 'rule' => array('validProfileSelectId', $system_id),
742 'allowEmpty' => true,
744 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
745 'rule' => array('validProfiletableId', $system_id),
746 'allowEmpty' => true,
751 function _restore_html_character($data, $nl2br = false) {
752 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
753 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
755 $data['notes'] = str_replace('<br />', "\n", $data['notes']);
761 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
762 if (empty($profile)) {
766 foreach($profile as $k => $v) {
767 if (empty($v['CharactersHasProfile'])) {
770 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
775 function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
780 foreach ($data as $k => $v) {
781 if (isset($v['value'])) {
782 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
783 if ($nl2br && $profile_type == 'textarea') {
784 $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);