1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2014-2018 |
||
6 | * @package MShop |
||
7 | * @subpackage Common |
||
8 | */ |
||
9 | |||
10 | |||
11 | namespace Aimeos\MShop\Common\Manager\Property; |
||
12 | |||
13 | |||
14 | /** |
||
15 | * Abstract property manager implementation. |
||
16 | * |
||
17 | * @package MShop |
||
18 | * @subpackage Common |
||
19 | */ |
||
20 | abstract class Base |
||
21 | extends \Aimeos\MShop\Common\Manager\Base |
||
22 | { |
||
23 | private $prefix; |
||
24 | private $searchConfig; |
||
25 | private $languageId; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * Initializes the object. |
||
30 | * |
||
31 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
32 | */ |
||
33 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
34 | { |
||
35 | parent::__construct( $context ); |
||
36 | |||
37 | $this->languageId = $context->getLocale()->getLanguageId(); |
||
38 | $this->searchConfig = $this->getSearchConfig(); |
||
39 | |||
40 | if( ( $entry = reset( $this->searchConfig ) ) === false ) { |
||
41 | throw new \Aimeos\MShop\Exception( sprintf( 'Search configuration not available' ) ); |
||
42 | } |
||
43 | |||
44 | if( ( $pos = strrpos( $entry['code'], '.' ) ) === false ) { |
||
45 | throw new \Aimeos\MShop\Exception( sprintf( 'Search configuration for "%1$s" not available', $entry['code'] ) ); |
||
46 | } |
||
47 | |||
48 | if( ( $this->prefix = substr( $entry['code'], 0, $pos + 1 ) ) === false ) { |
||
49 | throw new \Aimeos\MShop\Exception( sprintf( 'Search configuration for "%1$s" not available', $entry['code'] ) ); |
||
50 | } |
||
51 | |||
52 | $this->plugins[$this->prefix . 'key'] = new \Aimeos\MW\Criteria\Plugin\Cut(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Creates a new empty item instance |
||
58 | * |
||
59 | * @param array $values Values the item should be initialized with |
||
60 | * @return \Aimeos\MShop\Common\Item\Property\Iface New property item object |
||
61 | */ |
||
62 | public function createItem( array $values = [] ) |
||
63 | { |
||
64 | $values[$this->prefix . 'siteid'] = $this->getContext()->getLocale()->getSiteId(); |
||
65 | return $this->createItemBase( $values ); |
||
66 | } |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Creates a search object and optionally sets base criteria. |
||
71 | * |
||
72 | * @param boolean $default Add default criteria |
||
73 | * @return \Aimeos\MW\Criteria\Iface Criteria object |
||
74 | */ |
||
75 | public function createSearch( $default = false ) |
||
76 | { |
||
77 | $object = parent::createSearch(); |
||
78 | |||
79 | if( $default === true ) |
||
80 | { |
||
81 | $langid = $this->getContext()->getLocale()->getLanguageId(); |
||
82 | |||
83 | $expr = array( |
||
84 | $object->compare( '==', $this->prefix . 'languageid', null ), |
||
85 | $object->compare( '==', $this->prefix . 'languageid', $langid ), |
||
86 | ); |
||
87 | |||
88 | $object->setConditions( $object->combine( '||', $expr ) ); |
||
89 | } |
||
90 | |||
91 | return $object; |
||
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Inserts the new property items for product item |
||
97 | * |
||
98 | * @param \Aimeos\MShop\Common\Item\Property\Iface $item Property item which should be saved |
||
99 | * @param boolean $fetch True if the new ID should be returned in the item |
||
100 | * @return \Aimeos\MShop\Common\Item\Iface $item Updated item including the generated ID |
||
101 | */ |
||
102 | public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true ) |
||
103 | { |
||
104 | self::checkClass( \Aimeos\MShop\Common\Item\Property\Iface::class, $item ); |
||
105 | |||
106 | if( !$item->isModified() ) { |
||
107 | return $item; |
||
108 | } |
||
109 | |||
110 | $context = $this->getContext(); |
||
111 | $dbm = $context->getDatabaseManager(); |
||
112 | $dbname = $this->getResourceName(); |
||
113 | $conn = $dbm->acquire( $dbname ); |
||
114 | |||
115 | try |
||
116 | { |
||
117 | $id = $item->getId(); |
||
118 | $date = date( 'Y-m-d H:i:s' ); |
||
119 | |||
120 | if( $id === null ) { |
||
121 | $type = 'insert'; |
||
122 | } else { |
||
123 | $type = 'update'; |
||
124 | } |
||
125 | |||
126 | $stmt = $conn->create( $this->getSqlConfig( $this->getConfigPath() . $type ) ); |
||
127 | |||
128 | $stmt->bind( 1, $item->getParentId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
129 | $stmt->bind( 2, $this->plugins[$this->prefix . 'key']->translate( $item->getKey() ) ); |
||
130 | $stmt->bind( 3, $item->getType() ); |
||
131 | $stmt->bind( 4, $item->getLanguageId() ); |
||
132 | $stmt->bind( 5, $item->getValue() ); |
||
133 | $stmt->bind( 6, $date ); //mtime |
||
134 | $stmt->bind( 7, $context->getEditor() ); |
||
135 | $stmt->bind( 8, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
136 | |||
137 | if( $id !== null ) { |
||
138 | $stmt->bind( 9, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
139 | $item->setId( $id ); //is not modified anymore |
||
140 | } else { |
||
141 | $stmt->bind( 9, $date ); //ctime |
||
142 | } |
||
143 | |||
144 | $stmt->execute()->finish(); |
||
145 | |||
146 | if( $fetch === true ) |
||
147 | { |
||
148 | if( $id === null ) { |
||
149 | $item->setId( $this->newId( $conn, $this->getConfigPath() . 'newid' ) ); |
||
150 | } else { |
||
151 | $item->setId( $id ); // modified false |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $dbm->release( $conn, $dbname ); |
||
156 | } |
||
157 | catch( \Exception $e ) |
||
158 | { |
||
159 | $dbm->release( $conn, $dbname ); |
||
160 | throw $e; |
||
161 | } |
||
162 | |||
163 | return $item; |
||
164 | } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Removes multiple items specified by ids in the array. |
||
169 | * |
||
170 | * @param string[] $ids List of IDs |
||
171 | * @return \Aimeos\MShop\Common\Manager\Property\Iface Manager object for chaining method calls |
||
172 | */ |
||
173 | public function deleteItems( array $ids ) |
||
174 | { |
||
175 | $this->deleteItemsBase( $ids, $this->getConfigPath() . 'delete' ); |
||
176 | } |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Returns product property item with given Id. |
||
181 | * |
||
182 | * @param string $id Id of the product property item |
||
183 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
184 | * @param boolean $default Add default criteria |
||
185 | * @return \Aimeos\MShop\Common\Item\Property\Iface Returns the product property item of the given id |
||
186 | * @throws \Aimeos\MShop\Exception If item couldn't be found |
||
187 | */ |
||
188 | public function getItem( $id, array $ref = [], $default = false ) |
||
189 | { |
||
190 | return $this->getItemBase( $this->prefix . 'id', $id, $ref, $default ); |
||
191 | } |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Search for all property items based on the given critera. |
||
196 | * |
||
197 | * @param \Aimeos\MW\Criteria\Iface $search Search criteria object |
||
198 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
199 | * @param integer|null &$total Number of items that are available in total |
||
200 | * @return array List of property items implementing \Aimeos\MShop\Common\Item\Property\Iface |
||
201 | */ |
||
202 | public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null ) |
||
203 | { |
||
204 | $items = []; |
||
205 | $context = $this->getContext(); |
||
206 | |||
207 | $dbm = $context->getDatabaseManager(); |
||
208 | $dbname = $this->getResourceName(); |
||
209 | $conn = $dbm->acquire( $dbname ); |
||
210 | |||
211 | try |
||
212 | { |
||
213 | $level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
||
214 | $cfgPathSearch = $this->getConfigPath() . 'search'; |
||
215 | $cfgPathCount = $this->getConfigPath() . 'count'; |
||
216 | $required = array( trim( $this->prefix, '.' ) ); |
||
217 | |||
218 | $results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level ); |
||
219 | |||
220 | while( ( $row = $results->fetch() ) !== false ) { |
||
221 | $items[$row[$this->prefix . 'id']] = $this->createItemBase( $row ); |
||
222 | } |
||
223 | |||
224 | $dbm->release( $conn, $dbname ); |
||
225 | } |
||
226 | catch( \Exception $e ) |
||
227 | { |
||
228 | $dbm->release( $conn, $dbname ); |
||
229 | throw $e; |
||
230 | } |
||
231 | |||
232 | return $items; |
||
233 | } |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Returns a new manager for product extensions |
||
238 | * |
||
239 | * @param string $manager Name of the sub manager type in lower case |
||
240 | * @param string|null $name Name of the implementation, will be from |
||
241 | * configuration (or Default) if null |
||
242 | * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g property types, property lists etc. |
||
243 | */ |
||
244 | public function getSubManager( $manager, $name = null ) |
||
245 | { |
||
246 | return $this->getSubManagerBase( 'common', 'property/' . $manager, $name ); |
||
247 | } |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Returns the config path for retrieving the configuration values. |
||
252 | * |
||
253 | * @return string Configuration path |
||
254 | */ |
||
255 | abstract protected function getConfigPath(); |
||
256 | |||
257 | |||
258 | /** |
||
259 | * Returns the search configuration for searching items. |
||
260 | * |
||
261 | * @return array Associative list of search keys and search definitions |
||
262 | */ |
||
263 | abstract protected function getSearchConfig(); |
||
264 | |||
265 | |||
266 | /** |
||
267 | * Creates new property item object. |
||
268 | * |
||
269 | * @param array $values Associative list of key/value pairs |
||
270 | * @return \Aimeos\MShop\Common\Item\Property\Standard New property item object |
||
271 | */ |
||
272 | protected function createItemBase( array $values = [] ) |
||
273 | { |
||
274 | $values['languageid'] = $this->languageId; |
||
275 | return new \Aimeos\MShop\Common\Item\Property\Standard( $this->prefix, $values ); |
||
276 | } |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Returns the prefix used for the item keys. |
||
281 | * |
||
282 | * @return string Item key prefix |
||
283 | */ |
||
284 | protected function getPrefix() |
||
285 | { |
||
286 | return $this->prefix; |
||
287 | } |
||
288 | } |
||
289 |