<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Supervisings Controller
 *
 * @property \App\Model\Table\SupervisingsTable $Supervisings
 */
class SupervisingsController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Schooltypes']
        ];
        $supervisings = $this->paginate($this->Supervisings);

        $this->set(compact('supervisings'));
        $this->set('_serialize', ['supervisings']);
    }

    /**
     * View method
     *
     * @param string|null $id Supervising id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $supervising = $this->Supervisings->get($id, [
            'contain' => ['Schooltypes', 'Departments']
        ]);

        $this->set('supervising', $supervising);
        $this->set('_serialize', ['supervising']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $supervising = $this->Supervisings->newEntity();
        if ($this->request->is('post')) {
            $supervising = $this->Supervisings->patchEntity($supervising, $this->request->data);
            if ($this->Supervisings->save($supervising)) {
                $this->Flash->success(__('The supervising has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The supervising could not be saved. Please, try again.'));
        }
        $schooltypes = $this->Supervisings->Schooltypes->find('list', ['limit' => 200]);
        $this->set(compact('supervising', 'schooltypes'));
        $this->set('_serialize', ['supervising']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Supervising id.
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $supervising = $this->Supervisings->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $supervising = $this->Supervisings->patchEntity($supervising, $this->request->data);
            if ($this->Supervisings->save($supervising)) {
                $this->Flash->success(__('The supervising has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The supervising could not be saved. Please, try again.'));
        }
        $schooltypes = $this->Supervisings->Schooltypes->find('list', ['limit' => 200]);
        $this->set(compact('supervising', 'schooltypes'));
        $this->set('_serialize', ['supervising']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Supervising id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $supervising = $this->Supervisings->get($id);
        if ($this->Supervisings->delete($supervising)) {
            $this->Flash->success(__('The supervising has been deleted.'));
        } else {
            $this->Flash->error(__('The supervising could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
