OSDN Git Service

bca6eeaf7e8b13ac37a96da8160d9808e7567bea
[trpgtools-onweb/cake-frame.git] / app / views / helpers / profiledisp.php
1 <?php
2 /* 
3  * プロフィール表示用Helper
4  * i18n対応
5  */
6 class ProfiledispHelper extends Helper {
7         var $helpers = array(
8                 'Html',
9                 'Text',
10                 'Form',
11                 'Select',
12         );
13
14         var $i18n_profile_types = array();
15
16         var $profile_type = array();
17
18         var $public_flags= array();
19
20         var $character_has_profile_num = 0;
21         var $character_has_profile_cols = 0;
22
23         var $form_id = 0;
24
25         function __construct()
26         {
27                 parent::__construct();
28
29                 $this->Character = CorePlus::set_model('Character');
30                 $this->public_flags = $this->Character->public_flag;
31         }
32
33
34         /**
35          * profile_typeの設定をview表示形式に変換
36          */
37         function get_i18n_profile_type($profile_type, $profile_types)
38         {
39                 if (empty($profile_type)) {
40                         return null;
41                 }
42
43                 if (empty($this->i18n_profile_types)) {
44                         $this->i18n_profile_types = $this->set_profile_type($profile_types);
45                 }
46
47                 return $this->i18n_profile_types[$profile_type];
48         }
49         /**
50          * profile_typeの選択プルダウン出力
51          * i18n対応
52          */
53         function create_profile_type_select($profile_types = array(), $fieldName = 'profile_type', $options = array('type' => 'select'))
54         {
55                 if (!isset($options['type'])) {
56                         $options['type'] = 'select';
57                 }
58                 if (!isset($options['options'])) {
59                         $options['options'] = array();
60                 }
61                 $options['options'] = $this->set_profile_type($profile_types, $options['options']);
62
63                 return $this->Form->input($fieldName, $options);
64         }
65         /* profile_typeの設定を、key->value(多言語化対応)に変換 */
66         function set_profile_type($profile_types = array(), $options = array())
67         {
68                 if (!empty($options)) {
69                         $this->i18n_profile_types = $options;
70                 }
71
72                 if (empty($profile_types)) {
73                         App::import('Model', 'Profile');
74                         $Profile = new ProfileModel;
75                         $profile_types = $Profile->profile_type;
76                 }
77
78                 foreach ($profile_types as $k => $v) {
79                         if ($v == 'input') {
80                                 $this->i18n_profile_types[$v] = __('input', true);
81                         } elseif($v == 'textarea') {
82                                 $this->i18n_profile_types[$v] = __('textarea', true);
83                         } elseif($v == 'select') {
84                                 $this->i18n_profile_types[$v] = __('select', true);
85                         } elseif($v == 'radio') {
86                                 $this->i18n_profile_types[$v] = __('radio', true);
87                         } elseif($v == 'checkbox') {
88                                 $this->i18n_profile_types[$v] = __('checkbox', true);
89                         } elseif($v == 'm-input') {
90                                 $this->i18n_profile_types[$v] = __('multi input', true);
91                         } elseif($v == 'table') {
92                                 $this->i18n_profile_types[$v] = __('table', true);
93                         } elseif($v == 's-table') {
94                                 $this->i18n_profile_types[$v] = __('static table', true);
95                         } else {
96                                 $this->i18n_profile_types[$v] = $v;
97                         }
98                 }
99
100                 return $this->i18n_profile_types;
101         }
102
103         /* profileの設定をフォームで出力 */
104         function disp_form4profile($profile, $form_num = 2, $disp_add_link = false, $disp_public = false, $options = array())
105         {
106                 if ($profile['profile_type'] == 'textarea') {
107                         $options['type'] = 'textarea';
108                 } else {
109                         $options['type'] = 'text';
110                 }
111
112                 $character_id = null;
113                 if (isset($options['character_id'])) {
114                         $character_id = $options['character_id'];
115                 }
116
117                 $isMulti = false;
118                 if ($options['type'] == 'textarea') {
119                         $isMulti = true;
120                 }
121
122                 if (!CorePlus::is_valid($profile, 'CharactersHasProfile.0.value')) {
123                         $profile['CharactersHasProfile'][0]['value'] = null;
124                 }
125
126                 foreach ($profile['CharactersHasProfile'] as $k => $v) {
127                         $disp_profile = $this->_get_profile_tag($profile['id'], $v['value'], false, $options);
128
129                         if ($disp_public) {
130                                 echo $this->dispPublicFlag4Profile($disp_profile, $this->character_has_profile_num, $v, false)."<br>\n";
131                         } else {
132                                 echo $disp_profile;
133                         }
134
135                         $this->character_has_profile_num++;
136                 }
137
138                 if ($this->profile_type['is_multi']) {
139                         for ($i=0;$i<$form_num;$i++) {
140                                 $disp_profile =  $this->_get_profile_tag($profile['id'], null, false, $options);
141
142                                 if ($disp_public) {
143                                         echo $this->dispPublicFlag4Profile($disp_profile, $this->character_has_profile_num, $profile, $isMulti)."<br>\n";
144                                 } else {
145                                         echo $disp_profile;
146                                 }
147
148                                 $this->character_has_profile_num++;
149                         }
150
151                         if ($disp_add_link) {
152                                 echo $this->Html->div('editLink', $this->Html->link(
153                                         __('Edit', true),
154                                         array(
155                                                 'action' => 'add_milti_profiles',
156                                                 $options['character_id'].
157                                                 '/profile_id:'.$profile['id']
158                                         ),
159                                         null,
160                                         __('If some data have been changed, you should save before click it. OK?', true)
161                                 ));
162                         }
163                 }
164         }
165         function _get_profile_tag($profile_id, $value = null, $label = false, $options = array())
166         {
167                 if (empty($profile_id)) {
168                         return null;
169                 }
170
171                 $name1 = sprintf('CharactersHasProfile.%s.value', $this->character_has_profile_num);
172                 $name2 = sprintf('CharactersHasProfile.%s.profile_id', $this->character_has_profile_num);
173                 $options = array_merge(array(
174                         'label' => $label,
175                         'value' => $value,
176                         ), $options
177                 );
178
179                 $profile_tag = 
180                         $this->Form->input($name1, $options).
181                         $this->Form->input($name2, array(
182                                 'type' => 'hidden',
183                                 'value' => $profile_id,
184                         ));
185                 if ($options['type'] == 'textarea') {
186                         $name3 = sprintf('CharactersHasProfile.%s.is_textarea', $this->character_has_profile_num);
187                         $profile_tag .= $this->Form->input($name3, array(
188                                 'type' => 'hidden',
189                                 'value' => 1,
190                         ));
191                 }
192
193                 return $profile_tag;
194         }
195
196         function dispPublicFlag4Profile($profile, $profile_num, $public_flag = null, $isMulti = false)
197         {
198                 $profilePublicName = sprintf('CharactersHasProfile.%d.public_flag', $profile_num);
199
200                 if (is_array($profile) && isset($profile['CharactersHasProfile'][0]['public_flag'])) {
201                         $selected_public_flag = $profile['CharactersHasProfile'][0]['public_flag'];
202                 } elseif(is_array($public_flag) && isset($public_flag['public_flag'])) {
203                         $selected_public_flag = $public_flag['public_flag'];
204                 } elseif (is_string($public_flag)) {
205                         $selected_public_flag = $public_flag;
206                 } else {
207                         $selected_public_flag = null;
208                 }
209
210                 $publicSelect = array(
211                         'label' => false,
212                         'div' => 'profilePublic',
213                         'value' => $selected_public_flag, 
214                 );
215                 if ($isMulti === true) {
216                         $publicSelect['div'] = 'profilePublic profilePublicMulti';
217                 }
218
219                 if (!empty($profile)) {
220                         $publicSelect['before'] = $profile;
221                 }
222
223                 $public_select = $this->Select->create_publicflag_select($this->public_flags, $profilePublicName, $publicSelect);
224                 if (!empty($profile)) {
225                         $public_select .= '<WBR>';
226                 }
227
228                 return $public_select;
229         }
230
231         /* profile_selectsの設定を出力 */
232         function disp4profile_selects($profile_selects, $disp_public = false, $option = array())
233         {
234                 foreach ($profile_selects as $k => $v) {
235                         $options[stripcslashes($v['value'])] = stripcslashes($v['value']);
236                 }
237                 $options = array_merge(
238                         array(
239                         'type' => 'select',
240                         'options' => $options,
241                         ),
242                         $option
243                 );
244
245                 $profile_select_tag = 
246                          $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', $options).
247                          $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_id', array(
248                                 'type' => 'hidden',
249                                 'value' => $profile_selects[0]['profile_id'],
250                         )).
251                          $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_select_id', array(
252                                 'type' => 'hidden',
253                                 'value' => $profile_selects[0]['id'],
254                         ));
255                 $profile = $profile_select_tag;
256
257                 if ($disp_public) {
258                         echo $this->dispPublicFlag4Profile($profile, $this->character_has_profile_num, $options['public_flag']);
259                 } else {
260                         echo $profile;
261                 }
262
263                 $this->character_has_profile_num++;
264         }
265
266
267         /* profile_selectsの設定をプルダウンで出力 */
268         function disp_select4profile_selects($profile_selects, $characters_has_profile = array(), $options = array(), $label = false, $legend = false, $disp_public = false, $isProfileDefault = false)
269         {
270                 if (empty($profile_selects)) {
271                         return null;
272                 }
273
274                 $selected = $this->set_form_value($profile_selects, $characters_has_profile, $isProfileDefault);
275
276                 $public_flag = null;
277                 if (isset($characters_has_profile[0]['public_flag'])) {
278                         $public_flag = $characters_has_profile[0]['public_flag'];
279                 }
280
281                 $options = array_merge(
282                         array(
283                                 'type' => 'select',
284                                 'label' => $label,
285                                 'legend' => $legend,
286                                 'selected' => $selected['value'],
287                                 'public_flag' => $public_flag,
288                         ),
289                         $options
290                 );
291
292                 $this->disp4profile_selects($profile_selects, $disp_public, $options);
293         }
294
295         /* profile_selectsの設定をラジオボタンで出力 */
296         function disp_radio4profile_selects($profile_selects, $characters_has_profile = array(), $label = false, $legend = false, $disp_public = false)
297         {
298                 if (empty($profile_selects)) {
299                         return null;
300                 }
301
302                 $selected = $this->set_form_value($profile_selects, $characters_has_profile);
303                 if ($selected['value']) {
304                         $selected_value = $selected['value'];
305                 } else {
306                         $first = reset($profile_selects);
307                         $selected_value = $first['value'];
308                 }
309
310                 $public_flag = null;
311                 if (isset($characters_has_profile[0]['public_flag'])) {
312                         $public_flag = $characters_has_profile[0]['public_flag'];
313                 }
314
315                 $options = array(
316                         'type' => 'radio',
317                         'value' => $selected_value,
318                         'label' => $label,
319                         'legend' => $legend,
320                         'public_flag' => $public_flag,
321                         'div' => array(
322                                 'class' => 'input radio inline'
323                         ),
324                 );
325
326                 return $this->disp4profile_selects($profile_selects, $disp_public, $options);
327
328         }
329
330         /* profile_selectsの設定をチェックボックスで出力 */
331         function disp_checkbox4profile_selects($profile_selects, $characters_has_profile = array(), $label = false, $legend = false, $disp_public = false, $isProfileDefault = false)
332         {
333                 if (empty($profile_selects)) {
334                         return null;
335                 }
336
337                 foreach($profile_selects as $k => $v) {
338                         if (empty($v)) {
339                                 continue;
340                         }
341
342                         $profile_select[0] = $v;
343                         $has_profile[0] = $this->get_selected_profile($v, $characters_has_profile, $isProfileDefault);
344                         $this->disp_select4profile_selects(
345                                 $profile_select, 
346                                 $has_profile,
347                                 array(
348                                         'multiple' => 'checkbox',
349                                         'label' => $label,
350                                         'legend' => $legend,
351                                         'div' => array(
352                                                 'class' => 'input checkbox inline'
353                                         ),
354                                 ), $label, $legend, $disp_public, $isProfileDefault
355                         );
356                 }
357         }
358
359         /* profile_tablesの設定からtableHeadersを出力 */
360         function disp_th4profile_tables($profile_tables, $tableId = null, $disp_public = false, $tr_options = array(), $th_options = array())
361         {
362                 if (empty($profile_tables)) {
363                         return null;
364                 }
365
366                 $values = array();
367                 $style_options = array();
368                 $i = 1;
369                 foreach ($profile_tables as $k => $v) {
370                         $values[$k] = $v['title'];
371
372                         if ($v['width']) {
373                                 $key = '#'.$tableId.' td.column-' . $i;
374                                 $style_options[$key] = array('width' => $v['width']."px");
375                                 $key = '#'.$tableId.' td.column-' . $i. ' input';
376                                 $style_options[$key] = array('width' => $v['width']."px");
377                         }
378                         $i++;
379                 }
380
381                 if ($disp_public === true) {
382                         $values[] = __('Public Flag', true);
383                 }
384
385                 if (!empty($style_options)) {
386                         echo '<style type="text/css"><!--'."\n";
387                         foreach($style_options as $k => $v) {
388                                 echo $k. " {\n";
389                                 echo $this->Html->style($v);
390                                 echo "}\n";
391                         }
392                         echo '--></style>';
393                 }
394
395                 $table_header = $this->Html->tableHeaders(
396                         $values,
397                         $tr_options,
398                         $th_options
399                 );
400
401                 $table_header = '<thead>'.$table_header.'</thead>';
402
403                 echo $table_header;
404         }
405
406         /* characters_has_profilesの設定からtableCellsを出力 */
407         function disp_td4characters_has_profiles($characters_has_profiles, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
408         {
409                 if (empty($characters_has_profiles)) {
410                         return null;
411                 }
412
413                 if (isset($characters_has_profiles[0]['ProfileTableStatic'])) {
414                         $statics = $characters_has_profiles[0]['ProfileTableStatic'];
415                 }
416
417                 $values = array();
418                 foreach ($characters_has_profiles as $k => $v) {
419                         if (is_string($v)) {
420                                 $values[$k] = $v;
421                         } else {
422                                 $values[$k] = $v['value'];
423                         }
424                 }
425
426                 $table_cells = $this->Html->tableCells($values, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
427
428                 echo $table_cells;
429         }
430
431         /* characters_has_profileからtable td出力 */
432         function disp_tds4characters_has_profiles($characters_has_profile, $type = 'view', $publicOnly = true, $profile_type = null, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true, $isProfileDefault = false)
433         {
434                 $i = 1;
435                 $profile_num = 0;
436                 $profiles[$i] = array();
437                 $profile_id = null;
438                 foreach($characters_has_profile as $k => $v) {
439                         if (is_null($profile_id) && CorePlus::is_valid($v, 'profile_id')) {
440                                 $profile_id = $v['profile_id'];
441                         }
442                         if (!CorePlus::is_valid($v, 'profile_id')) {
443                                 if (!is_null($profile_id) && !CorePlus::is_valid($v, 'profile_id')) {
444                                         $v['profile_id'] = $profile_id;
445                                 } else {
446                                         continue;
447                                 }
448                         }
449
450                         if (!CorePlus::is_valid($v, 'profile_table_id')) {
451                                 $profile_table_ids = array_keys(Set::combine($characters_has_profile, '{n}.profile_table_id'));
452
453                                 if (!empty($prof_table_ids)) {
454                                         $v['profile_table_id'] = current(array_slice($prof_table_ids, $profile_num+1, 1));
455                                         $v['value'] = null;
456                                 } else {
457                                         $v['profile_table_id'] = null;
458                                         $v['value'] = null;
459                                         continue;
460                                 }
461                         }
462
463                         $v['id'] = $v['profile_table_id'];
464
465                         if (isset($profiles[$i][$v['profile_table_id']])) {
466                                 $i++;
467                         }
468                         $profiles[$i][$v['profile_table_id']] = $v;
469
470                         $profile_num++;
471                 }
472                 $forms = array();
473
474                 foreach($profiles as $k => $v) {
475                         if ($type == 'form') {
476                                 $tds = $this->disp_form4profile_tables($v, $publicOnly, $oddTrOptions, $evenTrOptions, true, true, $isProfileDefault);
477                         } else {
478                                 $tds = $this->disp_value4profile_tables($v, $publicOnly, $profile_type, null, null, true, $isProfileDefault);
479                         }
480                 }
481                 $this->character_has_profile_cols = 0;
482
483                 $this->disp_td4characters_has_profiles($tds, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
484         }
485
486
487         /* characters_has_profileからvalue出力 */
488         function disp_value_td4characters_has_profiles($characters_has_profile, $publicOnly = true, $profile_type = null)
489         {
490                 $this->disp_tds4characters_has_profiles($characters_has_profile, 'view', $publicOnly, $profile_type, null, null, true);
491         }
492
493         /* characters_has_profileからtdフォーム出力 */
494         function disp_form_td4characters_has_profiles($characters_has_profile, $disp_public = false, $profile_type = null, $isProfileDefault = false)
495         {
496                 if ($disp_public === true) {
497                         $publicOnly = false;
498                 } else {
499                         $publicOnly = true;
500                 }
501                 $this->disp_tds4characters_has_profiles($characters_has_profile, 'form', $publicOnly, $profile_type, null, null, true, true, $isProfileDefault);
502         }
503
504         /* profile_tablesの設定からView表示を出力 */
505         function disp_value4profile_tables($profile_tables, $publicOnly = true, $profile_type = null, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true, $isProfileDefault = false)
506         {
507                 if (empty($profile_tables)) {
508                         return null;
509                 }
510
511                 $tds = array();
512                 $i = 0;
513                 foreach ($profile_tables as $k => $v) {
514                         if (!Set::check($v, 'value')) {
515                                 $v['value'] = "&nbsp";
516                         } elseif (!$this->isPublic($v, $publicOnly)) {
517                                 $v['value'] = "&nbsp";
518                         }
519                         if(!$publicOnly && $v['public_flag'] != 'public') {
520                                 $v['value'] = $this->Html->tag('span', $v['value'], array('class' => 'publicFlagBg'));
521                         }
522                         if ($this->isPublic($v, $publicOnly)|| $profile_type != 'table') {
523                                 $tds[] = $v['value'];
524                         }
525                         $i++;
526                 }
527
528                 $this->disp_td4characters_has_profiles($tds, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
529         }
530
531         /* profile_tablesの設定から入力フォームを出力 */
532         function disp_form4profile_tables($profile_tables, $publicOnly = true, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true, $isProfileDefault = false)
533         {
534                 if (empty($profile_tables)) {
535                         return null;
536                 }
537                 $first = reset($profile_tables);
538
539                 if (isset($first['ProfileTableStatic'])) {
540                         $statics = $first['ProfileTableStatic'];
541                 }
542
543                 $forms = array();
544                 $i = 0;
545                 $first_num = $this->character_has_profile_num;
546                 $first = reset($profile_tables);
547                 foreach ($profile_tables as $k => $v) {
548                         if (!Set::check($v, 'profile_id')) {
549                                 continue;
550                         }
551                         if (!Set::check($v, 'id')) {
552                                 if (Set::check($v, 'profile_table_id')) {
553                                         $v['id'] = $v['profile_table_id'];
554                                 } else {
555                                         continue;
556                                 }
557                         }
558
559                         if (!Set::check($v, 'value')) {
560                                 $v['value'] = null;
561                         }
562
563                         if ($i == 0 && (isset($statics[$this->character_has_profile_cols]) || (isset($v['profile_table_static_id']) && !empty($v['profile_table_static_id'])))) {
564                                 $value = isset($v['profile_table_static_id']) ? $v['value']: $statics[$this->character_has_profile_cols]['title']; 
565                                 $form = '<span class="staticHead">'.$value.'</span>'.
566                                         $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', array(
567                                                 'type' => 'hidden',
568                                                 'value' => $value,
569                                         ));
570                         } else {
571                                 $form = $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', array(
572                                         'type' => 'text',
573                                         'label' => false,
574                                         'class' => 'longInput',
575                                         'value' => $v['value'],
576                                 ));
577                         }
578
579                         if (isset($v['profile_table_static_id'])) {
580                                 $profile_table_static_id = $v['profile_table_static_id'];
581                         } elseif (isset($profile_tables[0]['ProfileTableStatic'][$this->character_has_profile_cols]['id'])) {
582                                 $profile_table_static_id = $profile_tables[0]['ProfileTableStatic'][$this->character_has_profile_cols]['id'];
583                         } else {
584                                 $profile_table_static_id = null;
585                         }
586
587                         $forms[] = 
588                                 $form.
589                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_id', array(
590                                         'type' => 'hidden',
591                                         'value' => $v['profile_id'],
592                                 )).
593                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_table_id', array(
594                                         'type' => 'hidden',
595                                         'value' => $v['id'],
596                                 )).
597                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_table_static_id', array(
598                                         'type' => 'hidden',
599                                         'value' => $profile_table_static_id,
600                                 ));
601                         $this->character_has_profile_num++;
602                         $i++;
603                 }
604
605                 if ($publicOnly === false) {
606                         if ($isProfileDefault && isset($last["public_flag"])) {
607                                 $last = end($profile_tables);
608                                 $forms[] = $this->dispPublicFlag4Profile(null, $first_num, $last["public_flag"], false);
609                         } else {
610                                 $forms[] = $this->dispPublicFlag4Profile(null, $first_num, $first, false);
611                         }
612                 }
613
614                 $this->character_has_profile_cols++;
615
616                 $this->disp_td4characters_has_profiles($forms, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
617         }
618
619         /* profile_tablesの設定からブランクのtableCellsを出力 */
620         function disp_blank_td4profile_tables($profile_tables, $num = 2, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
621         {
622                 if (empty($profile_tables)) {
623                         return null;
624                 }
625
626                 $blank = array_fill(0, count($profile_tables), $this->Html->div('blank', "&nbsp;"));
627
628                 for ($i=0; $i<$num; $i++) {
629                         $this->disp_td4characters_has_profiles($blank, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
630                 }
631         }
632
633         /* Profile種類を判断 */
634         function set_profile_type4view($profile)
635         {
636                 $this->profile_type = array(
637                         'is_subsetting' => false,
638                         'is_select' => false,
639                         'is_radio' => false,
640                         'is_checkbox' => false,
641                         'is_table' => false,
642                         'is_multi' => false,
643                 );
644
645                 if ($profile['profile_type'] == 'select' || $profile['profile_type'] == 'radio' || $profile['profile_type'] == 'checkbox'  || $profile['profile_type'] == 'table' ) {
646                         $this->profile_type['is_subsetting'] = true;
647                 }
648                 if ($profile['profile_type'] == 'select') {
649                         $this->profile_type['is_select'] = true;
650                 }
651                 if ($profile['profile_type'] == 'radio') {
652                         $this->profile_type['is_radio'] = true;
653                 }
654                 if ($profile['profile_type'] == 'checkbox') {
655                         $this->profile_type['is_checkbox'] = true;
656                 }
657                 if ($profile['profile_type'] == 'table' || $profile['profile_type'] == 's-table') {
658                         $this->profile_type['is_table'] = true;
659                 }
660                 if ($profile['profile_type'] == 'm-input') {
661                         $this->profile_type['is_multi'] = true;
662                 }
663         }
664
665         /* profile_typeの設定に応じてフォーム表示
666          * set_profile_type4view($profile)チェック済み前提。
667          */
668         function disp_forms4profile_type($profile, $form_num = 2, $table_blank = 2, $disp_add_link = false, $character_id = null, $disp_public = false, $options = array(), $isProfileDefault = false)
669         {
670                 if (!isset($profile['CharactersHasProfile']) || empty($profile['CharactersHasProfile'])) {
671                         if (isset($profile['DefaultProfile']) && !empty($profile['DefaultProfile'])) {
672                                 $profile['CharactersHasProfile'] = $profile['DefaultProfile'];
673                                 $form_num = $form_num - count($profile['DefaultProfile']) +1;
674                         } else {
675                                 $profile['CharactersHasProfile'] = array();
676                         }
677                 }
678
679                 if ($this->profile_type['is_select'] || $this->profile_type['is_radio'] || $this->profile_type['is_checkbox']) {
680                         if ($this->profile_type['is_select']) {
681                                 $this->disp_select4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile'], array('div' => array('class' => 'inline')), false, false, $disp_public);
682                         } elseif ($this->profile_type['is_radio']) {
683                                 $this->disp_radio4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile'], false, false, $disp_public);
684                         } elseif ($this->profile_type['is_checkbox']) {
685                                 $this->disp_checkbox4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile'],  false, false, $disp_public, $isProfileDefault);
686                         }
687                 } elseif ($this->profile_type['is_table']) {
688                         if ($disp_public == true) {
689                                 $publicOnly = false;
690                         } else {
691                                 $publicOnly = true;
692                         }
693
694                         if (isset($profile['id'])) {
695                                 $tableId = 'TableProfileId'. $profile['id'];
696                         } else {
697                                 $tableId = 'TableProfileId'. $profile['Profile']['id'];
698                         }
699                         echo '<table id="'.$tableId.'">';
700                         $this->disp_th4profile_tables($profile['ProfileTable'], $tableId, $disp_public);
701                         echo '<tbody>';
702                         if (!empty($profile['CharactersHasProfile'])) {
703                                 $this->disp_form_td4characters_has_profiles($profile['CharactersHasProfile'], $disp_public, null, $isProfileDefault);
704
705                         }
706                         if (intval($form_num) > 0 
707                                 && ((isset ($profile['Profile']['profile_type']) && $profile['Profile']['profile_type'] != 's-table') || (isset($profile['profile_type']) && $profile['profile_type'] != 's-table'))
708                                 || empty($profile['CharactersHasProfile'])) {
709                                 for ($i=0; $i<$form_num; $i++) {
710                                         $this->disp_form4profile_tables($profile['ProfileTable'], $publicOnly, array(), array(), true);
711                                 }
712                                 $this->character_has_profile_cols = 0;
713                         }
714                         if (intval($table_blank) > 0 && $profile['profile_type'] != 's-table') {
715                                 $this->disp_blank_td4profile_tables($profile['ProfileTable'], intval($table_blank), null, null, true);
716                         }
717                         echo '</tbody>';
718                         echo '</table>';
719                         if ($disp_add_link) {
720                                 $link = $this->Html->link(
721                                         __('Add Column', true),
722                                         array(
723                                                 'action' => 'add_milti_profiles',
724                                                 $character_id.
725                                                 '/profile_id:'.$profile['id']
726                                         ),
727                                         null,
728                                         __('If some data have been changed, you should save before click it. OK?', true)
729                                 );
730                                 if (!empty($profile['CharactersHasProfile'][0]['character_id'])) {
731                                         $link .= " ".$this->Html->link(
732                                                 __('Clear', true),
733                                                 array(
734                                                         'controller' => 'characters_has_profiles',
735                                                         'action' => 'delete',
736                                                         $profile['id'],
737                                                         'character_id:'.$profile['CharactersHasProfile'][0]['character_id'],
738                                                 ),
739                                                 null,
740                                                 sprintf(__('Are you sure you want to clear the data of %s?', true), $profile['name'])
741                                         );
742                                 }
743
744                                 echo $this->Html->div(
745                                         'editLink text', 
746                                         $link
747                                 );
748                         }
749
750                 } else {
751                         $options = array_merge(array(
752                                 'character_id' => $character_id,
753                                 ), $options
754                         );
755                         $this->disp_form4profile($profile, $form_num, $disp_add_link, $disp_public, $options);
756                 }
757
758                 if (isset($profile['required']) && $profile['required']) 
759                 {
760                         echo $this->Html->tag(
761                                 'div',
762                                 __(' required', true),
763                                 array(
764                                         'class' => 'required'
765                                 )
766                         );
767                 }
768         }
769
770         /* characters_has_profilesの設定に応じてView表示
771          * set_profile_type4view($profile)チェック済み前提。
772          * $profileのkeyはprofilesのkey_name
773          */
774         function disp_values4characters_has_profiles($profile, $table_blank = 2, $disp_add_link = false, $character_id = null, $publicOnly = true)
775         {
776                 if (!isset($profile['CharactersHasProfile']) && !$this->profile_type['is_table']) {
777                         $profile['CharactersHasProfile'] = array();
778                 }
779
780                 if (!$this->profile_type['is_table']) {
781                         $num = 0;
782                         foreach ($profile['CharactersHasProfile'] as $v) {
783                                 if (!empty($v['value']) && $this->isPublic($v, $publicOnly)) {
784                                         if ($num > 0) {
785                                                 echo "&nbsp;";
786                                         }
787
788
789                                         if (!$publicOnly && isset($v['public_flag']) && $v['public_flag'] != 'public') {
790                                                 echo $this->Html->tag('span', 
791                                                         $this->autoLinkProf($v['value']),
792                                                         array(
793                                                                 'class' => 'publicFlagBg',
794                                                         )
795                                                 );
796
797                                                 echo $this->Html->tag('span', 
798                                                         __('[Private]', true), 
799                                                         array(
800                                                                 'class' => 'publicFlag publicFlagBg',
801                                                         )
802                                                 );
803                                         } else {
804                                                 echo $this->Html->tag('span',
805                                                         $this->autoLinkProf($v['value'])
806                                                 );
807                                         }
808
809                                         $num++;
810                                 }
811                         }
812
813                         if ($this->profile_type['is_multi'] && $disp_add_link) {
814                                 echo $this->Html->div('editLink', $this->Html->link(
815                                         __('Add Column', true),
816                                         array(
817                                                 'action' => 'add_milti_profiles',
818                                                 $character_id.
819                                                 '/profile_id:'.$profile['id']
820                                         )
821                                 ));
822                         }
823
824                 } else {
825                         $tableId = 'TableProfileId'. $profile['id'];
826                         echo '<table id="'.$tableId.'">';
827                         if (isset($profile['ProfileTable'])) {
828                                 $this->disp_th4profile_tables($profile['ProfileTable'], $tableId);
829                                 echo '<tbody>';
830                                 if (!empty($profile['CharactersHasProfile'])) {
831                                         $this->disp_value_td4characters_has_profiles($profile['CharactersHasProfile'], $publicOnly, $profile['profile_type']);
832                                 } else {
833                                         if (intval($table_blank) > 0) {
834                                                 $this->disp_blank_td4profile_tables($profile['ProfileTable'], intval($table_blank));
835                                         }
836                                 }
837                                 echo '</tbody>';
838                         }
839                         echo '</table>';
840                         if ($disp_add_link) {
841                                 $link = $this->Html->link(
842                                         __('Edit', true),
843                                         array(
844                                                 'action' => 'add_milti_profiles',
845                                                 $character_id.
846                                                 '/profile_id:'.$profile['id']
847                                         )
848                                 );
849                                 if (isset($profile['CharactersHasProfile'][0]) && !empty($profile['CharactersHasProfile'][0]['character_id'])) {
850                                         $link .= " ".$this->Html->link(
851                                                 __('Clear', true),
852                                                 array(
853                                                         'controller' => 'characters_has_profiles',
854                                                         'action' => 'delete',
855                                                         $profile['id'],
856                                                         'character_id:'.$profile['CharactersHasProfile'][0]['character_id'],
857                                                 ),
858                                                 null,
859                                                 sprintf(__('Are you sure you want to clear the data of %s?', true), $profile['name'])
860                                         );
861                                 }
862                                 echo $this->Html->div(
863                                         'editLink text', 
864                                         $link
865                                 );
866                         }
867                 }
868         }
869
870         /* フォームに値入力 */
871         function set_form_value($profile_selects, $characters_has_profile, $isProfileDefault = false)
872         {
873                 $this->form_id++;
874
875                 if ($this->profile_type['is_select'] || $this->profile_type['is_radio'] || $this->profile_type['is_checkbox']) {
876                         return $this->set_form_value_select($profile_selects, $characters_has_profile, $isProfileDefault);
877                 } elseif ($this->profile_type['is_table']) {
878                         return $this->set_form_value_table($characters_has_profile);
879                 } else {
880 //                      return $profile_selects;
881                         return $this->set_form_value_profile($profile_selects, $characters_has_profile);
882                 }
883
884         }
885
886         /* profile_select */
887         /* profile_select_id->profile_idで抽出 */
888         function set_form_value_select($profile_selects, $characters_has_profile, $isProfileDefault = false)
889         {
890                 if (isset($characters_has_profile[0]['value'])) {
891                         $selected = array(
892                                 'value' => $characters_has_profile[0]['value'],
893                         );
894                         if (isset($characters_has_profile[0]['public_flag'])) {
895                                 $selected['public_flag'] = $characters_has_profile[0]['public_flag'];
896                         }
897                         if (isset($characters_has_profile[0]['link_value'])) {
898                                 $selected['link_value'] = $characters_has_profile[0]['link_value'];
899                         }
900                 } else {
901                         $selected = array(
902                                 'value' => '',
903                                 'public_flag' => '',
904                                 'link_value' => '',
905                         );
906                 }
907
908                 return $selected;
909         }
910         /* profile_table */
911         function set_form_value_table($profile_selects, $characters_has_profile)
912         {
913         }
914
915         /* postからprofile_select_id->profile_idで抽出 */
916         function get_selected_profile($profile_select, $characters_has_profile, $isProfileDefault = false)
917         {
918                 $profile = array(
919                         'profile_select_id' => $profile_select['id'],
920                         'profile_id' => $profile_select['profile_id'],
921                 );
922
923                 if (!empty($isProfileDefault)) {
924                 } else {
925                         $profile['value'] = $profile_select['value'];
926                 }
927
928                 foreach ($characters_has_profile as $v) {
929                         if (Set::contains($v, $profile)) {
930                                 return $v;
931                         }
932                 }
933
934                 return array();
935
936         }
937
938         function isPublic($profile, $publicOnly = true) 
939         {
940                 if ($publicOnly && !CorePlus::check_public_flag($profile)) {
941                         return false;
942                 }
943
944                 return true;
945         }
946
947         function autoLinkProf($value)
948         {
949                 if (!$value) {
950                         return null;
951                 }
952
953                 // URL espaceの解除
954                 $patterns = array("/\&amp;/", "/\&#37;/", "/\&#43;/", "/\&#45;/");
955                 $replacements = array("&", "%", "+", "-");
956                 $value = preg_replace($patterns, $replacements, $value);
957
958                 return $this->Text->autoLinkUrls(
959                         $value,
960                         array(
961                                 'target' => '_blank',
962                                 'escape' => false,
963                         )
964                 );
965 ;
966         }
967
968         /* ProfileTableを横並びに表示(行単位) */
969         function dispTableBySideways($profile, $key_name = 'title', $option = array())
970         {
971                 $option = array_merge(array(
972                         'header' => null,
973                         'type' => 'text',
974                         'tabindex' => array(
975                                 'start' => 0,
976                                 'gap' => 0,
977                         ),
978                         'add_column' => array(),
979                         'form_options' => array(),
980                         'tr_options' => array(),
981                         'td_options' => array(),
982                 ), $option);
983
984                 if (!isset($profile['ProfileTable']) || empty($profile['ProfileTable'])) {
985                         if (isset($option['add_column']) && empty($option['add_column'])) {
986                                 return null;
987                         }
988                 }
989
990                 if (!empty($option['add_column'])) {
991                         $add_profile = array(
992                                 'id' => 0,
993                                 'profile_id' => $profile['Profile']['id'],
994                                 'title' => '',
995                                 'tablekey_name' => '',
996                                 'width' => '',
997                                 'sort_order' => '',
998                                 'ProfileTableStatic' => array(),
999                         );
1000                         if (isset($option['add_column']['data']) && isset($add_profile[$key_name])) {
1001                                 $add_profile[$key_name] = $option['add_column']['data'];
1002                         }
1003                         if (isset($option['add_column']['place']) && $option['add_column']['place'] == 'top') {
1004                                 array_unshift($profile['ProfileTable'], $add_profile);
1005                         } else {
1006                                 array_push($profile['ProfileTable'], $add_profile);
1007                         }
1008                 }
1009
1010                 $rows = count($profile['ProfileTable']);
1011                 if (!empty($option['header'])) {
1012                         $tds = $this->Html->tag('th', $option['header'], array('style' => 'width:65px;'));
1013                 } else {
1014                         $tds = null;
1015                 }
1016
1017                 for ($i=0; $i<$rows;$i++) {
1018                         if (!isset($profile['ProfileTable'][$i][$key_name])) {
1019                                 $profile['ProfileTable'][$i][$key_name] = null;
1020                         }
1021
1022                         if ($option['type'] == 'form') {
1023                                 $tabindex = $option['tabindex']['start'] + $option['tabindex']['gap'] * $i;
1024                                 $form_options = array_merge(
1025                                         array(
1026                                                 'type' => 'text',
1027                                                 'value' => $profile['ProfileTable'][$i][$key_name],
1028                                                 'label' => false,
1029                                                 'tabindex' => $tabindex,
1030                                         ),
1031                                         $option['form_options']
1032                                 );
1033
1034                                 $content = $this->Form->input(
1035                                         'ProfileTable.'.$profile['ProfileTable'][$i]['id'].'.'.$key_name,
1036                                         $form_options
1037                                 );
1038                                 $option['td_options'] = array_merge($option['td_options'], array('escape' => false));
1039                         } else {
1040                                 if (isset($option['add_column']['place']) &&
1041                                         (($option['add_column']['place'] == 'top' && $i == 0) || 
1042                                         ($option['add_column']['place'] != 'top' && $i == $rows - 1))) {
1043                                         $content = __('Add', true);
1044                                 } else {
1045                                         $content = $profile['ProfileTable'][$i][$key_name];
1046                                 }
1047                         }
1048
1049                         $tds .= $this->Html->tag('td', $content, $option['td_options']);
1050                 }
1051
1052                 return $this->Html->tag('tr', $tds, $option['tr_options']);
1053         }
1054
1055
1056         /* 一定の文字数で複数行に折り返すtruncate
1057          * マルチバイト対応
1058          * URLやタグはスキップしない
1059          * @param array $options is for truncate()
1060          */
1061         function multi_truncate($text, $length = 20, $rows = 3, $options = array())
1062         {
1063                 if (empty($text)) {
1064                         return $text;
1065                 }
1066
1067                 $rows = (int)$rows;
1068                 if (!($rows > 0)) {
1069                         $rows = 1;
1070                 }
1071
1072                 // 置換
1073                 $replace_pairs = array(
1074                         // 改行
1075                         '<br>' => "\n",
1076                         '<br />' => "\n",
1077                         // スペースの連続
1078                         "\s\s" => "\s",
1079                         ' ' => '',
1080                 );
1081                 $text = strtr($text, $replace_pairs);
1082
1083                 // Sanitize戻し
1084                 $Sanitize = CorePlus::set_behavoir('SanitizePlus');
1085                 $text = $Sanitize->restore_html($this->model(), $text);
1086
1087                 $text = trim($text);
1088
1089                 if (mb_strlen($text) < $length) {
1090                         return $text;
1091                 }
1092
1093                 // 複数行対応
1094                 $result = array();
1095                 $last_row = '';
1096                 for ($i = 1; $i <= $rows; $i++) {
1097                         if ($i > 1) {
1098                                 if (!$text) {
1099                                         break;
1100                                 }
1101
1102                                 // 前行の分を切り取り
1103                                 if (!empty($last_row)) {
1104                                         $text = substr_replace($text, '', 0, strlen($last_row));
1105                                 }
1106                         }
1107                         if ($i == $rows) {
1108                                 $text_len = mb_strlen($text);
1109
1110                                 if ($text_len < $length) {
1111                                         $result[$i] = $text;
1112                                 } else {
1113                                         $result[$i] = $this->Text->truncate($text, mb_strlen(mb_substr($text, 0, $length)), $options);
1114                                 }
1115
1116                                 break;
1117                         } else {
1118                                 $result[$i] = mb_substr($text, 0, $length);
1119                         }
1120
1121                         $last_row = $result[$i];
1122                         $result[$i] = strip_tags($result[$i]);
1123
1124
1125                         // タグをエスケープ
1126                         $result[$i] = $Sanitize->escapeTags($this->model(), $result[$i]);
1127                 }
1128
1129                 $text = implode("<br />\n", $result);
1130
1131                 return $text;
1132         }
1133
1134
1135 }
1136