1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2016 Marcos García <[email protected]> |
4
|
|
|
* Copyright (C) 2022 Open-Dsi <[email protected]> |
5
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Dolibarr\Code\Variants\Classes; |
22
|
|
|
|
23
|
|
|
use DoliDB; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* \file htdocs/variants/class/ProductCombination2ValuePair.class.php |
27
|
|
|
* \ingroup variants |
28
|
|
|
* \brief File of the ProductCombination2ValuePair class |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class ProductCombination2ValuePair |
33
|
|
|
* Used to represent the relation between a variant and its attributes. |
34
|
|
|
* |
35
|
|
|
* Example: a product "shirt" has a variant "shirt XL white" linked to the attributes "size: XL" and "color: white". |
36
|
|
|
* This is represented with two ProductCombination2ValuePair objects: |
37
|
|
|
* - One for "size: XL": |
38
|
|
|
* * $object->fk_prod_combination ID of the ProductCombination object between product "shirt" and its variant "shirt XL white" |
39
|
|
|
* * $object->fk_prod_attr ID of the ProductAttribute object "size" |
40
|
|
|
* * $object->fk_prod_attr_val ID of the ProductAttributeValue object "XL" |
41
|
|
|
* - Another for "color: white": |
42
|
|
|
* * $object->fk_prod_combination ID of the ProductCombination object between product "shirt" and its variant "shirt XL white" |
43
|
|
|
* * $object->fk_prod_attr ID of the ProductAttribute object "color" |
44
|
|
|
* * $object->fk_prod_attr_val ID of the ProductAttributeValue object "white" |
45
|
|
|
*/ |
46
|
|
|
class ProductCombination2ValuePair |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Database handler |
50
|
|
|
* @var DoliDB |
51
|
|
|
*/ |
52
|
|
|
private $db; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* ID of this ProductCombination2ValuePair |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
public $id; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* ID of the ProductCombination linked to this object |
62
|
|
|
* (ex: ID of the ProductCombination between product "shirt" and its variant "shirt XL white") |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
public $fk_prod_combination; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* ID of the ProductAttribute linked to this object |
69
|
|
|
* (ex: ID of the ProductAttribute "color") |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
public $fk_prod_attr; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* ID of the ProductAttributeValue linked to this object |
76
|
|
|
* (ex: ID of the ProductAttributeValue "white") |
77
|
|
|
* @var int |
78
|
|
|
*/ |
79
|
|
|
public $fk_prod_attr_val; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Error message |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
public $error; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Array of error messages |
89
|
|
|
* @var string[] |
90
|
|
|
*/ |
91
|
|
|
public $errors = array(); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Constructor |
95
|
|
|
* |
96
|
|
|
* @param DoliDB $db Database handler |
97
|
|
|
*/ |
98
|
|
|
public function __construct(DoliDB $db) |
99
|
|
|
{ |
100
|
|
|
$this->db = $db; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Translates this class to a human-readable string |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function __toString() |
109
|
|
|
{ |
110
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/variants/class/ProductAttributeValue.class.php'; |
111
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/variants/class/ProductAttribute.class.php'; |
112
|
|
|
|
113
|
|
|
$prodattr = new ProductAttribute($this->db); |
114
|
|
|
$prodattrval = new ProductAttributeValue($this->db); |
115
|
|
|
|
116
|
|
|
$prodattr->fetch($this->fk_prod_attr); |
117
|
|
|
$prodattrval->fetch($this->fk_prod_attr_val); |
118
|
|
|
|
119
|
|
|
return $prodattr->label . ': ' . $prodattrval->value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Create a ProductCombination2ValuePair |
124
|
|
|
* |
125
|
|
|
* @param User $user User that creates //not used |
126
|
|
|
* @return -1|1 1 if OK, -1 if KO |
|
|
|
|
127
|
|
|
*/ |
128
|
|
|
public function create($user) |
129
|
|
|
{ |
130
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_attribute_combination2val |
131
|
|
|
(fk_prod_combination, fk_prod_attr, fk_prod_attr_val) |
132
|
|
|
VALUES(" . (int) $this->fk_prod_combination . ", " . (int) $this->fk_prod_attr . ", " . (int) $this->fk_prod_attr_val . ")"; |
133
|
|
|
|
134
|
|
|
$query = $this->db->query($sql); |
135
|
|
|
|
136
|
|
|
if ($query) { |
137
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'product_attribute_combination2val'); |
138
|
|
|
|
139
|
|
|
return 1; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return -1; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Retrieve all ProductCombination2ValuePair linked to a given ProductCombination ID. |
147
|
|
|
* |
148
|
|
|
* @param int $fk_combination ID of the ProductCombination |
149
|
|
|
* @return -1|ProductCombination2ValuePair[] -1 if KO, array of ProductCombination2ValuePair if OK |
|
|
|
|
150
|
|
|
*/ |
151
|
|
|
public function fetchByFkCombination($fk_combination) |
152
|
|
|
{ |
153
|
|
|
$sql = "SELECT |
154
|
|
|
c.rowid, |
155
|
|
|
c2v.fk_prod_attr_val, |
156
|
|
|
c2v.fk_prod_attr, |
157
|
|
|
c2v.fk_prod_combination |
158
|
|
|
FROM " . MAIN_DB_PREFIX . "product_attribute c LEFT JOIN " . MAIN_DB_PREFIX . "product_attribute_combination2val c2v ON c.rowid = c2v.fk_prod_attr |
159
|
|
|
WHERE c2v.fk_prod_combination = " . (int) $fk_combination; |
160
|
|
|
|
161
|
|
|
$sql .= $this->db->order('c.position', 'asc'); |
162
|
|
|
|
163
|
|
|
$query = $this->db->query($sql); |
164
|
|
|
|
165
|
|
|
if (!$query) { |
166
|
|
|
return -1; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$return = array(); |
170
|
|
|
|
171
|
|
|
while ($obj = $this->db->fetch_object($query)) { |
172
|
|
|
$tmp = new ProductCombination2ValuePair($this->db); |
173
|
|
|
$tmp->fk_prod_attr_val = $obj->fk_prod_attr_val; |
174
|
|
|
$tmp->fk_prod_attr = $obj->fk_prod_attr; |
175
|
|
|
$tmp->fk_prod_combination = $obj->fk_prod_combination; |
176
|
|
|
$tmp->id = $obj->rowid; |
177
|
|
|
|
178
|
|
|
$return[] = $tmp; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Delete all ProductCombination2ValuePair linked to a given ProductCombination ID. |
186
|
|
|
* |
187
|
|
|
* @param int $fk_combination ID of the ProductCombination |
188
|
|
|
* @return -1|1 -1 if KO, 1 if OK |
|
|
|
|
189
|
|
|
*/ |
190
|
|
|
public function deleteByFkCombination($fk_combination) |
191
|
|
|
{ |
192
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_attribute_combination2val WHERE fk_prod_combination = " . (int) $fk_combination; |
193
|
|
|
|
194
|
|
|
if ($this->db->query($sql)) { |
195
|
|
|
return 1; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return -1; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|