PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/rxwandc/application/admin.rxwan.com/classes/controller/base.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 233 lines | 111 code | 24 blank | 98 comment | 36 complexity | 189a56cb72308e8cdab7e7f2c5860ac6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. defined('SYSPATH') or die('No direct script access.');
  3. class Controller_Base extends Controller {
  4. public $_response;
  5. public $_success = false;
  6. public $_index;
  7. public $_file = "common";
  8. public $_data = array();
  9. public $_totalCount = NULL;
  10. protected $_msg;
  11. public function message( $file="" ){
  12. $file = empty($file) ? $this->_file : $file;
  13. return Kohana::message( $this->_file, $this->_index );
  14. }
  15. protected function _debug( $param, $exit = false ){
  16. echo "<pre>";
  17. var_dump( $param );
  18. if( $exit ) exit;
  19. }
  20. public function _response( $result=NULL, $file="", $data=array()){
  21. $this->_response["success"] = is_null($result) ? $this->_success : $result;
  22. if(!empty($this->_index))
  23. $this->_response["message"] = $this->message( $file );
  24. if(!is_null($this->_totalCount))
  25. $this->_response["results"] = $this->_totalCount;
  26. $this->_response["data"] = empty($data) ? $this->_data : $data;
  27. $this->response->body( json_encode($this->_response) );
  28. }
  29. public function _param( $keys, $default=NULL ){
  30. $data = array();
  31. if(!empty($keys)){
  32. foreach($keys as $k=>$key){
  33. $val = is_array($default) ? $default[$k] : $default;
  34. $data[$key] = Arr::get( $_REQUEST, $key, $val );
  35. }
  36. }
  37. //执行数据过滤
  38. $data = array_filter($data,array($this,'_filter'));
  39. return $data;
  40. }
  41. protected function _param1(){
  42. // $jsonStr = Arr::get($_REQUEST,'json','');
  43. $jsonStr = file_get_contents("php://input");
  44. //var_dump($jsonStr);exit;
  45. return json_decode($jsonStr,true);
  46. }
  47. protected function _operation( $model, $type, $data=array(), $key="id",$param='' ){
  48. $id = Arr::get( $_REQUEST, $key );
  49. if( is_null( $id ) ) $id = Arr::get( $data, $key );
  50. if( isset( $data[$key] ) ) unset( $data[$key] );
  51. $this->_index = $type . ".fail";
  52. if($type=="create")
  53. list($id, $row) = $model->add( $data );
  54. elseif($type=="edit")
  55. $row = $model->edit( $id, $data,$param );
  56. elseif($type=="del")
  57. $row = $model->del( $id, $data );
  58. if(empty($row))
  59. return $this->_response( false );
  60. $this->_index = $type . ".success";
  61. $this->_data = $model->getDetail( $id );
  62. return $this->_response( true );
  63. }
  64. protected function _responseBody( $success=false, $param = "", $type="message"){
  65. $json = array();
  66. $json["success"] = $success;
  67. $json[$type] = empty($param) ? $this->_msg : $param;
  68. $json["data"] = $this->_data;
  69. $this->_output( json_encode($json) );
  70. }
  71. protected function _output( $json ){
  72. // $this->response->body( $json );
  73. echo $json;
  74. }
  75. /*
  76. * 过滤掉空字符串
  77. */
  78. protected function _filter($data1){
  79. return is_null($data1)||$data1===""?false:true;
  80. }
  81. /**
  82. *
  83. */
  84. protected function getFirstWord($s0){
  85. $fchar = ord($s0{0}); //获取字符串的第一个字节
  86. //判断是不是英文字符,如果是则直接返回该字母大写
  87. if ($fchar >= ord('A') and $fchar <= ord('z'))return strtoupper($s0{0});
  88. //判断是否为数字,如果是的话则直接返回数字的
  89. if ($fchar >= 48 and $fchar <= 57)return strtoupper($s0{0});
  90. //以下代码用字符编码转换函数,通过两种字符集编码转换的对比,判断字符串是哪种字符集
  91. //最终取字符串为GB字符集
  92. $s1 = @iconv('UTF-8', 'GBK', $s0);
  93. $s2 = @iconv('GBK', 'UTF-8', $s1);
  94. if ($s2 == $s0) {
  95. $s = $s1;
  96. } else {
  97. $s = $s0;
  98. }
  99. //计算给出的字符串的前两个字节内码,然后再根据结果判断在GB字符集中的位置,从而根据位置与拼音的关系,最终得出拼音字母
  100. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  101. if($asc >= -20319 and $asc <= -20284) return "A";
  102. if($asc >= -20283 and $asc <= -19776) return "B";
  103. if($asc >= -19775 and $asc <= -19219) return "C";
  104. if($asc >= -19218 and $asc <= -18711) return "D";
  105. if($asc >= -18710 and $asc <= -18527) return "E";
  106. if($asc >= -18526 and $asc <= -18240) return "F";
  107. if($asc >= -18239 and $asc <= -17923) return "G";
  108. if($asc>=-17922 and $asc <=-17418) return "H";
  109. if($asc >= -17417 and $asc <= -16475) return "J";
  110. if($asc >= -16474 and $asc <= -16213) return "K";
  111. if($asc >= -16212 and $asc <= -15641) return "L";
  112. if($asc >= -15640 and $asc <= -15166) return "M";
  113. if($asc >= -15165 and $asc <= -14923) return "N";
  114. if($asc >= -14922 and $asc <= -14915) return "O";
  115. if($asc >= -14914 and $asc <= -14631) return "P";
  116. if($asc >= -14630 and $asc <= -14150) return "Q";
  117. if($asc >= -14149 and $asc <= -14091) return "R";
  118. if($asc >= -14090 and $asc <= -13319) return "S";
  119. if($asc >= -13318 and $asc <= -12839) return "T";
  120. if($asc >= -12838 and $asc <= -12557) return "W";
  121. if($asc >= -12556 and $asc <= -11848) return "X";
  122. if($asc >= -11847 and $asc <= -11056) return "Y";
  123. if($asc >= -11055 and $asc <= -10247) return "Z";
  124. return null;
  125. }
  126. /**
  127. * 写入日志文件
  128. */
  129. // public function __destruct(){
  130. //
  131. // $controller = $this->request->controller();
  132. // $action = $this->request->action();
  133. // $act = $this->request->param('act');
  134. // $type = $this->request->param('type');
  135. // $s = $this->request->param('s');
  136. //
  137. // //判断是否是list的路由如果是的话,就跳出
  138. // if($act=='list' || $act == 'lists' || $action =='list' || $action =='ledger'|| $controller == 'toolbar')
  139. // //echo $act.'<br/><br/><br/>'.$action.'<br/><br/><br/>';
  140. // return;
  141. // //如果有增删改操作的话,执行日志写入
  142. // $username=Session::instance()->get('username');
  143. //
  144. // if(is_array($this->_data))
  145. // $data = json_encode($this->_data);
  146. // //封装日期
  147. // $date = date("Y-m-d H:i:s",time());
  148. //
  149. //
  150. // //定义message文件
  151. // $this->_file="adminlog";
  152. //
  153. // /**
  154. // * 根据路由来封装日志信息
  155. // */
  156. // switch($controller){
  157. // case 'game':
  158. // case 'operator':
  159. // case 'mapping':
  160. // $this->_index=$controller.".".$act;
  161. // break;
  162. // case 'opsys':
  163. // $this->_index = $controller.".".$type.".".$act;
  164. // break;
  165. // case 'user':
  166. // case 'accounts':
  167. // $this->_index = $controller.".".$action;
  168. // break;
  169. // }
  170. //
  171. // $msg = $this->message();
  172. //
  173. // if(!isset($msg))
  174. // return;
  175. // //定义一个数据来存放日志信息
  176. // $arr = array('create_date'=>$date,'username'=>$username,'action'=>$msg,'data'=>$data);
  177. //
  178. //
  179. // $model = Model::factory('adminlog');
  180. // $model->add($arr);
  181. //
  182. //
  183. // //根据日期来定义文件夹
  184. // /*
  185. // $dir1 ="/opt/data/www/kohana/application/admin/adminlog/".date('Ym',time())."/";//月份
  186. // if(!file_exists($dir1))
  187. // mkdir($dir1);
  188. // $file = $dir1.date('Ymd',time()).'.txt';
  189. //
  190. //
  191. // //执行写文件操作
  192. // if (!$handle = fopen($file, 'a')) {
  193. // return;
  194. // }
  195. //
  196. //
  197. // if (fwrite($handle, $str) === FALSE) {
  198. // return;
  199. // }
  200. //
  201. //
  202. //
  203. // fclose($handle);
  204. //
  205. //
  206. // */
  207. //
  208. // }
  209. }