CriteresRecherche.php 28.8 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20 21
 */

22
class Class_CriteresRecherche extends Class_CriteresRecherche_Abstract {
Patrick Barroca's avatar
Patrick Barroca committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  public static $criteres = ['expressionRecherche' => 'Expression',
                             'rech_titres' => 'Titre',
                             'rech_auteurs' => 'Auteur',
                             'rech_matieres' => 'Sujet',
                             'rech_dewey' => 'Dewey / pcdm4',
                             'rech_editeur' => 'Editeur',
                             'rech_collection' => 'Collection',
                             'type_doc' => 'Type de document',
                             'nouveaute' => 'Nouveauté',
                             'annee_debut' => 'Documents parus entre ',
                             'annee_fin' => 'et',
                             'rubrique' => 'Rubrique',
                             'tri' => 'Trié par',
                             'id_catalogue' => 'Catalogue',
                             'id_panier' => 'Panier',
                             'retour_panier' => 'Panier',
                             'retour_abonne' => 'Retour compte',
                             'retour_avis' => 'Retour avis',
                             'operateur_editeur'=> 'op editeur',
                             'operateur_titres'=> '',
                             'operateur_auteurs'=> '',
                             'operateur_matieres' => '',
                             'operateur_dewey'=>'',
                             'operateur_editeur'=>'',
                             'operateur_collection'=>'',
                             'section' => '',
50
                             'form_id' => '',
Patrick Barroca's avatar
Patrick Barroca committed
51 52 53 54 55
                             'page' => 1,
                             'page_size' => '',
                             'genre' => '',
                             'liste_format' => '',
                             'digital_lib' => '',
56
                             'no_extension' => '',
57 58
                             'bookmarked_search' => '',
                             'bookmarked_version' => '',
59
                             'in_files' => ''
Patrick Barroca's avatar
Patrick Barroca committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
  ];


  public function __construct() {
    $this->_profil = Class_Profil::getCurrentProfil();
  }


  public function __wakeup() {
    $this->_profil = Class_Profil::getCurrentProfil();
  }


  public function __sleep() {
74 75
    return ['_params',
            '_time'];
Patrick Barroca's avatar
Patrick Barroca committed
76 77 78 79 80 81 82 83 84 85
  }


  public function getValidParameters() {
    return array_merge(array_keys(static::$criteres),
                       ['filtres',
                        'id_user',
                        'id_module',
                        'facette',
                        'facettes',
Patrick Barroca's avatar
Patrick Barroca committed
86
                        'multifacets',
Patrick Barroca's avatar
Patrick Barroca committed
87 88 89 90 91 92 93 94
                        'code_rebond',
                        'pertinence',
                        'type_recherche',
                        'annexe',
                        'geo_zone',
                        'geo_bib',
                        'fil',
                        'bib_select',
95
                        'serie',
96
                        'from',
97 98
                        'selection',
                        'authorities']);
Patrick Barroca's avatar
Patrick Barroca committed
99 100 101
  }


102 103 104 105 106 107
  public function getSearchModeList() {
    return [$this->_('Index seulement'),
            $this->_('Index et contenu des fichiers')];
  }


Patrick Barroca's avatar
Patrick Barroca committed
108
  public function getListeTris() {
lbrun's avatar
lbrun committed
109 110 111 112 113 114
    if (!isset($this->_liste_tris)) {
      $relevance_label = $this->getPanier()
        ? $this->_('Ordre du panier')
        : $this->_('Pertinence');

      $this->_liste_tris =  [self::SORT_RELEVANCE => $relevance_label,
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                             self::SORT_TITLE_ASC => $this->_('Titre A-Z'),
                             self::SORT_TITLE_DESC => $this->_('Titre Z-A'),
                             self::SORT_AUTHOR_ASC => $this->_('Auteur A-Z'),
                             self::SORT_AUTHOR_DESC => $this->_('Auteur Z-A'),
                             self::SORT_PUBLICATION_ASC => $this->_('Année de publication croissant'),
                             self::SORT_PUBLICATION_DESC => $this->_('Année de publication décroissant'),
                             self::SORT_DOCTYPE_ASC => $this->_('Type de document A-Z'),
                             self::SORT_DOCTYPE_DESC => $this->_('Type de document Z-A'),
                             self::SORT_NOVELTY_ASC => $this->_('Date de nouveauté croissant'),
                             self::SORT_NOVELTY_DESC => $this->_('Date de nouveauté décroissant'),
                             self::SORT_VIEWS_ASC => $this->_('Consultation croissant'),
                             self::SORT_VIEWS_DESC => $this->_('Consultation décroissant'),
                             static::SORT_TITLE => '',
                             static::SORT_AUTHOR => '',
                             static::SORT_PUBLICATION => '',
                             static::SORT_DOCTYPE => '',
                             static::SORT_NOVELTY => '',
                             static::SORT_VIEWS => ''];
lbrun's avatar
lbrun committed
133
    }
Patrick Barroca's avatar
Patrick Barroca committed
134 135 136 137
    return $this->_liste_tris;
  }


138
  public function getAvailablePageSize() {
139
    $profil_param = (new Class_Profil_Preferences_SearchResult())->getPageSize($this->_profil);
140 141 142 143 144 145 146 147
    $options = array_unique(['10' => 10,
                             '20' => 20,
                             '30' => 30,
                             '40' => 40,
                             '50' => 50,
                             $profil_param => $profil_param]);
    ksort($options);
    return $options;
148
  }
149 150


Patrick Barroca's avatar
Patrick Barroca committed
151 152 153 154 155 156 157 158 159 160
  public function rechercheElargie() {
    $this->_params['pertinence']=1;
    return $this;
  }


