model.php 9.45 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
5
6
7
8
9
 * OPEN 2.0
 *
 * @see https://www.open2.0.regione.lombardia.it Developers'community
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
Marcello Pivanti's avatar
Marcello Pivanti committed
10
11
 *
 *
waltermazza's avatar
waltermazza committed
12
 * @package    arter\amos\translation
Marcello Pivanti's avatar
Marcello Pivanti committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

/**
 * This is the template for generating the model class of a specified table.
 * DO NOT EDIT THIS FILE! It may be regenerated with Gii.
 *
 * @var yii\web\View $this
 * @var yii\gii\generators\model\Generator $generator
 * @var string $tableName full table name
 * @var string $className class name
 * @var yii\db\TableSchema $tableSchema
 * @var string[] $labels list of attribute labels (name => label)
 * @var string[] $rules list of validation rules
 * @var array $relations list of relations (name => relation declaration)
 */
use yii\helpers\Inflector;

echo "<?php\n";
?>

namespace <?= $generator->ns ?>;

use Yii;
use raoul2000\workflow\base\SimpleWorkflowBehavior;
use yii\helpers\ArrayHelper;

/**
* This is the base-model class for table "<?= $tableName ?>".
*
<?php foreach ($tableSchema->columns as $column): ?>
    * @property <?= "{$column->phpType} \${$column->name}\n" ?>
<?php endforeach; ?>
<?php if (!empty($relations)): ?>
    *
    <?php foreach ($relations as $name => $relation): ?>
        * @property \<?= $ns ?>\<?= $relation[1] . ($relation[2] ? '[]' : '') . ' $' . lcfirst($name) . "\n" ?>
    <?php endforeach; ?>
<?php endif; ?>
*/
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
{

<?php
if(!empty($generator->workflow)){
    ?>
const TR_WORKFLOW = '<?= $generator->workflow ?>';
<?php
}
$arrayAttributi = [];
$indx = [];
$elencoAttributi = [];
foreach ((array) $otherRelations as $Relation):
    if ($Relation['fromEntity'] == $tableName):
        $attributoEntita = $Relation['toEntity'];
        if (!(in_array($attributoEntita, $arrayAttributi))):
            $arrayAttributi[] = $attributoEntita;
            $indx[$attributoEntita] = 0;
            $elencoAttributi[$attributoEntita]['required'] = ($Relation['required']) ? 1 : 0;
            $elencoAttributi[$attributoEntita]['descriptorField'] = addslashes($Relation['descriptorField']);
            ?>
            /**
            * @var array Attributo di relazione
            */
            public $attr<?= Inflector::id2camel($attributoEntita, '_') ?>Mm;    
            <?php
        else:
            $indx[$attributoEntita] = $indx[$attributoEntita] + 1;
            $newIndx = $indx[$attributoEntita] - 1;
            $newAttributoEntita = $attributoEntita . $newIndx;
            $elencoAttributi[$newAttributoEntita]['required'] = ($Relation['required']) ? 1 : 0;
            $elencoAttributi[$newAttributoEntita]['descriptorField'] = addslashes($Relation['descriptorField']);
            ?>
            /**
            * @var array Attributo di relazione
            */
            public $attr<?= Inflector::id2camel($newAttributoEntita, '_') ?>Mm;
        <?php
        endif;
    endif;
endforeach;
?>

    public $language_source;

<?php
if(!empty($generator->workflow)){
?>
/**
* @inheritdoc
*/
public function behaviors() {
        return ArrayHelper::merge(parent::behaviors(), [
                    [
                        'class' => SimpleWorkflowBehavior::className(),
                        'defaultWorkflowId' => self::TR_WORKFLOW,
                        'propagateErrorsToModel' => true,
                    ],
        ]);
}
<?php
}
?>

/**
* @inheritdoc
*/
public static function tableName()
{
return '<?= $tableName ?>';
}

/**
* @inheritdoc
*/
public function rules()
{
return [<?= "\n            " . implode(",\n            ", $rules) ?>,
 ['language_source', 'safe'],
<?php foreach ((array) $elencoAttributi as $key => $value):
    ?>   
    [['attr<?= Inflector::id2camel($key, '_') ?>Mm'], 'safe'],
    <?php if ($value['required'] == 1): ?>
        [['attr<?= Inflector::id2camel($key, '_') ?>Mm'], 'required'],
        <?php
    endif;
endforeach;
?>
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'language_source' => \Yii::t('amostranslation', 'Select another source language'),
<?php foreach ($labels as $name => $label): ?>
    <?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
<?php endforeach; ?>
<?php foreach ((array) $elencoAttributi as $key => $value):
    ?>   
    '<?= lcfirst(Inflector::id2camel($key, '_')) ?>' => \Yii::t('amostranslation', '<?= ucfirst(addslashes($value['descriptorField'])) ?>'),<?= "\n" ?>
    'attr<?= Inflector::id2camel($key, '_') ?>Mm' => \Yii::t('amostranslation', '<?= ucfirst(addslashes($value['descriptorField'])) ?>'),<?= "\n" ?>
    <?php
endforeach;
?>
];
}
<?php foreach ($relations as $name => $relation): ?>

    /**
    * @return \yii\db\ActiveQuery
    */
    public function get<?= $name ?>()
    {
    <?= $relation[0] . "\n" ?>
    }
<?php endforeach; ?>
<?php
$entita = [];
$indice = [];
foreach ((array) $otherRelations as $Relation):
    if ($Relation['fromEntity'] == $tableName):
        $tabella = $Relation['fromEntity'] . '_' . $Relation['toEntity'] . '_mm';
        if (!(in_array($tabella, $entita) || in_array($Relation['toEntity'] . '_' . $Relation['fromEntity'] . '_mm', $entita))):
            $entita[] = $tabella;
            $indice[$tabella] = 0;
            ?>
            /**
            * @return \yii\db\ActiveQuery
            */
            public function get<?= Inflector::id2camel($Relation['toEntity'], '_') ?>()
            {
            return $this-><?= ($Relation['type'] == 'mtm') ? 'hasMany' : 'hasOne' ?>(\backend\modules\<?= $Relation['toModule'] ?>\models\<?= Inflector::id2camel($Relation['toEntity'], '_') ?>::className(), ['id' => '<?= $Relation['toEntity'] ?>_id'])->via('<?= lcfirst(Inflector::id2camel($Relation['fromEntity'] . '_' . $Relation['toEntity'] . '_mm', '_')) ?>s');
            }
            <?php if ($Relation['type'] == 'mtm') { ?>
                public function getAttr<?= Inflector::id2camel($Relation['toEntity'], '_') ?>Mm($separator = ' '){
                $ritorno = "";
                $ind = 0;
                foreach((array) $this-><?= lcfirst(Inflector::id2camel($Relation['toEntity'], '_')) ?> as $str){
                <?php foreach ($Relation['toFields'] as $field) : ?>
                    $ritorno .= (($ind == 0)? '': ', ') . $str['<?= $field['toFieldName'] ?>'];
                <?php endforeach; ?>
                $ind++;
                }
                return $ritorno;
                }
            <?php } else { ?>
                public function getAttr<?= Inflector::id2camel($Relation['toEntity'], '_') ?>Mm(){
                $ritorno = "";                           
                <?php $ind = 0;
                foreach ($Relation['toFields'] as $field) :
                    ?>
                    $ritorno .= '<?= (($ind == 0) ? '' : ' ') ?>' . $this-><?= lcfirst(Inflector::id2camel($Relation['toEntity'], '_')) ?>-><?= $field['toFieldName'] ?>;
                    <?php $ind++;
                endforeach;
                ?>                          
                return $ritorno;
                }
            <?php } ?> 
            <?php
        else:
            $indice[$tabella] = $indice[$tabella] + 1;
            $newIndice = $indice[$tabella] - 1;
            $newTabella = $tabella . $newIndice;
            ?>    
            /**
            * @return \yii\db\ActiveQuery
            */
            public function get<?= Inflector::id2camel($Relation['toEntity'], '_') . $newIndice ?>()
            {
            return $this-><?= ($Relation['type'] == 'mtm') ? 'hasMany' : 'hasOne' ?>(\backend\modules\<?= $Relation['toModule'] ?>\models\<?= Inflector::id2camel($Relation['toEntity'], '_') ?>::className(), ['id' => '<?= $Relation['toEntity'] ?>_id'])->via('<?= lcfirst(Inflector::id2camel($newTabella, '_')) ?>s');
            }
            <?php if ($Relation['type'] == 'mtm') { ?>
                public function getAttr<?= Inflector::id2camel($Relation['toEntity'], '_') . $newIndice ?>Mm($separator = ' '){
                $ritorno = "";
                $ind = 0;
                foreach((array) $this-><?= lcfirst(Inflector::id2camel($Relation['toEntity'], '_')) . $newIndice ?> as $str){
                <?php foreach ($Relation['toFields'] as $field) : ?>
                    $ritorno .= (($ind == 0)? '':', ') . $str['<?= $field['toFieldName'] ?>'];
                <?php endforeach; ?>
                $ind++;
                }
                return $ritorno;
                }
            <?php } else { ?>   
                public function getAttr<?= Inflector::id2camel($Relation['toEntity'], '_') . $newIndice ?>Mm(){
                $ritorno = "";                     
                <?php $ind = 0;
                foreach ($Relation['toFields'] as $field) :
                    ?>
                    $ritorno .= '<?= (($ind == 0) ? '' : ' ') ?>' . $this-><?= lcfirst(Inflector::id2camel($Relation['toEntity'], '_')) . $newIndice ?>-><?= $field['toFieldName'] ?>;
                    <?php $ind++;
                endforeach;
                ?>
                $ind++;            
                return $ritorno;
                }
            <?php } ?>
        <?php endif; ?>    

        <?php
    endif;
endforeach;
?>
}