OSDN Git Service

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