~ubuntu-branches/ubuntu/raring/extplorer/raring-proposed

« back to all changes in this revision

Viewing changes to include/result.class.php

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Goirand
  • Date: 2010-07-05 19:53:12 UTC
  • Revision ID: james.westby@ubuntu.com-20100705195312-i92s1udelus7gl52
Tags: upstream-2.1.0b6+dfsg
ImportĀ upstreamĀ versionĀ 2.1.0b6+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// ensure this file is being included by a parent file
 
3
if( !defined( '_JEXEC' ) && !defined( '_VALID_MOS' ) ) die( 'Restricted access' );
 
4
/**
 
5
 * @version $Id: result.class.php 160 2009-11-16 05:58:30Z soeren $
 
6
 * @package eXtplorer
 
7
 * @copyright soeren 2007
 
8
 * @author The eXtplorer project (http://sourceforge.net/projects/extplorer)
 
9
 * @author The  The QuiX project (http://quixplorer.sourceforge.net)
 
10
 * 
 
11
 * @license
 
12
 * The contents of this file are subject to the Mozilla Public License
 
13
 * Version 1.1 (the "License"); you may not use this file except in
 
14
 * compliance with the License. You may obtain a copy of the License at
 
15
 * http://www.mozilla.org/MPL/
 
16
 * 
 
17
 * Software distributed under the License is distributed on an "AS IS"
 
18
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 
19
 * License for the specific language governing rights and limitations
 
20
 * under the License.
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the terms
 
23
 * of the GNU General Public License Version 2 or later (the "GPL"), in
 
24
 * which case the provisions of the GPL are applicable instead of
 
25
 * those above. If you wish to allow use of your version of this file only
 
26
 * under the terms of the GPL and not to allow others to use
 
27
 * your version of this file under the MPL, indicate your decision by
 
28
 * deleting  the provisions above and replace  them with the notice and
 
29
 * other provisions required by the GPL.  If you do not delete
 
30
 * the provisions above, a recipient may use your version of this file
 
31
 * under either the MPL or the GPL."
 
32
 * 
 
33
 */
 
34
 
 
35
/**
 
36
 * Allows to handle errors and send results in JSON format
 
37
 *
 
38
 */
 
