<?php
	namespace controller;
	use berkaPhp\Controller\BerkaPhpController;

	class {controller_name}Controller extends BerkaPhpController
	{

		function __construct() {
			parent::__construct();
		}

        /* Display all {names} from database
        *  Default action in this controller
        *  @author berkaPhp
        */

		function index() {

			$result = $this->model->fetchAll();
			$this->view->set('{names}', $result);
			$this->view->render();

		}

        /* Add {name} into database
        *  Getting data from Post
        *  @author berkaPhp
        */

		function add() {

			if($this->is_set($this->getPost())) {
				if ($this->model->add($this->getPost())) {
					$this->view->set('message', ['success'=>'Saved {name}']);
				} else {
					$this->view->set('message', ['error'=>' Could not Saved {name} !']);
				}
			}

			$this->view->render();
		}

        /* Edit {name} and update the table
        *  Getting data from Post
        *  Id from params array
        *  @author berkaPhp
        */

		function edit($params) {

			$id = $params['params'];

			if($this->is_set($this->getPost())) {
				if ($this->model->update($this->getPost())) {
					$this->view->set('message', ['success'=>'Edited {name}']);
				} else {
					$this->view->set('message', ['error'=>' Could not Edit {name} !']);
				}
			}

			$result = $this->model->fetchBy(['fields'=>['{primary_key}'=>$id]]);
			$this->view->set('{name}',$result);
			$this->view->render();
		}

        /* Delete {name} from the table
        *  Getting {name} Id from params array
        *  @author berkaPhp
        */

		function delete($params) {

			$id = $params['params'];

			if($this->model->delete($id)) {
				$this->view->set('message', ['success'=>'Deleted {name}']);
			} else {
				$this->view->set('message', ['error'=>' Could not Delete {name} !']);
			}

			$this->index();

		}

        /* Viewing {name} from the table
        *  Getting {name} Id from params array
        *  @author berkaPhp
        */

		function view($params) {

			$id = $params['params'];

			$result = $this->model->fetchBy(['fields'=>['{primary_key}'=>$id]]);
			$this->view->set('{name}',$result);
			$this->view->render();
		}

	}

?>