OSDN Git Service

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