[ '@namespace' => 'default_objects/UserProcess', 'ID' => [ '@type' => 'xsd:integer' ], 'PARENT_ID' => [ '@type' => 'xsd:integer' ], 'nazwa' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ], 'opis' => [ '@type' => 'xsd:string', '@alias' => 'OPIS' ], 'link_uruchom_filtr_procesu' => [ '@type' => 'p5:www_link' ], 'autor' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_CREATE_AUTHOR' ], 'utworzono' => [ '@type' => 'xsd:date' , '@alias' => 'A_RECORD_CREATE_DATE' ], 'zaktualizował' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_UPDATE_AUTHOR' ], 'zaktualizowano' => [ '@type' => 'xsd:date', '@alias' => 'A_RECORD_UPDATE_DATE' ] ] ]; public $_rootTableName = 'CRM_PROCES'; public $idUser = null; public function __construct($simpleSchema = null) { parent::__construct($simpleSchema); $this->idUser = User::getID();// default - current user } public function setIdUser($idUser) { $this->idUser = intval($idUser); } public function getIdUser() { return $this->idUser; } public function getTotal($params = []) { $sqlWhereAnd = $this->_parseSqlWhere($params); $idGroupList = $this->_getUserIdGroupList(); if (empty($idGroupList)) throw new Exception("Brak przyipsanych grup do użytwkonika"); $sqlIdGroupCsv = implode(",", $idGroupList); return DB::getPDO()->fetchValue(" select count(1) as total from `CRM_PROCES` p where p.`TYPE` = 'PROCES_INIT' and p.`A_STATUS` not in('DELETED', 'OFF_HARD', 'OFF_SOFT') and p.ID in ( select gi.ID_PROCES_INIT from `CRM_PROCES_idx_GROUP_to_INIT_VIEW` as gi where gi.ID_GROUP in({$sqlIdGroupCsv}) ) {$sqlWhereAnd} "); } public function _parseSqlWhere($params = []) { $sqlWhereAnd = ""; // TODO: parse where/ogc, etc. return $sqlWhereAnd; } 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 p.`{$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}"; $idGroupList = $this->_getUserIdGroupList(); if (empty($idGroupList)) throw new Exception("Brak przyipsanych grup do użytwkonika"); $sqlIdGroupCsv = implode(",", $idGroupList); $items = DB::getPDO()->fetchAllByKey(" select p.ID , p.PARENT_ID , p.`DESC` as nazwa , p.`OPIS` as opis , p.A_RECORD_CREATE_AUTHOR as `autor` , p.A_RECORD_CREATE_DATE as `utworzono` , p.A_RECORD_UPDATE_AUTHOR as `zaktualizował` , p.A_RECORD_UPDATE_DATE as `zaktualizowano` from `CRM_PROCES` p where p.`TYPE` = 'PROCES_INIT' and p.`A_STATUS` not in('DELETED', 'OFF_HARD', 'OFF_SOFT') and p.ID in ( select gi.ID_PROCES_INIT from `CRM_PROCES_idx_GROUP_to_INIT_VIEW` as gi where gi.ID_GROUP in({$sqlIdGroupCsv}) ) {$sqlWhereAnd} group by p.ID {$sqlOrderBy} {$sqlLimitOffset} ", 'ID'); 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; } public function _getUserIdGroupList() { return array_map( function ($row) { return $row['ID']; } , DB::getPDO()->fetchAll(" select z.ID from `CRM_AUTH_PROFILE` as up left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`) where up.`REMOTE_ID`='{$this->idUser}' and up.`A_STATUS` in('WAITING', 'NORMAL') and up.`REMOTE_TABLE`='ADMIN_USERS' and z.`ID` is not null and z.`TYPE` in('STANOWISKO','PODMIOT','DZIAL') ") ); } }