Now we need to add / update / delete an entity, so we will create the action file Act.class.php controller in the Admin folder.

The class needs to extend Controller\AdminAct.

In the constructor function we need to:

  • set the permission for acting on the data (we registered the Manage blog permission at the Setup)
  • set the entity (the Model object for the MySQL tablename)
  • set the fields values (from one of the $_POST, $_PATCH or $_DELETE global variables)

In our case we need to override the Controller\AdminAct create(), patch() or delete() functions because we need to take care with the post image (move to it's folder, create it's thumbnail) and to build it's URL.

The content of Act.class.php file

<?php
//PSR-0 namespace
namespace Module\Blog\Admin;
//AdminAct is the parent class
use Controller\AdminAct;
//We need AdminController to check if current admin has access for creating / updating / deleting posts
use Controller\AdminController;
//We need Model to work with the database
use Model\Model;
//We need Setup if we need to recreate an URL from the post title
use Module\Blog\Setup;
//We need GD library for post's thumbnail
use \PHPThumb\GD;
//We need Util class for a function that converts posts titles into URLs
use Utils\Util;

require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/Utils/functions.php');

class Act extends AdminAct {
	public function __construct($id) {
		//Set permission for Create / Update / Delete actions
		$this->permission = 'Manage blog';
		//Set the entity's table name
		$this->entity = new Model('posts');
        $act = false;
        //Check if user has access
        if ($this->hasAccess()) {
            //$id is needed for update and delete, it is set in constructor by the admin router
            if ($id) $this->fields['id'] = $id;
            //The methods are POST for create, PATCH for update and DELETE for delete
            if (strtolower($_SERVER['REQUEST_METHOD']) == 'delete') {
                if ($id) {
                    $act = $this->delete();
                }
                else $this->sendStatus(false, __('No ID set'));
            }
            else {
                $method = 'patch';
                if (strtolower($_SERVER['REQUEST_METHOD']) == 'patch') {
                    //Set the global variable PATCH from the input
                    if ($id) parse_str(file_get_contents('php://input'), $_PATCH);
                    else $this->sendStatus(false, __('No ID set'));
                }
                else {
                    $method = 'create';
                    //Set the admin user for the post current admin
                    $this->fields['admin'] = AdminController::getCurrentUser()->id;
                }
                foreach ($method == 'patch' ? $_PATCH : $_POST AS $key => $value) $this->fields[$key] = $value;
                try {
                    $act = call_user_func_array(array($this, $method), array());
                }
                catch (\Exception $e) {
                    $this->sendStatus(false, $e->getMessage());
                }
            }
        }
        $this->sendStatus($act);
	}

	//Create post
    public function create() {
        $this->createThumbnail()->setupPostContent()->setupPostUrl();
        foreach ($this->fields AS $key => $value) $this->entity->$key = $value;
        return $this->entity->create();
    }

    //Update post
    public function patch() {
        $this->createThumbnail()->setupPostContent()->setupPostUrl(true);
        foreach ($this->fields AS $key => $value) $this->entity->$key = $value;
        return $this->entity->update();
    }

    //Delete post
    public function delete() {
        $this->entity->id = $this->fields['id'];
        //We have to delete the URL for the post
        $oldPostValues = new Model('posts');
        $oldPostValues = $oldPostValues->getOneResult('id', $this->fields['id']);
        $oldUrl = 'blog/' . Util::getUrlFromString($oldPostValues->title);
        $mR = new Model('module_routes');
        $mR = $mR->getOneResult('url', $oldUrl);
        if ($mR && $mR->modules->name == 'Blog') {
            $mR->delete();
        }
        return $this->entity->delete();
    }

    private function createThumbnail() {
        if(arrayKeyExists('image', $this->fields)) {
            $value = $this->fields['image'];
            $filename = strip_tags($value);
            if(!empty(trim($filename))) {
                $upload_dir = _APP_DIR_ . 'uploads/';
                $uploaded_file = $upload_dir . $filename;
                $path_parts = pathinfo($uploaded_file);
                $fname = $path_parts['filename'];
                $extension = $path_parts['extension'];
                $target_dir = _APP_DIR_ . 'assets/img/posts/';
                $destination_file = $target_dir . $filename;
                $thumb_file_name = 'thumb' . $path_parts['filename'] . '.jpg';
                $thumb_target_file = $upload_dir . $thumb_file_name;
                unlink($thumb_target_file);
                rename($uploaded_file, $destination_file);
                $thumb_file_name360 = $target_dir . $fname . '-360x220.' . $extension;
                $thumb_file_name720 = $target_dir . $fname . '-720x220.' . $extension;
                $thumb = new GD($destination_file);
                $thumb->resize(720, 220);
                $thumb->save($thumb_file_name720, $extension);
                $thumb->resize(360, 220);
                $thumb->save($thumb_file_name360, $extension);
            }
            else unset($this->fields['image']);
        }
        return $this;
    }

    private function setupPostContent() {
        if(arrayKeyExists('content', $this->fields)) $this->fields['content'] = htmlspecialchars_decode($this->fields['content']);
        return $this;
    }

    private function setupPostUrl($isUpdate = false) {
        //If it is an update, delete the old url for the post if it's changed
        if($isUpdate) {
            $oldPostValues = new Model('posts');
            $oldPostValues = $oldPostValues->getOneResult('id', $this->fields['id']);
            if($oldPostValues->title != $this->fields['title']) {
                $oldUrl = 'blog/' . Util::getUrlFromString($oldPostValues->title);
                $mR = new Model('module_routes');
                $mR = $mR->getOneResult('url', $oldUrl);
                if ($mR && $mR->modules->name == 'Blog') {
                    $mR->delete();
                }
            }
        }
        $url = 'blog/' . Util::getUrlFromString($this->fields['title']);
        $mR = new Model('module_routes');
        $mR = $mR->getOneResult('url', $url);
        if(!$mR) {
            $setup = new Setup();
            $setup->registerFrontendUrl(array('url' => $url, 'type' => 0, 'mustBeLoggedIn' => 0, 'menu_position' => 0));
        }
        return $this;
    }
}