RouteBase.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 reinstall() {
  85. $clsName = get_class($this);
  86. if ('Route_UrlAction' == substr($clsName, 0, strlen('Route_UrlAction'))) {
  87. $actionName = substr($clsName, strlen('Route_UrlAction_'));
  88. DBG::log(['msg'=>"\$actionName", '$actionName'=>$actionName]);
  89. $idTypespecial = DB::getPDO()->fetchValue("
  90. select z.ID
  91. from CRM_LISTA_ZASOBOW z
  92. where z.`TYPE` = 'TYPESPECIALS'
  93. ");
  94. if (!$idTypespecial) {
  95. $idTypespecial = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  96. 'TYPE' => 'TYPESPECIALS',
  97. 'DESC' => 'TYPESPECIALS',
  98. 'DESC_PL' => 'Powiązania tabel',
  99. 'OPIS' => 'Powiązania i relacje tabel do wyświetlania w funkcjach systemowych',
  100. ]);
  101. }
  102. DBG::log(['msg'=>"\$idTypespecial", '$idTypespecial'=>$idTypespecial]);
  103. if (!$idTypespecial) throw new Exception("Wystąpił błąd podczas tworzenia zasobu 'Powiązania tabel' (TYPESPECIALS)");
  104. $idAction = DB::getPDO()->fetchValue("
  105. select z.ID
  106. from CRM_LISTA_ZASOBOW z
  107. where z.`PARENT_ID` = {$idTypespecial}
  108. and z.`DESC` = '{$actionName}'
  109. ");
  110. if (!$idAction) {
  111. $idAction = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  112. 'PARENT_ID' => $idTypespecial,
  113. 'TYPE' => 'URL_ACTION',
  114. 'DESC' => $actionName,
  115. 'DESC_PL' => $this->getActionLabel(),
  116. 'OPIS' => $this->getActionDescription(),
  117. ]);
  118. }
  119. DBG::log(['msg'=>"\$idAction", '$idAction'=>$idAction]);
  120. if (!$idAction) throw new Exception("Wystąpił błąd podczas tworzenia zasobu funkcji '{$actionName}' (URL_ACTION)");
  121. $args = $this->getActionArgs();
  122. foreach ($args as $argName => $argDesc) {
  123. $idArg = DB::getPDO()->fetchValue("
  124. select z.ID
  125. from CRM_LISTA_ZASOBOW z
  126. where z.`PARENT_ID` = {$idAction}
  127. and z.`TYPE` = 'PARAM_IN'
  128. and z.`DESC` = '{$argName}'
  129. ");
  130. if (!$idArg) {
  131. $idArg = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  132. 'PARENT_ID' => $idAction,
  133. 'TYPE' => 'PARAM_IN',
  134. 'DESC' => $argName,
  135. 'OPIS' => $argDesc,
  136. ]);
  137. }
  138. DBG::log(['msg'=>"\$idArg ('{$argName}')", '$idArg'=>$idArg]);
  139. if (!$idArg) throw new Exception("Wystąpił błąd podczas tworzenia zasobu dla argumentu funkcji '{$actionName}' '{$argName}' (PARAM_IN)");
  140. }
  141. }
  142. }
  143. public function getActionLabel() { return ""; }
  144. public function getActionDescription() { return ""; }
  145. public function getActionArgs() { return []; }
  146. }