OSDN Git Service

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