';
//We dont want to update always the stock, this would lead to wrong stocks, if the store has activity, while the vendor is editing the product
//$html .= '
'.vHtml::_ ('select.genericlist', $options, 'field[' . $row . '][customfield_value]', '', 'value', 'text', $field->customfield_value) .
'';
/* parent hint, this is a GROUP and should be G not P*/
case 'G':
return $field->customfield_value . '
';
break;
/* image */
case 'M':
if($field->is_list and $field->is_input){
$html = $priceInput . '
is list ';
$values = explode (';', $field->custom_value);
foreach($values as $val){
$html .= $this->displayCustomMedia ($val,'product');
}
return $html;
} else {
if(empty($field->custom_value)){
$q = 'SELECT `virtuemart_media_id` as value,`file_title` as text FROM `#__virtuemart_medias` WHERE `published`=1
AND (`virtuemart_vendor_id`= "' . $virtuemart_vendor_id . '" OR `shared` = "1")';
$db = vFactory::getDbo();
$db->setQuery ($q);
$options = $db->loadObjectList ();
} else {
$values = explode (';', $field->custom_value);
$mM = VmModel::getModel('media');
foreach ($values as $key => $val) {
$mM->setId($val);
$file = $mM->getFile();
$tmp = array('value' => $val, 'text' => $file->file_name);
$options[] = (object)$tmp;
}
}
return $priceInput . '
' . vmJsApi::jDate ($field->customfield_value, 'field[' . $row . '][customfield_value]', 'field_' . $row . '_customvalue') ;
break;
//'X'=>'COM_VIRTUEMART_CUSTOM_EDITOR',
case 'X':
// Not sure why this block is needed to get it to work when editing the customfield (the subsequent block works fine when creating it, ie. in JS)
$document = vFactory::getDocument();
if (get_class($document) == 'JDocumentHTML') {
$editor = vFactory::getEditor();
return '
'. $retValue;
break;
/* related category*/
case 'Z':
if (empty($field->customfield_value)) {
return '';
} // special case it's category ID !
$q = 'SELECT * FROM `#__virtuemart_categories_' . VmConfig::$vmlang . '` INNER JOIN `#__virtuemart_categories` AS p using (`virtuemart_category_id`) WHERE `virtuemart_category_id`= "' . (int)$field->customfield_value . '" ';
$db = vFactory::getDbo();
$db->setQuery ($q);
if ($category = $db->loadObject ()) {
$q = 'SELECT `virtuemart_media_id` FROM `#__virtuemart_category_medias` WHERE `virtuemart_category_id`= "' . (int)$field->customfield_value . '" ';
$db->setQuery ($q);
$thumb = '';
if ($media_id = $db->loadResult ()) {
$thumb = $this->displayCustomMedia ($media_id,'category');
}
$display = '';
$display .= ''.$thumb.'';
$display .= vHtml::link ('index.php?option=com_virtuemart&view=category&task=edit&cid=' . (int)$field->customfield_value, $category->category_name, array('title' => $category->category_name,'target'=>'blank')).'';
return $display;
}
else {
return 'no result $product_id = '.$product_id.' and '.$field->customfield_value;
}
/* related product*/
case 'R':
if (!$product_id) {
return '';
}
$pModel = VmModel::getModel('product');
$related = $pModel->getProduct((int)$field->customfield_value,TRUE,FALSE,FALSE,1);
if (!empty($related->virtuemart_media_id[0])) {
$thumb = $this->displayCustomMedia ($related->virtuemart_media_id[0]).' ';
} else {
$thumb = $this->displayCustomMedia (0).' ';
}
$display = '';
$display .= ''.$thumb.'';
$display .= vHtml::link ('index.php?option=com_virtuemart&view=product&task=edit&virtuemart_product_id=' . $related->virtuemart_product_id , $related->product_name, array('title' => $related->product_name,'target'=>'blank')).'';
return $display;
}
}
static $customfieldRenderer = true;
/**
* @author Max Milbers
* @param $product
* @param $customfield
*/
public static function displayProductCustomfieldFE (&$product, &$customfields) {
$session = vFactory::getSession ();
$virtuemart_category_id = $session->get ('vmlastvisitedcategoryid', 0, 'vm');
if(self::$customfieldRenderer){
self::$customfieldRenderer = false;
if (!class_exists ('VmView'))
require(VMPATH_SITE . DS . 'helpers' . DS . 'vmview.php');
$lPath = VmView::getVmSubLayoutPath ('customfield');
if($lPath){
require ($lPath);
} else {
vmdebug('displayProductCustomfieldFE layout not found customfield');
}
}
VirtueMartCustomFieldRenderer::renderCustomfieldsFE($product, $customfields, $virtuemart_category_id);
}
/**
* There are too many functions doing almost the same for my taste
* the results are sometimes slighty different and makes it hard to work with it, therefore here the function for future proxy use
*
*/
static public function displayProductCustomfieldSelected ($product, $html, $trigger) {
if(self::$customfieldRenderer){
self::$customfieldRenderer = false;
if (!class_exists ('VmView'))
require(VMPATH_SITE . DS . 'helpers' . DS . 'vmview.php');
$lPath = VmView::getVmSubLayoutPath ('customfield');
if($lPath){
require ($lPath);
} else {
vmdebug('displayProductCustomfieldFE layout not found customfield');
}
}
return VirtueMartCustomFieldRenderer::renderCustomfieldsCart($product, $html, $trigger);
}
/**
* TODO This is html and view stuff and MUST NOT be in the model, notice by Max
* render custom fields display cart module FE
*/
static public function CustomsFieldCartModDisplay ($product) {
return self::displayProductCustomfieldSelected ($product, '
', 'plgVmOnViewCartModule');
}
/**
* render custom fields display cart FE
*/
static public function CustomsFieldCartDisplay ($product) {
return self::displayProductCustomfieldSelected ($product, '
', 'plgVmOnViewCart');
}
/**
* render custom fields display order BE/FE
*/
static public function CustomsFieldOrderDisplay ($item, $view = 'FE', $absUrl = FALSE) {
if(empty($item->virtuemart_product_id)) return false;
if (!empty($item->product_attribute)) {
$item->customProductData = json_decode ($item->product_attribute, TRUE);
}
return self::displayProductCustomfieldSelected ($item, '
', 'plgVmDisplayInOrder' . $view);
}
static function displayCustomMedia ($media_id, $table = 'product', $width = false, $height = false, $absUrl = false) {
if (!class_exists ('TableMedias'))
require(VMPATH_ADMIN . DS . 'tables' . DS . 'medias.php');
$db = vFactory::getDbo ();
$data = new TableMedias($db);
$data->load ((int)$media_id);
if(!empty($data->file_type)){
$table = $data->file_type;
}
if (!class_exists ('VmMediaHandler'))
require(VMPATH_ADMIN . DS . 'helpers' . DS . 'mediahandler.php');
$media = VmMediaHandler::createMedia ($data, $table);
if(!$width) $width = VmConfig::get('img_width',90);
if(!$height) $height = VmConfig::get('img_height',90);
return $media->displayMediaThumb ('', FALSE, '', TRUE, TRUE, $absUrl, $width, $height);
}
static function _getCustomPrice($customPrice, $currency, $calculator) {
if ((float)$customPrice) {
$price = strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($customPrice)));
if ($customPrice >0) {
$price ="+".$price;
}
}
else {
$price = ($customPrice === '') ? '' : vmText::sprintf('COM_VIRTUEMART_CART_PRICE_FREE',$currency->getSymbol());
}
return $price;
}
/**
* @param $product
* @param $variants ids of the selected variants
* @return float
*/
public function calculateModificators(&$product) {
if (!isset($product->modificatorSum)){
$product->modificatorSum = 0.0;
if(!empty($product->customfields)) {
foreach( $product->customfields as $k => $productCustom ) {
$selected = -1;
if(isset($product->cart_item_id)) {
if(!class_exists( 'VirtueMartCart' ))
require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart();
//vmdebug('my $productCustom->customfield_price '.$productCustom->virtuemart_customfield_id,$cart->cartProductsData,$cart->cartProductsData[$product->cart_item_id]['customProductData'][$productCustom->virtuemart_custom_id]);
if(isset($cart->cartProductsData[$product->cart_item_id]['customProductData'][$productCustom->virtuemart_custom_id][$productCustom->virtuemart_customfield_id])) {
$selected = $cart->cartProductsData[$product->cart_item_id]['customProductData'][$productCustom->virtuemart_custom_id][$productCustom->virtuemart_customfield_id];
} else if(isset($cart->cartProductsData[$product->cart_item_id]['customProductData'][$productCustom->virtuemart_custom_id])) {
if($cart->cartProductsData[$product->cart_item_id]['customProductData'][$productCustom->virtuemart_custom_id] == $productCustom->virtuemart_customfield_id) {
$selected = $productCustom->virtuemart_customfield_id; //= 1;
}
}
//vmdebug('my $productCustom->customfield_price',$selected,$productCustom->virtuemart_custom_id,$productCustom->virtuemart_customfield_id,$cart->cartProductsData[$product->cart_item_id]['customProductData']);
} else {
$pluginFields = vRequest::getVar( 'customProductData', NULL );
if($pluginFields == NULL and isset($product->customPlugin)) {
$pluginFields = json_decode( $product->customPlugin, TRUE );
}
if(isset($pluginFields[$product->virtuemart_product_id][$productCustom->virtuemart_custom_id][$productCustom->virtuemart_customfield_id])) {
$selected = $pluginFields[$product->virtuemart_product_id][$productCustom->virtuemart_custom_id][$productCustom->virtuemart_customfield_id];
} else if(isset($pluginFields[$product->virtuemart_product_id][$productCustom->virtuemart_custom_id])) {
if($pluginFields[$product->virtuemart_product_id][$productCustom->virtuemart_custom_id] == $productCustom->virtuemart_customfield_id) {
$selected = 1;
}
}
}
if($selected === -1) {
continue;
}
if(!empty($productCustom) and $productCustom->field_type == 'E') {
if(!class_exists( 'vmCustomPlugin' )) require(VMPATH_PLUGINLIBS.DS.'vmcustomplugin.php');
vPluginHelper::importPlugin( 'vmcustom' );
$dispatcher = vDispatcher::getInstance();
$dispatcher->trigger( 'plgVmPrepareCartProduct', array(&$product, &$product->customfields[$k], $selected, &$product->modificatorSum) );
} else {
if($productCustom->customfield_price) {
//vmdebug('calculateModificators $productCustom->customfield_price ',$productCustom->customfield_price);
//TODO adding % and more We should use here $this->interpreteMathOp
$product->modificatorSum += $productCustom->customfield_price;
}
}
}
}
}
return $product->modificatorSum;
}
/** Save and delete from database
* all product custom_fields and xref
@ var $table : the xref table(eg. product,category ...)
@array $data : array of customfields
@int $id : The concerned id (eg. product_id)
*/
public function storeProductCustomfields($table,$datas, $id) {
vRequest::vmCheckToken('Invalid token in storeProductCustomfields');
//Sanitize id
$id = (int)$id;
//Table whitelist
$tableWhiteList = array('product','category','manufacturer');
if(!in_array($table,$tableWhiteList)) return false;
// Get old IDS
$db = vFactory::getDbo();
$db->setQuery( 'SELECT `virtuemart_customfield_id` FROM `#__virtuemart_'.$table.'_customfields` as `PC` WHERE `PC`.virtuemart_'.$table.'_id ='.$id );
$old_customfield_ids = $db->loadColumn();
if (array_key_exists('field', $datas)) {
foreach($datas['field'] as $key => $fields){
if(!empty($datas['field'][$key]['virtuemart_product_id']) and (int)$datas['field'][$key]['virtuemart_product_id']!=$id){
//aha the field is from the parent, what we do with it?
$fields['override'] = (int)$fields['override'];
$fields['disabler'] = (int)$fields['disabler'];
if($fields['override']!=0 or $fields['disabler']!=0){
//If it is set now as override, store it as clone, therefore set the virtuemart_customfield_id = 0
if($fields['override']!=0){
$fields['override'] = $fields['virtuemart_customfield_id'];
}
if($fields['disabler']!=0){
$fields['disabler'] = $fields['virtuemart_customfield_id'];
}
$fields['virtuemart_customfield_id'] = 0;
}
else {
//we do not store customfields inherited by the parent, therefore
$key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids );
if ($key !== false ){
unset( $old_customfield_ids[ $key ] );
}
continue;
}
}
if($fields['field_type']=='C'){
$cM = VmModel::getModel('custom');
$c = $cM->getCustom($fields['virtuemart_custom_id'],'');
if(!empty($c->sCustomId)){
$sCustId = $c->sCustomId;
$labels = array();
foreach($fields['selectoptions'] as $k => $option){
if($option['voption'] == 'clabels' and !empty($option['clabel'])){
$labels[$k] = $option['clabel'];
}
}
//for testing
foreach($fields['options'] as $prodId => $lvalue){
if($prodId == $id) continue;
$db->setQuery( 'SELECT `virtuemart_customfield_id` FROM `#__virtuemart_'.$table.'_customfields` as `PC` WHERE `PC`.virtuemart_'.$table.'_id ="'.$prodId.'" AND `virtuemart_custom_id`="'.$sCustId.'" ' );
$strIds = $db->loadColumn();
$i=0;
foreach($lvalue as $k=>$value) {
if(!empty($labels[$k])) {
$ts = array();
$ts['field_type'] = 'S';
$ts['virtuemart_product_id'] = $prodId;
$ts['virtuemart_custom_id'] = $sCustId;
if(isset($strIds[$i])){
$ts['virtuemart_customfield_id'] = $strIds[$i];
unset( $strIds[$i++] );
}
$ts['customfield_value'] = $value;
$tableCustomfields = $this->getTable($table.'_customfields');
$tableCustomfields->bindChecknStore($ts);
}
}
if(count($strIds)>0){
// delete old unused Customfields
$db->setQuery( 'DELETE FROM `#__virtuemart_'.$table.'_customfields` WHERE `virtuemart_customfield_id` in ("'.implode('","', $strIds ).'") ');
$db->execute();
}
}
}
}
$fields['virtuemart_'.$table.'_id'] = $id;
$tableCustomfields = $this->getTable($table.'_customfields');
$tableCustomfields->setPrimaryKey('virtuemart_product_id');
if (!empty($datas['customfield_params'][$key]) and !isset($datas['clone']) ) {
if (array_key_exists( $key,$datas['customfield_params'])) {
$fields = array_merge ((array)$fields, (array)$datas['customfield_params'][$key]);
}
}
$tableCustomfields->_xParams = 'customfield_params';
if(!class_exists('VirtueMartModelCustom')) require(VMPATH_ADMIN.DS.'models'.DS.'custom.php');
VirtueMartModelCustom::setParameterableByFieldType($tableCustomfields,$fields['field_type'],$fields['custom_element'],$fields['custom_jplugin_id']);
$tableCustomfields->bindChecknStore($fields);
$key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids );
if ($key !== false ) unset( $old_customfield_ids[ $key ] );
}
} else {
vmdebug('storeProductCustomfields nothing to store');
}
vmdebug('Delete $old_customfield_ids',$old_customfield_ids);
if ( count($old_customfield_ids) ) {
// delete old unused Customfields
$db->setQuery( 'DELETE FROM `#__virtuemart_'.$table.'_customfields` WHERE `virtuemart_customfield_id` in ("'.implode('","', $old_customfield_ids ).'") ');
$db->execute();
vmdebug('Deleted $old_customfield_ids',$old_customfield_ids);
}
vPluginHelper::importPlugin('vmcustom');
$dispatcher = vDispatcher::getInstance();
if (isset($datas['customfield_params']) and is_array($datas['customfield_params'])) {
foreach ($datas['customfield_params'] as $key => $plugin_param ) {
$dispatcher->trigger('plgVmOnStoreProduct', array($datas, $plugin_param ));
}
}
}
static public function setEditCustomHidden ($customfield, $i) {
if (!isset($customfield->virtuemart_customfield_id))
$customfield->virtuemart_customfield_id = '0';
if (!isset($customfield->virtuemart_product_id))
$customfield->virtuemart_product_id = '';
$html = '
';
$html .= '';
return $html;
}
private $_hidden = array();
public function addHidden ($name, $value = '') {
$this->_hidden[$name] = $value;
}
}
// pure php no closing tag