  public function getPertinence() {
    return (1==$this->getParam('pertinence', false));
  }

161

162 163 164 165 166
  public function filterLetterCode($data) {
    return preg_match('/[a-zA-Z][0-9]+/', trim($data));
  }


Patrick Barroca's avatar
Patrick Barroca committed
167
  public function getMultiFacets() {
168 169 170
    return array_filter(array_merge(explode('-', $this->getParam('multifacets','')),
                                    $this->selectionToArray(Class_CodifGenre::CODE_FACETTE,
                                                            $this->getGenre())));
Patrick Barroca's avatar
Patrick Barroca committed
171 172 173
  }


174 175 176 177 178 179
  public function getAuthorities() {
    return (new Class_CriteresRecherche_AuthoritiesParam)
      ->fromParamString($this->getParam('authorities'));
  }


Patrick Barroca's avatar
Patrick Barroca committed
180 181 182 183
  public function hasEmptyDomain() {
    if( !$catalogue = Class_Catalogue::find($this->getParam('id_catalogue')))
      return false;
    return $catalogue->isEmpty();
184

Patrick Barroca's avatar
Patrick Barroca committed
185
  }
186 187


188 189
  public function getGeoZone() {
    return $this->getParam('geo_zone', false);
Patrick Barroca's avatar
Patrick Barroca committed
190
  }
191

192

Patrick Barroca's avatar
Patrick Barroca committed
193 194 195
  public function getGeoBib() {
    return $this->getParam('geo_bib', false);
  }
196

197

Patrick Barroca's avatar
Patrick Barroca committed
198 199 200
  public function getGenre() {
    return $this->getParam('genre', null);
  }
201 202


Patrick Barroca's avatar
Patrick Barroca committed
203
  public function getFiltres() {
204
    $filtres = [];
205
    if (!$this->isRecherchePanier()
206
        && !$this->isSelection()
207 208 209
        && !$this->isRechercheCatalogueEmpty()
        && $this->_profil) {
      $filtres = array_merge($filtres,
Laurent's avatar
Laurent committed
210
                             $this->_getProfilFilters($this->_profil));
Patrick Barroca's avatar
Patrick Barroca committed
211
    }
212

213
    $filtres_array = array_filter(explode('-', $this->getParam('filtres', null)));
214

Patrick Barroca's avatar
Patrick Barroca committed
215
    foreach ($filtres_array as $filtre_str) {
216 217 218
      if (($codes = array_filter(explode(';',$filtre_str), [$this, 'validCodeOrNull']))
          && ($key = Class_Notice_Facette::extractCodeRubrique($codes[0])))
        $filtres[$key] = $codes;
Patrick Barroca's avatar
Patrick Barroca committed
219
    }
220

Laurent's avatar
Laurent committed
221 222 223 224 225 226 227
    if ($bibs = $this->getBibs())
      $filtres[Class_Bib::CODE_FACETTE] = array_map(function($bib)
                                                    {
                                                      return Class_Bib::CODE_FACETTE . $bib;
                                                    },
                                                    $bibs);

228 229 230
    if (($geo_zone = $this->getGeoZone())
        && ($geo_zone = Class_Zone::find($geo_zone))) {
      $all_bibs = $geo_zone->getBibs();
Laurent's avatar
Laurent committed
231 232 233 234 235
      $filtres[Class_Bib::CODE_FACETTE] = array_map(function($bib)
                                                    {
                                                      return Class_Bib::CODE_FACETTE . $bib->getId();
                                                    },
                                                    $all_bibs);
Patrick Barroca's avatar
Patrick Barroca committed
236
    }
efalcy's avatar
efalcy committed
237

Patrick Barroca's avatar
Patrick Barroca committed
238 239
    if (($geo_bib = $this->getGeoBib())
        && $bib = Class_Bib::find((int)$geo_bib)) {
Laurent's avatar
Laurent committed
240
      $filtres[Class_Bib::CODE_FACETTE] = [Class_Bib::CODE_FACETTE . $bib->getId()];
Patrick Barroca's avatar
Patrick Barroca committed
241
    }
242

243
    return array_filter($filtres);
Patrick Barroca's avatar
Patrick Barroca committed
244
  }
245 246


247
  public function selectionToArray($letter, $str, $closure = null) {
Patrick Barroca's avatar
Patrick Barroca committed
248
    if (!$str)
efalcy's avatar
efalcy committed
249
      return [];
250 251
    $items = array_filter(explode(';',str_replace(' ','',$str)),
                          [$this, 'validCodeOrNull']);
Patrick Barroca's avatar
Patrick Barroca committed
252
    foreach ($items as $key =>  $item) {
253
      if (!$closure) {
Laurent's avatar
Laurent committed
254
        $items[$key] = $letter . $item;
255 256
        continue;
      }
Laurent's avatar
Laurent committed
257

258 259
      if (!$code = $closure($item))
        continue;
Laurent's avatar
Laurent committed
260
      $items[$key] = $letter . $code;
Patrick Barroca's avatar
Patrick Barroca committed
261 262 263
    }
    return $items;
  }
264

265

Patrick Barroca's avatar
Patrick Barroca committed
266
  public function getTypeDoc() {
267
    $digital_resource = Class_DigitalResource::getInstance();
268
    $validator = function($type) use($digital_resource){
269
      return null !== Class_TypeDoc::find($type);
270
    };
271 272 273
    return array_filter(explode(',',
                                $this->getParam('type_doc')),
                        $validator);
Patrick Barroca's avatar
Patrick Barroca committed
274
  }
275 276


Patrick Barroca's avatar
Patrick Barroca committed
277 278 279
  public function getMatiere() {
    return $this->getParam('matiere', 0);
  }
280

281

282 283 284 285 286
  public function getInFiles() {
    return 1 === (int)$this->getParam('in_files');
  }


Patrick Barroca's avatar
Patrick Barroca committed
287
  public function getTri() {
288 289 290 291 292 293 294 295 296 297 298 299
    if ($tri = urldecode($this->getParam('tri', '')))
      return isset($this->getListeTris()[$tri])
        ? $tri
        : Class_CriteresRecherche::SORT_RELEVANCE;

    if (($user_order = (new Class_User_Settings(Class_Users::getIdentity()))->getSearchOrder())
        && (isset($this->getListeTris()[$user_order])))
      return $user_order;

    return $this->isRechercheCatalogue()
      ? self::SORT_PUBLICATION
      : static::SORT_RELEVANCE;
Patrick Barroca's avatar
Patrick Barroca committed
300
  }
301

302

Patrick Barroca's avatar
Patrick Barroca committed
303 304 305 306 307 308
  public function getTypeRecherche() {
    $type = $this->getParam('type_recherche', 'fulltext');
    if ($type != 'commence')
      return 'fulltext';
    return $type;
  }
309

310

Patrick Barroca's avatar
Patrick Barroca committed
311 312 313 314
  public function getCatalogue()  {
    if ($this->getParam('id_catalogue',''))
      return Class_Catalogue::find($this->getParam('id_catalogue'));
  }
315 316


Patrick Barroca's avatar
Patrick Barroca committed
317 318
  public function getPanier()  {
    $panier = null;
319
    $id_panier = $this->getParam('id_panier','');
320

Patrick Barroca's avatar
Patrick Barroca committed
321
    if ($id_user=$this->getParam('id_user'))
322
      $panier = Class_PanierNotice::findFirstBy(['id_user' => $id_user,
323
                                                              'id' => $id_panier]);
Patrick Barroca's avatar
Patrick Barroca committed
324 325
    return $panier ? $panier : Class_PanierNotice::find($id_panier);
  }
326

llaffont's avatar
llaffont committed
327

328 329 330 331 332 333 334
  public function getSelection() {
    return $this->isSelection()
      ? new Class_RecordSelection()
      : null;
  }


Patrick Barroca's avatar
Patrick Barroca committed
335
  public function visitCatalogue($visitor, $catalogue) {
Laurent's avatar
Laurent committed
336 337
    $visitor->visitCatalogue($catalogue);

Patrick Barroca's avatar
Patrick Barroca committed
338
    $criteres = $this->getCriteres();
339

Patrick Barroca's avatar
Patrick Barroca committed
340 341
    $domain_settings = $catalogue->buildCriteresRecherche();
    $domain_critere = (new Class_CriteresRecherche())->clearProfil();
342

Patrick Barroca's avatar
Patrick Barroca committed
343
    $visitor->visitDomain($domain_critere->setParams($domain_settings));
344

Patrick Barroca's avatar
Patrick Barroca committed
345 346
    if ($catalogue->getCoteDebut()||$catalogue->getCoteFin())
      $visitor->visitCoteDebutFin($catalogue->getCoteDebut(),$catalogue->getCoteFin());
347

Patrick Barroca's avatar
Patrick Barroca committed
348 349
    if (1 == $catalogue->getNouveaute())
      $visitor->visitNouveaute(0);
350

Patrick Barroca's avatar
Patrick Barroca committed
351 352
    if ($catalogue->getAnneeDebut() || $catalogue->getAnneeFin())
      $visitor->visitAnneeDebutFin($catalogue->getAnneeDebut(),$catalogue->getAnneeFin());
353

Patrick Barroca's avatar
Patrick Barroca committed
354 355
    if($cles_notices = Class_NoticeDomain::getClesNoticesForDomain($catalogue->getId()))
      $visitor->visitFacetteDomainForOrConditions($catalogue->getId());
356

Patrick Barroca's avatar
Patrick Barroca committed
357 358 359 360 361
    if(isset($criteres['id_catalogue']) && !empty($criteres['id_catalogue'])) {
      $visitor->visitFilterOnDomain((new Class_CriteresRecherche())
                                    ->setParams($this->cleansCritereForFilterOnDomain($criteres, ['id_catalogue'])));
    }
  }
362 363


Laurent's avatar
Laurent committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  protected function _getProfilFilters($profil) {
    return
      [
       Class_CodifTypeDoc::CODE_FACETTE
       => $this->selectionToArray(Class_CodifTypeDoc::CODE_FACETTE,
                                  $this->_profil->getSelTypeDoc()),

       Class_CodifAnnexe::CODE_FACETTE
       => $this->selectionToArray(Class_CodifAnnexe::CODE_FACETTE,
                                  $this->_profil->getSelAnnexe()),

       Class_CodifSection::CODE_FACETTE
       => $this->selectionToArray(Class_CodifSection::CODE_FACETTE,
                                  $this->_profil->getSelSection()),

       Class_CodifThesaurus::CODE_FACETTE
       => $this->selectionToArray(Class_CodifThesaurus::CODE_FACETTE,
                                  $this->_profil->getDomainIds(),
                                  function($id)
                                  {
                                    return ($thesaurus = Class_CodifThesaurus::findThesaurusForCatalogue($id))
                                      ? $thesaurus->getIdThesaurus()
                                      : null;
                                  })
      ];
  }


Patrick Barroca's avatar
Patrick Barroca committed
392 393 394
  protected function cleansCritereForFilterOnDomain($criteres, $keys = []) {
    foreach($keys as $key)
      unset($criteres[$key]);
395

Patrick Barroca's avatar
Patrick Barroca committed
396 397
    return $criteres;
  }
398

399

Patrick Barroca's avatar
Patrick Barroca committed
400 401 402
  protected function isRechercheCatalogueEmpty() {
    return $this->isRechercheCatalogue() && !Class_Catalogue::hasFilters($this->getParam('id_catalogue'));
  }
403 404


Patrick Barroca's avatar
Patrick Barroca committed
405 406 407
  protected function isRechercheCatalogue() {
    return (!$this->getCodeRebond() && !$this->getSerie() && $this->getParam('id_catalogue'));
  }
408

409

Patrick Barroca's avatar
Patrick Barroca committed
410 411 412
  protected function isRecherchePanier() {
    return (!$this->getCodeRebond() && !$this->getSerie() && $this->getParam('id_panier'));
  }
413

414

415
  public function isSelection() {
416 417 418 419
    return (bool)$this->getParam('selection');
  }


420 421 422 423 424
  public function isEmptySelection() {
    return $this->isSelection() && $this->getSelection()->isEmpty();
  }


425 426 427 428
  public function isRechercheCatalogueOuPanierOuSelection() {
    return $this->isRechercheCatalogue()
      || $this->isRecherchePanier()
      || $this->isSelection();
Patrick Barroca's avatar
Patrick Barroca committed
429
  }
gloas's avatar
gloas committed
430

431

Patrick Barroca's avatar
Patrick Barroca committed
432 433 434
  public function isSearchInBasket() {
    return null != $this->getParam('id_panier');
  }
435 436


Patrick Barroca's avatar
Patrick Barroca committed
437
  public function visitPanierCatalogue($visitor) {
438
    if (!$this->isRechercheCatalogueOuPanierOuSelection())
Patrick Barroca's avatar
Patrick Barroca committed
439
      return $this;
440

Patrick Barroca's avatar
Patrick Barroca committed
441
    if ($catalogue = $this->getCatalogue()) {
Laurent's avatar
Laurent committed
442
      $this->visitCatalogue($visitor, $catalogue);
Patrick Barroca's avatar
Patrick Barroca committed
443 444
      return $this;
    }
445

Patrick Barroca's avatar
Patrick Barroca committed
446
    if (($panier = $this->getPanier()) && ($notices = $panier->getClesNotices())) {
Laurent's avatar
Laurent committed
447
      $visitor->visitPanier($panier);
Patrick Barroca's avatar
Patrick Barroca committed
448 449
      return $this;
    }
450

451
    if ($selection = $this->getSelection()) {
452 453 454 455
      $visitor->visitSelection($selection);
      return $this;
    }

Patrick Barroca's avatar
Patrick Barroca committed
456 457 458
    $visitor->setErreur($this->_('La sélection ne contient aucune notice'));
    return $this;
  }
gloas's avatar
gloas committed
459 460


Patrick Barroca's avatar
Patrick Barroca committed
461 462
  public function visitByRechercheType($visitor) {
    $this->visitPanierCatalogue($visitor);
gloas's avatar
gloas committed
463

Patrick Barroca's avatar
Patrick Barroca committed
464 465
    if ($visitor->hasErreur())
      return $this;
gloas's avatar
gloas committed
466

Patrick Barroca's avatar
Patrick Barroca committed
467 468 469 470
    if ($serie = $this->getSerie())  {
      $visitor->visitSerie($serie,$this->getTri());
      return $this;
    }
471

Patrick Barroca's avatar
Patrick Barroca committed
472 473 474 475
    if ($code_rebond = $this->getCodeRebond()) {
      $visitor->visitCodeRebond($code_rebond);
      return $this;
    }
476

Patrick Barroca's avatar
Patrick Barroca committed
477 478 479 480 481
    if ($rubrique = $this->getRubrique()) {
      unset($this->_params['expressionRecherche']);
      $visitor->visitRubrique($rubrique, $this->getFil());
      return $this;
    }
482

Patrick Barroca's avatar
Patrick Barroca committed
483
    if ($expression = $this->getExpressionRecherche())
484 485 486 487
      $visitor->visitExpression($expression,
                                $this->getPertinence(),
                                $this->getTri(),
                                $this->getInFiles());
488

Patrick Barroca's avatar
Patrick Barroca committed
489
    $type_recherche = $this->getTypeRecherche();
Ghislain Loas's avatar
Ghislain Loas committed
490 491 492 493 494 495 496

    $text_inputs = array_merge(['titres',
                                'auteurs',
                                'matieres',
                                'dewey',
                                'collection']);

Patrick Barroca's avatar
Patrick Barroca committed
497
    foreach($text_inputs as $name) {
Ghislain Loas's avatar
Ghislain Loas committed
498 499
      $operateur = $this->getParam('operateur_' . $name);
      if ($value = $this->getParam('rech_' . $name))
Patrick Barroca's avatar
Patrick Barroca committed
500 501
        $visitor->visitTextInput($name, $operateur, $type_recherche, $value,$this->getPertinence());
    }
502

503 504 505
    if ($editor = $this->getParam('rech_editeur'))
      $visitor->visitEditor($editor);

506
    if ($type_doc = $this->getTypeDoc())
Patrick Barroca's avatar
Patrick Barroca committed
507
      $visitor->visitTypeDoc($type_doc);
508

509
    if ($section = $this->getSection())
Patrick Barroca's avatar
Patrick Barroca committed
510
      $visitor->visitSection($section);
511

512
    if ($annexe = $this->getAnnexe())
Patrick Barroca's avatar
Patrick Barroca committed
513
      $visitor->visitAnnexe($annexe);
514

Patrick Barroca's avatar
Patrick Barroca committed
515
    $visitor->visitDigitalLib($this->getParam('digital_lib'));
516

Patrick Barroca's avatar
Patrick Barroca committed
517 518
    $annee_debut = $this->getAnneeDebut();
    $annee_fin = $this->getAnneeFin();
519

Patrick Barroca's avatar
Patrick Barroca committed
520 521
    if ($annee_debut)
      $visitor->visitAnneeDebutFin($annee_debut, $annee_fin);
522

Patrick Barroca's avatar
Patrick Barroca committed
523 524
    if ($nouveaute = $this->getNouveaute())
      $visitor->visitNouveaute($nouveaute);
525

526 527 528
    if($from = $this->getFrom())
      $visitor->visitFrom($from);

Patrick Barroca's avatar
Patrick Barroca committed
529 530
    if (1 < ($page = (int)$this->getPage()))
      $visitor->visitPage($page);
531

Patrick Barroca's avatar
Patrick Barroca committed
532 533
    if ($tri = $this->getTri())
      $visitor->visitTri($tri, $this->getListeTris()[$tri]);
534

Patrick Barroca's avatar
Patrick Barroca committed
535 536
    return $this;
  }
537

efalcy's avatar
efalcy committed
538

Patrick Barroca's avatar
Patrick Barroca committed
539 540
  public function acceptVisitor($visitor) {
    $visitor->visitNoExtension($this->getParam('no_extension'));
Laurent's avatar
Laurent committed
541 542
    $visitor->visitSearchUrl($this->getUrlCriteresWithFacettes());

Patrick Barroca's avatar
Patrick Barroca committed
543
    $this->visitByRechercheType($visitor);
544

545
    foreach($this->getFacettes() as $facette)
Patrick Barroca's avatar
Patrick Barroca committed
546
      $visitor->visitFacette($facette);
547

Patrick Barroca's avatar
Patrick Barroca committed
548 549 550
    foreach($this->getMultiFacets() as $facet)
      $visitor->visitMultiFacet($facet);

551 552
    $this->getAuthorities()->acceptVisitor($visitor);

Patrick Barroca's avatar
Patrick Barroca committed
553 554 555
    $filtres = $this->getFiltres();
    foreach($filtres as $filtre)
      $visitor->visitFiltre($filtre);
556

557 558 559 560 561
    if ($search = $this->_getBookmark())
      $visitor->visitBookmarkedSearch($search,
                                      $search->getVersion($this->getParam('bookmarked_version')));

    if ($limit = $this->getMaxSearchResults())
562 563
      $visitor->visitLimit($limit);

Patrick Barroca's avatar
Patrick Barroca committed
564 565
    return $this;
  }
566 567


Patrick Barroca's avatar
Patrick Barroca committed
568 569 570
  public function getNouveaute() {
    return $this->getParam('nouveaute', null);
  }
571 572


Patrick Barroca's avatar
Patrick Barroca committed
573 574 575
  public function getSection() {
    return $this->getParam('section', null);
  }
576 577


Patrick Barroca's avatar
Patrick Barroca committed
578
  public function getAnnexe() {
579 580 581 582 583
    return $this->validCodeOrNull($this->getParam('annexe', null));
  }


