OSDN Git Service

ナビなどデザイン修正
[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                         'order' => array('Character.modified' => 'desc'),
336                         'contain' => array(
337                                 'User',
338                                 'System' => array(
339                                         'Profile' => array(
340                                                 'CharactersHasProfile',
341                                                 'ProfileTable',
342                                         ),
343                                 ),
344                         ),
345                         'recursive' => 3,
346                 ));
347
348                 if (empty($orig_character['Character'])) {
349                         return $orig_character;
350                 }
351
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'];
360                                 }
361                                 $new_profile[$profile['key_name']]['CharactersHasProfile'] = $profile['CharactersHasProfile'];
362                         }
363
364                         $orig_character['System']['Profile'] = $new_profile;
365                 }
366
367                 $character = array(
368                         'Character' => $orig_character['Character'],
369                         'System' => $orig_character['System'],
370                         'User' => $orig_character['User']
371                 );
372
373
374                 return $character;
375         } 
376
377
378         /* 共通関数 */
379         function _get_character4edit($conditions, $character_id)
380         {
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;
383
384                 if (!empty($this->data)) {
385                         $character = $this->Character->find('first', array(
386                                 'conditions' => $conditions,
387                                 'contain' => array(
388                                         'User',
389                                         'System' => array(
390                                                 'Profile' => array(
391                                                         'ProfileSelect',
392                                                         'ProfileTable',
393                                                         'CharactersHasProfile',
394                                                 ),
395                                         ),
396                                 ),
397                                 'recursive' => 3,
398                         ));
399                 } else {
400                         $character = $this->Character->find('first', array(
401                                 'conditions' => $conditions,
402                                 'contain' => array(
403                                         'User',
404                                         'System' => array(
405                                                 'Profile' => array(
406                                                         'ProfileSelect',
407                                                         'ProfileTable' => array(
408                                                                 'CharactersHasProfile',
409                                                         ),
410                                                         'CharactersHasProfile',
411                                                 ),
412                                         ),
413                                 ),
414                                 'recursive' => 4,
415                         ));
416                 }
417
418                 if (empty($character)) {
419                         $this->Session->setFlash(__('No Character', true));
420                         $this->redirect(array('action'=>'index'));
421                 }
422
423                 if (!$this->isOwner($character['Character'], $this->user_id)) {
424                         $this->Session->setFlash(__('No Permission', true));
425                         $this->redirect(array('action'=>'index'));
426                 }
427         
428                 return $character;
429         }
430
431         // ProfileTableをCharacterHasProfilesのTableの形式にセット
432         function _set_profile_table2characters_has_profiles($profile)
433         {
434                 foreach($profile as $k => $v) {
435                         if (empty($v['ProfileTable'])) {
436                                 continue;
437                         }
438                         $tmp = array();
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];
444                                         } else {
445                                                 $tmp[] = null;
446                                         }
447                                 }
448                         }
449                         $profile[$k]['CharactersHasProfile'] = $tmp;
450                         }
451
452                 return $profile;
453         }
454
455         /* POSTのCharactersHasProfileを処理 */
456         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
457         {
458                 $prev_profile_id = null;
459                 $profileTable_tmp = array();
460                 $i = 0;
461                 foreach ($characters_has_profiles as $k => $v) {
462                         if (!empty($character_id)) {
463                                 $characters_has_profiles[$k]['character_id'] = $character_id;
464                         }
465
466                         // 配列valueの処理
467                         if (is_array($v['value'])) {
468                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
469                         }
470
471                         // 空の値処理
472                         // table: 一時保管して全項目空の場合行削除
473                         if (CorePlus::is_valid($v, 'profile_table_id')) {
474                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
475                                         $i++;
476                                 }
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]);
481                                         continue;*/
482                         }
483                 }
484
485                 // table: 空行削除、不正profile_table_idデータ削除
486                 if (!empty($profileTable_tmp)) {
487                         if (!isset($this->Character->ProfileTable)) {
488                                 $this->Character->set_model('ProfileTable');
489                         }
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'),
495                                         'recursive' => -1,
496                                 ));
497
498                                 foreach ($v as $i => $profile_tables) {
499                                         // 不正POSTの削除
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]]);
504                                                 }
505                                         }
506
507                                         // 空行削除
508                                         $delete_flg = true;
509                                         foreach($profile_table_columns as $k => $columns) {
510                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
511                                                         $delete_flg = false;
512                                                         break;
513                                                 }
514                                         }
515                                         if ($delete_flg === true) {
516                                                 foreach ($profile_tables as $delk) {
517                                                         unset($characters_has_profiles[$delk[1]]);
518                                                 }
519                                         }
520                                 }
521                         }
522                 }
523
524                 // 指定データのみ変更
525                 if (!is_null($profile_id) && !empty($now_data)) {
526                         $tmp = array();
527                         $i = 0;
528                         foreach($now_data as $k => $v) {
529                                 if ($v['id'] == $profile_id) {
530                                         $v['CharactersHasProfile'] = $characters_has_profiles;
531                                 }
532
533                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
534                                         $tmp[$i] = $v2;
535                                         if (isset($tmp[$i]['id'])) {
536                                                 unset($tmp[$i]['id']);
537                                         }
538                                         if (isset($tmp[$i]['character_id'])) {
539                                                 unset($tmp[$i]['character_id']);
540                                         }
541
542                                         $i++;
543                                 }
544                         }
545
546                         $characters_has_profiles = $tmp;
547                 }
548
549                 return $characters_has_profiles;
550         }
551
552         // CharactersHasProfileの追加valiadte設定
553         function _set_validate4characters_has_profile($system_id)
554         {
555                 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
556                         'rule' => array('validProfileId', $system_id),
557                 );
558                 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
559                         'rule' => array('validProfileSelectId', $system_id),
560                         'allowEmpty' => true,
561                 );
562                 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
563                 'rule' => array('validProfiletableId', $system_id),
564                 'allowEmpty' => true,
565                 );
566         }
567
568         /* restore_html */
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);
572                 if ($nl2br) {
573                         $data['notes'] = nl2br($data['notes']);
574                 }
575
576                 return $data;
577         }
578
579         function _restore_html_characters_has_profiles($profile, $nl2br = false) {
580                 if (empty($profile)) {
581                         return null;
582                 }
583
584                 foreach($profile as $k => $v) {
585                         if (empty($v['CharactersHasProfile'])) {
586                                 continue;
587                         }
588                         $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br);
589                 }
590
591                 return $profile;
592         }
593         function __restore_html_characters_has_profiles($data, $nl2br = false) {
594                 if (empty($data)) {
595                         return null;
596                 }
597
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);
601                                 if ($nl2br) {
602                                 }
603                         }
604                 }
605
606                 return $data;
607         }
608
609         /* status設定をview用にセット */
610         function set_status4view()
611         {
612                 $model_status = $this->get_status();
613
614                 $this->set('status', $model_status);
615         }
616         function get_status()
617         {
618                 return $this->Character->status;
619         }
620
621 }
622