Advertisement
Alii1li

EmployeesControlller

Mar 10th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace App\Controller;
  5.  
  6. /**
  7. * Employees Controller
  8. *
  9. * @property \App\Model\Table\EmployeesTable $Employees
  10. * @method \App\Model\Entity\Employee[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
  11. */
  12. class EmployeesController extends AppController
  13. {
  14. /**
  15. * Index method
  16. *
  17. * @return \Cake\Http\Response|null|void Renders view
  18. */
  19. public function index()
  20. {
  21. $this->set('isEmployeePage', true);
  22. $employees = $this->paginate($this->Employees);
  23.  
  24. $this->set(compact('employees'));
  25. }
  26.  
  27. /**
  28. * View method
  29. *
  30. * @param string|null $id Employee id.
  31. * @return \Cake\Http\Response|null|void Renders view
  32. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  33. */
  34. public function view($id = null)
  35. {
  36. $this->set('isEmployeePage', true);
  37. $employees = $this->paginate($this->Employees);
  38. $employee = $this->Employees->get($id, [
  39. 'contain' => ['EmployeeMemberRelationships', 'Journals', 'WorkingTimes'],
  40. ]);
  41.  
  42. $this->set(compact('employee'));
  43. }
  44.  
  45. /**
  46. * Add method
  47. *
  48. * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
  49. */
  50. public function add()
  51. {
  52. $this->set('isEmployeePage', true);
  53. $employees = $this->paginate($this->Employees);
  54. $employee = $this->Employees->newEmptyEntity();
  55. if ($this->request->is('post')) {
  56. $employee = $this->Employees->patchEntity($employee, $this->request->getData());
  57. $file = $this->request->getUploadedFiles();
  58.  
  59. $employee->profile_picture = $file['profile_picture']->getClientFilename();
  60. $file['profile_picture']->moveTo(WWW_ROOT . 'img' . DS . $employee->profile_picture);
  61. if ($this->Employees->save($employee)) {
  62. $this->Flash->success(__('The employee has been saved.'));
  63.  
  64. return $this->redirect(['action' => 'index']);
  65. }
  66. $this->Flash->error(__('The employee could not be saved. Please, try again.'));
  67. }
  68. $this->set(compact('employee'));
  69. }
  70.  
  71. /**
  72. * Edit method
  73. *
  74. * @param string|null $id Employee id.
  75. * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
  76. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  77. */
  78. public function edit($id = null)
  79. {
  80. $this->set('isEmployeePage', true);
  81. $employees = $this->paginate($this->Employees);
  82. $employee = $this->Employees->get($id, [
  83. 'contain' => [],
  84. ]);
  85. if ($this->request->is(['patch', 'post', 'put'])) {
  86. $employee = $this->Employees->patchEntity($employee, $this->request->getData());
  87. $file = $this->request->getUploadedFiles();
  88.  
  89. $employee->profile_picture = $file['profile_picture']->getClientFilename();
  90. $file['profile_picture']->moveTo(WWW_ROOT . 'img' . DS . $employee->profile_picture);
  91. if ($this->Employees->save($employee)) {
  92. $this->Flash->success(__('The employee has been saved.'));
  93.  
  94. return $this->redirect(['action' => 'index']);
  95. }
  96. $this->Flash->error(__('The employee could not be saved. Please, try again.'));
  97. }
  98. $this->set(compact('employee'));
  99. }
  100.  
  101. /**
  102. * Delete method
  103. *
  104. * @param string|null $id Employee id.
  105. * @return \Cake\Http\Response|null|void Redirects to index.
  106. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  107. */
  108. public function delete($id = null)
  109. {
  110. $this->request->allowMethod(['post', 'delete']);
  111. $employee = $this->Employees->get($id);
  112. if ($this->Employees->delete($employee)) {
  113. $this->Flash->success(__('The employee has been deleted.'));
  114. } else {
  115. $this->Flash->error(__('The employee could not be deleted. Please, try again.'));
  116. }
  117.  
  118. return $this->redirect(['action' => 'index']);
  119. }
  120.  
  121. public function beforeFilter(\Cake\Event\EventInterface $event)
  122. {
  123. parent::beforeFilter($event);
  124. // Configure the login action to not require authentication, preventing
  125. // the infinite redirect loop issue
  126. $this->Authentication->addUnauthenticatedActions(['login']);
  127. $this->Authentication->addUnauthenticatedActions(['login', 'add']);
  128. }
  129.  
  130. public function login()
  131. {
  132. $this->request->allowMethod(['get', 'post']);
  133. $result = $this->Authentication->getResult();
  134. // regardless of POST or GET, redirect if user is logged in
  135. if ($result && $result->isValid()) {
  136. // redirect to /articles after login success
  137. $redirect = $this->request->getQuery('redirect', [
  138. 'controller' => 'Employees',
  139. 'action' => 'index',
  140. ]);
  141.  
  142. return $this->redirect($redirect);
  143. }
  144. // display error if user submitted and authentication failed
  145. if ($this->request->is('post') && !$result->isValid()) {
  146. $this->Flash->error(__('Invalid username or password'));
  147. }
  148. }
  149. public function logout()
  150. {
  151. $result = $this->Authentication->getResult();
  152. // regardless of POST or GET, redirect if user is logged in
  153. if ($result && $result->isValid()) {
  154. $this->Authentication->logout();
  155. return $this->redirect(['controller' => 'Employees', 'action' => 'login']);
  156. }
  157. }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement