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.

Monday, 27 October 2014

How to insert a script file in osclass admin.

The enqueue functions are used for loading your javascript and css files in osclass admin.The reason for using enqueue functions is becuse many plugins are using javascript frameworks such as JQuery, JQuery-ui, and fancybox just to name a few. There is a chance that these javascript files could be loaded multiple times and that is why the enqueue functions are used. The enqueue functions help to reduce the chances of the same file getting loaded more than once in osclass. 

Here, I have explained with examples below about how to insert a script file in osclass admin when we creating a plugin.
Registering a javascript file
osc_register_script('jqueryPopup', osc_base_url() . 'oc-content/plugins/popup_for_osclass/js/jCarouselPopup.js', 'jquery');
Enqueueing the script
osc_enqueue_script('jqueryPopup');
Enqueueing a style
osc_enqueue_style('popuplCss', osc_base_url() . 'oc-content/plugins/popup_for_osclass/css/popup.css');
You may have noticed that i did not register the style and that is because it is not required and there is no function to do so.Real life example,for inserting a script file in osclass admin.Ok now let us move to a real life example. To use these functions you have to create a function for this example I am going to use examle_load_scripts() please use a unique function name in your plugin if you are planning on releasing it otherwise we could end up with conflicting function names.

function examle_load_scripts() {
     osc_register_script('jqueryPopup', osc_base_url() . 'oc-content/plugins/popup_for_osclass/js/jCarouselPopup.js', 'jquery');
     osc_enqueue_script('jqueryPopup');
     osc_enqueue_style('carouselCss', osc_base_url() . 'oc-content/plugins/popup_for_osclass/css/popup.css');
 }
Once you add that code you will then need to add the following hook to your index.php file of your plugin. 

osc_add_hook('init', 'examle_load_scripts');
To load the script on the admin side you would use this hook

osc_add_hook('init_admin', 'examle_load_scripts');
This is the way of inserting a script or a css file into osclass admin.