Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Took store scope into account when uploading and rendering product downloads. #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Block/Adminhtml/Product/Edit/Tab/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Download extends \Magento\Backend\Block\Widget
* @var string
*/
protected $_template = 'product/tab/download.phtml';

/**
* Core registry
*
Expand All @@ -37,8 +38,12 @@ class Download extends \Magento\Backend\Block\Widget
* @param \Sebwite\ProductDownloads\Model\Download $download
* @param array $data
*/
public function __construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $coreRegistry, \Sebwite\ProductDownloads\Model\Download $download,
array $data = [])
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Sebwite\ProductDownloads\Model\Download $download,
array $data = []
)
{
$this->coreRegistry = $coreRegistry;
$this->download = $download;
Expand Down Expand Up @@ -103,7 +108,8 @@ public function isVirtual()
*/
public function getDownloads()
{
return $this->download->getResource()->getDownloadsForProduct($this->getProduct()->getId());
$product = $this->getProduct();
return $this->download->getResource()->getDownloadsForProductInStore($product->getId(), $product->getStoreId(), false);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Block/Product/Downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function setTabTitle()
*/
public function getDownloads()
{
return $this->download->getDownloadsForProduct($this->getProduct()->getId());
$product = $this->getProduct();
return $this->download->getDownloadsForProductInStore($product->getId(), $product->getStoreId());
}

/**
Expand Down
68 changes: 0 additions & 68 deletions Block/Product/Downloads1.php

This file was deleted.

5 changes: 3 additions & 2 deletions Controller/Adminhtml/Delete/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,20 @@ public function execute()
$download->load($downloadId);
$name = $download->getName();
$productId = $download['product_id'];
$storeId = $download['store_id'];
$download->delete();
$this->messageManager->addSuccess(__('The download has been deleted.'));
$this->_eventManager->dispatch('adminhtml_sebwite_productdownloads_download_on_delete', ['name' => $name, 'status' => 'success']);

$resultRedirect->setPath('catalog/product/edit/*', ['id' => $productId, 'active_tab' => 'downloads']);
$resultRedirect->setPath('catalog/product/edit/*', ['id' => $productId, 'active_tab' => 'downloads', 'store' => $storeId]);

return $resultRedirect;
} catch (\Exception $e) {
$this->_eventManager->dispatch('adminhtml_sebwite_productdownloads_delete_on_delete', ['name' => $name, 'status' => 'fail']);
// display error message
$this->messageManager->addError($e->getMessage());
// go back to edit form
$resultRedirect->setPath('catalog/product/edit/', ['id' => $productId, 'active_tab' => 'downloads']);
$resultRedirect->setPath('catalog/product/edit/', ['id' => $productId, 'active_tab' => 'downloads', 'store' => $storeId]);

return $resultRedirect;
}
Expand Down
4 changes: 3 additions & 1 deletion Model/Adminhtml/Download/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public function execute(EventObserver $observer)
// Get current product
$product = $this->coreRegistry->registry('product');
$productId = $product->getId();
$storeId = $product->getStoreId();

// Loop through uploaded downlaods
// Loop through uploaded downloads
foreach ($downloads as $download) {

// Upload file
Expand All @@ -68,6 +69,7 @@ public function execute(EventObserver $observer)
$download->setDownloadFile($uploadedDownload['name']);
$download->setDownloadType($uploadedDownload['type']);
$download->setProductId($productId);
$download->setStoreId($storeId);
$download->save();
}
}
Expand Down
13 changes: 13 additions & 0 deletions Model/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public function getDownloadsForProduct($downloadId)
return $this->getResource()->getDownloadsForProduct($downloadId);
}

/**
* Get downloads
*
* @param $downloadId
* @param null $storeId
* @param bool $fallbackToDefault
* @return mixed
*/
public function getDownloadsForProductInStore($downloadId, $storeId = null, $fallbackToDefault = true)
{
return $this->getResource()->getDownloadsForProductInStore($downloadId, $storeId, $fallbackToDefault);
}

/**
* Initialize resource model
*
Expand Down
30 changes: 30 additions & 0 deletions Model/Resource/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ public function getDownloadsForProduct($id)
return $adapter->fetchAll($select, $binds);
}

/**
* Retrieve all Downloads for product - within the desired store view.
*
* @param $id
* @param null $storeId
* @param bool $fallbackToDefault
* @return array
*/
public function getDownloadsForProductInStore($id, $storeId = null, $fallbackToDefault = true)
{
$adapter = $this->getConnection();

if (is_null($storeId)) {
$storeId = 0;
}

$storeWhereStatement = (int) $storeId;

if ($fallbackToDefault) {
$storeWhereStatement = 'IF((SELECT COUNT(*) AS count FROM ' . $this->getMainTable() . ' WHERE store_id = '. (int) $storeId .') > 0, ' . (int) $storeId . ', 0)';
}

$select = $adapter->select()->from($this->getMainTable())->where('product_id = :product_id AND store_id = ' . $storeWhereStatement);
$binds = [
'product_id' => (int) $id
];

return $adapter->fetchAll($select, $binds);
}

/**
* Check if author url_key exist
* return author id if author exists
Expand Down
48 changes: 48 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Sebwite\ProductDownloads\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
* @codeCoverageIgnore
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {@inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '2.0.1', '<')) {
$setup->getConnection()->addColumn(
$setup->getTable('sebwite_product_downloads'),
'store_id',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'The Magento store id.',
'after' => 'product_id'
]
);

if ($setup->getConnection()->tableColumnExists($setup->getTable('sebwite_product_downloads'), 'store_id')) {
$setup->getConnection()->addForeignKey(
$setup->getFkName('sebwite_product_downloads', 'store_id', 'store', 'store_id'),
$setup->getTable('sebwite_product_downloads'),
'store_id',
$setup->getTable('store'),
'store_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
);
}
}

$setup->endSetup();
}
}
8 changes: 2 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
"name": "sebwite/magento2-product-downloads",
"description": "Magento 2.0 product download module",
"type": "magento2-module",
"version": "1.0.2",
"version": "2.0.1",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/magento-composer-installer": "*"
},
"autoload": {
"psr-4": {
"Sebwite\\ProductDownloads\\": ""
Expand All @@ -19,4 +15,4 @@
"registration.php"
]
}
}
}
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Sebwite_ProductDownloads" setup_version="2.0.0">
<module name="Sebwite_ProductDownloads" setup_version="2.0.1">
</module>
</config>