Completed
Push — master ( 99ba08...e85760 )
by Carlos
05:29
created

AtributoValor::codeModelAll()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2015-2019 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace FacturaScripts\Core\Model;
20
21
/**
22
 * A Value for an article attribute.
23
 *
24
 * @author Carlos García Gómez <[email protected]>
25
 */
26
class AtributoValor extends Base\ModelClass
27
{
28
29
    use Base\ModelTrait;
30
31
    /**
32
     * Code of the related attribute.
33
     *
34
     * @var string
35
     */
36
    public $codatributo;
37
38
    /**
39
     * Attribute name + value.
40
     *
41
     * @var string
42
     */
43
    public $descripcion;
44
45
    /**
46
     * Primary key
47
     *
48
     * @var int
49
     */
50
    public $id;
51
52
    /**
53
     * Position for visualization and print
54
     *
55
     * @var int
56
     */
57
    public $orden;
58
59
    /**
60
     * Value of the attribute
61
     *
62
     * @var string
63
     */
64
    public $valor;
65
66
    /**
67
     * Reset the values of all model properties.
68
     */
69
    public function clear()
70
    {
71
        parent::clear();
72
        $this->orden = 100;
73
    }
74
75
    /**
76
     * 
77
     * @param string $fieldCode
78
     *
79
     * @return CodeModel[]
80
     */
81
    public function codeModelAll(string $fieldCode = '')
82
    {
83
        $results = [];
84
        $field = empty($fieldCode) ? static::primaryColumn() : $fieldCode;
85
86
        $sql = 'SELECT DISTINCT ' . $field . ' AS code, ' . $this->primaryDescriptionColumn() . ' AS description, codatributo, orden '
87
            . 'FROM ' . static::tableName() . ' ORDER BY codatributo ASC, orden ASC';
88
        foreach (self::$dataBase->selectLimit($sql, CodeModel::ALL_LIMIT) as $d) {
89
            $results[] = new CodeModel($d);
90
        }
91
92
        return $results;
93
    }
94
95
    /**
96
     * This function is called when creating the model table. Returns the SQL
97
     * that will be executed after the creation of the table. Useful to insert values
98
     * default.
99
     *
100
     * @return string
101
     */
102
    public function install()
103
    {
104
        /// needed dependency
105
        new Atributo();
106
107
        return parent::install();
108
    }
109
110
    /**
111
     * Returns the name of the column that is the model's primary key.
112
     *
113
     * @return string
114
     */
115
    public static function primaryColumn()
116
    {
117
        return 'id';
118
    }
119
120
    /**
121
     * Returns the name of the table that uses this model.
122
     *
123
     * @return string
124
     */
125
    public static function tableName()
126
    {
127
        return 'atributos_valores';
128
    }
129
130
    /**
131
     * Check the delivery note data, return True if it is correct.
132
     *
133
     * @return bool
134
     */
135
    public function test()
136
    {
137
        $this->valor = $this->toolBox()->utils()->noHtml($this->valor);
138
139
        /// combine attribute name + value
140
        $attribute = new Atributo();
141
        if ($attribute->loadFromCode($this->codatributo)) {
142
            $this->descripcion = $attribute->nombre . ' ' . $this->valor;
143
        }
144
145
        return parent::test();
146
    }
147
148
    /**
149
     *
150
     * @param string $type
151
     * @param string $list
152
     *
153
     * @return string
154
     */
155
    public function url(string $type = 'auto', string $list = 'ListAtributo')
156
    {
157
        $value = $this->codatributo;
158
        switch ($type) {
159
            case 'edit':
160
                return is_null($value) ? 'EditAtributo' : 'EditAtributo?code=' . $value;
0 ignored issues
show
introduced by
The condition is_null($value) is always false.
Loading history...
161
162
            case 'list':
163
                return $list;
164
165
            case 'new':
166
                return 'EditAtributo';
167
        }
168
169
        /// default
170
        return empty($value) ? $list : 'EditAtributo?code=' . $value;
171
    }
172
}
173