UrlAction.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. /*
  4. ## Url Actions (to replace Typespecial's):
  5. - new Zasob type: 'URL_ACTION'
  6. ## Config in Zasoby tree (TODO: upload from gui xml file or php):
  7. [100] - TYPESPECIALS
  8. [110] - URL_ACTION ProjektyKosztyWstepnychRobot
  9. [111] - PARAM_IN ID_PROJECT
  10. [200] - TABELA IN7_MK_BAZA_DYSTRYBUCJI Projekty
  11. [210] - KOMORKA ID
  12. [220] - (ALIAS to [110]) URL_ACTION action name/label
  13. [221] - (ALIAS to [210]) PARAM_IN ID_PROJECT
  14. class Route_UrlAction_ProjektyKosztyWstepnychRobot extends RouteBase {
  15. public function defaultAction() {
  16. $args = array();
  17. $args['ID_PROJECT'] = V::get('ID_PROJECT', '', $_REQUEST);// [111]
  18. }
  19. }
  20. ## Process - Create:
  21. - 1. define action name in Zasoby tree (URL_ACTION) and expected args (PARAM_IN) - `ActionDefinition`
  22. - 2. create action alias inside TABLE - `ActionInstance`
  23. - 3. set perms for `ActionInstance`
  24. ## Process - Install ActionDefinitio's:
  25. - class UrlActionBase :: installZasobAction
  26. - create required Zasoby in Zasob Tree if not exists
  27. */
  28. class Route_UrlAction extends RouteBase {// TODO: UrlActionBase
  29. public function handleAuth() {
  30. if (!User::logged()) {
  31. throw new HttpException('Unauthorized', 401);
  32. }
  33. }
  34. public function defaultAction() {
  35. SE_Layout::gora();
  36. ?>
  37. <div class="container">
  38. <h1>UrlActions system</h1>
  39. ...
  40. </div>
  41. <?php
  42. SE_Layout::dol();
  43. }
  44. public function getArgsList() {
  45. $args = array();
  46. return $args;
  47. }
  48. public function reinstallAction() {
  49. $jsonData = new stdClass();
  50. $jsonData->type = 'success';
  51. $jsonData->msg = 'Gotowe';
  52. try {
  53. $this->reinstall();
  54. } catch (Exception $e) {
  55. $jsonData->type = 'danger';
  56. $jsonData->msg = $e->getMessage();
  57. }
  58. echo json_encode($jsonData);
  59. }
  60. public function reinstall() {
  61. $sqlList = array();
  62. // alter table enum add URL_ACTION
  63. //$sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `CRM_UI_MSGS`";
  64. $pdo = DB::getPDO();
  65. foreach ($sqlList as $sqlName => $sql) {
  66. $pdo->exec($sql);
  67. }
  68. }
  69. /*
  70. 636 TABELA IN7_MK_BAZA_DYSTRYBUCJI Projekty
  71. 763 KOMORKA ID Nr
  72. 22332 22317 URL_ACTION ProjektyKosztyWstepnychRobot
  73. 22335 763 PARAM_IN ID_PROJECT
  74. 25 TYPESPECIALS TYPESPECIALS Powiązania tabel
  75. 22317 URL_ACTION ProjektyKosztyWstepnychRobot
  76. 22324 PARAM_IN ID_PROJECT
  77. */
  78. public static function fetchTableFunctions($idTbl, $idRecord) {
  79. $sqlIdZasob = DB::getPDO()->quote($idTbl, PDO::PARAM_STR);
  80. return DB::getPDO()->fetchAll("
  81. select z.ID as ID
  82. , IF(z.DESC_PL != '', z.DESC_PL, IF(za.DESC_PL != '', za.DESC_PL, z.`DESC`)) as fun_label
  83. , za.`DESC` as fun_name
  84. , zp.ALIAS_ID as param_in_to_cell_id
  85. , zp.DESC as param_in_name
  86. from CRM_LISTA_ZASOBOW z
  87. join CRM_LISTA_ZASOBOW za on(za.ID = z.ALIAS_ID)
  88. left join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = z.ID and zp.`TYPE` = 'PARAM_IN')
  89. left join CRM_LISTA_ZASOBOW zpa on(zpa.ID = zp.ALIAS_ID and zpa.`TYPE` = 'PARAM_IN' and zpa.`DESC` = zp.`DESC`)
  90. where z.PARENT_ID = {$sqlIdZasob}
  91. and z.`TYPE` = 'URL_ACTION'
  92. ");
  93. }
  94. public static function fetchTableFunctionsForUser($idTbl, $idRecord, $usrLogin) {
  95. $sqlIdZasob = DB::getPDO()->quote($idTbl, PDO::PARAM_STR);
  96. $sqlUserLogin = DB::getPDO()->quote($usrLogin, PDO::PARAM_STR);
  97. return DB::getPDO()->fetchAll("
  98. select z.ID as ID
  99. , IF(z.DESC_PL != '', z.DESC_PL, IF(za.DESC_PL != '', za.DESC_PL, z.`DESC`)) as fun_label
  100. , za.`DESC` as fun_name
  101. , zp.ALIAS_ID as param_in_to_cell_id
  102. , zp.DESC as param_in_name
  103. , param.`DESC` as link_param
  104. -- TODO find LINK_TARGET_SELF under za.ID - link target defined globally
  105. from CRM_LISTA_ZASOBOW z
  106. join CRM_LISTA_ZASOBOW za on(za.ID = z.ALIAS_ID)
  107. left join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = z.ID and zp.`TYPE` = 'PARAM_IN')
  108. left join CRM_LISTA_ZASOBOW zpa on(zpa.ID = zp.ALIAS_ID and zpa.`TYPE` = 'PARAM_IN' and zpa.`DESC` = zp.`DESC`)
  109. join CRM_WSKAZNIK w on(w.ID_ZASOB = z.ID and w.A_STATUS not in('DELETED'))
  110. -- join CRM_PROCES p on(p.ID = w.ID_PROCES)
  111. join CRM_PROCES_idx_USER_to_PROCES_VIEW upv on(upv.ID_PROCES = w.ID_PROCES)
  112. left join CRM_LISTA_ZASOBOW param on(param.PARENT_ID = z.ID and param.`TYPE` = 'DANE')
  113. where z.PARENT_ID = {$sqlIdZasob}
  114. and z.`TYPE` = 'URL_ACTION'
  115. and upv.ADM_ACCOUNT = {$sqlUserLogin}
  116. -- group by z.ID
  117. ");
  118. }
  119. public static function getTableFunctions($idTbl, $idRecord, $tblName = '', $usrLogin = null) {
  120. $rows = array();
  121. if (!empty($usrLogin)) {
  122. $rows = self::fetchTableFunctionsForUser($idTbl, $idRecord, $usrLogin);
  123. } else {
  124. $rows = self::fetchTableFunctions($idTbl, $idRecord);
  125. }
  126. DBG::log($rows, 'array', "getTableFunctions({$idTbl}, {$idRecord}, ...) :: rows");
  127. $functions = array();
  128. foreach ($rows as $row) {
  129. // TODO: Router::getRoute("UrlAction_{$row['fun_name']}")->isRecordAllowed($idTbl, $idRecord, $tblName);
  130. if (!array_key_exists($row['ID'], $functions)) {
  131. $fun = array();
  132. $fun['label'] = $row['fun_label'];
  133. $fun['name'] = $row['fun_name'];
  134. $fun['baseLink'] = "index.php?_route=UrlAction_{$row['fun_name']}";
  135. $fun['ico'] = "glyphicon glyphicon-share";
  136. $fun['cell_id_params'] = array();
  137. $fun['link_target'] = '_blank';// LINK_TARGET_SELF
  138. $functions[ $row['ID'] ] = $fun;
  139. }
  140. $funParams = $functions[ $row['ID'] ]['cell_id_params'];
  141. if (!empty($row['param_in_name'])) {
  142. if ($row['param_in_to_cell_id'] > 0) {// ALIAS to field - get field value from row
  143. $funParams[ $row['param_in_to_cell_id'] ] = $row['param_in_name'];
  144. $functions[ $row['ID'] ]['cell_id_params'] = $funParams;
  145. }
  146. else if (false !== strpos($row['param_in_name'], '=')) {// "{arg_name}={value}" - add to url
  147. $functions[ $row['ID'] ][ 'baseLink' ] .= "&" . $row['param_in_name'];
  148. }
  149. }
  150. if ('LINK_TARGET_SELF' == $row['link_param']) {
  151. unset($functions[ $row['ID'] ]['link_target']);
  152. }
  153. }
  154. $functions = array_filter($functions, function ($rowFunction) use ($idRecord) {
  155. return ($idRecord > 0)
  156. ? !empty($rowFunction['cell_id_params'])
  157. : empty($rowFunction['cell_id_params']);
  158. });
  159. DBG::log($functions, 'array', "getTableFunctions({$idTbl}, {$idRecord}, ...) :: functions");
  160. return $functions;
  161. }
  162. }