DailyMenus.php 10 KB
Newer Older
François Jacquet's avatar
François Jacquet committed
1 2
<?php

3 4
DrawHeader( ProgramTitle() );

5
if ( empty( $_REQUEST['month'] ) )
6 7 8 9
{
	$_REQUEST['month'] = date( 'm' );
}

10
if ( empty( $_REQUEST['year'] ) )
11 12 13
{
	$_REQUEST['year'] = date( 'Y' );
}
François Jacquet's avatar
François Jacquet committed
14 15

$last = 31;
François Jacquet's avatar
François Jacquet committed
16 17 18

while ( ! checkdate( $_REQUEST['month'], $last, $_REQUEST['year'] ) )
{
François Jacquet's avatar
François Jacquet committed
19
	$last--;
François Jacquet's avatar
François Jacquet committed
20
}
François Jacquet's avatar
François Jacquet committed
21

François Jacquet's avatar
François Jacquet committed
22 23
$time = mktime( 0, 0, 0, $_REQUEST['month'], 1, $_REQUEST['year'] );
$time_last = mktime( 0, 0, 0, $_REQUEST['month'], $last, $_REQUEST['year'] );
François Jacquet's avatar
François Jacquet committed
24

25 26 27 28 29 30 31 32 33 34 35
// Use default calendar.
$default_calendar_id = DBGetOne( "SELECT CALENDAR_ID
	FROM ATTENDANCE_CALENDARS
	WHERE SYEAR='" . UserSyear() . "'
	AND SCHOOL_ID='" . UserSchool() . "'
	AND DEFAULT_CALENDAR='Y'" );

if ( $default_calendar_id )
{
	$calendar_id = $default_calendar_id;
}
François Jacquet's avatar
François Jacquet committed
36 37
else
{
38 39 40 41 42 43 44 45 46
	$calendar_id = DBGetOne( "SELECT CALENDAR_ID
		FROM ATTENDANCE_CALENDARS
		WHERE SYEAR='" . UserSyear() . "'
		AND SCHOOL_ID='" . UserSchool() . "'" );

	if ( ! $calendar_id )
	{
		ErrorMessage( array( _( 'There are no calendars yet setup.' ) ), 'fatal' );
	}
François Jacquet's avatar
François Jacquet committed
47 48
}

49 50 51 52 53
$menus_RET = DBGet( "SELECT MENU_ID,TITLE
	FROM FOOD_SERVICE_MENUS
	WHERE SCHOOL_ID='" . UserSchool() . "'
	ORDER BY SORT_ORDER", array(), array( 'MENU_ID' ) );

54
if ( empty( $_REQUEST['menu_id'] ) )
François Jacquet's avatar
François Jacquet committed
55
{
François Jacquet's avatar
François Jacquet committed
56
	if ( empty( $_SESSION['FSA_menu_id'] ) )
François Jacquet's avatar
François Jacquet committed
57 58 59 60 61
	{
		if ( ! empty( $menus_RET ) )
		{
			$_REQUEST['menu_id'] = $_SESSION['FSA_menu_id'] = key( $menus_RET );
		}
François Jacquet's avatar
François Jacquet committed
62
		else
François Jacquet's avatar
François Jacquet committed
63 64 65 66
		{
			ErrorMessage( array( _( 'There are no menus yet setup.' ) ), 'fatal' );
		}
	}
François Jacquet's avatar
François Jacquet committed
67
	else
François Jacquet's avatar
François Jacquet committed
68
	{
François Jacquet's avatar
François Jacquet committed
69
		$_REQUEST['menu_id'] = $_SESSION['FSA_menu_id'];
François Jacquet's avatar
François Jacquet committed
70 71
	}
}
François Jacquet's avatar
François Jacquet committed
72
else
François Jacquet's avatar
François Jacquet committed
73
{
74
	$_SESSION['FSA_menu_id'] = $_REQUEST['menu_id'];
François Jacquet's avatar
François Jacquet committed
75
}
François Jacquet's avatar
François Jacquet committed
76

François Jacquet's avatar
François Jacquet committed
77
if ( ! empty( $_REQUEST['submit']['save'] )
François Jacquet's avatar
François Jacquet committed
78 79 80
	&& $_REQUEST['food_service']
	&& $_POST['food_service']
	&& AllowEdit() )
François Jacquet's avatar
François Jacquet committed
81
{
82
	$events_RET = DBGet( "SELECT ID,SCHOOL_DATE
83
	FROM CALENDAR_EVENTS
François Jacquet's avatar
François Jacquet committed
84 85 86 87
	WHERE SCHOOL_DATE BETWEEN '" . date( 'Y-m-d', $time ) . "' AND '" . date( 'Y-m-d', $time_last ) . "'
	AND SYEAR='" . UserSyear() . "'
	AND SCHOOL_ID='" . UserSchool() . "'
	AND TITLE='" . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . "'", array(), array( 'SCHOOL_DATE' ) );
François Jacquet's avatar
François Jacquet committed
88 89
	//echo '<pre>'; var_dump($events_RET); echo '</pre>';

François Jacquet's avatar
François Jacquet committed
90
	foreach ( (array) $_REQUEST['food_service'] as $school_date => $description )
François Jacquet's avatar
François Jacquet committed
91
	{
François Jacquet's avatar
François Jacquet committed
92
		if ( ! empty( $events_RET[$school_date] ) )
François Jacquet's avatar
François Jacquet committed
93
		{
François Jacquet's avatar
François Jacquet committed
94
			if ( ! empty( $description['text'] ) || ! empty( $description['select'] ) )
François Jacquet's avatar
François Jacquet committed
95
			{
François Jacquet's avatar
François Jacquet committed
96 97 98
				DBQuery( "UPDATE CALENDAR_EVENTS
					SET DESCRIPTION='" . $description['text'] . $description['select'] . "'
					WHERE ID='" . $events_RET[$school_date][1]['ID'] . "'" );
François Jacquet's avatar
François Jacquet committed
99
			}
François Jacquet's avatar
François Jacquet committed
100
			else
François Jacquet's avatar
François Jacquet committed
101
			{
François Jacquet's avatar
François Jacquet committed
102 103
				DBQuery( "DELETE FROM CALENDAR_EVENTS
					WHERE ID='" . $events_RET[$school_date][1]['ID'] . "'" );
François Jacquet's avatar
François Jacquet committed
104 105
			}
		}
François Jacquet's avatar
François Jacquet committed
106
		elseif ( ! empty( $description['text'] ) || ! empty( $description['select'] ) )
François Jacquet's avatar
François Jacquet committed
107
		{
François Jacquet's avatar
François Jacquet committed
108 109 110 111
			DBQuery( "INSERT INTO CALENDAR_EVENTS (ID,SYEAR,SCHOOL_ID,SCHOOL_DATE,TITLE,DESCRIPTION)
				VALUES(" . db_seq_nextval( 'calendar_events_id_seq' ) . ",'" . UserSyear() . "','" .
				UserSchool() . "','" . $school_date . "','" . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . "','" .
				$description['text'] . issetVal( $description['select'], '' ) . "')" );
François Jacquet's avatar
François Jacquet committed
112
		}
François Jacquet's avatar
François Jacquet committed
113
	}
François Jacquet's avatar
François Jacquet committed
114 115 116

	// Unset food_service & redirect URL.
	RedirectURL( 'food_service' );
François Jacquet's avatar
François Jacquet committed
117 118
}

119
if ( ! empty( $_REQUEST['submit']['print'] ) )
François Jacquet's avatar
François Jacquet committed
120
{
121
	$events_RET = DBGet( "SELECT TITLE,DESCRIPTION,SCHOOL_DATE
122
	FROM CALENDAR_EVENTS
François Jacquet's avatar
François Jacquet committed
123 124 125 126
	WHERE SCHOOL_DATE BETWEEN '" . date( 'Y-m-d', $time ) . "' AND '" . date( 'Y-m-d', $time_last ) . "'
	AND SYEAR='" . UserSyear() . "'
	AND SCHOOL_ID='" . UserSchool() . "'
	AND (TITLE='" . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . "' OR TITLE='No School')", array(), array( 'SCHOOL_DATE' ) );
François Jacquet's avatar
François Jacquet committed
127

François Jacquet's avatar
François Jacquet committed
128
	$skip = date( "w", $time );
François Jacquet's avatar
François Jacquet committed
129

130 131
	echo '<br /><table class="width-100p">';

132
	if ( ! empty( $_REQUEST['_ROSARIO_PDF'] ) )
133 134 135 136
	{
		// Landscape PDF.
		$_SESSION['orientation'] = 'landscape';

François Jacquet's avatar
François Jacquet committed
137
		if ( is_file( 'assets/dailymenu' . UserSchool() . '.jpg' ) )
François Jacquet's avatar
François Jacquet committed
138
		{
139
			echo '<tr class="center"><td colspan="3"><img src="assets/dailymenu' . UserSchool() . '.jpg" /></td></tr>';
François Jacquet's avatar
François Jacquet committed
140 141
		}
		else
François Jacquet's avatar
François Jacquet committed
142
		{
143
			echo '<tr class="center"><td colspan="3"><b class="sizep2">' . SchoolInfo( 'TITLE' ) . '</b></td></tr>';
François Jacquet's avatar
François Jacquet committed
144
		}
145 146 147
	}

	//FJ display locale with strftime()
François Jacquet's avatar
François Jacquet committed
148
	echo '<tr class="center"><td>' . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . '</td>
149
		<td><b class="sizep2">' . ProperDate( date( 'Y-m-d', mktime( 0, 0, 0, $_REQUEST['month'], 1, $_REQUEST['year'] ) ) ) . '</b></td>
François Jacquet's avatar
François Jacquet committed
150
		<td>' . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . '</td></tr></table>';
151 152 153 154

	echo '<table id="calendar" class="width-100p valign-top"><thead><tr class="center">';

	echo '<th>' . _( 'Sunday' ) . '</th>' .
François Jacquet's avatar
François Jacquet committed
155 156 157 158 159 160
	'<th>' . _( 'Monday' ) . '</th>' .
	'<th>' . _( 'Tuesday' ) . '</th>' .
	'<th>' . _( 'Wednesday' ) . '</th>' .
	'<th>' . _( 'Thursday' ) . '</th>' .
	'<th>' . _( 'Friday' ) . '</th>' .
	'<th>' . _( 'Saturday' ) . '</th>';
161

François Jacquet's avatar
François Jacquet committed
162
	echo '</tr></thead><tbody>';
François Jacquet's avatar
François Jacquet committed
163

164 165 166 167
	if ( $skip )
	{
		echo '<tr><td colspan="' . $skip . '" class="calendar-skip">&nbsp;</td>';
	}
François Jacquet's avatar
François Jacquet committed
168

169
	for ( $i = 1; $i <= $last; $i++ )
François Jacquet's avatar
François Jacquet committed
170
	{
François Jacquet's avatar
François Jacquet committed
171 172
		if ( $skip % 7 == 0 )
		{
François Jacquet's avatar
François Jacquet committed
173
			echo '<tr>';
François Jacquet's avatar
François Jacquet committed
174
		}
175

François Jacquet's avatar
François Jacquet committed
176
		$day_time = mktime( 0, 0, 0, $_REQUEST['month'], $i, $_REQUEST['year'] );
177

François Jacquet's avatar
François Jacquet committed
178
		$date = date( 'Y-m-d', $day_time );
François Jacquet's avatar
François Jacquet committed
179

180 181 182
		$day_classes = '';

		// Thursdays, Fridays, Saturdays.
François Jacquet's avatar
François Jacquet committed
183 184 185

		if (  ( $i + 1 ) % 7 === 0
			|| ( $i + 1 ) % 7 > 4 )
186 187 188 189 190 191
		{
			$day_classes .= ' thu-fri-sat';
		}

		$day_inner_classes = 'width-100p';

François Jacquet's avatar
François Jacquet committed
192
		if ( ! empty( $events_RET[$date] ) )
193 194 195 196 197 198 199
		{
			$day_inner_classes .= ' hover';
		}

		$day_number_classes = 'number';

		// Bold class
François Jacquet's avatar
François Jacquet committed
200 201 202

		if ( ! empty( $events_RET[$date] )
			|| ! empty( $assignments_RET[$date] ) )
203 204 205 206
		{
			$day_number_classes .= ' bold';
		}

François Jacquet's avatar
François Jacquet committed
207
		echo '<td class="calendar-day' . $day_classes . '" style="background-color:' . ( ! empty( $events_RET[$date] ) ? '#ffaaaa;' : '#fff' ) . '">
208 209 210 211
			<table class="' . $day_inner_classes . '">
				<tr><td class="' . $day_number_classes . '">' . $i . '</td></tr>';

		echo '<tr><td class="calendar-menu">';
François Jacquet's avatar
François Jacquet committed
212

François Jacquet's avatar
François Jacquet committed
213
		if ( ! empty( $events_RET[$date] ) )
François Jacquet's avatar
François Jacquet committed
214
		{
François Jacquet's avatar
François Jacquet committed
215
			foreach ( (array) $events_RET[$date] as $event )
François Jacquet's avatar
François Jacquet committed
216
			{
François Jacquet's avatar
François Jacquet committed
217 218 219 220 221 222
				if ( $event['TITLE'] != $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] )
				{
					echo '<i>' . $event['TITLE'] . '</i><br />';
				}

				echo htmlspecialchars( $event['DESCRIPTION'], ENT_QUOTES );
François Jacquet's avatar
François Jacquet committed
223 224
			}
		}
François Jacquet's avatar
François Jacquet committed
225

François Jacquet's avatar
François Jacquet committed
226
		echo '</td></tr></table></td>';
François Jacquet's avatar
François Jacquet committed
227 228 229

		$skip++;

230 231
		if ( $skip % 7 == 0 )
		{
François Jacquet's avatar
François Jacquet committed
232
			echo '</tr>';
233 234 235 236 237 238
		}
	}

	if ( $skip % 7 != 0 )
	{
		echo '<td colspan="' . ( 7 - $skip % 7 ) . '" class="calendar-skip">&nbsp;</td></tr>';
François Jacquet's avatar
François Jacquet committed
239 240
	}

François Jacquet's avatar
François Jacquet committed
241
	echo '</tbody></table></p>';
François Jacquet's avatar
François Jacquet committed
242 243 244
}
else
{
François Jacquet's avatar
François Jacquet committed
245 246
	$description_select = '';

François Jacquet's avatar
François Jacquet committed
247
	if ( AllowEdit() )
François Jacquet's avatar
François Jacquet committed
248
	{
François Jacquet's avatar
François Jacquet committed
249 250 251 252 253 254 255
		$description_RET = DBGet( "SELECT DISTINCT DESCRIPTION
			FROM CALENDAR_EVENTS
			WHERE SYEAR='" . UserSyear() . "'
			AND SCHOOL_ID='" . UserSchool() . "'
			AND TITLE='" . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . "'
			AND DESCRIPTION IS NOT NULL
			ORDER BY DESCRIPTION" );
François Jacquet's avatar
François Jacquet committed
256 257

		if ( ! empty( $description_RET ) )
François Jacquet's avatar
François Jacquet committed
258
		{
François Jacquet's avatar
François Jacquet committed
259
			$description_select .= '<option value="">' . _( 'or select previous meal' ) . '</option>';
François Jacquet's avatar
François Jacquet committed
260 261 262 263 264 265

			foreach ( (array) $description_RET as $description )
			{
				$description_select .= '<option value="' . $description['DESCRIPTION'] . '">' . $description['DESCRIPTION'] . '</option>';
			}

François Jacquet's avatar
François Jacquet committed
266
			$description_select .= '</select>';
François Jacquet's avatar
François Jacquet committed
267 268 269
		}
	}

270
	$calendar_RET = DBGet( "SELECT SCHOOL_DATE
271
	FROM ATTENDANCE_CALENDAR
François Jacquet's avatar
François Jacquet committed
272 273 274 275
	WHERE SCHOOL_DATE BETWEEN '" . date( 'Y-m-d', $time ) . "' AND '" . date( 'Y-m-d', $time_last ) . "'
	AND SYEAR='" . UserSyear() . "'
	AND SCHOOL_ID='" . UserSchool() . "'
	AND CALENDAR_ID='" . $calendar_id . "'
276
	AND MINUTES>0
François Jacquet's avatar
François Jacquet committed
277
	ORDER BY SCHOOL_DATE", array(), array( 'SCHOOL_DATE' ) );
278

279
	$events_RET = DBGet( "SELECT ID,TITLE,DESCRIPTION,SCHOOL_DATE
280
	FROM CALENDAR_EVENTS
François Jacquet's avatar
François Jacquet committed
281 282 283 284 285
	WHERE SCHOOL_DATE BETWEEN '" . date( 'Y-m-d', $time ) . "' AND '" . date( 'Y-m-d', $time_last ) . "'
	AND SYEAR='" . UserSyear() . "'
	AND SCHOOL_ID='" . UserSchool() . "'
	AND TITLE='" . $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] . "'
	ORDER BY SCHOOL_DATE", array( 'DESCRIPTION' => 'makeDescriptionInput', 'SCHOOL_DATE' => 'ProperDate' ) );
François Jacquet's avatar
François Jacquet committed
286 287

	$events_RET[0] = array(); // make sure indexing from 1
François Jacquet's avatar
François Jacquet committed
288 289 290 291 292 293 294

	foreach ( (array) $calendar_RET as $school_date => $value )
	{
		$events_RET[] = array(
			'ID' => '',
			'SCHOOL_DATE' => ProperDate( $school_date ),
			'DESCRIPTION' => TextInput( '', 'food_service[' . $school_date . '][text]', '', 'size=20' ) .
François Jacquet's avatar
François Jacquet committed
295 296
			( $description_select ? '<select name="food_service[' . $school_date . '][select]">' .
				$description_select : '' ),
François Jacquet's avatar
François Jacquet committed
297 298 299 300 301
		);
	}

	unset( $events_RET[0] );

François Jacquet's avatar
François Jacquet committed
302
	$LO_columns = array( 'ID' => _( 'ID' ), 'SCHOOL_DATE' => _( 'Date' ), 'DESCRIPTION' => _( 'Description' ) );
François Jacquet's avatar
François Jacquet committed
303

304 305
	echo '<form action="' . URLEscape( 'Modules.php?modname=' . $_REQUEST['modname'] . '&menu_id=' . $_REQUEST['menu_id'] .
		'&month=' . $_REQUEST['month'] . '&year=' . $_REQUEST['year']  ) . '" method="POST">';
306 307 308 309 310 311 312 313 314 315 316 317 318

	DrawHeader(
		PrepareDate(
			mb_strtoupper( date( "d-M-y", $time ) ),
			'',
			false,
			array(
				'M' => 1,
				'Y' => 1,
				'submit' => true,
			)
		),
		SubmitButton( _( 'Save' ), 'submit[save]' ) .
319
		SubmitButton( _( 'Generate Menu' ), 'submit[print]', '' ) // No .primary button class.
320 321
	);

François Jacquet's avatar
François Jacquet committed
322
	echo '<br />';
François Jacquet's avatar
François Jacquet committed
323 324

	$tabs = array();
325 326 327 328 329 330

	foreach ( (array) $menus_RET as $id => $meal )
	{
		$tabs[] = array(
			'title' => $meal[1]['TITLE'],
			'link' => 'Modules.php?modname=' . $_REQUEST['modname'] .
François Jacquet's avatar
François Jacquet committed
331
			'&menu_id=' . $id . '&month=' . $_REQUEST['month'] . '&year=' . $_REQUEST['year'],
332 333
		);
	}
François Jacquet's avatar
François Jacquet committed
334

François Jacquet's avatar
François Jacquet committed
335 336 337 338 339 340 341
	$extra = array(
		'save' => false,
		'search' => false,
		'header' => WrapTabs( $tabs, 'Modules.php?modname=' . $_REQUEST['modname'] .
			'&menu_id=' . $_REQUEST['menu_id'] . '&month=' . $_REQUEST['month'] . '&year=' . $_REQUEST['year'] ),
	);

François Jacquet's avatar
François Jacquet committed
342 343
	$singular = sprintf( _( '%s Day' ), $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] );
	$plural = sprintf( _( '%s Days' ), $menus_RET[$_REQUEST['menu_id']][1]['TITLE'] );
François Jacquet's avatar
François Jacquet committed
344

François Jacquet's avatar
François Jacquet committed
345
	ListOutput( $events_RET, $LO_columns, $singular, $plural, array(), array(), $extra );
François Jacquet's avatar
François Jacquet committed
346

François Jacquet's avatar
François Jacquet committed
347
	echo '<br /><div class="center">' . SubmitButton( _( 'Save' ), 'submit[save]' ) . '</div>';
François Jacquet's avatar
François Jacquet committed
348
	echo '</form>';
François Jacquet's avatar
François Jacquet committed
349 350
}

François Jacquet's avatar
François Jacquet committed
351 352 353 354 355 356 357
/**
 * @param $value
 * @param $name
 */
function makeDescriptionInput( $value, $name )
{
	global $THIS_RET, $calendar_RET;
François Jacquet's avatar
François Jacquet committed
358

François Jacquet's avatar
François Jacquet committed
359 360
	if ( $calendar_RET[$THIS_RET['SCHOOL_DATE']] )
	{
361
		unset( $calendar_RET[$THIS_RET['SCHOOL_DATE']] );
François Jacquet's avatar
François Jacquet committed
362
	}
François Jacquet's avatar
François Jacquet committed
363

François Jacquet's avatar
François Jacquet committed
364
	return TextInput( $value, 'food_service[' . $THIS_RET['SCHOOL_DATE'] . '][text]', '', 'size=20' );
François Jacquet's avatar
François Jacquet committed
365
}