Advertisement
Guest User

Untitled

a guest
May 25th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: Adriano
  5. * Date: 12/11/12
  6. * Time: 17:11
  7. * To change this template use File | Settings | File Templates.
  8. */
  9.  
  10. if(!defined('ROOTPATH'))
  11. define('ROOTPATH', dirname(__FILE__).'/../..');
  12.  
  13. require_once ROOTPATH.'/api/controller.php';
  14. use prototype\api\Config as Config;
  15.  
  16.  
  17. class Grid{
  18.  
  19.  
  20. private $post;
  21. private $get;
  22. private $offset;
  23. private $response;
  24.  
  25. public function Grid( $post, $get ){
  26. $this->post = $post;
  27. $this->get = $get;
  28.  
  29. $this->offset = ($this->get['rows'] * $this->get['page']) - $this->get['rows'];
  30. }
  31.  
  32. public function Execute(){
  33.  
  34. $start = '';
  35. $end = '';
  36.  
  37. $order = array(
  38. 'GITEC' => 's.gitec',
  39. 'SR' => 's.sr',
  40. 'PV' => 's.pv',
  41. 'owner' => 's.owner',
  42. 'id' => 's.id_object',
  43. 'cause' => 's.cause',
  44. 'dateOccurrence' => 's.date_occurrence',
  45. 'status' => 's.id_status',
  46. 'category' => 's.id_category',
  47. 'budget' => 'b.control_number'
  48. );
  49.  
  50. $condition = array();
  51.  
  52. //Dados do chamado
  53. $sql = 'select s.id_object as "id", s.gitec as "GITEC", s.pv as "PV",'
  54. .'s.sr as "SR", s.timestamp as "timestamp", s.owner as "owner",'
  55. .'s.cause as "cause", s.occurrence as "occurrence", s.id_category as "category",'
  56. .'s.id_status as "status", s.post_date as "postDate", s.insurer_date as "insurerDate",'
  57. .'s.is_valid as "isValid", s.insurer_protocol as "insurerProtocol", bu.id_archive as "budgetId", '
  58.  
  59. //verify as budget
  60. .'b.control_number as "budget", '
  61.  
  62. //verify as budget upload
  63. .'count( bu.id_archive ) as "budgetUpload", '
  64.  
  65. //verify as payment
  66. .'p.id_payment as "payment" '
  67.  
  68. //Join entre object e budget
  69. .'FROM sinister_object as "s" LEFT JOIN sinister_budget as "b" ON (b.id_object = s.id_object) '
  70.  
  71. //Join entre object e payment
  72. .'LEFT JOIN sinister_payment as "p" ON (p.id_object = s.id_object) '
  73.  
  74. //Join entre object e budget upload
  75. .'LEFT JOIN sinister_archive as "bu" ON (bu.id_object = s.id_object AND bu.type_archive = 4) ';
  76.  
  77. //Chamados apartir de uma data
  78. if (isset($this->get['start']) && $this->get['start'] != '') {
  79. $condition[] = (count( $condition ) > 0 ? ' AND' : '') . ' s.date_occurrence >= \''. pg_escape_string( $this->get['start'] ) .'\'';
  80. }
  81.  
  82. //Chamados at� de uma data
  83. if (isset($this->get['end']) && $this->get['end'] != '') {
  84. $condition[] = (count( $condition ) > 0 ? ' AND' : '') . ' s.date_occurrence <= \''. pg_escape_string( $this->get['end'] ) .'\'';
  85. }
  86.  
  87.  
  88. //Chamados de um usu�rio em especifico
  89. if (isset($this->get['owner']) && $this->get['owner'] != '') {
  90. $condition[] = (count( $condition ) > 0 ? ' AND' : '') . ' s.owner = \''. pg_escape_string( $this->get['owner'] ) .'\'';
  91. }
  92.  
  93. ////Chamados de uma GITEC
  94. //if (isset($this->get['GITEC']) && $this->get['GITEC'] != '') {
  95. // $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.gitec = \''. pg_escape_string( $this->get['GITEC'] ) .'\'';
  96. //}
  97.  
  98. ////Chamados de uma FUNC_GITEC
  99. //if (isset($this->get['FUNC_GITEC']) && $this->get['FUNC_GITEC'] != '') {
  100. // $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.gitec = \''. pg_escape_string( $this->get['FUNC_GITEC'] ) .'\'';
  101. //}
  102.  
  103. //Chamados de uma SR
  104. //if (isset($this->get['SR']) && $this->get['SR'] != '') {
  105. // $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.sr = \''. pg_escape_string( $this->get['SR'] ) .'\'';
  106. //}
  107. //
  108. ////Chamados de uma FUNC_SR
  109. //if (isset($this->get['FUNC_SR']) && $this->get['FUNC_SR'] != '') {
  110. // $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.sr = \''. pg_escape_string( $this->get['FUNC_SR'] ) .'\'';
  111. //}
  112. //
  113. ////Chamados de uma PV
  114. //if (isset($this->get['PV']) && $this->get['PV'] != '') {
  115. // $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.pv = \''. pg_escape_string( $this->get['PV'] ) .'\'';
  116. //}
  117. //
  118. ////Chamados de uma FUNC_PV
  119. //if (isset($this->get['FUNC_PV']) && $this->get['FUNC_PV'] != '') {
  120. // $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.pv = \''. pg_escape_string( $this->get['FUNC_PV'] ) .'\'';
  121. //}
  122.  
  123. //Chamados em Status
  124. if (isset($this->get['status']) && $this->get['status'] != '') {
  125. $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.id_status = \''. pg_escape_string( $this->get['status'] ) .'\'';
  126. }
  127.  
  128. //Chamados por id
  129. if (isset($this->get['id']) && $this->get['id'] != '') {
  130. $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.id_object = \''. pg_escape_string( $this->get['id'] ) .'\'';
  131. }
  132.  
  133. //Chamados por categoria
  134. if (isset($this->get['category']) && $this->get['category'] != '') {
  135. $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' s.id_category = \''. pg_escape_string( $this->get['category'] ) .'\'';
  136. }
  137.  
  138. //Filtro por numero de or�amento
  139. if (isset($this->get['controlNumber']) && $this->get['controlNumber'] != '') {
  140. $condition[] = (count( $condition ) > 0 ? ' AND' : '') .' b.control_number LIKE \''."%". pg_escape_string( $this->get['controlNumber'] ) ."%".'\'';
  141. }
  142.  
  143. //Not in Status
  144. if (isset($this->get['!status']) && $this->get['!status'] != '') {
  145. $condition[] = (count( $condition ) > 0 ? ' AND' : '') . ' s.id_status NOT IN ( '. implode(', ', $this->get['!status'] ) .')';
  146. }
  147.  
  148.  
  149. if( count( $condition ) ){
  150.  
  151. //Custom where
  152. $where = 'where ( ';
  153.  
  154. foreach($condition as $v){
  155. if($v != '')
  156. $where .= $v;
  157. }
  158.  
  159. $where .= ') ';
  160. }else{
  161.  
  162. $where = '';
  163.  
  164. }
  165.  
  166. $sqlCount = 'SELECT count( * ) from sinister_object as "s" LEFT JOIN sinister_budget as "b" ON ( s.id_object = b.id_object ) ';
  167.  
  168. $count = Controller::service('PostgreSQL')->execResultSql( $sqlCount.$where );
  169.  
  170. $count = is_array( $count ) ? $count[0]['count'] : $count;
  171.  
  172. if(isset( $order[ $this->get['sidx'] ])){
  173. $where .= ' GROUP BY s.id_object, b.control_number, p.id_payment, bu.id_archive ORDER BY '.$order[ $this->get['sidx'] ]. ' '. $this->get['sord'];
  174. }else{
  175. $where .= ' GROUP BY s.id_object, b.control_number, p.id_payment, bu.id_archive ORDER BY s.timestamp DESC';
  176.  
  177. }
  178.  
  179. $where .= ' limit '.$this->get['rows'].' offset '.$this->offset;
  180.  
  181. $data = Controller::service('PostgreSQL')->execResultSql( $sql.$where );
  182.  
  183. if(empty($data) || count($data) == 0){
  184. echo json_encode( array() );
  185. return false;
  186. }
  187.  
  188. $i = 0;
  189. $this->response['rows'] = array();
  190.  
  191. $userIndex = array();
  192.  
  193. /*
  194. * Normalize data
  195. * */
  196. foreach($data as &$v){
  197.  
  198. if( !isset($userIndex[ $v['owner'] ]) ){
  199.  
  200. $user = Controller::read(array( 'concept' => 'user', 'id' => (int)$v['owner'] ));
  201. if( isset( $user[0] ) ) $user = $user[0];
  202.  
  203. $userIndex[ $v['owner'] ] = $user;
  204.  
  205. }
  206.  
  207. $v['name'] = $userIndex[ $v['owner'] ]['name'];
  208. $v['dateOccurrence'] = date("d/m/Y", ($v['timestamp'] / 1000) );
  209.  
  210.  
  211. $this->response['rows'][$i] = $v;
  212. ++$i;
  213. }
  214.  
  215. $this->response['page'] = $this->get['page'];
  216. $this->response['total'] = ceil($count / $this->get['rows']);
  217. $this->response['records'] = $count;
  218.  
  219. echo json_encode( $this->toUtf8( $this->response ) );
  220. return false;
  221. }
  222.  
  223. public function toUtf8($data)
  224. {
  225. if(!is_array($data))
  226. return mb_convert_encoding( $data , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
  227.  
  228. $return = array();
  229.  
  230. foreach ($data as $i => $v)
  231. $return[$this->toUtf8($i)] = $this->toUtf8($v);
  232.  
  233. return $return;
  234. }
  235.  
  236.  
  237. }
  238.  
  239. $grid = new Grid($_POST, $_GET );
  240. $grid->Execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement