We need to create the JSON.class.php controller in the Admin folder.

The class needs to extend Module\JSON\Admin.

In the constructor function we need to set the permission for reading the data (we registered the Manage blog permission at the Setup) and call the parent constructor.

Then we need to implement the get() function.

This function will look at the $_REQUEST global variable to see if we searched for a specific article (when we need to edit an article we request the post by it's id) or we want to populate the data table.

For the data table we will lookup for the following filters that are send by the loadData(aoData) function from blog.js file:

  • length
  • start
  • secho
  • filters

The content of JSON.class.php file

//PSR-0 namespace
namespace Module\Blog\Admin;
//We need Model to work with the database
use Model\Model;
//JSON\Admin is the parent class
use Module\JSON\Admin;

require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/Utils/functions.php');
class JSON extends Admin {
	public function __construct() {
		//Set permission for Read action
		$this->permission = "Manage blog";
	public function get() {
		//Check if we need a specific post (for edit it's data) or we need a multiple posts (for the data table)
		if(!array_key_exists('id', $_REQUEST)) {
			//$posts is the database model
			$posts = new Model('posts');
			//If we selected a number of posts per page then set $itemsPerPage as that number, else set it to 10 posts per page
			$itemsPerPage = (array_key_exists('length', $_REQUEST))?$_REQUEST['length']:10;
			//If we have clicked on a page number then set the starting point for the query's limit
			$limit = ((array_key_exists('start', $_REQUEST))?$_REQUEST['start']:0) . ', ' . $itemsPerPage;
			//Count the total posts
			$countTotal = $posts->countItems();
			//Build the filters parameter
			if(array_key_exists('filters', $_REQUEST)) {
				foreach($_REQUEST['filters'] AS $key => $value) {
					//Use wildcard if we search by the title
					if(in_array($key, array('title'))) $posts->$key = array('%' . $value . '%', ' LIKE ');
					//Use exact match for every other filter
					else $posts->$key = $value;
			//Count the filtered posts
			$countFiltered = $posts->countItems();
			//Set the limit (starting point, posts per page)
			//Order posts by their updated value descending
			$posts->order('date_updated DESC');
			//Get the filtered & limited posts
			$posts = $posts->get('AND');
			$aaData = array();
			foreach($posts AS $post) {
				//We don't need content for every post because we don't show the value in the data table and will create a huge json if we don't unset it
				//Set the thumbnail filename for every post
				$post->image = str_replace('.jpg', '-360x220.jpg', $post->image);
				//Add the object to the returning array
				$aaData[] = $post;
			//Build the returning array
			$responseArray = array('sEcho' => $_REQUEST['secho'],
				'iTotalRecords'        => $countTotal,
				'iTotalDisplayRecords' => $countFiltered,
				'aaData'               => $aaData
			//Encode the array to json
			$response = json_encode($responseArray);
			//If we have a callback parameter then add it to the response
			if(array_key_exists('callback', $_GET)) $response = $_GET['callback'] . '(' . $response . ')';
			echo $response;
		else {
			//We searched a post by it's id
			$posts = new Model('posts');
			$posts = $posts->getOneResult('id', $_REQUEST['id']);
			//Set the thumbnail filename for the post
			$posts->image = str_replace('.jpg', '-360x220.jpg', $posts->image);
			echo json_encode($posts);