Tuesday, 28 October 2014

How to exclude items by user id in osclass.

In this post i have explained you how to exclude the items in main page listing by user id in osclass.
By using this function you can exclude the items posted by a user in main page listing or in any page listing.

Usually we will use the function osc_query_item in osclass to exclude the items.But in this case its not usefull to use the osc_query_item function to exclude the item using user id.We can only do this by the use of the functions . Follow the procedures to use the function.

Add this at the bottom of the functions.php file inside your theme folder.

<?php function cust_query_item($params = null) {
 $mSearch = new Search(); if($params==null) { $params = array(); }
else if(is_string($params)){
      $keyvalue = explode("=", $params); $params = array($keyvalue[0] => $keyvalue[1]);
}
 foreach($params as $key => $value) {
   switch($key) {
      case 'id': $mSearch->addItemId($value); break;
      case 'author': $tmp = explode(",", $value);
         foreach($tmp as $t) { $mSearch->fromUser($t); }
         break;
     case 'category':
     case 'category_name': $tmp = explode(",", $value);
          foreach($tmp as $t) { $mSearch->addCategory($t); }
         break;
   case 'country':
   case 'country_name': $tmp = explode(",", $value);
         foreach($tmp as $t) { $mSearch->addCountry($t); } break;
   case 'region':
   case 'region_name': $tmp = explode(",", $value);
        foreach($tmp as $t) { $mSearch->addRegion($t); }
        break;
   case 'city':
   case 'city_name': $tmp = explode(",", $value);
        foreach($tmp as $t) { $mSearch->addCity($t); }
        break;
   case 'city_area':
   case 'city_area_name': $tmp = explode(",", $value);
        foreach($tmp as $t) { $mSearch->addCityArea($t); }
        break;
  case 'results_per_page':
       $mSearch->set_rpp($value);
       break;
  case 'premium': $mSearch->onlyPremium(($value==1?true:false));
      break;
 case 'page': $mSearch->page($value);
      break;
 case 'offset': $mSearch->limit($value);
      break;
 case 'excluded_author':
       $mSearch->addConditions(sprintf("%st_item.fk_i_user_id NOT IN (%s)", DB_TABLE_PREFIX, implode(',', $value))); break; case 'order': $mSearch->order($value, '');
        break;
 default: osc_run_hook('custom_query', $mSearch, $key, $value); break;
   }
 }
View::newInstance()->_exportVariableToView("customItems", $mSearch->doSearch());
 }?>

Now, this is the same code as core function osc_query_item, but I've added two more customizable cases, "excluded_author" (for our needs) and "order" so you might get a truly randomized list. 

 Then you call this new function like this:
 
<?php
cust_query_item(array("premium"=>1, "results_per_page" => 495, "order"=> 'RAND()', 'excluded_author' => array(XX,YY,ZZ)));
?>
You can change the result per page as your wish. And you can edit the function as your need.