|
2 | 2 | use Cake\Core\Configure; |
3 | 3 | use Cake\I18n\I18n; |
4 | 4 | use Cake\Utility\Inflector; |
| 5 | +use Cake\Utility\Hash; |
5 | 6 |
|
6 | 7 | $languages = Configure::read('I18n.languages'); |
7 | | -$language = I18n::getLocale(); |
| 8 | +$primary = I18n::getLocale(); |
8 | 9 |
|
9 | | -$this->Html->scriptBlock('var languages = '. json_encode($languages).'; var language = "'.$language.'";', ['block' => 'script']); |
| 10 | +?> |
10 | 11 |
|
11 | | -$li = $fieldInputs = $originalFieldInputs = ''; |
12 | | - |
13 | | -foreach( $languages as $lng ) |
14 | | -{ |
15 | | - //active |
16 | | - $active = ( $lng == $language )? 'active': ''; |
17 | | - |
18 | | - //class |
19 | | - $class = 'locale-'.$lng; |
20 | | - |
21 | | - // tabs |
22 | | - $li .= $this->Html->tag( |
23 | | - 'li', |
24 | | - $this->Html->link($lng, '#locale-selector-ul', ['aria-controls' => '#locale-selector-ul']), |
25 | | - [ |
26 | | - 'role' => 'presentation', |
27 | | - 'class' => 'locale-selector-li locale-selector-li--'.$lng.' '.$active |
28 | | - ] |
29 | | - ); |
30 | | - |
31 | | - // fields |
32 | | - $i = 0; |
33 | | - foreach($fields as $key => $field) |
34 | | - { |
35 | | - |
36 | | - if(!is_array($field)) |
37 | | - { |
38 | | - |
39 | | - if(!empty($labels) && !empty($labels[$i])){ |
40 | | - $label = Inflector::humanize($labels[$i]).' ('.$lng.')'; |
41 | | - }else{ |
42 | | - $label = Inflector::humanize($field).' ('.$lng.')'; |
43 | | - } |
44 | | - if( $lng == I18n::getDefaultLocale() ) |
| 12 | +<locale :languages='<?= json_encode($languages) ?>' primary="<?= $primary ?>"> |
| 13 | + <?php foreach ($languages as $lng):?> |
| 14 | + <locale-tab lng="<?= $lng ?>" > |
| 15 | + <?php |
| 16 | + $i = 0; |
| 17 | + foreach($fields as $key => $field) |
45 | 18 | { |
46 | | - $fieldInputs .= $this->Form->control($field, ['class' => 'form-control '.$class, 'label' => $label]); |
47 | | - }else{ |
48 | | - $fieldInputs .= $this->Form->control('_translations.'.$lng.'.'.$field, ['class' => 'form-control '.$class, 'label' => $label,'required' => false]); |
49 | | - } |
50 | | - }else{ |
51 | | - if(!empty($labels) && !empty($labels[$i])){ |
52 | 19 | $label = Inflector::humanize($labels[$i]).' ('.$lng.')'; |
53 | | - }else{ |
54 | | - $label = Inflector::humanize($key).' ('.$lng.')'; |
55 | | - } |
56 | | - |
57 | | - $inputConf = ['class' => '', 'label' => $label]; |
58 | | - $inputConf['required'] = ($lng == I18n::getDefaultLocale())? true: false; |
59 | | - $fieldName = ($lng == I18n::getDefaultLocale())? $key: '_translations.'.$lng.'.'.$key; |
60 | | - if(!empty($field['Attachment.trumbowyg'])) |
61 | | - { |
62 | | - $inputConf = array_merge($inputConf, $field['Attachment.trumbowyg']); |
63 | | - $inputConf['class'] .= ' form-control '.$class; |
64 | | - if(!empty($field['Attachment.trumbowyg']['content'])){ |
65 | | - $inputConf['content'] = ($lng == I18n::getDefaultLocale())? $field['Attachment.trumbowyg']['content']->$fieldName : $field['Attachment.trumbowyg']['content']->_translations[$lng]->$key; |
66 | | - } |
67 | | - $fieldInputs .= $this->Attachment->trumbowyg($fieldName,$inputConf); |
68 | | - }elseif(!empty($field['Trois/Tinymce.tinymce'])) |
69 | | - { |
70 | | - $inputConf = array_merge($inputConf, $field['Trois/Tinymce.tinymce']); |
71 | | - $inputConf['init']['class'] = ' no-trumbowyg '.$class; |
72 | | - $inputConf['init']['label'] = $label; |
73 | | - $inputConf['init']['required'] = ($lng == I18n::getDefaultLocale() && !empty($field['Trois/Tinymce.tinymce']['init']['required']))? $field['Trois/Tinymce.tinymce']['init']['required'] : false; |
74 | | - $inputConf['field'] = $fieldName; |
| 20 | + $fieldName = $primary == $lng? '': '_translations.'.$lng.'.'; |
| 21 | + $fieldName .= is_numeric($key)? $field: $key; |
75 | 22 |
|
76 | | - if(!empty($field['Trois/Tinymce.tinymce']['value'])){ |
77 | | - $inputConf['value'] = ($lng == I18n::getDefaultLocale())? $field['Trois/Tinymce.tinymce']['value']->$fieldName : $field['Trois/Tinymce.tinymce']['value']->_translations[$lng]->$key; |
| 23 | + // value |
| 24 | + if(is_array($field) && !array_key_exists('value', $field) && isset($entity) && !$entity->isNew()) |
| 25 | + { |
| 26 | + $field['value'] = Hash::get($entity->toArray(), $fieldName); |
78 | 27 | } |
79 | 28 |
|
80 | 29 |
|
81 | | - $fieldInputs .= $this->element('Trois/Tinymce.tinymce', [ |
82 | | - 'field' => $inputConf['field'], |
83 | | - 'value' => $inputConf['value'], |
84 | | - 'init' => [ // optional |
85 | | - 'class' => $inputConf['init']['class'], |
86 | | - 'label' => $inputConf['init']['label'], |
87 | | - 'required' => $inputConf['init']['required'], |
88 | | - 'external_plugins' => (!empty($inputConf['init']['external_plugins']))? $inputConf['init']['external_plugins'] : null, |
89 | | - 'attachment_settings' => (!empty($inputConf['init']['attachment_settings']))? $this->Attachment->setup($fieldName, $inputConf['init']['attachment_settings']) : null |
90 | | - ] |
91 | | - ]); |
92 | | - }else |
| 30 | + if(is_numeric($key)) echo $this->Form->control($fieldName, ['class' => 'form-control', 'label' => $label]); |
| 31 | + else |
93 | 32 | { |
94 | | - $inputConf = array_merge($inputConf, $field); |
95 | | - $inputConf['class'] .= ' form-control '.$class; |
96 | | - $fieldInputs .= $this->Form->control($fieldName, $inputConf); |
| 33 | + $basicSettings = ['label' => $label,'lng' => $lng,'field' => $fieldName, 'init' => ['label' => $label]]; |
| 34 | + if(!empty($field['init']) && array_key_exists('attachment_settings', $field['init'])) |
| 35 | + { |
| 36 | + $basicSettings = array_merge($basicSettings, [ |
| 37 | + 'init' => [ |
| 38 | + 'label' => $label, |
| 39 | + 'attachment_settings' => $this->Attachment->setup($fieldName, $field['init']['attachment_settings']) |
| 40 | + ] |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + if(!empty($field['element'])) echo $this->element($field['element'], array_merge($field, $basicSettings)); |
| 45 | + else echo $this->Form->control($fieldName, array_merge($field, $basicSettings)); |
97 | 46 | } |
98 | | - } |
99 | | - $i++; |
100 | | - } |
101 | | - } |
102 | 47 |
|
103 | | - ?> |
104 | | - <div class="locale-area"> |
105 | | - <ul id="locale-selector-ul" class="nav nav-tabs" role="tablist"> |
106 | | - <?= $li; ?> |
107 | | - </ul> |
108 | | - <div class="tab-locale"> |
109 | | - <?= $fieldInputs ?> |
110 | | - <?= $originalFieldInputs ?> |
111 | | - </div> |
112 | | - </div> |
| 48 | + $i++; |
| 49 | + } |
| 50 | + ?> |
| 51 | + </locale-tab> |
| 52 | +<?php endforeach; ?> |
| 53 | +</locale> |
0 commit comments