  public function validCodeOrNull($data) {
584
    return preg_match('/^[a-z\\s\\*A-Z0-9]+$/', $data)
585 586
      ? $data
      : null;
Patrick Barroca's avatar
Patrick Barroca committed
587
  }
588 589


590 591 592 593 594
  public function getFrom() {
    return $this->getParam('from', null);
  }


Patrick Barroca's avatar
Patrick Barroca committed
595 596 597
  public function getAnneeDebut() {
    return $this->getParam('annee_debut', $this->getParam('annee_fin', null));
  }
598 599


Patrick Barroca's avatar
Patrick Barroca committed
600 601 602
  public function getAnneeFin() {
    return $this->getParam('annee_fin', $this->getParam('annee_debut', null));
  }
603 604


Patrick Barroca's avatar
Patrick Barroca committed
605 606 607 608 609
  public function replaceEmptyRechercheByStar($params) {
    if (isset( $params['expressionRecherche']) &&  $params['expressionRecherche']==='')
      $params['expressionRecherche']='*';
    return $params;
  }
efalcy's avatar
efalcy committed
610 611


612 613
  protected function _prepareParamsForValidation($params) {
    return $this->replaceEmptyRechercheByStar($params);
Patrick Barroca's avatar
Patrick Barroca committed
614
  }
615 616


Patrick Barroca's avatar
Patrick Barroca committed
617 618 619
  public function getExpressionRecherche() {
    return $this->getParam('expressionRecherche');
  }
620 621


622 623 624 625 626
  public function hasExpressionRecherche() {
    return !in_array($this->getExpressionRecherche(), ['', '*']);
  }


Patrick Barroca's avatar
Patrick Barroca committed
627
  public function getUrlRetourRechercheInitiale(){
628 629 630 631 632 633 634 635
    if ($url = $this->_getNotSearchPagesBackUrl())
      return array_filter($url);

    if (array_key_exists('rubrique', $this->_params))
      return ['controller' => 'recherche',
              'action' => 'guidee' ];


Patrick Barroca's avatar
Patrick Barroca committed
636 637 638
    $action = 'avancee';
    if (array_key_exists('expressionRecherche',$this->_params) || array_key_exists('id_panier',$this->_params) || array_key_exists('id_catalogue',$this->_params))
      $action = 'saisie';
639

640 641 642 643 644 645 646 647 648 649
    $url = array_merge(array_intersect_key($this->_params, self::$criteres),
                       ['controller' => 'recherche',
                        'action' => $action ]);

    return array_filter($url);
  }


