Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Standard::addData()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 8.4905
c 0
b 0
f 0
cc 6
nc 8
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Email\Delivery\Html;
13
14
15
/**
16
 * Default implementation of email html HTML client.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Common\Client\Summary\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/email/delivery/html/subparts
26
	 * List of HTML sub-clients rendered within the email delivery html section
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * @param array List of sub-client names
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
	private $subPartPath = 'client/html/email/delivery/html/subparts';
59
	private $subPartNames = [];
60
61
62
	/**
63
	 * Returns the HTML code for insertion into the body.
64
	 *
65
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
66
	 * @return string HTML code
67
	 */
68
	public function getBody( string $uid = '' ) : string
69
	{
70
		$view = $this->getView();
71
72
		$content = '';
73
		foreach( $this->getSubClients() as $subclient ) {
74
			$content .= $subclient->setView( $view )->getBody( $uid );
75
		}
76
		$view->htmlBody = $content;
77
78
		/** client/html/email/delivery/html/template-body
79
		 * Relative path to the HTML body template of the email delivery html client.
80
		 *
81
		 * The template file contains the HTML code and processing instructions
82
		 * to generate the result shown in the body of the e-mail. The
83
		 * configuration string is the path to the template file relative
84
		 * to the templates directory (usually in client/html/templates).
85
		 *
86
		 * You can overwrite the template file configuration in extensions and
87
		 * provide alternative templates. These alternative templates should be
88
		 * named like the default one but with the string "standard" replaced by
89
		 * an unique name. You may use the name of your project for this. If
90
		 * you've implemented an alternative client class as well, "standard"
91
		 * should be replaced by the name of the new class.
92
		 *
93
		 * The email delivery html client allows to use a different template for
94
		 * each delivery status value. You can create a template for each delivery
95
		 * status and store it in the "email/delivery/<status number>/" directory
96
		 * below the "templates" directory (usually in client/html/templates). If no
97
		 * specific layout template is found, the common template in the
98
		 * "email/delivery/" directory is used.
99
		 *
100
		 * @param string Relative path to the template creating code for the HTML e-mail body
101
		 * @since 2014.03
102
		 * @category Developer
103
		 * @see client/html/email/delivery/html/template-header
104
		 */
105
		$tplconf = 'client/html/email/delivery/html/template-body';
106
107
		$html = $view->render( $view->config( $tplconf, 'email/delivery/html-body-standard' ) );
108
		$view->mail()->setBodyHtml( $html );
109
110
		return $html;
111
	}
112
113
114
	/**
115
	 * Returns the sub-client given by its name.
116
	 *
117
	 * @param string $type Name of the client type
118
	 * @param string|null $name Name of the sub-client (Default if null)
119
	 * @return \Aimeos\Client\Html\Iface Sub-client object
120
	 */
