<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Schooltypes Controller
 *
 * @property \App\Model\Table\SchooltypesTable $Schooltypes
 */
class SchooltypesController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $schooltypes = $this->paginate($this->Schooltypes);

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

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

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

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

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The schooltype could not be saved. Please, try again.'));
        }
        $this->set(compact('schooltype'));
        $this->set('_serialize', ['schooltype']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Schooltype 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)
    {
        $schooltype = $this->Schooltypes->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $schooltype = $this->Schooltypes->patchEntity($schooltype, $this->request->data);
            if ($this->Schooltypes->save($schooltype)) {
                $this->Flash->success(__('The schooltype has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The schooltype could not be saved. Please, try again.'));
        }
        $this->set(compact('schooltype'));
        $this->set('_serialize', ['schooltype']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Schooltype 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']);
        $schooltype = $this->Schooltypes->get($id);
        if ($this->Schooltypes->delete($schooltype)) {
            $this->Flash->success(__('The schooltype has been deleted.'));
        } else {
            $this->Flash->error(__('The schooltype could not be deleted. Please, try again.'));
        }

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