RouteBase.php 5.6 KB

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