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,
339 'CharactersHasProfile',
347 if (empty($orig_character['Character'])) {
348 return $orig_character;
351 if (!empty($orig_character['System']['Profile'])) {
352 $new_profile = array();
353 foreach ($orig_character['System']['Profile'] as $k => $profile) {
354 $new_profile[$profile['key_name']]['id'] = $profile['id'];
355 $new_profile[$profile['key_name']]['name'] = $profile['name'];
356 $new_profile[$profile['key_name']]['profile_type'] = $profile['profile_type'];
357 if (!empty($profile['ProfileTable'])) {
358 $new_profile[$profile['key_name']]['ProfileTable'] = $profile['ProfileTable'];
360 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
363 $orig_character['System']['Profile'] = $new_profile;
367 'Character' => $orig_character['Character'],
368 'System' => $orig_character['System'],
369 'User' => $orig_character['User']
378 function _get_character4edit($conditions, $character_id)
380 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $character_id;
381 $this->Character->System->Profile->ProfileTable->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $character_id;
383 if (!empty($this->data)) {
384 $character = $this->Character->find('first', array(
385 'conditions' => $conditions,
392 'CharactersHasProfile',
399 $character = $this->Character->find('first', array(
400 'conditions' => $conditions,
406 'ProfileTable' => array(
407 'CharactersHasProfile',
409 'CharactersHasProfile',
417 if (empty($character)) {
418 $this->Session->setFlash(__('No Character', true));
419 $this->redirect(array('action'=>'index'));
422 if (!$this->isOwner($character['Character'], $this->user_id)) {
423 $this->Session->setFlash(__('No Permission', true));
424 $this->redirect(array('action'=>'index'));
430 // ProfileTableをCharacterHasProfilesのTableの形式にセット
431 function _set_profile_table2characters_has_profiles($profile)
433 foreach($profile as $k => $v) {
434 if (empty($v['ProfileTable'])) {
438 $rows = count($v['ProfileTable'][0]['CharactersHasProfile']);
439 for($i=0; $i<$rows; $i++) {
440 foreach($v['ProfileTable'] as $k2 => $v2) {
441 if (isset($v2['CharactersHasProfile'][$i])) {
442 $tmp[] = $v2['CharactersHasProfile'][$i];
448 $profile[$k]['CharactersHasProfile'] = $tmp;
454 /* POSTのCharactersHasProfileを処理 */
455 function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
457 $prev_profile_id = null;
458 $profileTable_tmp = array();
460 foreach ($characters_has_profiles as $k => $v) {
461 if (!empty($character_id)) {
462 $characters_has_profiles[$k]['character_id'] = $character_id;
466 if (is_array($v['value'])) {
467 $characters_has_profiles[$k]['value'] = $v['value'][0];
471 // table: 一時保管して全項目空の場合行削除
472 if (CorePlus::is_valid($v, 'profile_table_id')) {
473 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
476 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($v, $k);
477 // select/table以外は処理なし(処理停止)
478 /* } elseif (!CorePlus::is_valid($v, 'profile_select_id') && empty($v['value'])) {
479 unset($characters_has_profiles[$k]);
484 // table: 空行削除、不正profile_table_idデータ削除
485 if (!empty($profileTable_tmp)) {
486 if (!isset($this->Character->ProfileTable)) {
487 $this->Character->set_model('ProfileTable');
489 foreach($profileTable_tmp as $profile_id => $v) {
490 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
491 'conditions' => array('ProfileTable.profile_id' => $profile_id),
492 'fields' => array('ProfileTable.id'),
493 'order' => array('ProfileTable.sort_order' => 'asc'),
497 foreach ($v as $i => $profile_tables) {
499 $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
500 if (!empty($diff_keys)) {
501 foreach($diff_keys as $dvalue) {
502 unset($characters_has_profiles[$dvalue[1]]);
508 foreach($profile_table_columns as $k => $columns) {
509 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
514 if ($delete_flg === true) {
515 foreach ($profile_tables as $delk) {
516 unset($characters_has_profiles[$delk[1]]);
524 if (!is_null($profile_id) && !empty($now_data)) {
527 foreach($now_data as $k => $v) {
528 if ($v['id'] == $profile_id) {
529 $v['CharactersHasProfile'] = $characters_has_profiles;
532 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
534 if (isset($tmp[$i]['id'])) {
535 unset($tmp[$i]['id']);
537 if (isset($tmp[$i]['character_id'])) {
538 unset($tmp[$i]['character_id']);
545 $characters_has_profiles = $tmp;
548 return $characters_has_profiles;
551 // CharactersHasProfileの追加valiadte設定
552 function _set_validate4characters_has_profile($system_id)
554 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
555 'rule' => array('validProfileId', $system_id),
557 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
558 'rule' => array('validProfileSelectId', $system_id),
559 'allowEmpty' => true,
561 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
562 'rule' => array('validProfiletableId', $system_id),
563 'allowEmpty' => true,
568 function _restore_html_character($data, $nl2br = false) {
569 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
570 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
572 $data['notes'] = nl2br($data['notes']);
578 function _restore_html_characters_has_profiles($profile, $nl2br = false) {
579 if (empty($profile)) {
583 foreach($profile as $k => $v) {
584 if (empty($v['CharactersHasProfile'])) {
587 $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br);
592 function __restore_html_characters_has_profiles($data, $nl2br = false) {
597 foreach ($data as $k => $v) {
598 if (isset($v['value'])) {
599 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
608 /* status設定をview用にセット */
609 function set_status4view()
611 $model_status = $this->get_status();
613 $this->set('status', $model_status);
615 function get_status()
617 return $this->Character->status;