/** * downloads a file * * @return void */ function downloadFile() { $app = JFactory::getApplication(); $user = JFactory::getUser(); $productfile_id = $app->input->getInt('id', 0); //$productfile_id = intval( JRequest::getvar( 'id', '', 'request', 'int' ) ); $product_id = $app->input->getInt('product_id', 0); $link = 'index.php?option=com_citruscart&view=products&task=edit&id=' . $product_id; Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = CitruscartHelperBase::getInstance('ProductDownload', 'CitruscartHelper'); JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_citruscart' . DS . 'tables'); $productfile = JTable::getInstance('ProductFiles', 'CitruscartTable'); $productfile->load($productfile_id); if (empty($productfile->productfile_id)) { $this->messagetype = 'notice'; $this->message = JText::_('COM_CITRUSCART_INVALID FILE'); $this->setRedirect($link, $this->message, $this->messagetype); return false; } // log and download Citruscart::load('CitruscartFile', 'library.file'); // geting the ProductDownloadId to updated for which productdownload_max is greater then 0 $productToDownload = $helper->getProductDownloadInfo($productfile->productfile_id, $user->id); if ($downloadFile = CitruscartFile::download($productfile)) { $link = JRoute::_($link, false); $this->setRedirect($link); } }
private function _migrateProduct($data) { // Check for product_name. if (!empty($data->product_name)) { $isNew = false; if (!empty($data->product_id)) { if ((int) $data->product_id) { $data['product_id'] = 0; $isNew = true; } } JTable::addIncludePath(JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_citruscart' . DIRECTORY_SEPARATOR . 'tables'); $product = JTable::getInstance('Products', 'CitruscartTable'); if (!$isNew) { if (!$product->load($data['product_id'])) { $isNew = true; $data['product_id'] = 0; } } // If is a new product, use product->create() if ($isNew) { $product->product_price = 0; $product->product_quantity = 0; $product->bind($this->simpleXml2Array($data)); if ($product->product_full_image) { Citruscart::load('CitruscartFile', 'library.file'); // Do the same cleaning to the image title that the image helper does $name = explode('.', $product->product_full_image); $name = CitruscartFile::cleanTitle($name[0]) . '.' . $name[count($name) - 1]; $product->product_full_image = $name; } $product->create(); $this->_migrateAttributes($product->product_id, $data->product_attributes); $this->_migrateCategories($product->product_id, $data->product_categories); $this->_migrateFiles($product->product_id, $data->product_files); $this->_migrateImages($product->product_id, $data->product_images); $this->_migrateChildren($product->product_id, $data->product_children); $this->_migrateRelated($product->product_id, $data->related_products); $this->_migrateCustomFields($product->product_id, $data->product_custom_fields); } else { $product->bind($this->simpleXml2Array($data)); //check if normal price exists Citruscart::load("CitruscartHelperProduct", 'helpers.product'); $prices = CitruscartHelperProduct::getPrices($product->product_id); $quantities = CitruscartHelperProduct::getProductQuantities($product->product_id); if ($product->save()) { $product->product_id = $product->id; // New price? if (empty($prices)) { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->product_id = $product->id; $price->product_price = (string) $data->product_price; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } else { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->load($prices[0]->product_price_id); $price->product_price = (string) $data->product_price; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } // New quantity? if (empty($quantities)) { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->product_id = $product->id; $quantity->quantity = (string) $data->product_quantity; $quantity->save(); } else { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->load($quantities[0]->productquantity_id); $quantity->product_id = $product->id; $quantity->quantity = (string) $data->product_quantity; $quantity->save(); } } } return $product; } return null; }
/** * Do the migration * * @return array */ function _migrate($datas) { $queries = array(); $results = array(); $n = 0; // Loop though the rows foreach ($datas as $data) { // Check for product_name. Explode() could have generated an empty row if (!empty($data['product_name'])) { $isNew = false; if (!$data['product_id']) { $data['product_id'] = 0; $isNew = true; } JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_citruscart' . DS . 'tables'); $product = JTable::getInstance('Products', 'CitruscartTable'); if (!$isNew) { if (!$product->load($data['product_id'])) { $isNew = true; $data['product_id'] = 0; } } // If is a new product, use product->create() if ($isNew) { $product->product_price = 0; $product->product_quantity = 0; $product->bind($data); if ($product->product_full_image) { Citruscart::load('CitruscartFile', 'library.file'); // Do the same cleaning to the image title that the image helper does $name = explode('.', $product->product_full_image); $name = CitruscartFile::cleanTitle($name[0]) . '.' . $name[count($name) - 1]; $product->product_full_image = $name; } $product->create(); $this->_migrateAttributes($product->product_id, $data['product_attributes']); } else { $product->bind($data); //check if normal price exists Citruscart::load("CitruscartHelperProduct", 'helpers.product'); $prices = CitruscartHelperProduct::getPrices($product->product_id); $quantities = CitruscartHelperProduct::getProductQuantities($product->product_id); if ($product->save()) { $product->product_id = $product->id; // New price? if (empty($prices)) { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->product_id = $product->id; $price->product_price = $data['product_price']; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } else { // set price if new or no prices set $price = JTable::getInstance('Productprices', 'CitruscartTable'); $price->load($prices[0]->product_price_id); $price->product_price = $data['product_price']; $price->group_id = Citruscart::getInstance()->get('default_user_group', '1'); $price->save(); } // New quantity? if (empty($quantities)) { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->product_id = $product->id; $quantity->quantity = $data['product_quantity']; $quantity->save(); } else { // save default quantity $quantity = JTable::getInstance('Productquantities', 'CitruscartTable'); $quantity->load($quantities[0]->productquantity_id); $quantity->product_id = $product->id; $quantity->quantity = $data['product_quantity']; $quantity->save(); } } } // at this point, the product is saved, so now do additional relationships // such as categories if (!empty($product->product_id) && !empty($data['product_categories'])) { foreach ($data['product_categories'] as $category_id) { // This is probably not the best way to do it // Numeric = id, string = category name if (!is_numeric($category_id)) { // check for existance JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_citruscart' . DS . 'models'); $model = JModelLegacy::getInstance('Categories', 'CitruscartModel'); $model->setState('filter_name', $category_id); $matches = $model->getList(); $matched = false; if ($matches) { foreach ($matches as $match) { // is a perfect match? if (strtolower($category_id) == strtolower($match->category_name)) { $category_id = $match->category_id; $matched = true; } } } // Not matched, create category if (!$matched) { $category = JTable::getInstance('Categories', 'CitruscartTable'); $category->category_name = $category_id; $category->parent_id = 1; $category->category_enabled = 1; $category->save(); $category_id = $category->category_id; } } // save xref in every case $xref = JTable::getInstance('ProductCategories', 'CitruscartTable'); $xref->product_id = $product->product_id; $xref->category_id = $category_id; $xref->save(); } } $results[$n]->title = $product->product_name; $results[$n]->query = ""; $results[$n]->error = implode('\\n', $product->getErrors()); $results[$n]->affectedRows = 1; $n++; $this->_migrateImages($product->product_id, $data['product_images'], $results); } } return $results; }
/** * downloads a file * * @return void */ function downloadFile() { $input = JFactory::getApplication()->input; $user = JFactory::getUser(); $productfile_id = intval($input->getInt('id', 0)); $product_id = intval($input->getInt('product_id', 0)); $link = 'index.php?option=com_citruscart&controller=products&view=products&task=view&id=' . $product_id; Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = CitruscartHelperBase::getInstance('ProductDownload', 'CitruscartHelper'); if (!($canView = $helper->canDownload($productfile_id, JFactory::getUser()->id))) { $this->messagetype = 'notice'; $this->message = JText::_('COM_CITRUSCART_NOT_AUTHORIZED_TO_DOWNLOAD_FILE'); $this->setRedirect($link, $this->message, $this->messagetype); return false; } JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables'); $productfile = JTable::getInstance('ProductFiles', 'CitruscartTable'); $productfile->load($productfile_id); if (empty($productfile->productfile_id)) { $this->messagetype = 'notice'; $this->message = JText::_('COM_CITRUSCART_INVALID_FILE'); $this->setRedirect($link, $this->message, $this->messagetype); return false; } // log and download Citruscart::load('CitruscartFile', 'library.file'); // Log the download $productfile->logDownload($user->id); // After download complete it will update the productdownloads on the basis of the user // geting the ProductDownloadId to updated for which productdownload_max is greater then 0 $productToDownload = $helper->getProductDownloadInfo($productfile->productfile_id, $user->id); if (!empty($productToDownload)) { $productDownload = JTable::getInstance('ProductDownloads', 'CitruscartTable'); $productDownload->load($productToDownload->productdownload_id); $productDownload->productdownload_max = $productDownload->productdownload_max - 1; if (!$productDownload->save()) { // TODO in case product Download is not updating properly . } } if ($downloadFile = CitruscartFile::download($productfile->productfile_path)) { $link = JRoute::_($link, false); $this->setRedirect($link); } }
/** * Uploads a file to associate to an item * * @return unknown_type */ function addfile($fieldname = 'createproductfile_file', $path = 'products_files') { Citruscart::load('CitruscartFile', 'library.file'); $upload = new CitruscartFile(); // handle upload creates upload object properties $upload->handleUpload($fieldname); // then save image to appropriate folder if ($path == 'products_files') { $path = Citruscart::getPath('products_files'); } $upload->setDirectory($path); $dest = $upload->getDirectory() . DS . $upload->getPhysicalName(); // delete the file if dest exists if ($fileexists = JFile::exists($dest)) { JFile::delete($dest); } // save path and filename or just filename if (!JFile::upload($upload->file_path, $dest)) { $this->setError(sprintf(JText::_('COM_CITRUSCART_MOVE_FAILED_FROM'), $upload->file_path, $dest)); return false; } $upload->full_path = $dest; return $upload; }
/** * * Method to upload and verify file if uploaded * @return boolean */ function _verifyFile() { $state = $this->_getState(); $success = false; // Uploads the file Citruscart::load('CitruscartFile', 'library.file'); $upload = new CitruscartFile(); // we have to upload the file if ($state->uploaded_file == '') { // handle upload creates upload object properties $success = $upload->handleUpload('file'); if ($success) { if (strtolower($upload->getExtension()) != 'sql') { $this->setError(JText::_('COM_CITRUSCART_THIS_IS_NOT_AN_SQL_FILE')); return false; } // Move the file to let us reuse it $upload->setDirectory(JFactory::getConfig()->get('tmp_path', JPATH_SITE . '/tmp')); $success = $upload->upload(); if (!$success) { $this->setError($upload->getError()); return false; } $upload->file_path = $upload->getFullPath(); } else { $this->setError(JText::_('COM_CITRUSCART_COULD_NOT_UPLOAD_SQL_FILE' . $upload->getError())); $success = false; } } else { $upload->full_path = $upload->file_path = $state->uploaded_file; $upload->proper_name = CitruscartFile::getProperName($state->uploaded_file); $success = true; } if ($success) { // Set the uploaded file as the file to use during the real import $this->_uploaded_file = $upload->getFullPath(); $this->_uploaded_filename = str_replace(".sql", "", $upload->physicalname); } return $success; }