OSDN Git Service

Character一覧検索:名前と詳細
[trpgtools-onweb/cake-frame.git] / app / controllers / characters_controller.php
1 <?php
2 /**
3  * PHP version 5
4  *
5  * @category Controller
6  * @package  Chara-Shee!
7  * @version  beta
8  * @author   Cake <cake_67@users.sourceforge.jp>
9  * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
10  * @link     http://trpgtools-onweb.sourceforge.jp/
11  */
12
13 class CharactersController extends AppController {
14
15         var $name = 'Characters';
16         var $helpers = array(
17                 'Select',
18                 'Profiledisp',
19         );
20
21         /* テーマ */
22         var $view = 'View';
23         var $theme = null;
24
25         var $isNpc = false;
26
27         var $disableTokenActions = array();
28         var $post_data = array();
29
30         // 検索対象項目
31         var $search_cols = array(
32         );
33
34         /* メソッド */
35
36         function beforeFilter() {
37
38                 parent::beforeFilter();
39
40                 // Cache
41                 $this->cacheAction = array(
42                         'index' => Configure::read('Cache.expireShort'),
43                 );
44
45                 // 認証なしアクセス可
46                 $this->AuthPlus->allow('view');
47                 $this->AuthPlus->allow('index');
48         }
49
50         function beforeRender()
51         {
52                 parent::beforeRender();
53
54                 $this->set_public_flag4view();
55                 $this->set_status4view();
56         }
57
58
59         /* アクションメソッド */
60
61         function index($id = null) {
62                 if (!empty($id) && $id == $this->user_id) {
63                         $this->redirect(array_merge(array('action'=>'mycharacter'), $this->params['named']));
64                 }
65
66                 $this->set('isOwner', false);
67
68                 $this->_index($id);
69         }
70
71         function search($id = null) {
72                 if (!empty($id) && $id == $this->user_id) {
73                         $this->redirect(array_merge(array('action'=>'mycharacter'), $this->params['named']));
74                 }
75
76                 $this->set('isOwner', false);
77
78                 $this->_search($id);
79         }
80
81         function mysearch(){
82                 $this->set('isOwner', true);
83
84                 $this->_search($this->user_id);
85         }
86
87         function mycharacter(){
88                 $this->set('isOwner', true);
89
90                 $this->_index($this->user_id);
91         }
92
93         function view($id = null) {
94                 if (!$id) {
95                         $this->Session->setFlash(__('Invalid Character.', true));
96                         $this->redirect(array('action'=>'index'));
97                 }
98
99                 $character = $this->_view($id, array(), false);
100                 // Systemチェック
101                 if (!$this->check_public_flag($character['System'])) {
102                         unset($character['System']['Profile']);
103                 }
104
105                 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
106
107                 $isOwner = false;
108                 if ($this->isOwner($character['Character'], $this->user_id)) {
109                         $isOwner = true;
110                 }
111                 $this->set('isOwner', $isOwner);
112
113                 // キャラ画像のpublic_flagチェック
114                 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
115                         foreach($character['CharacterPicture'] as $k => $v) {
116                                 if (!$this->check_public_flag2($v)) {
117                                         unset($character['CharacterPicture'][$k]);
118                                 }
119                         }
120                 }
121
122                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
123         }
124
125         function add() {
126                 $systems = $this->_get_systems('public');
127                 // SingleSystem
128                 if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
129                         $this->params['named']['system_id'] = key($systems);
130                 }
131
132                 if (isset($this->params['named']['system_id']) && !empty($this->params['named']['system_id'])) {
133                         $system = $this->Character->System->find('first', array(
134                                 'conditions' => array(
135                                         'System.id' => $this->params['named']['system_id'],
136                                 ),
137                                 'recursive' => -1,
138                                 'fields' => array(
139                                         'System.id',
140                                         'System.name',
141                                         'System.public_flag',
142                                         'System.set_npc',
143                                 )
144                         ));
145                         if (empty($system) || !$this->check_public_flag2($system['System'])) {
146                                 $this->Session->setFlash(__('Invalid ID.', true));
147                                 $this->redirect(array('action'=>'index'));
148                         }
149                 }
150                 if (isset($system['System']) && !empty($system['System']) && $system['System']['set_npc']) {
151                         $this->isNpc = true;
152                 }
153
154                 if (!empty($this->data)) {
155                         $this->data['Character']['user_id'] = $this->user_id;
156                         if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
157                                 $this->data['Character']['system_id'] = key($systems);
158                         }
159
160                         $this->Character->create();
161                         if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
162
163                                 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
164                                 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
165                         } else {
166                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
167                         }
168                 }
169
170                 $this->set('systems', $systems);
171                 $this->set('isOwner', false);
172
173                 $this->set('title_for_layout', " ". __('Add Character', true));
174         }
175
176         function edit($id = null) {
177                 if (!$id && empty($this->data)) {
178                         $this->Session->setFlash(__('Invalid ID.', true));
179                         $this->redirect(array('action'=>'index'));
180                 }
181
182                 // Characterデータ取得
183                 unset($this->Character->System->hasMany['Profile']['fields']);
184                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
185                 unset($this->Character->hasMany['CharacterProfileArchive']);
186
187                 $character = $this->_get_character($id, array(), false);
188                 if (!$this->isOwner($character['Character'], $this->user_id)) {
189                         $this->Session->setFlash(__('No Permission', true));
190                         $this->redirect(array('action'=>'index'));
191                 }
192
193                 // Systemチェック
194                 if (!$this->check_public_flag($character['System'])) {
195                         $this->redirect(array('action'=>'change_system', $id));
196                 }
197                 if ($character['System']['set_npc']) {
198                         $this->isNpc = true;
199                 }
200
201                 if (empty($this->data)) {
202                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
203                 }
204
205                 if (!empty($this->data)) {
206                         $this->post_data = $this->data;
207
208                         $this->data['clearCache'] = array(
209                                 'character_id' => $id,
210                                 'user_id' => $this->user_id,
211                                 'system_id' => $character['System']['id'],
212                         );
213
214                         // 新hasProfile処理
215                         if (isset($this->data['CharactersHasProfile'])) {
216                                 $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
217                         }
218
219                         /* validate */
220                         $this->_set_validate4characters_has_profile($character['System']['id']);
221                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
222                                 // requiredのチェック
223                                 $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
224
225                                 if ($requiredCheck) {
226                                         // 現在のhas_profiles削除
227                                         $this->Character->CharactersHasProfile->deleteAll(array(
228                                                 'CharactersHasProfile.character_id' => $id
229                                         ));
230
231                                         /* データ保存 */
232                                         $this->data['Character']['id'] = $id;
233                                         $this->Character->create();
234
235                                         if ($this->Character->saveAll($this->data, array(
236                                                 'validate' => false,
237                                                  'fieldList' => array_merge(
238                                                         $this->Character->fields['edit'], 
239                                                         $this->Character->CharactersHasProfile->fields['add']
240                                                  )
241                                         ))) {
242
243                                                 // Archives保存
244                                                 if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
245                                                         $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
246                                                 }
247
248                                                 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
249                                                 $this->redirect(array('action'=>'view', $id));
250                                         }
251                                 }
252                         }
253
254                         $this->data = array_merge($character, $this->data);
255                         $this->data['Character'] = $this->post_data['Character'];
256                         $this->data['Character']['main_picture'] = $character['Character']['main_picture'];
257                         $this->data['Character']['id'] = $id;
258                         foreach ($this->data['System']['Profile'] as $k1 => $profile) {
259                                 if ($profile['profile_type'] == 'table') {
260                                         $colum_num = count($profile['CharactersHasProfile']);
261                                 } elseif ($profile['profile_type'] == 's-table') {
262                                         $colum_num = count($profile['CharactersHasProfile']);
263                                 } elseif ($profile['profile_type'] == 'checkbox') {
264                                         $colum_num = count($profile['ProfileSelect']);
265                                 } elseif ($profile['profile_type'] == 'm-input') {
266                                         $colum_num = count($profile['CharactersHasProfile']);
267                                 } else {
268                                         $colum_num = 1;
269                                 }
270
271                                 if ($colum_num == 0) {
272                                         if ($profile['profile_type'] == 's-table') {
273                                                 $colum_num = count($profile['ProfileTable']) * count($profile['ProfileTable'][0]['ProfileTableStatic']);
274                                         } elseif ($profile['profile_type'] == 'table') {
275                                                 $colum_num = count($profile['ProfileTable']) * 4;
276                                         } elseif ($profile['profile_type'] == 'checkbox') {
277                                                 $colum_num = count($profile['ProfileSelect']);
278                                         } elseif ($profile['profile_type'] == 'm-input') {
279                                                 $colum_num = 5;
280                                         } else {
281                                                 $colum_num = 1;
282                                         }
283                                 }
284
285                                 for ($i=0; $i<$colum_num; $i++) {
286                                         if (!isset($profile['CharactersHasProfile'][$i])) {
287                                                 $profile['CharactersHasProfile'][$i] = array('character_id' => $character['Character']['id']);
288                                         }
289
290                                         $postdata = array_shift($this->post_data['CharactersHasProfile']);
291
292                                         if (is_array($postdata['value'])) {
293                                                 $postdata['value'] = $postdata['value'][0];
294                                         }
295
296                                         if (isset($postdata[$i])) {
297                                                 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], $postdata[$i]);
298                                         } else {
299                                                 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], $postdata);
300                                         }
301                                 }
302                         }
303                 }
304
305                 elseif (empty($this->data)) {
306                         $this->data = $character;
307                         $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
308
309                 }
310                 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
311
312                 $this->set('isOwner', true);
313
314                 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
315         }
316
317         function add_milti_profiles($id = null) {
318                 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
319                         $this->Session->setFlash(__('Invalid ID.', true));
320                         $this->redirect(array('action'=>'index'));
321                 }
322
323                 // Characterデータ取得
324                 $this->Character->System->hasMany['Profile']['fields'] = '';
325                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
326                 unset($this->Character->hasMany['CharacterProfileArchive']);
327
328                 $character = $this->_get_character($id);
329                 if (!$this->isOwner($character['Character'], $this->user_id)) {
330                         $this->Session->setFlash(__('No Permission', true));
331                         $this->redirect(array('action'=>'index'));
332                 }
333                 // Systemチェック
334                 if (!$this->check_public_flag($character['System'])) {
335                         $this->redirect(array('action'=>'change_system', $id));
336                 }
337
338                 // Profilesデータ取得
339                 if (empty($character['System']['Profile'])) {
340                         $this->Session->setFlash(__('Invalid Profile.', true));
341                         $this->redirect(array('action'=>'view', $id));
342                 }
343
344                 $profile_id = $this->params['named']['profile_id'];
345
346                 $target = array();
347                 foreach ($character['System']['Profile'] as $profile) {
348                         if ($profile['id'] == $profile_id) {
349                                 $target = $profile;
350                                 break;
351                         }
352                 }
353
354                 if (!$target) {
355                         $this->Session->setFlash(__('No Profile.', true));
356                         $this->redirect(array('action'=>'view', $id));
357                 }
358
359                 $character['System']['Profile'] = array();
360                 $character['System']['Profile'][0] = $target;
361
362                 $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
363
364                 if (!empty($this->data)) {
365                         $this->data['clearCache'] = array(
366                                 'character_id' => $id,
367                                 'user_id' => $this->user_id,
368                                 'system_id' => $character['System']['id'],
369                         );
370
371                         $this->post_data = $this->data;
372
373                         $this->data['Character'] = array_merge($character['Character'], $this->data['Character']);
374                         $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
375                         $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
376                         $this->data['Character']['modified'] = null;
377
378                         // 新hasProfile処理
379                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
380
381                         /* validate */
382                         $this->_set_validate4characters_has_profile($character['System']['id']);
383                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
384                                 // requiredのチェック
385                                 $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
386
387                                 if ($requiredCheck) {
388                                         // 現在のhas_profiles削除
389                                         $this->Character->CharactersHasProfile->deleteAll(array(
390                                                 'CharactersHasProfile.profile_id' => $target['id'],
391                                                 'CharactersHasProfile.character_id' => $id
392                                         ));
393
394                                         /* データ保存 */
395                                         $this->Character->create();
396                                         if ($this->Character->saveAll($this->data, array(
397                                                 'validate' => false,
398                                                  'fieldList' => array_merge(
399                                                         $this->Character->CharactersHasProfile->fields['add']
400                                                  )
401                                         ))) {
402
403                                                 // Archives保存
404                                                 if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
405                                                         $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
406                                                 }
407
408                                                 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
409                                                 $this->redirect(array('action'=>'view', $id));
410                                         } else {
411                                                 $this->data = array_merge($character, $this->data);
412                                         }
413                                 } else {
414                                 }
415                         }
416
417                         $this->data = array_merge($character, $this->data);
418                         $this->data['Character']['id'] = $id;
419                         unset($this->data['Character']['__Token']);
420
421                 }
422
423                 if (empty($this->data)) {
424                         $this->data = $character;
425                 }
426                 $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
427
428                 $this->set('isOwner', true);
429
430                 if (isset($this->params['named']['num'])) {
431                         $form_nums = $this->params['named']['num'];
432                 } else {
433                         $form_nums = 3;
434                 }
435                 $this->set('form_nums', $form_nums);
436
437                 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
438         }
439
440         function change_system($id = null) {
441                 if (!$id) {
442                         $this->Session->setFlash(__('Invalid ID.', true));
443                         $this->redirect(array('action'=>'index'));
444                 }
445
446                 // Characterデータ取得
447                 $this->Character->System->hasMany['Profile']['fields'] = '';
448                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
449
450                 $character = $this->_get_character($id);
451                 if (!$this->isOwner($character['Character'], $this->user_id)) {
452                         $this->Session->setFlash(__('No Permission', true));
453                         $this->redirect(array('action'=>'index'));
454                 }
455                 // Systemチェック
456                 // Systemあり
457                 if (isset($character['System']) && !empty($character['System'])) {
458                         // 公開状態は変更不可
459                         if ($this->check_public_flag($character['System'])) {
460                                 $this->redirect(array('action'=>'edit', $id));
461                         // System非公開
462                         } else {
463                                 $this->set('isChange', true);
464                         }
465                 }
466
467                 if (!empty($this->data)) {
468                         $this->data['clearCache'] = array(
469                                 'character_id' => $id,
470                                 'user_id' => $this->user_id,
471                                 'system_id' => $character['System']['id'],
472                         );
473
474                         $this->data['Character']['id'] = $id;
475
476                         // validateはsystem_idのみ
477                         $this->Cahracter['validate'] = array(
478                                 'system_id' => array(
479                                         'validSystemId' => array(
480                                                 'rule' => array('validSystemId', true),
481                                                 'allowEmpty' => false,
482                                         )
483                                 ),
484                         );
485
486                         if ($this->Character->save(
487                                 $this->data,
488                                 array(
489                                         'validate' => true,
490                                         'fieldList' => array('system_id'),
491                                 )
492                         )) {
493                                 $this->Session->setFlash(__('The new system has been saved', true));
494                                 $this->redirect(array('action'=>'view', $id));
495                         } else {
496                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
497                         }
498                 }
499
500                 $this->set('isOwner', true);
501                 $this->set('character', $character);
502
503                 $systems = $this->_get_systems('public');
504                 $this->set('systems', $systems);
505
506                 $this->set('title_for_layout', " ". sprintf(__('%s Change System', true), $character['Character']['name']));
507         }
508
509
510         function set_status($id = null) {
511                 if (!$id || !isset($this->params['named']['mode'])) {
512                         $this->Session->setFlash(__('Invalid ID.', true));
513                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
514                 }
515
516                 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
517
518                 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
519                         $this->Session->setFlash(__('Invalid URL.', true));
520                         $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
521                 }
522
523                 // 新picture設定
524                 $new_character_picture = null;
525                 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
526                         if ($character['CharacterPicture']) {
527                                 foreach($character['CharacterPicture'] as $k => $v) {
528                                         if ($v['id'] == $this->params['named']['new_picture_id']) {
529                                                 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
530                                                 break;
531                                         }
532                                 }
533                         }
534                         if (!$new_character_picture) {
535                                 $this->Session->setFlash(__('Invalid data.', true));
536                                 $this->redirect(array('controller' => 'character_pictures', 'action'=>'listview', $id));
537                         }
538                 }
539
540
541                 $this->data['clearCache'] = array(
542                         'character_id' => $id,
543                         'user_id' => $this->user_id,
544                         'system_id' => $character['Character']['system_id'],
545                 );
546
547                 // 設定変更
548                 $this->Character->id = $id;
549                 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
550                 $this->Character->save(
551                         $this->data, 
552                         array(
553                                 'fieldList' => array(
554                                         $this->params['named']['mode']
555                                 ), 
556                         )
557                 );
558
559                 $this->Character->deleteCache4CharacterPicture($id);
560
561
562                 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
563                 $this->redirect(array('controller' => 'character_pictures', 'action' => 'listview', $id));
564         }
565
566         function delete($id = null) {
567                 if (!$id) {
568                         $this->Session->setFlash(__('Invalid id for Character', true));
569                         $this->redirect(array('action'=>'index'));
570                 }
571
572                 if ($this->_delete($id)) {
573                         $this->redirect(array('controller' => 'users', 'action'=>'index'));
574                 }
575
576                 $this->set('title_for_layout', " - ". __('Delete Character', true));
577         }
578
579         function admin_index($id = null) {
580
581                 $this->_index($id, array('isAdmin' => true));
582         }
583
584         function admin_search($id = null) {
585
586                 $this->_search($id, array('isAdmin' => true));
587         }
588
589         function admin_view($id = null) {
590                 if (!$id) {
591                         $this->Session->setFlash(__('Invalid Character.', true));
592                         $this->redirect(array('action' => 'admin_index'));
593                 }
594
595                 $character = $this->_view($id, array(), true);
596
597                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
598         }
599
600         function admin_edit($id = null) {
601                 if (!$id && empty($this->data)) {
602                         $this->Session->setFlash(__('Invalid ID.', true));
603                         $this->redirect(array('action'=>'index'));
604                 }
605
606                 // Characterデータ取得
607                 $this->Character->System->hasMany['Profile']['fields'] = '';
608                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
609                 unset($this->Character->hasMany['CharacterProfileArchive']);
610
611                 $character = $this->_get_character($id, array(), true);
612                 if (!$character) {
613                         $this->Session->setFlash(__('No Character', true));
614                         $this->redirect(array('action'=>'index'));
615                 }
616
617                 if (empty($this->data)) {
618                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
619                 }
620
621                 if (!empty($this->data)) {
622
623                         $this->data['clearCache'] = array(
624                                 'character_id' => $id,
625                                 'user_id' => $this->user_id,
626                                 'system_id' => $character['System']['id'],
627                         );
628
629                         if (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_private') {
630                                 $this->data['Character']['public_flag'] = 'private';
631                         } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_public') {
632                                 $this->data['Character']['public_flag'] = 'public';
633                         } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'delete_confirm') {
634                                 $this->redirect(array('controller' => 'characters', 'action'=>'admin_delete', $id));
635                         } else {
636                                 $this->Session->setFlash(sprintf(__('Invalid data.', true), $this->data['Character']['name']));
637                                 $this->redirect(array('action'=>'view', $id));
638                         }
639
640                         $this->data['Character']['id'] = $id;
641                         $this->Character->create();
642                         if ($this->Character->save($this->data, array(
643                                 'validate' => true,
644                                 'fieldList' => array(
645                                         'public_flag',
646                                 ),
647                         ))) {
648
649                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $character['Character']['name']));
650                                         $this->redirect(array('action'=>'view', $id));
651                                 }
652
653                         $this->data = array_merge($character, $this->data);
654                         $this->data['Character']['id'] = $id;
655                 }
656
657                 if (empty($this->data)) {
658                         $this->data = $character;
659                         $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
660
661                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
662                 }
663
664                 $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
665         }
666
667         function admin_delete($id = null) {
668                 if (!$id) {
669                         $this->Session->setFlash(__('Invalid id for Character', true));
670                         $this->redirect(array('action'=>'index'));
671                 }
672
673                 if ($this->_delete($id, array(), true)) {
674                         $this->redirect(array('action'=>'index'));
675                 }
676
677                 $this->set('title_for_layout', " - ". __('Delete Character', true));
678         }
679
680
681         /* 共通化アクションメソッド */
682         function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
683                 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
684                         $isAdmin = true;
685                         unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
686                 } else {
687                         $isAdmin = false;
688                 }
689
690                 $title = __('List of All Characters', true);
691
692                 // System指定分岐
693                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
694                         //情報取得
695                         $system_conditions['System.id'] = $this->params['named']['system'];
696                         if ($isAdmin === false) {
697                                 $system_conditions['System.public_flag'] = 'public';
698                         }
699
700                         $this_system = $this->Character->System->find('first', array(
701                                 'conditions' => $system_conditions,
702                                 'recursive' => -1,
703                         ));
704
705                         if (!isset($this_system['System'])) {
706                                 $this->Session->setFlash(__('Invalid System.', true));
707                                 $this->redirect(array('controller' => 'systems', 'action' => 'index'));
708                         }
709                         $conditions['Character.system_id'] = $this->params['named']['system'];
710
711                         $this_system = $this->_restore_html_system($this_system);
712
713                         if ($this_system['System']['set_npc']) {
714                                 $this->isNpc = true;
715                         }
716
717                         $title = $this_system['System']['name']. " ". $title;
718
719                         $this->set('this_system', $this_system);
720                 } else {
721                         $contain = array_merge(array('System'), (array)$contain);
722
723                         $this->isNpc = true;
724                 }
725
726                 $user = array();
727                 if (!empty($id)) {
728                         $user = $this->Character->User->find('first', array(
729                                 'conditions' => array('User.id' => $id),
730                                 'recursive' => -1,
731                                 'fields' => array(
732                                         'User.id',
733                                         'User.name',
734                                 ),
735                         ));
736
737                         if (empty($user)) {
738                                 $this->Session->setFlash(__('Invalid Id.', true));
739                                 $this->redirect(array('action'=>'index'));
740                         }
741
742                         $conditions['Character.user_id'] = $id;
743                         if ($id == $this->user_id) {
744                                 unset($this->paginate['conditions']['Character.public_flag']);
745                         }
746
747                         $title = $user['User']['name']. " ". $title;
748                 } else {
749                         $contain = array_merge($contain, array('User'));
750                 }
751                 $this->set('userSet', $user);
752
753                 // Status分岐
754                 if (!isset($this->params['named']['status'])) {
755                         $status = 'active';
756                 } else {
757                         $status = $this->params['named']['status'];
758                 }
759                 switch ($status) {
760                         case 'active':
761                         case 'inactive':
762                         case 'npc':
763                                 $conditions['Character.status'] = $status;
764                                 break;
765                         default:
766                                 $status = 'all';
767                                 unset($conditions['Character.status']);
768                                 break;
769                 }
770                 $this->set('selected_status', $status);
771
772                 $characters =  $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order));
773
774                 $this->set('characters', $characters);
775
776                 $this->set('title_for_layout', " - ". $title);
777         }
778
779         function _search($id, $conditions = array(), $limit = 20) {
780                 $this->search_cols = array(
781                         'Character.name' => __('Charater Name', true),
782                         'Character.notes' => __('Notes', true),
783                 );
784                 $this->fields = array_merge($this->fields, array('Character.notes'));
785                 $this->set('fields', $this->fields);
786
787                 // 検索条件設定
788                 $keywords = null;
789                 $type = 'OR';
790                 if (isset($this->params['url']['keyword']) && !empty($this->params['url']['keyword'])) {
791                         if (is_string($this->params['url']['keyword'])) {
792                                 foreach ($this->search_cols as $field => $name) {
793                                         $new_condition = $this->_create_search_sql($this->params['url']['keyword'], $field, $type);
794                                         $conditions[$type][] = $new_condition;
795                                 }
796                         } else if (is_array($this->params['url']['keyword'])) {
797                         } else {
798                                 $this->Session->setFlash(__('Invalid Data.', true));
799                                 $this->redirect(array('action'=>'index'));
800                         }
801
802                         $this->set('keyword', $this->params['url']['keyword']);
803                 } else {
804                         $this->set('keyword', null);
805                 }
806
807                 $this->_index($id, $conditions, $limit);
808         }
809
810
811
812         function _view($id, $conditions = array(), $isAdmin = false) {
813                 $this->helpers[] = 'CharacterSheet';
814
815                 // キャラクターシート設定
816                 $mode = null;
817                 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
818                         $this->view = 'Theme';
819                         $this->theme = $this->params['named']['mode'];
820
821                 }
822
823                 // キャラデータ取得
824                 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
825                 $character = $this->Character->set_profiles2view($orig_character);
826
827                 Configure::write('isOwner', false);
828                 if ($isAdmin || CorePlus::isOwner($character['Character'], $this->user_id)) {
829                         Configure::write('isOwner', true);
830                 }
831
832                 // Systemチェック
833                 $this->set('systemValid', 'public');
834                 if (!$character['System']) {
835                         $this->set('systemValid', false);
836                 }
837                 if (!$this->check_public_flag($character['System'])) {
838                         $this->set('systemValid', 'unpublic');
839                 }
840                 if ($character['System']['set_npc']) {
841                         $this->isNpc = true;
842                 }
843                 $character = $this->_restore_html_system($character);
844
845                 $this->set('title_for_layout', " - ". $character['Character']['name']);
846
847                 return $character;
848         } 
849
850         function _delete($id = null, $conditions = array(), $isAdmin = false) {
851                 if (!$id) {
852                         $this->Session->setFlash(__('Invalid id for Character', true));
853                         $this->redirect(array('action'=>'index'));
854                 }
855
856                 $character = $this->_view($id, $conditions, $isAdmin);
857                 if (empty($character)) {
858                         $this->Session->setFlash(__('No Character', true));
859                         $this->redirect(array('action'=>'index'));
860                 }
861                 $this->set('character', $character);
862
863                 if (!$isAdmin && !$this->isOwner($character['Character'], $this->user_id)) {
864                         $this->Session->setFlash(__('No Permission', true));
865                         $this->redirect(array('action'=>'index'));
866                 }
867                 $this->set('isOwner', true);
868
869                 if (!empty($this->data)) {
870
871                         $this->data['clearCache'] = array(
872                                 'character_id' => $id,
873                                 'user_id' => $this->user_id,
874                                 'system_id' => $character['System']['id'],
875                         );
876
877                         if ($this->data['Character']['confirm'] == 'yes') {
878                                 // Character
879                                 $this->data['Character']['id'] = $id;
880                                 $this->data['Character']['main_picture'] = null;
881                                 $this->data['Character']['full_length'] = null;
882                                 $this->data['Character']['deleted'] = true;
883                                 $this->data['Character']['deleted_date'] = date('Y-m-d H:i:s');
884
885                                 $this->Character->create();
886                                 if ($this->Character->save(
887                                         $this->data,
888                                         array(
889                                                 'validate' => false,
890                                                 'fieldList' => array(
891                                                         'main_picture',
892                                                         'full_length',
893                                                         'deleted',
894                                                         'deleted_date'
895                                                 ),
896                                         )
897                                 )) {
898
899                                         // CharactersHasProfiles
900                                         $this->Character->CharactersHasProfile->updateAll(
901                                                 array(
902                                                         'CharactersHasProfile.public_flag' => "'private'",
903                                                 ),
904                                                 array(
905                                                         'CharactersHasProfile.character_id' => $id
906                                                 )
907                                         );
908
909                                         // CharacterProfileArchives
910                                         $date = date('Y-m-d H:i:s');
911                                         $this->Character->CharacterProfileArchive->updateAll(
912                                                 array(
913                                                         'CharacterProfileArchive.deleted' => true,
914                                                         'CharacterProfileArchive.deleted_date' => "'$date'",
915                                                 ),
916                                                 array(
917                                                         'CharacterProfileArchive.character_id' => $id
918                                                 )
919                                         );
920
921                                         // Attachments
922                                         if (isset($character['CharacterPicture']) && !empty($character['CharacterPicture'])) {
923                                                 foreach ($character['CharacterPicture'] as $picture) {
924                                                         $this->Character->CharacterPicture->delete($picture['id']);
925                                                 }
926                                         }
927
928                                         $this->Session->setFlash(__('Character deleted', true));
929
930                                         return true;
931                                 } else {
932                                         $this->Session->setFlash(__('The Character could not be deleted.', true));
933                                 }
934                         }
935                 }
936
937                 return false;
938         }
939
940         /* 共通関数 */
941         function _get_character($id, $conditions = array(), $isAdmin = false)
942         {
943                 $character = $this->Character->get_character($id, $conditions, $isAdmin);
944
945                 if (empty($character)) {
946                         $this->Session->setFlash(__('No Character', true));
947                         $this->redirect(array('action'=>'index'));
948                 }
949
950                 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
951                         $this->Session->setFlash(__('No Permission', true));
952                         $this->redirect(array('action'=>'index'));
953                 }
954
955                 if (isset($character['System']['Profile'])) {
956                         $character['System'] = $this->_restore_html_profile($character['System']);
957                 }
958
959                 return $character;
960         }
961
962         // ProfileTableをCharacterHasProfilesのTableの形式にセット
963         function _set_profile_table2characters_has_profiles($profile)
964         {
965                 if (!empty($profile[0]['CharactersHasProfile'])) {
966                         return $profile;
967                 }
968
969                 foreach($profile as $k => $v) {
970                         if (empty($v['ProfileTable'])) {
971                                 continue;
972                         }
973
974                         $tmp = array();
975                         $rows = count($v['CharactersHasProfile']);
976                         for($i=0; $i<$rows; $i++) {
977                                 foreach($v['ProfileTable'] as $k2 => $v2) {
978                                         if (isset($v2['CharactersHasProfile'][$i])) {
979                                                 $tmp[] = $v2['CharactersHasProfile'][$i];
980                                         } elseif (isset($v['CharactersHasProfile'][$i])) {
981                                                 $tmp[] = $v['CharactersHasProfile'][$i];
982                                                 break;
983
984                                         } else {
985                                                 $tmp[] = null;
986                                         }
987                                 }
988                         }
989                         $profile[$k]['CharactersHasProfile'] = $tmp;
990
991                         if (isset($profile[$k]['ProfileTable'][0]['ProfileTableStatic']) && !empty($profile[$k]['ProfileTable'][0]['ProfileTableStatic'])) {
992                                 $profile[$k]['ProfileTable'][0]['ProfileTableStatic'] = $this->_restore_html_profile_table_static($profile[$k]['ProfileTable'][0]['ProfileTableStatic']);
993                         }
994                 }
995
996                 return $profile;
997         }
998
999         /* POSTのCharactersHasProfileを処理 */
1000         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
1001         {
1002                 $prev_profile_id = null;
1003                 $profileTable_tmp = array();
1004                 $i = 0;
1005                 $public = null;
1006                 foreach ($characters_has_profiles as $k => $v) {
1007                         if (!empty($character_id)) {
1008                                 $characters_has_profiles[$k]['character_id'] = $character_id;
1009                         }
1010                         // 配列valueの処理
1011                         if (is_array($v['value'])) {
1012                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
1013                         }
1014
1015                         // サニタイズ
1016                         $characters_has_profiles[$k]['value'] = Sanitize::html($characters_has_profiles[$k]['value']);
1017                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
1018                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
1019                         // textarea改行処理
1020                         if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
1021                                 $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
1022                                 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
1023                         }
1024                         $characters_has_profiles[$k]['value'] = Sanitize::stripAll($characters_has_profiles[$k]['value']);
1025
1026                         // 空の値処理, public_flag
1027                         // table: 一時保管して全項目空の場合行削除
1028                         if (CorePlus::is_valid($v, 'profile_table_id')) {
1029                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
1030                                         $i++;
1031                                         $public = null;
1032                                 }
1033
1034                                 // 行単位public_flag
1035                                 if ($public == null && isset($characters_has_profiles[$k]['public_flag'])) {
1036                                         $public = $v['public_flag'];
1037                                         // Static設定タイトルは公開
1038                                         if ($v['profile_table_static_id']) {
1039                                                 $characters_has_profiles[$k]['public_flag'] = 'public';
1040                                         }
1041                                 } else {
1042                                         $characters_has_profiles[$k]['public_flag'] = $public;
1043                                 }
1044
1045                                 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
1046                         // それ以外は削除
1047                         } elseif (empty($v['value'])) {
1048                                         unset($characters_has_profiles[$k]);
1049                         }
1050                 }
1051
1052                 // table: 空行削除、不正profile_table_idデータ削除
1053                 if (!empty($profileTable_tmp)) {
1054                         if (!isset($this->Character->ProfileTable)) {
1055                                 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
1056                         }
1057                         foreach($profileTable_tmp as $profile_id => $v) {
1058                                 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
1059                                         'conditions' => array('ProfileTable.profile_id' => $profile_id),
1060                                         'fields' => array('ProfileTable.id'),
1061                                         'order' => array('ProfileTable.sort_order' => 'asc'),
1062                                         'recursive' => -1,
1063                                 ));
1064
1065                                 foreach ($v as $i => $profile_tables) {
1066                                         // 不正POSTの削除
1067                                         $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
1068                                         if (!empty($diff_keys)) {
1069                                                 foreach($diff_keys as $dvalue) {
1070                                                         unset($characters_has_profiles[$dvalue[1]]);
1071                                                 }
1072                                         }
1073
1074                                         // 空行削除
1075                                         $delete_flg = true;
1076                                         foreach($profile_table_columns as $k2 => $columns) {
1077                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
1078                                                         $delete_flg = false;
1079                                                         break;
1080                                                 }
1081                                         }
1082                                         if ($delete_flg === true) {
1083                                                 foreach ($profile_tables as $delk) {
1084                                                         unset($characters_has_profiles[$delk[1]]);
1085                                                 }
1086                                         }
1087                                 }
1088                         }
1089                 }
1090
1091                 // 指定データのみ変更
1092                 if (!is_null($profile_id) && !empty($now_data)) {
1093                         $tmp = array();
1094                         $i = 0;
1095                         foreach($now_data as $k => $v) {
1096                                 if ($v['id'] == $profile_id) {
1097                                         $v['CharactersHasProfile'] = $characters_has_profiles;
1098                                 }
1099
1100                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
1101                                         $tmp[$i] = $v2;
1102                                         if (isset($tmp[$i]['id'])) {
1103                                                 unset($tmp[$i]['id']);
1104                                         }
1105                                         if (isset($tmp[$i]['character_id'])) {
1106                                                 unset($tmp[$i]['character_id']);
1107                                         }
1108
1109                                         $i++;
1110                                 }
1111                         }
1112
1113                         $characters_has_profiles = $tmp;
1114                 }
1115
1116                 return $characters_has_profiles;
1117         }
1118
1119         // CharactersHasProfileの追加valiadte設定
1120         function _set_validate4characters_has_profile($system_id)
1121         {
1122                 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
1123                         'rule' => array('validProfileId', $system_id),
1124                 );
1125                 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
1126                         'rule' => array('validProfileSelectId', $system_id),
1127                         'allowEmpty' => true,
1128                 );
1129                 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
1130                 'rule' => array('validProfiletableId', $system_id),
1131                 'allowEmpty' => true,
1132                 );
1133         }
1134
1135         /* restore_html */
1136         function _restore_html_character($data, $nl2br = false) {
1137                 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
1138                 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
1139                 if ($nl2br) {
1140                         $data['notes'] = str_replace('<br />', "\n", $data['notes']);
1141                 }
1142
1143                 return $data;
1144         }
1145
1146         function _restore_html_characters_has_profiles($profile, $nl2br = false) {
1147                 if (empty($profile)) {
1148                         return null;
1149                 }
1150
1151                 foreach($profile as $k => $v) {
1152                         if (empty($v['CharactersHasProfile'])) {
1153                                 continue;
1154                         }
1155                         $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
1156                 }
1157
1158                 return $profile;
1159         }
1160         function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
1161                 if (empty($data)) {
1162                         return null;
1163                 }
1164
1165                 foreach ($data as $k => $v) {
1166                         if (isset($v['value'])) {
1167                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
1168                                 if ($nl2br && $profile_type == 'textarea') {
1169                                         $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);
1170                                 }
1171                         }
1172                 }
1173
1174                 return $data;
1175         }
1176
1177         function _checkRequiredProfile($profile)
1178         {
1179                 if (empty($profile) || !isset($this->data['CharactersHasProfile'])) {
1180                         return true;
1181                 }
1182
1183                 $requireCheck = array();
1184                 foreach($this->data['CharactersHasProfile'] as $k => $v) {
1185                         if (!isset($requireCheck[$v['profile_id']])) {
1186                                 $requireCheck[$v['profile_id']] = null;
1187                         }
1188
1189                         if (isset($requireCheck[$v['profile_id']]['value']) && !empty($requireCheck[$v['profile_id']]['value'])) {
1190                                 continue;
1191                         }
1192
1193                         if (empty($v['value'])) {
1194                                 continue;
1195                         }
1196
1197                         if (isset($v['profile_table_static_id']) && !empty($v['profile_table_static_id'])) {
1198                                 if (!isset($requireCheck[$v['profile_id']]['profile_table_id']) || empty($requireCheck[$v['profile_id']]['profile_table_id'])) {
1199                                         $requireCheck[$v['profile_id']]['profile_table_id'] = $v['profile_table_id'];
1200                                 }
1201
1202                                 if ($requireCheck[$v['profile_id']]['profile_table_id'] == $v['profile_table_id']) {
1203                                         continue;
1204                                 }
1205                         }
1206
1207                         $requireCheck[$v['profile_id']]['value'] = $v['value'];
1208                 }
1209
1210                 $errors = array();
1211                 foreach ($profile as $k => $v) {
1212                         if ($v['required']) {
1213                                 if (!isset($requireCheck[$v['id']]) || empty($requireCheck[$v['id']]['value'])) {
1214                                         $errors[] = $v['name'];
1215                                 }
1216                         }
1217                 }
1218
1219                 if ($errors) {
1220                         $this->Session->setFlash(sprintf(__('%s is required.', true), implode(',', $errors)));
1221                         return false;
1222                 } else {
1223                         return true;
1224                 }
1225         }
1226
1227         function _create_search_sql($keyword, $field, $type = 'OR')
1228         {
1229                 if (empty($keyword) || !is_string($keyword) || empty($field)) {
1230                         return array();
1231                 }
1232
1233                 if (!array_key_exists($field, $this->search_cols)) {
1234                         return array();
1235                 }
1236
1237                 $keywords = rawurldecode($keyword);
1238                 $searchwords = explode(",", $keywords);
1239
1240                 foreach ($searchwords as $k2 => $searchword) {
1241                         if ($k2 != 0) {
1242                                 $condition.= ' '. $type. ' ';
1243                         } else {
1244                                 $condition = '';
1245                         }
1246
1247                         $condition .= $field. ' LIKE '. '\'%'. $searchword. '%\'';
1248                 }
1249
1250                 return $condition;
1251         }
1252 }