UserAcl.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. Lib::loadClass('DB');
  3. Lib::loadClass('UsersHelper');
  4. Lib::loadClass('ProcesHelper');
  5. Lib::loadClass('TableAcl');
  6. Lib::loadClass('SchemaReader');
  7. class UserAcl {
  8. var $_user_id;
  9. var $_use_cache = false;
  10. /**
  11. * User groups (cached)
  12. */
  13. var $_groups = array();
  14. var $_proces_ids = array();
  15. var $_proces_tree_flat = array();
  16. var $_proces_used_ids = array();
  17. var $_proces_used_map = array();
  18. var $_proces_init_used_ids = array();
  19. function __construct($user_id, $use_cache = false) {
  20. $this->_user_id = $user_id;
  21. $this->_use_cache = $use_cache;
  22. $this->_cache_init();
  23. }
  24. function fetchGroups() {
  25. if ($this->_user_id < 0) return false;
  26. if (!empty($this->_groups)) {
  27. return $this->_groups;
  28. }
  29. $this->_groups = $this->_cache_read('_groups');
  30. if ($this->_groups != null) {
  31. return $this->_groups;
  32. }
  33. $this->_groups = array();
  34. $this->_groups = UsersHelper::get_group_by_user($this->_user_id);
  35. $this->_cache_save('_groups', $this->_groups);
  36. return $this->_groups;
  37. }
  38. function getProcesIds() {
  39. if (!empty($this->_proces_ids)) {
  40. return $this->_proces_ids;
  41. }
  42. $db = DB::getDB();
  43. $groups = $this->fetchGroups();
  44. if (empty($groups)) {
  45. return false;
  46. }
  47. $sql = "select p.`ID`
  48. from `CRM_PROCES` as p
  49. left join `CRM_WSKAZNIK` as w on(p.`ID`=w.`ID_PROCES`)
  50. where
  51. w.`ID_ZASOB` in(" . implode(",", array_keys($groups)) . ")
  52. and w.`A_STATUS` in('NORMAL', 'WAITING')
  53. and p.`A_STATUS` in('NORMAL', 'WAITING')
  54. ";
  55. $res = $db->query($sql);
  56. while ($r = $db->fetch($res)) {
  57. $this->_proces_ids [$r->ID] = true;
  58. }
  59. $this->_proces_ids = array_keys($this->_proces_ids);
  60. return $this->_proces_ids;
  61. }
  62. function getProcesTree() {
  63. if (!empty($this->_proces_tree_flat)) {
  64. return $this->_proces_tree_flat;
  65. }
  66. $db = DB::getDB();
  67. $sql = "select p.`ID`, p.`PARENT_ID`
  68. from `CRM_PROCES` as p
  69. where p.`A_STATUS` in('WAITING','NORMAL')
  70. union
  71. select p.`IF_TRUE_GOTO` as ID, p.`ID` as PARENT_ID
  72. from `CRM_PROCES` as p
  73. where p.`A_STATUS` in('WAITING','NORMAL')
  74. and p.IF_TRUE_GOTO>0
  75. and p.IF_TRUE_GOTO_FLAG='GOTO_AND_RETURN'
  76. ";
  77. // 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
  78. //union select '83','122'
  79. //union select '83','2025'
  80. $res = $db->query($sql);
  81. while ($r = $db->fetch($res)) {
  82. $this->_proces_tree_flat[$r->PARENT_ID][] = $r->ID;
  83. }
  84. return $this->_proces_tree_flat;
  85. }
  86. function _cache_init() {
  87. if (!$this->_use_cache) return;
  88. if (!isset($_SESSION['UserAcl_cache'])) {
  89. $_SESSION['UserAcl_cache'] = array();
  90. }
  91. else {
  92. $user_id = V::get('_user_id', 0, $_SESSION['UserAcl_cache'], 'int');
  93. if ($user_id > 0) {
  94. if ($user_id != $this->_user_id) {
  95. // clean cache if another user
  96. $_SESSION['UserAcl_cache'] = array();
  97. }
  98. }
  99. }
  100. }
  101. function _cache_clear() {
  102. if (!$this->_use_cache) return;
  103. $_SESSION['UserAcl_cache'] = array();
  104. }
  105. /**
  106. * Read data from cache.
  107. */
  108. function _cache_read($key) {
  109. if (!$this->_use_cache) return null;
  110. if (array_key_exists($key, $_SESSION['UserAcl_cache'])) {
  111. return $_SESSION['UserAcl_cache'][$key];
  112. }
  113. return null;
  114. }
  115. /**
  116. * Save data in cache.
  117. */
  118. function _cache_save($key, $value) {
  119. if (!$this->_use_cache) return;
  120. if ($key == 'foundTables') {
  121. $tblIds = array();
  122. foreach ($value as $idTable => $tableConfig) {
  123. $tblIds[] = $idTable;
  124. $vTableAcl = TableAcl::buildInstance($idTable, $tableConfig);
  125. }
  126. $value = $tblIds;
  127. }
  128. $_SESSION['UserAcl_cache'][$key] = $value;
  129. }
  130. public function getUrls() {
  131. $urls = $this->_cache_read('foundUrls');
  132. if (empty($urls)) $urls = array();
  133. return $urls;
  134. }
  135. public function getObjectAcl($sourceName, $objName) {
  136. if ('default_db' == $sourceName) {
  137. $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri("{$sourceName}/{$objName}");
  138. if ($zasobTblInfo) {
  139. if ($this->hasTableAcl($zasobTblInfo->ID)) {
  140. return $this->getTableAcl($zasobTblInfo->ID);
  141. }
  142. }
  143. }
  144. else {
  145. throw new HttpException("Not Implemented", 501);
  146. }
  147. return false;
  148. }
  149. public function getTablesAcl() {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW`
  150. $tbls = array();
  151. $tblIds = $this->_cache_read('foundTables');
  152. foreach ($tblIds as $vTableID) {
  153. $tbls[$vTableID] = TableAcl::getInstance($vTableID);
  154. }
  155. return $tbls;
  156. }
  157. public function hasTableAcl($tableID) {// TODO: read from `CRM_PROCES_idx_TABLE_TO_USER_VIEW`
  158. $tbls = $this->_cache_read('foundTables');
  159. return (is_array($tbls) && in_array($tableID, $tbls));
  160. }
  161. public function getTableAcl($tableID) {
  162. return TableAcl::getInstance($tableID);
  163. }
  164. /**
  165. * Check if perms are only for one proces.
  166. * @returns int or false
  167. */
  168. public function getPermsFiltrProcesId() {// TODO: RMME mved to getFilterIdProces
  169. return $this->getFilterIdProces();
  170. }
  171. public function getFilterIdProces() {
  172. $procesID = $this->_cache_read('permsByProcesID');
  173. return ($procesID > 0)? $procesID : false;
  174. }
  175. public function fetchAllPerms($force = false) {
  176. $this->_fetchPerms('All', $force);
  177. }
  178. public function fetchProcesPerms($procesID, $force = false) {
  179. $this->_fetchPerms($procesID, $force);
  180. }
  181. /**
  182. * @param $type - 'All', $procesID
  183. */
  184. private function _fetchPerms($type, $force = false) {
  185. $db = DB::getDB();
  186. $procesID = 0;// if 0 - All, alse perms by procesID
  187. $foundUrls = array();
  188. $foundTbls = array();
  189. if ($force) {
  190. $this->_cache_clear();
  191. }
  192. $schemaReader = new SchemaReader();
  193. if ($type == 'All') {
  194. $schemaReader->getAll();
  195. } else if (is_numeric($type) && $type > 0) {
  196. $procesID = (int)$type;
  197. }
  198. {// fetch from schema files in SE/schema/process/*.ini.php
  199. if ($schemaReader->hasProcessConfigs()) {
  200. foreach ($schemaReader->getProcessConfigs() as $process) {
  201. DBG::_('DBG_SCH', '1', "process", $process, __CLASS__, __FUNCTION__, __LINE__ );
  202. if ($process->hasAccess()) {
  203. $tables = $process->getTables();
  204. DBG::_('DBG_SCH', '1', "tables", $tables, __CLASS__, __FUNCTION__, __LINE__ );
  205. foreach ($tables as $vTable) {
  206. $tblUri = $vTable->getUri();
  207. $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri($tblUri);
  208. DBG::_('DBG_SCH', '1', "table(" . $vTable->getLabel() . ")", $zasobTblInfo, __CLASS__, __FUNCTION__, __LINE__ );
  209. if ($zasobTblInfo) {
  210. $idTable = $zasobTblInfo->ID;
  211. if (!array_key_exists($idTable, $foundTbls)) {
  212. $tableConfig = array();
  213. $tableConfig['ID_TABLE'] = $idTable;
  214. $tableConfig['db'] = $zasobTblInfo->P__ID;
  215. $tableConfig['name'] = $zasobTblInfo->DESC;
  216. $tableConfig['label'] = $zasobTblInfo->DESC_PL;
  217. $tableConfig['opis'] = $zasobTblInfo->OPIS;
  218. $foundTbls[$idTable] = $tableConfig;
  219. }
  220. $tableAcl = TableAcl::buildInstance($idTable, $foundTbls[$idTable]);
  221. $fieldsConfig = array();
  222. $fldsInfo = ProcesHelper::getZasobTableFieldsInfo($idTable);
  223. foreach ($vTable->getFields() as $field) {
  224. $fldInfo = V::get($field->getName(), null, $fldsInfo);
  225. if ($fldInfo) {
  226. if (!array_key_exists($fldInfo->ID, $fieldsConfig)) {//if (!$tableAcl->hasField($fldInfo->ID)) {
  227. $fieldsConfig[$fldInfo->ID] = array();
  228. $fieldsConfig[$fldInfo->ID]['ID_CELL'] = $fldInfo->ID;
  229. $fieldsConfig[$fldInfo->ID]['CELL_NAME'] = $fldInfo->DESC;
  230. $fieldsConfig[$fldInfo->ID]['CELL_DESC'] = $fldInfo->OPIS;
  231. $fieldsConfig[$fldInfo->ID]['SORT_PRIO'] = $fldInfo->SORT_PRIO;
  232. $fieldsConfig[$fldInfo->ID]['CELL_LABEL'] = $fldInfo->DESC_PL;
  233. $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] = '';
  234. //$tableAcl->addField($fldInfo->ID, $fldInfo->DESC, $fldInfo->OPIS, $fldInfo->SORT_PRIO, $fldInfo->DESC_PL);
  235. }
  236. // TODO: $field->getPerms() -> PERM_R, PERM_W, ... etc.?
  237. $fieldsConfig[$fldInfo->ID]['FORM_TREAT'] .= $field->getPerms();//$tableAcl->setFieldPerms($fldInfo->ID, $field->getPerms());
  238. }
  239. }
  240. $tableAcl->initFieldsFromConfig($fieldsConfig);
  241. DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  242. $tableAcl->save();
  243. DBG::_('DBG_SCH', '1', "tableAcl({$idTable})", $tableAcl, __CLASS__, __FUNCTION__, __LINE__);
  244. }
  245. }
  246. }
  247. }
  248. } else {
  249. DBG::_('DBG_SCH', '1', "NO \$schemaReader->hasProcessConfigs()", null, __CLASS__, __FUNCTION__, __LINE__);
  250. }
  251. DBG::_('DBG_SCH', '1', "foundTbls", $foundTbls, __CLASS__, __FUNCTION__, __LINE__);
  252. }// fetch from schema files
  253. $this->setFilterIdProces($procesID);//$this->_cache_save('permsByProcesID', $procesID);
  254. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  255. $userAccessForTables = array();
  256. $sql = <<<SQL
  257. select tpvg.`ID_TABLE`
  258. , tpvg.`ID_DATABASE` as `db`
  259. , tpvg.`TABLE_NAME` as `name`
  260. , tpvg.`TABLE_LABEL` as `label`
  261. , tpvg.`TABLE_DESCRIPTION` as `opis`
  262. from `CRM_PROCES_idx_TABLE_TO_PROCES_GROUPED_VIEW` as tpvg
  263. where tpvg.`ID_PROCES` in({$sqlIdProcesListSql})
  264. group by tpvg.`ID_TABLE`
  265. SQL;
  266. //echo'<pre>$userAccessForTables - $sql ';print_r($sql);echo'</pre>';
  267. $db = DB::getDB();
  268. $res = $db->query($sql);
  269. while ($h = $db->fetch_assoc($res)) {
  270. $userAccessForTables[$h['ID_TABLE']] = $h;
  271. }
  272. DBG::_('DBG_SCH', '1', "userAccessForTables", $userAccessForTables, __CLASS__, __FUNCTION__, __LINE__ );
  273. foreach ($userAccessForTables as $idTable => $tableConfig) {
  274. if (!array_key_exists($idTable, $foundTbls)) {
  275. $foundTbls[$idTable] = $tableConfig;
  276. }
  277. }
  278. $userAccessForUrls = array();
  279. $sql = <<<SQL
  280. select upvg.`ID_URL`
  281. , upvg.`URL_LINK` as `link`
  282. , upvg.`URL_LABEL` as `label`
  283. , upvg.`URL_DESC` as `opis`
  284. from `CRM_PROCES_idx_URL_TO_PROCES_VIEW` as upvg
  285. where upvg.`ID_PROCES` in({$sqlIdProcesListSql})
  286. group by upvg.`ID_URL`
  287. SQL;
  288. //echo'<pre>$userAccessForUrls - $sql ';print_r($sql);echo'</pre>';
  289. $db = DB::getDB();
  290. $res = $db->query($sql);
  291. while ($h = $db->fetch_assoc($res)) {
  292. $userAccessForUrls[$h['ID_URL']] = $h;
  293. }
  294. //echo'<pre>$userAccessForUrls ';print_r($userAccessForUrls);echo'</pre>';
  295. DBG::_('DBG_SCH', '2', "userAccessForUrls", $userAccessForUrls, __CLASS__, __FUNCTION__, __LINE__ );
  296. foreach ($userAccessForUrls as $idUrl => $vUrlConfig) {
  297. $foundUrls[$idUrl] = $vUrlConfig['opis'];
  298. }
  299. $this->_cache_save('foundUrls', $foundUrls);
  300. $this->_cache_save('foundTables', $foundTbls);
  301. }
  302. public function setFilterIdProces($procesID) {
  303. $this->_cache_save('permsByProcesID', $procesID);
  304. }
  305. public function getPermsForTable($idTable) {
  306. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  307. $tableCellToProcesSql = <<<SQL
  308. select z.`ID` as `ID_CELL`
  309. , z.`DESC` as `CELL_NAME`
  310. , z.`DESC_PL` as `CELL_LABEL`
  311. , z.`OPIS` as `CELL_DESC`
  312. , z.`SORT_PRIO` as `SORT_PRIO`
  313. , zp.`ID` as `ID_TABLE`
  314. -- , zp.`DESC` as `TABLE_NAME`
  315. -- , wsk.`ID_PROCES` as `ID_PROCES`
  316. , sum(IF(przyp.`FORM_TREAT` & 2, 1, 0)) as PERM_R
  317. , sum(IF(przyp.`FORM_TREAT` & 4, 1, 0)) as PERM_W
  318. , sum(IF(przyp.`FORM_TREAT` & 8, 1, 0)) as PERM_X
  319. , sum(IF(przyp.`FORM_TREAT` & 16, 1, 0)) as PERM_C
  320. , sum(IF(przyp.`FORM_TREAT` & 32, 1, 0)) as PERM_S
  321. , sum(IF(przyp.`FORM_TREAT` & 64, 1, 0)) as PERM_O
  322. , sum(IF(przyp.`FORM_TREAT` & 128, 1, 0)) as PERM_V
  323. , sum(IF(przyp.`FORM_TREAT` & 256, 1, 0)) as PERM_E
  324. from `CRM_LISTA_ZASOBOW` z
  325. join `CRM_LISTA_ZASOBOW` zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE`='TABELA' and zp.`A_STATUS` in('WAITING','NORMAL'))
  326. join `CRM_WSKAZNIK` wsk on(wsk.`ID_ZASOB`=z.`ID` and wsk.`A_STATUS` in('WAITING','NORMAL'))
  327. join `CRM_PROCES` p on(p.`ID`=wsk.`ID_PROCES` and p.`A_STATUS` in('WAITING','NORMAL'))
  328. join `CRM_PRZYPADEK` as przyp on (przyp.`ID`=wsk.`ID_PRZYPADEK`)
  329. where z.`TYPE`='KOMORKA'
  330. and z.`A_STATUS` in('WAITING','NORMAL')
  331. and zp.`ID`='{$idTable}'
  332. and wsk.`ID_PROCES` in({$sqlIdProcesListSql})
  333. -- group by z.`ID`, wsk.`ID_PROCES`
  334. group by z.`ID`
  335. order by z.`SORT_PRIO`
  336. SQL;
  337. //echo'<pre>UserAcl::getPermsForTable('.$idTable.')::$tableCellToProcesSql ';print_r($tableCellToProcesSql);echo'</pre>';
  338. $userPermsForTable = array();
  339. $db = DB::getDB();
  340. $res = $db->query($tableCellToProcesSql);
  341. while ($h = $db->fetch_assoc($res)) {
  342. $idCell = $h['ID_CELL'];
  343. $userPermsForTable[$idCell] = $h;
  344. }
  345. return $userPermsForTable;
  346. }
  347. public function getUsedUserGroupIds() {
  348. $idUserGroupList = User::getGroupsIds();
  349. // TODO: acl filtr by group ids
  350. return $idUserGroupList;
  351. }
  352. public function getUsedUserProcesIdsSql() {
  353. $filterIdProces = $this->getFilterIdProces();
  354. if ($filterIdProces > 0) {
  355. return <<<SQL
  356. select i.`ID_PROCES`
  357. from `CRM_PROCES_idx` i
  358. where i.`idx_MAIN_PROCES_INIT_ID`='{$filterIdProces}'
  359. SQL;
  360. }
  361. $idUserGroupList = $this->getUsedUserGroupIds();
  362. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  363. return <<<SQL
  364. select gi.`ID_PROCES`
  365. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  366. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  367. SQL;
  368. }
  369. public function getProcesMaxUpdateDate($idProcesInit) {
  370. $maxUpdateDate = null;
  371. $db = DB::getDB();
  372. $sql = <<<SQL
  373. select max(p.`A_RECORD_UPDATE_DATE`) as max_update_date
  374. from `CRM_PROCES` as p
  375. where p.`ID` in(
  376. select i.`ID_PROCES`
  377. from `CRM_PROCES_idx` i
  378. where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
  379. )
  380. SQL;
  381. $res = $db->query($sql);
  382. if ($r = $db->fetch($res)) {
  383. $maxUpdateDate = $r->max_update_date;
  384. }
  385. return $maxUpdateDate;
  386. }
  387. /**
  388. * Ids List of Proces Init for user (skip filters)
  389. */
  390. public function getUserProcesInitIds() {
  391. $procesInitList = $this->getUserProcesInitList();
  392. return array_keys($procesInitList);
  393. }
  394. /**
  395. * List of Proces Init for user (skip filters)
  396. */
  397. public function getUserProcesInitList() {
  398. $userProcesInitList = array();
  399. $idUserGroupList = User::getGroupsIds();
  400. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  401. $sqlIdProcesListSql = <<<SQL
  402. select gi.`ID_PROCES`
  403. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  404. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  405. SQL;
  406. $fetchUserProcesInitListSql = <<<SQL
  407. select p.`ID`, p.`DESC`
  408. from `CRM_PROCES_idx` i
  409. join `CRM_PROCES` p on(p.`ID`=i.`idx_PROCES_INIT_ID`)
  410. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  411. group by p.`ID`
  412. order by p.`SORT_PRIO`
  413. SQL;
  414. $db = DB::getDB();
  415. $res = $db->query($fetchUserProcesInitListSql);
  416. while ($r = $db->fetch($res)) {
  417. $userProcesInitList[$r->ID] = $r->DESC;
  418. }
  419. return $userProcesInitList;
  420. }
  421. /**
  422. * Ids List of Proces Init for user (use filters)
  423. */
  424. public function getUsedUserProcesInitIds() {
  425. $usedProcesInitList = $this->getUsedUserProcesInitList();
  426. return array_keys($usedProcesInitList);
  427. }
  428. /**
  429. * List of Proces Init for user (use filters)
  430. */
  431. public function getUsedUserProcesInitList() {
  432. $filterIdProces = $this->getFilterIdProces();
  433. if ($filterIdProces > 0) {
  434. return $filterIdProces;
  435. }
  436. $sqlIdProcesListSql = $this->getUsedUserProcesIdsSql();
  437. $fetchUsedProcesInitListSql = <<<SQL
  438. select p.`ID`, p.`DESC`
  439. from `CRM_PROCES` p
  440. where p.`ID` in({$sqlIdProcesListSql})
  441. and p.`TYPE`='PROCES_INIT'
  442. order by p.`SORT_PRIO`
  443. SQL;
  444. $usedProcesInitList = array();
  445. $db = DB::getDB();
  446. $res = $db->query($fetchUsedProcesInitListSql);
  447. while ($r = $db->fetch($res)) {
  448. $usedProcesInitList[$r->ID] = $r->DESC;
  449. }
  450. return $usedProcesInitList;
  451. }
  452. /**
  453. * Ids List of Proces Init for given tabel (skip filters)
  454. */
  455. public function getTableProcesInitIds($idTable) {// TODO: use in TableAjax
  456. $procesInitList = $this->getTableProcesInitList($idTable);
  457. return array_keys($procesInitList);
  458. }
  459. /**
  460. * List of Proces Init for given table (skip filters)
  461. */
  462. public function getTableProcesInitList($idTable) {// TODO: use in TableAjax
  463. $tableProcesInitList = array();
  464. $sqlIdProcesListSql = <<<SQL
  465. select tpv.`ID_PROCES`
  466. from `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
  467. where tpv.`ID_TABLE`='{$idTable}'
  468. SQL;
  469. $fetchTableProcesInitListSql = <<<SQL
  470. -- time ~0.07 -- no goto and return
  471. select p.`ID`, p.`DESC`
  472. from `CRM_PROCES` p
  473. where p.`ID` in(
  474. select i.`idx_PROCES_INIT_ID`
  475. from `CRM_PROCES_idx` i
  476. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  477. )
  478. and p.`TYPE`='PROCES_INIT'
  479. order by p.`SORT_PRIO`
  480. SQL;
  481. /*
  482. SELECT p.`ID` , p.`DESC`
  483. FROM `CRM_PROCES` p
  484. WHERE p.`ID`
  485. IN (
  486. SELECT i.`idx_PROCES_INIT_ID`
  487. FROM `CRM_PROCES_idx` i
  488. WHERE i.`ID_PROCES`
  489. IN (
  490. SELECT tpv.`ID_PROCES`
  491. FROM `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
  492. WHERE tpv.`ID_TABLE` = '13051'
  493. )
  494. )
  495. AND p.`TYPE` = 'PROCES_INIT'
  496. order by p.`SORT_PRIO`
  497. */
  498. $fetchTableProcesInitListSql = <<<SQL
  499. -- time ~0.15s
  500. select p.`ID`, p.`DESC`
  501. from `CRM_PROCES` p
  502. where p.`ID` in(
  503. select i.`idx_PROCES_INIT_ID`
  504. from `CRM_PROCES_idx` i
  505. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  506. union
  507. select ig.`idx_PROCES_INIT_ID`
  508. from `CRM_PROCES_idx` i
  509. join `CRM_PROCES_idx` ig on(ig.`ID_PROCES`=i.`idx_PROCES_WITH_GROUPS_ID`)
  510. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  511. )
  512. and p.`TYPE`='PROCES_INIT'
  513. order by p.`SORT_PRIO`
  514. SQL;
  515. $fetchTableProcesInitListSql = <<<SQL
  516. -- time ~0.14
  517. select p.`ID`, p.`DESC`
  518. from `CRM_PROCES` p
  519. where p.`ID` in(
  520. select i.`idx_PROCES_INIT_ID`
  521. from `CRM_PROCES_idx` i
  522. where i.`ID_PROCES` in({$sqlIdProcesListSql})
  523. or i.`ID_PROCES` in(
  524. select ig.`idx_PROCES_WITH_GROUPS_ID`
  525. from `CRM_PROCES_idx` ig
  526. where ig.`ID_PROCES` in({$sqlIdProcesListSql})
  527. )
  528. )
  529. and p.`TYPE`='PROCES_INIT'
  530. order by p.`SORT_PRIO`
  531. SQL;
  532. //echo'<pre>$fetchTableProcesInitListSql('.$idTable.') ';print_r($fetchTableProcesInitListSql);echo'</pre>';
  533. $tableProcesInitList = array();
  534. $db = DB::getDB();
  535. $res = $db->query($fetchTableProcesInitListSql);
  536. while ($r = $db->fetch($res)) {
  537. $tableProcesInitList[$r->ID] = $r->DESC;
  538. }
  539. return $tableProcesInitList;
  540. }
  541. public function canExecuteProcesInit($idProcesInit) {
  542. $canExecuteProcesInit = false;
  543. $idProcesInit = (int)$idProcesInit;
  544. if (!$idProcesInit) return false;
  545. $idUserGroupList = User::getGroupsIds();
  546. $sqlIdUserGroupList = implode(",", $idUserGroupList);
  547. $checkProcesAccessSql = <<<SQL
  548. select count(*) as cnt
  549. from `CRM_PROCES_idx_GROUP_to_PROCES` gi
  550. join `CRM_PROCES` p on(p.`ID`=gi.`ID_PROCES` and p.`TYPE`='PROCES_INIT')
  551. where gi.`ID_GROUP` in({$sqlIdUserGroupList})
  552. and gi.`ID_PROCES`='{$idProcesInit}'
  553. SQL;
  554. $db = DB::getDB();
  555. $res = $db->query($checkProcesAccessSql);
  556. if ($r = $db->fetch($res)) {
  557. if ($r->cnt > 0) {
  558. $canExecuteProcesInit = true;
  559. }
  560. }
  561. return $canExecuteProcesInit;
  562. }
  563. public function getProcesInitMapTreeOnlyIds($ids) {
  564. $mapTree = array();
  565. $map = $this->getProcesInitMapOnlyIds($ids);
  566. foreach ($map as $r) {
  567. if ('PROCES_INIT' == $r->TYPE) {
  568. $mapTree[$r->ID_PROCES] = array();
  569. }
  570. }
  571. foreach ($map as $r) {
  572. if ('GOTO_AND_RETURN' == $r->TYPE) {
  573. $mapTree[$r->idx_MAIN_PROCES_INIT_ID][$r->ID_PROCES] = true;
  574. }
  575. }
  576. return $mapTree;
  577. }
  578. public function getProcesInitMapOnlyIds($ids) {
  579. $map = array();
  580. $sqlIds = V::filter($ids, array('V', 'filterPositiveInteger'));
  581. $sqlIds = implode(',', $sqlIds);
  582. if (empty($sqlIds)) return $map;
  583. $sql = <<<SQL
  584. select i.`ID_PROCES`
  585. , i.`PARENT_ID`
  586. , i.`TYPE`
  587. , i.`idx_PROCES_INIT_ID`
  588. , i.`idx_MAIN_PROCES_INIT_ID`
  589. , i.`idx_PROCES_WITH_GROUPS_ID`
  590. from `CRM_PROCES_idx` i
  591. where i.`ID_PROCES` in({$sqlIds})
  592. and i.`idx_MAIN_PROCES_INIT_ID` in({$sqlIds})
  593. SQL;
  594. DBG::_('DBG_MAP', '1', "MAP SQL", $sql, __CLASS__, __FUNCTION__, __LINE__);
  595. $db = DB::getDB();
  596. $res = $db->query($sql);
  597. while ($r = $db->fetch($res)) {
  598. $map[] = $r;
  599. }
  600. return $map;
  601. }
  602. }