RouteBase.php 7.8 KB

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