110 lines
3.5 KiB
PHP
110 lines
3.5 KiB
PHP
<?php
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Blog extends Admin_Controller {
|
|
|
|
public function index() {
|
|
$data['page_title'] = 'Blog';
|
|
$this->renderPage('index', $data);
|
|
}
|
|
|
|
public function getBlogs() {
|
|
$postData = $this->input->post();
|
|
$this->validate($postData, 'filter');
|
|
|
|
$params = !empty($postData['params']) ? $postData['params'] : null;
|
|
$pageNo = !empty($postData['pageNo']) ? $postData['pageNo'] - 1 : 0;
|
|
$pageSize = !empty($postData['pageSize']) ? $postData['pageSize'] : 10;
|
|
|
|
$this->load->model('blog_model');
|
|
$data = $this->blog_model->getBlogs($params, $pageNo, $pageSize);
|
|
|
|
echo json_encode($data);
|
|
}
|
|
|
|
public function store() {
|
|
$postData = $this->input->post();
|
|
$this->validate($postData);
|
|
|
|
$this->load->model('blog_model');
|
|
|
|
// Find record with blog_id
|
|
$record = $this->blog_model->getBlogs(['blog_id' => $postData['blog_id']]);
|
|
|
|
// If we found a record then return errors
|
|
if(!empty($record['result'])) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(['errors' => ['message' => 'This Blog ID had been used']]);
|
|
return;
|
|
}
|
|
|
|
$this->blog_model->store($postData);
|
|
|
|
echo json_encode(['message' => 'Added successfully!']);
|
|
}
|
|
|
|
public function update() {
|
|
$postData = $this->input->post();
|
|
$this->validate($postData);
|
|
|
|
$this->load->model('blog_model');
|
|
|
|
// Find record with blog_id
|
|
$record = $this->blog_model->getBlogs(['blog_id' => $postData['blog_id']]);
|
|
|
|
// If we found a record then return errors
|
|
if(!empty($record['result']) && $record['result'][0]['id'] != $postData['id']) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(['errors' => ['message' => 'This Blog ID had been used']]);
|
|
return;
|
|
}
|
|
|
|
$id = array_shift($postData);
|
|
|
|
$this->blog_model->update($id, $postData);
|
|
|
|
echo json_encode(['message' => 'Updated successfully!']);
|
|
}
|
|
|
|
public function delete() {
|
|
$postData = $this->input->post();
|
|
|
|
$this->load->model('blog_model');
|
|
$this->blog_model->delete($postData);
|
|
|
|
echo json_encode(['message' => 'Deleted successfully!']);
|
|
}
|
|
|
|
protected function validate($data, $type = 'saveOrUpdate') {
|
|
$this->load->library('form_validation');
|
|
$this->form_validation->set_data($data);
|
|
$this->setFormRuleForBlog($type);
|
|
|
|
if ($this->form_validation->run() === FALSE) {
|
|
$errors = $this->form_validation->error_array();
|
|
header('HTTP/1.1 400 Bad Request');
|
|
echo json_encode(['errors' => $errors]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
protected function setFormRuleForBlog($type) {
|
|
if ($type === 'filter') {
|
|
$this->form_validation->set_rules('params[blog_id]', 'Blog ID', 'integer');
|
|
$this->form_validation->set_rules('params[status]', 'Status', 'integer');
|
|
return;
|
|
}
|
|
|
|
$this->form_validation->set_rules('blog_id', 'Blog ID', 'required|integer');
|
|
$this->form_validation->set_rules('description', 'Description', 'required');
|
|
$this->form_validation->set_rules('status', 'Status', 'integer');
|
|
}
|
|
|
|
protected function renderPage($page_name, $data) {
|
|
$this->load->view('admin/view_admin_header', $data);
|
|
$this->load->view('blog/' . $page_name, $data);
|
|
$this->load->view('admin/view_admin_footer', $data);
|
|
}
|
|
}
|