2009-09-01 20:57:38 +00:00
< ? php
/**
* @ Author : Arslan Hassan < arslan @ clip - bucket . com >
* This class is used to create
* and manage categories
* its an abstract class
2009-11-04 10:27:40 +00:00
* it will be used in custom plugins or built - in sections
* sections like videos , groups , channels etc use this category system
2009-09-01 20:57:38 +00:00
*
* this abstract class has some rules
* each section ' s category column should be named as " category "
* each section ' s category table must have same columns as video_categories
*/
abstract class CBCategory
{
var $cat_tbl = '' ; //Name of category Table
var $section_tbl = '' ; //Name of table that related to $cat_tbl
2010-08-19 09:56:41 +00:00
var $use_sub_cats = FALSE ; // Set to true if you using Sub-Cateogires
2009-09-01 20:57:38 +00:00
var $cat_thumb_height = '125' ;
var $cat_thumb_width = '125' ;
var $default_thumb = 'no_thumb.jpg' ;
/**
* Function used to check weather category exists or not
*/
function category_exists ( $cid )
{
global $db ;
return $this -> get_category ( $cid );
}
/**
* Function used to get category details
*/
function get_category ( $cid )
{
global $db ;
2010-02-06 08:18:36 +00:00
$results = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " category_id=' $cid ' " );
2009-09-01 20:57:38 +00:00
if ( $db -> num_rows > 0 )
{
return $results [ 0 ];
} else {
return false ;
}
}
/**
* Function used to get category by name
*/
function get_cat_by_name ( $name )
{
global $db ;
2010-02-06 08:18:36 +00:00
$results = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " category_name=' $name ' " );
2009-09-01 20:57:38 +00:00
if ( $db -> num_rows > 0 )
{
return $results [ 0 ];
} else {
return false ;
}
}
/**
* Function used to add new category
*/
function add_category ( $array )
{
global $db ;
2010-07-22 11:20:36 +00:00
$name = ( $array [ 'name' ]);
$desc = ( $array [ 'desc' ]);
2009-09-01 20:57:38 +00:00
$default = mysql_clean ( $array [ 'default' ]);
2010-08-19 09:56:41 +00:00
$flds = array ( " category_name " , " category_desc " , " date_added " );
$values = array ( $name , $desc , now ());
if ( ! empty ( $this -> use_sub_cats ))
{
$parent_id = mysql_clean ( $array [ 'parent_cat' ]);
$flds [] = " parent_id " ;
$values [] = $parent_id ;
}
2009-09-01 20:57:38 +00:00
if ( $this -> get_cat_by_name ( $name ))
{
e ( lang ( " add_cat_erro " ));
} elseif ( empty ( $name ))
{
e ( lang ( " add_cat_no_name_err " ));
} else {
2010-08-19 09:56:41 +00:00
$cid = $db -> insert ( tbl ( $this -> cat_tbl ), $flds , $values );
2009-09-01 20:57:38 +00:00
$cid = $db -> insert_id ();
if ( $default == 'yes' || ! $this -> get_default_category ())
$this -> make_default_category ( $cid );
2010-03-30 07:46:02 +00:00
e ( lang ( " cat_add_msg " ), 'm' );
2009-09-01 20:57:38 +00:00
//Uploading thumb
if ( ! empty ( $_FILES [ 'cat_thumb' ][ 'tmp_name' ]))
$this -> add_category_thumb ( $cid , $_FILES [ 'cat_thumb' ]);
}
}
/**
* Function used to make category as default
*/
function make_default_category ( $cid )
{
global $db ;
if ( $this -> category_exists ( $cid ))
{
2010-02-06 08:18:36 +00:00
$db -> update ( tbl ( $this -> cat_tbl ), array ( " isdefault " ), array ( " no " ), " isdefault='yes' " );
$db -> update ( tbl ( $this -> cat_tbl ), array ( " isdefault " ), array ( " yes " ), " category_id=' $cid ' " );
2010-03-30 07:46:02 +00:00
e ( lang ( " cat_set_default_ok " ), 'm' );
2009-09-01 20:57:38 +00:00
} else
e ( lang ( " cat_exist_error " ));
}
/**
* Function used to get list of categories
*/
function get_categories ()
{
global $db ;
2010-02-06 08:18:36 +00:00
$select = $db -> select ( tbl ( $this -> cat_tbl ), " * " , NULL , NULL , " category_order ASC " );
2009-09-01 20:57:38 +00:00
return $select ;
}
2010-08-06 11:42:41 +00:00
/**
* Function used to list of categories
*/
function cb_list_categories ( $type , $with_all = false )
{
global $db ;
if ( $type == 'video' || $type == 'vid' || $type == 'v' )
$cond = " parent_id = 0 " ;
else
$cond = NULL ;
//Getting List of categories
$cats = $db -> select ( tbl ( $this -> cat_tbl ), " * " , $cond , NULL , " category_order ASC " );
if ( $with_all )
array_unshift ( $cats , array ( " category_id " => " all " , " category_name " => " All " ));
$html = '' ;
for ( $i = 0 ; $i < count ( $cats ); $i ++ )
{
if ( $_GET [ 'cat' ] == $cats [ $i ][ 'category_id' ] || ( empty ( $_GET [ 'cat' ]) && $cats [ $i ][ 'category_id' ] == 'all' ))
$selected = " selected " ;
else
$selected = " " ;
$html .= " <li class=' " . $selected . " '> " ;
$html .= " <a href=' " . category_link ( $cats [ $i ], $type ) . " ' title=' " . $cats [ $i ][ 'category_name' ] . " '> " . $cats [ $i ][ 'category_name' ] . " </a> " ;
if ( $this -> is_parent ( $cats [ $i ][ 'category_id' ]))
{
$html .= $this -> cb_list_subs ( $cats [ $i ][ 'category_id' ], $type );
}
$html .= " </li> " ;
}
return $html ;
}
function cb_list_subs ( $cid , $type )
{
global $db ;
$html = " " ;
$query = mysql_query ( " SELECT * FROM " . tbl ( $this -> cat_tbl ) . " WHERE parent_id = $cid " );
if ( ! empty ( $query ))
{
$html .= " <ul id=' " . $cid . " _subs' class='sub_categories'> " ;
while ( $result = mysql_fetch_array ( $query ))
{
if ( $_GET [ 'cat' ] == $result [ 'category_id' ])
$selected = " selected " ;
else
$selected = " " ;
$html .= " <li class=' " . $selected . " '> " ;
$html .= " <a href=' " . category_link ( $result , $type ) . " ' title=' " . $result [ 'category_name' ] . " '> " . $result [ 'category_name' ] . " </a> " ;
if ( $this -> is_parent ( $result [ 'category_id' ]))
{
$html .= $this -> cb_list_subs ( $result [ 'category_id' ], $type );
}
$html .= " </li> " ;
}
$html .= " </ul> " ;
}
return $html ;
}
2009-09-01 20:57:38 +00:00
/**
* Function used to count total number of categoies
*/
function total_categories ()
{
global $db ;
2010-02-06 08:18:36 +00:00
return $db -> count ( tbl ( $this -> cat_tbl ), " * " );
2009-09-01 20:57:38 +00:00
}
/**
* Function used to delete category
*/
function delete_category ( $cid )
{
global $db ;
$cat_details = $this -> category_exists ( $cid );
if ( ! $cat_details )
e ( lang ( " cat_exist_error " ));
2010-08-06 11:42:41 +00:00
2009-09-01 20:57:38 +00:00
//CHecking if category is default or not
elseif ( $cat_details [ 'isdefault' ] == 'yes' )
e ( lang ( " cat_default_err " ));
else {
2010-08-06 11:42:41 +00:00
$pcat = $this -> has_parent ( $cid , true );
//Checking if category is both parent and child
if ( $pcat && $this -> is_parent ( $cid ))
{
$to = $pcat [ 0 ][ 'category_id' ];
$has_child = TRUE ;
}
elseif ( $pcat && ! $this -> is_parent ( $cid )) //Checking if category is only child
{
$to = $pcat [ 0 ][ 'category_id' ];
$has_child = TRUE ;
}
elseif ( ! $pcat && $this -> is_parent ( $cid )) //Checking if category is only parent
{
$to = NULL ;
$has_child = NULL ;
$db -> update ( tbl ( $this -> cat_tbl ), array ( 'parent_id' ), array ( '0' ), " parent_id = $cid " );
}
//Moving all contents to parent OR default category
$this -> change_category ( $cid , $to , $has_child );
2009-09-01 20:57:38 +00:00
//Removing Category
2010-02-06 08:18:36 +00:00
$db -> execute ( " DELETE FROM " . tbl ( $this -> cat_tbl ) . " WHERE category_id=' $cid ' " );
2010-03-30 07:46:02 +00:00
e ( lang ( " class_cat_del_msg " ), 'm' );
2009-09-01 20:57:38 +00:00
}
}
/**
* Functon used to get dafault categry
*/
function get_default_category ()
{
global $db ;
2010-02-06 08:18:36 +00:00
$results = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " isdefault='yes' " );
2009-09-01 20:57:38 +00:00
if ( $db -> num_rows > 0 )
return $results [ 0 ];
else
return false ;
}
/**
* Function used to get default category ID
*/
function get_default_cid ()
{
$default = $this -> get_default_category ();
return $default [ 'category_id' ];
}
/**
* Function used to move contents from one section to other
*/
2010-08-06 11:42:41 +00:00
function change_category ( $from , $to = NULL , $has_child = NULL , $check_multiple = false )
2009-09-01 20:57:38 +00:00
{
global $db ;
2010-08-06 11:42:41 +00:00
2009-09-01 20:57:38 +00:00
if ( ! $this -> category_exists ( $to ))
$to = $this -> get_default_cid ();
2010-08-06 11:42:41 +00:00
if ( $has_child ) {
$db -> update ( tbl ( $this -> cat_tbl ), array ( 'parent_id' ), array ( $to ), " parent_id = $from " );
}
2010-02-06 08:18:36 +00:00
$db -> execute ( " UPDATE " . tbl ( $this -> section_tbl ) . " SET category = replace(category,'# " . $from . " #','# " . $to . " #') WHERE category LIKE '%# " . $from . " #%' " );
2010-08-06 11:42:41 +00:00
2010-02-06 08:18:36 +00:00
$db -> execute ( " UPDATE " . tbl ( $this -> section_tbl ) . " SET category = replace(category,'# " . $to . " # # " . $to . " #','# " . $to . " #') WHERE category LIKE '%# " . $to . " #%' " );
2009-09-01 20:57:38 +00:00
}
/**
* Function used to edit category
* submit values and it will update category
*/
function update_category ( $array )
{
global $db ;
2010-07-22 11:20:36 +00:00
$name = ( $array [ 'name' ]);
$desc = ( $array [ 'desc' ]);
2009-09-01 20:57:38 +00:00
$default = mysql_clean ( $array [ 'default' ]);
2010-08-06 11:42:41 +00:00
$pcat = mysql_clean ( $array [ 'parent_cat' ]);
2009-09-01 20:57:38 +00:00
2010-08-19 09:56:41 +00:00
$flds = array ( " category_name " , " category_desc " );
$values = array ( $name , $desc );
2009-09-01 20:57:38 +00:00
$cur_name = mysql_clean ( $array [ 'cur_name' ]);
$cid = mysql_clean ( $array [ 'cid' ]);
2010-08-19 09:56:41 +00:00
if ( ! empty ( $this -> use_sub_cats ))
{
$flds [] = " parent_id " ;
$vlaues [] = $pcat ;
}
2009-09-01 20:57:38 +00:00
if ( $this -> get_cat_by_name ( $name ) && $cur_name != $name )
{
e ( lang ( " add_cat_erro " ));
2010-08-06 11:42:41 +00:00
} elseif ( empty ( $name )){
2009-09-01 20:57:38 +00:00
e ( lang ( " add_cat_no_name_err " ));
2010-08-06 11:42:41 +00:00
} elseif ( $pcat == $cid ){
e ( lang ( " You can not make category parent of itself " ));
2009-09-01 20:57:38 +00:00
} else {
2010-08-19 09:56:41 +00:00
$db -> update ( tbl ( $this -> cat_tbl ), $flds , $values , " category_id=' $cid ' " );
2009-09-01 20:57:38 +00:00
if ( $default == 'yes' || ! $this -> get_default_category ())
$this -> make_default_category ( $cid );
2010-03-30 07:46:02 +00:00
e ( lang ( " cat_update_msg " ), 'm' );
2009-09-01 20:57:38 +00:00
//Uploading thumb
if ( ! empty ( $_FILES [ 'cat_thumb' ][ 'tmp_name' ]))
$this -> add_category_thumb ( $cid , $_FILES [ 'cat_thumb' ]);
}
}
/**
* Function used to add category thumbnail
* @ param $Cid and Array
*/
function add_category_thumb ( $cid , $file )
{
global $imgObj ;
if ( $this -> category_exists ( $cid ))
{
//Checking for category thumbs direcotry
if ( isset ( $this -> thumb_dir ))
$dir = $this -> thumb_dir ;
else
$dir = $this -> section_tbl ;
//Checking File Extension
$ext = strtolower ( getext ( $file [ 'name' ]));
if ( $ext == 'jpg' || $ext == 'png' || $ext == 'gif' )
{
$dir_path = CAT_THUMB_DIR . '/' . $dir ;
if ( ! is_dir ( $dir_path ))
@ mkdir ( $dir_path , 0777 );
if ( is_dir ( $dir_path ))
{
$path = $dir_path . '/' . $cid . '.' . $ext ;
//Removing File if already exists
if ( file_exists ( $path ))
unlink ( $path );
move_uploaded_file ( $file [ 'tmp_name' ], $path );
//Now checking if file is really an image
if ( !@ $imgObj -> ValidateImage ( $path , $ext ))
e ( lang ( " pic_upload_vali_err " ));
else
{
$imgObj -> CreateThumb ( $path , $path , $this -> cat_thumb_width , $ext , $this -> cat_thumb_height , true );
}
} else {
e ( lang ( " cat_dir_make_err " ));
}
} else {
e ( lang ( " cat_img_error " ));
}
}
}
/**
* Function used to get category thumb
*/
function get_cat_thumb ( $cat_details )
{
//Checking for category thumbs direcotry
if ( isset ( $this -> thumb_dir ))
$dir = $this -> thumb_dir ;
else
$dir = $this -> section_tbl ;
$cid = $cat_details [ 'category_id' ];
$path = CAT_THUMB_DIR . '/' . $dir . '/' . $cid . '.' ;
$exts = array ( 'jpg' , 'png' , 'gif' );
$file_exists = false ;
foreach ( $exts as $ext )
{
$cur_ext = $ext ;
if ( file_exists ( $path . $ext ))
{
$file_exists = true ;
break ;
}
}
if ( $file_exists )
return CAT_THUMB_URL . '/' . $dir . '/' . $cid . '.' . $ext ;
else
return $this -> default_thumb ();
}
function get_category_thumb ( $i )
{
return $this -> get_cat_thumb ( $i );
}
/**
* function used to return default thumb
*/
function default_thumb ()
{
if ( empty ( $this -> default_thumb ))
$this -> default_thumb = 'no_thumb.jpg' ;
return CAT_THUMB_URL . '/' . $this -> default_thumb ;
}
2010-02-01 11:09:02 +00:00
/**
* Function used to update category id
*/
function update_cat_order ( $id , $order )
{
global $db ;
$cat = $this -> category_exists ( $id );
if ( ! $cat )
2010-02-09 11:47:08 +00:00
e ( lang ( " cat_exist_error " ));
2010-02-01 11:09:02 +00:00
else
{
if ( ! is_numeric ( $order ) || $order < 1 )
$order = 1 ;
2010-02-06 08:18:36 +00:00
$db -> update ( tbl ( $this -> cat_tbl ), array ( " category_order " ), array ( $order ), " category_id=' " . $id . " ' " );
2010-02-01 11:09:02 +00:00
}
}
2010-08-06 11:42:41 +00:00
/**
* Function used get parent cateogry
*/
function get_parent_category ( $pid )
{
global $db ;
$result = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " category_id = $pid " );
if ( $db -> num_rows > 0 )
return $result ;
else
return false ;
}
/**
* Function used to check category is parent or not
*/
function is_parent ( $cid )
{
global $db ;
$result = $db -> count ( tbl ( $this -> cat_tbl ), " category_id " , " parent_id = $cid " );
if ( $result > 0 )
return true ;
else
return false ;
}
/**
* Function used to check wheather category has parent or not
*/
function has_parent ( $cid , $return_parent = false )
{
global $db ;
$result = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " category_id = $cid AND parent_id != 0 " );
if ( $result > 0 ) {
if ( $return_parent )
{
$pid = $this -> get_parent_category ( $result [ 0 ][ 'parent_id' ]);
return $pid ;
} else {
return true ;
}
} else {
return false ;
}
}
/**
* Function used to get parent categories
*/
function get_parents ( $count = false ) {
global $db ;
if ( $count ) {
$result = $db -> count ( tbl ( $this -> cat_tbl ), " * " , " parent_id = 0 " );
} else {
$result = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " parent_id = 0 " );
}
return $result ;
}
/**
* Function used to list categories in admin area
* with indention
*/
function admin_area_cats ( $selected )
{
global $db ;
$html = '' ;
$pcats = $this -> get_parents ();
if ( ! empty ( $pcats ))
{
foreach ( $pcats as $key => $pcat )
{
if ( $selected == $pcat [ 'category_id' ])
$select = " selected='selected' " ;
else
$select = NULL ;
$html .= " <option value=' " . $pcat [ 'category_id' ] . " ' $select > " ;
$html .= $pcat [ 'category_name' ];
$html .= " </option> " ;
if ( $this -> is_parent ( $pcat [ 'category_id' ]))
$html .= $this -> get_sub_subs ( $pcat [ 'category_id' ], $selected );
}
return $html ;
}
}
/**
* Function used to get child categories
*/
function get_sub_categories ( $cid )
{
global $db ;
$result = $db -> select ( tbl ( $this -> cat_tbl ), " * " , " parent_id = $cid " );
//echo $db->db_query;
if ( $result > 0 ){
return $result ;
} else {
return false ;
}
}
/**
* Function used to get child child categories
*/
function get_sub_subs ( $cid , $selected , $space = " - " )
{
global $db ;
$html = '' ;
$subs = $this -> get_sub_categories ( $cid );
if ( ! empty ( $subs ))
{
foreach ( $subs as $sub_key => $sub )
{
if ( $selected == $sub [ 'category_id' ])
$select = " selected='selected' " ;
else
$select = NULL ;
$html .= " <option value=' " . $sub [ 'category_id' ] . " ' $select > " ;
$html .= $space . $sub [ 'category_name' ];
$html .= " </option> " ;
if ( $this -> is_parent ( $sub [ 'category_id' ]))
$html .= $this -> get_sub_subs ( $sub [ 'category_id' ], $selected , $space . " - " );
}
return $html ;
}
}
function get_category_field ( $cid , $field )
{
global $db ;
$result = $db -> select ( tbl ( $this -> cat_tbl ), " $field " , " category_id = $cid " );
//echo $db->db_query;
if ( $result )
return $result [ 0 ][ $field ];
else
return false ;
}
2009-09-01 20:57:38 +00:00
}
?>