OSDN Git Service

プロフィールのデフォルト入力機能追加
[trpgtools-onweb/cake-frame.git] / app / controllers / app_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  * 独自App Contoroller
16  */
17
18 App::import('Vendor', 'pear_ini');  
19
20 class AppController extends Controller
21 {
22         var $user = array('User' => array(
23                 'id' => 0,
24                 'group_id' => 0,
25         ));
26         var $user_id = 0;
27         var $isAdministrator = false;
28         var $isAdmin = false;
29
30         var $site_configs = array();
31
32         var $components = array(
33                 'Session',
34                 'AuthPlus',
35                 'Cakeplus.HtmlEscape',
36                 'Token',
37                 'Crypt',
38                 'DebugKit.Toolbar',
39 //              'Benchmark'
40         );
41
42         var $helpers = array(
43                 'Html',
44                 'Form',
45                 'Javascript',
46                 'Text',
47                 'Time',
48                 'Settings',
49                 'Session',
50                 'Media.Media',
51                 'Media.Upfile',
52                 'Cache',
53                 'PaginatorEx',
54                 'Token'
55         );
56
57         /* Characters抽出条件 */
58         var $conditions = array(
59                 'Character.public_flag' => 'public',
60                 'Character.deleted' => 0
61         );
62         var $fields = array(
63                 'Character.id',
64                 'Character.system_id',
65                 'Character.user_id',
66                 'Character.name',
67                 'Character.main_picture',
68                 'Character.sort_order',
69                 'Character.status',
70                 'Character.public_flag',
71                 'Character.modified',
72         );
73         var $contain = array();
74         var $recursive = -1;
75         var $order = array(
76                 'Character.modified' => 'DESC',
77         );
78         var $paginate = array(
79                 'Character' => array(
80                         'limit' => 20,
81                         'conditions' => array(
82                                 'Character.public_flag' => 'public',
83                                 'Character.deleted' => 0
84                         ),
85                         'recursive' => -1,
86                         'order' => array(
87                                 'Character.modified' => 'DESC',
88                         )
89                 ),
90         );
91
92         var $showlist_cols = array(
93         );
94
95         var $cacheAction = array();
96
97         // POSTのTokenチェックをしないアクション
98         var $disableTokenActions = array();
99
100         var $model_public_flags = array(
101         );
102
103         var $model_status = array(
104         );
105
106         var $model_status2 = array(
107         );
108
109         function beforeFilter()
110         {
111
112                 // Session->setFlashのメッセージがある場合、キャッシュしない
113                 $message = $this->Session->read('Message.flash');
114                 if (!empty($message['message']) && Configure::read('Cache.check')) {
115                         Configure::write('Cache.check', false);
116                 }
117
118                 if (isset($this->Benchmark) && is_object($this->Benchmark)) {
119                         $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' .' beforeFilterStart');
120                 }
121
122                 parent::beforeFilter();
123
124                 $this->user['User']['name'] = __('Guest', true);
125
126                 // ブラウザキャッシュ破棄
127                 $this->disableCache();
128
129                 if (isset($this->params['prefix'])) {
130                         if ($this->params['prefix'] == Configure::read('Routing.base_prefixes')) {
131                                 $this->isAdmin = true;
132                                 $this->layout = 'admin_'.$this->layout;
133                         }
134                 }
135                 $this->set('admin', $this->isAdmin);
136
137                 // サイト設定
138                 $this->site_configs = CorePlus::set_db_settings();
139                 $this->set('site_configs', $this->site_configs);
140
141                 if ($this->AuthPlus) {
142 //                      // ACL関連
143 //                      $this->AuthPlus->actionPath = 'controllers/';
144 //                      $this->AuthPlus->authorize = 'crud';
145                         $this->AuthPlus->authorize = 'orig';
146                         // 認証済みユーザ情報のセット
147                         $this->user_id = $this->AuthPlus->user('id');
148                         $user = $this->getUser($this->user_id);
149                         if (!empty($user)) {
150                                 $this->user = $user;
151                         }
152
153                         if (in_array($this->AuthPlus->user('group_id'), array(1,2,3))) {
154                                 $this->isAdministrator = true;
155                         }
156
157                         // 対CRSF:Tokenチェック
158                         $this->Token->checkToken();
159
160                         // 認証アクション設定
161                         $this->AuthPlus->loginAction = '/users/login';
162                         $this->AuthPlus->loginRedirect = Configure::read('Routing.basePath');
163                         $this->AuthPlus->logoutRedirect = Configure::read('Routing.basePath');
164                         if ($this->isAdmin) {
165                                 $this->AuthPlus->loginRedirect = '/admin/users/index';
166                         }
167                 }
168                 $this->set('user', $this->user);
169                 $this->set('isAdministrator', $this->isAdministrator);
170         }
171
172         function beforeRender()
173         {
174                 if (isset($this->Benchmark) && is_object($this->Benchmark)) {
175                         $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' . ' beforeRenderStart');
176                 }
177
178                 parent::beforeRender();
179         }
180
181         function afterFilter()
182         {
183                 if (isset($this->Benchmark) && is_object($this->Benchmark)) {
184                         $this->Benchmark->report($this->params['controller']. '/'. $this->action . ':' . ' afterFilterStart');
185                 }
186                 parent::afterFilter();
187         }
188
189         /* 共通関数 */
190         /* public_flag設定をview用にセット */
191         function set_public_flag4view()
192         {
193                 $this->model_public_flags = $this->get_public_flag();
194
195                 $this->set('public_flags', $this->model_public_flags);
196         }
197         function get_public_flag()
198         {
199                 return $this->{$this->modelClass}->public_flag;
200         }
201         function check_public_flag($data, $key = null)
202         {
203                 return CorePlus::check_public_flag($data, $key);
204         }
205         function check_public_flag2($data, $key = null)
206         {
207                 return CorePlus::check_public_flag2($data, $key);
208         }
209
210         /* status設定をview用にセット */
211         function set_status4view()
212         {
213                 $this->model_status = $this->get_status();
214
215                 $this->set('status', $this->model_status);
216
217                 $this->model_status2 = array_merge($this->model_status, array('all'));
218
219                 $this->set('status2', $this->model_status2);
220         }
221         function get_status()
222         {
223                 if (isset($this->Character)) {
224                         return $this->Character->status;
225                 } elseif (isset($this->CharacterProfileArchive)) {
226                         return $this->CharacterProfileArchive->Character->status;
227                 } else {
228                         return array();
229                 }
230         }
231
232         /* isOwner */
233         function isOwner($data, $user_id)
234         {
235                 return CorePlus::isOwner($data, $user_id);
236         }
237
238         /* ユーザ情報取得 */
239         function getUser($id, $isAdmin=false, $isDelete=false) {
240                 if (!$id) {
241                         return array();
242                 }
243
244                 if (!isset($this->User)) {
245                         $this->User = CorePlus::set_model('User');
246                 }
247
248                 $conditions = array(
249                         'User.id' => $id,
250                 );
251                 if ($isAdmin === true) {
252                         $conditions['User.group_id'] = array(
253                                 Configure::read('Group.admin'),
254                                 Configure::read('Group.subadmin'),
255                                 Configure::read('Group.watcher'),
256                                 Configure::read('Group.member'),
257                                 Configure::read('Group.locked'),
258                                 Configure::read('Group.pre'),
259                         );
260                 } else {
261                         $conditions['User.group_id'] = array(
262                                 Configure::read('Group.admin'),
263                                 Configure::read('Group.subadmin'),
264                                 Configure::read('Group.watcher'),
265                                 Configure::read('Group.member'),
266                                 Configure::read('Group.locked'),
267                         );
268                 }
269
270                 $fields = array(
271                         'User.id',
272                         'User.group_id',
273                         'User.name',
274                         'User.pcmail',
275                         'User.url',
276                         'User.notes',
277                         'User.modified',
278                 );
279                 if ($isAdmin === true) {
280                         $fields = array_merge($fields, array(
281                                 'User.username',
282                                 'User.useragent',
283                                 'User.host',
284                                 'User.created',
285                         ));
286                 }
287
288                 $contain = array(
289                          'Attachment',
290                 );
291                 if ($this->name == 'Users') {
292                         $contain = array_merge($contain, array(
293                                 'CharacterSheet' => array(
294                                         'System',
295                                 ),
296                         ));
297                 }
298
299                 if ($id == $this->user_id || $isAdmin === true) {
300                         unset($this->User->hasMany['CharacterSheet']['conditions']['CharacterSheet.public_flag']);
301                 } else {
302                         $this->User->hasMany['CharacterSheet']['conditions']['CharacterSheet.public_flag'] = 'public';
303                 }
304
305                 if ($isAdmin === true || $isDelete === true) {
306                         $contain = array_merge($contain, array(
307                                 'Character',
308                         ));
309
310                         unset($this->User->hasMany['Character']['conditions']['Character.public_flag']);
311                         unset($this->User->hasMany['Character']['limit']);
312                 }
313
314                 if (empty($contain)) {
315                         $recursive = -1;
316                 } else {
317                         $recursive = Set::countDim($contain, true);
318                 }
319
320                 $user = $this->User->find('first', array(
321                         'conditions' => $conditions,
322                         'fields' => $fields,
323                         'recursive' => $recursive,
324                         'contain' => $contain,
325                 ));
326
327                 return $user;
328         }
329
330         /* 指定Systemを取得 */
331         function _getThisSystem($id, $isAdmin = false)
332         {
333                 static $this_system;
334                 static $prev_id;
335                 static $prev_isAdmin;
336
337                 if (!empty($this_system) 
338                         && (!empty($prev_id) && $id == $prev_id)
339                         && ($isAdmin == $prev_isAdmin)) {
340                         return $this_system;
341                 }
342
343                 $prev_id = $id;
344                 $prev_isAdmin = $isAdmin;
345                 $system_conditions['System.id'] = $id;
346                 if ($isAdmin === false) {
347                         $system_conditions['System.public_flag'] = 'public';
348                 }
349
350                 $this_system = $this->{$this->modelClass}->System->find('first', array(
351                         'conditions' => $system_conditions,
352                         'recursive' => -1,
353                 ));
354
355                 if (!isset($this_system['System'])) {
356                         $this->Session->setFlash(__('Invalid System.', true));
357                         $this->redirect(array('action' => 'index'));
358                 }
359
360                 return $this_system;
361         }
362
363         /* デモモードチェック */
364         function _check_demo()
365         {
366                 if (Configure::read('Mode.Demo')) {
367                         if ($_SESSION['Auth']['User']['username'] == 'guest') {
368                                 $this->Session->setFlash(__('Unavailable for GUEST.', true));
369
370                                 if ($this->site_configs['User.newUserRegist']['value']) {
371                                         $this->redirect(array('controller' => 'users', 'action'=>'add'));
372                                 // 新規登録停止の場合
373                                 } else {
374                                         $this->redirect(array('action'=>'index'));
375                                 }
376                         }
377                 }
378         }
379
380         /* システム情報取得 */
381         function _restore_html_system($data, $nl2br = false) {
382                 if (isset($data['System']['name'])  && !empty($data['System']['name'])) {
383                         $data['System']['name'] = $this->{$this->modelClass}->restore_html($data['System']['name'], false, false, false);
384                 }
385                 if (isset($data['System']['copyright'])  && !empty($data['System']['copyright'])) {
386                         $data['System']['copyright'] = $this->{$this->modelClass}->restore_html($data['System']['copyright'], false, false, false);
387                 }
388                 if (isset($data['System']['url'])  && !empty($data['System']['url'])) {
389                         $data['System']['url'] = $this->{$this->modelClass}->restore_html($data['System']['url'], false, false, false);
390                 }
391                 if (isset($data['System']['detail'])  && !empty($data['System']['detail'])) {
392                         $data['System']['detail'] = $this->{$this->modelClass}->restore_html($data['System']['detail'], false, false, false);
393                         if ($nl2br) {
394                                 $data['System']['detail'] = str_replace('<br />', "\n", $data['System']['detail']);
395                         }
396                 }
397                 if (isset($data['System']['ad'])  && !empty($data['System']['ad'])) {
398                         $data['System']['ad'] = $this->{$this->modelClass}->restore_html($data['System']['ad'], false, false, false);
399                 }
400
401                 if (isset($data['Profile']) && !empty($data['Profile'])) {
402                         $data = $this->_restore_html_profile($data);
403                 }
404
405                 return $data;
406         }
407
408         function _get_systems($public_flag = 'public', $conditions = array(), $fields = array())
409         {
410                 if (!isset($this->System)) {
411                         $this->System = CorePlus::set_model('System');
412                 }
413
414                 if (empty($public_flag)) {
415                         if (isset($conditions['System.public_flag'])) {
416                                 unset($conditions['System.public_flag']);
417                         }
418                 } else {
419                         $conditions = array_merge(
420                                 $conditions,
421                                 array(
422                                         'System.public_flag' => $public_flag,
423                                 )
424                         );
425                 }
426
427
428                 $fields = array_merge(
429                         array(
430                                 'System.id',
431                                 'System.name',
432                                 'System.sort_order',
433                         ),
434                         $fields
435                 );
436
437                 $systems = $this->System->find('all', array(
438                         'conditions' => $conditions,
439                         'fields' => $fields,
440                         'recursive' => -1,
441                 ));
442
443                 if (!empty($systems)) {
444                         $systems = $this->_restore_html_get_systems($systems, $public_flag);
445                 }
446
447                 return $systems;
448         }
449
450         /* キャラクター一覧取得 */
451         /* 条件セット */
452         function _set_conditions_characters4user_id($user_id = null, $conditions = array(), $fields = array(), $contain = array(), $order = array())
453         {
454                 if (!empty($this->showlist_cols)) {
455                         $this->contain = array_merge($this->contain, array('CharactersHasProfile'));
456                         if (isset($this->showlist_cols['Profile']) && !empty($this->showlist_cols['Profile'])) {
457                                 $this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.profile_id'] = $this->showlist_cols['Profile'];
458                         }
459                 }
460
461                 if (!empty($user_id)) {
462                         $conditions = array_merge((array)$conditions, 
463                                 array(
464                                         'Character.user_id' => $user_id,
465                                 )
466                         );
467
468                 }
469
470                 $conditions = array_merge($this->conditions, (array)$conditions);
471                 if ((isset($conditions['isAdmin']) && !empty($conditions['isAdmin']))
472                         || (!empty($user_id) && ($user_id == $this->user_id))
473                         || (isset($conditions['public_force']) && !empty($conditions['public_force']))) {
474                         if (isset($this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.public_flag'])) {
475                                 unset($this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.public_flag']);
476                         }
477                         unset($conditions['Character.public_flag']);
478                         unset($conditions['CharactersHasProfile.public_flag']);
479                 } else {
480                         if (isset($conditions['profile_search'])) {
481                                 $this->Character->hasMany['CharactersHasProfile']['conditions']['CharactersHasProfile.public_flag'] = 'public';
482                         }
483                         $conditions['Character.public_flag'] = 'public';
484 /*                      if (!isset($conditions['CharactersHasProfile.public_flag']) || empty($conditions['CharactersHasProfile.public_flag'])) {
485                                 $conditions['CharactersHasProfile.public_flag'] = 'public';
486                         }*/
487                 }
488                 unset($conditions['isAdmin']);
489                 unset($conditions['public_force']);
490
491                 $fields = array_merge($this->fields,
492                         (array)$fields
493                 );
494
495                 $contain = array_merge($this->contain, (array)$contain);
496                 $contain = array_unique($contain);
497                 if (empty($contain)) {
498                         $recursive = -1;
499                 } else {
500                         $recursive = Set::countDim($contain);
501                 }
502
503                 $order = array_merge($this->order, (array)$order);
504
505                 return array($conditions, $fields, $contain, $recursive, $order);
506         }
507
508         function _get_characters_list4user_id($user_id = null, $conditions = array(), $limit = 5, $fields = array(), $contain = array(), $order = array())
509         {
510                 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
511
512                 if ($this->modelClass != 'Character') {
513                         $this->Character = CorePlus::set_model('Character');
514                 }
515
516                 return $this->Character->find('all', array(
517                         'conditions' => $conditions,
518                         'limit' => $limit,
519                         'fields' => $fields,
520                         'recursive' => $recursive,
521                         'contain' => $contain,
522                         'order' => $order,
523                 ));
524         }
525
526         function _get_characters_page4user_id($user_id = null, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1)
527         {
528                 list($conditions, $fields, $contain, $recursive, $order) = $this->_set_conditions_characters4user_id($user_id, $conditions, $fields, $contain, $order);
529
530                 if (!isset($this->Character)) {
531                         $this->Character = CorePlus::set_model('Character');
532                 }
533
534                 // Systemの取得項目
535                 if ($key = array_search('System.copyright', $this->Character->belongsTo['System']['fields'])) {
536                         unset($this->Character->belongsTo['System']['fields'][$key]);
537                 }
538
539                 $this->paginate['Character'] = array(
540                         'limit' => $limit,
541                         'conditions' => $conditions,
542                         'fields' => $fields,
543                         'contain' => $contain,
544                         'recursive' => $recursive,
545                         'order' => $order,
546                         'page' => $page
547                 );
548
549                 return $this->paginate('Character');
550         }
551
552
553         /* キャラクター情報取得 */
554         function _get_character4character_id($character_id, $user_id = null, $public_flag = null, $is_deleted = 0)
555         {
556                 if (!isset($this->Character)) {
557                         $this->Character = CorePlus::set_model('Character');
558                 }
559
560                 $conditions = array(
561                         'Character.id' => $character_id,
562                         'Character.deleted' => $is_deleted,
563                 );
564                 if ($user_id) {
565                         $conditions['Character.user_id'] = $user_id;
566                 }
567
568                 unset($this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag']);
569                 if ($public_flag) {
570                         $conditions['Character.public_flag'] = $public_flag;
571                         $this->Character->hasMany['CharacterPicture']['conditions']['CharacterPicture.public_flag'] = $public_flag;
572                 }
573
574                 $character = $this->Character->find('first', array(
575                         'conditions' => $conditions,
576                         'contain' => array(
577                                 'CharacterPicture' => array(
578                                         'Attachment',
579                                 ),
580                         ),
581                         'recursive' => 2,
582                 ));
583                 if (empty($character)) {
584                         $this->Session->setFlash(__('Invalid Character.', true));
585                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
586                 }
587                 return $character;
588         }
589
590         function check_character_picture_max($character_pictures_num)
591         {
592                 if (!$max_num = intval($this->site_configs['Character.maxPictures']['value'])) {
593                         return true;
594                 }
595
596                 if ($max_num <= $character_pictures_num) {
597                         return false;
598                 }
599
600                 return true;
601         }
602
603         /* restore_html */
604         function _restore_html_user($data, $nl2br = false) {
605                 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
606
607                 if (isset($data['notes']) && !empty($data['notes'])) {
608                         $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
609                         if ($nl2br) {
610                                 $data['notes'] = str_replace('<br />', "\n", $data['notes']);
611                         }
612                 }
613
614                 // アドレス復号化
615                 $data = $this->decrypt_mail($data);
616
617                 return $data;
618         }
619         function _restore_html_news($data, $nl2br = false) {
620                 $data['title'] = $this->{$this->modelClass}->restore_html($data['title'], false, false, false);
621
622                 if (isset($data['value']) && !empty($data['value'])) {
623                         $data['value'] = $this->{$this->modelClass}->restore_html($data['value'], false, false, false);
624                         if ($nl2br) {
625                                 $data['value'] = str_replace('<br />', "\n", $data['value']);
626                         }
627                 }
628
629                 return $data;
630         }
631
632         // アドレス復号化
633         function decrypt_mail($data)
634         {
635                 if (isset($data['pcmail']) && !empty($data['pcmail'])) {
636                         $data['pcmail'] = $this->Crypt->decrypt($data['pcmail']);
637                 }
638                 return $data;
639         }
640
641         /* Profile系 htmlRestore */
642         function _restore_html_profile($data) {
643                 if (isset($data['Profile']['name'])  && !empty($data['Profile']['name'])) {
644                         $data['Profile']['name'] = $this->{$this->modelClass}->restore_html($data['Profile']['name'], false, false, false);
645                 }
646
647                 if (isset($data['Profile'][0]['ProfileSelect'])) {
648                         foreach ($data['Profile'] as $k => $v) {
649                                 $data['Profile'][$k] = $this->__restore_html_profile($v);
650                         }
651                         $data['Profile'] = $this->_restore_html_profiles($data['Profile']);
652
653                 } elseif(isset($data['Profile']['ProfileSelect'])) {
654                         $data['Profile'] = $this->__restore_html_profile($data['Profile']);
655                 }
656
657                 return $data;
658         }
659         function __restore_html_profile($data) {
660                 if (isset($data['ProfileSelect']) && !empty($data['ProfileSelect'])) {
661                         $data['ProfileSelect'] = $this->_restore_html_profile_select($data['ProfileSelect']);
662                 }
663                 if (isset($data['ProfileTable']) && !empty($data['ProfileTable'])) {
664                         $data['ProfileTable'] = $this->_restore_html_profile_table($data['ProfileTable']);
665                         if (isset($data['ProfileTable'][0]['ProfileTableStatic']) && !empty($data['ProfileTable'][0]['ProfileTableStatic'])) {
666                                 $data['ProfileTable'][0]['ProfileTableStatic'] = $this->_restore_html_profile_table_static($data['ProfileTable'][0]['ProfileTableStatic']);
667                         }
668                 }
669
670                 return $data;
671         }
672         function _restore_html_profiles($data) {
673                 $sort_order = array();
674                 foreach ($data as $k => $v) {
675                         if (isset($v['value'])  && !empty($v['value'])) {
676                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
677                         }
678
679                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
680                                 $sort_order[$k] = $v['sort_order'];
681                         } else {
682                                 $sort_order[$k] = 0;
683                         }
684                 }
685
686                 $data = $this->sort4sort_order($data, $sort_order);
687
688                 return $data;
689         }
690         function _restore_html_get_systems($data, $public_flag = array()) {
691                 $sort_order = array();
692                 foreach ($data as $k => $v) {
693                         $restored[$this->{$this->modelClass}->restore_html($v['System']['name'])] = $v['System']['id'];
694                         // SingleSystem
695                         if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value'] && !empty($public_flag)) {
696                                 return $restored;
697                         }
698
699                         if (isset($v['System']['sort_order'])  && !empty($v['System']['sort_order'])) {
700                                 $sort_order[$v['System']['id']] = $v['System']['sort_order'];
701                         } else {
702                                 $sort_order[$v['System']['id']] = 0;
703                         }
704                 }
705
706                 $restored = $this->sort4sort_order($restored, $sort_order);
707
708                 return array_flip($restored);
709         }
710         function _restore_html_profile_select($data) {
711                 $sort_order = array();
712                 foreach ($data as $k => $v) {
713                         if (isset($v['value'])  && !empty($v['value'])) {
714                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
715                         }
716
717                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
718                                 $sort_order[$k] = $v['sort_order'];
719                         } else {
720                                 $sort_order[$k] = 0;
721                         }
722                 }
723
724                 $data = $this->sort4sort_order($data, $sort_order);
725
726                 return $data;
727         }
728         function _restore_html_profile_table($data) {
729                 foreach ($data as $k => $v) {
730                         if (isset($v['value'])  && !empty($v['value'])) {
731                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
732                         }
733
734                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
735                                 $sort_order[$k] = $v['sort_order'];
736                         } else {
737                                 $sort_order[$k] = 0;
738                         }
739                 }
740
741                 $data = $this->sort4sort_order($data, $sort_order);
742
743                 return $data;
744         }
745         function _restore_html_profile_table_static($data) {
746                 if (empty($data) || !is_array($data)) {
747                         return $data;
748                 }
749
750                 foreach ($data as $k => $v) {
751                         if (isset($v['title'])  && !empty($v['title'])) {
752                                 $data[$k]['title'] = $this->{$this->modelClass}->restore_html($v['title'], false, false, false);
753                         }
754
755                         if (isset($v['sort_order'])  && !empty($v['sort_order'])) {
756                                 $sort_order[$k] = $v['sort_order'];
757                         } else {
758                                 $sort_order[$k] = 0;
759                         }
760                 }
761
762                 $data = $this->sort4sort_order($data, $sort_order);
763
764                 return $data;
765         }
766
767         function sort4sort_order($data, $sort_order)
768         {
769                 if (empty($data) || empty($sort_order)) {
770                         return $data;
771                 }
772                 array_multisort($sort_order, SORT_ASC, $data);
773
774                 return $data;
775         }
776
777         function _checkCharaSheeOwner($characterSheet)
778         {
779                 if (!isset($characterSheet['CharacterSheet']) || empty($characterSheet['CharacterSheet']) || !isset($characterSheet['CharacterSheet']['user_id']) || empty($characterSheet['CharacterSheet']['user_id'])) {
780                         return false;
781                 }
782
783                 if ($characterSheet['CharacterSheet']['user_id'] == $this->user_id) {
784                         return true;
785                 }
786
787                 return false;
788         }
789
790         function checkEditOtherSystem($system_id = null)
791         {
792                 if ($this->isOtherSystem($system_id)) {
793                         $this->Session->setFlash(__('Other System cannot have ANY PROFILE SETTINGS.', true));
794                         $this->redirect(array('controller' => 'systems', 'action'=>'view', $system_id));
795                 }
796         }
797         function isOtherSystem($system_id = null)
798         {
799                 if (empty($system_id)) {
800                         return false;
801                 }
802
803                 if (empty($this->site_configs['System.otherSetting']['value']) || $this->site_configs['System.otherSetting']['value'] != $system_id) {
804                         return false;
805                 }
806
807                 return true;
808         }
809
810
811         /* POSTのCharactersHasProfileを処理 */
812         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array(), $allow_blank = false)
813         {
814                 $prev_profile_id = null;
815                 $profileTable_tmp = array();
816                 $i = 0;
817                 $public = null;
818                 foreach ($characters_has_profiles as $k => $v) {
819                         if (!empty($character_id)) {
820                                 $characters_has_profiles[$k]['character_id'] = $character_id;
821                         }
822                         // 配列valueの処理
823                         if (is_array($v['value'])) {
824                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
825                         }
826
827                         // サニタイズ
828                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
829                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
830                         // textarea改行処理
831                         if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
832 //                              $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
833                                 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
834                         }
835
836                         // 空の値処理, public_flag
837                         // table: 一時保管して全項目空の場合行削除
838                         if (CorePlus::is_valid($v, 'profile_table_id')) {
839                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
840                                         $i++;
841                                         $public = null;
842                                 }
843
844                                 // 行単位public_flag
845                                 if ($public == null && isset($characters_has_profiles[$k]['public_flag'])) {
846                                         $public = $v['public_flag'];
847                                         // Static設定タイトルは公開
848                                         if ($v['profile_table_static_id']) {
849                                                 $characters_has_profiles[$k]['public_flag'] = 'public';
850                                         }
851                                 } else {
852                                         $characters_has_profiles[$k]['public_flag'] = $public;
853                                 }
854
855                                 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
856                         // $allow_blank = falseならそれ以外は削除
857                         } elseif (empty($v['value']) && empty($allow_blank)) {
858                                 unset($characters_has_profiles[$k]);
859                         }
860                 }
861
862                 // table: 空行削除、不正profile_table_idデータ削除
863                 if (!empty($profileTable_tmp)) {
864                         if (!isset($this->Character->ProfileTable)) {
865                                 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
866                         }
867                         foreach($profileTable_tmp as $profile_id => $v) {
868                                 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
869                                         'conditions' => array('ProfileTable.profile_id' => $profile_id),
870                                         'fields' => array('ProfileTable.id'),
871                                         'order' => array('ProfileTable.sort_order' => 'asc'),
872                                         'recursive' => -1,
873                                 ));
874
875                                 foreach ($v as $i => $profile_tables) {
876                                         // 不正POSTの削除
877                                         $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
878                                         if (!empty($diff_keys)) {
879                                                 foreach($diff_keys as $dvalue) {
880                                                         unset($characters_has_profiles[$dvalue[1]]);
881                                                 }
882                                         }
883
884                                         // 空行削除
885                                         $delete_flg = true;
886                                         foreach($profile_table_columns as $k2 => $columns) {
887                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
888                                                         $delete_flg = false;
889                                                         break;
890                                                 }
891                                         }
892                                         if ($delete_flg === true) {
893                                                 foreach ($profile_tables as $delk) {
894                                                         unset($characters_has_profiles[$delk[1]]);
895                                                 }
896                                         }
897                                 }
898                         }
899                 }
900
901                 // 指定データのみ変更
902                 if (!is_null($profile_id) && !empty($now_data)) {
903                         $tmp = array();
904                         $i = 0;
905                         foreach($now_data as $k => $v) {
906                                 if ($v['id'] == $profile_id) {
907                                         $v['CharactersHasProfile'] = $characters_has_profiles;
908                                 }
909
910                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
911                                         $tmp[$i] = $v2;
912                                         if (isset($tmp[$i]['id'])) {
913                                                 unset($tmp[$i]['id']);
914                                         }
915                                         if (isset($tmp[$i]['character_id'])) {
916                                                 unset($tmp[$i]['character_id']);
917                                         }
918
919                                         $i++;
920                                 }
921                         }
922
923                         $characters_has_profiles = $tmp;
924                 }
925
926                 return $characters_has_profiles;
927         }
928
929         // 最新のお知らせ
930         function get_news($limit = 5) {
931                 $this->News = CorePlus::set_model('News');
932
933                 $news = $this->News->find('all', array(
934                         'conditions' => array(
935                                 'News.public_flag' => 'public',
936                                 'News.end_date > ' => date('Y-m-d 00:00:00', time()),
937                         ),
938                         'fields' => '',
939                         'recursive' => -1,
940                         'order' => array(
941                                 'News.date' => 'desc'
942                         ),
943                         'limit' => $limit,
944                 ));
945
946                 foreach($news as $k => $v) {
947                         $news[$k]['News'] = $this->_restore_html_news($v['News']);
948                 }
949
950                 return $news;
951         }
952
953 }
954
955 /*
956  * データ処理用
957  */
958
959 /*
960  * 文字コード変換 SJISWin->UTF-8
961  */
962 function convertEncodeSjis2Utf8(&$str, $key)
963 {
964         $str = mb_convert_encoding($str, 'UTF-8', 'SJIS-Win');
965 }
966