Skip to content

Instantly share code, notes, and snippets.

@nooot77
Created January 16, 2018 16:47
Show Gist options
  • Save nooot77/473909c4d2d73f5e7e581f756a335112 to your computer and use it in GitHub Desktop.
Save nooot77/473909c4d2d73f5e7e581f756a335112 to your computer and use it in GitHub Desktop.
articles
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
/* Articles Controller
*
* @property \App\Model\Table\ArticlesTable $Articles
*
* @method \App\Model\Entity\Article[] paginate($object = null, array $settings = [])
*/
use Cake\I18n\I18n;
class ArticlesController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|void
*/
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->Auth->allow(['search']);
}
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
}
public function index($search = null)
{
$this->paginate = [
'contain' => ['Categories']
];
$articles = $this->paginate($this->Articles);
$comments = $this->Articles->find('all')->contain(['Comments']);
$this->set(compact('articles','comments'));
$this->set('_serialize', ['articles']);
}
/**
* View method
*
* @param string|null $id Article id.
* @return \Cake\Http\Response|void
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$article = $this->Articles->get($id, [
'contain' => ['Categories']
]);
$this->set('article', $article);
$this->set('category_id', $article->category_id);
$this->set('category_parent_id', $article->category['parent_id']);
$this->set('article_id', $article->id);
$this->set('_serialize', ['article']);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$article = $this->Articles->newEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->getData());
if ($this->Articles->save($article)) {
$this->Flash->success(__('The article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The article could not be saved. Please, try again.'));
}
if ($this->request->session()->read('Config.language') == 'arabic')
{
$categorieslist = $this->Articles->Categories->find('list',['keyField' =>'id', 'valueField' => 'name_ara'])->toArray();
}else
{
$categorieslist = $this->Articles->Categories->find('list',['fields'=>array('id','name')])->toArray();
}
$this->set(compact('article', 'categorieslist'));
$this->set('_serialize', ['article']);
}
/**
* Edit method
*
* @param string|null $id Article id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$article = $this->Articles->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$article = $this->Articles->patchEntity($article, $this->request->getData());
if ($this->Articles->save($article)) {
$this->Flash->success(__('The article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The article could not be saved. Please, try again.'));
}
if ($this->request->session()->read('Config.language') == 'arabic')
{
$categorieslist = $this->Articles->Categories->find('list',['keyField' =>'id', 'valueField' => 'name_ara'])->toArray();
}else
{
$categorieslist = $this->Articles->Categories->find('list',['fields'=>array('id','name')])->toArray();
}
$this->set(compact('article', 'categorieslist'));
$this->set('_serialize', ['article']);
}
/**
* Delete method
*
* @param string|null $id Article id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$article = $this->Articles->get($id);
$query = $this->Articles->updateAll(['deleted'=>1],['id'=>$id]);
if ($query) {
$this->Flash->success(__('The article has been deleted.'));
} else {
$this->Flash->error(__('The article could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
function search()
{
if($this->request->is('ajax'))
{
$this->loadModel('Categories');
$this->loadModel('Articles');
if($this->request->data['keywords'] != '')
{
$keywords = strip_tags(trim($this->request->data['keywords']));
$categoriesResults = $this->Categories->find('all',['conditions' => ['or' => ['name like' => "%".$keywords."%", 'name_ara like' => "%".$keywords."%"]]])->toArray();
$articlesResults = $this->Articles->find('all',['conditions' => ['or' => ['title like' => "%".$keywords."%", 'title_ara like' => "%".$keywords."%"]]])->toArray();
$result = [];
if($categoriesResults)
$result['categories'] = $categoriesResults;
else
$result['categories'] = [];
if($articlesResults)
$result['articles'] = $articlesResults;
else
$result['categories'] = [];
if($categoriesResults && $articlesResults)
$result['success'] = 1;
else
$result['success'] = 0;
echo json_encode($result);
}
$this->autoRender= false;
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment