[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /*+********************************************************************************** 3 * The contents of this file are subject to the vtiger CRM Public License Version 1.1 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 ************************************************************************************/ 10 11 class Vtiger_Response { 12 // Constants 13 14 /** 15 * Emit response wrapper as raw string 16 */ 17 static $EMIT_RAW = 0; 18 19 /** 20 * Emit response wrapper as json string 21 */ 22 static $EMIT_JSON= 1; 23 24 /** 25 * Emit response wrapper as html string 26 */ 27 static $EMIT_HTML= 2; 28 29 /** 30 * Emit response wrapper as string/jsonstring 31 */ 32 static $EMIT_JSONTEXT= 3; 33 34 /** 35 * Emit response wrapper as padded-json 36 */ 37 static $EMIT_JSONP = 4; 38 39 /** 40 * Error data. 41 */ 42 private $error = NULL; 43 /** 44 * Result data. 45 */ 46 private $result = NULL; 47 48 /* Active emit type */ 49 private $emitType= 1; // EMIT_JSON 50 51 /* JSONP padding */ 52 private $emitJSONPFn=false;// for EMIT_JSONP 53 54 /* List of response headers */ 55 private $headers = array(); 56 57 /** 58 * Set headers to send 59 */ 60 function setHeader($header) { 61 $this->headers[] = $header; 62 } 63 64 /** 65 * Set error data to send 66 */ 67 function setError($code, $message=null) { 68 if ($message == null) $message = $code; 69 $error = array('code' => $code, 'message' => $message); 70 $this->error = $error; 71 } 72 73 /** 74 * Set emit type. 75 */ 76 function setEmitType($type) { 77 $this->emitType = $type; 78 } 79 80 /** 81 * Set padding method name for JSONP emit type. 82 */ 83 function setEmitJSONP($fn) { 84 $this->setEmitType(self::$EMIT_JSONP); 85 $this->emitJSONPFn = $fn; 86 } 87 88 /** 89 * Is emit type configured to JSON? 90 */ 91 function isJSON() { 92 return $this->emitType == self::$EMIT_JSON; 93 } 94 95 /** 96 * Get the error data 97 */ 98 function getError() { 99 return $this->error; 100 } 101 102 /** 103 * Check the presence of error data 104 */ 105 function hasError() { 106 return !is_null($this->error); 107 } 108 109 /** 110 * Set the result data. 111 */ 112 function setResult($result) { 113 $this->result = $result; 114 } 115 116 /** 117 * Update the result data. 118 */ 119 function updateResult($key, $value) { 120 $this->result[$key] = $value; 121 } 122 123 /** 124 * Get the result data. 125 */ 126 function getResult() { 127 return $this->result; 128 } 129 130 /** 131 * Prepare the response wrapper. 132 */ 133 protected function prepareResponse() { 134 $response = array(); 135 if($this->error !== NULL) { 136 $response['success'] = false; 137 $response['error'] = $this->error; 138 } else { 139 $response['success'] = true; 140 $response['result'] = $this->result; 141 } 142 return $response; 143 } 144 145 /** 146 * Send response to client. 147 */ 148 function emit() { 149 150 $contentTypeSent = false; 151 foreach ($this->headers as $header) { 152 if (!$contentTypeSent && stripos($header, 'content-type') === 0) { $contentTypeSent = true; } 153 header($header); 154 } 155 156 /* Set right charset (UTF-8) to avoid IE complaining about c00ce56e error */ 157 if ($this->emitType == self::$EMIT_JSON) { 158 if (!$contentTypeSent) header('Content-type: text/json; charset=UTF-8'); 159 $this->emitJSON(); 160 } else if ($this->emitType == self::$EMIT_JSONTEXT){ 161 if (!$contentTypeSent) header('Content-type: text/json; charset=UTF-8'); 162 $this->emitText(); 163 } else if ($this->emitType == self::$EMIT_HTML){ 164 if (!$contentTypeSent) header('Content-type: text/html; charset=UTF-8'); 165 $this->emitRaw(); 166 } else if ($this->emitType == self::$EMIT_RAW) { 167 if (!$contentTypeSent) header('Content-type: text/plain; charset=UTF-8'); 168 $this->emitRaw(); 169 } else if ($this->emitType == self::$EMIT_JSONP) { 170 if (!$contentTypeSent) header('Content-type: application/javascript; charset=UTF-8'); 171 echo $this->emitJSONPFn . "("; 172 $this->emitJSON(); 173 echo ")"; 174 } 175 } 176 177 /** 178 * Emit response wrapper as JSONString 179 */ 180 protected function emitJSON() { 181 echo Zend_Json::encode($this->prepareResponse()); 182 } 183 184 /** 185 * Emit response wrapper as String/JSONString 186 */ 187 protected function emitText() { 188 if ($this->result === NULL) { 189 if (is_string($this->error)) echo $this->error; 190 else echo Zend_Json::encode($this->prepareResponse()); 191 } else { 192 if (is_string($this->result)) echo $this->result; 193 else echo Zend_Json::encode($this->prepareResponse()); 194 } 195 } 196 197 /** 198 * Emit response wrapper as String. 199 */ 200 protected function emitRaw() { 201 if($this->result === NULL) echo (is_string($this->error))? $this->error : var_export($this->error, true); 202 echo $this->result; 203 } 204 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |