Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 109 |
| JqGridRead | |
0.00% |
0 / 1 |
|
0.00% |
0 / 14 |
4032.00 | |
0.00% |
0 / 109 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 4 |
|||
| respond | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 7 |
|||
| respond_grid | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
|||
| get_oper | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| get_where_clause | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| get_select | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 6 |
|||
| get_order_by | |
0.00% |
0 / 1 |
90.00 | |
0.00% |
0 / 13 |
|||
| get_limit | |
0.00% |
0 / 1 |
72.00 | |
0.00% |
0 / 10 |
|||
| set_total_records | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| total_pages | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 10 |
|||
| get_response | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| read_params | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 4 |
|||
| set_where_clause | |
0.00% |
0 / 1 |
72.00 | |
0.00% |
0 / 10 |
|||
| exporta_csv_falta | |
0.00% |
0 / 1 |
272.00 | |
0.00% |
0 / 31 |
|||
| <?php | |
| namespace ia\JqGrid; | |
| use ia\Util\Str; | |
| /* | |
| $grid = new JqGridRead($gSqlClass); | |
| $where = $grid->get_where_clause(); | |
| if(!empty($where)) { | |
| $where = " WHERE $where "; | |
| } | |
| // $sql_comment = " /*".basename(__FILE__)."/ "; | |
| $sql_count = "SELECT $sql_comment COUNT(*) FROM bitacora $where "; | |
| $Sql = "SELECT $sql_comment * FROM bitacora"; | |
| $grid->respond_grid($Sql, $sql_count); | |
| */ | |
| class JqGridRead { | |
| protected $db; | |
| protected $params = []; | |
| protected $where_clause; | |
| protected $response = []; | |
| public function __construct($db){ | |
| $this->db = $db; | |
| $this->read_params(); | |
| $this->where_clause = $this->set_where_clause(); | |
| } | |
| public function respond($sql, $sql_count) { | |
| $oper = $this->get_oper(); | |
| if($oper == '') { | |
| $this->respond_grid($sql, $sql_count); | |
| return; | |
| } | |
| header($_SERVER["SERVER_PROTOCOL"]." 500 Error de transmision"); | |
| header("Status: Error de transmision"); | |
| } | |
| public function respond_grid($sql, $sql_count, $tieneWhere=false) { | |
| $this->set_total_records($this->db->single_read( $sql_count )); | |
| $select = $this->get_select($sql, $tieneWhere); | |
| $this->response['rows'] = $this->db->selectArrayIndex($select); | |
| $errors = $this->db->errorLog_get(); | |
| if(!empty($errors)) { | |
| header($_SERVER["SERVER_PROTOCOL"]." 501 al leer datos"); | |
| //header("Status: al leer datos"); | |
| } else { | |
| echo json_encode($this->response); | |
| } | |
| } | |
| public function get_oper() { | |
| return $params['oper']; | |
| } | |
| public function get_where_clause() { | |
| return $this->where_clause; | |
| } | |
| public function get_select($select, $tieneWhere = false, $limit = true) { | |
| if(!empty($this->where_clause)) { | |
| $select .= ($tieneWhere ? ' AND ' : ' WHERE ') . '('.$this->where_clause.')'; | |
| } | |
| $select .= $this->get_order_by(); | |
| if($limit) { | |
| return $select . $this->get_limit(); | |
| } | |
| return $select; | |
| } | |
| public function get_order_by() { | |
| //@TODO FIELD(id,3,2,1,4), funccall() | |
| $orderBy = []; | |
| foreach(explode(',', $this->params['sidx'].' '.$this->params['sord']) as $clause) { | |
| $part = []; | |
| foreach(explode(' ', $clause) as $d) { | |
| $q0 = Str::strim($d); | |
| if(empty($q0)) { | |
| continue; | |
| } | |
| if(strcasecmp('ASC',$q0) === 0 || strcasecmp('DESC',$q0) === 0 || strcasecmp('RAND()',$q0) === 0 ) { | |
| $part[] = $q0; | |
| continue; | |
| } | |
| $part[] = is_numeric($q0) ? $q0 : Str::fieldit($q0); | |
| } | |
| $orderBy[] = implode(' ', $part); | |
| } | |
| return empty($orderBy) ? ' ORDER BY 1 ' : ' ORDER BY ' . implode(', ', $orderBy).' '; | |
| } | |
| public function get_limit() { | |
| if(empty($this->params['rows']) || !is_numeric($this->params['rows']) ) | |
| return ''; | |
| if($this->params['rows']<=0) | |
| $this->params['rows']=10; | |
| if( empty($this->params['page']) || !is_numeric($this->params['page']) || $this->params['page']<=0 ) | |
| $this->params['page']=1; | |
| $start = $this->params['rows']*$this->params['page'] - $this->params['rows']; | |
| if($start <0) | |
| $start = 0; | |
| return " LIMIT $start,".$this->params['rows']; | |
| } | |
| public function set_total_records($total_records) { | |
| $this->response['records'] = $total_records; | |
| $this->total_pages(); | |
| } | |
| protected function total_pages() { | |
| $this->response['page'] = $this->params['page']; | |
| if(empty($this->params['rows']) || !is_numeric($this->params['rows']) || $this->params['rows'] <= 0 ) { | |
| $this->response['total'] = 1; | |
| $this->response['page'] = 1; | |
| return; | |
| } | |
| $this->response['total'] = $this->response['records'] <= 0 ? 0 : | |
| $this->response['total'] = max( ceil($this->response['records']/$this->params['rows']),1); | |
| if ($this->params['page'] > $this->response['total']) { | |
| $this->response['page'] = $this->response['total']; | |
| } | |
| } | |
| public function get_response() { | |
| return $this->response; | |
| } | |
| protected function read_params() { | |
| $paramsToRead = [ | |
| 'oper' => '', | |
| '_search' => false, | |
| 'searchField' => '', | |
| 'searchOper' => '', | |
| 'searchString' => '', | |
| 'filters' => '', | |
| 'sidx' => '', | |
| 'sord' => '', | |
| 'rows' => null, | |
| 'page' => 1, | |
| ]; | |
| foreach($paramsToRead as $key => $default) { | |
| $this->params[$key] = isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default; | |
| } | |
| } | |
| protected function set_where_clause() { | |
| $where_clause = []; | |
| if(!empty($this->params['searchField']) && !empty($this->params['searchOper']) && !empty($this->params['searchString'])) { | |
| $where = iaFilter2where::rule2sql(['field'=>$this->params['searchField'], 'op'=>$this->params['searchOper'], 'Data'=>$this->params['searchString']]); | |
| if(!empty($where)) { | |
| $where_clause[] = "($where)"; | |
| } | |
| } | |
| if(!empty($this->params['filters']) && !empty($this->params['_search'])) { | |
| $where = iaFilter2where::filter2where(json_decode($this->params['filters'],true)); | |
| if(!empty($where)) { | |
| $where_clause[] = $where; | |
| } | |
| } | |
| return implode(' AND ', $where_clause); | |
| } | |
| private function exporta_csv_falta($sql,$fileName='download.csv') { | |
| header("Content-Type: text/html; charset=utf-8"); | |
| header("Cache-Control: no-store, no-Cache"); | |
| header("Content-Disposition: attachment; filename*=UTF-8''".$fileName.".csv"); | |
| echo "\xEF\xBB\xBF"; | |
| $exporta=ia_sqlArrayIndx($sql); | |
| if($exporta) { | |
| foreach($exporta as $rec) { | |
| $con=false; | |
| foreach($rec as $fieldName=>$v) if($conPK || (!$conPK && $fieldName!=$this->pk_field)) { | |
| if($con) | |
| echo ','.comillea(to_label($fieldName)); | |
| else { | |
| echo comillea(to_label($fieldName)); | |
| $con=true; | |
| } | |
| } | |
| break; | |
| } | |
| foreach($exporta as $rec) { | |
| echo PHP_EOL; | |
| $con=false; | |
| foreach($rec as $fieldName=>$v) if($conPK || (!$conPK && $fieldName!=$this->pk_field)) { | |
| if( is_numeric($v) ) | |
| if($con) | |
| echo ','.$v; | |
| else { | |
| echo $v; | |
| $con=true; | |
| } | |
| else | |
| if($con) | |
| echo ','.comillea($v); | |
| else { | |
| echo comillea($v); | |
| $con=true; | |
| } | |
| } | |
| } | |
| echo PHP_EOL; | |
| } | |
| $rep = new iaErrorReporter($gSqlClass); | |
| $rep->process(false); | |
| die(); | |
| } | |
| } |