3 class CharactersController extends AppController {
5 var $name = 'Characters';
13 var $actionMapPlus = array(
14 'add_milti_profiles' => 'update',
17 var $disableTokenActions = array();
19 var $post_data = array();
23 function beforeFilter() {
25 parent::beforeFilter();
28 $this->AuthPlus->allow('view');
29 $this->AuthPlus->allow('index');
32 function beforeRender()
34 parent::beforeRender();
36 $this->set_public_flag4view();
37 $this->set_status4view();
47 function view($id = null) {
49 $this->Session->setFlash(__('Invalid Character.', true));
50 $this->redirect(array('action'=>'index'));
54 'Character.id' => $id,
55 'Character.deleted' => 0,
56 // todo:public_flagチェック
59 $character = $this->_view($id, $conditions);
60 if (!$character['Character']) {
61 $this->Session->setFlash(__('Invalid Character.', true));
62 $this->redirect(array('action'=>'index'));
65 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
68 if ($this->isOwner($character['Character'], $this->user_id)) {
71 $this->set('is_owner', $is_owner);
75 if (!empty($this->data)) {
76 $this->data['Character']['user_id'] = $this->user_id;
78 $this->Character->create();
79 if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
80 $this->Session->setFlash(__('The Character has been saved', true));
81 $this->redirect(array('action'=>'index'));
83 $this->Session->setFlash(__('The Character could not be saved. Please, try again.', true));
86 $systems = $this->Character->System->find('list');
87 $this->set('systems', $systems);
90 function edit($id = null) {
91 if (!$id && empty($this->data)) {
92 $this->Session->setFlash(__('Invalid ID.', true));
93 $this->redirect(array('action'=>'index'));
98 'Character.id' => $id,
100 $this->Character->System->hasMany['Profile']['fields'] = '';
101 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
103 $character = $this->_get_character4edit($conditions, $id);
105 if (empty($this->data)) {
106 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
109 if (!empty($this->data)) {
110 $this->post_data = $this->data;
113 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
116 $this->_set_validate4characters_has_profile($character['System']['id']);
119 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
122 $this->Character->CharactersHasProfile->deleteAll(array(
123 'CharactersHasProfile.character_id' => $id
128 $this->data['Character']['id'] = $id;
129 $this->Character->create();
131 if ($this->Character->saveAll($this->data, array(
133 'fieldList' => array_merge(
134 $this->Character->fields['edit'],
135 $this->Character->CharactersHasProfile->fields['add']
138 $this->Session->setFlash(__('The Character has been saved.', true));
139 $this->redirect(array('action'=>'view', $id));
143 $this->data = $character;
144 $this->data['Character'] = $this->post_data['Character'];
145 $this->data['Character']['id'] = $id;
147 foreach($this->data['System']['Profile'] as $k1 => $profile) {
148 foreach($profile['CharactersHasProfile'] as $k2 => $v) {
149 $postdata = array_shift($this->post_data['CharactersHasProfile']);
150 if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
151 array_unshift($this->post_data['CharactersHasProfile'], $postdata);
154 if (is_array($postdata['value'])) {
155 $postdata['value'] = $postdata['value'][0];
157 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
158 if (isset($postdata['profile_select_id'])) {
159 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
165 if (empty($this->data)) {
166 $this->data = $character;
167 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
169 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
173 function add_milti_profiles($id = null) {
174 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
175 $this->Session->setFlash(__('Invalid ID.', true));
176 $this->redirect(array('action'=>'index'));
181 'Character.id' => $id,
183 $this->Character->System->hasMany['Profile']['fields'] = '';
184 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
186 $character = $this->_get_character4edit($conditions, $id);
189 if (empty($character['System']['Profile'])) {
190 $this->Session->setFlash(__('Invalid Profile.', true));
191 $this->redirect(array('action'=>'view', $id));
194 $profile_id = $this->params['named']['profile_id'];
196 if (empty($this->data)) {
198 foreach ($character['System']['Profile'] as $profile) {
199 if ($profile['id'] == $profile_id) {
206 $this->Session->setFlash(__('No Profile.', true));
207 $this->redirect(array('action'=>'view', $id));
209 $character['System']['Profile'] = array();
210 $character['System']['Profile'][0] = $target;
212 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
215 if (!empty($this->data)) {
216 $this->post_data = $this->data;
218 $this->data['Character'] = $character['Character'];
219 $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
220 $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
221 $this->data['Character']['modified'] = null;
224 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
227 $this->_set_validate4characters_has_profile($character['System']['id']);
230 if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
233 $this->Character->CharactersHasProfile->deleteAll(array(
234 'CharactersHasProfile.character_id' => $id
238 $this->Character->create();
239 if ($this->Character->saveAll($this->data, array(
241 'fieldList' => array_merge(
242 // array('Character.sort_order'),
243 $this->Character->CharactersHasProfile->fields['add']
246 $this->Session->setFlash(__('The Character has been saved.', true));
247 $this->redirect(array('action'=>'view', $id));
251 $this->data = $character;
252 $this->data['Character'] = $this->post_data['Character'];
253 $this->data['Character']['id'] = $id;
257 if (empty($this->data)) {
258 $this->data = $character;
259 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
264 function delete($id = null) {
266 $this->Session->setFlash(__('Invalid id for Character', true));
267 $this->redirect(array('action'=>'index'));
270 $character = $this->Character->read(null, $id);
271 if (empty($character)) {
272 $this->Session->setFlash(__('No Character', true));
273 $this->redirect(array('action'=>'index'));
276 if (!$this->isOwner($character['Character'], $this->user_id)) {
277 $this->Session->setFlash(__('No Permission', true));
278 $this->redirect(array('action'=>'index'));
281 if ($this->Character->del($id)) {
282 $this->Session->setFlash(__('Character deleted', true));
283 $this->redirect(array('action'=>'index'));
288 function admin_index() {
292 function admin_view($id = null) {
294 $this->Session->setFlash(__('Invalid Character.', true));
295 $this->redirect(array('action'=>'admin_index'));
299 'Character.id' => $id,
302 $character = $this->_view($id, $conditions);
303 if (!$character['Character']) {
304 $this->Session->setFlash(__('Invalid Character.', true));
305 $this->redirect(array('action'=>'admin_index'));
308 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
311 function admin_delete($id = null) {
313 $this->Session->setFlash(__('Invalid id for Character', true));
314 $this->redirect(array('action'=>'index'));
316 if ($this->Character->del($id)) {
317 $this->Session->setFlash(__('Character deleted', true));
318 $this->redirect(array('action'=>'index'));
325 $this->Character->recursive = 0;
326 $this->set('characters', $this->HtmlEscape->nl_unescape($this->paginate()));
329 function _view($id, $conditions) {
330 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $id;
333 $orig_character = $this->Character->find('first', array(
334 'conditions' => $conditions,
335 'order' => array('Character.modified' => 'desc'),
340 'CharactersHasProfile',
348 if (empty($orig_character['Character'])) {
349 return $orig_character;
352 if (!empty($orig_character['System']['Profile'])) {
353 $new_profile = array();
354 foreach ($orig_character['System']['Profile'] as $k => $profile) {
355 $new_profile[$profile['key_name']]['id'] = $profile['id'];
356 $new_profile[$profile['key_name']]['name'] = $profile['name'];
357 $new_profile[$profile['key_name']]['profile_type'] = $profile['profile_type'];
358 if (!empty($profile['ProfileTable'])) {
359 $new_profile[$profile['key_name']]['ProfileTable'] = $profile['ProfileTable'];
361 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
364 $orig_character['System']['Profile'] = $new_profile;
368 'Character' => $orig_character['Character'],
369 'System' => $orig_character['System'],
370 'User' => $orig_character['User']
379 function _get_character4edit($conditions, $character_id)
381 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $character_id;
382 $this->Character->System->Profile->ProfileTable->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $character_id;
384 if (!empty($this->data)) {
385 $character = $this->Character->find('first', array(
386 'conditions' => $conditions,
393 'CharactersHasProfile',
400 $character = $this->Character->find('first', array(
401 'conditions' => $conditions,
407 'ProfileTable' => array(
408 'CharactersHasProfile',
410 'CharactersHasProfile',
418 if (empty($character)) {
419 $this->Session->setFlash(__('No Character', true));
420 $this->redirect(array('action'=>'index'));
423 if (!$this->isOwner($character['Character'], $this->user_id)) {
424 $this->Session->setFlash(__('No Permission', true));
425 $this->redirect(array('action'=>'index'));
431 // ProfileTableをCharacterHasProfilesのTableの形式にセット
432 function _set_profile_table2characters_has_profiles($profile)
434 foreach($profile as $k => $v) {
435 if (empty($v['ProfileTable'])) {
439 $rows = count($v['ProfileTable'][0]['CharactersHasProfile']);
440 for($i=0; $i<$rows; $i++) {
441 foreach($v['ProfileTable'] as $k2 => $v2) {
442 if (isset($v2['CharactersHasProfile'][$i])) {
443 $tmp[] = $v2['CharactersHasProfile'][$i];
449 $profile[$k]['CharactersHasProfile'] = $tmp;
455 /* POSTのCharactersHasProfileを処理 */
456 function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
458 $prev_profile_id = null;
459 $profileTable_tmp = array();
461 foreach ($characters_has_profiles as $k => $v) {
462 if (!empty($character_id)) {
463 $characters_has_profiles[$k]['character_id'] = $character_id;
467 if (is_array($v['value'])) {
468 $characters_has_profiles[$k]['value'] = $v['value'][0];
472 // table: 一時保管して全項目空の場合行削除
473 if (CorePlus::is_valid($v, 'profile_table_id')) {
474 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
477 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($v, $k);
478 // select/table以外は処理なし(処理停止)
479 /* } elseif (!CorePlus::is_valid($v, 'profile_select_id') && empty($v['value'])) {
480 unset($characters_has_profiles[$k]);
485 // table: 空行削除、不正profile_table_idデータ削除
486 if (!empty($profileTable_tmp)) {
487 if (!isset($this->Character->ProfileTable)) {
488 $this->Character->set_model('ProfileTable');
490 foreach($profileTable_tmp as $profile_id => $v) {
491 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
492 'conditions' => array('ProfileTable.profile_id' => $profile_id),
493 'fields' => array('ProfileTable.id'),
494 'order' => array('ProfileTable.sort_order' => 'asc'),
498 foreach ($v as $i => $profile_tables) {
500 $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
501 if (!empty($diff_keys)) {
502 foreach($diff_keys as $dvalue) {
503 unset($characters_has_profiles[$dvalue[1]]);
509 foreach($profile_table_columns as $k => $columns) {
510 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
515 if ($delete_flg === true) {
516 foreach ($profile_tables as $delk) {
517 unset($characters_has_profiles[$delk[1]]);
525 if (!is_null($profile_id) && !empty($now_data)) {
528 foreach($now_data as $k => $v) {
529 if ($v['id'] == $profile_id) {
530 $v['CharactersHasProfile'] = $characters_has_profiles;
533 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
535 if (isset($tmp[$i]['id'])) {
536 unset($tmp[$i]['id']);
538 if (isset($tmp[$i]['character_id'])) {
539 unset($tmp[$i]['character_id']);
546 $characters_has_profiles = $tmp;
549 return $characters_has_profiles;
552 // CharactersHasProfileの追加valiadte設定
553 function _set_validate4characters_has_profile($system_id)
555 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
556 'rule' => array('validProfileId', $system_id),
558 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
559 'rule' => array('validProfileSelectId', $system_id),
560 'allowEmpty' => true,
562 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
563 'rule' => array('validProfiletableId', $system_id),
564 'allowEmpty' => true,
569 function _restore_html_character($data, $nl2br = false) {
570 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
571 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
573 $data['notes'] = nl2br($data['notes']);
579 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
580 if (empty($profile)) {
584 foreach($profile as $k => $v) {
585 if (empty($v['CharactersHasProfile'])) {
588 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br);
593 function __restore_html_characters_has_profiles($data, $nl2br = false) {
598 foreach ($data as $k => $v) {
599 if (isset($v['value'])) {
600 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
609 /* status設定をview用にセット */
610 function set_status4view()
612 $model_status = $this->get_status();
614 $this->set('status', $model_status);
616 function get_status()
618 return $this->Character->status;