Example #1
0
function ssp_get_where($columns, $request, &$bindings)
{
    $globalSearch = array();
    $columnSearch = array();
    $dtColumns = ssp_pluck($columns, 'dt');
    if (isset($request['search']) && $request['search']['value'] != '') {
        $str = $request['search']['value'];
        for ($i = 0, $ien = count($request['columns']); $i < $ien; $i++) {
            $requestColumn = $request['columns'][$i];
            $columnIdx = array_search($requestColumn['data'], $dtColumns);
            $column = $columns[$columnIdx];
            if ($requestColumn['searchable'] == 'true') {
                $check = false;
                if (isset($column['fn']) && function_exists($column['fn'])) {
                    $check = true;
                    $funct = $column['fn'];
                }
                if ($check === true) {
                    $binding = ssp_bind($bindings, '%' . $funct($str) . '%', PDO::PARAM_STR);
                } else {
                    $binding = ssp_bind($bindings, '%' . $str . '%', PDO::PARAM_STR);
                }
                $globalSearch[] = \cx\app\main_functions::fix_db_column($column['db']) . " LIKE " . $binding;
            }
        }
    }
    $clm = isset($request['columns']) ? $request['columns'] : false;
    if ($clm !== false) {
        // Individual column filtering
        for ($i = 0, $ien = count($clm); $i < $ien; $i++) {
            $requestColumn = $request['columns'][$i];
            $columnIdx = array_search($requestColumn['data'], $dtColumns);
            $column = $columns[$columnIdx];
            $str = $requestColumn['search']['value'];
            if ($requestColumn['searchable'] == 'true' && $str != '') {
                $check = false;
                if (isset($column['fn']) && function_exists($column['fn'])) {
                    $check = true;
                    $funct = $column['fn'];
                }
                if ($check === true) {
                    $binding = ssp_bind($bindings, '%' . $funct($str) . '%', PDO::PARAM_STR);
                } else {
                    $binding = ssp_bind($bindings, '%' . $str . '%', PDO::PARAM_STR);
                }
                $columnSearch[] = \cx\app\main_functions::fix_db_column($column['db']) . " LIKE " . $binding;
            }
        }
    }
    // Combine the filters into a single string
    $where = '';
    if (count($globalSearch)) {
        $where = '(' . implode(' OR ', $globalSearch) . ')';
    }
    if (count($columnSearch)) {
        $where = $where === '' ? implode(' AND ', $columnSearch) : $where . ' AND ' . implode(' AND ', $columnSearch);
    }
    if ($where !== '') {
        $where = ' ' . $where . ' AND ';
    }
    return $where;
}