3 class CharactersController extends AppController {
5 var $name = 'Characters';
13 var $actionMapPlus = array(
14 'add_milti_profiles' => 'update',
15 'set_status' => 'update',
18 var $disableTokenActions = array();
20 var $post_data = array();
24 function beforeFilter() {
26 parent::beforeFilter();
29 $this->AuthPlus->allow('view');
30 $this->AuthPlus->allow('index');
33 function beforeRender()
35 parent::beforeRender();
37 $this->set_public_flag4view();
38 $this->set_status4view();
48 function view($id = null) {
50 $this->Session->setFlash(__('Invalid Character.', true));
51 $this->redirect(array('action'=>'index'));
55 'Character.id' => $id,
56 'Character.deleted' => 0,
57 // todo:public_flagチェック
60 $character = $this->_view($id, $conditions);
61 if (!$character['Character']) {
62 $this->Session->setFlash(__('Invalid Character.', true));
63 $this->redirect(array('action'=>'index'));
66 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
69 if ($this->isOwner($character['Character'], $this->user_id)) {
72 $this->set('isOwner', $isOwner);
74 // キャラ画像のpublic_flagチェック
75 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
76 foreach($character['CharacterPicture'] as $k => $v) {
77 if (!$this->check_public_flag2($v)) {
78 unset($character['CharacterPicture'][$k]);
83 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
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']))) {
92 $this->Session->setFlash(__('The Character has been saved', true));
93 $this->redirect(array('action'=>'index'));
95 $this->Session->setFlash(__('The Character could not be saved. Please, try again.', true));
98 $systems = $this->Character->System->find('list');
99 $this->set('systems', $systems);
100 $this->set('isOwner', false);
103 function edit($id = null) {
104 if (!$id && empty($this->data)) {
105 $this->Session->setFlash(__('Invalid ID.', true));
106 $this->redirect(array('action'=>'index'));
111 'Character.id' => $id,
113 $this->Character->System->hasMany['Profile']['fields'] = '';
114 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
116 $character = $this->_get_character4edit($conditions, $id);
118 if (empty($this->data)) {
119 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
122 if (!empty($this->data)) {
123 $this->post_data = $this->data;
126 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
129 $this->_set_validate4characters_has_profile($character['System']['id']);
132 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
135 $this->Character->CharactersHasProfile->deleteAll(array(
136 'CharactersHasProfile.character_id' => $id
141 $this->data['Character']['id'] = $id;
142 $this->Character->create();
144 if ($this->Character->saveAll($this->data, array(
146 'fieldList' => array_merge(
147 $this->Character->fields['edit'],
148 $this->Character->CharactersHasProfile->fields['add']
151 $this->Session->setFlash(__('The Character has been saved.', true));
153 $this->redirect(array('action'=>'view', $id));
157 $this->data = $character;
158 $this->data['Character'] = $this->post_data['Character'];
159 $this->data['Character']['id'] = $id;
161 foreach($this->data['System']['Profile'] as $k1 => $profile) {
162 foreach($profile['CharactersHasProfile'] as $k2 => $v) {
163 $postdata = array_shift($this->post_data['CharactersHasProfile']);
164 if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
165 array_unshift($this->post_data['CharactersHasProfile'], $postdata);
168 if (is_array($postdata['value'])) {
169 $postdata['value'] = $postdata['value'][0];
171 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
172 if (isset($postdata['profile_select_id'])) {
173 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
179 if (empty($this->data)) {
180 $this->data = $character;
181 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
183 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
185 $this->set('isOwner', true);
188 function add_milti_profiles($id = null) {
189 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
190 $this->Session->setFlash(__('Invalid ID.', true));
191 $this->redirect(array('action'=>'index'));
196 'Character.id' => $id,
198 $this->Character->System->hasMany['Profile']['fields'] = '';
199 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
201 $character = $this->_get_character4edit($conditions, $id);
204 if (empty($character['System']['Profile'])) {
205 $this->Session->setFlash(__('Invalid Profile.', true));
206 $this->redirect(array('action'=>'view', $id));
209 $profile_id = $this->params['named']['profile_id'];
211 if (empty($this->data)) {
213 foreach ($character['System']['Profile'] as $profile) {
214 if ($profile['id'] == $profile_id) {
221 $this->Session->setFlash(__('No Profile.', true));
222 $this->redirect(array('action'=>'view', $id));
225 $character['System']['Profile'] = array();
226 $character['System']['Profile'][0] = $target;
228 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
231 if (!empty($this->data)) {
232 $this->post_data = $this->data;
234 $this->data['Character'] = $character['Character'];
235 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
236 $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
237 $this->data['Character']['modified'] = null;
240 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
243 $this->_set_validate4characters_has_profile($character['System']['id']);
246 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
249 $this->Character->CharactersHasProfile->deleteAll(array(
250 'CharactersHasProfile.character_id' => $id
254 $this->Character->create();
255 if ($this->Character->saveAll($this->data, array(
257 'fieldList' => array_merge(
258 $this->Character->CharactersHasProfile->fields['add']
261 $this->Session->setFlash(__('The Character has been saved.', true));
263 $this->redirect(array('action'=>'view', $id));
267 $this->data = $character;
268 $this->data['Character'] = $this->post_data['Character'];
269 $this->data['Character']['id'] = $id;
273 if (empty($this->data)) {
274 $this->data = $character;
275 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
278 $this->set('isOwner', true);
281 function set_status($id = null) {
282 if (!$id || !isset($this->params['named']['mode'])) {
283 $this->Session->setFlash(__('Invalid ID.', true));
284 $this->redirect(array('controller' => 'characters', 'action'=>'index'));
287 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
289 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
290 $this->Session->setFlash(__('Invalid URL.', true));
291 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
295 $new_character_picture = null;
296 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
297 if ($character['CharacterPicture']) {
298 foreach($character['CharacterPicture'] as $k => $v) {
299 if ($v['id'] == $this->params['named']['new_picture_id']) {
300 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
305 if (!$new_character_picture) {
306 $this->Session->setFlash(__('Invalid data.', true));
307 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
312 $this->Character->id = $id;
313 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
314 $this->Character->save(
317 'fieldList' => array(
318 $this->params['named']['mode']
323 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
324 $this->redirect(array('controller' => 'character_pictures', 'action' => 'index', $id));
327 function delete($id = null) {
329 $this->Session->setFlash(__('Invalid id for Character', true));
330 $this->redirect(array('action'=>'index'));
333 $character = $this->Character->read(null, $id);
334 if (empty($character)) {
335 $this->Session->setFlash(__('No Character', true));
336 $this->redirect(array('action'=>'index'));
339 if (!$this->isOwner($character['Character'], $this->user_id)) {
340 $this->Session->setFlash(__('No Permission', true));
341 $this->redirect(array('action'=>'index'));
344 if ($this->Character->del($id)) {
345 $this->Session->setFlash(__('Character deleted', true));
346 $this->redirect(array('action'=>'index'));
349 $this->set('isOwner', true);
353 function admin_index() {
357 function admin_view($id = null) {
359 $this->Session->setFlash(__('Invalid Character.', true));
360 $this->redirect(array('action'=>'admin_index'));
364 'Character.id' => $id,
367 $character = $this->_view($id, $conditions);
368 if (!$character['Character']) {
369 $this->Session->setFlash(__('Invalid Character.', true));
370 $this->redirect(array('action'=>'admin_index'));
373 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
376 function admin_delete($id = null) {
378 $this->Session->setFlash(__('Invalid id for Character', true));
379 $this->redirect(array('action'=>'index'));
381 if ($this->Character->del($id)) {
382 $this->Session->setFlash(__('Character deleted', true));
383 $this->redirect(array('action'=>'index'));
390 $this->Character->recursive = 0;
391 $this->set('characters', $this->HtmlEscape->nl_unescape($this->paginate()));
393 $this->set('isOwner', false);
396 function _view($id, $conditions, $public_flag = null) {
397 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $id;
398 if (empty($public_flag)) {
399 unset($this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
401 $this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag'] = $public_flag;
405 $orig_character = $this->Character->find('first', array(
406 'conditions' => $conditions,
407 'order' => array('Character.modified' => 'desc'),
412 'CharactersHasProfile',
413 'ProfileTable' => array(
414 'ProfileTableStatic',
418 'CharacterPicture' => array(
425 if (empty($orig_character['Character'])) {
426 return $orig_character;
429 if (!empty($orig_character['System']['Profile'])) {
430 $new_profile = array();
431 foreach ($orig_character['System']['Profile'] as $k => $profile) {
432 $new_profile[$profile['key_name']]['id'] = $profile['id'];
433 $new_profile[$profile['key_name']]['name'] = $profile['name'];
434 $new_profile[$profile['key_name']]['profile_type'] = $profile['profile_type'];
435 if (!empty($profile['ProfileTable'])) {
436 $new_profile[$profile['key_name']]['ProfileTable'] = $profile['ProfileTable'];
438 if ($profile['profile_type'] == 's-table' && empty($profile['CharactersHasProfile'])) {
439 if (isset($profile['ProfileTable'][0]['ProfileTableStatic'])) {
440 foreach ($profile['ProfileTable'][0]['ProfileTableStatic'] as $k2 => $v2) {
441 $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']);
442 for ($i=1;$i<count($profile['ProfileTable']);$i++) {
443 $new_profile[$profile['key_name']]['CharactersHasProfile'][] = $this->_set_blank4static_table(null, $profile['ProfileTable'][$i]['profile_id'], $profile['ProfileTable'][$i]['id']);
448 $new_profile[$profile['key_name']]['CharactersHasProfile'] = null;
451 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
455 $orig_character['System']['Profile'] = $new_profile;
459 'Character' => $orig_character['Character'],
460 'CharacterPicture' => $orig_character['CharacterPicture'],
461 'System' => $orig_character['System'],
462 'User' => $orig_character['User']
470 function _get_character4edit($conditions, $character_id)
472 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $character_id;
474 $character = $this->Character->find('first', array(
475 'conditions' => $conditions,
481 'ProfileTable' => array(
482 'ProfileTableStatic',
484 'CharactersHasProfile',
491 if (empty($character)) {
492 $this->Session->setFlash(__('No Character', true));
493 $this->redirect(array('action'=>'index'));
496 if (!$this->isOwner($character['Character'], $this->user_id)) {
497 $this->Session->setFlash(__('No Permission', true));
498 $this->redirect(array('action'=>'index'));
504 // ProfileTableをCharacterHasProfilesのTableの形式にセット
505 function _set_profile_table2characters_has_profiles($profile)
507 if (!empty($profile[0]['CharactersHasProfile'])) {
511 foreach($profile as $k => $v) {
512 if (empty($v['ProfileTable'])) {
516 $rows = count($v['CharactersHasProfile']);
517 for($i=0; $i<$rows; $i++) {
518 foreach($v['ProfileTable'] as $k2 => $v2) {
519 if (isset($v2['CharactersHasProfile'][$i])) {
520 $tmp[] = $v2['CharactersHasProfile'][$i];
526 $profile[$k]['CharactersHasProfile'] = $tmp;
532 /* POSTのCharactersHasProfileを処理 */
533 function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
535 $prev_profile_id = null;
536 $profileTable_tmp = array();
538 foreach ($characters_has_profiles as $k => $v) {
539 if (!empty($character_id)) {
540 $characters_has_profiles[$k]['character_id'] = $character_id;
544 if (is_array($v['value'])) {
545 $characters_has_profiles[$k]['value'] = $v['value'][0];
549 // table: 一時保管して全項目空の場合行削除
550 if (CorePlus::is_valid($v, 'profile_table_id')) {
551 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
554 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($v, $k);
555 // select/table以外は処理なし(処理停止)
556 /* } elseif (!CorePlus::is_valid($v, 'profile_select_id') && empty($v['value'])) {
557 unset($characters_has_profiles[$k]);
562 // table: 空行削除、不正profile_table_idデータ削除
563 if (!empty($profileTable_tmp)) {
564 if (!isset($this->Character->ProfileTable)) {
565 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
567 foreach($profileTable_tmp as $profile_id => $v) {
568 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
569 'conditions' => array('ProfileTable.profile_id' => $profile_id),
570 'fields' => array('ProfileTable.id'),
571 'order' => array('ProfileTable.sort_order' => 'asc'),
575 foreach ($v as $i => $profile_tables) {
577 $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
578 if (!empty($diff_keys)) {
579 foreach($diff_keys as $dvalue) {
580 unset($characters_has_profiles[$dvalue[1]]);
586 foreach($profile_table_columns as $k => $columns) {
587 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
592 if ($delete_flg === true) {
593 foreach ($profile_tables as $delk) {
594 unset($characters_has_profiles[$delk[1]]);
602 if (!is_null($profile_id) && !empty($now_data)) {
605 foreach($now_data as $k => $v) {
606 if ($v['id'] == $profile_id) {
607 $v['CharactersHasProfile'] = $characters_has_profiles;
610 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
612 if (isset($tmp[$i]['id'])) {
613 unset($tmp[$i]['id']);
615 if (isset($tmp[$i]['character_id'])) {
616 unset($tmp[$i]['character_id']);
623 $characters_has_profiles = $tmp;
626 return $characters_has_profiles;
629 // CharactersHasProfileの追加valiadte設定
630 function _set_validate4characters_has_profile($system_id)
632 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
633 'rule' => array('validProfileId', $system_id),
635 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
636 'rule' => array('validProfileSelectId', $system_id),
637 'allowEmpty' => true,
639 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
640 'rule' => array('validProfiletableId', $system_id),
641 'allowEmpty' => true,
646 function _restore_html_character($data, $nl2br = false) {
647 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
648 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
650 $data['notes'] = nl2br($data['notes']);
656 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
657 if (empty($profile)) {
661 foreach($profile as $k => $v) {
662 if (empty($v['CharactersHasProfile'])) {
665 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br);
670 function __restore_html_characters_has_profiles($data, $nl2br = false) {
675 foreach ($data as $k => $v) {
676 if (isset($v['value'])) {
677 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
686 /* status設定をview用にセット */
687 function set_status4view()
689 $model_status = $this->get_status();
691 $this->set('status', $model_status);
693 function get_status()
695 return $this->Character->status;
698 function _set_blank4static_table($value, $profile_id, $profile_table_id)
701 'profile_id' => $profile_id,
702 'profile_table_id' => $profile_table_id,
704 'public_flag' => 'public',