  protected function _getNotSearchPagesBackUrl() {
    if (array_key_exists('retour_panier',$this->_params))
650
      return ['controller' => 'panier',
Patrick Barroca's avatar
Patrick Barroca committed
651
              'action' => 'index',
652
              'id_panier' => $this->_params['retour_panier']] ;
653 654


655 656 657 658 659
    if (array_key_exists('retour_abonne',$this->_params))
      return ['controller' => 'abonne',
              'action' => $this->_params['retour_abonne'],
              'page' => $this->getParam('page') ,
              'page_size' => $this->getParam('page_size')];
660

661 662 663 664 665
    if (array_key_exists('retour_avis',$this->_params))
      return ['controller' => 'blog',
              'action' => 'viewcritiques',
              'id_module' => $this->_params['retour_avis'],
              'retour_avis' => $this->_params['retour_avis']] ;
666

667
    return null;
Patrick Barroca's avatar
Patrick Barroca committed
668
  }
669

670

671 672 673 674 675
  public function getUrlCriteres() {
    return $this->getUrlCriteresWithFacettes();
  }


Patrick Barroca's avatar
Patrick Barroca committed
676
  public function getUrlCriteresWithFacettes() {
677 678 679 680 681
    $intersect = $this->_params;

    if ($facettes = $this->getFacettesUrlEncoded())
      $intersect['facettes'] = $facettes;

Patrick Barroca's avatar
Patrick Barroca committed
682 683
    unset($intersect['facette']);
    unset($intersect['filtres']);
684

Patrick Barroca's avatar
Patrick Barroca committed
685 686 687 688
    if ($intersect)
      return array_merge(['controller' => 'recherche',
                          'action' => 'simple'],
                         $intersect);
689

690
    return ['controller' => 'recherche',
Patrick Barroca's avatar
Patrick Barroca committed
691 692
            'action' => 'simple'];
  }
693

694

Patrick Barroca's avatar
Patrick Barroca committed
695 696
  public function getCVSUrlCriteresWithFacettes() {
    $intersect=$this->_params;
697 698 699 700

    if ($facettes = $this->getFacettesUrlEncoded())
      $intersect['facettes'] = $facettes;

Patrick Barroca's avatar
Patrick Barroca committed
701 702 703 704 705 706
    unset($intersect['facette']);
    unset($intersect['filtres']);
    if ($intersect)
      return array_merge(['controller' => 'noticeajax',
                          'action' => 'cvs-search'],
                         $intersect);
707

Patrick Barroca's avatar
Patrick Barroca committed
708 709
    return ['controller'=>'noticeajax',
            'action' => 'cvs-search'];
710

Patrick Barroca's avatar
Patrick Barroca committed
711
  }
712

713

Patrick Barroca's avatar
Patrick Barroca committed
714 715 716 717
  public function getNewUrlCriteresRebond($code_rebond) {
    $url = $this->getUrlCriteresWithoutFacettes();
    $url['code_rebond'] = $code_rebond;
    return $url;
718

Patrick Barroca's avatar
Patrick Barroca committed
719
  }
720

721

Patrick Barroca's avatar
Patrick Barroca committed
722 723 724
  public function getNewUrlCriteresSerie($serie) {
    $url = $this->getUrlCriteresWithoutFacettes();
    $url['serie'] = $serie;
725
    $url['tri'] = self::SORT_NOVELTY_DESC;
Patrick Barroca's avatar
Patrick Barroca committed
726
    return $url;
727

Patrick Barroca's avatar
Patrick Barroca committed
728
  }
729 730


Patrick Barroca's avatar
Patrick Barroca committed
731 732 733 734
  public function getUrlCriteresWithoutFacettes() {
    $intersect=$this->_params;
    unset($intersect['facette']);
    unset($intersect['facettes']);
735
    unset($intersect['multifacets']);
Patrick Barroca's avatar
Patrick Barroca committed
736 737 738 739 740 741 742 743
    unset($intersect['filtres']);
    unset($intersect['page']);
    unset($intersect['serie']);
    unset($intersect['code_rebond']);
    return array_merge(['controller' => 'recherche',
                        'action' => 'simple'],
                       $intersect);
  }
744

745

Patrick Barroca's avatar
Patrick Barroca committed
746 747
  public function getUrlCriteresWithoutElement($element) {
    $intersect=$this->_params;
748 749 750 751

    if ($facettes = $this->getFacettesUrlEncoded())
      $intersect['facettes'] = $facettes;

Patrick Barroca's avatar
Patrick Barroca committed
752 753 754
    unset($intersect['facette']);
    unset($intersect[$element]);
    unset($intersect['filtres']);
755

Patrick Barroca's avatar
Patrick Barroca committed
756 757 758 759
    return array_merge(['controller' => 'recherche',
                        'action' => 'simple'],
                       $intersect);
  }
760

efalcy's avatar
efalcy committed
761

Patrick Barroca's avatar
Patrick Barroca committed
762 763
  public function getUrlCriteresWithoutTri() {
    $intersect=$this->_params;
764 765 766 767

    if ($facettes = $this->getFacettesUrlEncoded())
      $intersect['facettes'] = $facettes;

Patrick Barroca's avatar
Patrick Barroca committed
768 769 770
    unset($intersect['facette']);
    unset($intersect['filtres']);
    unset($intersect['tri']);
efalcy's avatar
efalcy committed
771

Patrick Barroca's avatar
Patrick Barroca committed
772 773 774 775
    return array_merge(['controller' => 'recherche',
                        'action' => 'simple'],
                       $intersect);
  }
efalcy's avatar
efalcy committed
776

777

Patrick Barroca's avatar
Patrick Barroca committed
778
  public function getUrlRetourListe() {
779 780 781
    if (!array_key_exists('code_rebond', $this->_params)
        && ($url = $this->_getNotSearchPagesBackUrl()))
      return array_filter($url);
782

Patrick Barroca's avatar
Patrick Barroca committed
783 784
    return $this->getUrlCriteresWithFacettes();
  }
785

786

787 788 789 790 791 792 793
  public function getUrlPager() {
    return array_merge($this->getUrlRetourListe(),
                       ['controller' => 'recherche',
                        'action' => 'simple']);
  }


Patrick Barroca's avatar
Patrick Barroca committed
794 795 796 797 798 799 800
  public function getUrlRemoveFacette($facette) {
    $facettes = array_diff($this->getFacettes(), [$facette->getCle()]);
    $url = $this->getUrlRetourListe();
    $url['facettes'] = implode('-', $facettes);
    $url['page'] = null;
    return array_filter($url);
  }
801 802


Patrick Barroca's avatar
Patrick Barroca committed
803 804 805
  public function getUrlRemoveMultiFacet($code) {
    $multifacets = [];
    foreach($this->getMultiFacets() as $facet) {
806 807 808
      if (!$facet)
        continue;

Patrick Barroca's avatar
Patrick Barroca committed
809 810 811 812
      $model = new Class_Notice_Facette($facet);
      if ($model->getRubrique()->getCode() != $code)
        $multifacets[] = $facet;
    }
813

Patrick Barroca's avatar
Patrick Barroca committed
814 815 816
    $url = $this->getUrlRetourListe();
    $url['multifacets'] = $multifacets ? implode('-', $multifacets) : null;
    $url['page'] = null;
817
    $url['genre'] = null;
Patrick Barroca's avatar
Patrick Barroca committed
818 819 820 821
    return array_filter($url);
  }


822 823 824 825 826 827 828 829 830 831 832
  public function getUrlRemoveAuthority($authority) {
    $url = $this->getUrlRetourListe();
    $url['authorities'] = $this->getAuthorities()
                               ->without($authority)
                               ->asParamString();
    $url['page'] = null;
    $url['genre'] = null;
    return array_filter($url);
  }


833
  public function multifacetsUpdate($update) {
834 835 836 837 838 839 840 841 842
    $url = $this->getUrlRetourListe();
    $multifacets = isset($url['multifacets']) ? explode('-', $url['multifacets']) : [];

    $add = $remove = [];
    foreach($update as $facet => $state)
      1 == $state ? ($add[] = $facet): ($remove[] = $facet);

    $multifacets = array_diff($multifacets, $remove);
    $multifacets = array_unique(array_merge($multifacets, $add));
843
    $this->setParam('multifacets', implode('-', $multifacets));
844

845 846 847 848 849 850
    return $this;
  }


