Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

215 řádky
6.8KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Repositories\TerritoryRepository;
  5. use Core\Controller;
  6. use Core\Pagination;
  7. use Core\Request;
  8. use Core\Response;
  9. use Core\Validator;
  10. class TerritoryController extends Controller
  11. {
  12. public function index(): Response
  13. {
  14. if ($redirect = $this->requireAuth()) {
  15. return $redirect;
  16. }
  17. $request = Request::capture();
  18. $search = (string) ($request->input('search') ?? '');
  19. $page = max(1, (int) ($request->input('page') ?? 1));
  20. $perPage = 20;
  21. $repo = new TerritoryRepository(database());
  22. $total = $repo->countAll($search);
  23. $pagination = new Pagination($total, $page, $perPage);
  24. $territories = $repo->findPaged($page, $perPage, $search);
  25. $counts = $repo->householdCountsKeyed();
  26. return $this->view('territories.index', [
  27. 'pageTitle' => 'Territories',
  28. 'territories' => $territories,
  29. 'counts' => $counts,
  30. 'search' => $search,
  31. 'pagination' => $pagination,
  32. ]);
  33. }
  34. public function show(int|string $id): Response
  35. {
  36. if ($redirect = $this->requireAuth()) {
  37. return $redirect;
  38. }
  39. $repo = new TerritoryRepository(database());
  40. $territory = $repo->find((int) $id);
  41. if (!$territory) {
  42. return Response::notFound('Territory not found.');
  43. }
  44. $streets = $repo->distinctStreets((int) $id);
  45. $householdRepo = new \App\Repositories\HouseholdRepository(database());
  46. $households = $householdRepo->findAllByTerritory((int) $id);
  47. return $this->view('territories.show', [
  48. 'pageTitle' => e($territory['name']),
  49. 'territory' => $territory,
  50. 'streets' => $streets,
  51. 'households' => $households,
  52. ]);
  53. }
  54. public function create(): Response
  55. {
  56. if ($redirect = $this->requireAuth()) {
  57. return $redirect;
  58. }
  59. return $this->view('territories.create', [
  60. 'pageTitle' => 'New Territory',
  61. 'errors' => [],
  62. 'old' => [],
  63. ]);
  64. }
  65. public function store(): Response
  66. {
  67. if ($redirect = $this->requireAuth()) {
  68. return $redirect;
  69. }
  70. $request = Request::capture();
  71. if (!verify_csrf_token($request->input('_token'))) {
  72. return $this->view('territories.create', [
  73. 'pageTitle' => 'New Territory',
  74. 'errors' => ['_token' => ['Invalid request. Please try again.']],
  75. 'old' => $request->all(),
  76. ]);
  77. }
  78. $name = trim((string) ($request->input('name') ?? ''));
  79. $description = trim((string) ($request->input('description') ?? ''));
  80. $coordinates = trim((string) ($request->input('coordinates') ?? ''));
  81. $validator = (new Validator())
  82. ->required('name', $name, 'Name is required.')
  83. ->maxLength('name', $name, 255);
  84. if ($validator->fails()) {
  85. return $this->view('territories.create', [
  86. 'pageTitle' => 'New Territory',
  87. 'errors' => $validator->errors(),
  88. 'old' => $request->all(),
  89. ]);
  90. }
  91. $now = date('Y-m-d H:i:s');
  92. database()->execute(
  93. 'INSERT INTO territories (name, description, coordinates, created_at, updated_at)
  94. VALUES (:name, :description, :coordinates, :now, :now)',
  95. ['name' => $name, 'description' => $description, 'coordinates' => $coordinates, 'now' => $now]
  96. );
  97. flash('success', "Territory \"{$name}\" created.");
  98. return $this->redirect('/territories');
  99. }
  100. public function edit(int|string $id): Response
  101. {
  102. if ($redirect = $this->requireAuth()) {
  103. return $redirect;
  104. }
  105. $territory = (new TerritoryRepository(database()))->find((int) $id);
  106. if (!$territory) {
  107. return Response::notFound('Territory not found.');
  108. }
  109. return $this->view('territories.edit', [
  110. 'pageTitle' => 'Edit Territory',
  111. 'territory' => $territory,
  112. 'errors' => [],
  113. ]);
  114. }
  115. public function update(int|string $id): Response
  116. {
  117. if ($redirect = $this->requireAuth()) {
  118. return $redirect;
  119. }
  120. $request = Request::capture();
  121. $repo = new TerritoryRepository(database());
  122. $territory = $repo->find((int) $id);
  123. if (!$territory) {
  124. return Response::notFound('Territory not found.');
  125. }
  126. if (!verify_csrf_token($request->input('_token'))) {
  127. return $this->view('territories.edit', [
  128. 'pageTitle' => 'Edit Territory',
  129. 'territory' => $territory,
  130. 'errors' => ['_token' => ['Invalid request. Please try again.']],
  131. ]);
  132. }
  133. $name = trim((string) ($request->input('name') ?? ''));
  134. $description = trim((string) ($request->input('description') ?? ''));
  135. $coordinates = trim((string) ($request->input('coordinates') ?? ''));
  136. $validator = (new Validator())
  137. ->required('name', $name, 'Name is required.')
  138. ->maxLength('name', $name, 255);
  139. if ($validator->fails()) {
  140. return $this->view('territories.edit', [
  141. 'pageTitle' => 'Edit Territory',
  142. 'territory' => array_merge($territory, ['name' => $name, 'description' => $description, 'coordinates' => $coordinates]),
  143. 'errors' => $validator->errors(),
  144. ]);
  145. }
  146. database()->execute(
  147. 'UPDATE territories SET name = :name, description = :description, coordinates = :coordinates,
  148. updated_at = :now WHERE id = :id',
  149. ['name' => $name, 'description' => $description, 'coordinates' => $coordinates,
  150. 'now' => date('Y-m-d H:i:s'), 'id' => $id]
  151. );
  152. flash('success', "Territory \"{$name}\" updated.");
  153. return $this->redirect('/territories/' . $id);
  154. }
  155. public function delete(int|string $id): Response
  156. {
  157. if ($redirect = $this->requireAuth()) {
  158. return $redirect;
  159. }
  160. $request = Request::capture();
  161. $territory = (new TerritoryRepository(database()))->find((int) $id);
  162. if (!$territory) {
  163. return Response::notFound('Territory not found.');
  164. }
  165. if (!verify_csrf_token($request->input('_token'))) {
  166. flash('error', 'Invalid request.');
  167. return $this->redirect('/territories');
  168. }
  169. database()->execute('DELETE FROM territories WHERE id = :id', ['id' => $id]);
  170. flash('success', 'Territory deleted.');
  171. return $this->redirect('/territories');
  172. }
  173. }

Powered by TurnKey Linux.