method __construct documentation in bbn\Appui\Grid
Grid constructor.
function(bbn\Db $db, array $post, $cfg)
{
// We inherit db and cacher properties
parent::__construct($db);
// Simple configuration using just a string with the table
if ( \is_string($cfg) ){
$cfg = [
'tables' => [$cfg]
];
}
if ( \is_array($cfg) && $this->db->check() ){
// Preparing a classic config array for DB
$db_cfg = [
'tables' => $cfg['tables'] ?? ($cfg['table'] ? (\is_string($cfg['table']) ? [$cfg['table']] : $cfg['table']) : null),
'fields' => !empty($cfg['fields']) ? (array)$cfg['fields'] : [],
'ofields' => !empty($cfg['ofields']) ? (array)$cfg['ofields'] : [],
'order' => $post['order'] ?? ($cfg['order'] ?? []),
'join' => $cfg['join'] ?? ($post['join'] ?? []),
'group_by' => $cfg['group_by'] ?? [],
'having' => !empty($post['having']) ? $post['having'] : [],
'limit' => $post['limit'] ?? ($cfg['limit'] ?? 20),
'start' => $post['start'] ?? 0,
'where' => !empty($post['filters']) ? $post['filters'] : []
];
if ( !empty($post['excel']) ){
$this->excel = $post['excel'];
if ( !empty($post['fields']) ){
$fields = $db_cfg['fields'];
$link = [];
$db_cfg['fields'] = [];
foreach ( $fields as $a => $f ){
$field = is_string($a) ? $a : $this->db->colSimpleName($f);
if ( in_array($field, $post['fields'], true) ){
$link[$field] = $a;
}
}
foreach ( $post['fields'] as $f ){
if ( isset($link[$f], $fields[$link[$f]]) ){
$db_cfg['fields'][$link[$f]] = $fields[$link[$f]];
}
}
}
if (
isset($cfg['map'], $cfg['map']['callable']) &&
is_callable($cfg['map']['callable'])
){
$this->excel['map'] = $cfg['map'];
}
}
// Adding all the fields if fields is empty
if ( empty($db_cfg['tables']) ){
$this->log(['NO TABLES!', $db_cfg]);
}
else if ( empty($db_cfg['fields']) ){
foreach ( array_unique(array_values($db_cfg['tables'])) as $t ){
foreach ( $this->db->getFieldsList($t) as $f ){
if ( !\in_array($f, $db_cfg['fields'], true) ){
$db_cfg['fields'][] = $f;
}
}
}
}
// For the server config both properties where and filters are accepted (backward compatibility)
if ( empty($cfg['filters']) && !empty($cfg['where']) ){
$cfg['filters'] = $cfg['where'];
}
// The (pre)filters set server-side are mandatory and are added to the client-side filters if any
if ( !empty($cfg['filters']) ){
$prefilters = isset($cfg['filters']['conditions']) ? $cfg['filters'] : [
'logic' => 'AND',
'conditions' => $cfg['filters']
];
// They either become the where or are added as a new root condition
$db_cfg['where'] = empty($db_cfg['where']) ? $prefilters : [
'logic' => 'AND',
'conditions' => [
$db_cfg['where'],
$prefilters
]
];
}
// The (pre)having set server-side are mandatory and are added to the client-side having if any
if ( !empty($cfg['having']) ){
$prehaving = isset($cfg['having']['conditions']) ? $cfg['having'] : [
'logic' => 'AND',
'conditions' => $cfg['having']
];
// They either become the where or are added as a new root condition
$db_cfg['having'] = empty($db_cfg['having']) ? $prehaving : [
'logic' => 'AND',
'conditions' => [
$db_cfg['having'],
$prehaving
]
];
}
$this->cfg = $this->db->processCfg($db_cfg);
$this->original_cfg = $db_cfg;
if ( !empty($cfg['query']) ){
$this->query = $cfg['query'];
}
// A query must exist, custom or generated
if ( $this->check() ){
if ( array_key_exists('observer', $cfg) && isset($cfg['observer']['request']) ){
$this->observer = $cfg['observer'];
}
if ( X::hasProp($cfg, 'count') ){
$this->count = $cfg['count'];
}
else{
$db_cfg['count'] = true;
$this->count_cfg = $this->db->processCfg($db_cfg);
//die(X::dump($this->count_cfg));
}
if ( !empty($cfg['num']) ){
$this->num = $cfg['num'];
}
}
}
$this->cache_uid = md5(serialize([
'tables' => $this->cfg['tables'],
'fields' => $this->cfg['fields'],
'order' => $this->cfg['order'],
'values' => $this->cfg['values'],
'join' => $this->cfg['join'],
'group_by' => $this->cfg['group_by'],
'having' => $this->cfg['having'],
'limit' => $this->cfg['limit'],
'start' => $this->cfg['start'],
'filters' => $this->cfg['filters']
]));
$this->chrono = new bbn\Util\Timer();
}
Grid constructor. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2023
BBN Solutions