Skip to content

Instantly share code, notes, and snippets.

@damianoporta
Last active November 2, 2015 23:39
Show Gist options
  • Save damianoporta/ff79491c1fdc29f127cc to your computer and use it in GitHub Desktop.
Save damianoporta/ff79491c1fdc29f127cc to your computer and use it in GitHub Desktop.
<?php
/**
* Recupera gli annunci relazionati
*
* Priorità di ricerca:
*
* 1. Filtro annunci con stesse regioni + settori + tags (`Regions.id` e `Sectors.id` e `Tags.id`)
* 2. Filtro annunci con stesse regioni + settori (`Regions.id` e `Sectors.id`)
* 3. Filtro annunci con stessi settori (`Sectors.id`)
* 4. Filtro annunci stesse regioni (`Regions.id`)
*
* Opzioni utilizzate:
*
* - excludeId: Lista degli id da escludere
* - Regions.id: Lista regioni da filtrare
* - Sectors.id: Lista settori da filtrare
* - Tags.id: Lista tags generici da filtrare
* - total: Numero massimo di record da recuperare
*
* @param Query $query
* @param array $options
* @return Query
*/
public function findRelated(Query $query, array $options = [])
{
$unionQueries = '';
//Controllo se sono stati inseriti id da escludere
if (!empty($options['excludeId'])) {
// Controllo se è stato passato un array
if (!is_array($options['excludeId'])) {
$options['excludeId'] = [$options['excludeId']];
}
}
$exclude = empty($options['excludeId']) ? []: ["Ads.id NOT IN" => $options['exclude']];
// 1. Controllo se deve ricercare per SETTORI + REGIONI + TAGS
If (!empty($options['Sectors.id']) && !empty($options['Regions.id']) && !empty($options['Tags.id'])) {
$q = $this->find('ads')
->find('sectors', ['matching' => true, 'query_conditions' => ['SectorsTags.tag_id IN' => $options['Sectors.id']]])
->find('regions', ['matching' => true, 'query_conditions' => ['RegionsTags.tag_id IN' => $options['Regions.id']]])
->find('generics', ['matching' => true, 'query_conditions' => ['GenericsTags.tag_id IN' => $options['Tags.id']]])
->find('online')
->where($exclude);
$unionQueries = $q;
}
// 2. Controllo se deve ricercare per SETTORI + REGIONI
If (!empty($options['Sectors.id']) && !empty($options['Regions.id'])) {
$q = $this->find('ads')
->find('sectors', ['matching' => true, 'query_conditions' => ['SectorsTags.tag_id IN' => $options['Sectors.id']]])
->find('regions', ['matching' => true, 'query_conditions' => ['RegionsTags.tag_id IN' => $options['Regions.id']]])
->find('generics')
->find('online')
->where($exclude);
if (!$unionQueries) {
$unionQueries = $q;
} else {
$unionQueries->union($q);
}
}
// 3. Controllo se deve solo SETTORI
if (!empty($options['Sectors.id'])) {
$q = $this->find('ads')
->find('sectors', ['matching' => true, 'query_conditions' => ['SectorsTags.tag_id IN' => $options['Sectors.id']]])
->find('regions')
->find('generics')
->find('online')
->where($exclude);
if (!$unionQueries) {
$unionQueries = $q;
} else {
$unionQueries->union($q);
}
}
// 4. Controllo se deve solo REGIONI
if (!empty($options['Regions.id'])) {
$q = $this->find('ads')
->find('sectors')
->find('regions', ['matching' => true, 'query_conditions' => ['RegionsTags.tag_id IN' => $options['Regions.id']]])
->find('generics')
->find('online')
->where($exclude);
if (!$unionQueries) {
$unionQueries = $q;
} else {
$unionQueries->union($q);
}
}
if ($unionQueries) {
if (!empty($options['total'])) {
return $unionQueries->epilog('LIMIT ' . $options['total']);
}
return $unionQueries;
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment