2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
16 * Class acting as MessageHandler for various message types
18 * @author Jacek Materna
24 * Reference to savant obj.
26 * @see /include/classes/Savant/Savant.php
32 * Stastic assoc. array of message types mapped to Savant template file names
37 var $tmpl = array( 'error' => 'errormessage.tmpl.php',
38 'feedback' => 'feedbackmessage.tmpl.php',
39 'warning' => 'warningmessage.tmpl.php',
40 'info' => 'infomessage.tmpl.php',
41 'help' => 'helpmessage.tmpl.php',
42 'confirm' => 'confirmmessage.tmpl.php'
46 * Static assoc array of message types mapped to Language code prefixes
48 * @see /include/lib/lang_constant.inc.php
51 var $prefix = array( 'error' =>'AT_ERROR_',
52 'feedback' => 'AT_FEEDBACK_',
53 'warning' => 'AT_WARNING_',
54 'info' => 'AT_INFOS_',
56 'confirm' => 'AT_CONFIRM_'
62 * @param obj $savant Reference to Savant object
63 * @author Jacek Materna
65 function Message($savant) {
66 $this->savant = $savant;
70 * Print message(s) of type $type. Processes stored messages in session var for type $type
71 * and translates them into language spec. Then passes processed data to savant template for display
73 * @param string $type error|warning|info|feedback|help|help_pop
74 * @author Jacek Materna
76 function printAbstract($type) {
77 if (!isset($_SESSION['message'][$type])) return;
81 foreach($_SESSION['message'][$type] as $e => $item) {
84 if ($type == 'confirm') {
85 // the confirm msg's have the hidden vars as the last element in the array
86 $hide_button_no = array_pop($item);
87 $button_no_text = array_pop($item);
88 $button_yes_text = array_pop($item);
89 $hidden_vars = array_pop($item);
91 if (count($item) == 1) {
96 // $item is either just a code or an array of argument with a particular code
97 if (is_array($item)) {
100 /* this is an array with terms to replace */
101 $first = array_shift($item);
102 $result = _AT($first); // lets translate the code
104 if ($result == '') { // if the code is not in the db lets just print out the code for easier trackdown
105 $result = '[' . $first . ']';
110 /* replace the tokens with the terms */
111 $result = vsprintf($result, $terms);
114 $result = _AT($item);
115 if ($result == '') // if the code is not in the db lets just print out the code for easier trackdown
116 $result = '[' . $item . ']';
119 array_push($_result, $result); // append to array
122 if (count($_result) > 0) {
123 $this->savant->assign('item', $_result); // pass translated payload to savant var for processing
125 if ($type == 'confirm') {
126 $this->savant->assign('hidden_vars', $hidden_vars);
127 $this->savant->assign('button_yes_text', $button_yes_text);
128 $this->savant->assign('button_no_text', $button_no_text);
129 $this->savant->assign('hide_button_no', $hide_button_no);
131 } else if ($type == 'help') { // special case for help message, we need to check a few conditions
132 $a = (!isset($_GET['e']) && !$_SESSION['prefs']['PREF_HELP'] && !$_GET['h']);
133 $b = ($_SESSION['prefs']['PREF_CONTENT_ICONS'] == 2);
134 $c = isset($_GET['e']);
135 $d = $_SESSION['course_id'];
137 $this->savant->assign('a', $a);
138 $this->savant->assign('b', $b);
139 $this->savant->assign('c', $c);
140 $this->savant->assign('d', $d);
143 $this->savant->display($this->tmpl[$type]);
146 unset($_SESSION['message'][$type]);
150 * Add message to be tracked by session obj
152 * @param string $sync ref to type of message
153 * @param string|array $code code of the message or array(code, args...)
154 * @author Jacek Materna
156 function addAbstract($sync, $code) {
157 $first = ''; // key value for storage
158 // Convert to strings
159 if (is_array($code)) {
160 foreach($code as $e) {
161 settype($e, "string");
164 $code[0] = $this->prefix[$sync] . $code[0]; // add prefix
168 if (!is_string($code))
169 settype($code, "string");
171 $code = $this->prefix[$sync] . $code;
177 if (!isset($_SESSION['message'][$sync]) || count($_SESSION['message'][$sync]) == 0) { // fresh
181 $_SESSION['message'][$sync] = array($first => $payload);
182 //} catch (Exception $e) {
185 } else if (isset($_SESSION['message'][$sync][$first])) { // already data there for that code, append
186 // existing data is either a collection or a single node
187 if(is_array($_SESSION['message'][$sync][$first])) { // already an array there
188 if (is_array($payload)) {
189 // lets ignore the code, its already there as the first element
190 $elem = array_shift($payload);
191 foreach($payload as $elem) {
192 array_push($_SESSION['message'][$sync][$first], $elem); // add ourselves to the chain
194 } else // no array here yet
195 $_SESSION['message'][$sync][$first][] = $payload; // add ourselves
197 } else { // just a string
198 if (is_array($payload)) {
199 $temp = $_SESSION['message'][$sync][$first]; // grab it
200 unset($_SESSION['message'][$sync][$first]); // make sure its gone
204 // skip first elem, we're asserting here that $first === $payload[0]
205 $grb = array_shift($payload);
206 foreach($payload as $elem) { // lets finish building the array
207 array_push($arr, $elem);
210 $_SESSION['message'][$sync][$first] = $arr; // put it back
215 // Already an array there, could be empty or have something in it, append.
216 // Store key = value for much faster unset as needed
220 $new = array($first => $payload);
221 $final = array_merge((array) $_SESSION['message'][$sync], (array) $new);
223 unset($_SESSION['message'][$sync]);
224 $_SESSION['message'][$sync] = $final;
225 //} catch (exception $e) {
232 * Simply check is a type $type message isset in the session obj
234 * @param string $type what type of message to check for
235 * @author Jacek Materna
237 function abstractContains($type) {
238 return (isset($_SESSION['message'][$type]));
242 * Deletes the tracked message code $code from the Session obj as well as all
245 * @param string $type what type of message to delete
246 # @param string $code The code to delete
247 * @author Jacek Materna
249 function abstractDelete($type, $code) {
250 if (!is_string($code))
251 settype($code, "string");
253 // Lets append the right prefic to this code for searching
254 $code = $this->prefix[$type] . $code;
256 if(isset($_SESSION['message'][$type][$code])) {
257 unset($_SESSION['message'][$type][$code]); // delete it and its children
262 * Add error message to be tracked by session obj
264 * @param string|array $code code of the message or array(code, args...)
265 * @author Jacek Materna
267 function addError($code) {
268 $this->addAbstract('error', $code);
272 * Print error messages using Savant template
274 * @author Jacek Materna
276 function printErrors($optional=null) {
277 if ($optional != null) // shortcut
278 $this->addAbstract('error', $optional);
280 $this->printAbstract('error');
284 function addConfirm($code, $hidden_vars = '', $button_yes_text='', $button_no_text='', $hide_button_no=false) {
285 $hidden_vars_string = '';
286 if (is_array($hidden_vars)) {
287 foreach ($hidden_vars as $key => $value) {
288 $hidden_vars_string .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
291 if (!is_array($code)) {
292 $code = array($code);
294 $code[] = $hidden_vars_string;
295 $code[] = ($button_yes_text == '') ? _AT("submit_yes") : $button_yes_text;
296 $code[] = ($button_no_text == '') ? _AT("submit_no") : $button_no_text;
297 $code[] = $hide_button_no;
299 $this->addAbstract('confirm', $code);
302 function printConfirm($optional=null) {
303 if ($optional != null) // shortcut
304 $this->addAbstract('confirm', $optional);
306 $this->printAbstract('confirm');
310 * Add warning message to be tracked by session obj
312 * @param string|array $code code of the message or array(code, args...)
313 * @author Jacek Materna
315 function addWarning($code) {
316 $this->addAbstract('warning', $code);
320 * Print warning messages using Savant template
322 * @author Jacek Materna
324 function printWarnings($optional=null) {
325 if ($optional != null) // shortcut
326 $this->addAbstract('warning', $optional);
328 $this->printAbstract('warning');
332 * Add info message to be tracked by session obj
334 * @param string|array $code code of the message or array(code, args...)
335 * @author Jacek Materna
337 function addInfo($code) {
338 $this->addAbstract('info', $code);
342 * Print info messages using Savant template
344 * @author Jacek Materna
346 function printInfos($optional=null) {
347 if ($optional != null) // shortcut
348 $this->addAbstract('info', $optional);
350 $this->printAbstract('info');
354 * Add feedback message to be tracked by session obj
356 * @param string|array $code code of the message or array(code, args...)
357 * @author Jacek Materna
359 function addFeedback($code) {
360 $this->addAbstract('feedback', $code);
364 * Print feedback messages using Savant template
366 * @author Jacek Materna
368 function printFeedbacks($optional=null) {
369 if ($optional != null) // shortcut
370 $this->addAbstract('feedback', $optional);
372 $this->printAbstract('feedback');
376 * Add help message to be tracked by session obj
378 * @param string|array $code code of the message or array(code, args...)
379 * @author Jacek Materna
381 function addHelp($code) {
382 $this->addAbstract('help', $code);
386 * Print help messages using Savant template
388 * @author Jacek Materna
390 function printHelps($optional=null) {
391 if ($optional != null) // shortcut
392 $this->addAbstract('help', $optional);
394 $this->printAbstract('help');
398 * Dump all the messages in the session to the screen in the following order
400 * @author Jacek Materna
402 function printAll() {
403 $this->printAbstract('feedback');
404 $this->printAbstract('error');
405 $this->printAbstract('warning');
406 $this->printAbstract('help');
407 $this->printAbstract('info');
411 * Print feedback message using Savant template with no Session dialog and
412 * no database dialog, straight text inside feedback box
414 * @param String String message to display inside feedback box
415 * @author Jacek Materna
417 function printNoLookupFeedback($str) {
419 $this->savant->assign('item', array($str)); // pass string to savant var for processing
420 $this->savant->display($this->tmpl['feedback']);
425 * Method which simply check if a particular message type exists in the session obj
427 function containsErrors() {
428 return $this->abstractContains('error');
431 function containsFeedbacks() {
432 return $this->abstractContains('feedback');
435 function containsWarnings() {
436 return $this->abstractContains('warning');
439 function containsInfos() {
440 return $this->abstractContains('info');
443 function containsHelps() {
444 return $this->abstractContains('help');
448 * Method that allow deletion of individual Message codes form the Session obj
450 function deleteError($code) {
451 $this->abstractDelete('error', $code);
454 function deleteFeedback($code) {
455 $this->abstractDelete('feedback', $code);
458 function deleteWarning($code) {
459 $this->abstractDelete('warning', $code);
462 function deleteInfo($code) {
463 $this->abstractDelete('info', $code);
466 function deleteHelp($code) {
467 $this->abstractDelete('help', $code);