AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / include / classes / Message / Message.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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 /************************************************************************/
12
13 /**
14 * Message
15 * Class acting as MessageHandler for various message types
16 * @access       public
17 * @author       Jacek Materna
18 */
19
20 class Message {
21
22         /*
23         * Reference to savant obj.
24         * @access private
25         * @see /include/classes/Savant/Savant.php
26         * @var object   
27         */
28         var $savant;
29         
30         /*
31         * Stastic assoc. array of message types mapped to Savant template file names
32         * @access private
33         * @see /templates/
34         * @var array
35         */
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'
41                                 );
42         
43         /*
44         * Static assoc array of message types mapped to Language code prefixes
45         * @access private
46         * @see /include/lib/lang_constant.inc.php
47         * @var array    
48         */
49         var $prefix = array( 'error'  =>'TR_ERROR_',
50                                                 'feedback' => 'TR_FEEDBACK_',
51                                                 'warning' => 'TR_WARNING_',
52                                                 'info' => 'TR_INFOS_',
53                                                 'help' => 'TR_HELP_',
54                                                 'confirm' => 'TR_CONFIRM_'
55                                   );
56         
57         /**
58         * Constructor
59         * @access  public
60         * @param   obj $savant Reference to Savant object
61         * @author  Jacek Materna
62         */
63         function Message($savant) { 
64                 $this->savant = $savant;
65         } 
66                 
67         /**
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
70         * @access  public
71         * @param   string $type                                 error|warning|info|feedback|help|help_pop
72         * @author  Jacek Materna
73         */
74         function printAbstract($type) {
75                 if (!isset($_SESSION['message'][$type])) return;
76
77                 $_result = array();
78                 
79                 foreach($_SESSION['message'][$type] as $e => $item) {
80                         $result = '';
81
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);
88                                 
89                                 if (count($item) == 1) {
90                                         $item = $item[0];
91                                 }
92                         }
93
94                         // $item is either just a code or an array of argument with a particular code
95                         if (is_array($item)) {
96         
97                         
98                                 /* this is an array with terms to replace */
99                                 $first = array_shift($item);
100                                 $result = _AT($first); // lets translate the code
101                                 
102                                 if ($result == '') { // if the code is not in the db lets just print out the code for easier trackdown
103                                         $result = '[' . $first . ']';
104                                 }
105                                                                                 
106                                 $terms = $item;
107                         
108                                 /* replace the tokens with the terms */
109                                 $result = vsprintf($result, $terms);
110                                 
111                         } else {
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 . ']';
115                         }
116                         
117                         array_push($_result, $result); // append to array
118                 }
119                 
120                 if (count($_result) > 0) {
121                         $this->savant->assign('item', $_result);        // pass translated payload to savant var for processing
122                         
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);
128
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'];
134                                 
135                                 $this->savant->assign('a', $a);
136                                 $this->savant->assign('b', $b);
137                                 $this->savant->assign('c', $c);
138                                 $this->savant->assign('d', $d);
139                         }
140                 
141                         $this->savant->display($this->tmpl[$type]);
142                 }
143
144                 unset($_SESSION['message'][$type]);
145         }
146
147         /**
148         * Add message to be tracked by session obj
149         * @access  public
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
153         */
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");
160                         }
161
162                         $code[0] = $this->prefix[$sync] . $code[0]; // add prefix               
163
164                         $first = $code[0];
165                 } else {
166                         if (!is_string($code))  
167                                 settype($code, "string");
168                         
169                         $code = $this->prefix[$sync] . $code;
170                         $first = $code;         
171                 }
172                 
173                 $payload = $code;
174                 
175                 if (!isset($_SESSION['message'][$sync]) || count($_SESSION['message'][$sync]) == 0) { // fresh
176                         
177                         // PHP 5 
178                         //try {
179                                 $_SESSION['message'][$sync] = array($first => $payload);
180                         //} catch (Exception $e) {
181                         //      return false;
182                         //}
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
191                                         }
192                                 } else // no array here yet
193                                         $_SESSION['message'][$sync][$first][] = $payload; // add ourselves 
194                                 
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
199                                         
200                                         $arr = array($temp);
201                                         
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);
206                                         }
207                                         
208                                         $_SESSION['message'][$sync][$first] = $arr; // put it back 
209                                 }
210                         }
211                 } else {
212                 
213                         // Already an array there, could be empty or have something in it, append.
214                         // Store key = value for much faster unset as needed 
215                         
216                         // PHP 5
217                         //try {
218                                 $new = array($first => $payload);
219                                 $final = array_merge((array) $_SESSION['message'][$sync], (array) $new);
220
221                                 unset($_SESSION['message'][$sync]);
222                                 $_SESSION['message'][$sync] = $final;
223                         //} catch (exception $e) {
224                         //      return false;
225                         //}
226                 }
227         }
228         
229         /**
230         * Simply check is a type $type message isset in the session obj
231         * @access  public
232         * @param   string $type                                 what type of message to check for
233         * @author  Jacek Materna
234         */
235         function abstractContains($type) {
236                 return (isset($_SESSION['message'][$type]));
237         }
238         
239         /**
240         * Deletes the tracked message code $code from the Session obj as well as all 
241         * if its children
242         * @access  public
243         * @param   string $type                                 what type of message to delete
244         # @param   string $code                                 The code to delete
245         * @author  Jacek Materna
246         */
247         function abstractDelete($type, $code) {
248                 if (!is_string($code))
249                         settype($code, "string");
250
251                 // Lets append the right prefic to this code for searching
252                 $code = $this->prefix[$type] . $code;
253         
254                 if(isset($_SESSION['message'][$type][$code])) {
255                         unset($_SESSION['message'][$type][$code]); // delete it and its children
256                 }
257         }
258         
259         /**
260         * Add error message to be tracked by session obj
261         * @access  public
262         * @param   string|array $code                   code of the message or array(code, args...)
263         * @author  Jacek Materna
264         */
265         function addError($code) {
266                 $this->addAbstract('error', $code);
267         }
268         
269         /**
270         * Print error messages using Savant template
271         * @access  public
272         * @author  Jacek Materna
273         */
274         function printErrors($optional=null) {
275                 if ($optional != null)  // shortcut
276                         $this->addAbstract('error', $optional);
277
278                 $this->printAbstract('error');
279         }
280         
281
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.'" />';
287                         }
288                 }
289                 if (!is_array($code)) {
290                         $code = array($code);
291                 }
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;
296                 
297                 $this->addAbstract('confirm', $code);
298         }
299         
300         function printConfirm($optional=null) {
301                 if ($optional != null)  // shortcut
302                         $this->addAbstract('confirm', $optional);
303
304                 $this->printAbstract('confirm');
305         }
306
307         /**
308         * Add warning message to be tracked by session obj
309         * @access  public
310         * @param   string|array $code                   code of the message or array(code, args...)
311         * @author  Jacek Materna
312         */
313         function addWarning($code) { 
314                 $this->addAbstract('warning', $code);
315         }
316         
317         /**
318         * Print warning messages using Savant template
319         * @access  public
320         * @author  Jacek Materna
321         */
322         function printWarnings($optional=null) {
323                 if ($optional != null)  // shortcut
324                         $this->addAbstract('warning', $optional);
325                 
326                 $this->printAbstract('warning');
327         }
328         
329         /**
330         * Add info message to be tracked by session obj
331         * @access  public
332         * @param   string|array $code                   code of the message or array(code, args...)
333         * @author  Jacek Materna
334         */
335         function addInfo($code) { 
336                 $this->addAbstract('info', $code);
337         }
338         
339         /**
340         * Print info messages using Savant template
341         * @access  public
342         * @author  Jacek Materna
343         */
344         function printInfos($optional=null) { 
345                 if ($optional != null)  // shortcut
346                         $this->addAbstract('info', $optional);
347
348                 $this->printAbstract('info');
349         }
350         
351         /**
352         * Add feedback message to be tracked by session obj
353         * @access  public
354         * @param   string|array $code                   code of the message or array(code, args...)
355         * @author  Jacek Materna
356         */
357         function addFeedback($code) { 
358                 $this->addAbstract('feedback', $code); 
359         }
360         
361         /**
362         * Print feedback messages using Savant template
363         * @access  public
364         * @author  Jacek Materna
365         */
366         function printFeedbacks($optional=null) {
367                 if ($optional != null) // shortcut
368                         $this->addAbstract('feedback', $optional); 
369                         
370                 $this->printAbstract('feedback');
371         }
372         
373         /**
374         * Add help message to be tracked by session obj
375         * @access  public
376         * @param   string|array $code                   code of the message or array(code, args...)
377         * @author  Jacek Materna
378         */
379         function addHelp($code) { 
380                 $this->addAbstract('help', $code);
381         }
382         
383         /**
384         * Print help messages using Savant template
385         * @access  public
386         * @author  Jacek Materna
387         */
388         function printHelps($optional=null) {
389                 if ($optional != null)  // shortcut
390                         $this->addAbstract('help', $optional);
391                         
392                 $this->printAbstract('help');
393         }
394          
395         /**
396         * Dump all the messages in the session to the screen in the following order
397         * @access  public
398         * @author  Jacek Materna
399         */
400         function printAll() {
401                 $this->printAbstract('feedback');
402                 $this->printAbstract('error');
403                 $this->printAbstract('warning');
404                 $this->printAbstract('help');
405                 $this->printAbstract('info');
406         }
407         
408         /**
409         * Print feedback message using Savant template with no Session dialog and
410         * no database dialog, straight text inside feedback box
411         * @access  public
412         * @param String String message to display inside feedback box
413         * @author  Jacek Materna
414         */
415         function printNoLookupFeedback($str) {
416                 if (str != null) {
417                         $this->savant->assign('item', array($str));     // pass string to savant var for processing
418                         $this->savant->display($this->tmpl['feedback']);
419                 }
420         }
421         
422         /**
423          * Method which simply check if a particular message type exists in the session obj
424          */
425         function containsErrors() {
426                 return $this->abstractContains('error');
427         }
428         
429         function containsFeedbacks() {
430                 return $this->abstractContains('feedback');
431         }
432         
433         function containsWarnings() {
434                 return $this->abstractContains('warning');
435         }
436         
437         function containsInfos() {
438                 return $this->abstractContains('info');
439         }
440         
441         function containsHelps() {
442                 return $this->abstractContains('help');
443         }
444         
445         /**
446          * Method that allow deletion of individual Message codes form the Session obj
447          */
448         function deleteError($code) {
449                 $this->abstractDelete('error', $code);
450         }
451         
452         function deleteFeedback($code) {
453                 $this->abstractDelete('feedback', $code);
454         }
455         
456         function deleteWarning($code) {
457                 $this->abstractDelete('warning', $code);
458         }
459         
460         function deleteInfo($code) {
461                 $this->abstractDelete('info', $code);
462         }
463         
464         function deleteHelp($code) {
465                 $this->abstractDelete('help', $code);
466         }
467         
468 } // end of class
469
470
471
472 ?>