Multi enhancements, including auto serialization, product editing

This commit is contained in:
Deon George
2012-03-30 15:13:01 +11:00
parent d9c3394b0f
commit 6807b6ab52
30 changed files with 445 additions and 115 deletions

View File

@@ -12,17 +12,41 @@
*/
class Controller_Admin_Product extends Controller_TemplateDefault_Admin {
protected $secure_actions = array(
'ajaxtranslateform'=>TRUE,
'list'=>TRUE,
'update'=>TRUE,
'view'=>TRUE,
);
public function action_ajaxtranslateform() {
$this->auto_render = FALSE;
$po = ORM::factory('product',$this->request->param('id'));
if (! $this->request->is_ajax() OR ! $po->loaded() OR ! isset($_REQUEST['key']))
$this->response->body(_('Unable to find translate data'));
else {
$pto = $po->product_translate->where('language_id','=',$_REQUEST['key'])->find();
$this->response->body(View::factory($this->viewpath())->set('pto',$pto));
}
}
/**
* Show a list of products
*/
public function action_list() {
if ($this->request->param('id'))
$prods = ORM::factory('product')->list_category($this->request->param('id'),FALSE);
else
$prods = ORM::factory('product')->order_by('active DESC,prod_plugin_file')->find_all();
Block::add(array(
'title'=>_('Customer Products'),
'body'=>Table::display(
ORM::factory('product')->order_by('active DESC,prod_plugin_file')->find_all(),
$prods,
25,
array(
'id'=>array('label'=>'ID','url'=>'product/view/'),
@@ -43,5 +67,78 @@ class Controller_Admin_Product extends Controller_TemplateDefault_Admin {
)),
));
}
/**
* Edit a product configuration
*/
public function action_update() {
$po = ORM::factory('product',$this->request->param('id'));
if (! $po->loaded())
Request::current()->redirect('welcome/index');
if ($_POST) {
if (isset($_POST['product_translate']['id']) AND ($pto=ORM::factory('product_translate',$_POST['product_translate']['id'])) AND $pto->loaded())
if (! $pto->values($_POST['product_translate'])->update()->saved())
throw new Kohana_Exception('Failed to save updates to product_translate data for record :record',array(':record'=>$po->id()));
if (! $po->values($_POST)->update()->saved())
throw new Kohana_Exception('Failed to save updates to product data for record :record',array(':record'=>$so->id()));
}
Block::add(array(
'title'=>sprintf('%s %s:%s',_('Update Product'),$po->id,$po->name()),
'body'=>View::factory($this->viewpath())
->set('po',$po)
->set('mediapath',Route::get('default/media'))
->set('plugin_form',$po->admin_update()),
));
Script::add(array('type'=>'stdin','data'=>'
$(document).ready(function() {
$("select[name=language_id]").change(function() {
// Send the request and update sub category dropdown
$.ajax({
type: "GET",
data: "key="+$(this).val(),
dataType: "html",
cache: false,
url: "'.URL::site('admin/product/ajaxtranslateform/'.$po->id).'",
timeout: 2000,
error: function(x) {
alert("Failed to submit");
},
success: function(data) {
$("div[id=translate]").replaceWith(data);
}
});
});
});
'));
}
public function action_view() {
$po = ORM::factory('product',$this->request->param('id'));
Block::add(array(
'title'=>sprintf('%s: %s',_('Current Services Using this Product'),$po->name()),
'body'=>Table::display(
ORM::factory('service')->where('product_id','=',$po->id)->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>'user/service/view/'),
'account->accnum()'=>array(),
'account->name()'=>array('label'=>'Account'),
'name()'=>array('label'=>'Details'),
'active'=>array('label'=>'Active'),
'price(TRUE,TRUE)'=>array('label'=>'Price','align'=>'right'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>'user/service/view',
)),
));
}
}
?>

View File

@@ -42,6 +42,8 @@ class Controller_Product extends Controller_TemplateDefault {
Request::current()->redirect('welcome/index');
Breadcrumb::name($this->request->uri(),$cat->name);
Breadcrumb::url('product','product/categorys');
Breadcrumb::url('product/category','product/categorys');
Block::add(array(
'title'=>sprintf('%s: %s',_('Category'),$cat->name),
@@ -68,6 +70,7 @@ class Controller_Product extends Controller_TemplateDefault {
Request::current()->redirect('product_category/index');
Breadcrumb::name($this->request->uri(),$po->product_translate->find()->name);
Breadcrumb::url('product','product/categorys');
// Work out our category id for the control line
if (! empty($_GET['cid'])) {
@@ -78,6 +81,7 @@ class Controller_Product extends Controller_TemplateDefault {
Request::current()->redirect('product_category/index');
Breadcrumb::name('product/view',$co->name);
Breadcrumb::url('product/view','product/category/'.$co->id);
}
Block::add(array(

View File

@@ -41,6 +41,19 @@ class Model_Product extends ORMOSB {
),
);
// Our attributes that are arrays, we'll convert/unconvert them
protected $_serialize_column = array(
'price_group',
);
public function rules() {
return array_merge(parent::rules(),array(
'price_group'=>array(
array('ORMOSB::serialize_array',array(':model',':field',':value')),
),
));
}
/**
* Return the object of the product plugin
*/
@@ -81,14 +94,14 @@ class Model_Product extends ORMOSB {
// @todo Need to work out our default groups elsewhere, not in product
// All users are members of the all user group "0"
$groups = array(0);
$pg = unserialize($this->price_group);
if (Auth::instance()->logged_in())
foreach (Auth::instance()->get_user()->group->find_all() as $go)
array_push($groups,$go->id);
// Work out the best price for the user
$price = array();
foreach (unserialize($this->price_group) as $bill_freq => $pg) {
if (is_array($this->price_group))
foreach ($this->price_group as $bill_freq => $pg) {
if (isset($pg['show']) AND $pg['show'])
foreach ($groups as $gid) {
if (! empty($pg[$gid])) {
@@ -133,6 +146,50 @@ class Model_Product extends ORMOSB {
echo '';
}
/**
* Enable the plugin to store data
*/
public function admin_update() {
if (is_null($plugin = $this->plugin()))
return NULL;
else
return $plugin->admin_update();
}
/**
* Is price shown for a specific period
*/
public function isPriceShown($p) {
$x = $this->keyget('price_group',$p);
return (isset($x['show']) AND $x['show']) ? TRUE : FALSE;
}
/**
* Return the configured price groups for this product
*/
public function availPriceGroups() {
// @todo This needs to be worked out dynamically
return array(0,2);
}
/**
* Return the available pricing options
*/
public function availPriceOptions() {
// @todo This needs to be worked out dynamically
return array('price_base','price_setup');
}
/**
* Return the price for the particle group and price option for the period
*/
public function price($grp,$period,$option) {
$x = $this->keyget('price_group',$period);
return isset($x[$grp][$option]) ? $x[$grp][$option] : NULL;
}
/**
* List the number of services using this product
*/
@@ -151,14 +208,20 @@ class Model_Product extends ORMOSB {
* Return the products for a given category
* @todo This shouldnt be here.
*/
public function list_category($cat) {
public function list_category($cat,$active=TRUE) {
$results = array();
foreach ($this->where('active','=',TRUE)->find_all() as $po) {
if ($active)
$cats = $this->where('active','=',TRUE);
else
$cats = $this;
foreach ($cats->find_all() as $po) {
if ($c = unserialize($po->avail_category) AND in_array($cat,$c))
array_push($results,$po);
}
Sort::MAsort($results,'position,price_base');
return $results;
}
}

View File

@@ -0,0 +1,20 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class product access to product category translation.
*
* @package OSB
* @subpackage Product
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Product_Category_Translate extends ORMOSB {
protected $_table_name = 'product_cat_translate';
protected $_belongs_to = array(
'product_category'=>array(),
);
}
?>

View File

@@ -14,6 +14,12 @@ abstract class Model_Product_Plugin extends ORMOSB {
// Reset any sorting that may be defined in our parent
protected $_sorting = array();
/**
* The admin_update should be implemented in plugins.
* It is used to update the plugin specific product information
*/
abstract public function admin_update();
/**
* The feature summary should be implemented in plugins.
* It is displayed on the product overview page, as a summary of the products features.

View File

@@ -11,6 +11,8 @@
* @license http://dev.osbill.net/license.html
*/
class Model_Product_Translate extends ORMOSB {
protected $_updated_column = FALSE;
protected $_belongs_to = array(
'product'=>array(),
);