121
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
122
	{
123
		/** client/html/email/delivery/html/decorators/excludes
124
		 * Excludes decorators added by the "common" option from the "email delivery html" html client
125
		 *
126
		 * Decorators extend the functionality of a class by adding new aspects
127
		 * (e.g. log what is currently done), executing the methods of the underlying
128
		 * class only in certain conditions (e.g. only for logged in users) or
129
		 * modify what is returned to the caller.
130
		 *
131
		 * This option allows you to remove a decorator added via
132
		 * "client/html/common/decorators/default" before they are wrapped
133
		 * around the html client.
134
		 *
135
		 *  client/html/email/delivery/html/decorators/excludes = array( 'decorator1' )
136
		 *
137
		 * This would remove the decorator named "decorator1" from the list of
138
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
139
		 * "client/html/common/decorators/default" to the html client.
140
		 *
141
		 * @param array List of decorator names
142
		 * @since 2015.08
143
		 * @category Developer
144
		 * @see client/html/common/decorators/default
145
		 * @see client/html/email/delivery/html/decorators/global
146
		 * @see client/html/email/delivery/html/decorators/local
147
		 */
148
149
		/** client/html/email/delivery/html/decorators/global
150
		 * Adds a list of globally available decorators only to the "email delivery html" html client
151
		 *
152
		 * Decorators extend the functionality of a class by adding new aspects
153
		 * (e.g. log what is currently done), executing the methods of the underlying
154
		 * class only in certain conditions (e.g. only for logged in users) or
155
		 * modify what is returned to the caller.
156
		 *
157
		 * This option allows you to wrap global decorators
158
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
159
		 *
160
		 *  client/html/email/delivery/html/decorators/global = array( 'decorator1' )
161
		 *
162
		 * This would add the decorator named "decorator1" defined by
163
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
164
		 *
165
		 * @param array List of decorator names
166
		 * @since 2015.08
167
		 * @category Developer
168
		 * @see client/html/common/decorators/default
169
		 * @see client/html/email/delivery/html/decorators/excludes
170
		 * @see client/html/email/delivery/html/decorators/local
171
		 */
172
173
		/** client/html/email/delivery/html/decorators/local
174
		 * Adds a list of local decorators only to the "email delivery html" html client
175
		 *
176
		 * Decorators extend the functionality of a class by adding new aspects
177
		 * (e.g. log what is currently done), executing the methods of the underlying
178
		 * class only in certain conditions (e.g. only for logged in users) or
179
		 * modify what is returned to the caller.
180
		 *
181
		 * This option allows you to wrap local decorators
182
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
183
		 *
184
		 *  client/html/email/delivery/html/decorators/local = array( 'decorator2' )
185
		 *
186
		 * This would add the decorator named "decorator2" defined by
187
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
188
		 *
189
		 * @param array List of decorator names
190
		 * @since 2015.08
191
		 * @category Developer
192
		 * @see client/html/common/decorators/default
193
		 * @see client/html/email/delivery/html/decorators/excludes
194
		 * @see client/html/email/delivery/html/decorators/global
195
		 */
196
197
		return $this->createSubClient( 'email/delivery/html/' . $type, $name );
198
	}
199
200
201
	/**
202
	 * Returns the list of sub-client names configured for the client.
203
	 *
204
	 * @return array List of HTML client names
205
	 */
206
	protected function getSubClientNames() : array
207
	{
208
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
209
	}
210
211
212
	/**
213
	 * Sets the necessary parameter values in the view.
214
	 *
215
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
216
	 * @param array &$tags Result array for the list of tags that are associated to the output
217
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
218
	 * @return \Aimeos\MW\View\Iface Modified view object
219
	 */
220
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
221
	{
222
		/** client/html/email/logo
223
		 * Path to the logo image displayed in HTML e-mails
224
		 *
225
		 * The path can either be an absolute local path or an URL to a file on a
226
		 * remote server. If the file is stored on a remote server, "allow_url_fopen"
227
		 * must be enabled. See {@link http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen php.ini allow_url_fopen}
228
		 * documentation for details.
229
		 *
230
		 * @param string Absolute file system path or remote URL to the logo image
231
		 * @since 2014.03
232
		 * @category User
233
		 * @see client/html/email/from-email
234
		 */
235
		$file = $view->config( 'client/html/email/logo', 'client/html/themes/elegance/media/aimeos.png' );
236
237
		if( file_exists( $file ) && ( $content = file_get_contents( $file ) ) !== false )
238
		{
239
			$finfo = new \finfo( FILEINFO_MIME_TYPE );
240
			$mimetype = $finfo->file( $file );
241
242
			$view->htmlLogo = $view->mail()->embedAttachment( $content, $mimetype, basename( $file ) );
243
		}
244
245
246
		$path = $view->config( 'client/html/common/template/baseurl', 'client/html/themes/elegance' );
247
		$filepath = $path . DIRECTORY_SEPARATOR . 'email.css';
248
249
		if( file_exists( $filepath ) && ( $css = file_get_contents( $filepath ) ) !== false ) {
250
			$view->htmlCss = $css;
251
		}
252
253
254
		$basket = $view->extOrderBaseItem;
255
256
		// we can't cache the calculation because the same client object is used for all e-mails
257
		$view->summaryCostsDelivery = $this->getCostsDelivery( $basket );
258
		$view->summaryCostsPayment = $this->getCostsPayment( $basket );
259
		$view->summaryNamedTaxes = $this->getNamedTaxes( $basket );
260
		$view->summaryTaxRates = $this->getTaxRates( $basket );
261
		$view->summaryBasket = $basket;
262
263
		if( $view->extOrderItem->getPaymentStatus() >= $this->getDownloadPaymentStatus() ) {
264
			$view->summaryShowDownloadAttributes = true;
265
		}
266
267
		return parent::addData( $view, $tags, $expire );
268
	}
269
}
270