Skip to content

Commit

Permalink
Initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
janpapenbrock committed Oct 31, 2014
1 parent 8693243 commit 1368014
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
mage-sorted-db-layouts
magento-sorted-db-layouts
======================

Modifies (fixes?) Magento 1.x core when fetching layout updates from core_layout. They are now applied in correct sort_order.

Useful when you want to specify sort order for widgets. Check [this stackexchange thread](http://magento.stackexchange.com/questions/14744/widgets-sort-order-in-different-design-packages) for an error description.

Install with composer
---------------------

```
"require": {
"gruenspar/magento-sorted-db-layouts":"1.*"
},
"repositories": [
{
"url": "https://github.com/gruenspar/magento-sorted-db-layouts",
"type": "git"
}
]
```

Rewrites
--------

- `Mage_Core_Model_Layout_Update`
- `Mage_Core_Model_Resource_Layout`
117 changes: 117 additions & 0 deletions app/code/community/Gruenspar/SortedDbLayouts/Model/Layout/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/**
* This file is part of Gruenspar_SortedDbLayouts.
*
* PHP version 5
*
* @category Gruenspar
* @package Gruenspar_SortedDbLayouts
* @author Jan Papenbrock <j.papenbrock@gruenspar.de>
* @copyright 2014 Gruenspar IT (http://www.gruenspar.de)
* @license MIT
* @link http://www.gruenspar.de
* @since 1.0.0
*/

/**
* Layout update model.
*
* @category Gruenspar
* @package Gruenspar_SortedDbLayouts
* @author Jan Papenbrock <j.papenbrock@gruenspar.de>
* @license MIT
*/
class Gruenspar_SortedDbLayouts_Model_Layout_Update extends Mage_Core_Model_Layout_Update
{
/**
* Add layout update in a sort order.
*
* @param string $update Update XML string.
* @param int $sortOrder Sort order
*
* @return $this
*/
public function addSortedUpdate($update, $sortOrder)
{
$veryLargeIndex = 1000000;
$sortOrder = $sortOrder * 100; // allows 100 elements with the same sort order

$index = $veryLargeIndex + $sortOrder - 1;

do {
$index++;
} while (isset($this->_updates[$index]));


$this->_updates[$index] = $update;

return $this;
}

/**
* Fetch layout updates from DB for given handle.
*
* @param string $handle Handle string.
*
* @return bool
*/
public function fetchDbLayoutUpdates($handle)
{
$_profilerKey = 'layout/db_update: '.$handle;
Varien_Profiler::start($_profilerKey);
$updates = $this->_getUpdates($handle);
if (!count($updates)) {
return false;
}

foreach ($updates as $update) {
$updateStr = $update['xml'];
$sortOrder = $update['sort_order'];

$updateStr = '<update_xml>' . $updateStr . '</update_xml>';
$updateStr = str_replace($this->_subst['from'], $this->_subst['to'], $updateStr);
$updateXml = simplexml_load_string($updateStr, $this->getElementClass());
$this->fetchRecursiveUpdates($updateXml);
$this->addSortedUpdate($updateXml->innerXml(), $sortOrder);
}

ksort($this->_updates);

Varien_Profiler::stop($_profilerKey);
return true;
}

/**
* Get updates by handle
*
* @param string $handle
*
* @return array
*/
protected function _getUpdates($handle)
{
return Mage::getResourceModel('core/layout')->fetchUpdatesByHandle($handle);
}

/**
* Get update string
*
* @deprecated
*
* @param string $handle
*
* @return string
*/
protected function _getUpdateString($handle)
{
$updates = $this->_getUpdates($handle);
$result = array();

foreach ($updates as $update) {
$result[] = $update['xml'];
}

return implode("", $result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* This file is part of Gruenspar_SortedDbLayouts.
*
* PHP version 5
*
* @category Gruenspar
* @package Gruenspar_SortedDbLayouts
* @author Jan Papenbrock <j.papenbrock@gruenspar.de>
* @copyright 2014 Gruenspar IT (http://www.gruenspar.de)
* @license MIT
* @link http://www.gruenspar.de
* @since 1.0.0
*/

/**
* Layout resource model.
*
* @category Gruenspar
* @package Gruenspar_SortedDbLayouts
* @author Jan Papenbrock <j.papenbrock@gruenspar.de>
* @license MIT
*/
class Gruenspar_SortedDbLayouts_Model_Resource_Layout extends Mage_Core_Model_Resource_Layout
{

/**
* Retrieve layout updates by handle.
*
* Overridden to load layout updates for all templates in the design
* hierarchy, not only for exact template match.
*
* @param string $handle Handle.
* @param array $params Params.
*
* @return string
*/
public function fetchUpdatesByHandle($handle, $params = array())
{
$bind = array(
'store_id' => Mage::app()->getStore()->getId(),
'area' => Mage::getSingleton('core/design_package')->getArea(),
'package' => Mage::getSingleton('core/design_package')->getPackageName(),
'theme' => Mage::getSingleton('core/design_package')->getTheme('layout')
);

foreach ($params as $key => $value) {
if (isset($bind[$key])) {
$bind[$key] = $value;
}
}
$bind['layout_update_handle'] = $handle;
$result = '';

$readAdapter = $this->_getReadAdapter();
if ($readAdapter) {
$select = $readAdapter->select()
->from(
array('layout_update' => $this->getMainTable()),
array($this->getIdFieldName(), 'xml', 'sort_order')
)
->join(
array(
'link'=>$this->getTable('core/layout_link')
),
'link.layout_update_id=layout_update.layout_update_id',
''
)
->where('link.store_id IN (0, :store_id)')
->where('link.area = :area')
->where('link.package = :package')
->where('link.theme = :theme')
->where('layout_update.handle = :layout_update_handle')
->order('layout_update.sort_order ' . Varien_Db_Select::SQL_ASC);

$result = $readAdapter->fetchAssoc($select, $bind);
}
return $result;
}
}
22 changes: 22 additions & 0 deletions app/code/community/Gruenspar/SortedDbLayouts/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Gruenspar_SortedDbLayouts>
<version>1.0.0</version>
</Gruenspar_SortedDbLayouts>
</modules>
<global>
<models>
<core>
<rewrite>
<layout_update>Gruenspar_SortedDbLayouts_Model_Layout_Update</layout_update>
</rewrite>
</core>
<core_resource>
<rewrite>
<layout>Gruenspar_SortedDbLayouts_Model_Resource_Layout</layout>
</rewrite>
</core_resource>
</models>
</global>
</config>
9 changes: 9 additions & 0 deletions app/etc/modules/Gruenspar_SortedDbLayouts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config>
<modules>
<Gruenspar_SortedDbLayouts>
<active>true</active>
<codePool>community</codePool>
</Gruenspar_SortedDbLayouts>
</modules>
</config>
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "gruenspar/magento-sorted-db-layouts",
"type": "magento-module",
"license": "MIT",
"description": "Magento: Sorted DB layouts.",
"homepage": "https://github.com/gruenspar/magento-sorted-db-layouts",
"authors": [
{
"name": "Jan Papenbrock",
"email": "j.papenbrock@gruenspar.de",
"homepage": "https://www.gruenspar.de",
"role": "Developer"
}
],
"extra": {
"map": [
["app/code/community/Gruenspar/SortedDbLayouts", "app/code/community/Gruenspar/SortedDbLayouts"],
["app/etc/modules/Gruenspar_SortedDbLayouts.xml", "app/etc/modules/Gruenspar_SortedDbLayouts.xml"]
]
},
"require": {
"php": ">=5.3.0"
},
"suggest": {
"magento-hackathon/magento-composer-installer": "*"
}
}

0 comments on commit 1368014

Please sign in to comment.