Advertisement
Guest User

Untitled

a guest
Apr 4th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 55.08 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * Description
  5.  *
  6.  * @package    VirtueMart
  7.  * @subpackage
  8.  * @author Max Milbers
  9.  * @link http://www.virtuemart.net
  10.  * @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved by the author.
  11.  * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  12.  * VirtueMart is free software. This version may have been modified pursuant
  13.  * to the GNU General Public License, and as distributed it includes or
  14.  * is derivative of works licensed under the GNU General Public License or
  15.  * other free or open source software licenses.
  16.  * @version $Id:$
  17.  */
  18.  
  19. // Check to ensure this file is included in Joomla!
  20. defined ('_JEXEC') or die('Restricted access');
  21.  
  22. if (!class_exists ('VmModel')) {
  23.     require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'vmmodel.php');
  24. }
  25.  
  26. /**
  27.  * Model for VirtueMart Customs Fields
  28.  *
  29.  * @package        VirtueMart
  30.  */
  31. class VirtueMartModelCustomfields extends VmModel {
  32.  
  33.     /**
  34.      * constructs a VmModel
  35.      * setMainTable defines the maintable of the model
  36.      *
  37.      * @author Max Milbers
  38.      */
  39. //  function __construct($modelName ='product') {
  40.     function __construct ($modelName = 'product') {
  41.  
  42.         parent::__construct ('virtuemart_customfield_id');
  43.         $this->setMainTable ('product_customfields');
  44.     }
  45.  
  46.  
  47.     /**
  48.      * Gets a single custom by virtuemart_customfield_id
  49.      *
  50.      * @param string $type
  51.      * @param string $mime mime type of custom, use for exampel image
  52.      * @return customobject
  53.      */
  54.     function getCustomfield () {
  55.  
  56.         $this->data = $this->getTable ('product_customfields');
  57.         $this->data->load ($this->_id);
  58.  
  59.         return $this;
  60.     }
  61.  
  62.     // **************************************************
  63.     // Custom FIELDS
  64.     //
  65.  
  66.     function getProductCustomsChilds ($childs) {
  67.  
  68.         $data = array();
  69.         foreach ($childs as $child) {
  70.             $query = 'SELECT C.* , field.*
  71.                     FROM `#__virtuemart_product_customfields` AS field
  72.                     LEFT JOIN `#__virtuemart_customs` AS C ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  73.                     WHERE `virtuemart_product_id` =' . (int)$child->virtuemart_product_id;
  74.             $query .= ' and C.field_type = "C" ';
  75.  
  76.             $this->_db->setQuery ($query);
  77.             $child->field = $this->_db->loadObject ();
  78.             $customfield = new stdClass();
  79.             $customfield->custom_value = $child->virtuemart_product_id;
  80.             $customfield->field_type = 'C';
  81.             $child->display = $this->displayProductCustomfieldFE ($child, $customfield);
  82.             if ($child->field) {
  83.                 $data[] = $child;
  84.             }
  85.         }
  86.         return $data;
  87.  
  88.     }
  89.  
  90.     public function getCustomParentTitle ($custom_parent_id) {
  91.  
  92.         $q = 'SELECT custom_title FROM `#__virtuemart_customs` WHERE virtuemart_custom_id =' . (int)$custom_parent_id;
  93.         $this->_db->setQuery ($q);
  94.         return $this->_db->loadResult ();
  95.     }
  96.  
  97.     /** @return autorized Types of data **/
  98.     function getField_types () {
  99.  
  100.         return array('S' => 'COM_VIRTUEMART_CUSTOM_STRING',
  101.                      'I' => 'COM_VIRTUEMART_CUSTOM_INT',
  102.                      'P' => 'COM_VIRTUEMART_CUSTOM_PARENT',
  103.                      'B' => 'COM_VIRTUEMART_CUSTOM_BOOL',
  104.                      'D' => 'COM_VIRTUEMART_DATE',
  105.                      'T' => 'COM_VIRTUEMART_TIME',
  106.                      'M' => 'COM_VIRTUEMART_IMAGE',
  107.                      'V' => 'COM_VIRTUEMART_CUSTOM_CART_VARIANT',
  108.                      'A' => 'COM_VIRTUEMART_CHILD_GENERIC_VARIANT',
  109.                      'X' => 'COM_VIRTUEMART_CUSTOM_EDITOR',
  110.                      'Y' => 'COM_VIRTUEMART_CUSTOM_TEXTAREA',
  111.                      'E' => 'COM_VIRTUEMART_CUSTOM_EXTENSION'
  112.         );
  113.  
  114.         // 'U'=>'COM_VIRTUEMART_CUSTOM_CART_USER_VARIANT',
  115.         // 'C'=>'COM_VIRTUEMART_CUSTOM_PRODUCT_CHILD',
  116.         // 'G'=>'COM_VIRTUEMART_CUSTOM_PRODUCT_CHILD_GROUP',
  117.         //          'R'=>'COM_VIRTUEMART_RELATED_PRODUCT',
  118.         //          'Z'=>'COM_VIRTUEMART_RELATED_CATEGORY',
  119.     }
  120.  
  121.     static function setParameterableByFieldType(&$table,$type=0){
  122.  
  123.         if($type===0) $type = $table->field_type;
  124.  
  125.         $varsToPush = self::getVarsToPush($type);
  126.  
  127.         if(!empty($varsToPush)){
  128.             $table->setParameterable('custom_param',$varsToPush,TRUE);
  129.         }
  130.  
  131.     }
  132.  
  133.     static function bindParameterableByFieldType(&$table,$type=0){
  134.  
  135.         if($type===0) $type = $table->field_type;
  136.  
  137.         $varsToPush = self::getVarsToPush($type);
  138.  
  139.         if(!empty($varsToPush)){
  140.             VmTable::bindParameterable($table,'custom_param',$varsToPush);
  141.         }
  142.  
  143.     }
  144.  
  145.  
  146.     static function getVarsToPush($type){
  147.  
  148.         $varsToPush = 0;
  149.         if($type=='A'){
  150.             $varsToPush = array(
  151.                 'withParent'        => array(0, 'int'),
  152.                 'parentOrderable'   => array(0, 'int')
  153.             );
  154.         }
  155.         return $varsToPush;
  156.     }
  157.  
  158.     private $_hidden = array();
  159.  
  160.     /**
  161.      * Use this to adjust the hidden fields of the displaycustomHandler to your form
  162.      *
  163.      * @author Max Milbers
  164.      * @param string $name for exampel view
  165.      * @param string $value for exampel custom
  166.      */
  167.     public function addHidden ($name, $value = '') {
  168.  
  169.         $this->_hidden[$name] = $value;
  170.     }
  171.  
  172.     /**
  173.      * Adds the hidden fields which are needed for the form in every case
  174.      *
  175.      * @author Max Milbers
  176.      * OBSELTE ?
  177.      */
  178.     private function addHiddenByType ($datas) {
  179.  
  180.         $this->addHidden ('virtuemart_custom_id', $datas->virtuemart_custom_id);
  181.         $this->addHidden ('option', 'com_virtuemart');
  182.  
  183.     }
  184.  
  185.     /**
  186.      * Displays a possibility to select created custom
  187.      *
  188.      * @author Max Milbers
  189.      * @author Patrick Kohl
  190.      */
  191.     public function displayCustomSelection () {
  192.  
  193.         $customslist = $this->getCustomsList ();
  194.         if (isset($this->virtuemart_custom_id)) {
  195.             $value = $this->virtuemart_custom_id;
  196.         }
  197.         else {
  198.             $value = JRequest::getInt ('custom_parent_id', 0);
  199.         }
  200.         return VmHTML::row ('select', 'COM_VIRTUEMART_CUSTOM_PARENT', 'custom_parent_id', $customslist, $value);
  201.     }
  202.  
  203.     /**
  204.      * Retrieve a list of layouts from the default and chosen templates directory.
  205.      *
  206.      * We may use here the getCustoms function of the custom model or write something simular
  207.      *
  208.      * @author Max Milbers
  209.      * @param name of the view
  210.      * @return object List of flypage objects
  211.      */
  212.     function getCustomsList ($publishedOnly = FALSE) {
  213.  
  214.         $vendorId = 1;
  215.         // get custom parents
  216.         $q = 'SELECT virtuemart_custom_id as value ,custom_title as text FROM `#__virtuemart_customs` where custom_parent_id=0
  217.             AND field_type <> "R" AND field_type <> "Z" ';
  218.         if ($publishedOnly) {
  219.             $q .= 'AND `published`=1';
  220.         }
  221.         if ($ID = JRequest::getInt ('virtuemart_custom_id', 0)) {
  222.             $q .= ' and `virtuemart_custom_id`!=' . (int)$ID;
  223.         }
  224.         //if (isset($this->virtuemart_custom_id)) $q.=' and virtuemart_custom_id !='.$this->virtuemart_custom_id;
  225.         $this->_db->setQuery ($q);
  226.         //      $result = $this->_db->loadAssocList();
  227.         $result = $this->_db->loadObjectList ();
  228.  
  229.         $errMsg = $this->_db->getErrorMsg ();
  230.         $errs = $this->_db->getErrors ();
  231.  
  232.         if (!empty($errMsg)) {
  233.             $app = JFactory::getApplication ();
  234.             $errNum = $this->_db->getErrorNum ();
  235.             $app->enqueueMessage ('SQL-Error: ' . $errNum . ' ' . $errMsg);
  236.         }
  237.  
  238.         if ($errs) {
  239.             $app = JFactory::getApplication ();
  240.             foreach ($errs as $err) {
  241.                 $app->enqueueMessage ($err);
  242.             }
  243.         }
  244.  
  245.         return $result;
  246.     }
  247.  
  248.     /**
  249.      * This displays a custom handler.
  250.      *
  251.      * @param string $html atttributes, Just for displaying the fullsized image
  252.      */
  253.     public function displayCustomFields ($datas) {
  254.  
  255.         $identify = ''; // ':'.$this->virtuemart_custom_id;
  256.         if (!class_exists ('VmHTML')) {
  257.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'html.php');
  258.         }
  259.         if ($datas->field_type) {
  260.             $this->addHidden ('field_type', $datas->field_type);
  261.         }
  262.         $this->addHiddenByType ($datas);
  263.  
  264.         //$html = '<div id="custom_title">'.$datas->custom_title.'</div>';
  265.         $html = "";
  266.         //$html = ' <table class="admintable"> ';
  267.  
  268.         if (!class_exists ('Permissions')) {
  269.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'permissions.php');
  270.         }
  271.         if (!Permissions::getInstance ()->check ('admin')) {
  272.             $readonly = 'readonly';
  273.         }
  274.         else {
  275.             $readonly = '';
  276.         }
  277.         // only input when not set else display
  278.         if ($datas->field_type) {
  279.             $html .= VmHTML::row ('value', 'COM_VIRTUEMART_CUSTOM_FIELD_TYPE', $datas->field_types[$datas->field_type]);
  280.         }
  281.         else {
  282.             $html .= VmHTML::row ('select', 'COM_VIRTUEMART_CUSTOM_FIELD_TYPE', 'field_type', $this->getOptions ($datas->field_types), $datas->field_type, VmHTML::validate ('R'));
  283.         }
  284.         $html .= VmHTML::row ('input', 'COM_VIRTUEMART_TITLE', 'custom_title', $datas->custom_title, VmHTML::validate ('S'));
  285.         $html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_PUBLISHED', 'published', $datas->published);
  286.         $html .= VmHTML::row ('select', 'COM_VIRTUEMART_CUSTOM_PARENT', 'custom_parent_id', $this->getParentList ($datas->virtuemart_custom_id), $datas->custom_parent_id, '');
  287.         $html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_CART_ATTRIBUTE', 'is_cart_attribute', $datas->is_cart_attribute);
  288.         $html .= VmHTML::row ('input', 'COM_VIRTUEMART_DESCRIPTION', 'custom_field_desc', $datas->custom_field_desc);
  289.         // change input by type
  290.         $html .= VmHTML::row ('input', 'COM_VIRTUEMART_DEFAULT', 'custom_value', $datas->custom_value);
  291.         $html .= VmHTML::row ('input', 'COM_VIRTUEMART_CUSTOM_TIP', 'custom_tip', $datas->custom_tip);
  292.         $html .= VmHTML::row ('input', 'COM_VIRTUEMART_CUSTOM_LAYOUT_POS', 'layout_pos', $datas->layout_pos);
  293.         //$html .= VmHTML::row('booleanlist','COM_VIRTUEMART_CUSTOM_PARENT','custom_parent_id',$this->getCustomsList(),  $datas->custom_parent_id,'');
  294.         $html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_ADMIN_ONLY', 'admin_only', $datas->admin_only);
  295.         $html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_LIST', 'is_list', $datas->is_list);
  296.         $html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_HIDDEN', 'is_hidden', $datas->is_hidden);
  297.  
  298.         // $html .= '</table>';  removed
  299.         $html .= VmHTML::inputHidden ($this->_hidden);
  300.  
  301.         return $html;
  302.     }
  303.  
  304.     /**
  305.      * child classes can add their own options and you can get them with this function
  306.      *
  307.      * @param array $optionsarray
  308.      */
  309.     private function getOptions ($field_types) {
  310.  
  311.         $options = array();
  312.         foreach ($field_types as $optionName=> $langkey) {
  313.             $options[] = JHTML::_ ('select.option', $optionName, JText::_ ($langkey));
  314.         }
  315.         return $options;
  316.     }
  317.  
  318.     /**
  319.      * Just for creating simpel rows
  320.      *
  321.      * @author Max Milbers
  322.      * @param string $descr
  323.      * @param string $name
  324.      */
  325.     private function displayRow ($descr, $name, $readonly = '') {
  326.  
  327.         $html = '<tr>
  328.         <td class="labelcell">' . JText::_ ($descr) . '</td>
  329.         <td> <input type="text" ' . $readonly . 'class="inputbox ' . $readonly . '" name="' . $name . '" size="70" value="' . $this->$name . '" /></td>
  330.     </tr>';
  331.         return $html;
  332.     }
  333.  
  334.     /**
  335.      *
  336.      * Enter description here ...
  337.      *
  338.      * @param unknown_type $excludedId
  339.      * @return unknown|multitype:
  340.      */
  341.     function getParentList ($excludedId = 0) {
  342.  
  343.         $this->_db->setQuery (' SELECT virtuemart_custom_id as value,custom_title as text FROM `#__virtuemart_customs` WHERE `field_type` ="P" and virtuemart_custom_id!=' . $excludedId);
  344.         if ($results = $this->_db->loadObjectList ()) {
  345.             return $results;
  346.         }
  347.         else {
  348.             return array();
  349.         }
  350.     }
  351.  
  352.     /**
  353.      *
  354.      * Enter description here ...
  355.      */
  356.     function getProductChildCustomRelation () {
  357.  
  358.         $this->_db->setQuery (' SELECT virtuemart_custom_id as value,custom_title as text FROM `#__virtuemart_customs` WHERE `field_type` ="C"');
  359.         if ($results = $this->_db->loadObjectList ()) {
  360.             return $results;
  361.         }
  362.         else {
  363.             return array();
  364.         }
  365.     }
  366.  
  367.     /**
  368.      *
  369.      * Enter description here ...
  370.      *
  371.      * @param unknown_type $product_id
  372.      * @return unknown
  373.      */
  374.     function getProductChildCustom ($product_id) {
  375.  
  376.         $this->_db->setQuery (' SELECT `virtuemart_custom_id`,`custom_value` FROM `#__virtuemart_product_customfields` WHERE  `virtuemart_product_id` =' . (int)$product_id);
  377.         if ($childcustom = $this->_db->loadObject ()) {
  378.             return $childcustom;
  379.         }
  380.         else {
  381.             $childcustom->virtuemart_custom_id = 0;
  382.             $childcustom->custom_value = '';
  383.             return $childcustom;
  384.         }
  385.     }
  386.  
  387.     /**
  388.      *
  389.      * Enter description here ...
  390.      *
  391.      * @param unknown_type $product_id
  392.      * @return string|Ambigous <string, mixed, multitype:>
  393.      */
  394.     function getProductParentRelation ($product_id) {
  395.  
  396.         $this->_db->setQuery (' SELECT `custom_value` FROM `#__virtuemart_product_customfields` WHERE  `virtuemart_product_id` =' . (int)$product_id);
  397.         if ($childcustom = $this->_db->loadResult ()) {
  398.             return '(' . $childcustom . ')';
  399.         }
  400.         else {
  401.             return JText::_ ('COM_VIRTUEMART_CUSTOM_NO_PARENT_RELATION');
  402.         }
  403.     }
  404.  
  405.     /**
  406.      * AUthor Kohl Patrick
  407.      * Load all custom fields for a Single product
  408.      * return custom fields value and definition
  409.      */
  410.     public function getproductCustomslist ($virtuemart_product_id, $parent_id = NULL) {
  411.  
  412.         $query = 'SELECT C.`virtuemart_custom_id` , `custom_element`, `custom_jplugin_id`, `custom_params`, `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_cart_attribute` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`,field.`custom_param`,field.`custom_price`,field.`ordering`
  413.             FROM `#__virtuemart_customs` AS C
  414.             LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  415.             Where `virtuemart_product_id` =' . $virtuemart_product_id . ' order by field.`ordering` ASC';
  416.         $this->_db->setQuery ($query);
  417.         $productCustoms = $this->_db->loadObjectList ();
  418.         //if (!$productCustoms ) return array();
  419.         if (!$productCustoms) {
  420.             return;
  421.         }
  422.         $row = 0;
  423.         foreach ($productCustoms as $field) {
  424.             if ($parent_id) {
  425.                 $field->custom_value = "";
  426.                 $field->virtuemart_customfield_id = "";
  427.                 $field->custom_param = NULL;
  428.                 $virtuemart_product_id = $parent_id;
  429.             }
  430.             if ($field->field_type == 'E') {
  431.  
  432.                 JPluginHelper::importPlugin ('vmcustom');
  433.                 $dispatcher = JDispatcher::getInstance ();
  434.                 $retValue = $dispatcher->trigger ('plgVmDeclarePluginParams', array('custom', $field->custom_element, $field->custom_jplugin_id, $field));
  435.  
  436.             }else {
  437.                 VirtueMartModelCustomfields::bindParameterableByFieldType($field);
  438.             }
  439.             //vmdebug('fields',$field);
  440.             $field->display = $this->displayProductCustomfieldBE ($field, $virtuemart_product_id, $row); //custom_param without S !!!
  441.             $row++;
  442.         }
  443.         return $productCustoms;
  444.     }
  445.  
  446.     /* Save and delete from database
  447.     * all product custom_fields and xref
  448.     @ var   $table  : the xref table(eg. product,category ...)
  449.     @array $data    : array of customfields
  450.     @int     $id        : The concerned id (eg. product_id)
  451.     */
  452.     public function storeProductCustomfields($table,$datas, $id) {
  453.  
  454.         //vmdebug('storeProductCustomfields',$datas);
  455.         JRequest::checkToken() or jexit( 'Invalid Token, in store customfields');
  456.         //Sanitize id
  457.         $id = (int)$id;
  458.  
  459.         //Table whitelist
  460.         $tableWhiteList = array('product','category','manufacturer');
  461.         if(!in_array($table,$tableWhiteList)) return false;
  462.  
  463.  
  464.         // Get old IDS
  465.         $this->_db->setQuery( 'SELECT `virtuemart_customfield_id` FROM `#__virtuemart_'.$table.'_customfields` as `PC` WHERE `PC`.virtuemart_'.$table.'_id ='.$id );
  466.         $old_customfield_ids = $this->_db->loadResultArray();
  467.  
  468.  
  469.         if (isset ( $datas['custom_param'] )) $params = true ;
  470.         else $params = false ;
  471.         if (array_key_exists('field', $datas)) {
  472.             //vmdebug('datas save',$datas);
  473.             $customfieldIds = array();
  474.  
  475.  
  476.             foreach($datas['field'] as $key => $fields){
  477.                 $fields['virtuemart_'.$table.'_id'] =$id;
  478.                 $tableCustomfields = $this->getTable($table.'_customfields');
  479.                 $tableCustomfields->setPrimaryKey('virtuemart_product_id');
  480.  
  481.                 if (!empty($datas['custom_param'][$key]) and !isset($datas['clone']) ) {
  482.                     if (array_key_exists( $key,$datas['custom_param'])) {
  483.                         $fields['custom_param'] = json_encode($datas['custom_param'][$key]);
  484.                     }
  485.                 }
  486.  
  487.                 VirtueMartModelCustomfields::setParameterableByFieldType($tableCustomfields,$fields['field_type']);
  488.                 if(!isset($datas['clone'])){
  489.                     VirtueMartModelCustomfields::bindParameterableByFieldType($tableCustomfields,$fields['field_type']);
  490.                 }
  491.  
  492.                 $tableCustomfields->bindChecknStore($fields);
  493.                 $errors = $tableCustomfields->getErrors();
  494.  
  495.                 foreach($errors as $error){
  496.                     vmError($error);
  497.                 }
  498.                 $key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids );
  499.                 if ($key !== false ) unset( $old_customfield_ids[ $key ] );
  500. //              vmdebug('datas clone',$old_customfield_ids,$fields);
  501.             }
  502.  
  503.         }
  504.  
  505.         if ( count($old_customfield_ids) ) {
  506.             // delete old unused Customfields
  507.             $this->_db->setQuery( 'DELETE FROM `#__virtuemart_'.$table.'_customfields` WHERE `virtuemart_customfield_id` in ("'.implode('","', $old_customfield_ids ).'") ');
  508.             $this->_db->query();
  509.         }
  510.  
  511.  
  512.         JPluginHelper::importPlugin('vmcustom');
  513.         $dispatcher = JDispatcher::getInstance();
  514.         if (isset($datas['plugin_param']) and is_array($datas['plugin_param'])) {
  515.             foreach ($datas['plugin_param'] as $key => $plugin_param ) {
  516.                 $dispatcher->trigger('plgVmOnStoreProduct', array($datas, $plugin_param ));
  517.             }
  518.         }
  519.  
  520.     }
  521.  
  522.  
  523.     /**
  524.      * Formatting admin display by roles
  525.      * input Types for product only !
  526.      * $field->is_cart_attribute if can have a price
  527.      */
  528.     public function displayProductCustomfieldBE ($field, $product_id, $row) {
  529.  
  530.         $field->custom_value = empty($field->custom_value) ? $field->value : $field->custom_value;
  531.  
  532.         if ($field->is_cart_attribute) {
  533.             if(!class_exists('VirtueMartModelVendor')) require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'vendor.php');
  534.             if(!class_exists('VirtueMartModelCurrency')) require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'currency.php');
  535.             $vendor_model = VmModel::getModel('vendor');
  536.             $vendor_model->setId(1);
  537.             $vendor = $vendor_model->getVendor();
  538.             $currency_model = VmModel::getModel('currency');
  539.             $vendor_currency = $currency_model->getCurrency($vendor->vendor_currency);
  540.             $priceInput = '<span style="white-space: nowrap;"><input type="text" size="12" style="text-align:right;" value="' . (isset($field->custom_price) ?  $field->custom_price : '0') . '" name="field[' . $row . '][custom_price]" /> '.$vendor_currency->currency_symbol."</span>";
  541.         }
  542.         else {
  543.             $priceInput = ' ';
  544.         }
  545.  
  546.         if ($field->is_list) {
  547.             $options = array();
  548.             $values = explode (';', $field->value);
  549.  
  550.             foreach ($values as $key => $val) {
  551.                 $options[] = array('value' => $val, 'text' => $val);
  552.             }
  553.  
  554.                 $currentValue = $field->custom_value;
  555.             return JHTML::_ ('select.genericlist', $options, 'field[' . $row . '][custom_value]', null, 'value', 'text', $currentValue) . '</td><td>' . $priceInput;
  556.         }
  557.         else {
  558.  
  559.             switch ($field->field_type) {
  560.  
  561.                 case 'A':
  562.                     //vmdebug('displayProductCustomfieldBE $field',$field);
  563.                     if(!isset($field->withParent)) $field->withParent = 0;
  564.                     if(!isset($field->parentOrderable)) $field->parentOrderable = 0;
  565.                     //vmdebug('displayProductCustomfieldFE',$field);
  566.                     if (!class_exists('VmHTML')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'html.php');
  567.                     $html = JText::_('COM_VIRTUEMART_CUSTOM_WP').VmHTML::checkbox('field[' . $row . '][withParent]',$field->withParent,1,0,'').'<br />';
  568.                     $html .= JText::_('COM_VIRTUEMART_CUSTOM_PO').VmHTML::checkbox('field[' . $row . '][parentOrderable]',$field->parentOrderable,1,0,'');
  569.  
  570.                     $options = array();
  571. //                  $options[] = array( 'value' => 'product_name' ,'text' =>JText::_('COM_VIRTUEMART_PRODUCT_FORM_NAME')); Is anyway displayed there
  572.                     $options[] = array('value' => 'product_sku', 'text' => JText::_ ('COM_VIRTUEMART_PRODUCT_SKU'));
  573.                     $options[] = array('value' => 'slug', 'text' => JText::_ ('COM_VIRTUEMART_PRODUCT_ALIAS'));
  574.                     $options[] = array('value' => 'product_length', 'text' => JText::_ ('COM_VIRTUEMART_PRODUCT_LENGTH'));
  575.                     $options[] = array('value' => 'product_width', 'text' => JText::_ ('COM_VIRTUEMART_PRODUCT_WIDTH'));
  576.                     $options[] = array('value' => 'product_height', 'text' => JText::_ ('COM_VIRTUEMART_PRODUCT_HEIGHT'));
  577.                     $options[] = array('value' => 'product_weight', 'text' => JText::_ ('COM_VIRTUEMART_PRODUCT_WEIGHT'));
  578.  
  579.                     $html .= JHTML::_ ('select.genericlist', $options, 'field[' . $row . '][custom_value]', '', 'value', 'text', $field->custom_value) . '</td><td>' . $priceInput;
  580.                     return $html;
  581.                     //                  return 'Automatic Childvariant creation (later you can choose here attributes to show, now product name) </td><td>';
  582.                     break;
  583.                 // variants
  584.                 case 'V':
  585.                     return '<input type="text" value="' . $field->custom_value . '" name="field[' . $row . '][custom_value]" /></td><td>' . $priceInput;
  586.                     break;
  587.                 /*
  588.                                      * Stockable (group of) child variants
  589.                                  * Special type setted by the plugin
  590.                                  */
  591.                 case 'G':
  592.                     return;
  593.                     break;
  594.                 /*Extended by plugin*/
  595.                 case 'E':
  596.  
  597.                     $html = '<input type="hidden" value="' . $field->value . '" name="field[' . $row . '][custom_value]" />';
  598.                     if (!class_exists ('vmCustomPlugin')) {
  599.                         require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
  600.                     }
  601.                     JPluginHelper::importPlugin ('vmcustom', $field->custom_element);
  602.                     $dispatcher = JDispatcher::getInstance ();
  603.                     $retValue = '';
  604.                     $dispatcher->trigger ('plgVmOnProductEdit', array($field, $product_id, &$row, &$retValue));
  605.  
  606.                     return $html . $retValue  . '</td><td>'. $priceInput;
  607.                     break;
  608.                 case 'D':
  609.                     return vmJsApi::jDate ($field->custom_value, 'field[' . $row . '][custom_value]', 'field_' . $row . '_customvalue') .'</td><td>'. $priceInput;
  610.                     break;
  611.                 case 'T':
  612.                     //TODO Patrick
  613.                     return '<input type="text" value="' . $field->custom_value . '" name="field[' . $row . '][custom_value]" /></td><td>' . $priceInput;
  614.                     break;
  615.                 /* string or integer */
  616.                 case 'S':
  617.                 case 'I':
  618.                     return '<input type="text" value="' . $field->custom_value . '" name="field[' . $row . '][custom_value]" /></td><td>' . $priceInput;
  619.                     break;
  620.                 //'X'=>'COM_VIRTUEMART_CUSTOM_EDITOR',
  621.                 case 'X':
  622.                     return '<textarea class="mceInsertContentNew" name="field[' . $row . '][custom_value]" id="field-' . $row . '-custom_value">' . $field->custom_value . '</textarea>
  623.                         <script type="text/javascript">// Creates a new editor instance
  624.                             tinymce.execCommand("mceAddControl",true,"field-' . $row . '-custom_value")
  625.                         </script></td><td>' . $priceInput;
  626.                     //return '<input type="text" value="'.$field->custom_value.'" name="field['.$row.'][custom_value]" /></td><td>'.$priceInput;
  627.                     break;
  628.                 //'Y'=>'COM_VIRTUEMART_CUSTOM_TEXTAREA'
  629.                 case 'Y':
  630.                     return '<textarea id="field[' . $row . '][custom_value]" name="field[' . $row . '][custom_value]" class="inputbox" cols=80 rows=50 >' . $field->custom_value . '</textarea></td><td>' . $priceInput;
  631.                     //return '<input type="text" value="'.$field->custom_value.'" name="field['.$row.'][custom_value]" /></td><td>'.$priceInput;
  632.                     break;
  633.  
  634.                 case 'editorta':
  635.                     jimport ('joomla.html.editor');
  636.                     $editor = JFactory::getEditor ();
  637.                     //TODO This is wrong!
  638.                     $_return['fields'][$_fld->name]['formcode'] = $editor->display ($_prefix . $_fld->name, $_return['fields'][$_fld->name]['value'], 300, 150, $_fld->cols, $_fld->rows);
  639.                     break;
  640.                 /* bool */
  641.                 case 'B':
  642.                     return JHTML::_ ('select.booleanlist', 'field[' . $row . '][custom_value]', 'class="inputbox"', $field->custom_value) . '</td><td>' . $priceInput;
  643.                     break;
  644.                 /* parent */
  645.                 case 'P':
  646.                     return $field->custom_value . '<input type="hidden" value="' . $field->custom_value . '" name="field[' . $row . '][custom_value]" /></td><td>';
  647.                     break;
  648.                 /* related category*/
  649.                 case 'Z':
  650.                     if (!$field->custom_value) {
  651.                         return '';
  652.                     } // special case it's category ID !
  653.                     $q = 'SELECT * FROM `#__virtuemart_categories_' . VMLANG . '` JOIN `#__virtuemart_categories` AS p using (`virtuemart_category_id`) WHERE `published`=1 AND `virtuemart_category_id`= "' . (int)$field->custom_value . '" ';
  654.                     $this->_db->setQuery ($q);
  655.                     //echo $this->_db->_sql;
  656.                     if ($category = $this->_db->loadObject ()) {
  657.                         $q = 'SELECT `virtuemart_media_id` FROM `#__virtuemart_category_medias` WHERE `virtuemart_category_id`= "' . (int)$field->custom_value . '" ';
  658.                         $this->_db->setQuery ($q);
  659.                         $thumb = '';
  660.                         if ($media_id = $this->_db->loadResult ()) {
  661.                             $thumb = $this->displayCustomMedia ($media_id,'category');
  662.                         }
  663.                         $display = '<input type="hidden" value="' . $field->custom_value . '" name="field[' . $row . '][custom_value]" />';
  664.                         return $display . JHTML::link (JRoute::_ ('index.php?option=com_virtuemart&view=category&task=edit&virtuemart_category_id=' . (int)$field->custom_value), $thumb . ' ' . $category->category_name, array('title' => $category->category_name)) . $display;
  665.                     }
  666.                     else {
  667.                         return 'no result';
  668.                     }
  669.                 /* related product*/
  670.                 case 'R':
  671.                     if (!$field->custom_value) {
  672.                         return '';
  673.                     }
  674.                     $q = 'SELECT `product_name`,`product_sku`,`product_s_desc` FROM `#__virtuemart_products_' . VMLANG . '` as l JOIN `#__virtuemart_products` AS p using (`virtuemart_product_id`) WHERE `virtuemart_product_id`=' . (int)$field->custom_value;
  675.                     $this->_db->setQuery ($q);
  676.                     $related = $this->_db->loadObject ();
  677.                     $display = $related->product_name . '(' . $related->product_sku . ')';
  678.                     $display = '<input type="hidden" value="' . $field->custom_value . '" name="field[' . $row . '][custom_value]" />';
  679.  
  680.                     $q = 'SELECT `virtuemart_media_id` FROM `#__virtuemart_product_medias`WHERE `virtuemart_product_id`= "' . (int)$field->custom_value . '" AND (`ordering` = 0 OR `ordering` = 1)';
  681.                     $this->_db->setQuery ($q);
  682.                     $thumb = '';
  683.                     if ($media_id = $this->_db->loadResult ()) {
  684.                         $thumb = $this->displayCustomMedia ($media_id);
  685.                     }
  686.                     $title= $related->product_s_desc?  $related->product_s_desc :'';
  687.                     return $display . JHTML::link (JRoute::_ ('index.php?option=com_virtuemart&view=product&task=edit&virtuemart_product_id=' . $field->custom_value), $thumb . '<br /> ' . $related->product_name, array('title' => $title));
  688.                     break;
  689.                 /* image */
  690.                 case 'M':
  691.                     if (empty($product)) {
  692.                         $vendorId = 1;
  693.                     }
  694.                     else {
  695.                         $vendorId = $product->virtuemart_vendor_id;
  696.                     }
  697.                     $q = 'SELECT `virtuemart_media_id` as value,`file_title` as text FROM `#__virtuemart_medias` WHERE `published`=1
  698.                     AND (`virtuemart_vendor_id`= "' . $vendorId . '" OR `shared` = "1")';
  699.                     $this->_db->setQuery ($q);
  700.                     $options = $this->_db->loadObjectList ();
  701.                     return JHTML::_ ('select.genericlist', $options, 'field[' . $row . '][custom_value]', '', 'value', 'text', $field->custom_value) . '</td><td>' . $priceInput;
  702.                     break;
  703.                 /* Child product Group */
  704.                 case 'G':
  705.                     break;
  706.                 /* Child product */
  707.                 /*              case 'C':
  708.                     if (empty($product)){
  709.                    $virtuemart_product_id = JRequest::getInt('virtuemart_product_id', 0);
  710.                    } else {
  711.                    $virtuemart_product_id = $product->virtuemart_product_id;
  712.                    }
  713.                    $html = '';
  714.                    $q='SELECT concat(`product_sku`,":",`product_name`) as text ,`virtuemart_product_id`,`product_in_stock` FROM `#__virtuemart_products` WHERE `published`=1
  715.                    AND `virtuemart_product_id`= "'.$field->custom_value.'"';
  716.                    //$db->setQuery(' SELECT virtuemart_product_id, product_name FROM `#__virtuemart_products` WHERE `product_parent_id` ='.(int)$product_id);
  717.                    $this->_db->setQuery($q);
  718.                    if ($child = $this->_db->loadObject()) {
  719.                    $html .= JHTML::link ( JRoute::_ ( 'index.php?option=com_virtuemart&view=product&task=edit&virtuemart_product_id='.$field->custom_value), $child->text.' ('.$field->custom_value.')', array ('title' => $child->text ));
  720.                    $html .= ' '.JText::_('COM_VIRTUEMART_PRODUCT_FORM_IN_STOCK').':'.$child->product_in_stock ;
  721.                    $html .= '<input type="hidden" value="'.$child->virtuemart_product_id.'" name="field['.$row.'][custom_value]" /></div><div>'.$priceInput;
  722.                    return $html;
  723.                    //                   return '<input type="text" value="'.$field->custom_value.'" name="field['.$row.'][custom_value]" />';
  724.                    }
  725.                    else return JText::_('COM_VIRTUEMART_CUSTOM_NO_CHILD_PRODUCT');
  726.                    break;*/
  727.             }
  728.  
  729.         }
  730.     }
  731.  
  732.     public function getProductCustomsField ($product) {
  733.  
  734.         $query = 'SELECT C.`virtuemart_custom_id` , `custom_element`, `custom_params`, `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden`, `layout_pos`, C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
  735.             FROM `#__virtuemart_customs` AS C
  736.             LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  737.             Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` != "G" and `field_type` != "R" and `field_type` != "Z"';
  738.         $query .= ' and is_cart_attribute = 0 order by field.`ordering`,virtuemart_custom_id';
  739.         $this->_db->setQuery ($query);
  740.         if ($productCustoms = $this->_db->loadObjectList ()) {
  741.  
  742.             $row = 0;
  743.             if (!class_exists ('vmCustomPlugin')) {
  744.                 require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
  745.             }
  746.             foreach ($productCustoms as $field) {
  747.                 if ($field->field_type == "E") {
  748.                     $field->display = '';
  749.                     JPluginHelper::importPlugin ('vmcustom');
  750.                     $dispatcher = JDispatcher::getInstance ();
  751.                     $ret = $dispatcher->trigger ('plgVmOnDisplayProductFE', array($product, &$row, &$field));
  752.  
  753.                 }
  754.                 else {
  755.                     $field->display = $this->displayProductCustomfieldFE ($product, $field, $row);
  756.                 }
  757.                 $row++;
  758.             }
  759.             return $productCustoms;
  760.         }
  761.         else {
  762.             return array();
  763.         }
  764.     }
  765.  
  766.     public function getProductCustomsFieldRelatedCategories ($product) {
  767.  
  768.         $query = 'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
  769.             FROM `#__virtuemart_customs` AS C
  770.             LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  771.             Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` = "Z"';
  772.         $query .= ' and is_cart_attribute = 0 order by virtuemart_custom_id';
  773.         $this->_db->setQuery ($query);
  774.         if ($productCustoms = $this->_db->loadObjectList ()) {
  775.             $row = 0;
  776.             foreach ($productCustoms as & $field) {
  777.                 $field->display = $this->displayProductCustomfieldFE ($product, $field, $row);
  778.                 $row++;
  779.             }
  780.             return $productCustoms;
  781.         }
  782.         else {
  783.             return array();
  784.         }
  785.     }
  786.  
  787.     public function getProductCustomsFieldRelatedProducts ($product) {
  788.  
  789.         $query = 'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
  790.             FROM `#__virtuemart_customs` AS C
  791.             LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  792.             Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` = "R"';
  793.         $query .= ' and is_cart_attribute = 0 order by virtuemart_customfield_id';
  794.         $this->_db->setQuery ($query);
  795.         if ($productCustoms = $this->_db->loadObjectList ()) {
  796.             $row = 0;
  797.             foreach ($productCustoms as & $field) {
  798.                 $field->display = $this->displayProductCustomfieldFE ($product, $field, $row);
  799.                 $row++;
  800.             }
  801.             return $productCustoms;
  802.         }
  803.         else {
  804.             return array();
  805.         }
  806.     }
  807.  
  808.     /**
  809.      * Display for the cart
  810.      *
  811.      * @author Patrick Kohl
  812.      * @param obj $product product object
  813.      * @return html code
  814.      */
  815.     public function getProductCustomsFieldCart ($product) {
  816.  
  817.         // group by virtuemart_custom_id
  818.         $query = 'SELECT C.`virtuemart_custom_id`, `custom_title`, C.`custom_value`,`custom_field_desc` ,`custom_tip`,`field_type`,field.`virtuemart_customfield_id`,`is_hidden`
  819.                 FROM `#__virtuemart_customs` AS C
  820.                 LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  821.                 Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` != "G" and `field_type` != "R" and `field_type` != "Z"';
  822.         $query .= ' and is_cart_attribute = 1 group by virtuemart_custom_id ORDER BY field.`ordering`';
  823.  
  824.         $this->_db->setQuery ($query);
  825.         $groups = $this->_db->loadObjectList ();
  826.  
  827.         if (!class_exists ('VmHTML')) {
  828.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'html.php');
  829.         }
  830.         $row = 0;
  831.         if (!class_exists ('CurrencyDisplay')) {
  832.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'currencydisplay.php');
  833.         }
  834.         $currency = CurrencyDisplay::getInstance ();
  835.  
  836.         if (!class_exists ('calculationHelper')) {
  837.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
  838.         }
  839.         $calculator = calculationHelper::getInstance ();
  840.         $calculator ->_product = $product;
  841.         if (!class_exists ('vmCustomPlugin')) {
  842.             require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
  843.         }
  844.  
  845.         //$free = JText::_ ('COM_VIRTUEMART_CART_PRICE_FREE');
  846.         // render select list
  847.         foreach ($groups as $group) {
  848.  
  849.             //              $query='SELECT  field.`virtuemart_customfield_id` as value ,concat(field.`custom_value`," :bu ", field.`custom_price`) AS text
  850.             $query = 'SELECT field.`virtuemart_product_id`, `custom_params`,`custom_element`, field.`virtuemart_custom_id`,
  851.                             field.`virtuemart_customfield_id`,field.`custom_value`, field.`custom_price`, field.`custom_param`
  852.                     FROM `#__virtuemart_customs` AS C
  853.                     LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  854.                     Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id;
  855.             $query .= ' and is_cart_attribute = 1 and C.`virtuemart_custom_id`=' . (int)$group->virtuemart_custom_id;
  856.  
  857.             // We want the field to be ordered as the user defined
  858.             $query .= ' ORDER BY field.`ordering`';
  859.  
  860.             $this->_db->setQuery ($query);
  861.             $options = $this->_db->loadObjectList ();
  862.             //vmdebug('getProductCustomsFieldCart options',$options);
  863.             $group->options = array();
  864.             foreach ($options as $option) {
  865.                 $group->options[$option->virtuemart_customfield_id] = $option;
  866.             }
  867.  
  868.             if ($group->field_type == 'V') {
  869.                 $default = current ($group->options);
  870.                 foreach ($group->options as $productCustom) {
  871.                     $price = self::_getCustomPrice($productCustom->custom_price, $currency, $calculator);
  872.                     $productCustom->text = $productCustom->custom_value . ' ' . $price;
  873.                 }
  874.                 $group->display = VmHTML::select ('customPrice[' . $row . '][' . $group->virtuemart_custom_id . ']', $group->options, $default->custom_value, '', 'virtuemart_customfield_id', 'text', FALSE);
  875.             }
  876.             else {
  877.                 if ($group->field_type == 'G') {
  878.                     $group->display .= ''; // no direct display done by plugin;
  879.                 }
  880.                 else {
  881.                     if ($group->field_type == 'E') {
  882.                         $group->display = '';
  883.  
  884.                         foreach ($group->options as $k=> $productCustom) {
  885.                             $price = self::_getCustomPrice($productCustom->custom_price, $currency, $calculator);
  886.                             $productCustom->text = $productCustom->custom_value . ' ' . $price;
  887.                             $productCustom->virtuemart_customfield_id = $k;
  888.                             if (!class_exists ('vmCustomPlugin')) {
  889.                                 require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
  890.                             }
  891.  
  892.                             //legacy, it will be removed 2.2
  893.                             $productCustom->value = $productCustom->virtuemart_customfield_id;
  894.                             JPluginHelper::importPlugin ('vmcustom');
  895.                             $dispatcher = JDispatcher::getInstance ();
  896.                             $fieldsToShow = $dispatcher->trigger ('plgVmOnDisplayProductVariantFE', array($productCustom, &$row, &$group));
  897.  
  898.                         //  $group->display .= '<input type="hidden" value="' . $k . '" name="customPrice[' . $row . '][' . $group->virtuemart_custom_id . ']" /> ';
  899.                             $group->display .= '<input type="hidden" value="' . $productCustom->virtuemart_customfield_id . '" name="customPrice[' . $row . '][' . $productCustom->virtuemart_custom_id . ']" /> ';
  900.                             if (!empty($currency->_priceConfig['variantModification'][0]) and $price !== '') {
  901.                                 $group->display .= '<div class="price-plugin">' . JText::_ ('COM_VIRTUEMART_CART_PRICE') . '<span class="price-plugin">' . $price . '</span></div>';
  902.                             }
  903.                             $row++;
  904.                         }
  905.                         $row--;
  906.                     }
  907.                     else {
  908.                         if ($group->field_type == 'U') {
  909.                             foreach ($group->options as $productCustom) {
  910.                                 $price = self::_getCustomPrice($productCustom->custom_price, $currency, $calculator);
  911.                                 $productCustom->text = $productCustom->custom_value . ' ' . $price;
  912.  
  913.                                 $group->display .= '<input type="text" value="' . JText::_ ($productCustom->custom_value) . '" name="customPrice[' . $row . '][' . $group->virtuemart_custom_id . '][' . $productCustom->value . ']" /> ';
  914.                                 if (!empty($currency->_priceConfig['variantModification'][0]) and $price !== '') {
  915.                                     $group->display .= '<div class="price-plugin">' . JText::_ ('COM_VIRTUEMART_CART_PRICE') . '<span class="price-plugin">' . $price . '</span></div>';
  916.                                 }
  917.                             }
  918.                         }
  919.                         else {
  920.                             if ($group->field_type == 'A') {
  921.                                 $group->display = '';
  922.                                 foreach ($group->options as $productCustom) {
  923.                                 /*  if ((float)$productCustom->custom_price) {
  924.                                         $price = $currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($productCustom->custom_price));
  925.                                     }
  926.                                     else {
  927.                                         $price = ($productCustom->custom_price === '') ? '' : $free;
  928.                                     }*/
  929.                                     $productCustom->field_type = $group->field_type;
  930.                                     $productCustom->is_cart = 1;
  931.                                     $group->display .= $this->displayProductCustomfieldFE ($product, $productCustom, $row);
  932.                                     $checked = '';
  933.                                 }
  934.                             }
  935.                             else {
  936.  
  937.                                 $group->display = '';
  938.                                 $checked = 'checked="checked"';
  939.                                 foreach ($group->options as $productCustom) {
  940.                                     //vmdebug('getProductCustomsFieldCart',$productCustom);
  941.                                     $price = self::_getCustomPrice($productCustom->custom_price, $currency, $calculator);
  942.                                     $productCustom->field_type = $group->field_type;
  943.                                     $productCustom->is_cart = 1;
  944.                                 //  $group->display .= '<input id="' . $productCustom->virtuemart_custom_id . '" ' . $checked . ' type="radio" value="' .
  945.                                 //      $productCustom->virtuemart_custom_id . '" name="customPrice[' . $row . '][' . $productCustom->virtuemart_customfield_id . ']" /><label
  946.                                 //      for="' . $productCustom->virtuemart_custom_id . '">' . $this->displayProductCustomfieldFE ($productCustom, $row) . ' ' . $price . '</label>';
  947.                         //MarkerVarMods
  948.                                     $group->display .= '<input id="' . $productCustom->virtuemart_custom_id .$row. '" ' . $checked . ' type="radio" value="' .
  949.                                         $productCustom->virtuemart_customfield_id . '" name="customPrice[' . $row . '][' . $productCustom->virtuemart_custom_id . ']" /><label
  950.                                         for="' . $productCustom->virtuemart_custom_id . '" class="other-customfield">' . $this->displayProductCustomfieldFE ($product, $productCustom, $row) . ' ' . $price . '</label>';
  951.  
  952.                                     $checked = '';
  953.                                 }
  954.                             }
  955.                         }
  956.                     }
  957.                 }
  958.             }
  959.             $row++;
  960.         }
  961.  
  962.         return $groups;
  963.  
  964.     }
  965.     static function _getCustomPrice($customPrice, $currency, $calculator) {
  966.         if ((float)$customPrice) {
  967.             $price = strip_tags ($currency->priceDisplay ($calculator->calculateCustomPriceWithTax ($customPrice)));
  968.             if ($customPrice >0) {
  969.                 $price ="+".$price;
  970.             }
  971.         }
  972.         else {
  973.             $price = ($customPrice === '') ? '' :  JText::_ ('COM_VIRTUEMART_CART_PRICE_FREE');
  974.         }
  975.         return $price;
  976.     }
  977.     /**
  978.      * Formating front display by roles
  979.      *  for product only !
  980.      */
  981.     public function displayProductCustomfieldFE (&$product, $customfield, $row = '') {
  982.  
  983.         $virtuemart_custom_id = isset($customfield->virtuemart_custom_id)? $customfield->virtuemart_custom_id:0;
  984.         $value = $customfield->custom_value;
  985.         $type = $customfield->field_type;
  986.         $is_list = isset($customfield->is_list)? $customfield->is_list:0;
  987.         $price = isset($customfield->custom_price)? $customfield->custom_price:0;
  988.         $is_cart = isset($customfield->is_cart)? $customfield->is_cart:0;
  989.  
  990.  
  991.         //vmdebug('displayProductCustomfieldFE and here is something wrong ',$customfield);
  992.  
  993.         if (!class_exists ('CurrencyDisplay'))
  994.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'currencydisplay.php');
  995.         $currency = CurrencyDisplay::getInstance ();
  996.  
  997.         if ($is_list > 0) {
  998.             $values = explode (';', $value);
  999.             if ($is_cart != 0) {
  1000.  
  1001.                 $options = array();
  1002.  
  1003.                 foreach ($values as $key => $val) {
  1004.                     $options[] = array('value' => $val, 'text' => $val);
  1005.                 }
  1006.                 vmdebug('displayProductCustomfieldFE is a list ',$options);
  1007.                 return JHTML::_ ('select.genericlist', $options, 'field[' . $row . '][custom_value]', NULL, 'value', 'text', FALSE, TRUE);
  1008.             }
  1009.             else {
  1010.                 $html = '';
  1011.                 //              if($type=='M'){
  1012.                 //                  foreach ($values as $key => $val){
  1013.                 //                      $html .= '<div id="custom_'.$virtuemart_custom_id.'_'.$val.'" >'.$this->displayCustomMedia($val).'</div>';
  1014.                 //                  }
  1015.  
  1016.                 //              } else {
  1017.                 //                  foreach ($values as $key => $val){
  1018.                 $html .= '<div id="custom_' . $virtuemart_custom_id . '_' . $value . '" >' . $value . '</div>';
  1019.                 //                  }
  1020.                 //              }
  1021.  
  1022.                 return $html;
  1023.             }
  1024.  
  1025.         }
  1026.         else {
  1027.             if ($price > 0) {
  1028.  
  1029.                 $price = $currency->priceDisplay ((float)$price);
  1030.             }
  1031.             switch ($type) {
  1032.  
  1033.                 case 'A':
  1034.  
  1035.                     $options = array();
  1036.  
  1037.                     $session = JFactory::getSession ();
  1038.                     $virtuemart_category_id = $session->get ('vmlastvisitedcategoryid', 0, 'vm');
  1039.  
  1040.                     $productModel = VmModel::getModel ('product');
  1041.  
  1042.                     //parseCustomParams
  1043.                     VirtueMartModelCustomfields::bindParameterableByFieldType($customfield);
  1044.                     //Todo preselection as dropdown of children
  1045.                     //Note by Max Milbers: This is not necessary, in this case it is better to unpublish the parent and to give the child which should be preselected a category
  1046.                     //Or it is withParent, in that case there exists the case, that a parent should be used as a kind of mini category and not be orderable.
  1047.                     //There exists already other customs and in special plugins which wanna disable or change the add to cart button.
  1048.                     //I suggest that we manipulate the button with a message "choose a variant first"
  1049.                     //if(!isset($customfield->pre_selected)) $customfield->pre_selected = 0;
  1050.                     $selected = JRequest::getInt ('virtuemart_product_id',0);
  1051.  
  1052.                     $html = '';
  1053.                     $uncatChildren = $productModel->getUncategorizedChildren ($customfield->withParent);
  1054.  
  1055.                     foreach ($uncatChildren as $k => $child) {
  1056.                         $options[] = array('value' => JRoute::_ ('index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id=' . $virtuemart_category_id . '&virtuemart_product_id=' . $child['virtuemart_product_id']), 'text' => $child['product_name']);
  1057.                     }
  1058.  
  1059.                     $html .= JHTML::_ ('select.genericlist', $options, 'field[' . $row . '][custom_value]', 'onchange="window.top.location.href=this.options[this.selectedIndex].value" size="1" class="inputbox"', "value", "text",
  1060.                         JRoute::_ ('index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id=' . $virtuemart_category_id . '&virtuemart_product_id=' . $selected));
  1061.                     //vmdebug('$customfield',$customfield);
  1062.  
  1063.                     if($customfield->parentOrderable==0 and $product->product_parent_id==0){
  1064.                         $product->orderable = FALSE;
  1065.                     }
  1066.  
  1067.                     return $html;
  1068.                     break;
  1069.  
  1070.                 /* variants*/
  1071.                 case 'V':
  1072.                     if ($price == 0)
  1073.                         $price = JText::_ ('COM_VIRTUEMART_CART_PRICE_FREE');
  1074.  
  1075.                     /* Loads the product price details */
  1076.                     return '<input type="text" value="' . JText::_ ($value) . '" name="field[' . $row . '][custom_value]" /> ' . JText::_ ('COM_VIRTUEMART_CART_PRICE') . $price . ' ';
  1077.                     break;
  1078.                 /*Date variant*/
  1079.                 case 'D':
  1080.                     return '<span class="product_custom_date">' . vmJsApi::date ($value, 'LC1', TRUE) . '</span>'; //vmJsApi::jDate($field->custom_value, 'field['.$row.'][custom_value]','field_'.$row.'_customvalue').$priceInput;
  1081.                     break;
  1082.                 /* text area or editor No JText, only displayed in BE */
  1083.                 case 'X':
  1084.                 case 'Y':
  1085.                     return $value;
  1086.                     break;
  1087.                 /* string or integer */
  1088.                 case 'S':
  1089.                 case 'I':
  1090.                     return JText::_ ($value);
  1091.                     break;
  1092.                 /* bool */
  1093.                 case 'B':
  1094.                     if ($value == 0)
  1095.                         return JText::_ ('COM_VIRTUEMART_NO');
  1096.                     return JText::_ ('COM_VIRTUEMART_YES');
  1097.                     break;
  1098.                 /* parent */
  1099.                 case 'P':
  1100.                     return '<span class="product_custom_parent">' . JText::_ ($value) . '</span>';
  1101.                     break;
  1102.                 /* related */
  1103.                 case 'R':
  1104.                     $q = 'SELECT l.`product_name`, p.`product_parent_id` , l.`product_name`, x.`virtuemart_category_id` FROM `#__virtuemart_products_' . VMLANG . '` as l
  1105.                      JOIN `#__virtuemart_products` AS p using (`virtuemart_product_id`)
  1106.                      LEFT JOIN `#__virtuemart_product_categories` as x on x.`virtuemart_product_id` = p.`virtuemart_product_id`
  1107.                      WHERE p.`published`=1 AND  p.`virtuemart_product_id`= "' . (int)$value . '" ';
  1108.                     $this->_db->setQuery ($q);
  1109.                     $related = $this->_db->loadObject ();
  1110.                     if (empty ($related))
  1111.                         return '';
  1112.                     $thumb = '';
  1113.                     $q = 'SELECT `virtuemart_media_id` FROM `#__virtuemart_product_medias`WHERE `virtuemart_product_id`= "' . (int)$value . '" AND (`ordering` = 0 OR `ordering` = 1)';
  1114.                     $this->_db->setQuery ($q);
  1115.                     $thumb="";
  1116.                     if ($media_id = $this->_db->loadResult ()) {
  1117.                         $thumb = $this->displayCustomMedia ($media_id).' ';
  1118.                     }
  1119.                     return JHTML::link (JRoute::_ ('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=' . $value . '&virtuemart_category_id=' . $related->virtuemart_category_id), $thumb   . $related->product_name, array('title' => $related->product_name));
  1120.                     break;
  1121.                 /* image */
  1122.                 case 'M':
  1123.                     return $this->displayCustomMedia ($value);
  1124.                     break;
  1125.                 /* categorie */
  1126.                 case 'Z':
  1127.                     $q = 'SELECT * FROM `#__virtuemart_categories_' . VMLANG . '` as l JOIN `#__virtuemart_categories` AS c using (`virtuemart_category_id`) WHERE `published`=1 AND l.`virtuemart_category_id`= "' . (int)$value . '" ';
  1128.                     $this->_db->setQuery ($q);
  1129.                     if ($category = $this->_db->loadObject ()) {
  1130.                         $q = 'SELECT `virtuemart_media_id` FROM `#__virtuemart_category_medias`WHERE `virtuemart_category_id`= "' . $category->virtuemart_category_id . '" ';
  1131.                         $this->_db->setQuery ($q);
  1132.                         $thumb = '';
  1133.                         if ($media_id = $this->_db->loadResult ()) {
  1134.                             $thumb = $this->displayCustomMedia ($media_id,'category');
  1135.                         }
  1136.                         return JHTML::link (JRoute::_ ('index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id), $thumb . ' ' . $category->category_name, array('title' => $category->category_name));
  1137.                     }
  1138.                     else return '';
  1139.                 /* Child Group list
  1140.                           * this have no direct display , used for stockable product
  1141.                          */
  1142.                 case 'G':
  1143.                     return ''; //'<input type="text" value="'.JText::_($value).'" name="field['.$row.'][custom_value]" /> '.JText::_('COM_VIRTUEMART_CART_PRICE').' : '.$price .' ';
  1144.                     break;
  1145.                     break;
  1146.             }
  1147.         }
  1148.     }
  1149.  
  1150.     function displayCustomMedia ($media_id, $table = 'product', $absUrl = FALSE) {
  1151.  
  1152.         if (!class_exists ('TableMedias'))
  1153.             require(JPATH_VM_ADMINISTRATOR . DS . 'tables' . DS . 'medias.php');
  1154.         //$data = $this->getTable('medias');
  1155.         $db = JFactory::getDBO ();
  1156.         $data = new TableMedias($db);
  1157.         $data->load ((int)$media_id);
  1158.  
  1159.         if (!class_exists ('VmMediaHandler'))
  1160.             require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'mediahandler.php');
  1161.         $media = VmMediaHandler::createMedia ($data, $table);
  1162.  
  1163.         return $media->displayMediaThumb ('', FALSE, '', TRUE, TRUE, $absUrl);
  1164.  
  1165.     }
  1166.  
  1167.     /**
  1168.      * There are too many functions doing almost the same for my taste
  1169.      * the results are sometimes slighty different and makes it hard to work with it, therefore here the function for future proxy use
  1170.      *
  1171.      */
  1172.     public function customFieldDisplay ($product, $variantmods, $html, $trigger) {
  1173.  
  1174.         //vmdebug('customFieldDisplay $variantmods',$variantmods);
  1175.         $row = 0;
  1176.         if (!class_exists ('shopFunctionsF'))
  1177.             require(JPATH_VM_SITE . DS . 'helpers' . DS . 'shopfunctionsf.php');
  1178.         //MarkerVarMods
  1179.         foreach ($variantmods as $selected => $variant) {
  1180.         //foreach ($variantmods as $variant=> $selected) {
  1181.             //vmdebug('customFieldDisplay '.$variant.' '.$selected);
  1182.             if ($selected) {
  1183.  
  1184.                 $productCustom = self::getProductCustomField ($selected);
  1185.                 //vmdebug('customFieldDisplay',$selected,$productCustom);
  1186.                 if (!empty($productCustom)) {
  1187.                     $html .= ' <span class="product-field-type-' . $productCustom->field_type . '">';
  1188.                     if ($productCustom->field_type == "E") {
  1189.  
  1190.                         $product = self::addParam ($product);
  1191.                         $product->productCustom = $productCustom;
  1192.                         //vmdebug('CustomsFieldCartDisplay $productCustom',$productCustom);
  1193. //                              vmdebug('customFieldDisplay $product->param selected '.$selected,$product->param);
  1194.                         if (!class_exists ('vmCustomPlugin'))
  1195.                             require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
  1196.                         JPluginHelper::importPlugin ('vmcustom');
  1197.                         $dispatcher = JDispatcher::getInstance ();
  1198.                         $dispatcher->trigger ($trigger, array($product, $row, &$html));
  1199.  
  1200.                     }
  1201.                     else {
  1202.                         //vmdebug('customFieldDisplay $productCustom by self::getProductCustomField $variant: '.$variant.' $selected: '.$selected,$productCustom);
  1203.                         $value = '';
  1204.                         if (($productCustom->field_type == "G")) {
  1205.  
  1206.                             $child = self::getChild ($productCustom->custom_value);
  1207.                             //                      $html .= $productCustom->custom_title.' '.$child->product_name;
  1208.                             $value = $child->product_name;
  1209.                         }
  1210.                         elseif (($productCustom->field_type == "M")) {
  1211.                             //                      $html .= $productCustom->custom_title.' '.self::displayCustomMedia($productCustom->custom_value);
  1212.                             $value = self::displayCustomMedia ($productCustom->custom_value);
  1213.                         }
  1214.                         elseif (($productCustom->field_type == "S")) {
  1215.                             //                  q   $html .= $productCustom->custom_title.' '.JText::_($productCustom->custom_value);
  1216.                             $value = $productCustom->custom_value;
  1217.                         }
  1218.                         else {
  1219.                             //                      $html .= $productCustom->custom_title.' '.$productCustom->custom_value;
  1220.                             //vmdebug('customFieldDisplay',$productCustom);
  1221.                             $value = $productCustom->custom_value;
  1222.                         }
  1223.                         $html .= ShopFunctionsF::translateTwoLangKeys ($productCustom->custom_title, $value);
  1224.                     }
  1225.                     $html .= '</span><br />';
  1226.                 }
  1227.                 else {
  1228.                     // falldown method if customfield are deleted
  1229.                     foreach ((array)$selected as $key => $value) {
  1230.                         $html .= '<br/ >Couldnt find customfield' . ($key ? '<span>' . $key . ' </span>' : '') . $value;
  1231.                     }
  1232.                     vmdebug ('CustomsFieldOrderDisplay, $item->productCustom empty? ' . $variant);
  1233.                     vmdebug ('customFieldDisplay, $productCustom is EMPTY ');
  1234.                 }
  1235.  
  1236.             }
  1237.             $row++;
  1238.         }
  1239.  
  1240.     //  vmdebug ('customFieldDisplay html begin: ' . $html . ' end');
  1241.         return $html . '</div>';
  1242.     }
  1243.  
  1244.     /**
  1245.      * TODO This is html and view stuff and MUST NOT be in the model, notice by Max
  1246.      * render custom fields display cart module FE
  1247.      */
  1248.     public function CustomsFieldCartModDisplay ($priceKey, $product) {
  1249.  
  1250.         if (empty($calculator)) {
  1251.             if (!class_exists ('calculationHelper'))
  1252.                 require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
  1253.             $calculator = calculationHelper::getInstance ();
  1254.         }
  1255.  
  1256.         $variantmods = $calculator->parseModifier ($priceKey);
  1257.  
  1258.         return self::customFieldDisplay ($product, $variantmods, '<div class="vm-customfield-mod">', 'plgVmOnViewCartModule');
  1259.  
  1260.     }
  1261.  
  1262.     /**
  1263.      *  TODO This is html and view stuff and MUST NOT be in the model, notice by Max
  1264.      * render custom fields display cart FE
  1265.      */
  1266.     public function CustomsFieldCartDisplay ($priceKey, $product) {
  1267.  
  1268.         if (empty($calculator)) {
  1269.             if (!class_exists ('calculationHelper'))
  1270.                 require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'calculationh.php');
  1271.             $calculator = calculationHelper::getInstance ();
  1272.         }
  1273.  
  1274.         vmdebug('CustomsFieldCartDisplay ',$priceKey);
  1275.         $variantmods = $calculator->parseModifier ($priceKey);
  1276.  
  1277.         return self::customFieldDisplay ($product, $variantmods, '<div class="vm-customfield-cart">', 'plgVmOnViewCart');
  1278.  
  1279.     }
  1280.  
  1281.     /*
  1282.            * render custom fields display order BE/FE
  1283.           */
  1284.     public function CustomsFieldOrderDisplay ($item, $view = 'FE', $absUrl = FALSE) {
  1285.  
  1286.         $row = 0;
  1287.         //      $item=(array)$item;
  1288.         if (!empty($item->product_attribute)) {
  1289.             $item->param = json_decode ($item->product_attribute, TRUE);
  1290. //                  $html = '<div class="vm-customfield-cart">';
  1291.             if (!empty($item->param)) {
  1292.                 return self::customFieldDisplay ($item, $item->param, '<div class="vm-customfield-cart">', 'plgVmDisplayInOrder' . $view);
  1293.  
  1294.             }
  1295.             else {
  1296.                 vmdebug ('CustomsFieldOrderDisplay $item->param empty? ');
  1297.             }
  1298.         }
  1299.         else {
  1300.             //          vmTrace('$item->product_attribut is empty');
  1301.         }
  1302.         return FALSE;
  1303.     }
  1304.  
  1305.     /**
  1306.      *
  1307.      * custom fields for cart and cart module
  1308.      */
  1309.     public function getProductCustomField ($selected) {
  1310.  
  1311.         $db = JFactory::getDBO ();
  1312.         $query = 'SELECT C.`virtuemart_custom_id` , `custom_element` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` ,
  1313.         C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_cart_attribute` , `is_hidden` , C.`published` ,
  1314.         field.`virtuemart_customfield_id` , field.`custom_value`,field.`custom_param`,field.`custom_price`
  1315.             FROM `#__virtuemart_customs` AS C
  1316.             LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
  1317.             WHERE `virtuemart_customfield_id` ="' . (int)$selected . '"';
  1318.         //      if($product_parent_id!=0){
  1319.         //          $query .= ' AND (`virtuemart_product_id` ="' . $product_id.'" XOR `virtuemart_product_id` ="' . $product_parent_id.'")';
  1320.         //      } else {
  1321.         //          $query .= ' AND (`virtuemart_product_id` ="' . $product_id.'"';
  1322.         //      }
  1323.         $db->setQuery ($query);
  1324.         return $db->loadObject ();
  1325.     }
  1326.  
  1327.     /*
  1328.            * add parameter to product definition
  1329.           */
  1330.     public function addParam ($product) {
  1331.  
  1332. //              vmdebug('addParam? ',$product->custom_param,$product->customPlugin);
  1333.         $custom_param = empty($product->custom_param) ? array() : json_decode ($product->custom_param, TRUE);
  1334.         $product_param = empty($product->customPlugin) ? array() : json_decode ($product->customPlugin, TRUE);
  1335.         $params = (array)$product_param + (array)$custom_param;
  1336.         foreach ($params as $key => $param) {
  1337.             $product->param[$key] = $param;
  1338.         }
  1339.         return $product;
  1340.     }
  1341.  
  1342.     public function getChild ($child) {
  1343.  
  1344.         $db = JFactory::getDBO ();
  1345.         $db->setQuery ('SELECT  `product_sku`, `product_name` FROM `#__virtuemart_products_' . VMLANG . '` WHERE virtuemart_product_id=' . $child);
  1346.         return $db->loadObject ();
  1347.     }
  1348.  
  1349.     static public function setEditCustomHidden ($customfield, $i) {
  1350.  
  1351.         if (!isset($customfield->virtuemart_customfield_id))
  1352.             $customfield->virtuemart_customfield_id = '0';
  1353.         $html = '
  1354.             <input type="hidden" value="' . $customfield->field_type . '" name="field[' . $i . '][field_type]" />
  1355.             <input type="hidden" value="' . $customfield->virtuemart_custom_id . '" name="field[' . $i . '][virtuemart_custom_id]" />
  1356.             <input type="hidden" value="' . $customfield->virtuemart_customfield_id . '" name="field[' . $i . '][virtuemart_customfield_id]" />
  1357.             <input type="hidden" value="' . $customfield->admin_only . '" checked="checked" name="field[' . $i . '][admin_only]" />';
  1358.         return $html;
  1359.  
  1360.     }
  1361. }
  1362. // pure php no closing tag
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement