forked from clark21/UAC
/
Page.php
executable file
·225 lines (189 loc) · 4.81 KB
/
Page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php //-->
/*
* This file is part of the Openovate Labs Inc. framework library
* (c) 2013-2014 Openovate Labs
*
* Copyright and license information can be found at LICENSE
* distributed with this package.
*/
/**
* The base class for any class that defines a view.
* A view controls how templates are loaded as well as
* being the final point where data manipulation can occur.
*
* @vendor Openovate
* @package Framework
*/
class Page extends Eden\Block\Base
{
/* CONSTANTS
-------------------------------*/
const TEMPLATE_EXTENSION = 'phtml';
/* Public Properties
-------------------------------*/
/* Protected Properties
-------------------------------*/
protected $meta = array();
protected $head = array();
protected $body = array();
protected $foot = array();
protected $active = '';
protected $id = NULL;
protected $title = NULL;
protected $messages = array();
public function __construct() {
$page = control()->registry()->get('page');
if(($page != 'Front\Page\Login' && $page != 'Front\Page\Verify') && !isset($_SESSION['user'])) {
header('Location: /login');
exit;
}
//logout
if(isset($_GET['logout'])) {
unset($_SESSION['user']);
header('Location: /login');
exit;
}
}
/**
* returns variables used for templating
*
* @return array
*/
public function getVariables()
{
return $this->body;
}
/**
* returns location of template file
*
* @return string
*/
public function getTemplate()
{
if(!$this->template) {
$start = strrpos(get_class($this), '\\');
$this->template = control('type', get_class($this))
->str_replace('\\', DIRECTORY_SEPARATOR)
->substr($start)
->strtolower().'.'
.static::TEMPLATE_EXTENSION;
}
return $this->template;
}
/**
* Transform block to string
*
* @param array
* @return string
*/
public function render()
{
$messages = array();
if(isset($_SESSION['messages'])) {
$messages = $_SESSION['messages'];
$_SESSION['messages'] = array();
}
$path = control()->path('template');
$template = $this->getTemplate();
$helpers = $this->getHelpers();
$head = array_merge($helpers, array(
'head' => $this->head,
'active' => $this->active
));
$body = array_merge($helpers, $this->getVariables());
$foot = array_merge($helpers, $this->foot);
$file = $path.'/defaults/head.'.static::TEMPLATE_EXTENSION;
$head = control()->trigger('head')->template($file, $head);
$file = $path.$template;
$body = control()->trigger('body')->template($file, $body);
$file = $path.'/defaults/foot.'.static::TEMPLATE_EXTENSION;
$foot = control()->trigger('foot')->template($file, $foot);
$page = array_merge($helpers, array(
'meta' => $this->meta,
'title' => $this->title,
'class' => $this->id,
'active' => $this->active,
'head' => $head,
'messages' => $messages,
'body' => $body,
'foot' => $foot));
//page
$file = $path.'/defaults/page.'.static::TEMPLATE_EXTENSION;
return control()->template($file, $page);
}
protected function addStyles($styles)
{
if(!isset($this->header)) {
$this->header = array();
}
if(is_string($styles)) {
$styles = array($styles);
}
if(!isset($this->header['styles'])) {
$this->header['styles'] = $styles;
} else {
$this->header['styles'] = array_merge($this->header['styles'], $styles);
}
return $this;
}
/**
* Adds flash messaging
*
* @param string
* @param string
* @param boolean
* @return Front\Page
*/
protected function addMessage($message, $type = 'info' , $flash = true)
{
$_SESSION['messages'][] = array(
'type' => $type,
'message' => $message);
return $this;
}
protected function getHelpers()
{
$urlRoot = control()->path('url');
$cdnRoot = control()->path('cdn');
$language = control()->language();
return array(
'url' => function() use ($urlRoot) {
echo $urlRoot;
},
'cdn' => function() use ($cdnRoot) {
echo $cdnRoot;
},
'_' => function($key) use ($language) {
echo $language[$key];
});
}
/**
* send email template
*
* @param string
* @param array
* @param array
*/
protected function sendEmail($subject, $to, $vars)
{
//set email account
$smtp = eden('mail')->smtp(
'smtp.gmail.com',
self::EMAIL,
self::PASSWORD, 465, true);
//parse the template with its variables
$message = control()->template($this->emailTemp, $vars);
//now send the parsed message to the user's email
$smtp->setSubject($subject)
->setBody($message, true);
foreach ($to as $email) {
$smtp->addTo($email);
}
return $smtp->send();
}
protected function error($key, $value) {
$this->errors[] = array(
$key => $value
);
}
}