Skip to content

Instantly share code, notes, and snippets.

@damianoporta
Created November 3, 2015 09:26
Show Gist options
  • Save damianoporta/684867dc90202de871b2 to your computer and use it in GitHub Desktop.
Save damianoporta/684867dc90202de871b2 to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
/**
* Tags Controller
*
* @property \App\Model\Table\TagsTable $Tags
*/
class TagsController extends AppController
{
public $components = ['Ajax.Ajax'];
public function initialize()
{
parent::initialize();
$this->Security->config('unlockedActions', ['getDataAjax']);
}
/**
* Richiesta ajax per ricerca Tags
*
* Parametri:
* - scope: intero o array di interi che indica lo scope dei tags desiderati
* - query: digitazione dell'utente
*
* @return type
*/
public function getDataAjax()
{
// Carico il componente ajax
$this->loadComponent('Ajax.Ajax', [
'flashKey' => false
]);
$this->request->allowMethod('ajax');
$tags = $this->Tags->find()->select(['Tags.id', 'Tags.tag_name']);
if (!empty($this->request->query['scope'])) {
if (!is_array($this->request->query['scope'])) {
$this->request->query['scope'] = [$this->request->query['scope']];
}
$tags->where(['Tags.scope' => $this->request->query['scope']], ['Tags.scope' => 'integer[]']);
}
if (!empty($this->request->params['query'])) {
$tags->where(['Tags.tag_name LIKE' => '%' . $this->request->query['query'] . '%']);
}
$data = $tags->order('Tags.tag_name ASC')->toArray();
$this->set(compact('data'));
$this->set('_serialize', ['data']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment