forked from joepie91/cphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.base.php
163 lines (149 loc) · 4.35 KB
/
class.base.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
<?php
/*
* CPHP is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if($_CPHP !== true) { die(); }
class CPHPBaseClass
{
public $render_template = "";
public function RenderTimeAgo($template, $property)
{
/* DEPRECATED: Please do not use this function if you can avoid it.
* A function offering similar functionality will be added soon. */
global $locale;
$variable_name = "s{$property}";
if(isset($this->$variable_name) && is_numeric($this->$variable_name))
{
$timestamp = $this->$variable_name;
if($timestamp > time())
{
$sTimeAgo = $locale->strings['event-future'];
}
elseif($timestamp == time())
{
$sTimeAgo = $locale->strings['event-now'];
}
else
{
$date1 = new DateTime("@{$timestamp}", new DateTimeZone("GMT"));
$date2 = new DateTime("now", new DateTimeZone("GMT"));
$interval = $date1->diff($date2);
$years = (int)$interval->format("%G");
$months = (int)$interval->format("%m");
$weeks = (int)$interval->format("%U");
$days = (int)$interval->format("%d");
$hours = (int)$interval->format("%H");
$minutes = (int)$interval->format("%i");
$seconds = (int)$interval->format("%S");
if($years > 1)
{
$sTimeAgo = sprintf($locale->strings['event-years-ago'], $years);
}
elseif($years == 1)
{
$sTimeAgo = $locale->strings['event-1year-ago'];
}
elseif($months > 1)
{
$sTimeAgo = sprintf($locale->strings['event-months-ago'], $months);
}
elseif($months == 1)
{
$sTimeAgo = $locale->strings['event-1month-ago'];
}
elseif($weeks > 1)
{
$sTimeAgo = sprintf($locale->strings['event-weeks-ago'], $weeks);
}
elseif($weeks == 1)
{
$sTimeAgo = $locale->strings['event-1week-ago'];
}
elseif($days > 1)
{
$sTimeAgo = sprintf($locale->strings['event-days-ago'], $days);
}
elseif($days == 1)
{
$sTimeAgo = $locale->strings['event-1day-ago'];
}
elseif($hours > 1)
{
$sTimeAgo = sprintf($locale->strings['event-hours-ago'], $hours);
}
elseif($hours == 1)
{
$sTimeAgo = $locale->strings['event-1hour-ago'];
}
elseif($minutes > 1)
{
$sTimeAgo = sprintf($locale->strings['event-minutes-ago'], $minutes);
}
elseif($minutes == 1)
{
$sTimeAgo = $locale->strings['event-1minute-ago'];
}
elseif($seconds > 1)
{
$sTimeAgo = sprintf($locale->strings['event-seconds-ago'], $seconds);
}
elseif($seconds == 1)
{
$sTimeAgo = $locale->strings['event-1second-ago'];
}
else
{
// If you see this, there's probably something wrong.
$sTimeAgo = $locale->strings['event-past'];
}
}
$sDate = local_from_unix($timestamp, $locale->datetime_long);
return $this->RenderTemplateExternal($template, array(
'local-time' => $sDate,
'time-ago' => $sTimeAgo,
'timestamp' => $timestamp
));
}
else
{
$classname = get_class($this);
throw new Exception("Property {$classname}.{$property} does not exist or is not of a valid format.");
}
}
public function RenderTemplateExternal($template, $strings)
{
/* DEPRECATED: Please do not use this function.
* Instead, you can use Templater::AdvancedParse for rendering arbitrary templates
* without instantiating a Templater yourself. */
return $this->DoRenderTemplate($template, $strings);
}
public function DoRenderTemplate($template, $strings)
{
/* DEPRECATED: Please do not use this function.
* Class-specific templater functions have been discontinued. Instead, you can use
* Templater::AdvancedParse for rendering templates without instantiating a Templater
* yourself. */
global $locale;
try
{
$tpl = new Templater();
$tpl->Load($template);
$tpl->Localize($locale->strings);
$tpl->Compile($strings);
return $tpl->Render();
}
catch(Exception $e)
{
$classname = get_class($this);
throw new Exception("Failed to render template {$classname}.{$template}.");
}
}
}