forked from gizmore/phpgdo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGDT_Redirect.php
159 lines (140 loc) · 3.33 KB
/
GDT_Redirect.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
<?php
namespace GDO\UI;
use GDO\Core\GDT;
use GDO\Core\Application;
use GDO\Session\GDO_Session;
use GDO\Core\Website;
/**
* A redirect.
* Renders <script> in ajax mode.
* Sets headers in html and can display a message after a redirect (session required).
*
* @TODO rename html to http rendering?
*
* @author gizmore
* @version 7.0.1
* @since 6.11.5
*/
final class GDT_Redirect extends GDT
{
use WithHREF;
use WithText;
const CODE = 307;
public static bool $REDIRECTED = false; # Only once
/**
* @param string $href
*/
public static function to(string $href) : void
{
$top = GDT_Page::instance()->topResponse();
$top->addField(GDT_Redirect::make()->href($href));
}
############
### Back ###
############
public function back() : self
{
return $this->href(self::hrefBack());
}
/**
* Try to get a referrer URL for hrefBack.
*/
public static function hrefBack(string $default=null) : string
{
if (Application::$INSTANCE->isCLI())
{
return $default ? $default : hrefDefault();
}
$sess = null;
if (class_exists('GDO\\Session\\GDO_Session', false))
{
$sess = GDO_Session::instance();
}
if ( (!$sess) || (!($url = $sess->getLastURL())) )
{
$url = isset($_SERVER['HTTP_REFERER']) ?
$_SERVER['HTTP_REFERER'] :
($default ? $default : hrefDefault());
}
return $url;
}
############
### Time ###
############
public int $redirectTime = 0;
public function redirectTime(int $redirectTime=8) : self
{
$this->redirectTime = $redirectTime;
return $this;
}
#############
### Flash ###
#############
public function redirectError(string $key, array $args=null, bool $log=true) : self
{
Website::error(t('redirect'), $key, $args, $log, self::CODE);
// $error = GDT_Error::make()->code(self::CODE)->text($key, $args);
// GDT_Page::instance()->topResponse()->addField($error);
if (module_enabled('Session'))
{
$app = Application::$INSTANCE;
if ($app->isWebserver())
{
GDO_Session::set('redirect_error', t($key, $args));
}
}
return $this;
}
public function redirectMessage(string $key, array $args=null, bool $log=true) : self
{
Website::message(t('redirect'), $key, $args, $log, self::CODE);
// $success = GDT_Success::make()->code(self::CODE)->text($key, $args);
// GDT_Page::instance()->topResponse()->addField($success);
if (module_enabled('Session'))
{
$app = Application::$INSTANCE;
if ($app->isWebserver())
{
GDO_Session::set('redirect_message', t($key, $args));
}
}
return $this;
}
##############
### Render ###
##############
public function renderCLI() : string
{
return GDT::EMPTY_STRING;
}
public function renderHTML() : string
{
$app = Application::$INSTANCE;
// $ajax = '';
$url = isset($this->href) ? $this->href : $this->hrefBack();
if ($app->isAjax())
{
// $ajax = $this->renderAjaxRedirect();
}
else
{
$time = $this->redirectTime;
if ($time > 0)
{
hdr("Refresh: {$time}; url={$url}");
}
else
{
hdr("Location: {$url}");
}
}
$link = GDT_Link::make()->href($url);
$link = $link->render();
return GDT_Panel::make()->text('gdt_redirect_to', [$link])->render();
}
private function renderAjaxRedirect()
{
return sprintf('<script>setTimeout(function(){ window.location.href="%s" }, %d);</script>',
$this->href, $this->redirectTime * 1000);
}
}