-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataModel.php
262 lines (213 loc) · 7.88 KB
/
DataModel.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* @file
* Used to create the model for the data processing application
*/
/**
* Verb Noun.
*/
class DateModel {
public $contents;
public $variables;
public $input;
public function __construct() {
$this->contents = array();
$this->variables = array();
$this->input = array();
}
// loads in values used in page generation
public function page_basics() {
$variables = $this->variables; // first arg is consdered to be the variables
$input = $this->input; // second arg is consdered to be the input from the user
$this->contents['page__content'] = '';
foreach ($variables as $key => $value) {
$this->contents[$key] = $value;
}
ksort($this->contents); // make sure $this->contents['page__content'] comes in first
// fill in the empty ones
if (!isset($this->contents['page_home'])) {
$this->contents['page_home'] = 'Welcome to the Date Calculator';
}
if (!isset($this->contents['page_navigation'])) {
$this->contents['page_navigation'] = ''; // nothing for now
}
if (!isset($this->contents['page_title'])) {
$this->contents['page_title'] = 'Date Calculator';
}
if (!isset($this->contents['page_path'])) {
$this->contents['page_path'] = $_SERVER['REQUEST_URI']; // get the current working path... if not set elsewhere and otherwise
}
if (!isset($this->contents['document_path'])) {
$this->contents['document_path'] = $this->get_path($this->contents['page_path']).'/template/'; // get the current document path
}
}
// models the form used in this application
public function page_form() {
$variables = $this->variables; // first arg is consdered to be the variables
$input = $this->input; // second arg is consdered to be the input from the user
$output = array();
// the namings for the date fields in case this needs to the swapped to be something dynamic or different later
$first = 'date_1';
$second = 'date_2';
// the lines of html output as we're working with function assumptions that this is a page and a form
$output[] = '<form action="'.$this->contents['page_path'].'" method="GET">';
$output[] = '<input type="hidden" name="action" value="'.htmlspecialchars($input['action']).'" />';
$output[] = $this->check_input($input[$first], $input[$second]);
$output[] = sprintf('<span class="date_diffs"><input type="datetime" class="date_diffs" name="%s" value="%s" /></span>', $first, htmlspecialchars($input[$first]));
$output[] = $this->calculate_difference($input[$first], $input[$second]);
$output[] = sprintf('<span class="date_diffs"><input type="datetime" class="date_diffs" name="%s" value="%s" /></span>', $second, htmlspecialchars($input[$second]));
$output[] = '<input type="submit" value="Calculate" />';
$output[] = '</form>';
$this->contents['page__content'] = implode("\n", $output); // imploded with line breaks for client side easy reading
}
private function get_path($path) {
// work from the assumption that the current page path ends in a uri / script call. toss that last element.
$paths = explode("/", $path);
array_pop($paths);
return implode("/", $paths);
}
// a server side looksee at the inputs
private function check_input() {
$args = func_get_args();
$output = " ";
// if there were multiples this could become a while loop
if (sizeof($args) > 1) {
// we have enough to work with
$first = array_shift($args); // first arg for the date_1 value
$second = array_shift($args); // second arg for the date_2 value
// taking the lengths as an indicator that the user made some attempt to input information
if ((strlen($first) > 2) && (strlen($second) > 2)) {
$first_date = $this->parse_date($first);
$second_date = $this->parse_date($second);
if ((array_key_exists('error', $first_date)) || (array_key_exists('error', $second_date))) {
$output = sprintf("try again. \n %s %s", $first_date['error'], $second_date['error']);
}
}
}
return '<span class="date_notice">'.$output.'</span>';
}
// a server side calculation of the differences
public function calculate_difference() {
define ('DAY', 86400);
$args = func_get_args();
$output = " ";
// if there were multiples this could become a while loop
if (sizeof($args) > 1) {
// we have enough to work with
$first = array_shift($args); // first arg for the date_1 value
$second = array_shift($args); // second arg for the date_2 value
$first_date = $this->parse_date($first);
$second_date = $this->parse_date($second);
if ((array_key_exists('date', $first_date)) && (array_key_exists('date', $second_date))) {
$diff = abs(strtotime($first_date['date']) - strtotime($second_date['date']));
$day_diff = floor($diff / DAY);
$output = sprintf(" is %d days from ", $day_diff);
}
}
return '<span class="date_diffs">'.$output.'</span>';
}
public function parse_date($date) {
/*
The form input type is datetime.
One Firefox, IE and pre-HTML5 capable browsers, the "now" and "today" can be used as input
*/
if ($date == "now") {
$date = date('Y-m-d H:i:s');
}
if ($date == "today") {
$date = date('Y-m-d');
}
// the original function to fulfill this role
// $date_array = date_parse($date);
// look for patterns
// year-month-day
$year = false;
$month = false;
$day = false;
if (preg_match('/(\d{4})[ |-](\d{2})[ |-](\d{2})/', $date, $matches)) {
$year = $matches[1];
$second = $matches[2];
$third = $matches[3];
if (intval($second) > 12) {
// good guess this is a month then day
$day = $second;
$month = $third;
}
else {
// good guess this is a day then month
$month = $second;
$day = $third;
}
}
// still don't have something
if ($month == false) {
$month_array = array(1 => 'jan', 2 => 'feb', 3 => 'mar', 4 => 'apr',
5 => 'may', 6 => 'jun', 7 => 'jul', 8 => 'aug',
9 => 'sep', 10 => 'oct', 11 => 'nov', 12 => 'dec');
$day_tidy = array(0 => array(1,2,3,4,5,6,7,8,9,0),
1 => array("1st","2nd","3rd","4th","5th","6th","7th","8th","9th", "0th"));
if (preg_match('/([\w]+)/', $date, $matches)) {
$raw_month = $matches[1]." ";
$short_month = strtolower(substr($raw_month, 0, 3));
if ($month = array_search($short_month, $month_array)) {
$date = str_replace($raw_month, "", $date); // remove the month name
$date = str_replace($day_tidy[1], $day_tidy[0], $date); // remove the suffixes to days
}
}
if (preg_match('/([\d]+)[ |,]+([\d]+)/', $date, $matches)) {
$first = $matches[1];
$second = $matches[2];
if (intval($second) > 31) {
// good guess this is a day then year
$day = $first;
$year = $second;
}
else {
// good guess this is a year then day
$year = $first;
$day = $second;
}
}
}
// get the hours
if (preg_match('/(\d{2}):(\d{2})/', $date, $matches)) {
$hour = $matches[1];
$minute = $matches[2];
}
// final checks
if ($day > 31) {
$day = false;
}
if (strlen($year) != 4) {
$year = false;
}
$error = array();
if (!$year) {
$error[] = "Missing the year";
}
if (!$month) {
$error[] = "Missing a month";
}
if (!$day) {
$error[] = "Missing the day";
}
$error_count = sizeof($error);
$date_array = array(
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => 0
);
if ($error_count == 0) {
// print print_r($date_array, TRUE);
// print print_r(sprintf("%4d-%02d-%02d %02d:%02d:%02d", $date_array['year'], $date_array['month'], $date_array['day'], $date_array['hour'], $date_array['minute'], $date_array['second']), TRUE);
return array('date' => sprintf("%4d-%02d-%02d %02d:%02d:%02d", $date_array['year'], $date_array['month'], $date_array['day'], $date_array['hour'], $date_array['minute'], $date_array['second']));
}
else {
return array('error' => "<ul><li>".implode("</li><li>", $error)."</li></ul>");
}
}
}
?>