OSDN Git Service

ProfileTables追加/表示実装
[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                 'Form',
10         );
11
12         var $i18n_profile_types = array();
13
14         var $profile_type = array();
15
16         var $character_has_profile_num = 0;
17
18         var $form_id = 0;
19
20         /**
21          * profile_typeの設定をview表示形式に変換
22          */
23         function get_i18n_profile_type($profile_type, $profile_types)
24         {
25                 if (empty($profile_type)) {
26                         return null;
27                 }
28
29                 if (empty($this->i18n_profile_types)) {
30                         $this->i18n_profile_types = $this->set_profile_type($profile_types);
31                 }
32
33                 return $this->i18n_profile_types[$profile_type];
34         }
35         /**
36          * profile_typeの選択プルダウン出力
37          * i18n対応
38          */
39         function create_profile_type_select($profile_types = array(), $fieldName = 'profile_type', $options = array('type' => 'select'))
40         {
41                 if (!isset($options['type'])) {
42                         $options['type'] = 'select';
43                 }
44                 if (!isset($options['options'])) {
45                         $options['options'] = array();
46                 }
47                 $options['options'] = $this->set_profile_type($profile_types, $options['options']);
48
49                 return $this->Form->input($fieldName, $options);
50         }
51         /* profile_typeの設定を、key->value(多言語化対応)に変換 */
52         function set_profile_type($profile_types = array(), $options = array())
53         {
54                 if (!empty($options)) {
55                         $this->i18n_profile_types = $options;
56                 }
57
58                 if (empty($profile_types)) {
59                         App::import('Model', 'Profile');
60                         $Profile = new ProfileModel;
61                         $profile_types = $Profile->profile_type;
62                 }
63
64                 foreach ($profile_types as $k => $v) {
65                         if ($v == 'input') {
66                                 $this->i18n_profile_types[$v] = __('input', true);
67                         } elseif($v == 'textarea') {
68                                 $this->i18n_profile_types[$v] = __('textarea', true);
69                         } elseif($v == 'select') {
70                                 $this->i18n_profile_types[$v] = __('select', true);
71                         } elseif($v == 'radio') {
72                                 $this->i18n_profile_types[$v] = __('radio', true);
73                         } elseif($v == 'checkbox') {
74                                 $this->i18n_profile_types[$v] = __('checkbox', true);
75                         } elseif($v == 'm-input') {
76                                 $this->i18n_profile_types[$v] = __('multi input', true);
77                         } elseif($v == 'm-select') {
78                                 $this->i18n_profile_types[$v] = __('multi select', true);
79                         } elseif($v == 'm-radio') {
80                                 $this->i18n_profile_types[$v] = __('multi radio', true);
81                         } elseif($v == 'table') {
82                                 $this->i18n_profile_types[$v] = __('table', true);
83                         } else {
84                                 $this->i18n_profile_types[$v] = $v;
85                         }
86                 }
87
88                 return $this->i18n_profile_types;
89         }
90
91         /* profileの設定をフォームで出力 */
92         function disp_form4profile($profile, $label = false, $option = array())
93         {
94                 if ($profile['profile_type'] == 'textarea') {
95                         $option['type'] = 'textarea';
96                 } else {
97                         $option['type'] = 'text';
98                 }
99
100                 if (!CorePlus::is_valid($profile, 'CharactersHasProfile.0.value')) {
101                         $profile['CharactersHasProfile'][0]['value'] = null;
102                 }
103
104                 foreach ($profile['CharactersHasProfile'] as $v) {
105                         $option = array_merge(array(
106                                 'label' => $label,
107                                 'value' => $v['value'],
108                                 ), $option
109                         );
110
111                         $profile_tag = 
112                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', $option).
113                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_id', array(
114                                         'type' => 'hidden',
115                                         'value' => $profile['id'],
116                                 ));
117
118                         echo $profile_tag;
119                         $this->character_has_profile_num++;
120                         if (!$this->profile_type['is_multi']) {
121                                 break;
122                         }
123                 }
124         }
125
126         /* profile_selectsの設定を出力 */
127         function disp4profile_selects($profile_selects, $option = array())
128         {
129                 foreach ($profile_selects as $k => $v) {
130                         $options[stripcslashes($v['value'])] = stripcslashes($v['value']);
131                 }
132                 $option = array_merge(
133                         array(
134                         'type' => 'select',
135                         'options' => $options,
136                         ),
137                         $option
138                 );
139
140                 $profile_select_tag = 
141                          $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', $option).
142                          $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_id', array(
143                                 'type' => 'hidden',
144                                 'value' => $profile_selects[0]['profile_id'],
145                         )).
146                          $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_select_id', array(
147                                 'type' => 'hidden',
148                                 'value' => $profile_selects[0]['id'],
149                         ));
150                 echo $profile_select_tag;
151                 $this->character_has_profile_num++;
152         }
153
154
155         /* profile_selectsの設定をプルダウンで出力 */
156         function disp_select4profile_selects($profile_selects, $characters_has_profile = array(), $option = array(), $label = false, $legend = false)
157         {
158                 if (empty($profile_selects)) {
159                         return null;
160                 }
161
162                 $selected = $this->set_form_value($profile_selects, $characters_has_profile);
163
164                 $option = array_merge(
165                         array(
166                                 'type' => 'select',
167                                 'label' => $label,
168                                 'legend' => $legend,
169                                 'selected' => $selected['value'],
170                         ),
171                         $option
172                 );
173
174                 $this->disp4profile_selects($profile_selects, $option);
175         }
176
177         /* profile_selectsの設定をラジオボタンで出力 */
178         function disp_radio4profile_selects($profile_selects, $characters_has_profile = array(), $label = false, $legend = false)
179         {
180                 if (empty($profile_selects)) {
181                         return null;
182                 }
183
184                 $selected = $this->set_form_value($profile_selects, $characters_has_profile);
185                 if ($selected['value']) {
186                         $selected_value = $selected['value'];
187                 } else {
188                         $first = reset($profile_selects);
189                         $selected_value = $first['value'];
190                 }
191
192                 $option = array(
193                         'type' => 'radio',
194                         'value' => $selected_value,
195                         'label' => $label,
196                         'legend' => $legend,
197                         'div' => array(
198                                 'class' => 'input radio inline'
199                         ),
200                 );
201
202                 return $this->disp4profile_selects($profile_selects, $option);
203
204         }
205
206         /* profile_selectsの設定をチェックボックスで出力 */
207         function disp_checkbox4profile_selects($profile_selects, $characters_has_profile = array(), $label = false, $legend = false)
208         {
209                 if (empty($profile_selects)) {
210                         return null;
211                 }
212
213                 foreach($profile_selects as $k => $v) {
214                         if (empty($v)) {
215                                 continue;
216                         }
217
218                         $profile_select[0] = $v;
219                         $has_profile[0] = $this->get_selected_profile($v, $characters_has_profile);
220                         $this->disp_select4profile_selects(
221                                 $profile_select, 
222                                 $has_profile,
223                                 array(
224                                         'multiple' => 'checkbox',
225                                         'label' => $label,
226                                         'legend' => $legend,
227                                         'div' => array(
228                                                 'class' => 'input checkbox inline'
229                                         ),
230                                 )
231                         );
232                 }
233         }
234
235         /* profile_tablesの設定からtableHeadersを出力 */
236         function disp_th4profile_tables($profile_tables, $tableId = null, $tr_options = array(), $th_options = array())
237         {
238                 if (empty($profile_tables)) {
239                         return null;
240                 }
241
242                 $values = array();
243                 $style_options = array();
244                 $i = 1;
245                 foreach ($profile_tables as $k => $v) {
246                         $values[$k] = $v['title'];
247
248                         if ($v['width']) {
249                                 $key = '#'.$tableId.' td.column-' . $i;
250                                 $style_options[$key] = array('width' => $v['width']."px");
251                                 $key = '#'.$tableId.' td.column-' . $i. ' input';
252                                 $input_width = $v['width']-8;
253                                 $style_options[$key] = array('width' => $input_width."px");
254                         }
255                         $i++;
256                 }
257
258                 if (!empty($style_options)) {
259                         echo '<style type="text/css"><!--';
260                         foreach($style_options as $k => $v) {
261                                 echo $k. " {\n";
262                                 echo $this->Html->style($v);
263                                 echo "}\n";
264                         }
265                         echo '--></style>';
266                 }
267
268                 $table_header = $this->Html->tableHeaders(
269                         $values,
270                         $tr_options,
271                         $th_options
272                 );
273
274                 $table_header = '<thead>'.$table_header.'</thead>';
275
276                 echo $table_header;
277         }
278
279         /* characters_has_profilesの設定からtableCellsを出力 */
280         function disp_td4characters_has_profiles($characters_has_profiles, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
281         {
282                 if (empty($characters_has_profiles)) {
283                         return null;
284                 }
285
286                 $values = array();
287                 foreach ($characters_has_profiles as $k => $v) {
288                         if (is_string($v)) {
289                                 $values[$k] = $v;
290                         } else {
291                                 $values[$k] = $v['value'];
292                         }
293                 }
294
295                 $table_cells = $this->Html->tableCells($values, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
296
297                 echo $table_cells;
298         }
299
300         /* characters_has_profileからtable td出力 */
301         function disp_tds4characters_has_profiles($characters_has_profile, $type = 'view', $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
302         {
303                 $i = 1;
304                 $profile_num = 0;
305                 $profiles[$i] = array();
306                 $profile_id = null;
307                 foreach($characters_has_profile as $k => $v) {
308                         if (is_null($profile_id) && CorePlus::is_valid($v, 'profile_id')) {
309                                 $profile_id = $v['profile_id'];
310                         }
311
312                         if (!CorePlus::is_valid($v, 'profile_id')) {
313                                 if (!is_null($profile_id) && !CorePlus::is_valid($v, 'profile_id')) {
314                                         $v['profile_id'] = $profile_id;
315                                 } else {
316                                         __('Disable');
317                                         continue;
318                                 }
319                         }
320
321                         if (!CorePlus::is_valid($v, 'profile_table_id')) {
322                                 if (!isset($ProfileTable)) {
323                                         App::import('Model', 'ProfileTable');
324                                         $ProfileTable = new ProfileTable;
325                                 }
326                                 $prof_table_ids = $ProfileTable->find('list', array(
327                                         'ProfileTable.profile_id' => $profile_id,
328                                         'fields' => array('ProfileTable.id'),
329                                         'order' => 'ProfileTable.sort_order'
330                                 ));
331
332                                 if (!empty($prof_table_ids)) {
333                                         $v['profile_table_id'] = current(array_slice($prof_table_ids, $profile_num+1, 1));
334                                         $v['value'] = null;
335                                 } else {
336                                         __('Disable');
337                                         continue;
338                                 }
339                         }
340
341                         $v['id'] = $v['profile_table_id'];
342
343                         if (isset($profiles[$i][$v['profile_table_id']])) {
344                                 $i++;
345                         }
346                         $profiles[$i][$v['profile_table_id']] = $v;
347
348                         $profile_num++;
349                 }
350                 $forms = array();
351
352                 foreach($profiles as $k => $v) {
353                         if ($type == 'form') {
354                                 $tds = $this->disp_form4profile_tables($v, null, null, true);
355                         } else {
356                                 $tds = $this->disp_value4profile_tables($v, null, null, true);
357                         }
358                 }
359
360                 $this->disp_td4characters_has_profiles($tds, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
361         }
362
363
364         /* characters_has_profileからvalue出力 */
365         function disp_value_td4characters_has_profiles($characters_has_profile)
366         {
367                 $this->disp_tds4characters_has_profiles($characters_has_profile, 'view', null, null, true);
368         }
369
370         /* characters_has_profileからtdフォーム出力 */
371         function disp_form_td4characters_has_profiles($characters_has_profile)
372         {
373                 $this->disp_tds4characters_has_profiles($characters_has_profile, 'form', null, null, true);
374         }
375
376         /* profile_tablesの設定からView表示を出力 */
377         function disp_value4profile_tables($profile_tables, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
378         {
379                 if (empty($profile_tables)) {
380                         return null;
381                 }
382
383                 $tds = array();
384                 foreach ($profile_tables as $k => $v) {
385                         if (!Set::check($v, 'value')) {
386                                 $v['value'] = null;
387                         }
388                         $tds[] = $v['value'];
389                 }
390                 $this->disp_td4characters_has_profiles($tds, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
391         }
392
393         /* profile_tablesの設定から入力フォームを出力 */
394         function disp_form4profile_tables($profile_tables, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
395         {
396                 if (empty($profile_tables)) {
397                         return null;
398                 }
399
400                 $forms = array();
401
402                 foreach ($profile_tables as $k => $v) {
403                         if (!Set::check($v, 'profile_id')) {
404                                 continue;
405                         }
406                         if (!Set::check($v, 'id')) {
407                                 if (Set::check($v, 'profile_table_id')) {
408                                         $v['id'] = $v['profile_table_id'];
409                                 } else {
410                                         continue;
411                                 }
412                         }
413
414                         if (!Set::check($v, 'value')) {
415                                 $v['value'] = null;
416                         }
417
418                         $forms[] = 
419                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.value', array(
420                                         'type' => 'text',
421                                         'label' => false,
422                                         'value' => $v['value'],
423                                 )).
424                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_id', array(
425                                         'type' => 'hidden',
426                                         'value' => $v['profile_id'],
427                                 )).
428                                 $this->Form->input('CharactersHasProfile.'.$this->character_has_profile_num.'.profile_table_id', array(
429                                         'type' => 'hidden',
430                                         'value' => $v['id'],
431                                 ));
432                         $this->character_has_profile_num++;
433                 }
434
435                 $this->disp_td4characters_has_profiles($forms, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
436         }
437
438         /* profile_tablesの設定からブランクのtableCellsを出力 */
439         function disp_blank_td4profile_tables($profile_tables, $num = 2, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true)
440         {
441                 if (empty($profile_tables)) {
442                         return null;
443                 }
444
445                 $blank = array_fill(0, count($profile_tables), $this->Html->div('blank', " "));
446
447                 for ($i=0; $i<$num; $i++) {
448                         $this->disp_td4characters_has_profiles($blank, $oddTrOptions, $evenTrOptions, $useCount, $continueOddEven);
449                 }
450         }
451
452         /* Profile種類を判断 */
453         function set_profile_type4view($profile)
454         {
455                 $this->profile_type = array(
456                         'is_subsetting' => false,
457                         'is_select' => false,
458                         'is_radio' => false,
459                         'is_checkbox' => false,
460                         'is_table' => false,
461                         'is_multi' => false,
462                 );
463
464                 if ($profile['profile_type'] == 'select' || $profile['profile_type'] == 'm-select' || $profile['profile_type'] == 'radio' || $profile['profile_type'] == 'm-radio' || $profile['profile_type'] == 'checkbox'  || $profile['profile_type'] == 'table' ) {
465                         $this->profile_type['is_subsetting'] = true;
466                 }
467                 if ($profile['profile_type'] == 'select' || $profile['profile_type'] == 'm-select') {
468                         $this->profile_type['is_select'] = true;
469                 }
470                 if ($profile['profile_type'] == 'radio' || $profile['profile_type'] == 'm-radio') {
471                         $this->profile_type['is_radio'] = true;
472                 }
473                 if ($profile['profile_type'] == 'checkbox') {
474                         $this->profile_type['is_checkbox'] = true;
475                 }
476                 if ($profile['profile_type'] == 'table') {
477                         $this->profile_type['is_table'] = true;
478                 }
479                 if ($profile['profile_type'] == 'm-select' || $profile['profile_type'] == 'm-input') {
480                         $this->profile_type['is_multi'] = true;
481                 }
482         }
483
484         /* profile_typeの設定に応じてフォーム表示
485          * set_profile_type4view($profile)チェック済み前提。
486          */
487         function disp_forms4profile_type($profile, $form_num = 2, $table_blank = 2)
488         {
489                 if (!isset($profile['CharactersHasProfile'])) {
490                         $profile['CharactersHasProfile'] = array();
491                 }
492
493                 if ($this->profile_type['is_select'] || $this->profile_type['is_radio'] || $this->profile_type['is_checkbox']) {
494                         if ($this->profile_type['is_select']) {
495                                 $this->disp_select4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile']);
496                         } elseif ($this->profile_type['is_radio']) {
497                                 $this->disp_radio4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile']);
498                         } elseif ($this->profile_type['is_checkbox']) {
499                                 $this->disp_checkbox4profile_selects($profile['ProfileSelect'], $profile['CharactersHasProfile']);
500                         }
501                 } elseif ($this->profile_type['is_table']) {
502                         if (isset($profile['id'])) {
503                                 $tableId = 'TableProfileId'. $profile['id'];
504                         } else {
505                                 $tableId = 'TableProfileId'. $profile['Profile']['id'];
506                         }
507                         echo '<table id="'.$tableId.'">';
508                         $this->disp_th4profile_tables($profile['ProfileTable'], $tableId);
509                         echo '<tbody>';
510                         echo "<BR><BR>";
511                         if (!empty($profile['CharactersHasProfile'])) {
512                                 $this->disp_form_td4characters_has_profiles($profile['CharactersHasProfile']);
513                         }
514
515                         if (empty($profile['CharacterHasProfile'])) {
516                                 if (intval($form_num) > 0) {
517                                         for ($i=0; $i<$form_num; $i++) {
518                                                 $this->disp_form4profile_tables($profile['ProfileTable'], null, null, true);
519                                         }
520                                 }
521                                 if (intval($table_blank) > 0) {
522                                         $this->disp_blank_td4profile_tables($profile['ProfileTable'], intval($table_blank));
523                                 }
524                         }
525                         echo '</tbody>';
526                         echo '</table>';
527                 } else {
528                         $this->disp_form4profile($profile);
529                 }
530         }
531
532         /* characters_has_profilesの設定に応じてView表示
533          * set_profile_type4view($profile)チェック済み前提。
534          * $profileのkeyはprofilesのkey_name
535          */
536         function disp_values4characters_has_profiles($profile, $table_blank = 2, $disp_add_link = false, $character_id = null)
537         {
538                 if (!isset($profile['CharactersHasProfile']) && !$this->profile_type['is_table']) {
539                         $profile['CharactersHasProfile'] = array();
540                 }
541
542                 if (!$this->profile_type['is_table']) {
543                         $num = 0;
544                         foreach ($profile['CharactersHasProfile'] as $v) {
545                                 if ($num > 0) {
546                                         echo ",&nbsp;";
547                                 }
548                                 if (!empty($v['value'])) {
549                                         echo $v['value'];
550                                         $num++;
551                                 }
552                         }
553                 } else {
554                         $tableId = 'TableProfileId'. $profile['id'];
555                         echo '<table id="'.$tableId.'">';
556                         $this->disp_th4profile_tables($profile['ProfileTable'], $tableId);
557                         echo '<tbody>';
558                         if (!empty($profile['CharactersHasProfile'])) {
559                                 $this->disp_value_td4characters_has_profiles($profile['CharactersHasProfile']);
560                         } else {
561                                 if (intval($table_blank) > 0) {
562                                         $this->disp_blank_td4profile_tables($profile['ProfileTable'], intval($table_blank));
563                                 }
564                         }
565                         echo '</tbody>';
566                         echo '</table>';
567                         if ($disp_add_link) {
568                                 echo $this->Html->div(
569                                         'editLink', 
570                                         $this->Html->link(
571                                                 __('Add Data', true),
572                                                 array(
573                                                         'action' => 'add_milti_profiles',
574                                                         $character_id.
575                                                         '/profile_id:'.$profile['id'],
576                                                 )
577                                         )
578                                 );
579                         }
580                 }
581         }
582
583         /* フォームに値入力 */
584         function set_form_value($profile_selects, $characters_has_profile)
585         {
586                 $this->form_id++;
587
588                 if ($this->profile_type['is_select'] || $this->profile_type['is_radio'] || $this->profile_type['is_checkbox']) {
589                         return $this->set_form_value_select($profile_selects, $characters_has_profile);
590                 } elseif ($this->profile_type['is_table']) {
591                         return $this->set_form_value_table($characters_has_profile);
592                 } else {
593                         return $this->set_form_value_profile($profile_selects, $characters_has_profile);
594                 }
595
596         }
597
598         /* profile_select */
599         /* profile_select_id->profile_idで抽出 */
600         function set_form_value_select($profile_selects, $characters_has_profile)
601         {
602                 if (isset($characters_has_profile[0]['id'])) {
603                         $selected = array(
604                                 'value' => $characters_has_profile[0]['value'],
605                                 'public_flag' => $characters_has_profile[0]['public_flag'],
606                                 'link_value' => $characters_has_profile[0]['link_value'],
607                         );
608                 } else {
609                         $selected = array(
610                                 'value' => '',
611                                 'public_flag' => '',
612                                 'link_value' => '',
613                         );
614                 }
615
616                 return $selected;
617         }
618         /* profile_table */
619         function set_form_value_table($profile_selects, $characters_has_profile)
620         {
621         }
622
623         /* postからprofile_select_id->profile_idで抽出 */
624         function get_selected_profile($profile_select, $characters_has_profile)
625         {
626                 $profile = array(
627                         'profile_select_id' => $profile_select['id'],
628                         'profile_id' => $profile_select['profile_id'],
629                         'value' => $profile_select['value'],
630                 );
631
632                 foreach ($characters_has_profile as $v) {
633                         if (Set::contains($v, $profile)) {
634                                 return $v;
635                         }
636                 }
637
638                 return array();
639
640         }
641
642 }
643