Passed
Push — master ( 434720...2fb7f4 )
by Aimeos
02:14
created

Standard::init()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 36
rs 9.2568
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
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
	/**
25
	 * Returns the HTML code for insertion into the body.
26
	 *
27
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
28
	 * @return string HTML code
29
	 */
30
	public function body( string $uid = '' ) : string
31
	{
32
		return '';
33
	}
34
35
36
	/**
37
	 * Modifies the cached body content to replace content based on sessions or cookies.
38
	 *
39
	 * @param string $content Cached content
40
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
41
	 * @return string Modified body content
42
	 */
43
	public function modify( string $content, string $uid ) : string
44
	{
45
		if( !$this->view()->access( ['editor', 'admin', 'super'] ) )
46
		{
47
			$csrf = $this->view()->csrf();
48
			$content = str_replace( ['%csrf.name%', '%csrf.value%'], [$csrf->name(), $csrf->value()], $content );
49
		}
50
51
		return $content;
52
	}
53
54
55
	/**
56
	 * Processes the input, e.g. store given values.
57
	 *
58
	 * A view must be available and this method doesn't generate any output
59
	 * besides setting view variables if necessary.
60
	 */
61
	public function init()
62
	{
63
		$view = $this->view();
64
65
		$name = $view->param( 'contact/name' );
66
		$email = $view->param( 'contact/email' );
67
		$msg = $view->param( 'contact/message' );
68
		$honeypot = $view->param( 'contact/url' );
69
70
		if( !$honeypot && $email && $msg )
71
		{
72
			$context = $this->context();
73
			$config = $context->config();
74
75
			$toAddr = $config->get( 'resource/email/from-email' );
76
			$toName = $config->get( 'resource/email/from-name' );
77
78
			if( $toAddr )
79
			{
80
				$label = $context->locale()->getSiteItem()->getLabel();
81
82
				$context->mail()->create()
83
					->to( $toAddr, $toName )
84
					->from( $email, $name )
85
					->subject( $context->translate( 'client', 'Your request' ) . ' - ' . $label )
86
					->text( $msg )
87
					->send();
88
89
				$error = [$context->translate( 'client', 'Message sent successfully' )];
90
			}
91
			else
92
			{
93
				$error = [$context->translate( 'client', 'No recipient configured' )];
94
			}
95
96
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
97
		}
98
	}
99
}
100