OSDN Git Service

StaticTableの静的行が変更可能になっていたのを修正
[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                 $this->pageTitle .= " ". sprintf(__('Edit %s', true), $character['Character']['name']);
332         }
333
334         function change_system($id = null) {
335                 if (!$id) {
336                         $this->Session->setFlash(__('Invalid ID.', true));
337                         $this->redirect(array('action'=>'index'));
338                 }
339
340                 // Characterデータ取得
341                 $this->Character->System->hasMany['Profile']['fields'] = '';
342                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
343
344                 $character = $this->_get_character($id);
345                 if (!$this->isOwner($character['Character'], $this->user_id)) {
346                         $this->Session->setFlash(__('No Permission', true));
347                         $this->redirect(array('action'=>'index'));
348                 }
349                 // Systemチェック
350                 // Systemあり
351                 if (isset($character['System']) && !empty($character['System'])) {
352                         // 公開状態は変更不可
353                         if ($this->check_public_flag($character['System'])) {
354                                 $this->redirect(array('action'=>'edit', $id));
355                         // System非公開
356                         } else {
357                                 $this->set('isChange', true);
358                         }
359                 }
360
361                 if (!empty($this->data)) {
362                         $this->data['clearCache'] = array(
363                                 'character_id' => $id,
364                                 'user_id' => $this->user_id,
365                                 'system_id' => $character['System']['id'],
366                         );
367
368                         $this->data['Character']['id'] = $id;
369
370                         // validateはsystem_idのみ
371                         $this->Cahracter['validate'] = array(
372                                 'system_id' => array(
373                                         'validSystemId' => array(
374                                                 'rule' => array('validSystemId', true),
375                                                 'allowEmpty' => false,
376                                         )
377                                 ),
378                         );
379
380                         if ($this->Character->save(
381                                 $this->data,
382                                 array(
383                                         'validate' => true,
384                                         'fieldList' => array('system_id'),
385                                 )
386                         )) {
387                                 $this->Session->setFlash(__('The new system has been saved', true));
388                                 $this->redirect(array('action'=>'view', $id));
389                         } else {
390                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
391                         }
392                 }
393
394                 $this->set('isOwner', true);
395                 $this->set('character', $character);
396
397                 $systems = $this->_get_systems();
398                 $this->set('systems', $systems);
399
400                 $this->pageTitle .= " ". sprintf(__('%s Change System', true), $character['Character']['name']);
401         }
402
403
404         function set_status($id = null) {
405                 if (!$id || !isset($this->params['named']['mode'])) {
406                         $this->Session->setFlash(__('Invalid ID.', true));
407                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
408                 }
409
410                 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
411
412                 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
413                         $this->Session->setFlash(__('Invalid URL.', true));
414                         $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
415                 }
416
417                 // 新picture設定
418                 $new_character_picture = null;
419                 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
420                         if ($character['CharacterPicture']) {
421                                 foreach($character['CharacterPicture'] as $k => $v) {
422                                         if ($v['id'] == $this->params['named']['new_picture_id']) {
423                                                 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
424                                                 break;
425                                         }
426                                 }
427                         }
428                         if (!$new_character_picture) {
429                                 $this->Session->setFlash(__('Invalid data.', true));
430                                 $this->redirect(array('controller' => 'character_pictures', 'action'=>'listview', $id));
431                         }
432                 }
433
434
435                 $this->data['clearCache'] = array(
436                         'character_id' => $id,
437                         'user_id' => $this->user_id,
438                         'system_id' => $character['Character']['system_id'],
439                 );
440
441                 // 設定変更
442                 $this->Character->id = $id;
443                 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
444                 $this->Character->save(
445                         $this->data, 
446                         array(
447                                 'fieldList' => array(
448                                         $this->params['named']['mode']
449                                 ), 
450                         )
451                 );
452
453                 $this->Character->deleteCache4CharacterPicture($id);
454
455
456                 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
457                 $this->redirect(array('controller' => 'character_pictures', 'action' => 'listview', $id));
458         }
459
460         function delete($id = null) {
461                 if (!$id) {
462                         $this->Session->setFlash(__('Invalid id for Character', true));
463                         $this->redirect(array('action'=>'index'));
464                 }
465
466                 if ($this->_delete($id)) {
467                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
468                 }
469
470                 $this->pageTitle .= " - ". __('Delete Character', true);
471         }
472
473         function admin_index($id = null) {
474                 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
475                 $this->_index($id, array('isAdmin' => true));
476         }
477
478         function admin_view($id = null) {
479                 if (!$id) {
480                         $this->Session->setFlash(__('Invalid Character.', true));
481                         $this->redirect(array('action' => 'admin_index'));
482                 }
483
484                 $character = $this->_view($id, array(), true);
485
486                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
487         }
488
489         function admin_edit($id = null) {
490                 if (!$id && empty($this->data)) {
491                         $this->Session->setFlash(__('Invalid ID.', true));
492                         $this->redirect(array('action'=>'index'));
493                 }
494
495                 // Characterデータ取得
496                 $this->Character->System->hasMany['Profile']['fields'] = '';
497                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
498                 unset($this->Character->hasMany['CharacterProfileArchive']);
499
500                 $character = $this->_get_character($id, array(), true);
501                 if (!$character) {
502                         $this->Session->setFlash(__('No Character', true));
503                         $this->redirect(array('action'=>'index'));
504                 }
505
506                 if (empty($this->data)) {
507                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
508                 }
509
510                 if (!empty($this->data)) {
511
512                         $this->data['clearCache'] = array(
513                                 'character_id' => $id,
514                                 'user_id' => $this->user_id,
515                                 'system_id' => $character['System']['id'],
516                         );
517
518                         if (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_private') {
519                                 $this->data['Character']['public_flag'] = 'private';
520                         } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_public') {
521                                 $this->data['Character']['public_flag'] = 'public';
522                         } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'delete_confirm') {
523                                 $this->redirect(array('controller' => 'characters', 'action'=>'admin_delete', $id));
524                         } else {
525                                 $this->Session->setFlash(sprintf(__('Invalid data.', true), $this->data['Character']['name']));
526                                 $this->redirect(array('action'=>'view', $id));
527                         }
528
529                         $this->data['Character']['id'] = $id;
530                         $this->Character->create();
531                         if ($this->Character->save($this->data, array(
532                                 'validate' => true,
533                                 'fieldList' => array(
534                                         'public_flag',
535                                 ),
536                         ))) {
537
538                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $character['Character']['name']));
539                                         $this->redirect(array('action'=>'view', $id));
540                                 }
541
542                         $this->data = array_merge($character, $this->data);
543                         $this->data['Character']['id'] = $id;
544                 }
545
546                 if (empty($this->data)) {
547                         $this->data = $character;
548                         $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
549
550                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
551                 }
552
553                 $this->pageTitle .= " ". sprintf(__('Edit %s', true), $character['Character']['name']);
554         }
555
556         function admin_delete($id = null) {
557                 if (!$id) {
558                         $this->Session->setFlash(__('Invalid id for Character', true));
559                         $this->redirect(array('action'=>'index'));
560                 }
561
562                 if ($this->_delete($id, array(), true)) {
563                         $this->redirect(array('action'=>'index'));
564                 }
565
566                 $this->pageTitle .= " - ". __('Delete Character', true);
567         }
568
569
570         /* 共通化アクションメソッド */
571         function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
572                 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
573                         $isAdmin = true;
574                 } else {
575                         $isAdmin = false;
576                 }
577                 unset($conditions['isAdmin']);
578
579                 $title = __('List of All Characters', true);
580
581                 // System指定分岐
582                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
583                         //情報取得
584                         $conditions['System.id'] = $this->params['named']['system'];
585                         if ($isAdmin === false) {
586                                 $conditions['System.public_flag'] = 'public';
587                         }
588
589                         $this_system = $this->Character->System->find('first', array(
590                                 'conditions' => $conditions,
591                                 'recursive' => -1,
592                         ));
593
594                         if (!isset($this_system['System'])) {
595                                 $this->Session->setFlash(__('Invalid System.', true));
596                                 $this->redirect(array('controller' => 'systems', 'action' => 'index'));
597                         }
598                         $this_system = $this->_restore_html_system($this_system);
599
600                         $title = $this_system['System']['name']. " ". $title;
601
602                         $this->set('this_system', $this_system);
603                 } else {
604                         $contain = array_merge(array('System'), (array)$contain);
605                 }
606
607                 $user = array();
608                 $conditions = array('User.id' => $id);
609
610                 if (!empty($id)) {
611                         $user = $this->Character->User->find('first', array(
612                                 'conditions' => $conditions,
613                                 'recursive' => -1,
614                                 'fields' => array(
615                                         'User.id',
616                                         'User.name',
617                                 ),
618                         ));
619
620                         if (empty($user)) {
621                                 $this->Session->setFlash(__('Invalid Id.', true));
622                                 $this->redirect(array('action'=>'index'));
623                         }
624
625                         $title = $user['User']['name']. " ". $title;
626                 } else {
627                         $contain = array_merge($contain, array('User'));
628                 }
629                 $this->set('userSet', $user);
630
631                 $conditions = array();
632                 if (!empty($id)) {
633                         $conditions['Character.user_id'] = $id;
634                         if ($id == $this->user_id) {
635                                 unset($this->paginate['conditions']['Character.public_flag']);
636                         }
637                 }
638                 if ($isAdmin === true) {
639                         $conditions['isAdmin'] = true;
640                 }
641                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
642                         $conditions['Character.system_id'] = $this->params['named']['system'];
643                 }
644                  $characters =  $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
645
646                 $this->set('characters', $characters);
647
648                 $this->pageTitle .= " - ". $title;
649         }
650
651         function _view($id, $conditions = array(), $isAdmin = false) {
652                 $this->helpers[] = 'CharacterSheet';
653
654                 // キャラクターシート設定
655                 $mode = null;
656                 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
657                         $this->view = 'Theme';
658                         $this->theme = $this->params['named']['mode'];
659
660                 }
661
662                 // キャラデータ取得
663                 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
664                 $character = $this->Character->set_profiles2view($orig_character);
665
666                 Configure::write('isOwner', false);
667                 if ($isAdmin || CorePlus::isOwner($character['Character'], $this->user_id)) {
668                         Configure::write('isOwner', true);
669                 }
670
671                 // Systemチェック
672                 $this->set('systemValid', 'public');
673                 if (!$character['System']) {
674                         $this->set('systemValid', false);
675                 }
676                 if (!$this->check_public_flag($character['System'])) {
677                         $this->set('systemValid', 'unpublic');
678                 }
679                 $character = $this->_restore_html_system($character);
680
681                 $this->pageTitle .= " - ". $character['Character']['name'];
682
683                 return $character;
684         } 
685
686         function _delete($id = null, $conditions = array(), $isAdmin = false) {
687                 if (!$id) {
688                         $this->Session->setFlash(__('Invalid id for Character', true));
689                         $this->redirect(array('action'=>'index'));
690                 }
691
692                 $character = $this->_view($id, $conditions, $isAdmin);
693                 if (empty($character)) {
694                         $this->Session->setFlash(__('No Character', true));
695                         $this->redirect(array('action'=>'index'));
696                 }
697                 $this->set('character', $character);
698
699                 if (!$isAdmin && !$this->isOwner($character['Character'], $this->user_id)) {
700                         $this->Session->setFlash(__('No Permission', true));
701                         $this->redirect(array('action'=>'index'));
702                 }
703                 $this->set('isOwner', true);
704
705                 if (!empty($this->data)) {
706
707                         $this->data['clearCache'] = array(
708                                 'character_id' => $id,
709                                 'user_id' => $this->user_id,
710                                 'system_id' => $character['System']['id'],
711                         );
712
713                         if ($this->data['Character']['confirm'] == 'yes') {
714                                 // Character
715                                 $this->data['Character']['id'] = $id;
716                                 $this->data['Character']['main_picture'] = null;
717                                 $this->data['Character']['full_length'] = null;
718                                 $this->data['Character']['deleted'] = true;
719                                 $this->data['Character']['deleted_date'] = date('Y-m-d H:i:s');
720
721                                 $this->Character->create();
722                                 if ($this->Character->save(
723                                         $this->data,
724                                         array(
725                                                 'validate' => false,
726                                                 'fieldList' => array(
727                                                         'main_picture',
728                                                         'full_length',
729                                                         'deleted',
730                                                         'deleted_date'
731                                                 ),
732                                         )
733                                 )) {
734
735                                         // CharactersHasProfiles
736                                         $this->Character->CharactersHasProfile->updateAll(
737                                                 array(
738                                                         'CharactersHasProfile.public_flag' => "'private'",
739                                                 ),
740                                                 array(
741                                                         'CharactersHasProfile.character_id' => $id
742                                                 )
743                                         );
744
745                                         // CharacterProfileArchives
746                                         $date = date('Y-m-d H:i:s');
747                                         $this->Character->CharacterProfileArchive->updateAll(
748                                                 array(
749                                                         'CharacterProfileArchive.deleted' => true,
750                                                         'CharacterProfileArchive.deleted_date' => "'$date'",
751                                                 ),
752                                                 array(
753                                                         'CharacterProfileArchive.character_id' => $id
754                                                 )
755                                         );
756
757                                         // Attachments
758                                         if (isset($character['CharacterPicture']) && !empty($character['CharacterPicture'])) {
759                                                 foreach ($character['CharacterPicture'] as $picture) {
760                                                         $this->Character->CharacterPicture->delete($picture['id']);
761                                                 }
762                                         }
763
764                                         $this->Session->setFlash(__('Character deleted', true));
765
766                                         return true;
767                                 } else {
768                                         $this->Session->setFlash(__('The Character could not be deleted.', true));
769                                 }
770                         }
771                 }
772
773                 return false;
774         }
775
776         /* 共通関数 */
777         function _get_character($id, $conditions = array(), $isAdmin = false)
778         {
779                 $character = $this->Character->get_character($id, $conditions, $isAdmin);
780
781                 if (empty($character)) {
782                         $this->Session->setFlash(__('No Character', true));
783                         $this->redirect(array('action'=>'index'));
784                 }
785
786                 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
787                         $this->Session->setFlash(__('No Permission', true));
788                         $this->redirect(array('action'=>'index'));
789                 }
790
791                 return $character;
792         }
793
794         // ProfileTableをCharacterHasProfilesのTableの形式にセット
795         function _set_profile_table2characters_has_profiles($profile)
796         {
797                 if (!empty($profile[0]['CharactersHasProfile'])) {
798                         return $profile;
799                 }
800
801                 foreach($profile as $k => $v) {
802                         if (empty($v['ProfileTable'])) {
803                                 continue;
804                         }
805                         $tmp = array();
806                         $rows = count($v['CharactersHasProfile']);
807                         for($i=0; $i<$rows; $i++) {
808                                 foreach($v['ProfileTable'] as $k2 => $v2) {
809                                         if (isset($v2['CharactersHasProfile'][$i])) {
810                                                 $tmp[] = $v2['CharactersHasProfile'][$i];
811                                         } elseif (isset($v['CharactersHasProfile'][$i])) {
812                                                 $tmp[] = $v['CharactersHasProfile'][$i];
813                                                 break;
814
815                                         } else {
816                                                 $tmp[] = null;
817                                         }
818                                 }
819                         }
820                         $profile[$k]['CharactersHasProfile'] = $tmp;
821                         }
822
823                 return $profile;
824         }
825
826         /* POSTのCharactersHasProfileを処理 */
827         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
828         {
829                 $prev_profile_id = null;
830                 $profileTable_tmp = array();
831                 $i = 0;
832                 $public = null;
833                 foreach ($characters_has_profiles as $k => $v) {
834                         if (!empty($character_id)) {
835                                 $characters_has_profiles[$k]['character_id'] = $character_id;
836                         }
837                         // 配列valueの処理
838                         if (is_array($v['value'])) {
839                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
840                         }
841
842                         // サニタイズ
843                         $characters_has_profiles[$k]['value'] = Sanitize::html($characters_has_profiles[$k]['value']);
844                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
845                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
846                         // textarea改行処理
847                         if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
848                                 $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
849                                 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
850                         }
851                         $characters_has_profiles[$k]['value'] = Sanitize::stripAll($characters_has_profiles[$k]['value']);
852
853                         // 空の値処理, public_flag
854                         // table: 一時保管して全項目空の場合行削除
855                         if (CorePlus::is_valid($v, 'profile_table_id')) {
856                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
857                                         $i++;
858                                         $public = null;
859                                 }
860
861                                 // 行単位public_flag
862                                 if ($public == null && isset($characters_has_profiles[$k]['public_flag'])) {
863                                         $public = $v['public_flag'];
864                                         // Static設定タイトルは公開
865                                         if ($v['profile_table_static_id']) {
866                                                 $characters_has_profiles[$k]['public_flag'] = 'public';
867                                         }
868                                 } else {
869                                         $characters_has_profiles[$k]['public_flag'] = $public;
870                                 }
871
872                                 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
873                         // それ以外は削除
874                         } elseif (empty($v['value'])) {
875                                         unset($characters_has_profiles[$k]);
876                         }
877                 }
878
879                 // table: 空行削除、不正profile_table_idデータ削除
880                 if (!empty($profileTable_tmp)) {
881                         if (!isset($this->Character->ProfileTable)) {
882                                 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
883                         }
884                         foreach($profileTable_tmp as $profile_id => $v) {
885                                 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
886                                         'conditions' => array('ProfileTable.profile_id' => $profile_id),
887                                         'fields' => array('ProfileTable.id'),
888                                         'order' => array('ProfileTable.sort_order' => 'asc'),
889                                         'recursive' => -1,
890                                 ));
891
892                                 foreach ($v as $i => $profile_tables) {
893                                         // 不正POSTの削除
894                                         $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
895                                         if (!empty($diff_keys)) {
896                                                 foreach($diff_keys as $dvalue) {
897                                                         unset($characters_has_profiles[$dvalue[1]]);
898                                                 }
899                                         }
900
901                                         // 空行削除
902                                         $delete_flg = true;
903                                         foreach($profile_table_columns as $k2 => $columns) {
904                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
905                                                         $delete_flg = false;
906                                                         break;
907                                                 }
908                                         }
909                                         if ($delete_flg === true) {
910                                                 foreach ($profile_tables as $delk) {
911                                                         unset($characters_has_profiles[$delk[1]]);
912                                                 }
913                                         }
914                                 }
915                         }
916                 }
917
918                 // 指定データのみ変更
919                 if (!is_null($profile_id) && !empty($now_data)) {
920                         $tmp = array();
921                         $i = 0;
922                         foreach($now_data as $k => $v) {
923                                 if ($v['id'] == $profile_id) {
924                                         $v['CharactersHasProfile'] = $characters_has_profiles;
925                                 }
926
927                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
928                                         $tmp[$i] = $v2;
929                                         if (isset($tmp[$i]['id'])) {
930                                                 unset($tmp[$i]['id']);
931                                         }
932                                         if (isset($tmp[$i]['character_id'])) {
933                                                 unset($tmp[$i]['character_id']);
934                                         }
935
936                                         $i++;
937                                 }
938                         }
939
940                         $characters_has_profiles = $tmp;
941                 }
942
943                 return $characters_has_profiles;
944         }
945
946         // CharactersHasProfileの追加valiadte設定
947         function _set_validate4characters_has_profile($system_id)
948         {
949                 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
950                         'rule' => array('validProfileId', $system_id),
951                 );
952                 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
953                         'rule' => array('validProfileSelectId', $system_id),
954                         'allowEmpty' => true,
955                 );
956                 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
957                 'rule' => array('validProfiletableId', $system_id),
958                 'allowEmpty' => true,
959                 );
960         }
961
962         /* restore_html */
963         function _restore_html_character($data, $nl2br = false) {
964                 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
965                 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
966                 if ($nl2br) {
967                         $data['notes'] = str_replace('<br />', "\n", $data['notes']);
968                 }
969
970                 return $data;
971         }
972
973         function _restore_html_characters_has_profiles($profile, $nl2br = false) {
974                 if (empty($profile)) {
975                         return null;
976                 }
977
978                 foreach($profile as $k => $v) {
979                         if (empty($v['CharactersHasProfile'])) {
980                                 continue;
981                         }
982                         $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
983                 }
984
985                 return $profile;
986         }
987         function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
988                 if (empty($data)) {
989                         return null;
990                 }
991
992                 foreach ($data as $k => $v) {
993                         if (isset($v['value'])) {
994                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
995                                 if ($nl2br && $profile_type == 'textarea') {
996                                         $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);
997                                 }
998                         }
999                 }
1000
1001                 return $data;
1002         }
1003
1004 }