OSDN Git Service

Merge branch 'master' of cake_67@git.sourceforge.jp:/gitroot/trpgtools-onweb/cake...
[trpgtools-onweb/cake-frame.git] / app / controllers / characters_controller.php
1 <?php
2
3 class CharactersController extends AppController {
4
5         var $name = 'Characters';
6         var $helpers = array(
7                 'Select',
8                 'Profiledisp',
9         );
10
11         /* ACL */
12         // 追加アクション用 crudMap
13         var $actionMapPlus = array(
14                 'add_milti_profiles' => 'update',
15         );
16
17         var $disableTokenActions = array();
18
19         var $post_data = array();
20
21         /* メソッド */
22
23         function beforeFilter() {
24
25                 parent::beforeFilter();
26
27                 // 認証なしアクセス可
28                 $this->AuthPlus->allow('view');
29                 $this->AuthPlus->allow('index');
30         }
31
32         function beforeRender()
33         {
34                 parent::beforeRender();
35
36                         $this->set_public_flag4view();
37                         $this->set_status4view();
38         }
39
40
41         /* アクションメソッド */
42
43         function index() {
44                 $this->_index();
45         }
46
47         function view($id = null) {
48                 if (!$id) {
49                         $this->Session->setFlash(__('Invalid Character.', true));
50                         $this->redirect(array('action'=>'index'));
51                 }
52
53                 $conditions = array(
54                         'Character.id' => $id,
55                         'Character.deleted' => 0,
56                 // todo:public_flagチェック
57                 );
58
59                 $character = $this->_view($id, $conditions);
60                 if (!$character['Character']) {
61                         $this->Session->setFlash(__('Invalid Character.', true));
62                         $this->redirect(array('action'=>'index'));
63                 }
64
65                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
66
67                 $is_owner = false;
68                 if ($this->isOwner($character['Character'], $this->user_id)) {
69                         $is_owner = true;
70                 }
71                 $this->set('is_owner', $is_owner);
72         }
73
74         function add() {
75                 if (!empty($this->data)) {
76                         $this->data['Character']['user_id'] = $this->user_id;
77
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'));
82                         } else {
83                                 $this->Session->setFlash(__('The Character could not be saved. Please, try again.', true));
84                         }
85                 }
86                 $systems = $this->Character->System->find('list');
87                 $this->set('systems', $systems);
88         }
89
90         function edit($id = null) {
91                 if (!$id && empty($this->data)) {
92                         $this->Session->setFlash(__('Invalid ID.', true));
93                         $this->redirect(array('action'=>'index'));
94                 }
95
96                 // Characterデータ取得
97                 $conditions = array(
98                         'Character.id' => $id,
99                 );
100                 $this->Character->System->hasMany['Profile']['fields'] = '';
101                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
102
103                 $character = $this->_get_character4edit($conditions, $id);
104
105                 if (empty($this->data)) {
106                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
107                 }
108
109                 if (!empty($this->data)) {
110                         $this->post_data = $this->data;
111
112                         // 新hasProfile処理
113                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
114
115                         /* validate */
116                         $this->_set_validate4characters_has_profile($character['System']['id']);
117
118                         /* validateCheck */
119                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
120
121                                 // 現在のhas_profiles削除
122                                 $this->Character->CharactersHasProfile->deleteAll(array(
123                                         'CharactersHasProfile.character_id' => $id
124                                 ));
125
126
127                                 /* データ保存 */
128                                 $this->data['Character']['id'] = $id;
129                                 $this->Character->create();
130
131                                 if ($this->Character->saveAll($this->data, array(
132                                         'validate' => false,
133                                          'fieldList' => array_merge(
134                                                 $this->Character->fields['edit'], 
135                                                 $this->Character->CharactersHasProfile->fields['add']
136                                          )
137                                 ))) {
138                                         $this->Session->setFlash(__('The Character has been saved.', true));
139                                         $this->redirect(array('action'=>'view', $id));
140                                 }
141                         }
142
143                         $this->data = $character;
144                         $this->data['Character'] = $this->post_data['Character'];
145                         $this->data['Character']['id'] = $id;
146
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);
152                                                 continue;
153                                         } 
154                                         if (is_array($postdata['value'])) {
155                                                 $postdata['value'] = $postdata['value'][0];
156                                         }
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'];
160                                         }
161                                 }
162                         }
163                 }
164
165                 if (empty($this->data)) {
166                         $this->data = $character;
167                         $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
168
169                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
170                 }
171         }
172
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'));
177                 }
178
179                 // Characterデータ取得
180                 $conditions = array(
181                         'Character.id' => $id,
182                 );
183                 $this->Character->System->hasMany['Profile']['fields'] = '';
184                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
185
186                 $character = $this->_get_character4edit($conditions, $id);
187
188                 // Profilesデータ取得
189                 if (empty($character['System']['Profile'])) {
190                         $this->Session->setFlash(__('Invalid Profile.', true));
191                         $this->redirect(array('action'=>'view', $id));
192                 }
193
194                 $profile_id = $this->params['named']['profile_id'];
195
196                 if (empty($this->data)) {
197                         $target = array();
198                         foreach ($character['System']['Profile'] as $profile) {
199                                 if ($profile['id'] == $profile_id) {
200                                         $target = $profile;
201                                         break;
202                                 }
203                         }
204
205                         if (!$target) {
206                                 $this->Session->setFlash(__('No Profile.', true));
207                                 $this->redirect(array('action'=>'view', $id));
208                         }
209                         $character['System']['Profile'] = array();
210                         $character['System']['Profile'][0] = $target;
211
212                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
213                 }
214
215                 if (!empty($this->data)) {
216                         $this->post_data = $this->data;
217
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;
222
223                         // 新hasProfile処理
224                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
225
226                         /* validate */
227                         $this->_set_validate4characters_has_profile($character['System']['id']);
228
229                         /* validateCheck */
230                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
231
232                                 // 現在のhas_profiles削除
233                                 $this->Character->CharactersHasProfile->deleteAll(array(
234                                         'CharactersHasProfile.character_id' => $id
235                                 ));
236
237                                 /* データ保存 */
238                                 $this->Character->create();
239                                 if ($this->Character->saveAll($this->data, array(
240                                         'validate' => false,
241                                          'fieldList' => array_merge(
242 //                                              array('Character.sort_order'), 
243                                                 $this->Character->CharactersHasProfile->fields['add']
244                                          )
245                                 ))) {
246                                         $this->Session->setFlash(__('The Character has been saved.', true));
247                                         $this->redirect(array('action'=>'view', $id));
248                                 }
249                         }
250
251                         $this->data = $character;
252                         $this->data['Character'] = $this->post_data['Character'];
253                         $this->data['Character']['id'] = $id;
254
255                 }
256
257                 if (empty($this->data)) {
258                         $this->data = $character;
259                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
260                 }
261
262         }
263
264         function delete($id = null) {
265                 if (!$id) {
266                         $this->Session->setFlash(__('Invalid id for Character', true));
267                         $this->redirect(array('action'=>'index'));
268                 }
269
270                 $character = $this->Character->read(null, $id);
271                 if (empty($character)) {
272                         $this->Session->setFlash(__('No Character', true));
273                         $this->redirect(array('action'=>'index'));
274                 }
275
276                 if (!$this->isOwner($character['Character'], $this->user_id)) {
277                         $this->Session->setFlash(__('No Permission', true));
278                         $this->redirect(array('action'=>'index'));
279                 }
280
281                 if ($this->Character->del($id)) {
282                         $this->Session->setFlash(__('Character deleted', true));
283                         $this->redirect(array('action'=>'index'));
284                 }
285         }
286
287
288         function admin_index() {
289                 $this->_index();
290         }
291
292         function admin_view($id = null) {
293                 if (!$id) {
294                         $this->Session->setFlash(__('Invalid Character.', true));
295                         $this->redirect(array('action'=>'admin_index'));
296                 }
297
298                 $conditions = array(
299                         'Character.id' => $id,
300                 );
301
302                 $character = $this->_view($id, $conditions);
303                 if (!$character['Character']) {
304                         $this->Session->setFlash(__('Invalid Character.', true));
305                         $this->redirect(array('action'=>'admin_index'));
306                 }
307
308                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
309         }
310
311         function admin_delete($id = null) {
312                 if (!$id) {
313                         $this->Session->setFlash(__('Invalid id for Character', true));
314                         $this->redirect(array('action'=>'index'));
315                 }
316                 if ($this->Character->del($id)) {
317                         $this->Session->setFlash(__('Character deleted', true));
318                         $this->redirect(array('action'=>'index'));
319                 }
320         }
321
322
323         /* 共通化アクションメソッド */
324         function _index() {
325                 $this->Character->recursive = 0;
326                 $this->set('characters', $this->HtmlEscape->nl_unescape($this->paginate()));
327         }
328
329         function _view($id, $conditions) {
330                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.character_id'] = $id;
331
332                 // キャラデータ取得
333                 $orig_character = $this->Character->find('first', array(
334                         'conditions' => $conditions,
335                         'contain' => array(
336                                 'User',
337                                 'System' => array(
338                                         'Profile' => array(
339                                                 'CharactersHasProfile',
340                                                 'ProfileTable',
341                                         ),
342                                 ),
343                         ),
344                         'recursive' => 3,
345                 ));
346
347                 if (empty($orig_character['Character'])) {
348                         return $orig_character;
349                 }
350
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'];
359                                 }
360                                 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
361                         }
362
363                         $orig_character['System']['Profile'] = $new_profile;
364                 }
365
366                 $character = array(
367                         'Character' => $orig_character['Character'],
368                         'System' => $orig_character['System'],
369                         'User' => $orig_character['User']
370                 );
371
372
373                 return $character;
374         } 
375
376
377         /* 共通関数 */
378         function _get_character4edit($conditions, $character_id)
379         {
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;
382
383                 if (!empty($this->data)) {
384                         $character = $this->Character->find('first', array(
385                                 'conditions' => $conditions,
386                                 'contain' => array(
387                                         'User',
388                                         'System' => array(
389                                                 'Profile' => array(
390                                                         'ProfileSelect',
391                                                         'ProfileTable',
392                                                         'CharactersHasProfile',
393                                                 ),
394                                         ),
395                                 ),
396                                 'recursive' => 3,
397                         ));
398                 } else {
399                         $character = $this->Character->find('first', array(
400                                 'conditions' => $conditions,
401                                 'contain' => array(
402                                         'User',
403                                         'System' => array(
404                                                 'Profile' => array(
405                                                         'ProfileSelect',
406                                                         'ProfileTable' => array(
407                                                                 'CharactersHasProfile',
408                                                         ),
409                                                         'CharactersHasProfile',
410                                                 ),
411                                         ),
412                                 ),
413                                 'recursive' => 4,
414                         ));
415                 }
416
417                 if (empty($character)) {
418                         $this->Session->setFlash(__('No Character', true));
419                         $this->redirect(array('action'=>'index'));
420                 }
421
422                 if (!$this->isOwner($character['Character'], $this->user_id)) {
423                         $this->Session->setFlash(__('No Permission', true));
424                         $this->redirect(array('action'=>'index'));
425                 }
426         
427                 return $character;
428         }
429
430         // ProfileTableをCharacterHasProfilesのTableの形式にセット
431         function _set_profile_table2characters_has_profiles($profile)
432         {
433                 foreach($profile as $k => $v) {
434                         if (empty($v['ProfileTable'])) {
435                                 continue;
436                         }
437                         $tmp = array();
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];
443                                         } else {
444                                                 $tmp[] = null;
445                                         }
446                                 }
447                         }
448                         $profile[$k]['CharactersHasProfile'] = $tmp;
449                         }
450
451                 return $profile;
452         }
453
454         /* POSTのCharactersHasProfileを処理 */
455         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
456         {
457                 $prev_profile_id = null;
458                 $profileTable_tmp = array();
459                 $i = 0;
460                 foreach ($characters_has_profiles as $k => $v) {
461                         if (!empty($character_id)) {
462                                 $characters_has_profiles[$k]['character_id'] = $character_id;
463                         }
464
465                         // 配列valueの処理
466                         if (is_array($v['value'])) {
467                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
468                         }
469
470                         // 空の値処理
471                         // table: 一時保管して全項目空の場合行削除
472                         if (CorePlus::is_valid($v, 'profile_table_id')) {
473                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
474                                         $i++;
475                                 }
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]);
480                                         continue;*/
481                         }
482                 }
483
484                 // table: 空行削除、不正profile_table_idデータ削除
485                 if (!empty($profileTable_tmp)) {
486                         if (!isset($this->Character->ProfileTable)) {
487                                 $this->Character->set_model('ProfileTable');
488                         }
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'),
494                                         'recursive' => -1,
495                                 ));
496
497                                 foreach ($v as $i => $profile_tables) {
498                                         // 不正POSTの削除
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]]);
503                                                 }
504                                         }
505
506                                         // 空行削除
507                                         $delete_flg = true;
508                                         foreach($profile_table_columns as $k => $columns) {
509                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
510                                                         $delete_flg = false;
511                                                         break;
512                                                 }
513                                         }
514                                         if ($delete_flg === true) {
515                                                 foreach ($profile_tables as $delk) {
516                                                         unset($characters_has_profiles[$delk[1]]);
517                                                 }
518                                         }
519                                 }
520                         }
521                 }
522
523                 // 指定データのみ変更
524                 if (!is_null($profile_id) && !empty($now_data)) {
525                         $tmp = array();
526                         $i = 0;
527                         foreach($now_data as $k => $v) {
528                                 if ($v['id'] == $profile_id) {
529                                         $v['CharactersHasProfile'] = $characters_has_profiles;
530                                 }
531
532                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
533                                         $tmp[$i] = $v2;
534                                         if (isset($tmp[$i]['id'])) {
535                                                 unset($tmp[$i]['id']);
536                                         }
537                                         if (isset($tmp[$i]['character_id'])) {
538                                                 unset($tmp[$i]['character_id']);
539                                         }
540
541                                         $i++;
542                                 }
543                         }
544
545                         $characters_has_profiles = $tmp;
546                 }
547
548                 return $characters_has_profiles;
549         }
550
551         // CharactersHasProfileの追加valiadte設定
552         function _set_validate4characters_has_profile($system_id)
553         {
554                 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
555                         'rule' => array('validProfileId', $system_id),
556                 );
557                 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
558                         'rule' => array('validProfileSelectId', $system_id),
559                         'allowEmpty' => true,
560                 );
561                 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
562                 'rule' => array('validProfiletableId', $system_id),
563                 'allowEmpty' => true,
564                 );
565         }
566
567         /* restore_html */
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);
571                 if ($nl2br) {
572                         $data['notes'] = nl2br($data['notes']);
573                 }
574
575                 return $data;
576         }
577
578         function _restore_html_characters_has_profiles($profile, $nl2br = false) {
579                 if (empty($profile)) {
580                         return null;
581                 }
582
583                 foreach($profile as $k => $v) {
584                         if (empty($v['CharactersHasProfile'])) {
585                                 continue;
586                         }
587                         $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br);
588                 }
589
590                 return $profile;
591         }
592         function __restore_html_characters_has_profiles($data, $nl2br = false) {
593                 if (empty($data)) {
594                         return null;
595                 }
596
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);
600                                 if ($nl2br) {
601                                 }
602                         }
603                 }
604
605                 return $data;
606         }
607
608         /* status設定をview用にセット */
609         function set_status4view()
610         {
611                 $model_status = $this->get_status();
612
613                 $this->set('status', $model_status);
614         }
615         function get_status()
616         {
617                 return $this->Character->status;
618         }
619
620 }
621