| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- <?php
- Lib::loadClass('DB');
- Lib::loadClass('UsersHelper');
- Lib::loadClass('ProcesHelper');
- Lib::loadClass('TableAcl');
- Lib::loadClass('SchemaReader');
- class UserAcl {
- var $_user_id;
- var $_use_cache = false;
- /**
- * User groups (cached)
- */
- var $_groups = array();
- var $_proces_ids = array();
- var $_proces_tree_flat = array();
- var $_proces_used_ids = array();
- var $_proces_used_map = array();
- var $_proces_init_used_ids = array();
- function __construct($user_id, $use_cache = false) {
- $this->_user_id = $user_id;
- $this->_use_cache = $use_cache;
- $this->_cache_init();
- }
- function fetchGroups() {
- if ($this->_user_id < 0) return false;
- if (!empty($this->_groups)) {
- return $this->_groups;
- }
- $this->_groups = $this->_cache_read('_groups');
- if ($this->_groups != null) {
- return $this->_groups;
- }
- $this->_groups = array();
- $this->_groups = UsersHelper::get_group_by_user($this->_user_id);
- $this->_cache_save('_groups', $this->_groups);
- return $this->_groups;
- }
- function getProcesIds() {
- if (!empty($this->_proces_ids)) {
- return $this->_proces_ids;
- }
- $db = DB::getDB();
- $groups = $this->fetchGroups();
- if (empty($groups)) {
- return false;
- }
- $sql = "select p.`ID`
- from `CRM_PROCES` as p
- left join `CRM_WSKAZNIK` as w on(p.`ID`=w.`ID_PROCES`)
- where
- w.`ID_ZASOB` in(" . implode(",", array_keys($groups)) . ")
- and w.`A_STATUS` in('NORMAL', 'WAITING')
- and p.`A_STATUS` in('NORMAL', 'WAITING')
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $this->_proces_ids [$r->ID] = true;
- }
- $this->_proces_ids = array_keys($this->_proces_ids);
- return $this->_proces_ids;
- }
- function getProcesTree() {
- if (!empty($this->_proces_tree_flat)) {
- return $this->_proces_tree_flat;
- }
- $db = DB::getDB();
- $sql = "select p.`ID`, p.`PARENT_ID`
- from `CRM_PROCES` as p
- where p.`A_STATUS` in('WAITING','NORMAL')
- union
- select p.`IF_TRUE_GOTO` as ID, p.`ID` as PARENT_ID
- from `CRM_PROCES` as p
- where p.`A_STATUS` in('WAITING','NORMAL')
- and p.IF_TRUE_GOTO>0
- and p.IF_TRUE_GOTO_FLAG='GOTO_AND_RETURN'
- ";
- // union select '83','122' union select p.`ID` as ID, p.`IF_TRUE_GOTO` as PARENT_ID from `CRM_PROCES` as p where p.`A_STATUS` in('WAITING','NORMAL') and p.IF_TRUE_GOTO>0
- //union select '83','122'
- //union select '83','2025'
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $this->_proces_tree_flat[$r->PARENT_ID][] = $r->ID;
- }
- return $this->_proces_tree_flat;
- }
- function _cache_init() {
- if (!$this->_use_cache) return;
- if (!isset($_SESSION['UserAcl_cache'])) {
- $_SESSION['UserAcl_cache'] = array();
- }
- else {
- $user_id = V::get('_user_id', 0, $_SESSION['UserAcl_cache'], 'int');
- if ($user_id > 0) {
- if ($user_id != $this->_user_id) {
- // clean cache if another user
- $_SESSION['UserAcl_cache'] = array();
- }
- }
- }
- }
- function _cache_clear() {
- if (!$this->_use_cache) return;
- $_SESSION['UserAcl_cache'] = array();
- }
- /**
- * Read data from cache.
- */
- function _cache_read($key) {
- if (!$this->_use_cache) return null;
- if (array_key_exists($key, $_SESSION['UserAcl_cache'])) {
- return $_SESSION['UserAcl_cache'][$key];
- }
- return null;
- }
- /**
- * Save data in cache.
- */
- function _cache_save($key, $value) {
- if (!$this->_use_cache) return;
- if ($key == 'foundTables') {
- $tblIds = array();
- foreach ($value as $idTable => $tableConfig) {
- $tblIds[] = $idTable;
- $vTableAcl = TableAcl::buildInstance($idTable, $tableConfig);
- }
- $value = $tblIds;
- }
- $_SESSION['UserAcl_cache'][$key] = $value;
- }
- public function getUrls() {
- $urls = $this->_cache_read('foundUrls');
- if (empty($urls)) $urls = array();
- return $urls;
- }
- public function getObjectAcl($sourceName, $objName) {
- if ('default_db' == $sourceName) {
- $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri("{$sourceName}/{$objName}");
- if ($zasobTblInfo) {
- if ($this->hasTableAcl($zasobTblInfo->ID)) {
- return $this->getTableAcl($zasobTblInfo->ID);
- }
- }
- }
- else {
- throw new HttpException("Not Implemented", 501);
- }
- return false;
- }
- public function getTablesAcl() {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW`
- $tbls = array();
- $tblIds = $this->_cache_read('foundTables');
- foreach ($tblIds as $vTableID) {
- $tbls[$vTableID] = TableAcl::getInstance($vTableID);
- }
- return $tbls;
- }
- public function hasTableAcl($tableID) {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW`
- $tbls = $this->_cache_read('foundTables');
- return (is_array($tbls) && in_array($tableID, $tbls));
- }
- public function getTableAcl($tableID) {
- return TableAcl::getInstance($tableID);
- }
- /**
- * Check if perms are only for one proces.
- * @returns int or false
- */
- public function getPermsFiltrProcesId() {// TODO: RMME mved to getFilterIdProces
- return $this->getFilterIdProces();
- }
- public function getFilterIdProces() {
- $procesID = $this->_cache_read('permsByProcesID');
- return ($procesID > 0)? $procesID : false;
- }
- public function fetchAllPerms($force = false) {
- $this->_fetchPerms('All', $force);
- }
- public function fetchProcesPerms($procesID, $force = false) {
- $this->_fetchPerms($procesID, $force);
- }
- /**
- * @param $type - 'All', $procesID
- */
- private function _fetchPerms($type, $force = false) {
- $db = DB::getDB();
- $procesID = 0;// if 0 - All, alse perms by procesID
- $foundUrls = array();
- $foundTbls = array();
- if ($force) {
- $this->_cache_clear();
- }
- $schemaReader = new SchemaReader();
- if ($type == 'All') {
- $schemaReader->getAll();
- } else if (is_numeric($type) && $type > 0) {
- $procesID = (int)$type;
- }
- {// fetch from schema files in SE/schema/process/*.ini.php
- if ($schemaReader->hasProcessConfigs()) {
- foreach ($schemaReader->getProcessConfigs() as $process) {
- DBG::_('DBG_SCH', '1', "process", $process, __CLASS__, __FUNCTION__, __LINE__ );
- if ($process->hasAccess()) {
- $tables = $process->getTables();
- DBG::_('DBG_SCH', '1', "tables", $tables, __CLASS__, __FUNCTION__, __LINE__ );
- foreach ($tables as $vTable) {
- $tblUri = $vTable->getUri();
- $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri($tblUri);
- DBG::_('DBG_SCH', '1', "table(" . $vTable->getLabel() . ")", $zasobTblInfo, __CLASS__, __FUNCTION__, __LINE__ );
- if ($zasobTblInfo) {
- $idTable = $zasobTblInfo->ID;
- if (!array_key_exists($idTable, $foundTbls)) {
- $tableConfig = array();
- $tableConfig['ID_TABLE'] = $idTable;
- $tableConfig['db'] = $zasobTblInfo->P__ID;
- $tableConfig['name'] = $zasobTblInfo->DESC;
- $tableConfig['label'] = $zasobTblInfo->DESC_PL;
- $tableConfig['opis'] = $zasobTblInfo->OPIS;
- $foundTbls[$idTable] = $tableConfig;
- }
- $tableAcl = TableAcl::buildInstance($idTable, $foundTbls[$idTable]);
- $fieldsConfig = array();
- $fldsInfo = ProcesHelper::getZasobTableFieldsInfo($idTable);
- foreach ($vTable->getFields() as $field) {
- $fldInfo = V::get($field->getName(), null, $fldsInfo);
- if ($fldInfo) {
- if (!array_key_exists($fldInfo->ID, $fieldsConfig)) {//if (!$tableAcl->hasField($fldInfo->ID)) {
- $fieldsConfig[$fldInfo->ID] = array();
- $fieldsConfig[$fldInfo->ID]['ID_CELL'] = $fldInfo->ID;
- $fieldsConfig[$fldInfo->ID]['CELL_NAME'] = $fldInfo->DESC;
- $fieldsConfig[$fldInfo->ID]['CELL_DESC'] = $fldInfo->OPIS;
- $fieldsConfig[$fldInfo->ID]['SORT_PRIO'] = $fldInfo->SORT_PRIO;
- $fieldsConfig[$fldInfo->ID]['CELL_LABEL'] = $fldInfo->DESC_PL;
- $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] = '';
- //$tableAcl->addField($fldInfo->ID, $fldInfo->DESC, $fldInfo->OPIS, $fldInfo->SORT_PRIO, $fldInfo->DESC_PL);
- }
- // TODO: $field->getPerms() -> PERM_R, PERM_W, ... etc.?
- $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] .= $field->getPerms();//$tableAcl->setFieldPerms($fldInfo->ID, $field->getPerms());
- }
- }
- $tableAcl->initFieldsFromConfig($fieldsConfig);
- DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- $tableAcl->save();
- DBG::_('DBG_SCH', '1', "tableAcl({$idTable})", $tableAcl, __CLASS__, __FUNCTION__, __LINE__);
- }
- }
- }
- }
- } else {
- DBG::_('DBG_SCH', '1', "NO \$schemaReader->hasProcessConfigs()", null, __CLASS__, __FUNCTION__, __LINE__);
- }
- DBG::_('DBG_SCH', '1', "foundTbls", $foundTbls, __CLASS__, __FUNCTION__, __LINE__);
- }// fetch from schema files
- $this->setFilterIdProces($procesID);//$this->_cache_save('permsByProcesID', $procesID);
- $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
- $userAccessForTables = array();
- $sql = <<<SQL
- select tpvg.`ID_TABLE`
- , tpvg.`ID_DATABASE` as `db`
- , tpvg.`TABLE_NAME` as `name`
- , tpvg.`TABLE_LABEL` as `label`
- , tpvg.`TABLE_DESCRIPTION` as `opis`
- from `CRM_PROCES_idx_TABLE_TO_PROCES_GROUPED_VIEW` as tpvg
- where tpvg.`ID_PROCES` in({$sqlIdProcesListSql})
- group by tpvg.`ID_TABLE`
- SQL;
- //echo'<pre>$userAccessForTables - $sql ';print_r($sql);echo'</pre>';
- $db = DB::getDB();
- $res = $db->query($sql);
- while ($h = $db->fetch_assoc($res)) {
- $userAccessForTables[$h['ID_TABLE']] = $h;
- }
- DBG::_('DBG_SCH', '1', "userAccessForTables", $userAccessForTables, __CLASS__, __FUNCTION__, __LINE__ );
- foreach ($userAccessForTables as $idTable => $tableConfig) {
- if (!array_key_exists($idTable, $foundTbls)) {
- $foundTbls[$idTable] = $tableConfig;
- }
- }
- $userAccessForUrls = array();
- $sql = <<<SQL
- select upvg.`ID_URL`
- , upvg.`URL_LINK` as `link`
- , upvg.`URL_LABEL` as `label`
- , upvg.`URL_DESC` as `opis`
- from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
- where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
- group by upvg.`ID_URL`
- SQL;
- //echo'<pre>$userAccessForUrls - $sql ';print_r($sql);echo'</pre>';
- $db = DB::getDB();
- $res = $db->query($sql);
- while ($h = $db->fetch_assoc($res)) {
- $userAccessForUrls[$h['ID_URL']] = $h;
- }
- //echo'<pre>$userAccessForUrls ';print_r($userAccessForUrls);echo'</pre>';
- DBG::_('DBG_SCH', '2', "userAccessForUrls", $userAccessForUrls, __CLASS__, __FUNCTION__, __LINE__ );
- foreach ($userAccessForUrls as $idUrl => $vUrlConfig) {
- $foundUrls[$idUrl] = $vUrlConfig['opis'];
- }
- $this->_cache_save('foundUrls', $foundUrls);
- $this->_cache_save('foundTables', $foundTbls);
- }
- public function setFilterIdProces($procesID) {
- $this->_cache_save('permsByProcesID', $procesID);
- }
- public function getPermsForTable($idTable) {
- $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
- $tableCellToProcesSql = <<<SQL
- select z.`ID` as `ID_CELL`
- , z.`DESC` as `CELL_NAME`
- , z.`DESC_PL` as `CELL_LABEL`
- , z.`OPIS` as `CELL_DESC`
- , z.`SORT_PRIO` as `SORT_PRIO`
- , zp.`ID` as `ID_TABLE`
- -- , zp.`DESC` as `TABLE_NAME`
- -- , wsk.`ID_PROCES` as `ID_PROCES`
- , sum(IF(przyp.`FORM_TREAT` & 2, 1, 0)) as PERM_R
- , sum(IF(przyp.`FORM_TREAT` & 4, 1, 0)) as PERM_W
- , sum(IF(przyp.`FORM_TREAT` & 8, 1, 0)) as PERM_X
- , sum(IF(przyp.`FORM_TREAT` & 16, 1, 0)) as PERM_C
- , sum(IF(przyp.`FORM_TREAT` & 32, 1, 0)) as PERM_S
- , sum(IF(przyp.`FORM_TREAT` & 64, 1, 0)) as PERM_O
- , sum(IF(przyp.`FORM_TREAT` & 128, 1, 0)) as PERM_V
- , sum(IF(przyp.`FORM_TREAT` & 256, 1, 0)) as PERM_E
- from `CRM_LISTA_ZASOBOW` z
- join `CRM_LISTA_ZASOBOW` zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE`='TABELA' and zp.`A_STATUS` in('WAITING','NORMAL'))
- join `CRM_WSKAZNIK` wsk on(wsk.`ID_ZASOB`=z.`ID` and wsk.`A_STATUS` in('WAITING','NORMAL'))
- join `CRM_PROCES` p on(p.`ID`=wsk.`ID_PROCES` and p.`A_STATUS` in('WAITING','NORMAL'))
- join `CRM_PRZYPADEK` as przyp on (przyp.`ID`=wsk.`ID_PRZYPADEK`)
- where z.`TYPE`='KOMORKA'
- and z.`A_STATUS` in('WAITING','NORMAL')
- and zp.`ID`='{$idTable}'
- and wsk.`ID_PROCES` in({$sqlIdProcesListSql})
- -- group by z.`ID`, wsk.`ID_PROCES`
- group by z.`ID`
- order by z.`SORT_PRIO`
- SQL;
- //echo'<pre>UserAcl::getPermsForTable('.$idTable.')::$tableCellToProcesSql ';print_r($tableCellToProcesSql);echo'</pre>';
- $userPermsForTable = array();
- $db = DB::getDB();
- $res = $db->query($tableCellToProcesSql);
- while ($h = $db->fetch_assoc($res)) {
- $idCell = $h['ID_CELL'];
- $userPermsForTable[$idCell] = $h;
- }
- return $userPermsForTable;
- }
- public function getUsedUserGroupIds() {
- $idUserGroupList = User::getGroupsIds();
- // TODO: acl filtr by group ids
- return $idUserGroupList;
- }
- public function getUsedUserProcesIdsSql() {
- $filterIdProces = $this->getFilterIdProces();
- if ($filterIdProces > 0) {
- return <<<SQL
- select i.`ID_PROCES`
- from `CRM_PROCES_idx` i
- where i.`idx_MAIN_PROCES_INIT_ID`='{$filterIdProces}'
- SQL;
- }
- $idUserGroupList = $this->getUsedUserGroupIds();
- $sqlIdUserGroupList = implode(",", $idUserGroupList);
- return <<<SQL
- select gi.`ID_PROCES`
- from `CRM_PROCES_idx_GROUP_to_PROCES` gi
- where gi.`ID_GROUP` in({$sqlIdUserGroupList})
- SQL;
- }
- public function getProcesMaxUpdateDate($idProcesInit) {
- $maxUpdateDate = null;
- $db = DB::getDB();
- $sql = <<<SQL
- select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
- from `CRM_PROCES` as p
- where p.`ID` in(
- select i.`ID_PROCES`
- from `CRM_PROCES_idx` i
- where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
- )
- SQL;
- $res = $db->query($sql);
- if ($r = $db->fetch($res)) {
- $maxUpdateDate = $r->max_update_date;
- }
- return $maxUpdateDate;
- }
- /**
- * Ids List of Proces Init for user (skip filters)
- */
- public function getUserProcesInitIds() {
- $procesInitList = $this->getUserProcesInitList();
- return array_keys($procesInitList);
- }
- /**
- * List of Proces Init for user (skip filters)
- */
- public function getUserProcesInitList() {
- $userProcesInitList = array();
- $idUserGroupList = User::getGroupsIds();
- $sqlIdUserGroupList = implode(",", $idUserGroupList);
- $sqlIdProcesListSql = <<<SQL
- select gi.`ID_PROCES`
- from `CRM_PROCES_idx_GROUP_to_PROCES` gi
- where gi.`ID_GROUP` in({$sqlIdUserGroupList})
- SQL;
- $fetchUserProcesInitListSql = <<<SQL
- select p.`ID`, p.`DESC`
- from `CRM_PROCES_idx` i
- join `CRM_PROCES` p on(p.`ID`=i.`idx_PROCES_INIT_ID`)
- where i.`ID_PROCES` in({$sqlIdProcesListSql})
- group by p.`ID`
- order by p.`SORT_PRIO`
- SQL;
- $db = DB::getDB();
- $res = $db->query($fetchUserProcesInitListSql);
- while ($r = $db->fetch($res)) {
- $userProcesInitList[$r->ID] = $r->DESC;
- }
- return $userProcesInitList;
- }
- /**
- * Ids List of Proces Init for user (use filters)
- */
- public function getUsedUserProcesInitIds() {
- $usedProcesInitList = $this->getUsedUserProcesInitList();
- return array_keys($usedProcesInitList);
- }
- /**
- * List of Proces Init for user (use filters)
- */
- public function getUsedUserProcesInitList() {
- $filterIdProces = $this->getFilterIdProces();
- if ($filterIdProces > 0) {
- return $filterIdProces;
- }
- $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
- $fetchUsedProcesInitListSql = <<<SQL
- select p.`ID`, p.`DESC`
- from `CRM_PROCES` p
- where p.`ID` in({$sqlIdProcesListSql})
- and p.`TYPE`='PROCES_INIT'
- order by p.`SORT_PRIO`
- SQL;
- $usedProcesInitList = array();
- $db = DB::getDB();
- $res = $db->query($fetchUsedProcesInitListSql);
- while ($r = $db->fetch($res)) {
- $usedProcesInitList[$r->ID] = $r->DESC;
- }
- return $usedProcesInitList;
- }
- /**
- * Ids List of Proces Init for given tabel (skip filters)
- */
- public function getTableProcesInitIds($idTable) {// TODO: use in TableAjax
- $procesInitList = $this->getTableProcesInitList($idTable);
- return array_keys($procesInitList);
- }
- /**
- * List of Proces Init for given table (skip filters)
- */
- public function getTableProcesInitList($idTable) {// TODO: use in TableAjax
- $tableProcesInitList = array();
- $sqlIdProcesListSql = <<<SQL
- select tpv.`ID_PROCES`
- from `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
- where tpv.`ID_TABLE`='{$idTable}'
- SQL;
- $fetchTableProcesInitListSql = <<<SQL
- -- time ~0.07 -- no goto and return
- select p.`ID`, p.`DESC`
- from `CRM_PROCES` p
- where p.`ID` in(
- select i.`idx_PROCES_INIT_ID`
- from `CRM_PROCES_idx` i
- where i.`ID_PROCES` in({$sqlIdProcesListSql})
- )
- and p.`TYPE`='PROCES_INIT'
- order by p.`SORT_PRIO`
- SQL;
- /*
- SELECT p.`ID` , p.`DESC`
- FROM `CRM_PROCES` p
- WHERE p.`ID`
- IN (
- SELECT i.`idx_PROCES_INIT_ID`
- FROM `CRM_PROCES_idx` i
- WHERE i.`ID_PROCES`
- IN (
- SELECT tpv.`ID_PROCES`
- FROM `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
- WHERE tpv.`ID_TABLE` = '13051'
- )
- )
- AND p.`TYPE` = 'PROCES_INIT'
- order by p.`SORT_PRIO`
- */
- $fetchTableProcesInitListSql = <<<SQL
- -- time ~0.15s
- select p.`ID`, p.`DESC`
- from `CRM_PROCES` p
- where p.`ID` in(
- select i.`idx_PROCES_INIT_ID`
- from `CRM_PROCES_idx` i
- where i.`ID_PROCES` in({$sqlIdProcesListSql})
- union
- select ig.`idx_PROCES_INIT_ID`
- from `CRM_PROCES_idx` i
- join `CRM_PROCES_idx` ig on(ig.`ID_PROCES`=i.`idx_PROCES_WITH_GROUPS_ID`)
- where i.`ID_PROCES` in({$sqlIdProcesListSql})
- )
- and p.`TYPE`='PROCES_INIT'
- order by p.`SORT_PRIO`
- SQL;
- $fetchTableProcesInitListSql = <<<SQL
- -- time ~0.14
- select p.`ID`, p.`DESC`
- from `CRM_PROCES` p
- where p.`ID` in(
- select i.`idx_PROCES_INIT_ID`
- from `CRM_PROCES_idx` i
- where i.`ID_PROCES` in({$sqlIdProcesListSql})
- or i.`ID_PROCES` in(
- select ig.`idx_PROCES_WITH_GROUPS_ID`
- from `CRM_PROCES_idx` ig
- where ig.`ID_PROCES` in({$sqlIdProcesListSql})
- )
- )
- and p.`TYPE`='PROCES_INIT'
- order by p.`SORT_PRIO`
- SQL;
- //echo'<pre>$fetchTableProcesInitListSql('.$idTable.') ';print_r($fetchTableProcesInitListSql);echo'</pre>';
- $tableProcesInitList = array();
- $db = DB::getDB();
- $res = $db->query($fetchTableProcesInitListSql);
- while ($r = $db->fetch($res)) {
- $tableProcesInitList[$r->ID] = $r->DESC;
- }
- return $tableProcesInitList;
- }
- public function canExecuteProcesInit($idProcesInit) {
- $canExecuteProcesInit = false;
- $idProcesInit = (int)$idProcesInit;
- if (!$idProcesInit) return false;
- $idUserGroupList = User::getGroupsIds();
- $sqlIdUserGroupList = implode(",", $idUserGroupList);
- $checkProcesAccessSql = <<<SQL
- select count(*) as cnt
- from `CRM_PROCES_idx_GROUP_to_PROCES` gi
- join `CRM_PROCES` p on(p.`ID`=gi.`ID_PROCES` and p.`TYPE`='PROCES_INIT')
- where gi.`ID_GROUP` in({$sqlIdUserGroupList})
- and gi.`ID_PROCES`='{$idProcesInit}'
- SQL;
- $db = DB::getDB();
- $res = $db->query($checkProcesAccessSql);
- if ($r = $db->fetch($res)) {
- if ($r->cnt > 0) {
- $canExecuteProcesInit = true;
- }
- }
- return $canExecuteProcesInit;
- }
- public function getProcesInitMapTreeOnlyIds($ids) {
- $mapTree = array();
- $map = $this->getProcesInitMapOnlyIds($ids);
- foreach ($map as $r) {
- if ('PROCES_INIT' == $r->TYPE) {
- $mapTree[$r->ID_PROCES] = array();
- }
- }
- foreach ($map as $r) {
- if ('GOTO_AND_RETURN' == $r->TYPE) {
- $mapTree[$r->idx_MAIN_PROCES_INIT_ID][$r->ID_PROCES] = true;
- }
- }
- return $mapTree;
- }
- public function getProcesInitMapOnlyIds($ids) {
- $map = array();
- $sqlIds = V::filter($ids, array('V', 'filterPositiveInteger'));
- $sqlIds = implode(',', $sqlIds);
- if (empty($sqlIds)) return $map;
- $sql = <<<SQL
- select i.`ID_PROCES`
- , i.`PARENT_ID`
- , i.`TYPE`
- , i.`idx_PROCES_INIT_ID`
- , i.`idx_MAIN_PROCES_INIT_ID`
- , i.`idx_PROCES_WITH_GROUPS_ID`
- from `CRM_PROCES_idx` i
- where i.`ID_PROCES` in({$sqlIds})
- and i.`idx_MAIN_PROCES_INIT_ID` in({$sqlIds})
- SQL;
- DBG::_('DBG_MAP', '1', "MAP SQL", $sql, __CLASS__, __FUNCTION__, __LINE__);
- $db = DB::getDB();
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $map[] = $r;
- }
- return $map;
- }
- }
|