| Current Path : /var/www/html/llcfapp/api/base/ |
| Current File : /var/www/html/llcfapp/api/base/BaseController.php |
<?php
class BaseController extends PdoCrudHandler{
protected $mendetoryParamsAgainstEachMethod;
protected $response = [];
public function __construct(){
$this->_pdo = $this->connect();
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
protected function transformDTQuery($data, $isGroup = false, $having = []){
// Column Searching | Ordering | Paging | Datatables | Start
$clauses = [];
$search = "";
$where = "WHERE ";
$have = "HAVING ";
$order = "ORDER BY ";
$limit = (intval($data['length']) > 0) ? " LIMIT {$data['length']}" : "";
$offset = (intval($data['length']) > 0) ? " OFFSET {$data['start']}": "";
foreach($data['columns'] as $val){
if($val['search']['value'] != ""){
if(!in_array($val['data'], $having)){
$val['data'] = strpos($val['data'], '||') ? str_replace('||','.', $val['data']) : $val['data'];
if($val['search']['value'] != "null"){
$where .= "{$val['data']} like '%{$val['search']['value']}%' AND ";
}else{
$where .= "{$val['data']} IS NULL AND ";
}
}else{
if($val['search']['value'] != "null"){
$have .= "{$val['data']} like '%{$val['search']['value']}%' AND ";
}else{
$have .= "{$val['data']} IS NULL AND ";
}
}
}
}
if($where != "WHERE "){
$where = preg_replace('/AND $/i', '',$where);
}else{
$where = "";
}
if($have != "HAVING "){
$have = preg_replace('/AND $/i', '',$have);
}else{
$have = "";
}
$data['columns'][intval($data['order'][0]['column'])]['data'] = strpos($data['columns'][intval($data['order'][0]['column'])]['data'], '||') ? str_replace('||','.', $data['columns'][intval($data['order'][0]['column'])]['data']) : $data['columns'][intval($data['order'][0]['column'])]['data'];
$order .= "{$data['columns'][intval($data['order'][0]['column'])]['data']} {$data['order'][0]['dir']}";
if(!$isGroup){
$clauses['where'] = $where.$order;
$clauses['paging'] = $where.$order.$limit.$offset;
}else{
$clauses['where'] = $where;
$clauses['having'] = $have;
$clauses['order'] = $order;
$clauses['paging'] = $limit.$offset;
}
// Column Searching | Ordering | Paging | Datatables | End
return $clauses;
}
protected function fullTextSearchDTQuery($data, $table, $extraCols = []){
// Full Text Searching | Datatable global search filter | Start
$limit = (intval($data['length']) > 0) ? " LIMIT {$data['length']}" : "";
$offset = (intval($data['length']) > 0) ? " OFFSET {$data['start']}": "";
$additionalCols = "";
if(count($extraCols) > 0){
foreach($extraCols as $vv){
$additionalCols .= "{$vv},";
}
$additionalCols = trim($additionalCols, ',');
$additionalCols = ", ".$additionalCols;
}
$qry = "SELECT * {$additionalCols} from {$table} {$table}";
if(isset($data['columns'][0]['data']) && strpos($data['columns'][0]['data'], '||')){
// This will only run when we have datable columns from multiple tables
// In datatable request params all the column names must be prefixed as follows tablename||columnname
// As DT would require same column names in response use method prefixColumnWithTableAndMapReqParamInResponse
// --for prefix column names and add missing columns in response
// For this to work all the primary keys of tables must be named "id"
// This would only search the columns of primary table
// Column of joined tables will be ignored
$qry = "";
$qry .= "SELECT ";
$joinTables = [];
foreach($data['columns'] as $val){
$t = explode('||', $val['data'])[0];
if(!in_array($t, $joinTables) && $t != $table){
$joinTables[] = $t;
}
$qry .= str_replace('||', '.', $val['data']) .',';
}
$qry = trim($qry, ",") . ' from '.$table.' '.$table;
foreach($joinTables as $v){
$qry .= " inner join {$v} {$v} on {$v}.id = {$table}.{$v}_id ";
}
// $qry;
}
$getAllColsQry = "
SELECT
group_concat(`column_name`) as column_names
FROM
`information_schema`.`columns`
WHERE
`table_schema`=DATABASE()
AND `table_name`='{$table}'
AND DATA_TYPE IN ('char','varchar')";
$fullTextSearchQuery = $qry . "
WHERE
MATCH ({$this->customSelect($getAllColsQry)['result'][0]->column_names})
AGAINST
('{$data['search']['value']}*' IN BOOLEAN MODE)";
$fullTextQry['where'] = $fullTextSearchQuery;
$fullTextSearchQuery .= $limit.$offset;
$fullTextQry['paging'] = $fullTextSearchQuery;
return $fullTextQry;
// Full Text Searching | Datatable global search filter | End
}
protected function globalSearch($data, $table, $calculatedCols = [], $groupQry = ""){
// Global Text search using OR
$calcColKeys = array_keys($calculatedCols);
$limit = (intval($data['length']) > 0) ? " LIMIT {$data['length']}" : "";
$offset = (intval($data['length']) > 0) ? " OFFSET {$data['start']}": "";
$qry = "SELECT * from {$table} ";
$where = " WHERE ";
$have = " HAVING ";
$qry = "";
$qry .= "SELECT ";
$joinTables = [];
$fullTextQry = [];
$i = 0;
if(isset($data['columns'][0]['data']) && strpos($data['columns'][0]['data'], '||') && $groupQry == ''){
// For this to work all the primary keys of table must be named "id"
// In datatable request params all the column names must be prefixed as follows tablename||columnname
// As DT would require same column names in response use method prefixColumnWithTableAndMapReqParamInResponse
// --for prefix column names and add missing columns in response
// This will search all primary and secondary tables unlike fullTextSearchDTQuery method
// For this to work primary keys of all tables must be named id
foreach($data['columns'] as $val){
$t = explode('||', $val['data'])[0];
if(!in_array($t, $joinTables) && $t != $table){
$joinTables[] = $t;
}
if(!in_array($val['data'], $calcColKeys)){
$valid_col = str_replace('||', '.', $val['data']);
$qry .= $valid_col .',';
if($i < count($data['columns'])-1){
$where .= "{$valid_col} like '%{$data['search']['value']}%' OR ";
}else{
$where .= "{$valid_col} like '%{$data['search']['value']}%' ";
}
}
$i++;
}
foreach($calculatedCols as $kcol => $vcol){
$qry .= "{$vcol}";
}
$qry = trim($qry, ",");
$qry .= ' from '.$table.' '.$table;
foreach($joinTables as $v){
$qry .= " LEFT join {$v} {$v} on {$v}.id = {$table}.{$v}_id ";
}
$qry .= $where;
$fullTextQry['where'] = $qry;
$qry .= $limit.$offset;
$fullTextQry['paging'] = $qry;
}
if($groupQry != ''){
foreach($data['columns'] as $val){
if(!in_array($val['data'], $calcColKeys)){
if($i < count($data['columns'])-1){
$have .= "{$val['data']} like '%{$data['search']['value']}%' OR ";
}else{
$have .= "{$val['data']} like '%{$data['search']['value']}%' ";
}
}
$i++;
}
$groupQry .= $have;
$fullTextQry['where'] = $groupQry;
$groupQry .= $limit.$offset;
$fullTextQry['paging'] = $groupQry;
}
return $fullTextQry;
// Global Text search using OR
}
protected function prefixColumnWithTableAndMapReqParamInResponse($req_cols, $res){
foreach($req_cols as $vals){
if(!strpos($vals['data'], '||')){
foreach($res['result'] as $rec){
if(!isset($rec->$vals['data'])){
$rec->$vals['data'] = "";
}
}
}else{
foreach($res['result'] as $rec){
$t = explode('||', $vals['data'])[0];
$f = explode('||', $vals['data'])[1];
if(isset($rec->$f) || isset($rec->{$t."||".$f})){
if(!isset($rec->{$t."||".$f})){
$rec->{$t."||".$f} = $rec->$f;
}
unset($rec->$f);
}else{
$rec->{$t."||".$f} = "";
}
}
}
}
return $res;
}
protected function handleEmptyDate($data, $dateFields){
foreach($dateFields as $val){
if(trim($data[$val]) == ''){
unset($data[$val]);
}
}
return $data;
}
protected function handleEmptyFields($data){
foreach($data as $key => $val){
if(empty(trim($val))){
unset($data[$key]);
}
}
return $data;
}
protected function unsetArrayKeys($data, $fields){
foreach($fields as $val){
unset($data[$val]);
}
return $data;
}
protected function cleanArray($data){
foreach($data as $k => $v){
if(empty($v)){
unset($data[$k]);
}
}
return $data;
}
}