OSDN Git Service

Document追加
[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         /* テーマ */
12         var $view = 'View';
13         var $theme = null;
14
15         var $disableTokenActions = array();
16         var $post_data = array();
17
18         /* メソッド */
19
20         function beforeFilter() {
21
22                 parent::beforeFilter();
23
24                 // Cache
25                 $this->cacheAction = array(
26                         'index' => Configure::read('Cache.expireShort'),
27                 );
28
29                 // 認証なしアクセス可
30                 $this->AuthPlus->allow('view');
31                 $this->AuthPlus->allow('index');
32         }
33
34         function beforeRender()
35         {
36                 parent::beforeRender();
37
38                 $this->set_public_flag4view();
39                 $this->set_status4view();
40         }
41
42
43         /* アクションメソッド */
44
45         function index($id = null) {
46                 if (!empty($id) && $id == $this->user_id) {
47                         $this->redirect(array('action'=>'mycharacter'));
48                 }
49
50                 $this->set('isOwner', false);
51                 $this->set('isOwner_userSet', false);
52
53                 $this->_index($id);
54         }
55
56         function mycharacter(){
57
58                 $this->set('isOwner', true);
59                 $this->set('isOwner_userSet', true);
60
61                 $this->_index($this->user_id);
62         }
63
64         function view($id = null) {
65                 if (!$id) {
66                         $this->Session->setFlash(__('Invalid Character.', true));
67                         $this->redirect(array('action'=>'index'));
68                 }
69
70                 $character = $this->_view($id, array(), false);
71                 // Systemチェック
72                 if (!$this->check_public_flag($character['System'])) {
73                         unset($character['System']['Profile']);
74                 }
75
76                 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
77
78                 $isOwner = false;
79                 if ($this->isOwner($character['Character'], $this->user_id)) {
80                         $isOwner = true;
81                 }
82                 $this->set('isOwner', $isOwner);
83
84                 // キャラ画像のpublic_flagチェック
85                 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
86                         foreach($character['CharacterPicture'] as $k => $v) {
87                                 if (!$this->check_public_flag2($v)) {
88                                         unset($character['CharacterPicture'][$k]);
89                                 }
90                         }
91                 }
92
93                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
94         }
95
96         function add() {
97                 $systems = $this->_get_systems();
98
99                 if (!empty($this->data)) {
100                         $this->data['Character']['user_id'] = $this->user_id;
101
102                         $this->Character->create();
103                         if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
104
105                                 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
106                                 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
107                         } else {
108                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
109                         }
110                 }
111
112                 $this->set('systems', $systems);
113                 $this->set('isOwner', false);
114
115                 $this->pageTitle .= " ". __('Add Character', true);
116         }
117
118         function edit($id = null) {
119                 if (!$id && empty($this->data)) {
120                         $this->Session->setFlash(__('Invalid ID.', true));
121                         $this->redirect(array('action'=>'index'));
122                 }
123
124                 // Characterデータ取得
125                 $this->Character->System->hasMany['Profile']['fields'] = '';
126                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
127                 unset($this->Character->hasMany['CharacterProfileArchive']);
128
129                 $character = $this->_get_character($id, array(), false);
130                 if (!$this->isOwner($character['Character'], $this->user_id)) {
131                         $this->Session->setFlash(__('No Permission', true));
132                         $this->redirect(array('action'=>'index'));
133                 }
134
135                 // Systemチェック
136                 if (!$this->check_public_flag($character['System'])) {
137                         $this->redirect(array('action'=>'change_system', $id));
138                 }
139
140                 if (empty($this->data)) {
141                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
142                 }
143
144                 if (!empty($this->data)) {
145                         $this->post_data = $this->data;
146
147                         $this->data['clearCache'] = array(
148                                 'character_id' => $id,
149                                 'user_id' => $this->user_id,
150                                 'system_id' => $character['System']['id'],
151                         );
152
153                         // 新hasProfile処理
154                         if (isset($this->data['CharactersHasProfile'])) {
155                                 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
156                         }
157
158                         /* validate */
159                         $this->_set_validate4characters_has_profile($character['System']['id']);
160                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
161
162                                 // 現在のhas_profiles削除
163                                 $this->Character->CharactersHasProfile->deleteAll(array(
164                                         'CharactersHasProfile.character_id' => $id
165                                 ));
166
167                                 /* データ保存 */
168                                 $this->data['Character']['id'] = $id;
169                                 $this->Character->create();
170
171                                 if ($this->Character->saveAll($this->data, array(
172                                         'validate' => false,
173                                          'fieldList' => array_merge(
174                                                 $this->Character->fields['edit'], 
175                                                 $this->Character->CharactersHasProfile->fields['add']
176                                          )
177                                 ))) {
178
179                                         // Archives保存
180                                         if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
181                                                 $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
182                                         }
183
184                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
185                                         $this->redirect(array('action'=>'view', $id));
186                                 }
187                         }
188
189                         $this->data = array_merge($character, $this->data);
190                         $this->data['Character'] = $this->post_data['Character'];
191                         $this->data['Character']['id'] = $id;
192
193                         foreach($this->data['System']['Profile'] as $k1 => $profile) {
194                                 foreach($profile['CharactersHasProfile'] as $k2 => $v) {
195                                         $postdata = array_shift($this->post_data['CharactersHasProfile']);
196                                         if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
197                                                 array_unshift($this->post_data['CharactersHasProfile'], $postdata);
198                                                 continue;
199                                         } 
200                                         if (is_array($postdata['value'])) {
201                                                 $postdata['value'] = $postdata['value'][0];
202                                         }
203                                         $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
204                                         if (isset($postdata['profile_select_id'])) {
205                                                 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
206                                         }
207                                 }
208                         }
209                 }
210
211                 if (empty($this->data)) {
212                         $this->data = $character;
213                         $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
214
215                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
216                 }
217                 $this->set('isOwner', true);
218
219                 $this->pageTitle .= " ". sprintf(__('Edit %s', true), $character['Character']['name']);
220         }
221
222         function add_milti_profiles($id = null) {
223                 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
224                         $this->Session->setFlash(__('Invalid ID.', true));
225                         $this->redirect(array('action'=>'index'));
226                 }
227
228                 // Characterデータ取得
229                 $this->Character->System->hasMany['Profile']['fields'] = '';
230                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
231                 unset($this->Character->hasMany['CharacterProfileArchive']);
232
233                 $character = $this->_get_character($id);
234                 if (!$this->isOwner($character['Character'], $this->user_id)) {
235                         $this->Session->setFlash(__('No Permission', true));
236                         $this->redirect(array('action'=>'index'));
237                 }
238                 // Systemチェック
239                 if (!$this->check_public_flag($character['System'])) {
240                         $this->redirect(array('action'=>'change_system', $id));
241                 }
242
243                 // Profilesデータ取得
244                 if (empty($character['System']['Profile'])) {
245                         $this->Session->setFlash(__('Invalid Profile.', true));
246                         $this->redirect(array('action'=>'view', $id));
247                 }
248
249                 $profile_id = $this->params['named']['profile_id'];
250
251                 if (empty($this->data)) {
252                         $target = array();
253                         foreach ($character['System']['Profile'] as $profile) {
254                                 if ($profile['id'] == $profile_id) {
255                                         $target = $profile;
256                                         break;
257                                 }
258                         }
259
260                         if (!$target) {
261                                 $this->Session->setFlash(__('No Profile.', true));
262                                 $this->redirect(array('action'=>'view', $id));
263                         }
264
265                         $character['System']['Profile'] = array();
266                         $character['System']['Profile'][0] = $target;
267
268                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
269                 }
270
271                 if (!empty($this->data)) {
272                         $this->data['clearCache'] = array(
273                                 'character_id' => $id,
274                                 'user_id' => $this->user_id,
275                                 'system_id' => $character['System']['id'],
276                         );
277
278                         $this->post_data = $this->data;
279
280                         $this->data['Character'] = array_merge($character['Character'], $this->data['Character']);
281                         $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
282                         $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
283                         $this->data['Character']['modified'] = null;
284
285                         // 新hasProfile処理
286                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
287
288                         /* validate */
289                         $this->_set_validate4characters_has_profile($character['System']['id']);
290                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
291
292                                 // 現在のhas_profiles削除
293                                 $this->Character->CharactersHasProfile->deleteAll(array(
294                                         'CharactersHasProfile.character_id' => $id
295                                 ));
296
297                                 /* データ保存 */
298                                 $this->Character->create();
299                                 if ($this->Character->saveAll($this->data, array(
300                                         'validate' => false,
301                                          'fieldList' => array_merge(
302                                                 $this->Character->CharactersHasProfile->fields['add']
303                                          )
304                                 ))) {
305
306                                         // Archives保存
307                                         if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
308                                                 $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
309                                         }
310
311                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
312                                         $this->redirect(array('action'=>'view', $id));
313                                 } else {
314                                         $this->data = array_merge($character, $this->data);
315                                 }
316                         }
317
318                         $this->data = $character;
319                         $this->data['Character'] = $this->post_data['Character'];
320                         $this->data['Character']['id'] = $id;
321
322                 }
323
324                 if (empty($this->data)) {
325                         $this->data = $character;
326                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
327                 }
328
329                 $this->set('isOwner', true);
330
331                 if (isset($this->params['named']['num'])) {
332                         $form_nums = $this->params['named']['num'];
333                 } else {
334                         $form_nums = 3;
335                 }
336                 $this->set('form_nums', $form_nums);
337
338                 $this->pageTitle .= " ". sprintf(__('Edit %s', true), $character['Character']['name']);
339         }
340
341         function change_system($id = null) {
342                 if (!$id) {
343                         $this->Session->setFlash(__('Invalid ID.', true));
344                         $this->redirect(array('action'=>'index'));
345                 }
346
347                 // Characterデータ取得
348                 $this->Character->System->hasMany['Profile']['fields'] = '';
349                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
350
351                 $character = $this->_get_character($id);
352                 if (!$this->isOwner($character['Character'], $this->user_id)) {
353                         $this->Session->setFlash(__('No Permission', true));
354                         $this->redirect(array('action'=>'index'));
355                 }
356                 // Systemチェック
357                 // Systemあり
358                 if (isset($character['System']) && !empty($character['System'])) {
359                         // 公開状態は変更不可
360                         if ($this->check_public_flag($character['System'])) {
361                                 $this->redirect(array('action'=>'edit', $id));
362                         // System非公開
363                         } else {
364                                 $this->set('isChange', true);
365                         }
366                 }
367
368                 if (!empty($this->data)) {
369                         $this->data['clearCache'] = array(
370                                 'character_id' => $id,
371                                 'user_id' => $this->user_id,
372                                 'system_id' => $character['System']['id'],
373                         );
374
375                         $this->data['Character']['id'] = $id;
376
377                         // validateはsystem_idのみ
378                         $this->Cahracter['validate'] = array(
379                                 'system_id' => array(
380                                         'validSystemId' => array(
381                                                 'rule' => array('validSystemId', true),
382                                                 'allowEmpty' => false,
383                                         )
384                                 ),
385                         );
386
387                         if ($this->Character->save(
388                                 $this->data,
389                                 array(
390                                         'validate' => true,
391                                         'fieldList' => array('system_id'),
392                                 )
393                         )) {
394                                 $this->Session->setFlash(__('The new system has been saved', true));
395                                 $this->redirect(array('action'=>'view', $id));
396                         } else {
397                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
398                         }
399                 }
400
401                 $this->set('isOwner', true);
402                 $this->set('character', $character);
403
404                 $systems = $this->_get_systems();
405                 $this->set('systems', $systems);
406
407                 $this->pageTitle .= " ". sprintf(__('%s Change System', true), $character['Character']['name']);
408         }
409
410
411         function set_status($id = null) {
412                 if (!$id || !isset($this->params['named']['mode'])) {
413                         $this->Session->setFlash(__('Invalid ID.', true));
414                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
415                 }
416
417                 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
418
419                 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
420                         $this->Session->setFlash(__('Invalid URL.', true));
421                         $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
422                 }
423
424                 // 新picture設定
425                 $new_character_picture = null;
426                 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
427                         if ($character['CharacterPicture']) {
428                                 foreach($character['CharacterPicture'] as $k => $v) {
429                                         if ($v['id'] == $this->params['named']['new_picture_id']) {
430                                                 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
431                                                 break;
432                                         }
433                                 }
434                         }
435                         if (!$new_character_picture) {
436                                 $this->Session->setFlash(__('Invalid data.', true));
437                                 $this->redirect(array('controller' => 'character_pictures', 'action'=>'listview', $id));
438                         }
439                 }
440
441
442                 $this->data['clearCache'] = array(
443                         'character_id' => $id,
444                         'user_id' => $this->user_id,
445                         'system_id' => $character['Character']['system_id'],
446                 );
447
448                 // 設定変更
449                 $this->Character->id = $id;
450                 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
451                 $this->Character->save(
452                         $this->data, 
453                         array(
454                                 'fieldList' => array(
455                                         $this->params['named']['mode']
456                                 ), 
457                         )
458                 );
459
460                 $this->Character->deleteCache4CharacterPicture($id);
461
462
463                 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
464                 $this->redirect(array('controller' => 'character_pictures', 'action' => 'listview', $id));
465         }
466
467         function delete($id = null) {
468                 if (!$id) {
469                         $this->Session->setFlash(__('Invalid id for Character', true));
470                         $this->redirect(array('action'=>'index'));
471                 }
472
473                 if ($this->_delete($id)) {
474                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
475                 }
476
477                 $this->pageTitle .= " - ". __('Delete Character', true);
478         }
479
480         function admin_index($id = null) {
481                 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
482                 $this->_index($id, array('isAdmin' => true));
483         }
484
485         function admin_view($id = null) {
486                 if (!$id) {
487                         $this->Session->setFlash(__('Invalid Character.', true));
488                         $this->redirect(array('action' => 'admin_index'));
489                 }
490
491                 $character = $this->_view($id, array(), true);
492
493                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
494         }
495
496         function admin_edit($id = null) {
497                 if (!$id && empty($this->data)) {
498                         $this->Session->setFlash(__('Invalid ID.', true));
499                         $this->redirect(array('action'=>'index'));
500                 }
501
502                 // Characterデータ取得
503                 $this->Character->System->hasMany['Profile']['fields'] = '';
504                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
505                 unset($this->Character->hasMany['CharacterProfileArchive']);
506
507                 $character = $this->_get_character($id, array(), true);
508                 if (!$character) {
509                         $this->Session->setFlash(__('No Character', true));
510                         $this->redirect(array('action'=>'index'));
511                 }
512
513                 if (empty($this->data)) {
514                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
515                 }
516
517                 if (!empty($this->data)) {
518
519                         $this->data['clearCache'] = array(
520                                 'character_id' => $id,
521                                 'user_id' => $this->user_id,
522                                 'system_id' => $character['System']['id'],
523                         );
524
525                         if (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_private') {
526                                 $this->data['Character']['public_flag'] = 'private';
527                         } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_public') {
528                                 $this->data['Character']['public_flag'] = 'public';
529                         } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'delete_confirm') {
530                                 $this->redirect(array('controller' => 'characters', 'action'=>'admin_delete', $id));
531                         } else {
532                                 $this->Session->setFlash(sprintf(__('Invalid data.', true), $this->data['Character']['name']));
533                                 $this->redirect(array('action'=>'view', $id));
534                         }
535
536                         $this->data['Character']['id'] = $id;
537                         $this->Character->create();
538                         if ($this->Character->save($this->data, array(
539                                 'validate' => true,
540                                 'fieldList' => array(
541                                         'public_flag',
542                                 ),
543                         ))) {
544
545                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $character['Character']['name']));
546                                         $this->redirect(array('action'=>'view', $id));
547                                 }
548
549                         $this->data = array_merge($character, $this->data);
550                         $this->data['Character']['id'] = $id;
551                 }
552
553                 if (empty($this->data)) {
554                         $this->data = $character;
555                         $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
556
557                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
558                 }
559
560                 $this->pageTitle .= " ". sprintf(__('Edit %s', true), $character['Character']['name']);
561         }
562
563         function admin_delete($id = null) {
564                 if (!$id) {
565                         $this->Session->setFlash(__('Invalid id for Character', true));
566                         $this->redirect(array('action'=>'index'));
567                 }
568
569                 if ($this->_delete($id, array(), true)) {
570                         $this->redirect(array('action'=>'index'));
571                 }
572
573                 $this->pageTitle .= " - ". __('Delete Character', true);
574         }
575
576
577         /* 共通化アクションメソッド */
578         function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
579                 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
580                         $isAdmin = true;
581                 } else {
582                         $isAdmin = false;
583                 }
584                 unset($conditions['isAdmin']);
585
586                 $title = __('List of All Characters', true);
587
588                 // System指定分岐
589                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
590                         //情報取得
591                         $conditions['System.id'] = $this->params['named']['system'];
592                         if ($isAdmin === false) {
593                                 $conditions['System.public_flag'] = 'public';
594                         }
595
596                         $this_system = $this->Character->System->find('first', array(
597                                 'conditions' => $conditions,
598                                 'recursive' => -1,
599                         ));
600
601                         if (!isset($this_system['System'])) {
602                                 $this->Session->setFlash(__('Invalid System.', true));
603                                 $this->redirect(array('controller' => 'systems', 'action' => 'index'));
604                         }
605                         $this_system = $this->_restore_html_system($this_system);
606
607                         $title = $this_system['System']['name']. " ". $title;
608
609                         $this->set('this_system', $this_system);
610                 } else {
611                         $contain = array_merge(array('System'), (array)$contain);
612                 }
613
614                 $user = array();
615                 $conditions = array('User.id' => $id);
616
617                 if (!empty($id)) {
618                         $user = $this->Character->User->find('first', array(
619                                 'conditions' => $conditions,
620                                 'recursive' => -1,
621                                 'fields' => array(
622                                         'User.id',
623                                         'User.name',
624                                 ),
625                         ));
626
627                         if (empty($user)) {
628                                 $this->Session->setFlash(__('Invalid Id.', true));
629                                 $this->redirect(array('action'=>'index'));
630                         }
631
632                         $title = $user['User']['name']. " ". $title;
633                 } else {
634                         $contain = array_merge($contain, array('User'));
635                 }
636                 $this->set('userSet', $user);
637
638                 $conditions = array();
639                 if (!empty($id)) {
640                         $conditions['Character.user_id'] = $id;
641                         if ($id == $this->user_id) {
642                                 unset($this->paginate['conditions']['Character.public_flag']);
643                         }
644                 }
645                 if ($isAdmin === true) {
646                         $conditions['isAdmin'] = true;
647                 }
648                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
649                         $conditions['Character.system_id'] = $this->params['named']['system'];
650                 }
651                  $characters =  $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
652
653                 $this->set('characters', $characters);
654
655                 $this->pageTitle .= " - ". $title;
656         }
657
658         function _view($id, $conditions = array(), $isAdmin = false) {
659                 $this->helpers[] = 'CharacterSheet';
660
661                 // キャラクターシート設定
662                 $mode = null;
663                 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
664                         $this->view = 'Theme';
665                         $this->theme = $this->params['named']['mode'];
666
667                 }
668
669                 // キャラデータ取得
670                 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
671                 $character = $this->Character->set_profiles2view($orig_character);
672
673                 Configure::write('isOwner', false);
674                 if ($isAdmin || CorePlus::isOwner($character['Character'], $this->user_id)) {
675                         Configure::write('isOwner', true);
676                 }
677
678                 // Systemチェック
679                 $this->set('systemValid', 'public');
680                 if (!$character['System']) {
681                         $this->set('systemValid', false);
682                 }
683                 if (!$this->check_public_flag($character['System'])) {
684                         $this->set('systemValid', 'unpublic');
685                 }
686                 $character = $this->_restore_html_system($character);
687
688                 $this->pageTitle .= " - ". $character['Character']['name'];
689
690                 return $character;
691         } 
692
693         function _delete($id = null, $conditions = array(), $isAdmin = false) {
694                 if (!$id) {
695                         $this->Session->setFlash(__('Invalid id for Character', true));
696                         $this->redirect(array('action'=>'index'));
697                 }
698
699                 $character = $this->_view($id, $conditions, $isAdmin);
700                 if (empty($character)) {
701                         $this->Session->setFlash(__('No Character', true));
702                         $this->redirect(array('action'=>'index'));
703                 }
704                 $this->set('character', $character);
705
706                 if (!$isAdmin && !$this->isOwner($character['Character'], $this->user_id)) {
707                         $this->Session->setFlash(__('No Permission', true));
708                         $this->redirect(array('action'=>'index'));
709                 }
710                 $this->set('isOwner', true);
711
712                 if (!empty($this->data)) {
713
714                         $this->data['clearCache'] = array(
715                                 'character_id' => $id,
716                                 'user_id' => $this->user_id,
717                                 'system_id' => $character['System']['id'],
718                         );
719
720                         if ($this->data['Character']['confirm'] == 'yes') {
721                                 // Character
722                                 $this->data['Character']['id'] = $id;
723                                 $this->data['Character']['main_picture'] = null;
724                                 $this->data['Character']['full_length'] = null;
725                                 $this->data['Character']['deleted'] = true;
726                                 $this->data['Character']['deleted_date'] = date('Y-m-d H:i:s');
727
728                                 $this->Character->create();
729                                 if ($this->Character->save(
730                                         $this->data,
731                                         array(
732                                                 'validate' => false,
733                                                 'fieldList' => array(
734                                                         'main_picture',
735                                                         'full_length',
736                                                         'deleted',
737                                                         'deleted_date'
738                                                 ),
739                                         )
740                                 )) {
741
742                                         // CharactersHasProfiles
743                                         $this->Character->CharactersHasProfile->updateAll(
744                                                 array(
745                                                         'CharactersHasProfile.public_flag' => "'private'",
746                                                 ),
747                                                 array(
748                                                         'CharactersHasProfile.character_id' => $id
749                                                 )
750                                         );
751
752                                         // CharacterProfileArchives
753                                         $date = date('Y-m-d H:i:s');
754                                         $this->Character->CharacterProfileArchive->updateAll(
755                                                 array(
756                                                         'CharacterProfileArchive.deleted' => true,
757                                                         'CharacterProfileArchive.deleted_date' => "'$date'",
758                                                 ),
759                                                 array(
760                                                         'CharacterProfileArchive.character_id' => $id
761                                                 )
762                                         );
763
764                                         // Attachments
765                                         if (isset($character['CharacterPicture']) && !empty($character['CharacterPicture'])) {
766                                                 foreach ($character['CharacterPicture'] as $picture) {
767                                                         $this->Character->CharacterPicture->delete($picture['id']);
768                                                 }
769                                         }
770
771                                         $this->Session->setFlash(__('Character deleted', true));
772
773                                         return true;
774                                 } else {
775                                         $this->Session->setFlash(__('The Character could not be deleted.', true));
776                                 }
777                         }
778                 }
779
780                 return false;
781         }
782
783         /* 共通関数 */
784         function _get_character($id, $conditions = array(), $isAdmin = false)
785         {
786                 $character = $this->Character->get_character($id, $conditions, $isAdmin);
787
788                 if (empty($character)) {
789                         $this->Session->setFlash(__('No Character', true));
790                         $this->redirect(array('action'=>'index'));
791                 }
792
793                 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
794                         $this->Session->setFlash(__('No Permission', true));
795                         $this->redirect(array('action'=>'index'));
796                 }
797
798                 if (isset($character['System']['Profile'])) {
799                         $character['System'] = $this->_restore_html_profile($character['System']);
800                 }
801
802                 return $character;
803         }
804
805         // ProfileTableをCharacterHasProfilesのTableの形式にセット
806         function _set_profile_table2characters_has_profiles($profile)
807         {
808                 if (!empty($profile[0]['CharactersHasProfile'])) {
809                         return $profile;
810                 }
811
812                 foreach($profile as $k => $v) {
813                         if (empty($v['ProfileTable'])) {
814                                 continue;
815                         }
816                         $tmp = array();
817                         $rows = count($v['CharactersHasProfile']);
818                         for($i=0; $i<$rows; $i++) {
819                                 foreach($v['ProfileTable'] as $k2 => $v2) {
820                                         if (isset($v2['CharactersHasProfile'][$i])) {
821                                                 $tmp[] = $v2['CharactersHasProfile'][$i];
822                                         } elseif (isset($v['CharactersHasProfile'][$i])) {
823                                                 $tmp[] = $v['CharactersHasProfile'][$i];
824                                                 break;
825
826                                         } else {
827                                                 $tmp[] = null;
828                                         }
829                                 }
830                         }
831                         $profile[$k]['CharactersHasProfile'] = $tmp;
832                         }
833
834                 return $profile;
835         }
836
837         /* POSTのCharactersHasProfileを処理 */
838         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
839         {
840                 $prev_profile_id = null;
841                 $profileTable_tmp = array();
842                 $i = 0;
843                 $public = null;
844                 foreach ($characters_has_profiles as $k => $v) {
845                         if (!empty($character_id)) {
846                                 $characters_has_profiles[$k]['character_id'] = $character_id;
847                         }
848                         // 配列valueの処理
849                         if (is_array($v['value'])) {
850                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
851                         }
852
853                         // サニタイズ
854                         $characters_has_profiles[$k]['value'] = Sanitize::html($characters_has_profiles[$k]['value']);
855                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
856                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
857                         // textarea改行処理
858                         if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
859                                 $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
860                                 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
861                         }
862                         $characters_has_profiles[$k]['value'] = Sanitize::stripAll($characters_has_profiles[$k]['value']);
863
864                         // 空の値処理, public_flag
865                         // table: 一時保管して全項目空の場合行削除
866                         if (CorePlus::is_valid($v, 'profile_table_id')) {
867                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
868                                         $i++;
869                                         $public = null;
870                                 }
871
872                                 // 行単位public_flag
873                                 if ($public == null && isset($characters_has_profiles[$k]['public_flag'])) {
874                                         $public = $v['public_flag'];
875                                         // Static設定タイトルは公開
876                                         if ($v['profile_table_static_id']) {
877                                                 $characters_has_profiles[$k]['public_flag'] = 'public';
878                                         }
879                                 } else {
880                                         $characters_has_profiles[$k]['public_flag'] = $public;
881                                 }
882
883                                 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
884                         // それ以外は削除
885                         } elseif (empty($v['value'])) {
886                                         unset($characters_has_profiles[$k]);
887                         }
888                 }
889
890                 // table: 空行削除、不正profile_table_idデータ削除
891                 if (!empty($profileTable_tmp)) {
892                         if (!isset($this->Character->ProfileTable)) {
893                                 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
894                         }
895                         foreach($profileTable_tmp as $profile_id => $v) {
896                                 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
897                                         'conditions' => array('ProfileTable.profile_id' => $profile_id),
898                                         'fields' => array('ProfileTable.id'),
899                                         'order' => array('ProfileTable.sort_order' => 'asc'),
900                                         'recursive' => -1,
901                                 ));
902
903                                 foreach ($v as $i => $profile_tables) {
904                                         // 不正POSTの削除
905                                         $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
906                                         if (!empty($diff_keys)) {
907                                                 foreach($diff_keys as $dvalue) {
908                                                         unset($characters_has_profiles[$dvalue[1]]);
909                                                 }
910                                         }
911
912                                         // 空行削除
913                                         $delete_flg = true;
914                                         foreach($profile_table_columns as $k2 => $columns) {
915                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
916                                                         $delete_flg = false;
917                                                         break;
918                                                 }
919                                         }
920                                         if ($delete_flg === true) {
921                                                 foreach ($profile_tables as $delk) {
922                                                         unset($characters_has_profiles[$delk[1]]);
923                                                 }
924                                         }
925                                 }
926                         }
927                 }
928
929                 // 指定データのみ変更
930                 if (!is_null($profile_id) && !empty($now_data)) {
931                         $tmp = array();
932                         $i = 0;
933                         foreach($now_data as $k => $v) {
934                                 if ($v['id'] == $profile_id) {
935                                         $v['CharactersHasProfile'] = $characters_has_profiles;
936                                 }
937
938                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
939                                         $tmp[$i] = $v2;
940                                         if (isset($tmp[$i]['id'])) {
941                                                 unset($tmp[$i]['id']);
942                                         }
943                                         if (isset($tmp[$i]['character_id'])) {
944                                                 unset($tmp[$i]['character_id']);
945                                         }
946
947                                         $i++;
948                                 }
949                         }
950
951                         $characters_has_profiles = $tmp;
952                 }
953
954                 return $characters_has_profiles;
955         }
956
957         // CharactersHasProfileの追加valiadte設定
958         function _set_validate4characters_has_profile($system_id)
959         {
960                 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
961                         'rule' => array('validProfileId', $system_id),
962                 );
963                 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
964                         'rule' => array('validProfileSelectId', $system_id),
965                         'allowEmpty' => true,
966                 );
967                 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
968                 'rule' => array('validProfiletableId', $system_id),
969                 'allowEmpty' => true,
970                 );
971         }
972
973         /* restore_html */
974         function _restore_html_character($data, $nl2br = false) {
975                 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
976                 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
977                 if ($nl2br) {
978                         $data['notes'] = str_replace('<br />', "\n", $data['notes']);
979                 }
980
981                 return $data;
982         }
983
984         function _restore_html_characters_has_profiles($profile, $nl2br = false) {
985                 if (empty($profile)) {
986                         return null;
987                 }
988
989                 foreach($profile as $k => $v) {
990                         if (empty($v['CharactersHasProfile'])) {
991                                 continue;
992                         }
993                         $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
994                 }
995
996                 return $profile;
997         }
998         function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
999                 if (empty($data)) {
1000                         return null;
1001                 }
1002
1003                 foreach ($data as $k => $v) {
1004                         if (isset($v['value'])) {
1005                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
1006                                 if ($nl2br && $profile_type == 'textarea') {
1007                                         $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);
1008                                 }
1009                         }
1010                 }
1011
1012                 return $data;
1013         }
1014
1015 }