array('','char'),'selectname2'=>array('','char'),'selectname3'=>array('','char'),'selectname4'=>array('','char'),
'selectoptions1'=>array('','char'),'selectoptions2'=>array('','char'),'selectoptions3'=>array('','char'),'selectoptions4'=>array('','char')
);
$this->setConfigParameterable('custom_params',$varsToPush);
// self::$_this = $this;
}
// function plgVmOnOrder($product) {
// $dbValues['virtuemart_product_id'] = $product->virtuemart_product_id;
// $dbValues['stockable'] = $this->_virtuemart_paymentmethod_id;
// $this->writeCustomData($dbValues, '#__virtuemart_product_custom_' . $this->_name);
// }
// get product param for this plugin on edit
/**
* @see components/com_virtuemart/helpers/vmCustomPlugin::plgVmOnProductEdit()
* @author Matt Lewis-Garner
* @author Patrick Kohl
*/
function plgVmOnProductEdit($field, $product_id, &$row,&$retValue) {
//TODO Give warning if config not set to disableit_children
if ($field->custom_element != $this->_name) return '';
$this->parseCustomParams($field);
$html ='';
if (!$childs = $this->getChilds($product_id) ) $html .='
'.JTEXT::_('VMCUSTOM_STOCKABLE_NO_CHILD').'
';
$db = JFactory::getDBO();
// $db->setQuery('SELECT `virtuemart_custom_id` FROM `#__virtuemart_customs` WHERE field_type="G" ');
// $group_custom_id = $db->loadResult();
// $plgParam = $this->getVmCustomParams($field->virtuemart_custom_id);
$html .=''.JText::_('VMCUSTOM_STOCKABLE_IS_VARIANT').'';
for ($i = 1; $i<5 ;$i++) {
$selectname = 'selectname'.$i ;
$listname = $field->$selectname;
if (!empty($listname)) {
$html .=' '.JTEXT::_($listname).'';
}
}
$html .=' '. JText::_('VMCUSTOM_STOCKABLE_PRICE') .'';
// $param = json_decode($field->custom_param,true);
if (isset($field->child)) $childList = $field->child;
else $childList = array();
$html .= '';
foreach ($childs as $child ) {
$checked ='';
$price = null;
if(!empty($childList)) {
if (!array_key_exists($child->id, $childList) ) $childList[$child->id]['is_variant'] = 1;
if ($childList[$child->id]['is_variant'] ) $checked='checked';
if (array_key_exists('custom_price', $childList[$child->id] ) )
$price = $childList[$child->id]['custom_price'] ;
}
//$html .= JHTML::_('select.genericlist', $childlist, 'custom_param['.$row.'][child_id]','','virtuemart_product_id','product_name',$param['child_id'],false,true);
$name='custom_param['.$row.'][child]['.$child->id.']';
$html .='
' ;
$html .=' ';
$html .=' ';
$html .=$this->getCustomOptionsForm($field, $name, $childList[$child->id]);
//$html .='';
// if (!$customfield = $this->getFieldId($product_id, $child->id) ) $price ='' ;
// else
$html .='';
// $html .='';
// $html .='';
$html .= ' '.$child->product_name.' ['.JText::_('COM_VIRTUEMART_PRODUCT_IN_STOCK').' : '.$child->stock.']
' ;
}
$html .='
';
$new_fields = $this->getCustomOptionsForm($field, '{{NAME}}', array());
$script = "
jQuery( function($) {
$('#new_stockable_product').click(function() {
var Prod = $('#new_stockable');// input[name^=\"stockable\"]').serialize();
$.getJSON('index.php?option=com_virtuemart&view=product&task=saveJS&token=".JUtility::getToken()."' ,
{
product_sku: Prod.find('input[name*=\"product_sku\"]').val(),
product_name: Prod.find('input[name*=\"product_name\"]').val(),
product_price: Prod.find('input[name*=\"product_price\"]').val(),
product_in_stock: Prod.find('input[name*=\"product_in_stock\"]').val(),
product_parent_id: ".$product_id.",
published: 1,
format: \"json\"
},
function(data) {
//console.log (data);
//$.each(data.msg, function(index, value){
// $(\"#new_stockable\").append(data.msg);
//});
name='custom_param[".$row."][child]['+data.product_id+']';
new_fields = '".str_replace("\n", '', $new_fields)."';
new_fields = new_fields.replace(/\{\{NAME\}\}/g, name);
$('#stockables').append(
'' +
'' +
'' +
new_fields +
'' +
' '+Prod.find('input[name*=\"product_name\"]').val()+' [".JText::_('COM_VIRTUEMART_PRODUCT_SKU')." : '+Prod.find('input[name*=\"product_sku\"]').val()+'] [".JText::_('COM_VIRTUEMART_PRODUCT_IN_STOCK')." : '+(Prod.find('input[name*=\"product_in_stock\"]').val() || 0)+']' +
'
');
Prod.find('input[name*=\"product_sku\"]').val('');
Prod.find('input[name*=\"product_name\"]').val('');
Prod.find('input[name*=\"product_price\"]').val('');
Prod.find('input[name*=\"product_in_stock\"]').val('');
});
});
});
";
//$document = JFactory::getDocument();
//$document->addScriptDeclaration($script);
// $html =' ';
// $html .='';
//$html .=JTEXT::_('VMCUSTOM_TEXTINPUT_NO_CHANGES_BE');
$retValue .= $html.'';
return true ;
}
/**
* Get the formatted options dropdowns and input fields (backend)
* @author Matt Lewis-Garner
*/
function getCustomOptionsForm($field, $name, $values) {
$options_html = '';
for ($i = 1; $i<5 ;$i++) {
$selectoptions = 'selectoptions'.$i ;
$attributes = 'attribute'.$i ;
if (isset($field->$selectoptions)) $selectoption = (string)$field->$selectoptions;
else $selectoption = "" ;
$option = array();
$tmpOptions = str_replace( "\r", "" ,$selectoption);
if ($listoptions = explode("\n",$tmpOptions ) ) {
foreach ($listoptions as $key => $val) $option[] = JHTML::_('select.option',JText::_( $val ) , $val );
if (empty($values[$selectoptions])) {
$values[$selectoptions] ='';
}
if ($listoptions[0] == '' && $field->{'selectname'.$i}) {
$options_html .= '';// '.JText::_('VMCUSTOM_STOCKABLE_NO_OPTION') .'';
} else if ($listoptions[0] == '') {
$options_html .= '';
} else {
$options_html .= JHTML::_('select.genericlist', $option, $name.'['.$selectoptions.']','style="width:100px !important;float:none;"','text','value',$values[$selectoptions],false,true)."\n";
}
}
}
return $options_html;
}
/**
* @ idx plugin index
* @see components/com_virtuemart/helpers/vmCustomPlugin::onDisplayProductFE()
* @author Matt Lewis-Garner
* @author Patrick Kohl
*/
function plgVmOnDisplayProductVariantFE($field,&$row,&$group) {
// default return if it's not this plugin
if ($field->custom_element != $this->_name) return '';
$row++;
$this->parseCustomParams($field);
//if (!$childs = $this->getChilds($product_id) ) return ;
$this->stockhandle = VmConfig::get('stockhandle','none');
$html='
';
$customfield_id = array();
$selects = array();
$js = array();
// generate option with valid child results
foreach($field->child as $child_id => &$attribut) {
if ($attribut['is_variant']==1) {
unset ($attribut['is_variant']);
if ($stock = $this->getValideChild( $child_id)) {
$field->child[$child_id]['in_stock'] = $stock->product_in_stock - $stock->product_ordered;
// Availability Image
if ($field->child[$child_id]['in_stock'] < 1) {
if ($this->stockhandle == 'risetime' and VmConfig::get('rised_availability') and empty($stock->product_availability)) {
$field->child[$child_id]['product_availability'] = (file_exists(JPATH_BASE . DS . VmConfig::get('assets_general_path') . 'images/availability/' . VmConfig::get('rised_availability')))
? JHTML::image(JURI::root() . VmConfig::get('assets_general_path') . 'images/availability/' . VmConfig::get('rised_availability', '7d.gif'), VmConfig::get('rised_availability', '7d.gif'), array('class' => 'availability'))
: $field->child[$child_id]['product_availability'] = VmConfig::get('rised_availability');
} else if (!empty($stock->product_availability)) {
$field->child[$child_id]['product_availability'] = (file_exists(JPATH_BASE . DS . VmConfig::get('assets_general_path') . 'images/availability/' . $stock->product_availability))
? JHTML::image(JURI::root() . VmConfig::get('assets_general_path') . 'images/availability/' . $stock->product_availability, $stock->product_availability, array('class' => 'availability'))
: $field->child[$child_id]['product_availability'] = $stock->product_availability;
}
}
//$field->child[$child_id]['product_availability'] = $stock->product_availability;
if ($attribut['custom_price'])
$js[]= '"'.$child_id.'" :'.$attribut['custom_price'];
unset ($attribut['custom_price']);
foreach ($attribut as $key => $list) {
// if (!in_array($key,$selects)) {
// $selects[$key] = array() ;
// }
// if (!in_array($list , $selects[$key]) ) {
if (preg_match('/^selectoptions\d+$/', $key)) {
$selects[$key][$list] = $list ;
$attribut[$key] = array($list,JText::_($list));
}
// }
}
}
} else unset ($attribut);
}
$i = 1;
foreach ($selects as $keys =>$options) {
$selectname = 'selectname'.$i;
$listname = $field->$selectname;
if (!empty($listname)) {
$optionName = 'customPlugin['.$field->virtuemart_customfield_id.']['.$this->_name.']['.$keys.']';
$option = array();
$show_select = false;
foreach ($options as $key => $val) {
if (!empty($val)) {
if (1 == $i) {
$option[] = JHTML::_('select.option', $val, JText::_( $val ));
}
$show_select = true;
}
}
if ($show_select) {
$html .=''.JTEXT::_($listname).'';
$html .= JHTML::_('select.genericlist', $option,$optionName ,'class="attribute_list" style="width:100px !important;"','value','text',reset($options),'selectoptions'.$i,false)."
\n";
} else $html .='' ;
}
$i++;
}
static $stockablejs;
$group->display = $html.'
';
// preventing 2 x load javascript
if ($stockablejs) return;
$stockablejs = true ;
// TODO ONE PARAM IS MISSING
$document = JFactory::getDocument();
$document->addScriptDeclaration('
//child)). '\') ;
var stockhandle = "'.$this->stockhandle.'";
var selecteds = [];//all selected options
var selections = [];
var found_id=0;//found child id
var original=[];
var totalattribut = $(".attribute_list").length+1;
// get all initial select list values
$.each($(".attribute_list"), function(idx,selec) {
original[selec.name] = $.map($(this).find("option"), function(idx, opt) {
return [[ idx.value ,idx.text ]];
});
});
if ( $("#selectoptions1").length ) recalculate($("#selectoptions1"));
$(".attribute_list").unbind("change");
$(".attribute_list").change(function(){
recalculate($(this));
});
function recalculate(Opt){
var currentIndex = $(".attribute_list").index(Opt) +1;
// Find current values
for(var i=1; ispan").remove();
$(".addtocart-bar>div").remove();
$(".addtocart-bar>a.notify").remove();
$(".addtocart-bar").append(\'' . JText::_('COM_VIRTUEMART_CART_NOTIFY') . '\');
} else {
$(".addtocart-bar>span").remove();
$(".addtocart-bar>div").remove();
$(".addtocart-bar>a.notify").remove();
$(".addtocart-bar").append(\'\');
Virtuemart.product($("form.product"));
}
$(".availability").remove();
if ("risetime" == stockhandle && stockable[found_id].product_availability) {
$(".addtocart-area").after(\'\' + stockable[found_id].product_availability + \'
\');
}
// recalculate the price by found product child id;
formProduct = Opt.parents(".productdetails-view").find(".product");
virtuemart_product_id = formProduct.find(\'input[name="virtuemart_product_id[]"]\').val();
//formProduct.find("#selectedStockable").remove();
//formProduct.append(\'\');
formProduct.find(\'input[name*="customPlugin['.$field->virtuemart_customfield_id.']['.$this->_name.'][child_id]"]\').val(found_id);
//(\'\');
Virtuemart.setproducttype(formProduct,virtuemart_product_id);
}
function isChildValid(child_attrib, currentIndex) {
return_value = true;
for (var i = currentIndex; i > 0; i--) {
if (child_attrib["selectoptions"+i][0] != selections[i]) {
return_value = false;
}
}
return return_value;
}
function populateNextSelect(child_attrib, nextIndex) {
var nextSelect = $("#selectoptions"+nextIndex);
if (nextSelect.length > 0) {
if ($("#selectoptions"+nextIndex+" option:contains(" + child_attrib["selectoptions"+nextIndex][0] + ")").length == 0) {
nextSelect.append("");
}
if (1 == $("#selectoptions"+nextIndex+" option").length) {
$("#selectoptions"+nextIndex+" option").attr("selected","selected");
selections[nextIndex] = child_attrib["selectoptions"+nextIndex][0];
populateNextSelect(child_attrib, nextIndex+1);
}
}
}
});
//]]>
');
// 'custom_param['.$keys.']'
//dump($param);
//"is_variant":"1","attribute1":"Red","attribute2":"20 cm","attribute3":"10","attribute4":"10"
//echo $plgParam->get('custom_info');
// Here the plugin values
//$html =JTEXT::_($param['custom_name']) ;
//$html.=':
';
return true;
}
function plgVmOnDisplayProductFE( $product, &$idx,&$group){}
/**
* @see components/com_virtuemart/helpers/vmCustomPlugin::plgVmOnViewCartModule()
* @author Patrick Kohl
*/
function plgVmOnViewCartModule( $product, $row,&$html) {
if (!$plgParam = $this->GetPluginInCart($product)) return false ;
foreach ($plgParam as $k => $attributes) {
foreach ($attributes as $k => $attribute) {
if ($k =='child_id') continue;
$html .=' '.JText::_($attribute).' ';
}
}
return true;
}
/**
* @see components/com_virtuemart/helpers/vmCustomPlugin::plgVmOnViewCart()
* @author Patrick Kohl
*/
function plgVmOnViewCart($product, $row,&$html) {
if (!$plgParam = $this->GetPluginInCart($product)) return false ;
$html .= '';
foreach ($plgParam as $attributes) {
foreach ($attributes as $k => $attribute) {
if ($k =='child_id') continue;
$html .=' '.JText::_($attribute).' ';
}
} // $html .=''.$param->Morecomment.'';
$html.=$product->test_parameter.'
';
return true;
//vmdebug('stockable attributs',$plgParam);
}
/**
*
* vendor order display BE
*/
function plgVmDisplayInOrderBE($item, $row,&$html) {
if (empty($item->productCustom->custom_element) or $item->productCustom->custom_element != $this->_name) return '';
return $this->plgVmOnViewCart($item, $row,$html);
}
/**
*
* shopper order display FE
*/
function plgVmDisplayInOrderFE($item, $row,&$html) {
if (empty($item->productCustom->custom_element) or $item->productCustom->custom_element != $this->_name) return '';
return $this->plgVmOnViewCart($item, $row,$html);
}
function getChilds($child_id = null) {
if ($child_id) {
$db = JFactory::getDBO();
$q = 'SELECT CONCAT( `product_name`, " [' .JText::_('COM_VIRTUEMART_PRODUCT_SKU').'"," : ",`product_sku`,"]") as product_name,`virtuemart_product_id` as id, `product_in_stock` as stock FROM `#__virtuemart_products_'.VMLANG.'` as l '
. ' JOIN `#__virtuemart_products` AS p using (`virtuemart_product_id`)'
. 'WHERE `product_parent_id` ='.(int)$child_id ;
$db->setQuery($q);
$result = $db->loadObjectList();
if (!($result)) {
//JError::raiseWarning(500, $db->getErrorMsg());
return array();
} else return $result ;
} else {
return array();
}
}
function getFieldId($virtuemart_product_id, $child_id ) {
$db = JFactory::getDBO();
$q = 'SELECT cf.* FROM `#__virtuemart_product_customfields` as cf JOIN `#__virtuemart_customs` as c ON `c`.`virtuemart_custom_id` = cf.`virtuemart_custom_id` AND c.`field_type`="G"
WHERE cf.`virtuemart_product_id` ='.(int)$virtuemart_product_id.' and cf.custom_value='.(int)$child_id ;
$db->setQuery($q);
$result = $db->loadObject();
if (!($result)) {
//JError::raiseWarning(500, $db->getErrorMsg());
return false;
} else return $result ;
}
/**
* Get the child object for the given ID if it is valid for the config
* @author Matt Lewis-Garner
*/
function getValideChild($child_id ) {
$db = JFactory::getDBO();
$q = 'SELECT `product_sku`,`product_name`,`product_in_stock`,`product_ordered`,`product_availability` FROM `#__virtuemart_products` JOIN `#__virtuemart_products_'.VMLANG.'` as l using (`virtuemart_product_id`) WHERE `published`=1 and `virtuemart_product_id` ='.(int)$child_id ;
$db->setQuery($q);
$child = $db->loadObject();
if ($child) {
if ('disableit_children' === $this->stockhandle) {
$stock = $child->product_in_stock - $child->product_ordered ;
if ($stock>0)return $child ;
else return false ;
}
else return $child ;
}
return false ;
}
public function plgVmGetProductStockToUpdateByCustom(&$item, $pluginParam, $productCustom) {
if ($productCustom->custom_element !== $this->_name) return false ;
//vmdebug('$pluginParam',$pluginParam[$this->_name]);
$item->virtuemart_product_id = (int)$pluginParam[$this->_name]['child_id'];
return true ;
// echo $item[0]->virtuemart_product_id;jexit();
}
/**
* We must reimplement this triggers for joomla 1.7
* vmplugin triggers note by Max Milbers
*/
public function plgVmOnStoreInstallPluginTable($psType) {
// return $this->onStoreInstallPluginTable($psType);
}
function plgVmDeclarePluginParamsCustom($psType,$name,$id, &$data){
return $this->declarePluginParams('custom', $name, $id, $data);
}
function plgVmSetOnTablePluginParamsCustom($name, $id, &$table){
return $this->setOnTablePluginParams($name, $id, $table);
}
/**
* Custom triggers note by Max Milbers
*/
function plgVmOnDisplayEdit($virtuemart_custom_id,&$customPlugin){
return $this->onDisplayEditBECustom($virtuemart_custom_id,$customPlugin);
}
public function plgVmCalculateCustomVariant(&$product, &$productCustomsPrice,$selected){
if ($productCustomsPrice->custom_element != $this->_name) return false;
if (!$customPlugin = JRequest::getVar('customPlugin',0)) {
$customPlugin = json_decode($product->customPlugin,true);
}
$selected = $customPlugin[$productCustomsPrice->virtuemart_customfield_id]['stockable']['child_id'];
$param = json_decode($productCustomsPrice->custom_param,true);
if ($child = $this->getValideChild($selected)) {
if ($param['child'][$selected]['custom_price'] !=='') {
$productCustomsPrice->custom_price = (float)$param['child'][$selected]['custom_price'];
} else {
$db = JFactory::getDBO();
$db->setQuery('SELECT `product_price` FROM `#__virtuemart_product_prices` WHERE `virtuemart_product_id`="' . (int)$selected . '" ');
if ($price = $db->loadResult()) $product->product_price = (float)$price;
}
return $child;
}
else return false;
// find the selected child
}
public function plgVmOnAddToCart(&$product){
$customPlugin = JRequest::getVar('customPlugin',0);
if ($customPlugin) {
$db = JFactory::getDBO();
$query = 'SELECT C.* , field.*
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
WHERE `virtuemart_product_id` =' . $product->virtuemart_product_id.' and `custom_element`="'.$this->_name.'"';
$query .=' and is_cart_attribute = 1';
$db->setQuery($query);
$productCustomsPrice = $db->loadObject();
if (!$productCustomsPrice) return null;
// if ( !in_array($this->_name,$customPlugin[$productCustomsPrice->virtuemart_custom_id]) ) return false;
$selected = $customPlugin[$productCustomsPrice->virtuemart_customfield_id]['stockable']['child_id'];
if (!$child = $this->plgVmCalculateCustomVariant($product, $productCustomsPrice,$selected) ) return false;
if ($child->product_sku)
$product->product_sku = $child->product_sku;
if ($child->product_name)
$product->product_name = $child->product_name;
$product->product_in_stock = $child->product_in_stock;
}
}
public function plgVmDisplayInOrderCustom(&$html,$item, $param,$productCustom, $row ,$view='FE'){
$this->plgVmDisplayInOrderCustom($html,$item, $param,$productCustom, $row ,$view);
}
public function plgVmCreateOrderLinesCustom(&$html,$item,$productCustom, $row ){
// $this->createOrderLinesCustom($html,$item,$productCustom, $row );
}
}
// No closing tag