This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/product_cat/product_cat.inc.php
2011-05-03 09:49:01 +10:00

368 lines
9.8 KiB
PHP

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @subpackage Module:Product
*/
/**
* The main AgileBill Product Category Class
*
* @package AgileBill
* @subpackage Module:Product
*/
class product_cat extends OSB_module {
/**
* User Menu
* List the available categories
*/
public function user_menu($VAR) {
global $smarty,$C_auth;
$db = &DB();
$result = $db->Execute(sqlSelect($db,'product_cat','*','status=1 AND (parent_id=0 OR parent_id IS NULL OR parent_id=id)','position'));
$smart = array();
if ($result->RecordCount() == 0) {
return false;
} else {
while (! $result->EOF) {
# Check for group settings:
$groups = unserialize($result->fields['group_avail']);
$auth = false;
for ($ii=0; $ii<count($groups); $ii++) {
if ($C_auth->auth_group_by_id($groups[$ii]))
$auth = true;
}
if ($auth) {
# Create the array for smarty
array_push($smart,$result->fields);
$i++;
}
$result->MoveNext();
}
$smarty->assign('product_cat',$smart);
}
}
/**
* User view a category
*/
public function user_view($VAR) {
global $smarty,$C_auth;
$db = &DB();
# Get the category information:
$result = $db->Execute(sqlSelect($db,'product_cat','id,position,template,name,thumbnail,group_avail,parent_id,max',array('status'=>1,'id'=>$VAR['id'])));
if (! $result || $result->RecordCount() == 0) {
return false;
} else {
# Check for group settings
$groups = unserialize($result->fields['group_avail']);
# Max results per page:
$max = $result->fields['max'];
if (empty($max))
$max = 25;
if (empty($VAR['page']) || ! is_numeric($VAR['page']) || $VAR['page'] <= 1) {
$page = 1;
$start = 0;
} else {
$page = $VAR['page'];
$start = ($page-1)*$max;
}
$auth = false;
for ($ii=0; $ii<count($groups); $ii++) {
if ($C_auth->auth_group_by_id($groups[$ii]))
$auth = true;
}
if ($auth)
$smarty->assign('product_cat_arr',array($result->fields));
else
return false;
$parent_id = $result->fields['parent_id'];
if (! $parent_id)
$parent_id = '0';
}
# Get the items in this category:
$result = $db->Execute(sqlSelect($db,'product','id,sku,thumbnail,avail_category_id,price_base,price_setup,group_avail',array('active'=>1),'position,sku'));
if (! $result || $result->RecordCount() == 0) {
return false;
} else {
$count = 0;
$smart_prod = array();
while (! $result->EOF) {
# check that this item can be displayed in the current category:
$cat = false;
$cats = unserialize($result->fields['avail_category_id']);
for ($i=0; $i<count($cats); $i++) {
if ($cats[$i] == $VAR['id']) {
$cat = true;
break;
}
}
if ($cat) {
# Check for group settings:
$groups = unserialize($result->fields['group_avail']);
$auth = false;
for ($ii=0; $ii<count($groups); $ii++) {
if ($C_auth->auth_group_by_id($groups[$ii])) {
$auth = true;
break;
}
}
# Paging
if ($auth) {
if ($count >= $start && $count < $max*$page)
array_push($smart_prod,$result->fields);
$count++;
}
}
$result->MoveNext();
}
$results = $count;
$pages = intval($results / $max);
if ($results % $max)
$pages++;
for ($i=1;$i<=$pages;$i++)
$pagearr[$i] = $i;
$smarty->assign('product_arr',$smart_prod);
$smarty->assign('page_page',$page);
$smarty->assign('page_results',$results);
$smarty->assign('page_pages',$pages);
$smarty->assign('page_arr',$pagearr);
}
# Get any sub-categories:
$result = $db->Execute($q=sqlSelect($db,'product_cat','*',array('status'=>1,'parent_id'=>$VAR['id']),'position,name'));
$smart_sub_cat = array();
if ($result && $result->RecordCount()) {
while (! $result->EOF) {
array_push($smart_sub_cat,$result->fields);
$result->MoveNext();
}
$smarty->assign('product_sub_cat',$smart_sub_cat);
}
# Get any parent categores:
$smart_parent_cat = array();
for ($i=0; $i<=5; $i++) {
if ($parent_id > 0 ) {
# Get parent id & add to array
$result = $db->Execute(sqlSelect($db,array('product_cat','product_cat_translate'),'A.id,A.parent_id,A.template,B.name',array('A.id'=>$parent_id,'B.product_cat_id'=>$parent_id,'B.language_id'=>SESS_LANGUAGE)));
if ($result && $result->RecordCount()) {
$parent_id = $result->fields['parent_id'];
array_push($smart_parent_cat,$result->fields);
}
}
}
$smart_parent_cat = array_reverse($smart_parent_cat);
# Get the current category:
$result = $db->Execute(sqlSelect($db,array('product_cat','product_cat_translate'),'A.id,A.parent_id,A.template,B.name',array('A.id'=>$VAR['id'],'B.product_cat_id'=>$VAR['id'],'B.language_id'=>SESS_LANGUAGE)));
if ($result && $result->RecordCount())
$smart_parent_cat[] = $result->fields;
$smarty->assign('parent_cat',$smart_parent_cat);
}
/**
* This function will build a nested option list
* showing the heirachy of the product categories
*
* @see tpl_admin_menu_parent
*/
private function build_nested_option_list($arr,$start,$level,$current) {
$option = '';
for ($i=0; $i<count($arr[$start]); $i++) {
$id = $arr[$start][$i]['id'];
if ($id == $current || $arr[$start][$i]['sel'])
$sel = ' selected="selected"';
else
$sel = '';
if ($level == 0)
$option .= sprintf('<option value="%s"%s>%s</option>',$id,$sel,$arr[$start][$i]['name']);
else
$option .= sprintf('<option value="%s"%s>%s- %s</option>',$id,$sel,str_repeat('&nbsp;',$level-1),$arr[$start][$i]['name']);
if (isset($arr[$id]))
$option .= $this->build_nested_option_list($arr,$id,$level+1,$current);
}
return $option;
}
/**
* Select a parent for a category
*/
public function tpl_admin_menu_parent($VAR) {
global $smarty,$C_auth;
$db = &DB();
# Get current id
if (! empty($VAR['id']))
$cid = str_replace(',','',$VAR['id']);
else
$current = '';
# Loop and put in array
$result = $db->Execute(sqlSelect($db,'product_cat','*','','parent_id,position,name'));
while (! $result->EOF) {
if ($result->fields['parent_id'] == '' || $result->fields['parent_id'] == 0 || $result->fields['parent_id'] == $result->fields['id'])
$arr[0][] = $result->fields;
else
$arr[$result->fields['parent_id']][] = $result->fields;
# get current parent_id
if ($cid > 0 && $result->fields['id'] == $cid)
$current = $result->fields['parent_id'];
$result->MoveNext();
}
# Create menu
echo '<select name="product_cat_parent_id" onChange="submit()">';
echo '<option value="">&nbsp;</option>';
echo $this->build_nested_option_list($arr,0,0,$current);
echo '</select>';
}
/**
* Select a product category for a product
*/
public function tpl_admin_menu_product($VAR) {
global $smarty,$C_auth;
$db = &DB();
$dbc = new CORE_database;
# Get current category id
if (! empty($VAR['id'])) {
$product_id = str_replace(',','',$VAR['id']);
$product = $db->Execute(sqlSelect($db,'product','avail_category_id',array('id'=>$product_id)));
$current = unserialize($product->fields['avail_category_id']);
} else {
$current = '';
}
# Loop and put in array
$result = $db->Execute(sqlSelect($db,'product_cat','*','','parent_id,position,name'));
while (! $result->EOF) {
# Determine if selected
$select = false;
for ($ix=0; $ix<count($current); $ix++) {
if ($current[$ix] == $result->fields['id']) {
$result->fields['sel'] = 'selected';
break;
}
}
# set array
if ($result->fields['parent_id'] == '' || $result->fields['parent_id'] == 0 || $result->fields['parent_id'] == $result->fields['id'])
$arr[0][] = $result->fields;
else
$arr[$result->fields['parent_id']][] = $result->fields;
$result->MoveNext();
}
# Create menu
echo '<select name="product_avail_category_id[]" size="5" multiple >';
echo $this->build_nested_option_list($arr,0,0,null);
echo '</select>';
}
/**
* Update an entry
*/
public function update($VAR) {
global $_FILES;
# Validate the thumbnail upload
if (isset($_FILES['upload_file1']) && $_FILES['upload_file1']['size'] > 0)
$VAR['product_cat_thumbnail'] = sprintf('cat_thmb_%s',$_FILES['upload_file1']['name']);
elseif ($VAR['delthumb'] == 1)
$VAR['product_cat_thumbnail'] = '';
# Validate the image upload
if (isset($_FILES['upload_file2']) && $_FILES['upload_file2']['size'] > 0)
$VAR['product_cat_image'] = sprintf('cat_img_%s',$_FILES['upload_file2']['name']);
elseif ( $VAR['delimg'] == 1 )
$VAR['product_cat_image'] = '';
$result = parent::update($VAR);
# Copy the image(s)
if ($result) {
# Copy 1ST file upload
if (isset($_FILES['upload_file1']) && $_FILES['upload_file1']['size'] > 0)
copy($_FILES['upload_file1']['tmp_name'],sprintf('%scat_thmb_%s',PATH_IMAGES,$_FILES['upload_file1']['name']));
# Copy the 2ND file upload
if (isset($_FILES['upload_file2']) && $_FILES['upload_file2']['size'] > 0)
copy($_FILES['upload_file2']['tmp_name'],sprintf('%scat_img_',PATH_IMAGES,$_FILES['upload_file2']['name']));
}
}
/**
* Delete an Entry
*/
public function delete($VAR) {
$this->associated_DELETE = array();
array_push($this->associated_DELETE,array('table' =>'product_cat_translate','field'=>'product_cat_id'));
parent::delete($VAR);
}
}
?>