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

368 lines
9.8 KiB
PHP
Raw Normal View History

<?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
2009-08-03 04:10:16 +00:00
*
* 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
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Module:Product
*/
2009-08-03 04:10:16 +00:00
/**
* 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;
2009-08-03 04:10:16 +00:00
} else {
while (! $result->EOF) {
# Check for group settings:
$groups = unserialize($result->fields['group_avail']);
$auth = false;
2009-08-03 04:10:16 +00:00
for ($ii=0; $ii<count($groups); $ii++) {
if ($C_auth->auth_group_by_id($groups[$ii]))
$auth = true;
}
2009-08-03 04:10:16 +00:00
if ($auth) {
# Create the array for smarty
array_push($smart,$result->fields);
$i++;
}
2009-08-03 04:10:16 +00:00
$result->MoveNext();
}
2009-08-03 04:10:16 +00:00
$smarty->assign('product_cat',$smart);
}
}
2009-08-03 04:10:16 +00:00
/**
* User view a category
*/
public function user_view($VAR) {
global $smarty,$C_auth;
2009-08-03 04:10:16 +00:00
$db = &DB();
2009-08-03 04:10:16 +00:00
# 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'])));
2009-08-03 04:10:16 +00:00
if (! $result || $result->RecordCount() == 0) {
return false;
2009-08-03 04:10:16 +00:00
} else {
# Check for group settings
$groups = unserialize($result->fields['group_avail']);
2009-08-03 04:10:16 +00:00
# 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;
2009-08-03 04:10:16 +00:00
} else {
$page = $VAR['page'];
$start = ($page-1)*$max;
2009-08-03 04:10:16 +00:00
}
$auth = false;
2009-08-03 04:10:16 +00:00
for ($ii=0; $ii<count($groups); $ii++) {
if ($C_auth->auth_group_by_id($groups[$ii]))
$auth = true;
}
2009-08-03 04:10:16 +00:00
if ($auth)
$smarty->assign('product_cat_arr',array($result->fields));
else
return false;
$parent_id = $result->fields['parent_id'];
2009-08-03 04:10:16 +00:00
if (! $parent_id)
$parent_id = '0';
}
2009-08-03 04:10:16 +00:00
# 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;
2009-08-03 04:10:16 +00:00
} else {
$count = 0;
$smart_prod = array();
while (! $result->EOF) {
# check that this item can be displayed in the current category:
2009-08-03 04:10:16 +00:00
$cat = false;
$cats = unserialize($result->fields['avail_category_id']);
for ($i=0; $i<count($cats); $i++) {
if ($cats[$i] == $VAR['id']) {
$cat = true;
break;
}
}
2009-08-03 04:10:16 +00:00
if ($cat) {
# Check for group settings:
$groups = unserialize($result->fields['group_avail']);
$auth = false;
2009-08-03 04:10:16 +00:00
for ($ii=0; $ii<count($groups); $ii++) {
if ($C_auth->auth_group_by_id($groups[$ii])) {
$auth = true;
break;
}
2009-08-03 04:10:16 +00:00
}
2009-08-03 04:10:16 +00:00
# Paging
if ($auth) {
if ($count >= $start && $count < $max*$page)
array_push($smart_prod,$result->fields);
$count++;
}
}
$result->MoveNext();
2009-08-03 04:10:16 +00:00
}
$results = $count;
2009-08-03 04:10:16 +00:00
$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);
}
2009-08-03 04:10:16 +00:00
# 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();
}
2009-08-03 04:10:16 +00:00
$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
2009-08-03 04:10:16 +00:00
$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);
}
2009-08-03 04:10:16 +00:00
}
}
$smart_parent_cat = array_reverse($smart_parent_cat);
2009-08-03 04:10:16 +00:00
# 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;
2009-08-03 04:10:16 +00:00
$smarty->assign('parent_cat',$smart_parent_cat);
}
2009-08-03 04:10:16 +00:00
/**
* 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'];
2009-08-03 04:10:16 +00:00
if ($id == $current || $arr[$start][$i]['sel'])
$sel = ' selected="selected"';
else
$sel = '';
2009-08-03 04:10:16 +00:00
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
2009-08-03 04:10:16 +00:00
if (! empty($VAR['id']))
$cid = str_replace(',','',$VAR['id']);
else
$current = '';
# Loop and put in array
2009-08-03 04:10:16 +00:00
$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;
2009-08-03 04:10:16 +00:00
else
$arr[$result->fields['parent_id']][] = $result->fields;
# get current parent_id
2009-08-03 04:10:16 +00:00
if ($cid > 0 && $result->fields['id'] == $cid)
$current = $result->fields['parent_id'];
$result->MoveNext();
}
2009-08-03 04:10:16 +00:00
# Create menu
echo '<select name="product_cat_parent_id" onChange="submit()">';
2009-08-03 04:10:16 +00:00
echo '<option value="">&nbsp;</option>';
echo $this->build_nested_option_list($arr,0,0,$current);
echo '</select>';
}
2009-08-03 04:10:16 +00:00
/**
* Select a product category for a product
*/
public function tpl_admin_menu_product($VAR) {
global $smarty,$C_auth;
2009-08-03 04:10:16 +00:00
$db = &DB();
$dbc = new CORE_database;
# Get current category id
2009-08-03 04:10:16 +00:00
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']);
2009-08-03 04:10:16 +00:00
} else {
$current = '';
}
# Loop and put in array
2009-08-03 04:10:16 +00:00
$result = $db->Execute(sqlSelect($db,'product_cat','*','','parent_id,position,name'));
while (! $result->EOF) {
# Determine if selected
$select = false;
2009-08-03 04:10:16 +00:00
for ($ix=0; $ix<count($current); $ix++) {
if ($current[$ix] == $result->fields['id']) {
$result->fields['sel'] = 'selected';
break;
}
2009-08-03 04:10:16 +00:00
}
2009-08-03 04:10:16 +00:00
# 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();
}
2009-08-03 04:10:16 +00:00
# Create menu
echo '<select name="product_avail_category_id[]" size="5" multiple >';
echo $this->build_nested_option_list($arr,0,0,null);
echo '</select>';
}
2009-08-03 04:10:16 +00:00
/**
* Update an entry
*/
public function update($VAR) {
global $_FILES;
2009-08-03 04:10:16 +00:00
# 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']));
}
}
2009-08-03 04:10:16 +00:00
/**
* Delete an Entry
*/
public function delete($VAR) {
$this->associated_DELETE = array();
2009-08-03 04:10:16 +00:00
array_push($this->associated_DELETE,array('table' =>'product_cat_translate','field'=>'product_cat_id'));
2009-08-03 04:10:16 +00:00
parent::delete($VAR);
}
}
2009-08-03 04:10:16 +00:00
?>