2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 * Class acting as MessageHandler for various message types
17 * @author Jacek Materna
23 * Reference to savant obj.
25 * @see /include/classes/Savant/Savant.php
31 * Stastic assoc. array of message types mapped to Savant template file names
36 var $tmpl = array( 'error' => 'errormessage.tmpl.php',
37 'feedback' => 'feedbackmessage.tmpl.php',
38 'warning' => 'warningmessage.tmpl.php',
39 'info' => 'infomessage.tmpl.php',
40 'confirm' => 'confirmmessage.tmpl.php'
44 * Static assoc array of message types mapped to Language code prefixes
46 * @see /include/lib/lang_constant.inc.php
49 var $prefix = array( 'error' =>'TR_ERROR_',
50 'feedback' => 'TR_FEEDBACK_',
51 'warning' => 'TR_WARNING_',
52 'info' => 'TR_INFOS_',
54 'confirm' => 'TR_CONFIRM_'
60 * @param obj $savant Reference to Savant object
61 * @author Jacek Materna
63 function Message($savant) {
64 $this->savant = $savant;
68 * Print message(s) of type $type. Processes stored messages in session var for type $type
69 * and translates them into language spec. Then passes processed data to savant template for display
71 * @param string $type error|warning|info|feedback|help|help_pop
72 * @author Jacek Materna
74 function printAbstract($type) {
75 if (!isset($_SESSION['message'][$type])) return;
79 foreach($_SESSION['message'][$type] as $e => $item) {
82 if ($type == 'confirm') {
83 // the confirm msg's have the hidden vars as the last element in the array
84 $hide_button_no = array_pop($item);
85 $button_no_text = array_pop($item);
86 $button_yes_text = array_pop($item);
87 $hidden_vars = array_pop($item);
89 if (count($item) == 1) {
94 // $item is either just a code or an array of argument with a particular code
95 if (is_array($item)) {
98 /* this is an array with terms to replace */
99 $first = array_shift($item);
100 $result = _AT($first); // lets translate the code
102 if ($result == '') { // if the code is not in the db lets just print out the code for easier trackdown
103 $result = '[' . $first . ']';
108 /* replace the tokens with the terms */
109 $result = vsprintf($result, $terms);
112 $result = _AT($item);
113 if ($result == '') // if the code is not in the db lets just print out the code for easier trackdown
114 $result = '[' . $item . ']';
117 array_push($_result, $result); // append to array
120 if (count($_result) > 0) {
121 $this->savant->assign('item', $_result); // pass translated payload to savant var for processing
123 if ($type == 'confirm') {
124 $this->savant->assign('hidden_vars', $hidden_vars);
125 $this->savant->assign('button_yes_text', $button_yes_text);
126 $this->savant->assign('button_no_text', $button_no_text);
127 $this->savant->assign('hide_button_no', $hide_button_no);
129 } else if ($type == 'help') { // special case for help message, we need to check a few conditions
130 $a = (!isset($_GET['e']) && !$_SESSION['prefs']['PREF_HELP'] && !$_GET['h']);
131 $b = ($_SESSION['prefs']['PREF_CONTENT_ICONS'] == 2);
132 $c = isset($_GET['e']);
133 $d = $_SESSION['course_id'];
135 $this->savant->assign('a', $a);
136 $this->savant->assign('b', $b);
137 $this->savant->assign('c', $c);
138 $this->savant->assign('d', $d);
141 $this->savant->display($this->tmpl[$type]);
144 unset($_SESSION['message'][$type]);
148 * Add message to be tracked by session obj
150 * @param string $sync ref to type of message
151 * @param string|array $code code of the message or array(code, args...)
152 * @author Jacek Materna
154 function addAbstract($sync, $code) {
155 $first = ''; // key value for storage
156 // Convert to strings
157 if (is_array($code)) {
158 foreach($code as $e) {
159 settype($e, "string");
162 $code[0] = $this->prefix[$sync] . $code[0]; // add prefix
166 if (!is_string($code))
167 settype($code, "string");
169 $code = $this->prefix[$sync] . $code;
175 if (!isset($_SESSION['message'][$sync]) || count($_SESSION['message'][$sync]) == 0) { // fresh
179 $_SESSION['message'][$sync] = array($first => $payload);
180 //} catch (Exception $e) {
183 } else if (isset($_SESSION['message'][$sync][$first])) { // already data there for that code, append
184 // existing data is either a collection or a single node
185 if(is_array($_SESSION['message'][$sync][$first])) { // already an array there
186 if (is_array($payload)) {
187 // lets ignore the code, its already there as the first element
188 $elem = array_shift($payload);
189 foreach($payload as $elem) {
190 array_push($_SESSION['message'][$sync][$first], $elem); // add ourselves to the chain
192 } else // no array here yet
193 $_SESSION['message'][$sync][$first][] = $payload; // add ourselves
195 } else { // just a string
196 if (is_array($payload)) {
197 $temp = $_SESSION['message'][$sync][$first]; // grab it
198 unset($_SESSION['message'][$sync][$first]); // make sure its gone
202 // skip first elem, we're asserting here that $first === $payload[0]
203 $grb = array_shift($payload);
204 foreach($payload as $elem) { // lets finish building the array
205 array_push($arr, $elem);
208 $_SESSION['message'][$sync][$first] = $arr; // put it back
213 // Already an array there, could be empty or have something in it, append.
214 // Store key = value for much faster unset as needed
218 $new = array($first => $payload);
219 $final = array_merge((array) $_SESSION['message'][$sync], (array) $new);
221 unset($_SESSION['message'][$sync]);
222 $_SESSION['message'][$sync] = $final;
223 //} catch (exception $e) {
230 * Simply check is a type $type message isset in the session obj
232 * @param string $type what type of message to check for
233 * @author Jacek Materna
235 function abstractContains($type) {
236 return (isset($_SESSION['message'][$type]));
240 * Deletes the tracked message code $code from the Session obj as well as all
243 * @param string $type what type of message to delete
244 # @param string $code The code to delete
245 * @author Jacek Materna
247 function abstractDelete($type, $code) {
248 if (!is_string($code))
249 settype($code, "string");
251 // Lets append the right prefic to this code for searching
252 $code = $this->prefix[$type] . $code;
254 if(isset($_SESSION['message'][$type][$code])) {
255 unset($_SESSION['message'][$type][$code]); // delete it and its children
260 * Add error message to be tracked by session obj
262 * @param string|array $code code of the message or array(code, args...)
263 * @author Jacek Materna
265 function addError($code) {
266 $this->addAbstract('error', $code);
270 * Print error messages using Savant template
272 * @author Jacek Materna
274 function printErrors($optional=null) {
275 if ($optional != null) // shortcut
276 $this->addAbstract('error', $optional);
278 $this->printAbstract('error');
282 function addConfirm($code, $hidden_vars = '', $button_yes_text='', $button_no_text='', $hide_button_no=false) {
283 $hidden_vars_string = '';
284 if (is_array($hidden_vars)) {
285 foreach ($hidden_vars as $key => $value) {
286 $hidden_vars_string .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
289 if (!is_array($code)) {
290 $code = array($code);
292 $code[] = $hidden_vars_string;
293 $code[] = ($button_yes_text == '') ? _AT("submit_yes") : $button_yes_text;
294 $code[] = ($button_no_text == '') ? _AT("submit_no") : $button_no_text;
295 $code[] = $hide_button_no;
297 $this->addAbstract('confirm', $code);
300 function printConfirm($optional=null) {
301 if ($optional != null) // shortcut
302 $this->addAbstract('confirm', $optional);
304 $this->printAbstract('confirm');
308 * Add warning message to be tracked by session obj
310 * @param string|array $code code of the message or array(code, args...)
311 * @author Jacek Materna
313 function addWarning($code) {
314 $this->addAbstract('warning', $code);
318 * Print warning messages using Savant template
320 * @author Jacek Materna
322 function printWarnings($optional=null) {
323 if ($optional != null) // shortcut
324 $this->addAbstract('warning', $optional);
326 $this->printAbstract('warning');
330 * Add info message to be tracked by session obj
332 * @param string|array $code code of the message or array(code, args...)
333 * @author Jacek Materna
335 function addInfo($code) {
336 $this->addAbstract('info', $code);
340 * Print info messages using Savant template
342 * @author Jacek Materna
344 function printInfos($optional=null) {
345 if ($optional != null) // shortcut
346 $this->addAbstract('info', $optional);
348 $this->printAbstract('info');
352 * Add feedback message to be tracked by session obj
354 * @param string|array $code code of the message or array(code, args...)
355 * @author Jacek Materna
357 function addFeedback($code) {
358 $this->addAbstract('feedback', $code);
362 * Print feedback messages using Savant template
364 * @author Jacek Materna
366 function printFeedbacks($optional=null) {
367 if ($optional != null) // shortcut
368 $this->addAbstract('feedback', $optional);
370 $this->printAbstract('feedback');
374 * Add help message to be tracked by session obj
376 * @param string|array $code code of the message or array(code, args...)
377 * @author Jacek Materna
379 function addHelp($code) {
380 $this->addAbstract('help', $code);
384 * Print help messages using Savant template
386 * @author Jacek Materna
388 function printHelps($optional=null) {
389 if ($optional != null) // shortcut
390 $this->addAbstract('help', $optional);
392 $this->printAbstract('help');
396 * Dump all the messages in the session to the screen in the following order
398 * @author Jacek Materna
400 function printAll() {
401 $this->printAbstract('feedback');
402 $this->printAbstract('error');
403 $this->printAbstract('warning');
404 $this->printAbstract('help');
405 $this->printAbstract('info');
409 * Print feedback message using Savant template with no Session dialog and
410 * no database dialog, straight text inside feedback box
412 * @param String String message to display inside feedback box
413 * @author Jacek Materna
415 function printNoLookupFeedback($str) {
417 $this->savant->assign('item', array($str)); // pass string to savant var for processing
418 $this->savant->display($this->tmpl['feedback']);
423 * Method which simply check if a particular message type exists in the session obj
425 function containsErrors() {
426 return $this->abstractContains('error');
429 function containsFeedbacks() {
430 return $this->abstractContains('feedback');
433 function containsWarnings() {
434 return $this->abstractContains('warning');
437 function containsInfos() {
438 return $this->abstractContains('info');
441 function containsHelps() {
442 return $this->abstractContains('help');
446 * Method that allow deletion of individual Message codes form the Session obj
448 function deleteError($code) {
449 $this->abstractDelete('error', $code);
452 function deleteFeedback($code) {
453 $this->abstractDelete('feedback', $code);
456 function deleteWarning($code) {
457 $this->abstractDelete('warning', $code);
460 function deleteInfo($code) {
461 $this->abstractDelete('info', $code);
464 function deleteHelp($code) {
465 $this->abstractDelete('help', $code);