OSDN Git Service

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