[ '@namespace' => 'default_objects/UserConfig', '@primaryKey' => 'ID_USER', 'ID_USER' => [ '@type' => 'xsd:integer' ], 'ID_PROCES' => [ '@type' => 'xsd:integer' ], 'lastLogin' => [ '@type' => 'xsd:dateTime', '@confKey' => 'auth_user_{ID_USER}' ],// TODO: store last login time 'lastAuthCacheUpdate' => [ '@type' => 'xsd:dateTime', '@confKey' => 'acl_user_{ID_USER}_{ID_PROCES}_cache_update' ], // 'appFilterProces' => [ '@type' => 'xsd:integer' ],// TODO: filter proces id set by app (web app stores in session - create UserSession Storage) ] ]; public $_rootTableName = 'CRM_CONFIG__#USER_CONF_VIEW';// 'CRM_CONFIG'; public $idUser = null; public $idProcesFilter = null; public function __construct($simpleSchema = null) { parent::__construct($simpleSchema); $this->idUser = User::getID();// default - current user $this->idProcesFilter = 0; } public function setIdUser($idUser) { $this->idUser = intval($idUser); if ($this->idUser <= 0) throw new Exception("Missing id user"); } public function getIdUser() { return $this->idUser; } public function setIdProcesFilter($idProcesFilter) { $this->idProcesFilter = intval($idProcesFilter); } public function getIdProcesFilter() { return $this->idProcesFilter; } public function getTotal($params = []) { return 1; } public function _parseSqlWhere($params = []) { return ''; } public function sqlFieldConfKey($fieldName) { if ('@' == substr($fieldName, 0, 1)) return null; if (!array_key_exists($fieldName, $this->_simpleSchema['root'])) return null; $field = $this->_simpleSchema['root'][$fieldName]; if ('ID_USER' == $fieldName) return null; if ('ID_PROCES' == $fieldName) return null; if (empty($field['@confKey'])) return null; $confKey = $field['@confKey']; $confKey = str_replace('{ID_USER}', $this->idUser, $confKey); $confKey = str_replace('{ID_PROCES}', $this->idProcesFilter, $confKey); return $confKey; } public function updateItem($itemPatch) { DBG::log($itemPatch, 'array', '$itemPatch'); $affected = 0; foreach ($itemPatch as $fieldName => $value) { if (!array_key_exists($fieldName, $this->_simpleSchema['root'])) throw new Exception("Field '{$fieldName}' not exists in '" . $this->getNamespace() . "'"); $field = $this->_simpleSchema['root'][$fieldName]; $confKey = $this->sqlFieldConfKey($fieldName); if ($confKey) { $sqlConfKey = DB::getPDO()->quote($confKey, PDO::PARAM_STR); $sqlValue = ''; if ('NOW()' == $value) { $sqlValue = 'NOW()'; } else if ('NULL' == $value) { $sqlValue = 'NULL'; } else { $sqlValue = DB::getPDO()->quote($value, PDO::PARAM_STR); } $fieldAffected = DB::getPDO()->execSql(" insert into CRM_CONFIG (CONF_KEY, CONF_VAL) values ({$sqlConfKey}, {$sqlValue}) on duplicate key update CONF_VAL = {$sqlValue} "); if ($fieldAffected > 0) $affected = 1; } } return $affected; } public function sqlSelect($params = []) { $select = []; $select[] = ['ID_USER', $this->idUser]; $select[] = ['ID_PROCES', $this->idProcesFilter]; foreach ($this->_simpleSchema['root'] as $fieldName => $conf) { if ('@' == substr($fieldName, 0, 1)) continue; $confKey = $this->sqlFieldConfKey($fieldName); if ($confKey) { $sqlLastLoginKey = DB::getPDO()->quote($confKey, PDO::PARAM_STR); $select[] = [$fieldName, "(select CONF_VAL from CRM_CONFIG where CONF_KEY = {$sqlLastLoginKey})"]; } } return implode("\n\t, ", array_map(function ($s) { return "{$s[1]} as {$s[0]}"; }, $select)); } public function getItem($primaryKey, $params = []) { $sqlSelect = $this->sqlSelect(); return DB::getPDO()->fetchFirst(" select {$sqlSelect} "); } public function getItems($params = []) { $sqlOrderBy = ""; $sqlLimitOffset = ""; $sqlWhereAnd = $this->_parseSqlWhere($params); $currSortCol = V::get('order_by', 'ID', $params); $currSortFlip = strtolower(V::get('order_dir', 'desc', $params)); // TODO: validate $currSortCol is in field list // TODO: validate $currSortFlip ('asc' or 'desc') $aliasMap = array(); foreach ($this->_simpleSchema['root'] as $key => $field) { if ('@' === substr($key, 0, 1)) continue; $aliasMap[ $key ] = (!empty($field['@alias'])) ? $field['@alias'] : $key; } // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort"); $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null; if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) { $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}"; } $limit = V::get('limit', 0, $params); $limit = ($limit < 0) ? 0 : $limit; $offset = V::get('limitstart', 0, $params); $offset = ($offset < 0) ? 0 : $offset; if ($limit > 0) $sqlLimitOffset = "limit {$limit} offset {$offset}"; $sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL'; $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL"; $items = []; $items[$this->idUser] = $this->getItem(1); // array_walk($items, function (&$item, $key) { // $item['link_uruchom_filtr_procesu'] = Request::getPathUri() . "index.php?FUNCTION_INIT=MENU_SELECT_PROCES&_action=setPermsByProces&id_proces={$item['ID']}"; // }); return $items; } }