Skip to content

Commit

Permalink
Add timed redirect r
Browse files Browse the repository at this point in the history
  • Loading branch information
DorsetDigital committed Feb 4, 2021
1 parent f932c30 commit e8b62b4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Screenshots of the QR preview, redirect editing and rule editing screens can be

* Add internationalisation to all fields / content
* Complete time-based redirection rule
* Add developer-configurable base URL for redirects
* Suppress rule confguration data in CMS
* Add ability to enable / disable specific rule types using the configuration API
* Add download option for generated QR at large size / high res
Expand Down
3 changes: 3 additions & 0 deletions src/Model/RedirectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DorsetDigital\SmartRedirect\Model\Rule\DefaultRule;
use DorsetDigital\SmartRedirect\Model\Rule\LanguageRule;
use DorsetDigital\SmartRedirect\Model\Rule\LocationRule;
use DorsetDigital\SmartRedirect\Model\Rule\TimedRuled;

class RedirectFactory
{
Expand All @@ -27,6 +28,8 @@ public function getRule(RedirectRule $rule = null, $ruleType = null)
return new LocationRule($rule);
case 'language':
return new LanguageRule($rule);
case 'timed':
return new TimedRuled($rule);
default:
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Model/RedirectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

class RedirectRule extends DataObject
{
private static $table_name = 'QRRedirectRule';
private static $table_name = 'SmartRedirectRule';
private static $db = [
'RuleType' => 'Enum("Default,Location,Language")',
'RuleType' => 'Enum("Default,Location,Language,Timed")',
'RuleConfig' => 'Text',
'RedirectTo' => 'Varchar(255)',
'SortOrder' => 'Int'
Expand Down
79 changes: 79 additions & 0 deletions src/Model/Rule/TimedRuled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php


namespace DorsetDigital\SmartRedirect\Model\Rule;


use DorsetDigital\SmartRedirect\Model\RedirectBase;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\ORM\FieldType\DBField;

class TimedRuled extends RedirectBase
{

public function checkRule()
{
$config = json_decode($this->getConfig(), true);
if (($this->isValidRedirect() === false) || (!$config) || (!isset($config['from'])) || (!isset($config['to']))) {
return false;
}
return $this->inDateRange($config['from'], $config['to']);
}

private function inDateRange($from, $to)
{
$now = time();
$start = strtotime($from);
$end = strtotime($to);

if (($now > $start) && ($now < $end)) {
return true;
}

return false;
}


public function getFormFields()
{
$fields = parent::getFormFields();

$configJSON = $this->getConfig();
$config = json_decode($configJSON, true);

$fields[] = DatetimeField::create('FromTime', 'Start date/time');
$fields[] = DatetimeField::create('ToTime', 'End date/time');

return $fields;
}


public function buildConfigData($obj, HTTPRequest $request = null)
{
if ($request) {
$from = $request->postVar('FromTime') ?: [];
$to = $request->postVar('ToTime');
}
$config = [
'from' => $from,
'to' => $to
];
return json_encode($config);
}

public function getSummaryDescription()
{
$format = 'jS M Y - H:i';
$config = json_decode($this->getConfig(), true);
$from = '';
$to = '';
if (($config) && (isset($config['from'])) && (isset($config['to']))) {
$from = date($format, strtotime($config['from']));
$to = date($format, strtotime($config['to']));
}

return DBField::create_field('HTMLFragment', "From: <em>" . $from . "</em> To: <em>" . $to . "</em>");
}

}

0 comments on commit e8b62b4

Please sign in to comment.