RouteBase.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. Lib::loadClass('UI');
  3. class RouteBase {
  4. public function route() {
  5. $task = V::get('_task', '', $_REQUEST);
  6. if (empty($task)) {
  7. $this->defaultAction();
  8. return;
  9. }
  10. $methodName = "{$task}Action";
  11. if (method_exists($this, $methodName)) {
  12. $this->{$methodName}();
  13. } else {
  14. die("Task '{$task}' not exists");
  15. }
  16. }
  17. public function defaultAction() {
  18. die("default task not implemented");
  19. }
  20. public function getUrlRouteName() {
  21. $clsName = get_class($this);
  22. if ('Route_' != substr($clsName, 0, strlen('Route_'))) throw new Exception("Wrong route class name '{$clsName}'");
  23. return substr($clsName, strlen('Route_'));
  24. }
  25. public function getLink($task = '', $args = null) {
  26. $routeName = $this->getUrlRouteName();
  27. if (empty($args)) return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($task) ? "&_task={$task}" : "");
  28. if (is_string($args)) {
  29. return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($task) ? "&_task={$task}" : "") . ltrim($args, '&');
  30. }
  31. if (is_array($args)) {
  32. $urlArgs = [];
  33. $uniqArgs = [];
  34. if (!empty($task)) $uniqArgs['_task'] = $task;
  35. foreach ($args as $name => $val) $uniqArgs[$name] = $val;
  36. foreach ($uniqArgs as $name => $val) $urlArgs[] = "{$name}={$val}";
  37. return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($urlArgs) ? '&' . implode('&', $urlArgs) : '');
  38. }
  39. throw new Exception("Not Implemented args type", 501);
  40. }
  41. public function runTask($task) {
  42. if (empty($task)) throw new Exception("Empty method name");
  43. $methodName = "{$task}Action";
  44. if (!method_exists($this, $methodName)) {
  45. throw new Exception("Task '{$task}' not exists");
  46. }
  47. $this->{$methodName}();
  48. }
  49. public function runMethod($methodName) {
  50. if (empty($methodName)) throw new Exception("Empty method name");
  51. if (!method_exists($this, $methodName)) {
  52. throw new Exception("Task '{$methodName}' not exists");
  53. }
  54. $this->{$methodName}();
  55. }
  56. public function parseMessageFromStorageTestAction() {
  57. $msgs = V::get('msgs', '', $_GET);
  58. //echo $this->parseMessageFromStorage($msg);
  59. Lib::loadClass('StorageException');
  60. if (is_array($msgs)) {
  61. foreach ($msgs as $vMsg) {
  62. try {
  63. throw new StorageException($vMsg);
  64. } catch (Exception $e) {
  65. $vParsedMsg = $e->getMessage();
  66. echo " MSG: {$vMsg}\n";
  67. echo "PARSED: {$vParsedMsg}\n";
  68. echo "isMsgParsed(" . ($vMsg != $vParsedMsg) . ")\n\n";
  69. }
  70. }
  71. } else {
  72. throw new StorageException($msgs);
  73. }
  74. echo "\n\n";
  75. die("parseMessageFromStorage end");
  76. }
  77. public function parseMessageFromStorage($msg) {
  78. return $msg;
  79. }
  80. public function parseMessageFromMsgsSystem($msg) {
  81. return $msg;
  82. }
  83. public function runByMessageFromMsgsSystem($msg, & $execNotes) {
  84. }
  85. public function handleAuth() {
  86. User::authByRequest();
  87. }
  88. public function menu() {
  89. $getLink = array($this, 'getLink');
  90. echo UI::h('ul', [], array_map(
  91. function ($method) use ($getLink) {
  92. $action = substr($method, 0, -6);
  93. return UI::h('li', [],
  94. UI::h('a', [
  95. 'href' => $getLink($action)
  96. ], $action)
  97. );
  98. }
  99. , array_filter(
  100. get_class_methods($this)
  101. , function ($method) {
  102. if ('Action' != substr($method, -6)) return false;
  103. if ('parseMessageFromStorageTestAction' == $method) return false;
  104. return true;
  105. }
  106. )
  107. ));
  108. }
  109. public function reinstall() {
  110. $clsName = get_class($this);
  111. if ('Route_UrlAction' == substr($clsName, 0, strlen('Route_UrlAction'))) {
  112. $actionName = substr($clsName, strlen('Route_UrlAction_'));
  113. DBG::log(['msg'=>"\$actionName", '$actionName'=>$actionName]);
  114. $idTypespecial = DB::getPDO()->fetchValue("
  115. select z.ID
  116. from CRM_LISTA_ZASOBOW z
  117. where z.`TYPE` = 'TYPESPECIALS'
  118. ");
  119. if (!$idTypespecial) {
  120. $idTypespecial = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  121. 'TYPE' => 'TYPESPECIALS',
  122. 'DESC' => 'TYPESPECIALS',
  123. 'DESC_PL' => 'Powiązania tabel',
  124. 'OPIS' => 'Powiązania i relacje tabel do wyświetlania w funkcjach systemowych',
  125. ]);
  126. }
  127. DBG::log(['msg'=>"\$idTypespecial", '$idTypespecial'=>$idTypespecial]);
  128. if (!$idTypespecial) throw new Exception("Wystąpił błąd podczas tworzenia zasobu 'Powiązania tabel' (TYPESPECIALS)");
  129. $idAction = DB::getPDO()->fetchValue("
  130. select z.ID
  131. from CRM_LISTA_ZASOBOW z
  132. where z.`PARENT_ID` = {$idTypespecial}
  133. and z.`DESC` = '{$actionName}'
  134. ");
  135. if (!$idAction) {
  136. $idAction = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  137. 'PARENT_ID' => $idTypespecial,
  138. 'TYPE' => 'URL_ACTION',
  139. 'DESC' => $actionName,
  140. 'DESC_PL' => $this->getActionLabel(),
  141. 'OPIS' => $this->getActionDescription(),
  142. ]);
  143. }
  144. DBG::log(['msg'=>"\$idAction", '$idAction'=>$idAction]);
  145. if (!$idAction) throw new Exception("Wystąpił błąd podczas tworzenia zasobu funkcji '{$actionName}' (URL_ACTION)");
  146. $args = $this->getActionArgs();
  147. foreach ($args as $argName => $argDesc) {
  148. $idArg = DB::getPDO()->fetchValue("
  149. select z.ID
  150. from CRM_LISTA_ZASOBOW z
  151. where z.`PARENT_ID` = {$idAction}
  152. and z.`TYPE` = 'PARAM_IN'
  153. and z.`DESC` = '{$argName}'
  154. ");
  155. if (!$idArg) {
  156. $idArg = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  157. 'PARENT_ID' => $idAction,
  158. 'TYPE' => 'PARAM_IN',
  159. 'DESC' => $argName,
  160. 'OPIS' => $argDesc,
  161. ]);
  162. }
  163. DBG::log(['msg'=>"\$idArg ('{$argName}')", '$idArg'=>$idArg]);
  164. if (!$idArg) throw new Exception("Wystąpił błąd podczas tworzenia zasobu dla argumentu funkcji '{$actionName}' '{$argName}' (PARAM_IN)");
  165. }
  166. }
  167. }
  168. public function getActionLabel() { return ""; }
  169. public function getActionDescription() { return ""; }
  170. public function getActionArgs() { return []; }
  171. function layout($viewName, $params = []) {
  172. // TODO: staticLayout: UI::layout([ 'Route_FixCrmProcesInitIdx', 'defaultView' ]);
  173. UI::layout([ $this, $viewName ], $params);
  174. }
  175. }