2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-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 'help' => 'helpmessage.tmpl.php',
41 'confirm' => 'confirmmessage.tmpl.php'
45 * Static assoc array of message types mapped to Language code prefixes
47 * @see /include/lib/lang_constant.inc.php
50 var $prefix = array( 'error' =>'AT_ERROR_',
51 'feedback' => 'AT_FEEDBACK_',
52 'warning' => 'AT_WARNING_',
53 'info' => 'AT_INFOS_',
55 'confirm' => 'AT_CONFIRM_'
61 * @param obj $savant Reference to Savant object
62 * @author Jacek Materna
64 function Message($savant) {
65 $this->savant = $savant;
69 * Print message(s) of type $type. Processes stored messages in session var for type $type
70 * and translates them into language spec. Then passes processed data to savant template for display
72 * @param string $type error|warning|info|feedback|help|help_pop
73 * @author Jacek Materna
75 function printAbstract($type) {
76 if (!isset($_SESSION['message'][$type])) return;
80 foreach($_SESSION['message'][$type] as $e => $item) {
83 if ($type == 'confirm') {
84 // the confirm msg's have the hidden vars as the last element in the array
85 $hide_button_no = array_pop($item);
86 $button_no_text = array_pop($item);
87 $button_yes_text = array_pop($item);
88 $hidden_vars = array_pop($item);
90 if (count($item) == 1) {
95 // $item is either just a code or an array of argument with a particular code
96 if (is_array($item)) {
99 /* this is an array with terms to replace */
100 $first = array_shift($item);
101 $result = _AT($first); // lets translate the code
103 if ($result == '') { // if the code is not in the db lets just print out the code for easier trackdown
104 $result = '[' . $first . ']';
109 /* replace the tokens with the terms */
110 $result = vsprintf($result, $terms);
113 $result = _AT($item);
114 if ($result == '') // if the code is not in the db lets just print out the code for easier trackdown
115 $result = '[' . $item . ']';
118 array_push($_result, $result); // append to array
121 if (count($_result) > 0) {
122 $this->savant->assign('item', $_result); // pass translated payload to savant var for processing
124 if ($type == 'confirm') {
125 $this->savant->assign('hidden_vars', $hidden_vars);
126 $this->savant->assign('button_yes_text', $button_yes_text);
127 $this->savant->assign('button_no_text', $button_no_text);
128 $this->savant->assign('hide_button_no', $hide_button_no);
130 } else if ($type == 'help') { // special case for help message, we need to check a few conditions
131 $a = (!isset($_GET['e']) && !$_SESSION['prefs']['PREF_HELP'] && !$_GET['h']);
132 $b = ($_SESSION['prefs']['PREF_CONTENT_ICONS'] == 2);
133 $c = isset($_GET['e']);
134 $d = $_SESSION['course_id'];
136 $this->savant->assign('a', $a);
137 $this->savant->assign('b', $b);
138 $this->savant->assign('c', $c);
139 $this->savant->assign('d', $d);
142 $this->savant->display($this->tmpl[$type]);
145 unset($_SESSION['message'][$type]);
149 * Add message to be tracked by session obj
151 * @param string $sync ref to type of message
152 * @param string|array $code code of the message or array(code, args...)
153 * @author Jacek Materna
155 function addAbstract($sync, $code) {
156 $first = ''; // key value for storage
157 // Convert to strings
158 if (is_array($code)) {
159 foreach($code as $e) {
160 settype($e, "string");
163 $code[0] = $this->prefix[$sync] . $code[0]; // add prefix
167 if (!is_string($code))
168 settype($code, "string");
170 $code = $this->prefix[$sync] . $code;
176 if (!isset($_SESSION['message'][$sync]) || count($_SESSION['message'][$sync]) == 0) { // fresh
180 $_SESSION['message'][$sync] = array($first => $payload);
181 //} catch (Exception $e) {
184 } else if (isset($_SESSION['message'][$sync][$first])) { // already data there for that code, append
185 // existing data is either a collection or a single node
186 if(is_array($_SESSION['message'][$sync][$first])) { // already an array there
187 if (is_array($payload)) {
188 // lets ignore the code, its already there as the first element
189 $elem = array_shift($payload);
190 foreach($payload as $elem) {
191 array_push($_SESSION['message'][$sync][$first], $elem); // add ourselves to the chain
193 } else // no array here yet
194 $_SESSION['message'][$sync][$first][] = $payload; // add ourselves
196 } else { // just a string
197 if (is_array($payload)) {
198 $temp = $_SESSION['message'][$sync][$first]; // grab it
199 unset($_SESSION['message'][$sync][$first]); // make sure its gone
203 // skip first elem, we're asserting here that $first === $payload[0]
204 $grb = array_shift($payload);
205 foreach($payload as $elem) { // lets finish building the array
206 array_push($arr, $elem);
209 $_SESSION['message'][$sync][$first] = $arr; // put it back
214 // Already an array there, could be empty or have something in it, append.
215 // Store key = value for much faster unset as needed
219 $new = array($first => $payload);
220 $final = array_merge((array) $_SESSION['message'][$sync], (array) $new);
222 unset($_SESSION['message'][$sync]);
223 $_SESSION['message'][$sync] = $final;
224 //} catch (exception $e) {
231 * Simply check is a type $type message isset in the session obj
233 * @param string $type what type of message to check for
234 * @author Jacek Materna
236 function abstractContains($type) {
237 return (isset($_SESSION['message'][$type]));
241 * Deletes the tracked message code $code from the Session obj as well as all
244 * @param string $type what type of message to delete
245 # @param string $code The code to delete
246 * @author Jacek Materna
248 function abstractDelete($type, $code) {
249 if (!is_string($code))
250 settype($code, "string");
252 // Lets append the right prefic to this code for searching
253 $code = $this->prefix[$type] . $code;
255 if(isset($_SESSION['message'][$type][$code])) {
256 unset($_SESSION['message'][$type][$code]); // delete it and its children
261 * Add error message to be tracked by session obj
263 * @param string|array $code code of the message or array(code, args...)
264 * @author Jacek Materna
266 function addError($code) {
267 $this->addAbstract('error', $code);
271 * Print error messages using Savant template
273 * @author Jacek Materna
275 function printErrors($optional=null) {
276 if ($optional != null) // shortcut
277 $this->addAbstract('error', $optional);
279 $this->printAbstract('error');
283 function addConfirm($code, $hidden_vars = '', $button_yes_text='', $button_no_text='', $hide_button_no=false) {
284 $hidden_vars_string = '';
285 if (is_array($hidden_vars)) {
286 foreach ($hidden_vars as $key => $value) {
287 $hidden_vars_string .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
290 if (!is_array($code)) {
291 $code = array($code);
293 $code[] = $hidden_vars_string;
294 $code[] = ($button_yes_text == '') ? _AT("submit_yes") : $button_yes_text;
295 $code[] = ($button_no_text == '') ? _AT("submit_no") : $button_no_text;
296 $code[] = $hide_button_no;
298 $this->addAbstract('confirm', $code);
301 function printConfirm($optional=null) {
302 if ($optional != null) // shortcut
303 $this->addAbstract('confirm', $optional);
305 $this->printAbstract('confirm');
309 * Add warning message to be tracked by session obj
311 * @param string|array $code code of the message or array(code, args...)
312 * @author Jacek Materna
314 function addWarning($code) {
315 $this->addAbstract('warning', $code);
319 * Print warning messages using Savant template
321 * @author Jacek Materna
323 function printWarnings($optional=null) {
324 if ($optional != null) // shortcut
325 $this->addAbstract('warning', $optional);
327 $this->printAbstract('warning');
331 * Add info message to be tracked by session obj
333 * @param string|array $code code of the message or array(code, args...)
334 * @author Jacek Materna
336 function addInfo($code) {
337 $this->addAbstract('info', $code);
341 * Print info messages using Savant template
343 * @author Jacek Materna
345 function printInfos($optional=null) {
346 if ($optional != null) // shortcut
347 $this->addAbstract('info', $optional);
349 $this->printAbstract('info');
353 * Add feedback message to be tracked by session obj
355 * @param string|array $code code of the message or array(code, args...)
356 * @author Jacek Materna
358 function addFeedback($code) {
359 $this->addAbstract('feedback', $code);
363 * Print feedback messages using Savant template
365 * @author Jacek Materna
367 function printFeedbacks($optional=null) {
368 if ($optional != null) // shortcut
369 $this->addAbstract('feedback', $optional);
371 $this->printAbstract('feedback');
375 * Add help message to be tracked by session obj
377 * @param string|array $code code of the message or array(code, args...)
378 * @author Jacek Materna
380 function addHelp($code) {
381 $this->addAbstract('help', $code);
385 * Print help messages using Savant template
387 * @author Jacek Materna
389 function printHelps($optional=null) {
390 if ($optional != null) // shortcut
391 $this->addAbstract('help', $optional);
393 $this->printAbstract('help');
397 * Dump all the messages in the session to the screen in the following order
399 * @author Jacek Materna
401 function printAll() {
402 $this->printAbstract('feedback');
403 $this->printAbstract('error');
404 $this->printAbstract('warning');
405 $this->printAbstract('help');
406 $this->printAbstract('info');
410 * Print feedback message using Savant template with no Session dialog and
411 * no database dialog, straight text inside feedback box
413 * @param String String message to display inside feedback box
414 * @author Jacek Materna
416 function printNoLookupFeedback($str) {
418 $this->savant->assign('item', array($str)); // pass string to savant var for processing
419 $this->savant->display($this->tmpl['feedback']);
424 * Method which simply check if a particular message type exists in the session obj
426 function containsErrors() {
427 return $this->abstractContains('error');
430 function containsFeedbacks() {
431 return $this->abstractContains('feedback');
434 function containsWarnings() {
435 return $this->abstractContains('warning');
438 function containsInfos() {
439 return $this->abstractContains('info');
442 function containsHelps() {
443 return $this->abstractContains('help');
447 * Method that allow deletion of individual Message codes form the Session obj
449 function deleteError($code) {
450 $this->abstractDelete('error', $code);
453 function deleteFeedback($code) {
454 $this->abstractDelete('feedback', $code);
457 function deleteWarning($code) {
458 $this->abstractDelete('warning', $code);
461 function deleteInfo($code) {
462 $this->abstractDelete('info', $code);
465 function deleteHelp($code) {
466 $this->abstractDelete('help', $code);