Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

254 Zeilen
8.8KB

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

Powered by TurnKey Linux.