Monday, 24 February 2014

Custom search and pagination using database item list in Concrete5 0

I faced this issue when I'm developing custom search page. I used my single page for search page. I'm searching values from my database. In concrete5 they used Request method in form to get form values to controller. But I used Post method in my form.

I followed article of Remo to create a database item list model file. Really very nice tutorial for beginners. But I could not find the search option in that article.

I tried by my self and all works fine. But I got issue in pagination. When I move to the next page, the previous, the page was reloading. So, the search values are gone in my form. So, with empty search variables, I showed error (Invalid argument supplied for foreach).

When I looked into StickySearchRequest, they used server REQUEST method for that function. But, I used server POST So, it will not going to work for me.

So, I found solution by my self.

In my controller's view function I placed my filter query and set the value from controller to view. This will only will execute if the session variable is set

 
public function view() {
    Loader::model('ListforDetails');
    $List = new ListforDetails();
    if (isset($_SESSION['var1'])) {
        $data['var1'] = $_SESSION['var1'];
        $data['var2'] = $_SESSION['var2'];
        $data['var3'] = $_SESSION['var3'];
 
        $List - > filter('field1', $data['var1'], '=');
        $List - > filter('field2', $data['var2'], '=');
        $List - > filter('field3', $data['var3'], '=');
    }
    $List - > setItemsPerPage(10);
    $this - > set('forSummary', $List);
    $currentPage = Page::getCurrentPage();
    $this - > set('LeaveList', $LeaveList - > getPage());
    $this - > set('LeavePagination', $LeaveList - > displayPagingV2(Loader::helper('navigation') - > getLinkToCollection($currentPage), true));
}

In my search function I get the search values from single page form and I manually add those values in session. Then simply call view function.

 
public function search()
{
      $data['var1']=$this->post('val1');
      $data['var2']=$this->post('val2');
      $data['var3']=$this->post('val3');
      foreach ($data as $key=>$sessData)
      {
      $_SESSION[$key]=$sessData;
      }
      echo $this->view();
}

I placed "back" button in my form. If the user click on the back button, I unset the session array values like,

 
public function back()
   {
      unset($_SESSION['var1']);
      unset($_SESSION['var2']);
      unset($_SESSION['var3']);
      echo $this->redirect('prev_url');
   }

Finally I it's working fine...

Here is my Concrete5 Forum Post

0 comments:

Post a Comment