Standard::init()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 3
b 0
f 0
nc 3
nop 0
dl 0
loc 32
rs 9.2728
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2025
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
		$csrf = $this->view()->csrf();
46
		return str_replace( ['%csrf.name%', '%csrf.value%'], [$csrf->name(), $csrf->value()], $content );
47
	}
48
49
50
	/**
51
	 * Processes the input, e.g. store given values.
52
	 *
53
	 * A view must be available and this method doesn't generate any output
54
	 * besides setting view variables if necessary.
55
	 */
56
	public function init()
57
	{
58
		$view = $this->view();
59
		$params = $view->param( 'contact' );
60
61
		if( !( $params['url'] ?? null ) && ( $params['email'] ?? null ) && ( $params['message'] ?? null ) )
62
		{
63
			$context = $this->context();
64
			$config = $context->config();
65
66
			$toAddr = $config->get( 'resource/email/from-email' );
67
			$toName = $config->get( 'resource/email/from-name' );
68
69
			if( $toAddr )
70
			{
71
				$label = $context->locale()->getSiteItem()->getLabel();
72
73
				$context->mail()->create()
74
					->to( $toAddr, $toName )
75
					->from( $toAddr, $toName )
76
					->replyTo( $params['email'], $params['name'] ?? null )
77
					->subject( $context->translate( 'client', 'Your request' ) . ' - ' . $label )
78
					->text( $this->text( $params ) )
79
					->send();
80
81
				$info = [$context->translate( 'client', 'Message sent successfully' )];
82
				$view->infos = array_merge( $view->get( 'infos', [] ), $info );
83
			}
84
			else
85
			{
86
				$error = [$context->translate( 'client', 'No recipient configured' )];
87
				$view->errors = array_merge( $view->get( 'errors', [] ), $error );
88
			}
89
		}
90
	}
91
92
93
	/**
94
	 * Returns the message text
95
	 *
96
	 * @param array $params Associative list of key/value pairs
97
	 * @return string Message text
98
	 */
99
	protected function text( array $params ) : string
100
	{
101
		$msg = '';
102
103
		foreach( $params as $key => $val ) {
104
			$msg .= $key . ': ' . $val . "\n";
105
		}
106
107
		return $msg;
108
	}
109
}
110