Passed
Push — master ( 6ab2f3...c0a629 )
by Aimeos
03:13
created

Base::getBase()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 8
nop 1
dl 0
loc 24
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Type;
12
13
14
/**
15
 * Base implementation of type JQAdm clients.
16
 *
17
 * @package Admin
18
 * @subpackage JQAdm
19
 */
20
abstract class Base
21
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
22
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
23
{
24
	/**
25
	 * Adds the required data used in the template
26
	 *
27
	 * @param \Aimeos\Base\View\Iface $view View object
28
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
29
	 */
30
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
31
	{
32
		$view->itemSubparts = $this->getSubClientNames();
33
		return $view;
34
	}
35
36
37
	/**
38
	 * Returns the rendered template including the view data
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
41
	 * @return string HTML output
42
	 */
43
	abstract protected function render( \Aimeos\Base\View\Iface $view ) : string;
44
45
46
	/**
47
	 * Modifiy several items at once
48
	 *
49
	 * @param string $domain Data domain of the items
50
	 * @param string|null $resource Resource name or null for domain name
51
	 * @return string|null Output to display
52
	 */
53
	protected function batchBase( string $domain, string $resource = null ) : ?string
54
	{
55
		return parent::batchBase( $domain . '/type', 'type/' . $domain );
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::batchBase($domai...pe', 'type/' . $domain) targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
56
	}
57
58
59
	/**
60
	 * Copies a resource
61
	 *
62
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
63
	 * @return string|null HTML output
64
	 */
65
	protected function copyBase( string $path ) : string
66
	{
67
		$view = $this->object()->data( $this->view() );
68
69
		try
70
		{
71
			if( ( $id = $view->param( 'id' ) ) === null )
72
			{
73
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
74
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
75
			}
76
77
			$manager = \Aimeos\MShop::create( $this->context(), $path . '/type' );
78
			$view->item = $manager->get( $id );
79
80
			$view->itemData = $this->toArray( $path, $view->item, true );
81
			$view->itemBody = parent::copy();
82
		}
83
		catch( \Exception $e )
84
		{
85
			$this->report( $e, 'copy' );
86
		}
87
88
		return $this->render( $view );
89
	}
90
91
92
	/**
93
	 * Creates a new resource
94
	 *
95
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
96
	 * @return string|null HTML output
97
	 */
98
	public function createBase( string $path ) : string
99
	{
100
		$view = $this->object()->data( $this->view() );
101
102
		try
103
		{
104
			$data = $view->param( 'item', [] );
105
106
			if( !isset( $view->item ) ) {
107
				$view->item = \Aimeos\MShop::create( $this->context(), $path . '/type' )->create();
108
			}
109
110
			$data[str_replace( '/', '.', $path ) . '.type.siteid'] = $view->item->getSiteId();
111
112
			$view->itemData = array_replace_recursive( $this->toArray( $path, $view->item ), $data );
113
			$view->itemBody = parent::create();
114
		}
115
		catch( \Exception $e )
116
		{
117
			$this->report( $e, 'create' );
118
		}
119
120
		return $this->render( $view );
121
	}
122
123
124
	/**
125
	 * Deletes a resource
126
	 *
127
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
128
	 * @return string|null HTML output
129
	 */
130
	public function deleteBase( string $path ) : ?string
131
	{
132
		$view = $this->view();
133
134
		$manager = \Aimeos\MShop::create( $this->context(), $path . '/type' );
135
		$manager->begin();
136
137
		try
138
		{
139
			if( ( $ids = $view->param( 'id' ) ) === null )
140
			{
141
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
142
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
143
			}
144
145
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
146
			$search->setConditions( $search->compare( '==', str_replace( '/', '.', $path ) . '.type.id', $ids ) );
147
			$items = $manager->search( $search );
148
149
			foreach( $items as $item )
150
			{
151
				$view->item = $item;
152
				parent::delete();
153
			}
154
155
			$manager->delete( $items->toArray() );
156
			$manager->commit();
157
158
			return $this->redirect( 'type/' . $path, 'search', null, 'delete' );
159
		}
160
		catch( \Exception $e )
161
		{
162
			$this->report( $e, 'delete' );
163
		}
164
165
		$manager->rollback();
166
167
		return $this->search();
168
	}
169
170
171
	/**
172
	 * Returns a single resource
173
	 *
174
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
175
	 * @return string|null HTML output
176
	 */
177
	public function getBase( string $path ) : string
178
	{
179
		$view = $this->object()->data( $this->view() );
180
181
		try
182
		{
183
			if( ( $id = $view->param( 'id' ) ) === null )
184
			{
185
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
186
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
187
			}
188
189
			$manager = \Aimeos\MShop::create( $this->context(), $path . '/type' );
190
191
			$view->item = $manager->get( $id );
192
			$view->itemData = $this->toArray( $path, $view->item );
193
			$view->itemBody = parent::get();
194
		}
195
		catch( \Exception $e )
196
		{
197
			$this->report( $e, 'get' );
198
		}
199
200
		return $this->render( $view );
201
	}
202
203
204
	/**
205
	 * Saves the data
206
	 *
207
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
208
	 * @return string|null HTML output
209
	 */
210
	public function saveBase( string $path ) : ?string
211
	{
212
		$view = $this->view();
213
214
		$manager = \Aimeos\MShop::create( $this->context(), $path . '/type' );
215
		$manager->begin();
216
217
		try
218
		{
219
			$item = $this->fromArray( $path, $view->param( 'item', [] ) );
220
			$view->item = $item->getId() ? $item : $manager->save( $item );
221
			$view->itemBody = parent::save();
222
223
			$manager->save( clone $view->item );
224
			$manager->commit();
225
226
			return $this->redirect( 'type/' . $path, $view->param( 'next' ), $view->item->getId(), 'save' );
0 ignored issues
show
Bug introduced by
It seems like $view->item->getId() can also be of type Aimeos\Map; however, parameter $id of Aimeos\Admin\JQAdm\Base::redirect() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
			return $this->redirect( 'type/' . $path, $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
227
		}
228
		catch( \Exception $e )
229
		{
230
			$manager->rollback();
231
			$this->report( $e, 'save' );
232
		}
233
234
		return $this->create();
235
	}
236
237
238
	/**
239
	 * Returns a list of resource according to the conditions
240
	 *
241
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
242
	 * @return \Aimeos\Base\View\Iface View object ready for rendering
243
	 */
244
	public function searchBase( string $path ) : \Aimeos\Base\View\Iface
245
	{
246
		$view = $this->view();
247
248
		try
249
		{
250
			$total = 0;
251
			$params = $this->storeFilter( $view->param(), 'type/' . $path );
252
			$manager = \Aimeos\MShop::create( $this->context(), $path . '/type' );
253
			$search = $this->initCriteria( $manager->filter(), $params );
254
255
			$view->items = $manager->search( $search, [], $total );
256
			$view->filterAttributes = $manager->getSearchAttributes( true );
257
			$view->filterOperators = $search->getOperators();
258
			$view->itemBody = parent::search();
259
			$view->total = $total;
260
		}
261
		catch( \Exception $e )
262
		{
263
			$this->report( $e, 'search' );
264
		}
265
266
		return $view;
267
	}
268
269
270
	/**
271
	 * Creates new and updates existing items using the data array
272
	 *
273
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
274
	 * @param array $data Data array
275
	 * @return \Aimeos\MShop\Common\Item\Type\Iface New type item object
276
	 */
277
	protected function fromArray( string $path, array $data ) : \Aimeos\MShop\Common\Item\Type\Iface
278
	{
279
		$key = str_replace( '/', '.', $path ) . '.type.id';
280
		$manager = \Aimeos\MShop::create( $this->context(), $path . '/type' );
281
282
		if( isset( $data[$key] ) && $data[$key] != '' ) {
283
			$item = $manager->get( $data[$key] );
284
		} else {
285
			$item = $manager->create();
286
		}
287
288
		$item->fromArray( $data, true );
289
290
		return $item;
291
	}
292
293
294
	/**
295
	 * Constructs the data array for the view from the given item
296
	 *
297
	 * @param string $path Path name like "attribute/lists" without "/type" postfix
298
	 * @param \Aimeos\MShop\Common\Item\Type\Iface $item Type item object
299
	 * @param bool True if item is going to be copied, false if not
300
	 * @return string[] Multi-dimensional associative list of item data
301
	 */
302
	protected function toArray( string $path, \Aimeos\MShop\Common\Item\Type\Iface $item, bool $copy = false )
303
	{
304
		$key = str_replace( '/', '.', $path );
305
		$data = $item->toArray( true );
306
307
		if( $copy === true )
308
		{
309
			$data[$key . '.type.code'] = $data[$key . '.type.code'] . '_' . substr( md5( microtime( true ) ), -5 );
310
			$data[$key . '.type.id'] = '';
311
		}
312
313
		return $data;
314
	}
315
}
316