Looks to me as if you could have done the same thing properly (not using session but PRG pattern) and with 3 lines of code
Am Montag, 29. Juni 2015 10:30:50 UTC+2 schrieb Eb ook:
-- using https://github.com/CakeDC/search
mark
Am Montag, 29. Juni 2015 10:30:50 UTC+2 schrieb Eb ook:
Hello all,
Result of function search is very large, It sometime is wrong.
Can you help me to fix it.
This is source this function.
public function find(){
$this->layout = 'listimage';
$this->set('title_for_layout', __('Search Results', true));
$this->Image->recursive = 0;
$search = $this->Session->read('Search');
if(!empty($_POST['data'])){
$this->Session->write('Search', $this->data);
$conditions = array();
if($this->Session->read('Search.Image.region_id')){
$q = $this->Session->read('Search.Image.region_id');
$conditions['Image.region_id'] = $q;
}
if($this->Session->read('Search.Image.artist_id')){
$q = $this->Session->read('Search.Image.artist_id');
$conditions['Image.artist_id'] = $q;
}
if($this->Session->read('Search.Image.movement_id')){
$q = $this->Session->read('Search.Image.movement_id');
$conditions['Image.movement_id'] = $q;
}
if($this->Session->read('Search.Image.museum_id')){
$q = $this->Session->read('Search.Image.museum_id');
$conditions['Image.museum_id'] = $q;
}
if($this->Session->read('Search.Image.museum_id')){
$q = $this->Session->read('Search.Image.museum_id');
$conditions['Image.museum_id'] = $q;
}
if($this->Session->read('Search.Theme.Theme.0')){
$q = $this->Session->read('Search.Theme.Theme.0');
$this->Image->bindModel(array('hasOne' => array('ImagesTheme')),false);
$conditions['ImagesTheme.theme_id']= $q;
}
if($this->Session->read('Search.Media.Media.0')){
$q = $this->Session->read('Search.Media.Media.0');
$this->Image->bindModel(array('hasOne' => array('ImagesMedium')),false);
$conditions['ImagesMedium.media_id']= $q;
}
if($this->Session->read('Search.Image.start_year')){
if($this->Session->read('Search.Image.end_year')){
$q = $this->Session->read('Search.Image.start_year');
$p = $this->Session->read('Search.Image.end_year');
if($this->Session->read('Search.Image.BC_start'))
$conditions['Image.start_year <='] = $q;
else
$conditions['Image.start_year >='] = $q;
if($this->Session->read('Search.Image.BC_end'))
$conditions['Image.end_year >='] = $p;
else
$conditions['Image.end_year <='] = $p;
}
else {
$q = $this->Session->read('Search.Image.start_year');
$conditions['Image.start_year ='] = $q;
}
}
if($this->Session->read('Search.Image.end_year')){
if($this->Session->read('Search.Image.start_year')){
$q = $this->Session->read('Search.Image.end_year');
$p = $this->Session->read('Search.Image.start_year');
if($this->Session->read('Search.Image.BC_start'))
$conditions['Image.start_year <='] = $p;
else
$conditions['Image.start_year >='] = $p;
if($this->Session->read('Search.Image.BC_end'))
$conditions['Image.end_year >='] = $q;
else
$conditions['Image.end_year <='] = $q;
}
else {
$q = $this->Session->read('Search.Image.end_year');
$conditions['Image.end_year ='] = $q;
}
}
$search = $this->Session->read('Search.Image.q');
$newconditions = array();
if($search) {
setlocale(LC_ALL, "en_US.utf8");
$search = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $search);
//$search = preg_replace('/[^a-zA-Z0-9]/', '_', $search);
//$search = '"'.$search.'"';
if(strpos($search, '"')!==false){
$search = str_replace('"','-',$search);
}elseif(strpos($search, '+')!==false){
$search = str_replace('+','-',$search);
$search = '"'.$search.'"';
}
else $search = $this->Image->searchstring($search);
$results = $this->Lucene->query($search);
$image_ids = array();
foreach($results as $result){
$image_ids[] = $result->image_id;
}
$newconditions['Image.id'] = $image_ids;
}
else if(!empty($conditions)) $newconditions = $conditions;
//pr($newconditions);
if(!empty($_POST['page'])) {
$this->Session->write('page',$_POST['page']);
}
$page = $this->Session->read('page');//echo $page;
$this->paginate = array('conditions'=>$newconditions,'order'=>array(' Image.id' => 'DESC'), 'limit' => $page);
$this->Session->write('SearchResult',$this->paginate( ));
$this->set('images', $this->paginate());
$this->set('page',$page);
}elseif(!empty($search)){
$conditions = array();
if($this->Session->read('Search.Image.region_id')){
$q = $this->Session->read('Search.Image.region_id');
$conditions['Image.region_id'] = $q;
}
if($this->Session->read('Search.Image.artist_id')){
$q = $this->Session->read('Search.Image.artist_id');
$conditions['Image.artist_id'] = $q;
}
if($this->Session->read('Search.Image.movement_id')){
$q = $this->Session->read('Search.Image.movement_id');
$conditions['Image.movement_id'] = $q;
}
if($this->Session->read('Search.Image.museum_id')){
$q = $this->Session->read('Search.Image.museum_id');
$conditions['Image.museum_id'] = $q;
}
if($this->Session->read('Search.Image.museum_id')){
$q = $this->Session->read('Search.Image.museum_id');
$conditions['Image.museum_id'] = $q;
}
if($this->Session->read('Search.Theme.Theme.0')){
$q = $this->Session->read('Search.Theme.Theme.0');
$this->Image->bindModel(array('hasOne' => array('ImagesTheme')),false);
$conditions['ImagesTheme.theme_id']= $q;
}
if($this->Session->read('Search.Media.Media.0')){
$q = $this->Session->read('Search.Media.Media.0');
$this->Image->bindModel(array('hasOne' => array('ImagesMedium')),false);
$conditions['ImagesMedium.media_id']= $q;
}
if($this->Session->read('Search.Image.start_year')){
if($this->Session->read('Search.Image.end_year')){
$q = $this->Session->read('Search.Image.start_year');
$p = $this->Session->read('Search.Image.end_year');
if($this->Session->read('Search.Image.BC_start'))
$conditions['Image.start_year <='] = $q;
else
$conditions['Image.start_year >='] = $q;
if($this->Session->read('Search.Image.BC_end'))
$conditions['Image.end_year >='] = $p;
else
$conditions['Image.end_year <='] = $p;
}
else {
$q = $this->Session->read('Search.Image.start_year');
$conditions['Image.start_year ='] = $q;
}
}
if($this->Session->read('Search.Image.end_year')){
if($this->Session->read('Search.Image.start_year')){
$q = $this->Session->read('Search.Image.end_year');
$p = $this->Session->read('Search.Image.start_year');
if($this->Session->read('Search.Image.BC_start'))
$conditions['Image.start_year <='] = $p;
else
$conditions['Image.start_year >='] = $p;
if($this->Session->read('Search.Image.BC_end'))
$conditions['Image.end_year >='] = $q;
else
$conditions['Image.end_year <='] = $q;
}
else {
$q = $this->Session->read('Search.Image.end_year');
$conditions['Image.end_year ='] = $q;
}
}
$search = $this->Session->read('Search.Image.q');
$newconditions = array();
if($search) {
setlocale(LC_ALL, "en_US.utf8");
$search = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $search);
//$search = preg_replace('/[^a-zA-Z0-9]/', '_', $search);
//$search = '"'.$search.'"';
if(strpos($search, '"')!==false){
$search = str_replace(' ','-',$search);
}elseif(strpos($search, '+')!==false){
$search = str_replace('+','-',$search);
$search = '"'.$search.'"';
}
else $search = $this->Image->searchstring($search);
$results = $this->Lucene->query($search);
$image_ids = array();
foreach($results as $result){
$image_ids[] = $result->image_id;
}
$newconditions['Image.id'] = $image_ids;
}
else if(!empty($conditions)) $newconditions = $conditions;
//pr($newconditions);
if(!empty($_POST['page'])) {
$this->Session->write('page',$_POST['page']);
}
$page = $this->Session->read('page');//echo $page;
$this->paginate = array('conditions'=>$newconditions,'order'=>array(' Image.id' => 'DESC'), 'limit' => $page);
$this->Session->write('SearchResult',$this->paginate( ));
$this->set('images', $this->paginate());
$this->set('page',$page);
}
else{
$this->Session->write('Search', array());
$this->redirect('/');
}
}
I think the result must not to find string which find per word. (I quess so)
Thanks all.
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment