oc-content/themes/veronika/functions.php
// RANDOM LATEST ITEMS ON HOME PAGE
RANDOM LATEST ITEMS ON HOME PAGE
RANDOM LATEST ITEMS ON HOME PAGE
function veronika_random_items($numItems = 10, $category = array(), $withPicture = false) {
  $max_items = osc_get_preference('maxLatestItems@home', 'osclass');
  if($max_items == '' or $max_items == 0) {
    $max_items = 24;
  }
  $numItems = $max_items;
  $withPicture = osc_get_preference('latest_picture', 'veronika_theme');
  $randomOrder = osc_get_preference('latest_random', 'veronika_theme');
  $premiums = osc_get_preference('latest_premium', 'veronika_theme');
  $category = osc_get_preference('latest_category', 'veronika_theme');
  $randSearch = Search::newInstance();
  $randSearch->dao->select(DB_TABLE_PREFIX.'t_item.* ');
  $randSearch->dao->from( DB_TABLE_PREFIX.'t_item use index (PRIMARY)' );
  $whe  = DB_TABLE_PREFIX.'t_item.b_active = 1 AND ';
  $whe .= DB_TABLE_PREFIX.'t_item.b_enabled = 1 AND ';
  $whe .= DB_TABLE_PREFIX.'t_item.b_spam = 0 AND ';
  if($premiums == 1) {
    $whe .= DB_TABLE_PREFIX.'t_item.b_premium = 1 AND ';
  }
  $whe .= '('.DB_TABLE_PREFIX.'t_item.b_premium = 1 || '.DB_TABLE_PREFIX.'t_item.dt_expiration >= \''. date('Y-m-d H:i:s').'\') ';
  if( $category <> '' and $category > 0 ) {
    $subcat_list = Category::newInstance()->findSubcategories( $category );
    $subcat_id = array();
    $subcat_id[] = $category;
    foreach( $subcat_list as $s) {
      $subcat_id[] = $s['pk_i_id'];
    }
    $listCategories = implode(', ', $subcat_id);
    $whe .= ' AND '.DB_TABLE_PREFIX.'t_item.fk_i_category_id IN ('.$listCategories.') ';
  }
  if($withPicture) {
    $prem_where = ' AND ' . $whe;
    $randSearch->dao->from( '(' . sprintf("select %st_item.pk_i_id FROM %st_item, %st_item_resource WHERE %st_item_resource.s_content_type LIKE '%%image%%' AND %st_item.pk_i_id = %st_item_resource.fk_i_item_id %s GROUP BY %st_item.pk_i_id ORDER BY %st_item.dt_pub_date DESC LIMIT %s", DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX, $prem_where, DB_TABLE_PREFIX, DB_TABLE_PREFIX, $numItems) . ') AS LIM' );
  } else {
    $prem_where = ' WHERE ' . $whe;
    $randSearch->dao->from( '(' . sprintf("select %st_item.pk_i_id FROM %st_item %s ORDER BY %st_item.dt_pub_date DESC LIMIT %s", DB_TABLE_PREFIX, DB_TABLE_PREFIX, $prem_where, DB_TABLE_PREFIX, $numItems) . ') AS LIM' );
  }
  $randSearch->dao->where(DB_TABLE_PREFIX.'t_item.pk_i_id = LIM.pk_i_id');
  $randSearch->dao->groupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
  if(!$randomOrder) {
    $randSearch->dao->orderBy(DB_TABLE_PREFIX.'t_item.dt_pub_date DESC');
  } else {
    $randSearch->dao->orderBy('RAND()');
  }
  $randSearch->dao->limit($numItems);
  $rs = $randSearch->dao->get();
  if($rs === false){
    return array();
  }
  if( $rs->numRows() == 0 ) {
    return array();
  }
  $items = $rs->result();
  return Item::newInstance()->extendData($items);
}
function veronika_random_items ( $ numItems = 10 , $ category = array ( ) , $ withPicture = false ) { $ max_items = osc_get_preference ( 'maxLatestItems@home' , 'osclass' ) ; if ( $ max_items == '' or $ max_items == 0 ) { $ max_items = 24 ; } $ numItems = $ max_items ; $ withPicture = osc_get_preference ( 'latest_picture' , 'veronika_theme' ) ; $ randomOrder = osc_get_preference ( 'latest_random' , 'veronika_theme' ) ; $ premiums = osc_get_preference ( 'latest_premium' , 'veronika_theme' ) ; $ category = osc_get_preference ( 'latest_category' , 'veronika_theme' ) ; $ randSearch = Search :: newInstance ( ) ; $ randSearch -> dao -> select ( DB_TABLE_PREFIX . 't_item.* ' ) ; $ randSearch -> dao -> from ( DB_TABLE_PREFIX . 't_item use index (PRIMARY)' ) ; $ whe = DB_TABLE_PREFIX . 't_item.b_active = 1 AND ' ; $ whe .= DB_TABLE_PREFIX . 't_item.b_enabled = 1 AND ' ; $ whe .= DB_TABLE_PREFIX . 't_item.b_spam = 0 AND ' ; if ( $ premiums == 1 ) { $ whe .= DB_TABLE_PREFIX . 't_item.b_premium = 1 AND ' ; } $ whe .= '(' . DB_TABLE_PREFIX . 't_item.b_premium = 1 || ' . DB_TABLE_PREFIX . 't_item.dt_expiration >= \'' . date ( 'Y-m-d H:i:s' ) . '\') ' ; if ( $ category <> '' and $ category > 0 ) { $ subcat_list = Category :: newInstance ( ) -> findSubcategories ( $ category ) ; $ subcat_id = array ( ) ; $ subcat_id [ ] = $ category ; foreach ( $ subcat_list as $ s ) { $ subcat_id [ ] = $ s [ 'pk_i_id' ] ; } $ listCategories = implode ( ', ' , $ subcat_id ) ; $ whe .= ' AND ' . DB_TABLE_PREFIX . 't_item.fk_i_category_id IN (' . $ listCategories . ') ' ; } if ( $ withPicture ) { $ prem_where = ' AND ' . $ whe ; $ randSearch -> dao -> from ( '(' . sprintf ( " select %st_item.pk_i_id FROM %st_item, %st_item_resource WHERE %st_item_resource.s_content_type LIKE '%%image%%' AND %st_item.pk_i_id = %st_item_resource.fk_i_item_id %s GROUP BY %st_item.pk_i_id ORDER BY %st_item.dt_pub_date DESC LIMIT %s " , DB_TABLE_PREFIX , DB_TABLE_PREFIX , DB_TABLE_PREFIX , DB_TABLE_PREFIX , DB_TABLE_PREFIX , DB_TABLE_PREFIX , $ prem_where , DB_TABLE_PREFIX , DB_TABLE_PREFIX , $ numItems ) . ') AS LIM' ) ; } else { $ prem_where = ' WHERE ' . $ whe ; $ randSearch -> dao -> from ( '(' . sprintf ( " select %st_item.pk_i_id FROM %st_item %s ORDER BY %st_item.dt_pub_date DESC LIMIT %s " , DB_TABLE_PREFIX , DB_TABLE_PREFIX , $ prem_where , DB_TABLE_PREFIX , $ numItems ) . ') AS LIM' ) ; } $ randSearch -> dao -> where ( DB_TABLE_PREFIX . 't_item.pk_i_id = LIM.pk_i_id' ) ; $ randSearch -> dao -> groupBy ( DB_TABLE_PREFIX . 't_item.pk_i_id' ) ; if ( ! $ randomOrder ) { $ randSearch -> dao -> orderBy ( DB_TABLE_PREFIX . 't_item.dt_pub_date DESC' ) ; } else { $ randSearch -> dao -> orderBy ( 'RAND()' ) ; } $ randSearch -> dao -> limit ( $ numItems ) ; $ rs = $ randSearch -> dao -> get ( ) ; if ( $ rs === false ) { return array ( ) ; } if ( $ rs -> numRows ( ) == 0 ) { return array ( ) ; } $ items = $ rs -> result ( ) ; return Item :: newInstance ( ) -> extendData ( $ items ) ; }
RANDOM LATEST ITEMS ON HOME PAGE
RANDOM LATEST ITEMS ON HOME PAGE
// where // group by & order & limit