Output handling to separate classes.
This commit is contained in:
parent
89af62d083
commit
be90c98118
36
index.php
36
index.php
@ -3,13 +3,10 @@
|
|||||||
require_once __DIR__ . '/vendor' . '/autoload.php';
|
require_once __DIR__ . '/vendor' . '/autoload.php';
|
||||||
|
|
||||||
use \Frs\FieldDefinition;
|
use \Frs\FieldDefinition;
|
||||||
|
use \Frs\Output\HtmlOutput;
|
||||||
|
use \Frs\Output\MailOutput;
|
||||||
|
|
||||||
$m = new Mustache_Engine(array(
|
$ho = new HtmlOutput(dirname(__FILE__) . '/templates');
|
||||||
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/templates'),
|
|
||||||
'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/templates/partials'),
|
|
||||||
'charset' => 'utf-8',
|
|
||||||
'logger' => new Mustache_Logger_StreamLogger('php://stderr'),
|
|
||||||
));
|
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
'session_time_left' => 0,
|
'session_time_left' => 0,
|
||||||
@ -49,7 +46,7 @@ $tpl_done = false;
|
|||||||
if (isset($_GET['action'])) {
|
if (isset($_GET['action'])) {
|
||||||
switch ($_GET['action']) {
|
switch ($_GET['action']) {
|
||||||
case 'faq':
|
case 'faq':
|
||||||
$tpl = $m->loadTemplate('faq_html');
|
$tpl = $ho->setTemplate('faq_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
break;
|
break;
|
||||||
case 'send':
|
case 'send':
|
||||||
@ -95,13 +92,14 @@ if (!$tpl_done && isset($_SESSION['access_token']) && $_SESSION['access_token'])
|
|||||||
|
|
||||||
// Check $userdata->verifiedEmail and deny if not verified.
|
// Check $userdata->verifiedEmail and deny if not verified.
|
||||||
if (!$userdata->verifiedEmail) {
|
if (!$userdata->verifiedEmail) {
|
||||||
$tpl = $m->loadTemplate('notverified_html');
|
$tpl = $ho->setTemplate('notverified_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
} else {
|
} else {
|
||||||
switch ($_GET['action']) {
|
switch ($_GET['action']) {
|
||||||
case 'send':
|
case 'send':
|
||||||
echo "This would send the mail...";
|
echo "This would send the mail...";
|
||||||
$mtpl = $m->loadTemplate('mail_' . $form_type);
|
$mo = new MailOutput(dirname(__FILE__) . '/templates');
|
||||||
|
$mo->setTemplate('mail_' . $form_type);
|
||||||
$action = $form_type;
|
$action = $form_type;
|
||||||
$skey = 'form_' . $action;
|
$skey = 'form_' . $action;
|
||||||
$data['action'] = $action;
|
$data['action'] = $action;
|
||||||
@ -114,9 +112,11 @@ if (!$tpl_done && isset($_SESSION['access_token']) && $_SESSION['access_token'])
|
|||||||
|
|
||||||
$data['email_date'] = date('r');
|
$data['email_date'] = date('r');
|
||||||
$data = array_merge($data, $fields);
|
$data = array_merge($data, $fields);
|
||||||
|
$mo->setTemplateVars($data);
|
||||||
|
|
||||||
$mail_html = $mtpl->render($data);
|
$mail_html = $mo->getRenderedOutput();
|
||||||
list($headers, $mailbody) = preg_split('/\r?\n\r?\n/', $mail_html, 2);
|
list($headers, $mailbody) = preg_split('/\r?\n\r?\n/', $mail_html, 2);
|
||||||
|
|
||||||
echo '<hr/>'.$headers.'<hr/>'.$mailbody;
|
echo '<hr/>'.$headers.'<hr/>'.$mailbody;
|
||||||
$header_lines = preg_split('/\r?\n/', $headers);
|
$header_lines = preg_split('/\r?\n/', $headers);
|
||||||
$header_filtered = '';
|
$header_filtered = '';
|
||||||
@ -141,30 +141,30 @@ if (!$tpl_done && isset($_SESSION['access_token']) && $_SESSION['access_token'])
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'event':
|
case 'event':
|
||||||
$tpl = $m->loadTemplate('event_html');
|
$tpl = $ho->setTemplate('event_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
break;
|
break;
|
||||||
case 'hotel':
|
case 'hotel':
|
||||||
$tpl = $m->loadTemplate('hotel_html');
|
$tpl = $ho->setTemplate('hotel_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
$action = 'hotel';
|
$action = 'hotel';
|
||||||
require 'prep_form.php';
|
require 'prep_form.php';
|
||||||
break;
|
break;
|
||||||
case 'restaurant':
|
case 'restaurant':
|
||||||
$tpl = $m->loadTemplate('restaurant_html');
|
$tpl = $ho->setTemplate('restaurant_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
$action = 'restaurant';
|
$action = 'restaurant';
|
||||||
require 'prep_form.php';
|
require 'prep_form.php';
|
||||||
break;
|
break;
|
||||||
case 'rentalcar':
|
case 'rentalcar':
|
||||||
$tpl = $m->loadTemplate('rentalcar_html');
|
$tpl = $ho->setTemplate('rentalcar_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
$action = 'rentalcar';
|
$action = 'rentalcar';
|
||||||
require 'prep_form.php';
|
require 'prep_form.php';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (!$tpl_done) {
|
if (!$tpl_done) {
|
||||||
$tpl = $m->loadTemplate('loggedin_html');
|
$tpl = $ho->setTemplate('loggedin_html');
|
||||||
$tpl_done = true;
|
$tpl_done = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -173,8 +173,10 @@ if (!$tpl_done && isset($_SESSION['access_token']) && $_SESSION['access_token'])
|
|||||||
} elseif (!$tpl_done) {
|
} elseif (!$tpl_done) {
|
||||||
// Not authenticated
|
// Not authenticated
|
||||||
$data['auth_url'] = $client->createAuthUrl();
|
$data['auth_url'] = $client->createAuthUrl();
|
||||||
$tpl = $m->loadTemplate('index_html');
|
$tpl = $ho->setTemplate('index_html');
|
||||||
}
|
}
|
||||||
|
|
||||||
$data['action'] = $_GET['action'];
|
$data['action'] = $_GET['action'];
|
||||||
echo $tpl->render($data);
|
|
||||||
|
$ho->addTemplateVars($data);
|
||||||
|
$ho->sendOutputToStdout();
|
||||||
|
51
lib/Frs/Output/GenericOutput.php
Normal file
51
lib/Frs/Output/GenericOutput.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Frs\Output;
|
||||||
|
|
||||||
|
class GenericOutput
|
||||||
|
{
|
||||||
|
private $templatesPath;
|
||||||
|
private $partialsPath;
|
||||||
|
private $templateEngine;
|
||||||
|
private $template;
|
||||||
|
private $templateVars = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new output object for generic output.
|
||||||
|
*
|
||||||
|
* @param string $templatesPath Path to templates. Must be a folder, no slash at end!
|
||||||
|
* @param string $partialsPath Path to partials (relative to $templatesPath). Must be a folder, no slash at end!
|
||||||
|
*/
|
||||||
|
public function __construct($templatesPrefix = 'templates', $partialsPrefix = 'partials')
|
||||||
|
{
|
||||||
|
$this->templatesPrefix = $templatesPrefix;
|
||||||
|
$this->partialsPrefix = $templatesPrefix . DIRECTORY_SEPARATOR . $partialsPrefix;
|
||||||
|
$this->templateEngine = new \Mustache_Engine(array(
|
||||||
|
'loader' => new \Mustache_Loader_FilesystemLoader($this->templatesPrefix),
|
||||||
|
'partials_loader' => new \Mustache_Loader_FilesystemLoader($this->partialsPrefix),
|
||||||
|
'charset' => 'utf-8',
|
||||||
|
'logger' => new \Mustache_Logger_StreamLogger('php://stderr'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTemplate($templateName)
|
||||||
|
{
|
||||||
|
$this->template = $this->templateEngine->loadTemplate($templateName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addTemplateVar($key, $value)
|
||||||
|
{
|
||||||
|
$this->templateVars[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addTemplateVars($tplVars)
|
||||||
|
{
|
||||||
|
// maybe use array_merge_recursive one day... but currently I think this is better
|
||||||
|
$this->templateVars = array_merge($this->templateVars, $tplVars);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRenderedOutput()
|
||||||
|
{
|
||||||
|
return $this->template->render($this->templateVars);
|
||||||
|
}
|
||||||
|
}
|
13
lib/Frs/Output/HtmlOutput.php
Normal file
13
lib/Frs/Output/HtmlOutput.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Frs\Output;
|
||||||
|
|
||||||
|
use \Frs\Output\GenericOutput;
|
||||||
|
|
||||||
|
class HtmlOutput extends GenericOutput
|
||||||
|
{
|
||||||
|
public function sendOutputToStdout()
|
||||||
|
{
|
||||||
|
echo $this->getRenderedOutput();
|
||||||
|
}
|
||||||
|
}
|
9
lib/Frs/Output/MailOutput.php
Normal file
9
lib/Frs/Output/MailOutput.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Frs\Output;
|
||||||
|
|
||||||
|
use \Frs\Output\GenericOutput;
|
||||||
|
|
||||||
|
class MailOutput extends GenericOutput
|
||||||
|
{
|
||||||
|
}
|
Reference in New Issue
Block a user