Esempio n. 1
0
 /**
  * Saves an item and redirects based on task
  * @return void
  */
 function save()
 {
     $task = JRequest::getVar('task');
     $model = $this->getModel($this->get('suffix'));
     $isSaveAs = false;
     $row = $model->getTable();
     $row->load($model->getId());
     $row->bind(JRequest::get('POST'));
     $row->product_description = JRequest::getVar('product_description', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $row->product_description_short = JRequest::getVar('product_description_short', '', 'post', 'string', JREQUEST_ALLOWRAW);
     // set the id as 0 for new entry
     if ($task == "save_as") {
         unset($row);
         // load WITHOUT EAV! otherwise the save will fail
         $row = $model->getTable();
         $row->load($model->getId(), true, false);
         $row->bind(JRequest::get('POST'));
         $row->product_description = JRequest::getVar('product_description', '', 'post', 'string', JREQUEST_ALLOWRAW);
         $row->product_description_short = JRequest::getVar('product_description_short', '', 'post', 'string', JREQUEST_ALLOWRAW);
         $isSaveAs = true;
         $oldProductImagePath = $row->getImagePath();
         $pk = $row->getKeyName();
         $oldPk = $row->{$pk};
         // these get reset
         $row->{$pk} = 0;
         $row->product_images_path = '';
         $row->product_rating = '';
         $row->product_comments = '';
     }
     $row->_isNew = empty($row->product_id);
     $fieldname = 'product_full_image_new';
     $userfiles = JRequest::getVar($fieldname, '', 'files', 'array');
     // save the integrations
     $row = $this->prepareParameters($row);
     //check if normal price exists
     Tienda::load("TiendaHelperProduct", 'helpers.product');
     if ($isSaveAs) {
         // and the prices
         $prices = TiendaHelperProduct::getPrices($oldPk);
     } else {
         $prices = TiendaHelperProduct::getPrices($row->product_id);
     }
     if ($row->save()) {
         $row->product_id = $row->id;
         $model->setId($row->id);
         $this->messagetype = 'message';
         $this->message = JText::_('COM_TIENDA_SAVED');
         // check it's new entry or empty price but not save as
         if (($row->_isNew || empty($prices)) && !$isSaveAs) {
             // set price if new or no prices set
             $price = JTable::getInstance('Productprices', 'TiendaTable');
             $price->product_id = $row->id;
             $price->product_price = JRequest::getVar('product_price');
             $price->group_id = Tienda::getInstance()->get('default_user_group', '1');
             if (!$price->save()) {
                 $this->messagetype = 'notice';
                 $this->message .= " :: " . $price->getError();
             }
         }
         if ($row->_isNew && !$isSaveAs) {
             // set category
             $category = JTable::getInstance('Productcategories', 'TiendaTable');
             $category->product_id = $row->id;
             $category->category_id = JRequest::getVar('category_id');
             if (!$category->save()) {
                 $this->messagetype = 'notice';
                 $this->message .= " :: " . $category->getError();
             }
             // save default quantity
             $quantity = JTable::getInstance('Productquantities', 'TiendaTable');
             $quantity->product_id = $row->id;
             $quantity->quantity = JRequest::getInt('product_quantity');
             if (!$quantity->save()) {
                 $this->messagetype = 'notice';
                 $this->message .= " :: " . $quantity->getError();
             }
         }
         if ($isSaveAs) {
             // set price when cloning
             $priceTable = JTable::getInstance('Productprices', 'TiendaTable');
             foreach ($prices as $price) {
                 $priceTable->product_id = $row->id;
                 $priceTable->product_price = $price->product_price;
                 $priceTable->product_price_startdate = $price->product_price_startdate;
                 $priceTable->product_price_enddate = $price->product_price_enddate;
                 $priceTable->created_date = $price->created_date;
                 $priceTable->modified_date = $price->modified_date;
                 $priceTable->group_id = $price->group_id;
                 $priceTable->price_quantity_start = $price->price_quantity_start;
                 $priceTable->price_quantity_end = $price->price_quantity_end;
                 if (!$priceTable->save()) {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $priceTable->getError();
                 }
             }
             // set category
             $categoryTable = JTable::getInstance('Productcategories', 'TiendaTable');
             $categories = TiendaHelperProduct::getCategories($oldPk);
             foreach ($categories as $category) {
                 $categoryTable->product_id = $row->id;
                 $categoryTable->category_id = $category;
                 if (!$categoryTable->save()) {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $categoryTable->getError();
                 }
             }
             // TODO Save Attributes
             // An array to map attribute id  old attribute id  are as key and new attribute id are as value
             $attrbuteMappingArray = array();
             $attrbuteParentMappingArray = array();
             $attributes = TiendaHelperProduct::getAttributes($oldPk);
             foreach ($attributes as $attribute) {
                 $attributeTable = JTable::getInstance('ProductAttributes', 'TiendaTable');
                 $attributeTable->productattribute_name = $attribute->productattribute_name;
                 $attributeTable->product_id = $row->id;
                 $attributeTable->ordering = $attribute->ordering;
                 if ($attributeTable->save()) {
                     $attrbuteMappingArray[$attribute->productattribute_id] = $attributeTable->productattribute_id;
                     $attrbuteParentMappingArray[$attributeTable->productattribute_id] = $attribute->parent_productattributeoption_id;
                 } else {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $attributeTable->getError();
                 }
             }
             // set Attribute options
             $attrbuteOptionsMappingArray = array();
             foreach ($attrbuteMappingArray as $oldAttrbuteId => $newAttributeId) {
                 // set Attribute options
                 $options = TiendaHelperProduct::getAttributeOptionsObjects($oldAttrbuteId);
                 foreach ($options as $option) {
                     $attributeOptionsTable = JTable::getInstance('ProductAttributeOptions', 'TiendaTable');
                     $attributeOptionsTable->productattribute_id = $newAttributeId;
                     $attributeOptionsTable->productattributeoption_name = $option->productattributeoption_name;
                     $attributeOptionsTable->productattributeoption_price = $option->productattributeoption_price;
                     $attributeOptionsTable->productattributeoption_prefix = $option->productattributeoption_prefix;
                     $attributeOptionsTable->productattributeoption_code = $option->productattributeoption_code;
                     $attributeOptionsTable->ordering = $option->ordering;
                     if ($attributeOptionsTable->save()) {
                         $attrbuteOptionsMappingArray[$option->productattributeoption_id] = $attributeOptionsTable->productattributeoption_id;
                     } else {
                         $this->messagetype = 'notice';
                         $this->message .= " :: " . $attributeOptionsTable->getError();
                     }
                 }
                 // save parent relationship
                 if ($attrbuteParentMappingArray[$newAttributeId]) {
                     $attributeTable = JTable::getInstance('ProductAttributes', 'TiendaTable');
                     $attributeTable->load($newAttributeId);
                     $attributeTable->parent_productattributeoption_id = $attrbuteOptionsMappingArray[$attrbuteParentMappingArray[$newAttributeId]];
                     if (!$attributeTable->save()) {
                         $this->messagetype = 'notice';
                         $this->message .= " :: " . $attributeTable->getError();
                     }
                 }
             }
             // set quantity
             $quantityTable = JTable::getInstance('Productquantities', 'TiendaTable');
             $quantities = TiendaHelperProduct::getProductQuantitiesObjects($oldPk);
             foreach ($quantities as $quantity) {
                 $quantityTable->product_attributes = $quantity->product_attributes;
                 $quantityTable->product_id = $row->id;
                 $quantityTable->vendor_id = $quantity->vendor_id;
                 $quantityTable->quantity = $quantity->quantity;
                 $optionsCSV = $quantity->product_attributes;
                 $options = explode(",", $optionsCSV);
                 $newOptions = array();
                 foreach ($options as $option) {
                     $newOptions[] = $attrbuteOptionsMappingArray[$option];
                 }
                 $optionsCSV = implode(",", $newOptions);
                 $quantityTable->product_attributes = $optionsCSV;
                 if (!$quantityTable->save()) {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $quantityTable->getError();
                 }
             }
             // copy all gallery files
             jimport('joomla.filesystem.folder');
             jimport('joomla.filesystem.file');
             $galleryFiles = JFolder::files($oldProductImagePath);
             // get all gallery images
             if (count($galleryFiles)) {
                 JFolder::create($row->getImagePath());
                 // create folder for images
                 JFolder::create($row->getImagePath() . 'thumbs');
                 // create folder for thumbnails images
                 for ($i = 0, $c = count($galleryFiles); $i < $c; $i++) {
                     // copy only images with both original file and a corresponding thumbnail
                     if (JFile::exists($oldProductImagePath . 'thumbs/' . $galleryFiles[$i]) && JFile::exists($oldProductImagePath . $galleryFiles[$i])) {
                         JFile::copy($oldProductImagePath . $galleryFiles[$i], $row->getImagePath() . DS . $galleryFiles[$i]);
                         JFile::copy($oldProductImagePath . 'thumbs/' . $galleryFiles[$i], $row->getImagePath() . '/thumbs/' . $galleryFiles[$i]);
                     }
                 }
             }
             // duplicate product files (only in db)
             $modelFiles = $this->getModel('productfiles');
             $modelFiles->setState('filter_product', $oldPk);
             $listFiles = $modelFiles->getList();
             if (count($listFiles)) {
                 $row_file = JTable::getInstance('Productfiles', 'TiendaTable');
                 for ($i = 0, $c = count($listFiles); $i < $c; $i++) {
                     $row_file->bind($listFiles[$i]);
                     // bind old data
                     $row_file->productfile_id = 0;
                     // will be set
                     $row_file->product_id = $row->product_id;
                     // use clone's ID
                     $row_file->save();
                     // save the data
                 }
             }
             // create duplicate connections for EAV custom fields
             JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/models');
             $model = JModel::getInstance('EavAttributes', 'TiendaModel');
             $model->setState('filter_entitytype', 'products');
             $model->setState('filter_entityid', $oldPk);
             $listEAV = $model->getList();
             $teav = $model->getTable();
             if (is_array($listEAV)) {
                 for ($i = 0, $c = count($listEAV); $i < $c; $i++) {
                     $tblEAV = JTable::getInstance('EavAttributeEntities', 'TiendaTable');
                     $tblEAV->eaventity_id = $row->product_id;
                     $tblEAV->eaventity_type = 'products';
                     $tblEAV->eavattribute_id = $listEAV[$i]->eavattribute_id;
                     $tblEAV->save();
                     // Clone the values too!
                     $teav->load($listEAV[$i]->eavattribute_id);
                     $value = TiendaHelperEav::getAttributeValue($teav, 'products', $row->product_id);
                     $newValue = JTable::getInstance('EavValues', 'TiendaTable');
                     $newValue->setType($teav->eavattribute_type);
                     $newValue->eavattribute_id = $listEAV[$i]->eavattribute_id;
                     $newValue->eaventity_id = $row->product_id;
                     // Store the value
                     $newValue->eavvalue_value = $value;
                     $newValue->eaventity_type = 'products';
                     $newValue->store();
                 }
             }
         }
         // Multiple images processing
         $i = 0;
         $error = false;
         while (!empty($userfiles['size'][$i])) {
             $dir = $row->getImagePath(true);
             if ($upload = $this->addimage($fieldname, $i, $dir)) {
                 // The first One is the default (if there is no default yet)
                 if ($i == 0 && (empty($row->product_full_image) || $row->product_full_image == '')) {
                     $row->product_full_image = $upload->getPhysicalName();
                     // need to re-save in this instance
                     // should we be storing or saving?
                     $row->save();
                 }
             } else {
                 $error = true;
             }
             $i++;
         }
         if ($error) {
             $this->messagetype = 'notice';
             $this->message .= " :: " . $this->getError();
         }
         $helper = new TiendaHelperProduct();
         $helper->onAfterSaveProducts($row);
         $model->clearCache();
         $dispatcher = JDispatcher::getInstance();
         $dispatcher->trigger('onAfterSave' . $this->get('suffix'), array($row));
     } else {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_TIENDA_SAVE_FAILED') . " - " . $row->getError();
     }
     $redirect = "index.php?option=com_tienda";
     switch ($task) {
         case "save_as":
             $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $row->product_id;
             $this->message .= " - " . JText::_('COM_TIENDA_YOU_ARE_NOW_EDITING_NEW_PRODUCT');
             break;
         case "saveprev":
             $redirect .= '&view=' . $this->get('suffix');
             // get prev in list
             Tienda::load("TiendaHelperProduct", 'helpers.product');
             $surrounding = TiendaHelperProduct::getSurrounding($model->getId());
             if (!empty($surrounding['prev'])) {
                 $redirect .= '&task=edit&id=' . $surrounding['prev'];
             }
             break;
         case "savenext":
             $redirect .= '&view=' . $this->get('suffix');
             // get next in list
             Tienda::load("TiendaHelperProduct", 'helpers.product');
             $surrounding = TiendaHelperProduct::getSurrounding($model->getId());
             if (!empty($surrounding['next'])) {
                 $redirect .= '&task=edit&id=' . $surrounding['next'];
             }
             break;
         case "savenew":
             $redirect .= '&view=' . $this->get('suffix') . '&task=add';
             break;
         case "apply":
             $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $model->getId();
             break;
         case "save":
         default:
             $redirect .= "&view=" . $this->get('suffix');
             break;
     }
     $redirect = JRoute::_($redirect, false);
     $this->setRedirect($redirect, $this->message, $this->messagetype);
 }
Esempio n. 2
0
        <td class="dsc-key"><?php 
    echo JText::_('COM_TIENDA_CATEGORIES');
    ?>
:</td>
        <td><?php 
    Tienda::load('TiendaHelperCategory', 'helpers.category');
    ?>
 <?php 
    Tienda::load('TiendaUrl', 'library.url');
    $options = array('update' => true);
    ?>
 [<?php 
    echo TiendaUrl::popup("index.php?option=com_tienda&view=products&task=selectcategories&id=" . $row->product_id . "&tmpl=component", JText::_('COM_TIENDA_SELECT_CATEGORIES'), $options);
    ?>
] <?php 
    $categories = $helper_product->getCategories($row->product_id);
    ?>
            <div id="current_categories">
                <?php 
    foreach (@$categories as $category) {
        ?>
                [<a href="<?php 
        echo "index.php?option=com_tienda&view=products&task=selected_disable&id=" . $row->product_id . "&cid[]=" . $category . "&return=" . base64_encode("index.php?option=com_tienda&view=products&task=edit&id=" . $row->product_id);
        ?>
"> <?php 
        echo JText::_('COM_TIENDA_REMOVE');
        ?>
                </a>]
                <?php 
        echo TiendaHelperCategory::getPathName($category);
        ?>
Esempio n. 3
0
 /**
  * Sample use of the products model for getting products with certain properties
  * See admin/models/products.php for all the filters currently built into the model
  *
  * @param $parameters
  * @return unknown_type
  */
 function getProducts()
 {
     // Check the registry to see if our Tienda class has been overridden
     if (!class_exists('Tienda')) {
         JLoader::register("Tienda", JPATH_ADMINISTRATOR . "/components/com_tienda/defines.php");
     }
     // load the config class
     Tienda::load('Tienda', 'defines');
     Tienda::load('TiendaHelperProduct', 'helpers.product');
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables');
     JModel::addIncludePath(JPATH_SITE . '/components/com_tienda/models');
     // get the model
     $model = JModel::getInstance('Products', 'TiendaModel');
     // setting the model's state tells it what items to return
     $model->setState('filter_published', '1');
     $date = JFactory::getDate();
     $model->setState('filter_published_date', $date->toMysql());
     $model->setState('filter_enabled', '1');
     // Set category state
     if ($this->params->get('category', '1') != '1') {
         $model->setState('filter_category', $this->params->get('category', '1'));
     }
     // Set manufacturer state
     if ($this->params->get('manufacturer', '') != '') {
         $model->setState('filter_manufacturer', $this->params->get('manufacturer', ''));
     }
     // Set id set state
     if ($this->params->get('id_set', '') != '') {
         $params_id_set = $this->params->get('id_set');
         $id_array = explode(',', $params_id_set);
         $id_set = "'" . implode("', '", $id_array) . "'";
         $model->setState('filter_id_set', $id_set);
     }
     // set the states based on the parameters
     $model->setState('limit', $this->params->get('max_number', '10'));
     if ($this->params->get('price_from', '-1') != '-1') {
         $model->setState('filter_price_from', $this->params->get('price_from', '-1'));
     }
     if ($this->params->get('price_to', '-1') != '-1') {
         $model->setState('filter_price_to', $this->params->get('price_to', '-1'));
     }
     $order = $this->params->get('order');
     $direction = $this->params->get('direction', 'ASC');
     switch ($order) {
         case "2":
         case "name":
             $model->setState('order', 'tbl.product_name');
             break;
         case "1":
         case "created":
             $model->setState('order', 'tbl.created_date');
             break;
         case "0":
         case "ordering":
         default:
             $model->setState('order', 'tbl.ordering');
             break;
     }
     if ($this->params->get('random', '0') == '1') {
         $model->setState('order', 'RAND()');
     }
     $model->setState('direction', $direction);
     $config = Tienda::getInstance();
     $show_tax = $config->get('display_prices_with_tax');
     $default_user_group = Tienda::getInstance()->get('default_user_group');
     $user_groups_array = $this->getUserGroups();
     $overide_price = false;
     if (count($user_groups_array) > 1 && $user_groups_array[0] != $default_user_group) {
         $overide_price = true;
     }
     // using the set filters, get a list of products
     if ($products = $model->getList(true, false)) {
         if ($show_tax) {
             Tienda::load('TiendaHelperUser', 'helpers.user');
             $geozones = TiendaHelperUser::getGeoZones(JFactory::getUser()->id);
             if (empty($geozones)) {
                 // use the default
                 $table = JTable::getInstance('Geozones', 'TiendaTable');
                 $table->load(array('geozone_id' => Tienda::getInstance()->get('default_tax_geozone')));
                 $geozones = array($table);
             }
         }
         foreach ($products as $product) {
             if ($overide_price) {
                 $filter_group = TiendaHelperUser::getUserGroup(JFactory::getUser()->id, $product->product_id);
                 $price = TiendaHelperProduct::getPrice($product->product_id, '1', $filter_group);
                 $product->price = $price->product_price;
             }
             $product->taxtotal = 0;
             $product->tax = 0;
             if ($show_tax) {
                 $taxtotal = TiendaHelperProduct::getTaxTotal($product->product_id, $geozones);
                 $product->taxtotal = $taxtotal;
                 $product->tax = $taxtotal->tax_total;
             }
             $product->filter_category = '';
             $categories = TiendaHelperProduct::getCategories($product->product_id);
             if (!empty($categories)) {
                 $product->link .= "&filter_category=" . $categories[0];
                 $product->filter_category = $categories[0];
             }
             $itemid = Tienda::getClass("TiendaHelperRoute", 'helpers.route')->category($product->filter_category, true);
             if (empty($itemid)) {
                 $product->itemid = $this->params->get('itemid');
             } else {
                 $product->itemid = $itemid;
             }
         }
     }
     return $products;
 }