Passed
Branch master (560f53)
by Aimeos
13:02
created

Standard::getSubClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Cms\Page\Contact;
12
13
14
/**
15
 * Default implementation for CMS contact form.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/cms/page/contact/subparts
25
	 * List of HTML sub-clients rendered within the cms page contact section
26
	 *
27
	 * The output of the frontend is composed of the code generated by the HTML
28
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
29
	 * that are responsible for rendering certain sub-parts of the output. The
30
	 * sub-clients can contain HTML clients themselves and therefore a
31
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
32
	 * the output that is placed inside the container of its parent.
33
	 *
34
	 * At first, always the HTML code generated by the parent is printed, then
35
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
36
	 * determines the order of the output of these sub-clients inside the parent
37
	 * container. If the configured list of clients is
38
	 *
39
	 *  array( "subclient1", "subclient2" )
40
	 *
41
	 * you can easily change the order of the output by reordering the subparts:
42
	 *
43
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
44
	 *
45
	 * You can also remove one or more parts if they shouldn't be rendered:
46
	 *
47
	 *  client/html/<clients>/subparts = array( "subclient1" )
48
	 *
49
	 * As the clients only generates structural HTML, the layout defined via CSS
50
	 * should support adding, removing or reordering content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2021.04
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/cms/page/contact/subparts';
58
	private $subPartNames = [];
59
60
61
	/**
62
	 * Returns the HTML code for insertion into the body.
63
	 *
64
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
65
	 * @return string HTML code
66
	 */
67
	public function getBody( string $uid = '' ) : string
68
	{
69
		return '';
70
	}
71
72
73
	/**
74
	 * Returns the sub-client given by its name.
75
	 *
76
	 * @param string $type Name of the client type
77
	 * @param string|null $name Name of the sub-client (Default if null)
78
	 * @return \Aimeos\Client\Html\Iface Sub-client object
79
	 */
80
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
81
	{
82
		/** client/html/cms/page/contact/decorators/excludes
83
		 * Excludes decorators added by the "common" option from the cms page contact html client
84
		 *
85
		 * Decorators extend the functionality of a class by adding new aspects
86
		 * (e.g. log what is currently done), executing the methods of the underlying
87
		 * class only in certain conditions (e.g. only for logged in users) or
88
		 * modify what is returned to the caller.
89
		 *
90
		 * This option allows you to remove a decorator added via
91
		 * "client/html/common/decorators/default" before they are wrapped
92
		 * around the html client.
93
		 *
94
		 *  client/html/cms/page/contact/decorators/excludes = array( 'decorator1' )
95
		 *
96
		 * This would remove the decorator named "decorator1" from the list of
97
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
98
		 * "client/html/common/decorators/default" to the html client.
99
		 *
100
		 * @param array List of decorator names
101
		 * @since 2021.04
102
		 * @category Developer
103
		 * @see client/html/common/decorators/default
104
		 * @see client/html/cms/page/contact/decorators/global
105
		 * @see client/html/cms/page/contact/decorators/local
106
		 */
107
108
		/** client/html/cms/page/contact/decorators/global
109
		 * Adds a list of globally available decorators only to the cms page contact html client
110
		 *
111
		 * Decorators extend the functionality of a class by adding new aspects
112
		 * (e.g. log what is currently done), executing the methods of the underlying
113
		 * class only in certain conditions (e.g. only for logged in users) or
114
		 * modify what is returned to the caller.
115
		 *
116
		 * This option allows you to wrap global decorators
117
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
118
		 *
119
		 *  client/html/cms/page/contact/decorators/global = array( 'decorator1' )
120
		 *
121
		 * This would add the decorator named "decorator1" defined by
122
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
123
		 *
124
		 * @param array List of decorator names
125
		 * @since 2021.04
126
		 * @category Developer
127
		 * @see client/html/common/decorators/default
128
		 * @see client/html/cms/page/contact/decorators/excludes
129
		 * @see client/html/cms/page/contact/decorators/local
130
		 */
131
132
		/** client/html/cms/page/contact/decorators/local
133
		 * Adds a list of local decorators only to the cms page contact html client
134
		 *
135
		 * Decorators extend the functionality of a class by adding new aspects
136
		 * (e.g. log what is currently done), executing the methods of the underlying
137
		 * class only in certain conditions (e.g. only for logged in users) or
138
		 * modify what is returned to the caller.
139
		 *
140
		 * This option allows you to wrap local decorators
141
		 * ("\Aimeos\Client\Html\Cms\Decorator\*") around the html client.
142
		 *
143
		 *  client/html/cms/page/contact/decorators/local = array( 'decorator2' )
144
		 *
145
		 * This would add the decorator named "decorator2" defined by
146
		 * "\Aimeos\Client\Html\Cms\Decorator\Decorator2" only to the html client.
147
		 *
148
		 * @param array List of decorator names
149
		 * @since 2021.04
150
		 * @category Developer
151
		 * @see client/html/common/decorators/default
152
		 * @see client/html/cms/page/contact/decorators/excludes
153
		 * @see client/html/cms/page/contact/decorators/global
154
		 */
155
156
		return $this->createSubClient( 'cms/page/contact/' . $type, $name );
157
	}
158
159
160
	/**
161
	 * Modifies the cached body content to replace content based on sessions or cookies.
162
	 *
163
	 * @param string $content Cached content
164
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
165
	 * @return string Modified body content
166
	 */
167
	public function modifyBody( string $content, string $uid ) : string
168
	{
169
		$content = parent::modifyBody( $content, $uid );
170
171
		return $this->replaceSection( $content, $this->getView()->csrf()->formfield(), 'cms.page.contact.csrf' );
172
	}
173
174
175
	/**
176
	 * Processes the input, e.g. store given values.
177
	 *
178
	 * A view must be available and this method doesn't generate any output
179
	 * besides setting view variables if necessary.
180
	 */
181
	public function process()
182
	{
183
		$view = $this->getView();
184
185
		$name = $view->param( 'contact/name' );
186
		$email = $view->param( 'contact/email' );
187
		$msg = $view->param( 'contact/message' );
188
		$honeypot = $view->param( 'contact/url' );
189
190
		if( !$honeypot && $name && $email && $msg )
191
		{
192
			$context = $this->getContext();
193
			$config = $context->getConfig();
194
195
			$toAddr = $config->get( 'resource/email/from-address' );
196
			$toName = $config->get( 'resource/email/from-name' );
197
198
			if( $toAddr )
199
			{
200
				$context->mail()->createMessage()
201
				->addTo( $toAddr, $toName )
202
				->addFrom( $email, $name )
203
				->setBody( $msg )
204
				->send();
205
206
				$error = [$context->getI18n()->dt( 'client', 'Message sent successfully' )];
207
			}
208
			else
209
			{
210
				$error = [$context->getI18n()->dt( 'client', 'No recipient configured' )];
211
			}
212
213
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
214
		}
215
216
		parent::process();
217
	}
218
219
220
	/**
221
	 * Returns the list of sub-client names configured for the client.
222
	 *
223
	 * @return array List of HTML client names
224
	 */
225
	protected function getSubClientNames() : array
226
	{
227
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
228
	}
229
}
230