2013-10-10 02:44:53 +00:00
< ? php defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
* This class provides Admin Product management
*
* @ package Product
* @ category Controllers / Admin
* @ author Deon George
* @ copyright ( c ) 2009 - 2013 Open Source Billing
* @ license http :// dev . osbill . net / license . html
*/
2013-04-26 01:42:09 +00:00
class Controller_Admin_Product extends Controller_Product {
protected $auth_required = TRUE ;
2013-10-10 02:44:53 +00:00
protected $secure_actions = array (
2016-06-05 12:33:12 +00:00
'ajaxaccounting' => TRUE ,
2013-04-26 01:42:09 +00:00
'ajaxtranslatecategory' => TRUE ,
'ajaxtranslate' => TRUE ,
'category' => TRUE ,
'edit' => TRUE ,
2013-10-10 02:44:53 +00:00
'list' => TRUE ,
2016-06-05 12:33:12 +00:00
'listused' => TRUE ,
2013-10-10 02:44:53 +00:00
'view' => TRUE ,
);
2016-06-05 12:33:12 +00:00
public function action_ajaxaccounting () {
$products = ORM :: factory ( 'Product' ) -> where ( 'accounting' , 'LIKE' , '%' . $this -> request -> query ( 'term' ) . '%' ) -> find_all ();
$result = array ();
foreach ( $products as $po )
array_push ( $result , array ( 'value' => $po -> accounting , 'label' => $po -> accounting ));
$this -> response -> headers ( 'Content-Type' , 'application/json' );
$this -> response -> body ( json_encode ( array_values ( $result )));
}
2013-04-26 01:42:09 +00:00
public function action_ajaxtranslate () {
2013-10-10 02:44:53 +00:00
$po = ORM :: factory ( 'Product' , $this -> request -> param ( 'id' ));
2016-08-03 06:20:25 +00:00
if ( ! $po -> loaded () OR ! $this -> request -> query ( 'key' )) {
2013-04-26 01:42:09 +00:00
$output = _ ( 'Unable to find translate data' );
2013-10-10 02:44:53 +00:00
2013-04-26 01:42:09 +00:00
} else {
2016-08-03 06:20:25 +00:00
$pto = $po -> translate -> where ( 'language_id' , '=' , $this -> request -> query ( 'key' )) -> find ();
2013-10-10 02:44:53 +00:00
2013-04-26 01:42:09 +00:00
$output = View :: factory ( 'product/admin/ajaxtranslate' )
-> set ( 'o' , $pto );
2013-10-10 02:44:53 +00:00
}
2013-04-26 01:42:09 +00:00
2013-06-17 08:01:47 +00:00
$this -> template -> content = $output ;
2013-04-26 01:42:09 +00:00
}
/**
* Retrieve the product category translate record
*/
public function action_ajaxtranslatecategory () {
$pco = ORM :: factory ( 'Product_Category' , $this -> request -> param ( 'id' ));
2016-08-03 06:20:25 +00:00
if ( ! $pco -> loaded () OR ! $this -> request -> query ( 'key' )) {
2013-04-26 01:42:09 +00:00
$output = _ ( 'Unable to find translate data' );
} else {
2016-08-03 06:20:25 +00:00
$pcto = $pco -> translate -> where ( 'language_id' , '=' , $this -> request -> query ( 'key' )) -> find ();
2013-04-26 01:42:09 +00:00
$output = View :: factory ( 'product/category/admin/ajaxtranslate' )
-> set ( 'o' , $pcto );
}
2013-06-17 08:01:47 +00:00
$this -> template -> content = $output ;
2013-04-26 01:42:09 +00:00
}
/**
* Update the product category
*/
public function action_category () {
$pco = ORM :: factory ( 'Product_Category' , $this -> request -> param ( 'id' ));
if ( ! $pco -> loaded ())
2013-06-01 13:43:31 +00:00
HTTP :: redirect ( URL :: link ( 'admin' , 'product/list' ));
2013-04-26 01:42:09 +00:00
2016-08-03 06:20:25 +00:00
if ( $this -> request -> post () AND $pco -> values ( $this -> request -> post ()) -> changed () AND ( ! $this -> save ( $pco )))
2013-11-22 04:36:50 +00:00
$pco -> reload ();
2013-04-26 01:42:09 +00:00
Script :: factory ()
-> type ( 'stdin' )
-> data ( '
$ ( document ) . ready ( function () {
$ ( " select[name=language_id] " ) . change ( function () {
// If we select a blank, then dont continue
if ( this . value == 0 )
return false ;
// Send the request and update sub category dropdown
$ . ajax ({
type : " GET " ,
data : " key= " + $ ( this ) . val (),
dataType : " html " ,
cache : false ,
url : " '.URL::link('admin','product/ajaxtranslatecategory/'. $pco->id ,TRUE).' " ,
timeout : 2000 ,
error : function ( x ) {
alert ( " Failed to submit " );
},
success : function ( data ) {
2013-11-27 00:22:20 +00:00
$ ( " div[id=translate] " ) . empty () . append ( data );
2013-04-26 01:42:09 +00:00
}
});
});
});
' );
Block :: factory ()
-> type ( 'form-horizontal' )
-> title ( 'Update Category' )
-> title_icon ( 'icon-wrench' )
-> body ( View :: factory ( 'product/category/admin/edit' )
-> set ( 'o' , $pco ));
}
/**
* Edit a product configuration
*/
public function action_edit () {
$po = ORM :: factory ( 'Product' , $this -> request -> param ( 'id' ));
if ( ! $po -> loaded ())
HTTP :: redirect ( 'welcome/index' );
2016-08-03 06:20:25 +00:00
if ( $this -> request -> post () AND $po -> values ( $this -> request -> post ()) -> changed () AND ( ! $this -> save ( $po )))
2013-11-22 04:36:50 +00:00
$po -> reload ();
2013-04-26 01:42:09 +00:00
Script :: factory ()
-> type ( 'stdin' )
-> data ( '
$ ( document ) . ready ( function () {
$ ( " select[name=language_id] " ) . change ( function () {
// If we select a blank, then dont continue
if ( this . value == 0 )
return false ;
// Send the request and update sub category dropdown
$ . ajax ({
type : " GET " ,
data : " key= " + $ ( this ) . val (),
dataType : " html " ,
cache : false ,
url : " '.URL::link('admin','product/ajaxtranslate/'. $po->id ,TRUE).' " ,
timeout : 2000 ,
error : function ( x ) {
alert ( " Failed to submit " );
},
success : function ( data ) {
2013-11-27 00:22:20 +00:00
$ ( " div[id=translate] " ) . empty () . append ( data );
2013-04-26 01:42:09 +00:00
}
});
});
2016-06-05 12:33:12 +00:00
$ ( " input[name=accounting] " ) . typeahead ({
minLength : 2 ,
source : function ( query , process ) {
accounting ( \ ' a / product / ajaxaccounting\ ' , query , process );
},
matcher : function () { return true ; },
});
2013-04-26 01:42:09 +00:00
});
2016-06-05 12:33:12 +00:00
var d = 0 ;
var accounting = _ . debounce ( function ( url , query , process ){
$ . ajax ({
url : site_url + url ,
type : " GET " ,
data : " term= " + query ,
dataType : " JSON " ,
async : true ,
cache : false ,
beforeSend : function () {
if ( d ++ == 0 ) $ ( \ ' i [ name = acclook ] \ ' ) . removeClass ( " hidden " );
},
success : function ( data ) {
// if json is null, means no match, won\'t do again.
if ( data == null || ( data . length === 0 )) return ;
users = {};
userLabels = [];
_ . each ( data , function ( item , ix , list ) {
if ( _ . contains ( users , item . label )) item . label = item . label + \ ' #\' + item.value;
userLabels . push ( item . label );
users [ item . label ] = item . value ;
});
process ( userLabels );
},
complete : function () {
if ( -- d == 0 ) $ ( \ ' i [ name = acclook ] \ ' ) . addClass ( " hidden " );
}
})
}, 500 );
2013-04-26 01:42:09 +00:00
' );
Block :: factory ()
-> type ( 'form-horizontal' )
-> title ( 'Update Product' )
2016-06-05 12:33:12 +00:00
-> title_icon ( 'fa fa-wrench' )
2013-04-26 01:42:09 +00:00
-> body ( View :: factory ( 'product/admin/edit' )
2013-11-27 00:22:20 +00:00
-> set ( 'plugin_form' , $po -> plugin_edit ())
2013-04-26 01:42:09 +00:00
-> set ( 'o' , $po ));
2013-10-10 02:44:53 +00:00
}
/**
* Show a list of products
*/
public function action_list () {
2016-08-31 05:03:09 +00:00
$products = ( $x = ORM :: factory ( 'Product_Category' , $this -> request -> param ( 'id' )) AND $x -> loaded ()) ? $x -> products () : ORM :: factory ( 'Product' ) -> order_by ( 'active' , 'DESC' ) -> order_by ( 'prod_plugin_file' ) -> find_all ();
2013-04-26 01:42:09 +00:00
Block :: factory ()
2016-06-05 12:33:12 +00:00
-> title ( _ ( 'Products' ) . ( $x -> loaded () ? ' - ' . $x -> name () : ' - All' ))
-> title_icon ( 'fa fa-list' )
-> body ( View :: factory ( 'product/list' ) -> set ( 'list' , $products ));
}
/**
* List all the products that have an active service
*/
public function action_listused () {
$db = Database :: instance ();
$columns = '' ;
foreach ( array_keys ( ORM :: factory ( 'Product' ) -> table_columns ()) as $text )
$columns .= ( $columns ? ',' : '' ) . 'p.' . $text ;
2016-08-31 05:03:09 +00:00
$products = $db -> query ( Database :: SELECT , sprintf ( 'SELECT %s FROM ab_product p,ab_service s WHERE s.product_id=p.id AND s.active=1 AND p.site_id = s.site_id AND s.site_id=%s GROUP BY p.id' , $columns , Company :: instance () -> site ()), 'Model_Product' );
2016-06-05 12:33:12 +00:00
Block :: factory ()
-> title ( _ ( 'Products with Active Services' ))
-> title_icon ( 'fa fa-list' )
-> body ( View :: factory ( 'product/list' ) -> set ( 'list' , $products ));
2013-10-10 02:44:53 +00:00
}
2016-06-05 12:33:12 +00:00
2013-10-10 02:44:53 +00:00
public function action_view () {
$po = ORM :: factory ( 'Product' , $this -> request -> param ( 'id' ));
2013-06-13 13:35:19 +00:00
if ( ! $po -> loaded ())
throw HTTP_Exception :: factory ( 403 , 'Product either doesnt exist, or you are not authorised to see it' );
2013-04-26 01:42:09 +00:00
Block :: factory ()
2016-08-15 11:23:18 +00:00
-> title ( sprintf ( '%s: %s' , _ ( 'Current Services Using this Product' ), $po -> name ( Site :: language ())))
2013-04-26 01:42:09 +00:00
-> title_icon ( 'icon-th-list' )
2016-07-29 13:04:34 +00:00
-> body ( View :: factory ( 'service/reseller/list' ) -> set ( 'o' , $po -> service -> where_active () -> find_all ()));
2013-10-10 02:44:53 +00:00
}
}
?>