39
class ext_Result {
 
40
        function init() {
 
41
                ext_Result::empty_errors();
 
42
                ext_Result::empty_messages();
 
43
        }
 
44
        function add_message($message, $type='general') {
 
45
                $_SESSION['ext_message'][$type][] = $message;
 
46
        }
 
47
        function empty_messages() {
 
48
                $_SESSION['ext_message'] = array();
 
49
        }
 
50
        function count_messages() {
 
51
 
 
52
                if( empty($_SESSION['ext_message'])) {
 
53
                        return 0;
 
54
                }
 
55
                $count = 0;
 
56
                foreach( $_SESSION['ext_message'] as $type ) {
 
57
                        if( !empty( $type ) && is_array( $type )) {
 
58
                                $count += sizeof( $type );
 
59
                        }
 
60
                }
 
61
                return $count;
 
62
        }
 
63
        function add_error($error, $type='general') {
 
64
                $_SESSION['ext_error'][$type][] = $error;
 
65
        }
 
66
        function empty_errors() {
 
67
                $_SESSION['ext_error'] = array();
 
68
        }
 
69
        function count_errors() {
 
70
                if( empty($_SESSION['ext_error'])) {
 
71
                        return 0;
 
72
                }
 
73
                $count = 0;
 
74
                foreach( @$_SESSION['ext_error'] as $type ) {
 
75
                        if( !empty( $type ) && is_array( $type )) {
 
76
                                $count += sizeof( $type );
 
77
                        }
 
78
                }
 
79
                return $count;
 
80
        }
 
81
        function sendResult( $action, $success, $msg,$extra=array() ) {         // show error-message
 
82
                
 
83
                if( ext_isXHR() ) {
 
84
                        $success = (bool)$success;
 
85
                        if( $success && ext_Result::count_errors() > 0 ) {
 
86
                                $success = false;
 
87
                                foreach( @$_SESSION['ext_error'] as $type ) {
 
88
                                        if( is_array( $type )) {
 
89
                                                foreach( $type as $error ) {
 
90
                                                        $msg .= '<br />'.$error;
 
91
                                                }
 
92
                                        }
 
93
                                }
 
94
                        }
 
95
                        $result = array('action' => $action,
 
96
                                                        'message' => str_replace("'", "\\'", $msg ),
 
97
                                                        'error' => str_replace("'", "\\'", $msg ),//.print_r($_POST,true),
 
98
                                                        'success' => $success 
 
99
                                                );
 
100
                        foreach( $extra as $key => $value ) {
 
101
                                $result[$key] = $value;
 
102
                        }
 
103
                        $classname = class_exists('ext_Json') ? 'ext_Json' : 'Services_JSON';
 
104
                        $json = new $classname();
 
105
                        $jresult = $json->encode($result);
 
106
                        if(strtolower(extGetParam($_POST,'requestType')) == 'xmlhttprequest') {
 
107
                                header("Content-type: text/html");
 
108
                        }
 
109
                        print $jresult;
 
110
                        ext_exit();
 
111
                }
 
112
 
 
113
                if($extra != NULL) {
 
114
                        $msg .= " - ".$extra;
 
115
                }
 
116
                ext_Result::add_error( $msg );
 
117
 
 
118
                if( empty( $_GET['error'] )) {
 
119
                        session_write_close();
 
120
                        extRedirect( make_link("show_error", $GLOBALS["dir"], null, null, null, null, '&error=1&extra='.urlencode( $extra )) );
 
121
                }
 
122
                else {
 
123
                        show_header($GLOBALS["error_msg"]["error"]);
 
124
 
 
125
                        echo '<div class="quote">';
 
126
                        echo '<a href="#errors">'.ext_Result::count_errors() .' '.$GLOBALS["error_msg"]["error"].'</a>, ';
 
127
                        echo '<a href="#messages">'.ext_Result::count_messages() .' '.$GLOBALS["error_msg"]["message"].'</a><br />';
 
128
                        echo "</div>\n";
 
129
 
 
130
                        if( !empty( $_SESSION['ext_message'])) {
 
131
                                echo "<a href=\"".str_replace('&dir=', '&ignore=', make_link("list", '' ))."\">[ "
 
132
                                                .$GLOBALS["error_msg"]["back"]." ]</a>";
 
133
 
 
134
                                echo "<div class=\"ext_message\"><a name=\"messages\"></a>
 
135
                                                <h3>".$GLOBALS["error_msg"]["message"].":</strong>"."</h3>\n";
 
136
 
 
137
                                foreach ( $_SESSION['ext_message'] as $msgtype ) {
 
138
                                        foreach ( $msgtype as $message ) {
 
139
                                                echo $message ."\n<br/>";
 
140
                                        }
 
141
                                        echo '<br /><hr /><br />';
 
142
                                }
 
143
                                ext_Result::empty_messages();
 
144
 
 
145
                                if( !empty( $_REQUEST['extra'])) echo " - ".htmlspecialchars(urldecode($_REQUEST['extra']), ENT_QUOTES );
 
146
                                echo "</div>\n";
 
147
                        }
 
148
 
 
149
                        if( !empty( $_SESSION['ext_error'])) {
 
150
                                echo "<div class=\"ext_error\"><a name=\"errors\"></a>
 
151
                                                <h3>".$GLOBALS["error_msg"]["error"].":</strong>"."</h3>\n";
 
152
 
 
153
                                foreach ( $_SESSION['ext_error'] as $errortype ) {
 
154
                                        foreach ( $errortype as $error ) {
 
155
                                                echo $error ."\n<br/>";
 
156
                                        }
 
157
                                        echo '<br /><hr /><br />';
 
158
                                }
 
159
                                ext_Result::empty_errors();
 
160
                        }
 
161
                        echo "<a href=\"".str_replace('&dir=', '&ignore=', make_link("list", '' ))."\">".$GLOBALS["error_msg"]["back"]."</a>";
 
162
                        if( !empty( $_REQUEST['extra'])) echo " - ".htmlspecialchars(urldecode($_REQUEST['extra']), ENT_QUOTES );
 
163
                        echo "</div>\n";
 
164
                        defined('EXPLORER_NOEXEC') or define('EXPLORER_NOEXEC', 1 );
 
165
                }
 
166
        }
 
167
}
 
168
//------------------------------------------------------------------------------
 
169
?>