Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 0
dl 0
loc 22
rs 9.8333
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-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Dashboard\Job;
12
13
14
/**
15
 * Default implementation of dashboard job JQAdm client.
16
 *
17
 * @package Admin
18
 * @subpackage JQAdm
19
 */
20
class Standard
21
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
22
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
23
{
24
	/** admin/jqadm/dashboard/job/name
25
	 * Name of the job subpart used by the JQAdm dashboard implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Dashboard\Job\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the JQAdm class name
31
	 * @since 2017.08
32
	 * @category Developer
33
	 */
34
35
36
	/**
37
	 * Deletes a resource
38
	 *
39
	 * @return string|null Output to display or null for none
40
	 */
41
	public function delete() : ?string
42
	{
43
		$view = $this->getView();
44
		$context = $this->getContext();
45
46
		if( ( $id = $view->param( 'id' ) ) === null ) {
47
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
48
		}
49
50
		$fs = $context->getFileSystemManager()->get( 'fs-admin' );
51
		$manager = \Aimeos\MAdmin::create( $context, 'job' );
52
		$item = $manager->getItem( $id );
53
		$result = $item->getResult();
54
55
		if( isset( $result['file'] ) && $fs->has( $result['file'] ) ) {
56
			$fs->rm( $result['file'] );
57
		}
58
59
		$manager->deleteItem( $id );
60
61
		return $this->search();
62
	}
63
64
65
	/**
66
	 * Returns a single resource
67
	 *
68
	 * @return string|null Output to display or null for none
69
	 */
70
	public function get() : ?string
71
	{
72
		$view = $this->getView();
73
		$context = $this->getContext();
74
75
		if( ( $id = $view->param( 'id' ) ) === null ) {
76
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
77
		}
78
79
		$fs = $context->getFileSystemManager()->get( 'fs-admin' );
80
		$item = \Aimeos\MAdmin::create( $context, 'job' )->getItem( $id );
81
		$result = $item->getResult();
82
83
		if( isset( $result['file'] ) && $fs->has( $result['file'] ) )
84
		{
85
			$stream = $view->response()->createStream( $fs->reads( $result['file'] ) );
86
			$view->response()->withHeader( 'Content-Disposition', 'attachment; filename="' . $result['file'] . '"' );
87
			$view->response()->withHeader( 'Content-Type', 'text/csv' );
88
			$view->response()->withBody( $stream );
89
		}
90
91
		return null;
92
	}
93
94
95
	/**
96
	 * Returns a list of resource according to the conditions
97
	 *
98
	 * @return string Output to display
99
	 */
100
	public function search() : ?string
101
	{
102
		$view = $this->getView();
103
		$context = $this->getContext();
104
105
		$manager = \Aimeos\MAdmin::create( $context, 'job' );
106
107
		$search = $manager->createSearch();
108
		$search->setSortations( [$search->sort( '-', 'job.ctime' ), $search->sort( '-', 'job.id' )] );
109
		$total = 0;
110
111
		$view->jobBody = '';
112
		$view->jobItems = $manager->searchItems( $search, [], $total );
113
		$view->jobTotal = $total;
114
115
116
		foreach( $this->getSubClients() as $client ) {
117
			$view->jobBody .= $client->search();
118
		}
119
120
		/** admin/jqadm/dashboard/job/template-list
121
		 * Relative path to the HTML body template of the job subpart for the dashboard.
122
		 *
123
		 * The template file contains the HTML code and processing instructions
124
		 * to generate the result shown in the body of the frontend. The
125
		 * configuration string is the path to the template file relative
126
		 * to the templates directory (usually in admin/jqadm/templates).
127
		 *
128
		 * You can overwrite the template file configuration in extensions and
129
		 * provide alternative templates. These alternative templates should be
130
		 * named like the default one but with the string "default" replaced by
131
		 * an unique name. You may use the name of your project for this. If
132
		 * you've implemented an alternative client class as well, "default"
133
		 * should be replaced by the name of the new class.
134
		 *
135
		 * @param string Relative path to the template creating the HTML code
136
		 * @since 2017.08
137
		 * @category Developer
138
		 */
139
		$tplconf = 'admin/jqadm/dashboard/job/template-list';
140
		$default = 'dashboard/list-job-standard';
141
142
		return $view->render( $view->config( $tplconf, $default ) );
143
	}
144
145
146
	/**
147
	 * Returns the sub-client given by its name.
148
	 *
149
	 * @param string $type Name of the client type
150
	 * @param string|null $name Name of the sub-client (Default if null)
151
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
152
	 */
153
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
154
	{
155
		/** admin/jqadm/dashboard/job/decorators/excludes
156
		 * Excludes decorators added by the "common" option from the dashboard JQAdm client
157
		 *
158
		 * Decorators extend the functionality of a class by adding new aspects
159
		 * (e.g. log what is currently done), executing the methods of the underlying
160
		 * class only in certain conditions (e.g. only for logged in users) or
161
		 * modify what is returned to the caller.
162
		 *
163
		 * This option allows you to remove a decorator added via
164
		 * "admin/jqadm/common/decorators/default" before they are wrapped
165
		 * around the JQAdm client.
166
		 *
167
		 *  admin/jqadm/dashboard/job/decorators/excludes = array( 'decorator1' )
168
		 *
169
		 * This would remove the decorator named "decorator1" from the list of
170
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
171
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
172
		 *
173
		 * @param array List of decorator names
174
		 * @since 2017.08
175
		 * @category Developer
176
		 * @see admin/jqadm/common/decorators/default
177
		 * @see admin/jqadm/dashboard/job/decorators/global
178
		 * @see admin/jqadm/dashboard/job/decorators/local
179
		 */
180
181
		/** admin/jqadm/dashboard/job/decorators/global
182
		 * Adds a list of globally available decorators only to the dashboard JQAdm client
183
		 *
184
		 * Decorators extend the functionality of a class by adding new aspects
185
		 * (e.g. log what is currently done), executing the methods of the underlying
186
		 * class only in certain conditions (e.g. only for logged in users) or
187
		 * modify what is returned to the caller.
188
		 *
189
		 * This option allows you to wrap global decorators
190
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
191
		 *
192
		 *  admin/jqadm/dashboard/job/decorators/global = array( 'decorator1' )
193
		 *
194
		 * This would add the decorator named "decorator1" defined by
195
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
196
		 *
197
		 * @param array List of decorator names
198
		 * @since 2017.08
199
		 * @category Developer
200
		 * @see admin/jqadm/common/decorators/default
201
		 * @see admin/jqadm/dashboard/job/decorators/excludes
202
		 * @see admin/jqadm/dashboard/job/decorators/local
203
		 */
204
205
		/** admin/jqadm/dashboard/job/decorators/local
206
		 * Adds a list of local decorators only to the dashboard JQAdm client
207
		 *
208
		 * Decorators extend the functionality of a class by adding new aspects
209
		 * (e.g. log what is currently done), executing the methods of the underlying
210
		 * class only in certain conditions (e.g. only for logged in users) or
211
		 * modify what is returned to the caller.
212
		 *
213
		 * This option allows you to wrap local decorators
214
		 * ("\Aimeos\Admin\JQAdm\Dashboard\Decorator\*") around the JQAdm client.
215
		 *
216
		 *  admin/jqadm/dashboard/job/decorators/local = array( 'decorator2' )
217
		 *
218
		 * This would add the decorator named "decorator2" defined by
219
		 * "\Aimeos\Admin\JQAdm\Dashboard\Decorator\Decorator2" only to the JQAdm client.
220
		 *
221
		 * @param array List of decorator names
222
		 * @since 2017.08
223
		 * @category Developer
224
		 * @see admin/jqadm/common/decorators/default
225
		 * @see admin/jqadm/dashboard/job/decorators/excludes
226
		 * @see admin/jqadm/dashboard/job/decorators/global
227
		 */
228
		return $this->createSubClient( 'dashboard/job/' . $type, $name );
229
	}
230
231
232
	/**
233
	 * Returns the list of sub-client names configured for the client.
234
	 *
235
	 * @return array List of JQAdm client names
236
	 */
237
	protected function getSubClientNames() : array
238
	{
239
		/** admin/jqadm/dashboard/job/standard/subparts
240
		 * List of JQAdm sub-clients rendered within the dashboard job section
241
		 *
242
		 * The output of the frontend is composed of the code generated by the JQAdm
243
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
244
		 * that are responsible for rendering certain sub-parts of the output. The
245
		 * sub-clients can contain JQAdm clients themselves and therefore a
246
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
247
		 * the output that is placed inside the container of its parent.
248
		 *
249
		 * At first, always the JQAdm code generated by the parent is printed, then
250
		 * the JQAdm code of its sub-clients. The job of the JQAdm sub-clients
251
		 * determines the job of the output of these sub-clients inside the parent
252
		 * container. If the configured list of clients is
253
		 *
254
		 *  array( "subclient1", "subclient2" )
255
		 *
256
		 * you can easily change the job of the output by reordering the subparts:
257
		 *
258
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
259
		 *
260
		 * You can also remove one or more parts if they shouldn't be rendered:
261
		 *
262
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
263
		 *
264
		 * As the clients only generates structural JQAdm, the layout defined via CSS
265
		 * should support adding, removing or reordering content by a fluid like
266
		 * design.
267
		 *
268
		 * @param array List of sub-client names
269
		 * @since 2017.08
270
		 * @category Developer
271
		 */
272
		return $this->getContext()->getConfig()->get( 'admin/jqadm/dashboard/job/standard/subparts', [] );
273
	}
274
}
275