  public function getUrlWithMultifacetsUpdate($update) {
    return array_filter($this->multifacetsUpdate($update)->getUrlRetourListe());
851 852 853
  }


854 855
  public function getUrlRemoveBookmarkVersion() {
    $url = $this->getUrlRetourListe();
856
    unset($url['bookmarked_search']);
857 858 859 860 861 862
    unset($url['bookmarked_version']);
    unset($url['page']);
    return array_filter($url);
  }


Patrick Barroca's avatar
Patrick Barroca committed
863 864 865
  public function getUrlNouvelleRecherche(){
    if (array_key_exists('expressionRecherche',$this->_params))
      return ['controller' => 'recherche',
866
              'action' => 'simple'];
867

Patrick Barroca's avatar
Patrick Barroca committed
868 869 870
    return ['controller' => 'recherche',
            'action' => 'avancee'];
  }
871

872

Patrick Barroca's avatar
Patrick Barroca committed
873 874 875
  function getCodeRebond() {
    return $this->getParam('code_rebond','');
  }
876

877

Patrick Barroca's avatar
Patrick Barroca committed
878 879 880
  function getSerie() {
    return $this->getParam('serie','');
  }
881 882


Patrick Barroca's avatar
Patrick Barroca committed
883 884 885
  function getFil() {
    return $this->getParam('fil','');
  }
efalcy's avatar
efalcy committed
886 887


Patrick Barroca's avatar
Patrick Barroca committed
888
  function getBibs() {
889 890 891 892 893
    $bibs = explode(',',$this->getParam('bib_select',''));
    if ($this->_profil
        && ($selection_bib = $this->_profil->getIdSite()))
      $bibs[] = $selection_bib;
    return array_filter($bibs);
Patrick Barroca's avatar
Patrick Barroca committed
894
  }
efalcy's avatar
efalcy committed
895

896

Patrick Barroca's avatar
Patrick Barroca committed
897 898 899
  function getRubrique() {
    return $this->getParam('rubrique','');
  }
efalcy's avatar
efalcy committed
900

901

902 903 904 905 906
  public function getIdModule() {
    return $this->getParam('id_module', 0);
  }


Patrick Barroca's avatar
Patrick Barroca committed
907 908 909
  function updateRubrique($rubrique) {
    return $this->_params['rubrique']=$rubrique;
  }
910 911


Patrick Barroca's avatar
Patrick Barroca committed
912 913 914 915
  public function clearProfil() {
    $this->_profil = null;
    return $this;
  }
916 917


918 919 920 921 922
  public function getProfil() {
    return $this->_profil;
  }


923
  public function getFormat() {
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    if ($layout = $this->getParam('liste_format'))
      return $layout;

    if ($user_layout = (new Class_User_Settings(Class_Users::getIdentity()))->getSearchLayout())
      return $user_layout;

    return (new Class_Profil_Preferences_SearchResult())->getDisplayFormat($this->_profil);
  }


  public function getPageSize() {
    if ($size = $this->getParam('page_size'))
      return $size;

    if ($user_page_size = (new Class_User_Settings(Class_Users::getIdentity()))->getSearchPageSize())
      return $user_page_size;

    return parent::getPageSize();
942 943 944 945 946 947
  }


  public function getAvailableFormats() {
    return Class_Systeme_ModulesAppli::getAvailableListeNoticeFormat();
  }
948 949 950


  public function isBookmarked() {
951
    return $this->getParam('bookmarked_search');
952 953 954 955 956 957 958
  }


  public function ownedBy($user) {
    if (!$user)
      return false;

959
    if (!$search = $this->_getBookmark())
960 961 962 963 964 965 966
      return false;

    return $search->belongsTo($user);
  }


  public function getBookmarkedLabel() {
967 968
    return ($search = $this->_getBookmark())
      ? $this->_('Ma recherche "%s"', $search->getLabel())
969 970
      : null;
  }
971 972 973 974 975


  protected function _getBookmark() {
    return Class_User_BookmarkedSearch::find($this->getParam('bookmarked_search'));
  }
976 977 978 979 980 981 982


  public function isEmpty() {
    return ! ($this->hasExpressionRecherche()
              || $this->getFacettes()
              || $this->getMultiFacets());
  }
983 984 985 986 987 988 989 990 991 992 993 994


  public function getStateError() {
    if ($this->hasEmptyDomain()
        && !$this->isSearchInBasket())
      return $this->_('Domaine non paramétré');

    if ($this->isEmptySelection())
      return $this->_('La sélection courante est vide');

    return parent::getStateError();
  }
995
}