I am faced with a dilemma.
To perform an advanced search model more precise than the one proposed by Joomla I created my own model.
Code: Select all
class Book extends DataModel
{
/**
* @return $this
*
* @throws RecordNotLoaded
*/
public function hit()
{
if(!$this->getId())
{
throw new RecordNotLoaded("Can't change the state of a not loaded DataModel");
}
if (!$this->hasField('hits'))
{
return $this;
}
$enabled = $this->getFieldAlias('hits');
$this->$enabled = $this->$enabled + 1;
$this->save();
return $this;
}
/**
* @param \JDatabaseQuery $query
* @param boolean $overrideLimits
*
* @return void
*/
public function onAfterBuildQuery($query, $overrideLimits = false)
{
/**
* TMP FIX
*/
$db = $this->getDbo();
$query->clear('order');
$order = $this->input->get('filter_order');
$order = $db->qn($order);
$dir = strtoupper($this->getState('filter_order_Dir', null, 'cmd'));
if (!in_array($dir, ['ASC', 'DESC']))
{
$dir = 'ASC';
$this->setState('filter_order_Dir', $dir);
}
//$query->group('`book`.`nglibrary_book_id`');
$query->order($order . ' ' . $dir);
/**
* /TMP FIX
*/
}
/**
* @param \JDatabaseQuery $query
* @param boolean $overrideLimits
*
* @return void
*/
public function onBeforeBuildQuery($query,$overrideLimits = false)
{
/**
* TMP FIX
*/
$query->clear('from')->from($this->getTableName() . ' AS book');
$this->setBehaviorParam('tableAlias', 'book');
$query->clear('group');
$outerGlue = $this->input->get('_outerGlue');
$query->group('book.nglibrary_book_id');
$query->clear('select');
//$query->from(array('#__nglibrary_books AS book','#__nglibrary_copies AS copie','#__nglibrary_books_authors AS link_author','#__nglibrary_authors AS author ','#__nglibrary_editors AS editor'));
$query->select('book.*,copie.*,author.*');
//Recherche dans la table __Copies
//Join _Copies
$query->leftjoin('#__nglibrary_copies AS copie ON book.nglibrary_book_id = copie.nglibrary_book_id');
//Join _Authors
$query->leftjoin('#__nglibrary_books_authors AS link_author ON book.nglibrary_book_id = link_author.nglibrary_book_id');
$query->leftjoin('#__nglibrary_authors AS author ON author.nglibrary_author_id = link_author.nglibrary_author_id');
//Join _Editors
$query->leftjoin('#__nglibrary_editors AS editor ON book.nglibrary_editor_id = editor.nglibrary_editor_id');
if ($this->input->get('_resetState') == 1)
{
// Save the state we need to keep
$limit = $this->getState('limit');
// Reset model and user state
$this->clearState()->reset();
\JFactory::getApplication()->setUserState(substr($this->getHash(), 0, -1), null);
// Set back the saved state
$this->input->set('limit', $limit);
$this->setState('limit', $limit);
}
//Process pour la recherche via le module
if($this->input->get('search') !=''){
$search = $this->input->get('search');
$books = $this->getState('books');
$copies = $this->getState('copies');
if($this->input->get('title') =='Y')
{ $this->input->set('title',NULL);
//$this->input->set('title',$this->input->getString('search'));
$this->input->set('books',
array_merge(['title' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_title', 'OR');
}
if($this->input->get('author_adv') =='Y')
{ $this->input->set('author_adv',NULL);
//$this->input->set('author_adv',$this->input->getString('search'));
$this->input->set('books',
array_merge(['author_adv' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_author', 'OR');
}
if($this->input->get('serie') =='Y')
{ $this->input->set('serie',NULL);
//$this->input->set('serie',$this->input->getString('search'));
$this->input->set('books',
array_merge(['serie' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_serie', 'OR');
}
if($this->input->get('collection') =='Y')
{ $this->input->set('collection',NULL);
//$this->input->set('collection',$this->input->getString('search'));
$this->input->set('books',
array_merge(['collection' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_collection', 'OR');
}
if($this->input->get('resume') =='Y')
{ $this->input->set('resume',NULL);
//$this->input->set('resume',$this->input->getString('search'));
$this->input->set('books',
array_merge(['resume' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_resume', 'OR');
}
if($this->input->get('isbn') =='Y')
{ $this->input->set('isbn',NULL);
//$this->input->set('isbn',$this->input->getString('search'));
$this->input->set('books',
array_merge(['isbn' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_isbn', 'OR');
}
if($this->input->get('editor') =='Y')
{ $this->input->set('editor',NULL);
//$this->input->set('editor',$this->input->getString('search'));
$this->input->set('books',
array_merge(['editor' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_editor', 'OR');
}
if($this->input->get('interest') =='Y')
{ $this->input->set('interest',NULL);
//$this->input->set('interest',$this->input->getString('search'));
$this->input->set('books',
array_merge(['interest' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_interest', 'OR');
}
if($this->input->get('support') =='Y')
{ $this->input->set('support',NULL);
//$this->input->set('support',$this->input->getString('search'));
$this->input->set('books',
array_merge(['support' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_support', 'OR');
}
if($this->input->get('publics') =='Y')
{ $this->input->set('grp_public',NULL);
//$this->input->set('public',$this->input->getString('search'));
$this->input->set('books',
array_merge(['grp_public' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_grppublic', 'OR');
}
if($this->input->get('number') =='Y')
{ $this->input->set('number',NULL);
//$this->input->set('support',$this->input->getString('search'));
$this->input->set('copies',
array_merge(['number' => $this->input->getString('search')],$this->input->get('copies', [], 'array') )
);
$this->input->set('operateur_number', 'OR');
}
if($this->input->get('grp_genre') =='Y')
{ $this->input->set('grp_genre',NULL);
//$this->input->set('support',$this->input->getString('search'));
$this->input->set('books',
array_merge(['grp_genre' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_genre', 'OR');
}
if($this->input->get('genre') =='Y')
{ $this->input->set('genre',NULL);
//$this->input->set('support',$this->input->getString('search'));
$this->input->set('books',
array_merge(['genre' => $this->input->getString('search')],$this->input->get('books', [], 'array') )
);
$this->input->set('operateur_genre', 'OR');
}
//OPTIONNAL
if($this->input->get('origin') =='Y')
{ $this->input->set('origin',NULL);
//$this->input->set('support',$this->input->getString('search'));
$this->input->set('copies',
array_merge(['origin' => $this->input->getString('search')],$this->input->get('copies', [], 'array') )
);
$this->input->set('operateur_support', 'AND');
}
if($this->input->get('library') =='Y')
{ $this->input->set('library',NULL);
//$this->input->set('support',$this->input->getString('search'));
$this->input->set('copies',
array_merge(['library' => $this->input->getString('search')],$this->input->get('copies', [], 'array') )
);
$this->input->set('operateur_library', 'AND');
}
}
//Process pour la recherche avancé
if($this->input->get('button_menu') !=''||$this->input->get('advancedsearch') !=''|| $this->input->get('search') !=''){
if ($this->input->get('copiesStatusId'))
{
$this->input->set('copies',
array_merge(['statusId' => $this->input->get('copiesStatusId')],$this->input->get('copies', [], 'array') )
);
}
if ($this->input->get('copiesLibraryId'))
{
$this->input->set('copies',
array_merge(['libraryId' => $this->input->get('copiesLibraryId')],$this->input->get('copies', [], 'array') )
);
}
if ($this->input->get('origin'))
{
$this->input->set('copies',
array_merge(['origin' => $this->input->getString('origin')],$this->input->get('copies', [], 'array') )
);
}
if ($this->input->get('location'))
{
$this->input->set('copies',
array_merge(['location' => $this->input->getString('location')],$this->input->get('copies', [], 'array') )
);
}
if (($copies = $this->getState('copies'))
&& array_filter($copies))
{ $sql = '';
if (!empty($copies['number']))
$sql .= ('' . $this->input->get('operateur_exemplaire').'(`copie`.`number` = "' . $copies['number'] . '")');
if (!empty($copies['cote']))
$sql .= ('' . $this->input->get('operateur_cote').'(`copie`.`cote` LIKE "' . $copies['cote'] . '%")');
if (!empty($copies['origin']))
$sql .= ('' . $this->input->get('operateur_origin').'(`copie`.`origin` LIKE "' . $copies['origin'] . '")');
if (!empty($copies['location']))
$sql .= ('' . $this->input->get('operateur_location').'(`copie`.`location` LIKE "' . $copies['location'] . '%")');
if (!empty($copies['statusId']))
$sql .= ('' . $this->input->get('operateur_bookstatus_id').'(`copie`.`nglibrary_bookstatus_id` = "' . $copies['statusId'] . '")');
if (!empty($copies['libraryId']))
$sql .= ('' . $this->input->get('operateur_library_id').'(`copie`.`nglibrary_library_id` = "' . $copies['libraryId'] . '")');
if (!empty($copies['days']))
{$dateJ = strftime('%Y-%m-%d');
$year = substr($dateJ, 0, -6);
$month = substr($dateJ, -5, -3);
$day = substr($dateJ, -2);
// récupère la date du jour
$date_string = mktime(0,0,0,$month,$day,$year);
// Supprime les jours
$timestamp = $date_string - ($copies['days'] * 86400);
$nouvelle_date = date("Y-m-d", $timestamp);
$sql .= ('' . $this->input->get('operateur_newunder').'(`copie`.`created_on` >= \''. $nouvelle_date .'\')');
}
if(substr($sql,-strlen($sql),2) == 'OR'){
$sql = substr($sql,2);
}else if(substr($sql,-strlen($sql),3) == 'AND'){
$sql = substr($sql,3);
}
}
//Recherche dans la table __Books
// est-ce encore utile ?
// if ($this->input->get('title'))
// {
// $this->input->set('books',
// array_merge(['title' => $this->input->getString('title')],$this->input->get('books', [], 'array') )
// );
// }
// if ($this->input->get('author_adv'))
// {
// $this->input->set('books',
// array_merge(['author_adv' => $this->input->getString('author_adv')],$this->input->get('books', [], 'array') )
// );
// }
// if ($this->input->get('serie'))
// {
// $this->input->set('books',
// array_merge(['serie' => $this->input->getString('serie')],$this->input->get('books', [], 'array') )
// );
// }
if (($books = $this->getState('books'))
&& array_filter($books))
{ $sql2 ='';
if (!empty($books['title']))
//j'enleve les prefixes du titre pour la recherche et je cherche dans les sous-titres
{ $title = preg_replace ("#^(le |la |les |LE |LA |LES )#","",$books['title']);
$sql2 .= ('((`book`.`title` LIKE "%' . $title . '%") OR ');
$sql2 .= ('(`book`.`subtitle` LIKE "%' . $title . '%") OR ');
$sql2 .= ('(`book`.`coll5` LIKE "%' . $title . '%"))');
}
if (!empty($books['resume']))
$sql2 .= ('' . $this->input->get('operateur_resume').'(`book`.`resume` LIKE "%' . $books['resume'] . '%")');
if (!empty($books['isbn']))
$sql2 .= ('' . $this->input->get('operateur_isbn').'(`book`.`isbn` = "' . $books['isbn'] . '")');
if (!empty($books['author_adv']))
{ $sql2 .= ('' . $this->input->get('operateur_author').'((`book`.`author_p` LIKE "%' . $books['author_adv'] . '%")');
//Je cherche aussi dans la table authors
$sql2 .= (' OR (`author`.`title` LIKE "%' . $books['author_adv'] . '%"))');
}
if (!empty($books['coll7']))
$sql2 .= ('' . $this->input->get('operateur_interest').'(`book`.`coll7` = "' . $books['coll7'] . '")');
if (!empty($books['coll6']))
$sql2 .= ('' . $this->input->get('operateur_topic').'(`book`.`coll6` = "' . $books['coll6'] . '")');
if (!empty($books['editor']))
$sql2 .= ('' . $this->input->get('operateur_editor').'(`editor`.`title` LIKE "%' . $books['editor'] . '%")');
if (!empty($books['collection']))
$sql2 .= ('' . $this->input->get('operateur_collection').'(`book`.`collection` LIKE "%' . $books['collection'] . '%")');
if (!empty($books['serie']))
$sql2 .= ('' . $this->input->get('operateur_serie').'(`book`.`serie` LIKE "%' . $books['serie'] . '%")');
if (!empty($books['year-from']))
$sql2 .= ('' . $this->input->get('operateur_inputyear').'(`book`.`year` BETWEEN '.$books['year-from'].' AND '.$books['year-to'].')');
if (!empty($books['support']))
$sql2 .= ('' . $this->input->get('operateur_support').'(`book`.`support` = "' . $books['support'] . '")');
if (!empty($books['grp_public']))
$sql2 .= ('' . $this->input->get('operateur_grppublic').'(`book`.`grp_public` = "' . $books['grp_public'] . '")');
if (!empty($books['public']))
$sql2 .= ('' . $this->input->get('operateur_section').'(`book`.`public` = "' . $books['public'] . '")');
if (!empty($books['genre']))
$sql2 .= ('' . $this->input->get('operateur_genre').'(`book`.`genre` = "' . $books['genre'] . '")');
if (!empty($books['statusBib']))
$sql2 .= ('' . $this->input->get('operateur_statutbib').'(`book`.`status_bib` = "' . $books['statusBib'] . '")');
if (!empty($books['hideperiodique']) == 1)
{$sql2 .= ('AND ((`book`.`periodique` = 1 ) OR (`book`.`periodique` IS NULL ))');
}
}
if(isset($sql) == false && $sql2 !=''){
$rsql =$sql2;
}else if(isset($sql2) == false && $sql !=''){
//$sql2 = '(`book`.`nglibrary_book_id` IS NOT NULL)';
$rsql=$sql;
}else{
$rsql= $sql2.' AND '.$sql;
}
if(substr($rsql,-strlen($rsql),2) == 'OR'){
$rsql = substr($rsql,2);
}else if(substr($rsql,-strlen($rsql),3) == 'AND'){
$rsql = substr($rsql,3);
}
$query->where($rsql);
return $query;
}
}
}
This one works fine for all that is research.
The problem is that on my results window the filters displayed by Joomla for sorting, ordering and number of rows re-initializes my search result systematically to display everything.
Can anyone tell me how this is done ...
Thank you for your help