-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 in PLUG_FREE/frontend_swagcustomsort from revie…
…w to master * commit '2366992b33fadbe4de9892bd776ab96d1cfcc547': (68 commits) limit product list on save fix permissions on custom sort change clear cache add minimum version check refactoring fix product move buttons url fix not selecting category in category tree combo fixed link category fix aritcle list loading mask fix slow article list for large categories fix the position, when a product was deleted fix defaultsort and category tree combo colapse fix preload of category tree combo Remove sort on category or product remove fixed sorting bug in frontend and backend display default sorting option Add frontend sorting widget cache problem in progress 1 cache problem in progress node load for linked category tree ...
- Loading branch information
Showing
35 changed files
with
3,253 additions
and
15 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
class Shopware_Plugins_Frontend_SwagCustomSort_Bootstrap extends Shopware_Components_Plugin_Bootstrap | ||
{ | ||
/** | ||
* Returns the plugin label which is displayed in the plugin information and | ||
* in the Plugin Manager. | ||
* @return string | ||
*/ | ||
public function getLabel() | ||
{ | ||
return 'Custom sorting'; | ||
} | ||
|
||
/** | ||
* Returns the version of the plugin as a string | ||
* | ||
* @return string|void | ||
* @throws Exception | ||
*/ | ||
public function getVersion() | ||
{ | ||
$info = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'plugin.json'), true); | ||
|
||
if ($info) { | ||
return $info['currentVersion']; | ||
} else { | ||
throw new Exception('The plugin has an invalid version file.'); | ||
} | ||
} | ||
|
||
/** | ||
* Install Plugin / Add Events | ||
* | ||
* @throws Enlight_Exception | ||
* @return bool | ||
*/ | ||
public function install() | ||
{ | ||
if (!$this->assertVersionGreaterThen('5.0.0')) { | ||
throw new \Exception('This plugin requires Shopware 5.0.0 or a later version'); | ||
} | ||
|
||
$this->subscribeEvents(); | ||
$this->createDatabase(); | ||
$this->createAttributes(); | ||
$this->createMenu(); | ||
$this->createForm($this->Form()); | ||
|
||
return array('success' => true, 'invalidateCache' => array('backend')); | ||
} | ||
|
||
/** | ||
* Registers all necessary events. | ||
*/ | ||
public function subscribeEvents() | ||
{ | ||
$this->subscribeEvent('Enlight_Controller_Front_StartDispatch', 'onStartDispatch'); | ||
} | ||
|
||
/** | ||
* Main entry point for the bonus system: Registers various subscribers to hook into shopware | ||
*/ | ||
public function onStartDispatch() | ||
{ | ||
$subscribers = array( | ||
new \Shopware\SwagCustomSort\Subscriber\Resource(Shopware()->Container()), | ||
new \Shopware\SwagCustomSort\Subscriber\ControllerPath($this), | ||
new \Shopware\SwagCustomSort\Subscriber\Frontend($this), | ||
new \Shopware\SwagCustomSort\Subscriber\Backend($this, Shopware()->Models()), | ||
new \Shopware\SwagCustomSort\Subscriber\Sort($this) | ||
); | ||
|
||
foreach ($subscribers as $subscriber) { | ||
$this->Application()->Events()->addSubscriber($subscriber); | ||
} | ||
} | ||
|
||
/** | ||
* Method to always register the custom models and the namespace for the auto-loading | ||
*/ | ||
public function afterInit() | ||
{ | ||
$this->registerCustomModels(); | ||
$this->Application()->Loader()->registerNamespace('Shopware\SwagCustomSort', $this->Path()); | ||
} | ||
|
||
/** | ||
* Creates the backend menu item. | ||
*/ | ||
public function createMenu() | ||
{ | ||
$parent = $this->Menu()->findOneBy(array('label' => 'Artikel')); | ||
|
||
$this->createMenuItem( | ||
array( | ||
'label' => 'Custom sort', | ||
'controller' => 'CustomSort', | ||
'action' => 'Index', | ||
'active' => 1, | ||
'class' => 'sprite-blue-document-text-image', | ||
'parent' => $parent, | ||
'position' => 6, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Creates the plugin database tables over the doctrine schema tool. | ||
*/ | ||
public function createDatabase() | ||
{ | ||
$em = $this->Application()->Models(); | ||
$tool = new \Doctrine\ORM\Tools\SchemaTool($em); | ||
|
||
$classes = array( | ||
$em->getClassMetadata('Shopware\CustomModels\CustomSort\ArticleSort'), | ||
); | ||
|
||
try { | ||
$tool->createSchema($classes); | ||
} catch(\Doctrine\ORM\Tools\ToolsException $e) { | ||
// | ||
} | ||
} | ||
|
||
public function createAttributes() | ||
{ | ||
$em = $this->Application()->Models(); | ||
$em->addAttribute( | ||
's_categories_attributes', | ||
'swag', | ||
'link', | ||
'int(11)', | ||
true, | ||
null | ||
); | ||
$em->addAttribute( | ||
's_categories_attributes', | ||
'swag', | ||
'show_by_default', | ||
'int(1)', | ||
true, | ||
0 | ||
); | ||
$em->addAttribute( | ||
's_categories_attributes', | ||
'swag', | ||
'deleted_position', | ||
'int(11)', | ||
true, | ||
null | ||
); | ||
|
||
$em->generateAttributeModels(array( | ||
's_categories_attributes' | ||
)); | ||
} | ||
|
||
protected function createForm(Shopware\Models\Config\Form $form) | ||
{ | ||
$form->setElement('text', 'swagCustomSortName', | ||
array( | ||
'label' => 'Name', | ||
'value' => 'Custom Sorting', | ||
'description' => 'The new sort, will be visible in the frontend under this name option.', | ||
'required' => true, | ||
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP | ||
) | ||
); | ||
|
||
$this->addFormTranslations( | ||
array('en_GB' => array( | ||
'swagCustomSortName' => array( | ||
'label' => 'Name', | ||
'description' => 'The new sort, will be visible in the frontend under this name option.', | ||
'value' => 'Custom Sorting' | ||
) | ||
)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
namespace Shopware\SwagCustomSort\Components; | ||
|
||
class Listing | ||
{ | ||
/** | ||
* @var Shopware_Components_Config | ||
*/ | ||
private $config = null; | ||
|
||
/** | ||
* @var /Shopware\Components\Model\ModelManager | ||
*/ | ||
private $em = null; | ||
|
||
private $categoryAttributesRepo = null; | ||
|
||
private $categoryRepo = null; | ||
|
||
private $customSortRepo = null; | ||
|
||
public function __construct(Shopware_Components_Config $config, \Shopware\Components\Model\ModelManager $em) { | ||
$this->config = $config; | ||
$this->em = $em; | ||
} | ||
|
||
public function getConfig() | ||
{ | ||
return $this->config; | ||
} | ||
|
||
public function getEntityManager() | ||
{ | ||
return $this->em; | ||
} | ||
|
||
public function getCategoryAttributesRepository() | ||
{ | ||
if ($this->categoryAttributesRepo === null) { | ||
$this->categoryAttributesRepo = $this->getEntityManager()->getRepository('Shopware\Models\Attribute\Category'); | ||
} | ||
|
||
return $this->categoryAttributesRepo; | ||
} | ||
|
||
public function getCategoryRepository() | ||
{ | ||
if ($this->categoryRepo === null) { | ||
$this->categoryRepo = $this->getEntityManager()->getRepository('Shopware\Models\Category\Category'); | ||
} | ||
|
||
return $this->categoryRepo; | ||
} | ||
|
||
public function getCustomSortRepository() | ||
{ | ||
if ($this->customSortRepo === null) { | ||
$this->customSortRepo = $this->getEntityManager()->getRepository('Shopware\CustomModels\CustomSort\ArticleSort'); | ||
} | ||
|
||
return $this->customSortRepo; | ||
} | ||
|
||
public function showCustomSortName($categoryId) | ||
{ | ||
$sortName = $this->getFormattedSortName(); | ||
if (empty($sortName)) { | ||
return false; | ||
} | ||
|
||
$hasCustomSort = $this->hasCustomSort($categoryId); | ||
if ($hasCustomSort) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getFormattedSortName() | ||
{ | ||
$formattedName = $this->getSortName(); | ||
|
||
return trim($formattedName); | ||
} | ||
|
||
public function getSortName() | ||
{ | ||
$name = $this->getConfig()->get('swagCustomSortName'); | ||
|
||
return $name; | ||
} | ||
|
||
public function hasCustomSort($categoryId) | ||
{ | ||
$isLinked = $this->isLinked($categoryId); | ||
if ($isLinked) { | ||
return true; | ||
} | ||
|
||
$hasOwnSort = $this->hasOwnSort($categoryId); | ||
if ($hasOwnSort) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function isLinked($categoryId) | ||
{ | ||
/* @var \Shopware\Models\Attribute\Category $categoryAttributes */ | ||
$categoryAttributes = $this->getCategoryAttributesRepository()->findOneBy(array('categoryId' => $categoryId)); | ||
if (!$categoryAttributes instanceof \Shopware\Models\Attribute\Category) { | ||
return false; | ||
} | ||
|
||
$linkedCategoryId = $categoryAttributes->getSwagLink(); | ||
if ($linkedCategoryId === null) { | ||
return false; | ||
} | ||
|
||
/* @var \Shopware\Models\Category\Category $category */ | ||
$category = $this->getCategoryRepository()->find($linkedCategoryId); | ||
if (!$category instanceof \Shopware\Models\Category\Category) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Checks whether this category has own custom sort | ||
* | ||
* @return bool | ||
*/ | ||
public function hasOwnSort($categoryId) | ||
{ | ||
return $this->getCustomSortRepository()->hasCustomSort($categoryId); | ||
} | ||
|
||
/** | ||
* Checks whether this category has to use its custom sort by default, e.g. on category load use this custom sort | ||
* | ||
* @return bool | ||
*/ | ||
public function showCustomSortAsDefault($categoryId) | ||
{ | ||
/* @var \Shopware\Models\Attribute\Category $categoryAttributes */ | ||
$categoryAttributes = $this->getCategoryAttributesRepository()->findOneBy(array('categoryId' => $categoryId)); | ||
if (!$categoryAttributes instanceof \Shopware\Models\Attribute\Category) { | ||
return false; | ||
} | ||
|
||
$useDefaultSort = (bool) $categoryAttributes->getSwagShowByDefault(); | ||
$hasOwnSort = $this->hasOwnSort($categoryId); | ||
if ($useDefaultSort && $hasOwnSort) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Returns the id of the linked category. | ||
* | ||
* @return int | ||
*/ | ||
public function getLinkedCategoryId($categoryId) | ||
{ | ||
/* @var \Shopware\Models\Attribute\Category $categoryAttributes */ | ||
$categoryAttributes = $this->getCategoryAttributesRepository()->findOneBy(array('categoryId' => $categoryId)); | ||
if (!$categoryAttributes instanceof \Shopware\Models\Attribute\Category) { | ||
return false; | ||
} | ||
|
||
$linkedCategoryId = $categoryAttributes->getSwagLink(); | ||
if ($linkedCategoryId === null) { | ||
return false; | ||
} | ||
|
||
/* @var \Shopware\Models\Category\Category $category */ | ||
$category = $this->getCategoryRepository()->find($linkedCategoryId); | ||
if (!$category instanceof \Shopware\Models\Category\Category) { | ||
return false; | ||
} | ||
|
||
return $linkedCategoryId; | ||
} | ||
} |
Oops, something went wrong.