Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace App\Controller;
- /**
- * Employees Controller
- *
- * @property \App\Model\Table\EmployeesTable $Employees
- * @method \App\Model\Entity\Employee[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
- */
- class EmployeesController extends AppController
- {
- /**
- * Index method
- *
- * @return \Cake\Http\Response|null|void Renders view
- */
- public function index()
- {
- $this->set('isEmployeePage', true);
- $employees = $this->paginate($this->Employees);
- $this->set(compact('employees'));
- }
- /**
- * View method
- *
- * @param string|null $id Employee id.
- * @return \Cake\Http\Response|null|void Renders view
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function view($id = null)
- {
- $this->set('isEmployeePage', true);
- $employees = $this->paginate($this->Employees);
- $employee = $this->Employees->get($id, [
- 'contain' => ['EmployeeMemberRelationships', 'Journals', 'WorkingTimes'],
- ]);
- $this->set(compact('employee'));
- }
- /**
- * Add method
- *
- * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
- */
- public function add()
- {
- $this->set('isEmployeePage', true);
- $employees = $this->paginate($this->Employees);
- $employee = $this->Employees->newEmptyEntity();
- if ($this->request->is('post')) {
- $employee = $this->Employees->patchEntity($employee, $this->request->getData());
- $file = $this->request->getUploadedFiles();
- $employee->profile_picture = $file['profile_picture']->getClientFilename();
- $file['profile_picture']->moveTo(WWW_ROOT . 'img' . DS . $employee->profile_picture);
- if ($this->Employees->save($employee)) {
- $this->Flash->success(__('The employee has been saved.'));
- return $this->redirect(['action' => 'index']);
- }
- $this->Flash->error(__('The employee could not be saved. Please, try again.'));
- }
- $this->set(compact('employee'));
- }
- /**
- * Edit method
- *
- * @param string|null $id Employee id.
- * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function edit($id = null)
- {
- $this->set('isEmployeePage', true);
- $employees = $this->paginate($this->Employees);
- $employee = $this->Employees->get($id, [
- 'contain' => [],
- ]);
- if ($this->request->is(['patch', 'post', 'put'])) {
- $employee = $this->Employees->patchEntity($employee, $this->request->getData());
- $file = $this->request->getUploadedFiles();
- $employee->profile_picture = $file['profile_picture']->getClientFilename();
- $file['profile_picture']->moveTo(WWW_ROOT . 'img' . DS . $employee->profile_picture);
- if ($this->Employees->save($employee)) {
- $this->Flash->success(__('The employee has been saved.'));
- return $this->redirect(['action' => 'index']);
- }
- $this->Flash->error(__('The employee could not be saved. Please, try again.'));
- }
- $this->set(compact('employee'));
- }
- /**
- * Delete method
- *
- * @param string|null $id Employee id.
- * @return \Cake\Http\Response|null|void Redirects to index.
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function delete($id = null)
- {
- $this->request->allowMethod(['post', 'delete']);
- $employee = $this->Employees->get($id);
- if ($this->Employees->delete($employee)) {
- $this->Flash->success(__('The employee has been deleted.'));
- } else {
- $this->Flash->error(__('The employee could not be deleted. Please, try again.'));
- }
- return $this->redirect(['action' => 'index']);
- }
- public function beforeFilter(\Cake\Event\EventInterface $event)
- {
- parent::beforeFilter($event);
- // Configure the login action to not require authentication, preventing
- // the infinite redirect loop issue
- $this->Authentication->addUnauthenticatedActions(['login']);
- $this->Authentication->addUnauthenticatedActions(['login', 'add']);
- }
- public function login()
- {
- $this->request->allowMethod(['get', 'post']);
- $result = $this->Authentication->getResult();
- // regardless of POST or GET, redirect if user is logged in
- if ($result && $result->isValid()) {
- // redirect to /articles after login success
- $redirect = $this->request->getQuery('redirect', [
- 'controller' => 'Employees',
- 'action' => 'index',
- ]);
- return $this->redirect($redirect);
- }
- // display error if user submitted and authentication failed
- if ($this->request->is('post') && !$result->isValid()) {
- $this->Flash->error(__('Invalid username or password'));
- }
- }
- public function logout()
- {
- $result = $this->Authentication->getResult();
- // regardless of POST or GET, redirect if user is logged in
- if ($result && $result->isValid()) {
- $this->Authentication->logout();
- return $this->redirect(['controller' => 'Employees', 'action' => 'login']);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement