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 the $_POST variable)
  • call the act() function to do the action

In our case we need to override the act() function 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

//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 Utils\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() {
		//Set permission for Create / Update / Delete actions
		$this->permission = 'Manage blog';
		//Set the entity's table name
		$this->entity = new Model('posts');
		//Set the fields for the entity from $_POST
		$this->fields = $_POST;
		//Set the admin user for the post current admin
		$this->fields['admin'] = AdminController::getCurrentUser()->id;
		//Do action on entity
		return $this->act();
	}
	public function act() {
		$adminController = new AdminController();
		//Check if current admin has permission for this operations
		if($adminController->checkPermission($this->permission)) {
			//If is a create / update request then we have id set on fields
			if(array_key_exists('id', $this->fields)) {
				foreach($this->fields AS $key => $value) {
					//If current field is image then create post thumbnail
					if($key == '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_ . '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']);
					}
					elseif($key == 'content') $this->fields[$key] = htmlspecialchars_decode($value);
					elseif($key == 'title') {
						//Register route in the database
						$this->fields[$key] = $value;
						$url = 'blog/' . Util::getUrlFromString($this->fields['title']) . '.html';
						//If route exists but is not registered by module Blog then return false
						$mR = new Model('module_routes');
						$mR = $mR->getOneResult('url', $url);
						if($mR && $mR->modules->name != 'Blog') return false;
						else {
							if(!$mR) {
								$setup = new Setup();
								$setup->registerFrontendUrl(array('url' => $url, 'type' => 0, 'mustBeLoggedIn' => 0, 'menu_position' => 0));
							}
						}
					}
				}
			}
		}
		return parent::act();
	}
}