UrlAction.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. static function fetchTableFunctions($idTbl) {
  79. return DB::getPDO()->fetchAll("
  80. select z.ID as ID
  81. , IF(z.DESC_PL != '', z.DESC_PL, IF(za.DESC_PL != '', za.DESC_PL, z.`DESC`)) as fun_label
  82. , za.`DESC` as fun_name
  83. , zp.ALIAS_ID as param_in_to_cell_id
  84. , zp.DESC as param_in_name
  85. from CRM_LISTA_ZASOBOW z
  86. join CRM_LISTA_ZASOBOW za on(za.ID = z.ALIAS_ID)
  87. left join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = z.ID and zp.`TYPE` = 'PARAM_IN')
  88. left join CRM_LISTA_ZASOBOW zpa on(zpa.ID = zp.ALIAS_ID and zpa.`TYPE` = 'PARAM_IN' and zpa.`DESC` = zp.`DESC`)
  89. where z.PARENT_ID = :id_table
  90. and z.`TYPE` = 'URL_ACTION'
  91. ", [
  92. ':id_table' => $idTbl,
  93. ]);
  94. }
  95. static function fetchTableFunctionsForUser($idTbl, $usrLogin) {
  96. return DB::getPDO()->fetchAll("
  97. select z.ID as ID
  98. , IF(z.DESC_PL != '', z.DESC_PL, IF(za.DESC_PL != '', za.DESC_PL, z.`DESC`)) as fun_label
  99. , za.`DESC` as fun_name
  100. , zp.ALIAS_ID as param_in_to_cell_id
  101. , zp.DESC as param_in_name
  102. , param.`DESC` as link_param
  103. -- TODO find LINK_TARGET_SELF under za.ID - link target defined globally
  104. from CRM_LISTA_ZASOBOW z
  105. join CRM_LISTA_ZASOBOW za on(za.ID = z.ALIAS_ID)
  106. left join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = z.ID and zp.`TYPE` = 'PARAM_IN')
  107. left join CRM_LISTA_ZASOBOW zpa on(zpa.ID = zp.ALIAS_ID and zpa.`TYPE` = 'PARAM_IN' and zpa.`DESC` = zp.`DESC`)
  108. join CRM_WSKAZNIK w on(w.ID_ZASOB = z.ID and w.A_STATUS not in('DELETED'))
  109. -- join CRM_PROCES p on(p.ID = w.ID_PROCES)
  110. join CRM_PROCES_idx_USER_to_PROCES_VIEW upv on(upv.ID_PROCES = w.ID_PROCES)
  111. left join CRM_LISTA_ZASOBOW param on(param.PARENT_ID = z.ID and param.`TYPE` = 'DANE')
  112. where z.PARENT_ID = :id_table
  113. and z.`TYPE` = 'URL_ACTION'
  114. and upv.ADM_ACCOUNT = :user_login
  115. -- group by z.ID
  116. ", [
  117. ':id_table' => $idTbl,
  118. ':user_login' => $usrLogin,
  119. ]);
  120. }
  121. static function getFeatureTools($idTable, $idFeature, $usrLogin = null) {
  122. return array_filter(
  123. self::_fetchAllTools($idTable, $usrLogin),
  124. function ($func) use ($idFeature) {
  125. return ("rowFunction" === $func['type']);
  126. }
  127. );
  128. }
  129. static function getObjectTools($idTable, $usrLogin = null) {
  130. return array_filter(
  131. self::_fetchAllTools($idTable, $usrLogin),
  132. function ($func) use ($idFeature) {
  133. return ("table" === $func['type']);
  134. }
  135. );
  136. }
  137. static function getSelectedFeatureTools($idTable, $usrLogin = null) {
  138. return array_filter(
  139. self::_fetchAllTools($idTable, $usrLogin),
  140. function ($func) use ($idFeature) {
  141. return ("@selected" === $func['type']);
  142. }
  143. );
  144. }
  145. static function _fetchAllTools($idTable, $usrLogin = null) {
  146. $rows = (!empty($usrLogin))
  147. ? self::fetchTableFunctionsForUser($idTable, $usrLogin)
  148. : $rows = self::fetchTableFunctions($idTable)
  149. ;
  150. DBG::log($rows, 'array', "_fetchAllTools({$idTbl}, {$idRecord}, ...) :: rows");
  151. $functions = array_reduce($rows, function ($ret, $item) {
  152. if (!array_key_exists($item['ID'], $ret)) {
  153. $fun = array();
  154. $fun['type'] = "table"; // default type - 'table', may be: '@selected' or 'rowFunction'
  155. $fun['label'] = $item['fun_label'];
  156. $fun['name'] = $item['fun_name'];
  157. $fun['baseLink'] = "index.php?_route=UrlAction_{$item['fun_name']}";
  158. $fun['ico'] = "glyphicon glyphicon-share";
  159. $fun['cell_id_params'] = array();
  160. $fun['link_target'] = '_blank'; // LINK_TARGET_SELF
  161. $fun['url_args'] = []; // Zasob `TYPE` = 'DANE' whhere `DESC` != 'LINK_TARGET_SELF'
  162. $ret[ $item['ID'] ] = $fun;
  163. }
  164. $funParams = $ret[ $item['ID'] ]['cell_id_params'];
  165. if ("@selected" === $item['param_in_name']) {
  166. $ret[ $item['ID'] ]['type'] = "@selected";
  167. } else if (!empty($item['param_in_name'])) {
  168. if ($item['param_in_to_cell_id'] > 0) {// ALIAS to field - get field value from item
  169. $funParams[ $item['param_in_to_cell_id'] ] = $item['param_in_name'];
  170. $ret[ $item['ID'] ]['cell_id_params'] = $funParams;
  171. $ret[ $item['ID'] ]['type'] = "rowFunction";
  172. }
  173. else if (false !== strpos($item['param_in_name'], '=')) {// "{arg_name}={value}" - add to url
  174. $ret[ $item['ID'] ][ 'baseLink' ] .= "&" . $item['param_in_name'];
  175. }
  176. }
  177. if ('LINK_TARGET_SELF' == $item['link_param']) {
  178. unset($ret[ $item['ID'] ]['link_target']);
  179. }
  180. else if (!empty($item['link_param'])) {
  181. $ret[ $item['ID'] ]['url_args'][] = ( strpos($item['link_param'], '=') ? $item['link_param'] : $item['link_param'] . "=1" );
  182. }
  183. return $ret;
  184. }, []);
  185. $functions = array_map(function ($fun) { // fix url_args
  186. $urlArgs = array_unique($fun['url_args']);
  187. unset($fun['url_args']);
  188. if (!empty($urlArgs)) {
  189. $fun['baseLink'] .= "&" . implode("&", $urlArgs);
  190. }
  191. return $fun;
  192. }, $functions);
  193. DBG::log($functions, 'array', "_fetchAllTools({$idTbl}, {$idRecord}, ...) :: all functions");
  194. return $functions;
  195. }
  196. }