1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\PostModel; |
6
|
|
|
use Core\Container; |
7
|
|
|
use Core\Traits\StringFunctions; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Home |
11
|
|
|
* |
12
|
|
|
* The home page |
13
|
|
|
* |
14
|
|
|
* @package App\Controllers |
15
|
|
|
*/ |
16
|
|
|
class Home extends \Core\Controller |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
use StringFunctions; |
|
|
|
|
20
|
|
|
|
21
|
|
|
protected $siteConfig; |
22
|
|
|
protected $sendMail; |
23
|
|
|
|
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
public function __construct(Container $container) |
27
|
|
|
{ |
28
|
|
|
$this->loadModules[] = 'SiteConfig'; |
29
|
|
|
$this->loadModules[] = 'SendMail'; |
30
|
|
|
parent::__construct($container); |
31
|
|
|
|
32
|
|
|
$this->config = $this->siteConfig->getSiteConfig(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Show the front page |
37
|
|
|
* @throws \ErrorException |
38
|
|
|
* @throws \ReflectionException |
39
|
|
|
* @throws \Twig_Error_Loader |
40
|
|
|
* @throws \Twig_Error_Runtime |
41
|
|
|
* @throws \Twig_Error_Syntax |
42
|
|
|
*/ |
43
|
|
|
public function index() |
44
|
|
|
{ |
45
|
|
|
$frontPostModel = new PostModel($this->container); |
46
|
|
|
|
47
|
|
|
$frontPosts = $frontPostModel->getFrontPosts(); |
48
|
|
|
|
49
|
|
|
$this->data['configs'] = $this->config; |
50
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
51
|
|
|
$this->data['jumbotron'] = true; |
52
|
|
|
$this->data['front_posts'] = $frontPosts; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
//check if have prefilled form data and error messages |
56
|
|
|
$this->data["contactInfo"] = $this->session->get("contactInfo"); |
57
|
|
|
$this->data["contactErrors"] = $this->session->get("contactErrors"); |
58
|
|
|
|
59
|
|
|
//remove the set data as it is now sent to the template |
60
|
|
|
$this->session->remove("contactInfo"); |
61
|
|
|
$this->session->remove("contactErrors"); |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->renderView('Home'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Send the contact form with error checking |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
|
|
public function sendContactForm() |
73
|
|
|
{ |
74
|
|
|
$this->onlyPost(); |
75
|
|
|
|
76
|
|
|
//verify input values (html special chars ?) |
77
|
|
|
$to = $this->config["admin_email_address"]; |
78
|
|
|
$message = $this->request->getDataFull(); |
79
|
|
|
|
80
|
|
|
//Error checking |
81
|
|
|
|
82
|
|
|
//check all the fields |
83
|
|
|
$error = false; |
84
|
|
|
$contactErrors = new \stdClass(); |
85
|
|
|
|
86
|
|
|
if ($message["contactName"] == "") { |
87
|
|
|
$error = true; |
88
|
|
|
$contactErrors->contactName = "Name must not be empty"; |
89
|
|
|
} |
90
|
|
|
if ($message["contactEmail"] == "") { |
91
|
|
|
$error = true; |
92
|
|
|
$contactErrors->contactEmail = "Email must not be empty"; |
93
|
|
|
} |
94
|
|
|
if ($message["contactSubject"] == "") { |
95
|
|
|
$error = true; |
96
|
|
|
$contactErrors->contactSubject = "Subject must not be empty"; |
97
|
|
|
} |
98
|
|
|
if ($message["contactMessage"] == "") { |
99
|
|
|
$error = true; |
100
|
|
|
$contactErrors->contactMessage = "Message must not be empty"; |
101
|
|
|
} |
102
|
|
|
if (!$this->isEmail($message["contactEmail"])) { |
103
|
|
|
$error = true; |
104
|
|
|
$contactErrors->contactEmail = "email is not valid"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//If we found an error, return data to the register form and no create |
108
|
|
|
if ($error) { |
109
|
|
|
$this->session->set("contactInfo", $message); |
110
|
|
|
$this->session->set("contactErrors", $contactErrors); |
111
|
|
|
$this->response->redirect(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$config = $this->siteConfig->getSiteConfig(); |
115
|
|
|
|
116
|
|
|
//from here all is good, send mail |
117
|
|
|
$userName = htmlspecialchars($message["contactName"]); |
118
|
|
|
$subject = "Contact from ".$config["site_name"]." : "; |
119
|
|
|
$subject .= htmlspecialchars($message["contactSubject"]); |
120
|
|
|
$textMessage = "<h1>message sent by ".$userName."</h1>"; |
121
|
|
|
$textMessage .= htmlspecialchars($message["contactMessage"]); |
122
|
|
|
$from = $message["contactEmail"]; |
123
|
|
|
|
124
|
|
|
$this->sendMail->send($to, $subject, $textMessage, $from); |
125
|
|
|
|
126
|
|
|
$this->alertBox->setAlert('Email sent'); |
127
|
|
|
$this->response->redirect(); |
128
|
|
|
} |
129
|
|
|
} |