RouteBase.php 6.9 KB

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