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
|
<?php
# Lifter010: TODO
/**
* DateFormater.class.php - Handles the formatting of one date and associated rooms.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author sbrummer <soenke.brummerloh@uni-osnabrueck.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
/**
* Formates one SingleDate object or a series of SingleDate objects into a nice format.
*/
class DateFormatter {
/**
* @var array holds the dates use for formatting
*/
private $dates;
/**
* @var string holds the return-type, may be int or string
*/
private $return_mode;
/**
* @param $dates an array with an array of SingleDate objects.
* @param string $return_mode expected values are 'int', 'string' and 'export'. The default value is 'string'.
* @return void
*/
private function __construct($dates, $return_mode = 'string')
{
$this->dates = $dates;
$this->return_mode = $return_mode;
}
/**
* Formats one single date into a nice format.
* @static
* @param $date SingleDate object
* @param string $return_mode expected values are 'int', 'string' and 'export'. The default value is 'string'.
* @return string
*/
public static function formatDateAndRoom($date, $return_mode = 'string')
{
$dates = DateFormatter::wrapDateWithArray($date);
return DateFormatter::formatDateWithAllRooms($dates, $return_mode);
}
/**
* Formats a series of SingleDate objects into a nice format. The dates parameter is an array of dates.
* The array has to have the key 'termin' with an array of SingleDate objects as value.
* @static
* @param $dates an array with an array of SingleDate objects
* @param string $return_mode expected values are 'int', 'string' and 'export'. The default value is 'string'.
* @return string
*/
public static function formatDateWithAllRooms($dates, $return_mode = 'string')
{
$dateFormatter = new DateFormatter($dates, $return_mode);
return $dateFormatter->internalFormatDateWithAllRooms();
}
private static function wrapDateWithArray($date)
{
$dates = [];
$dates['termin'] = [$date];
return $dates;
}
private function internalFormatDateWithAllRooms()
{
$dateWithRooms = '';
if ($this->dates['termin']) {
// if we have multiple rooms at the same time we display them all
foreach ($this->dates['termin'] as $num => $termin_id) {
$date = new SingleDate($termin_id);
// if we want an int and format the date ourself
if ($this->return_mode == 'int') {
return $date->getStartTime();
}
$isFirstDate = ($num == 0);
if ($isFirstDate) {
$dateWithRooms = $this->internalFormatDateAndRoom($date);
} else {
$dateWithRooms .= ', ' . $this->formatRoom($date);
}
}
}
return $dateWithRooms;
}
private function internalFormatDateAndRoom($date)
{
$ret = $this->formatDate($date);
if ($this->return_mode != 'int') {
$formatedRooms = $this->formatRoom($date);
if ($formatedRooms) {
$ret .= ', ';
$ret .= _("Ort:") . ' ';
$ret .= $formatedRooms;
}
}
return $ret;
}
private function formatDate($date)
{
if ($this->return_mode == 'int') {
return $date->getStartTime();
}
else {
return $date->toString();
}
}
private function formatRoom($date)
{
if ($this->return_mode == 'int') {
return '';
}
else {
return $this->formatLocationText($date);
}
}
private function formatLocationText($date)
{
if ($this->hasResource($date)) {
$resObj = Resource::find($date->getResourceID());
return $this->generateLocationTextFromResourceObject($resObj);
} else if ($this->hasFreeRoomText($date)) {
return $this->generateLocationTextFromFreeRoomText($date);
} else {
return '';
}
}
private function generateLocationTextFromResourceObject($resObj)
{
if ($resObj) {
if ($this->return_mode == 'string') {
return '<a href="' . $resObj->getActionLink() . '" data-dialog="1">'
. htmlReady($resObj->name)
. '</a>';
}
else {
return htmlReady($resObj->name);
}
}
return '';
}
private function generateLocationTextFromFreeRoomText($date)
{
return '(' . htmlReady($date->getFreeRoomText()) . ')';
}
private function hasResource($date)
{
return $date && $date->getResourceID();
}
private function hasFreeRoomText($date)
{
return $date && $date->getFreeRoomText();
}
}
|