RouteBase.php 5.5 KB

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