Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
[~TASK] Checked extension against Magento coding standard (ruleset from Magento 2) and solved most violations
[~TASK] Added some more documentation
[~TASK] Moved rearrangement of the shipping rates to helper
[~TASK] Changed license in Model/System/Config.php to CC-BY 3.0 too
  • Loading branch information
therouv committed Mar 16, 2012
1 parent 3440ff2 commit 9591631
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 107 deletions.
Expand Up @@ -17,21 +17,29 @@
* @copyright Copyright (c) 2012 Bastian Ike (http://thebod.de/)
* @author Bastian Ike <b-ike@b-ike.de>
* @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
* @todo move inline html into templates
*/

class Thebod_Shippingrates_Block_Adminhtml_Config extends Mage_Adminhtml_Block_System_Config_Form_Field {

class Thebod_Shippingrates_Block_Adminhtml_Config extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/**
* @var array Add buttons
*/
protected $_addRowButtonHtml = array();

/**
* @var array Remove buttons
*/
protected $_removeRowButtonHtml = array();

/**
* basic template for shipping rates configurator
*
* @todo move inline html into templates
*
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$this->setElement($element);

$html = '<div id="shippingconfig_template" style="display:none">';
Expand Down Expand Up @@ -128,7 +136,8 @@ function shippingconfig_filter_hide() {
* @param int $key
* @return string
*/
protected function _getRowTemplateHtml($key = 0) {
protected function _getRowTemplateHtml($key = 0)
{
$html = '<li style="display: block; width: 550px;">';
$html .= '<div style="float: left;">';
$html .= '<input class="name input-text ' . $this->_getDisabled() . '" style="vertical-align: top; width: 100px; margin: 0 6px;" name="' . $this->getElement()->getName() . '[code][]" value="' . $this->_getValue('code/' . $key) . '"/>';
Expand All @@ -149,7 +158,8 @@ protected function _getRowTemplateHtml($key = 0) {
*
* @return string
*/
protected function _getDisabled() {
protected function _getDisabled()
{
return $this->getElement()->getDisabled() ? ' disabled' : '';
}

Expand All @@ -159,27 +169,29 @@ protected function _getDisabled() {
* @param string $key
* @return string
*/
protected function _getValue($key) {
protected function _getValue($key)
{
return $this->getElement()->getData('value/' . $key);
}

/**
* returns 'add' button html code
*
* @param $container
* @param $template
* @param string $container
* @param string $template
* @param string $title
* @return string
*/
protected function _getAddRowButtonHtml($container, $template, $title = 'Add') {
protected function _getAddRowButtonHtml($container, $template, $title = 'Add')
{
if (!isset($this->_addRowButtonHtml[$container])) {
$this->_addRowButtonHtml[$container] = $this->getLayout()->createBlock('adminhtml/widget_button')
->setType('button')
->setClass('add ' . $this->_getDisabled())
->setLabel($this->__($title))
->setOnClick("Element.insert($('" . $container . "'), {bottom: $('" . $template . "').innerHTML})")
->setDisabled($this->_getDisabled())
->toHtml();
->setType('button')
->setClass('add ' . $this->_getDisabled())
->setLabel($this->__($title))
->setOnClick("Element.insert($('" . $container . "'), {bottom: $('" . $template . "').innerHTML})")
->setDisabled($this->_getDisabled())
->toHtml();
}
return $this->_addRowButtonHtml[$container];
}
Expand All @@ -191,16 +203,16 @@ protected function _getAddRowButtonHtml($container, $template, $title = 'Add') {
* @param string $title
* @return array
*/
protected function _getRemoveRowButtonHtml($selector = 'li', $title = 'Delete') {
protected function _getRemoveRowButtonHtml($selector = 'li', $title = 'Delete')
{
if (!$this->_removeRowButtonHtml) {
$this->_removeRowButtonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')
->setType('button')
->setClass('delete ' . $this->_getDisabled())
->setOnClick("Element.remove($(this).up('" . $selector . "'))")
->setDisabled($this->_getDisabled())
->toHtml();
->setType('button')
->setClass('delete ' . $this->_getDisabled())
->setOnClick("Element.remove($(this).up('" . $selector . "'))")
->setDisabled($this->_getDisabled())
->toHtml();
}
return $this->_removeRowButtonHtml;
}

}
38 changes: 35 additions & 3 deletions src/app/code/community/Thebod/Shippingrates/Helper/Data.php
Expand Up @@ -18,6 +18,38 @@
* @author Bastian Ike <b-ike@b-ike.de>
* @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
*/

class Thebod_Shippingrates_Helper_Data extends Mage_Core_Helper_Abstract {
}
class Thebod_Shippingrates_Helper_Data extends Mage_Core_Helper_Abstract
{
/**
* Rearranges the complete shipping rates array
*
* @param array $data array(
* code => array('', 'a', 'b'),
* price => array('', 10, 20),
* description => array('', 'desc1', 'desc2')
* )
* @return array array(
* methodid => array(
* code => 'a',
* price => 10,
* description => 'desc1'
* )
* )
*/
public function rearrangeShippingRates($data)
{
$methods = array();
foreach ($data as $key => $value) {
/*
* $methodId => id of this method
* $methodValue => value of this entry
*/
foreach ($value as $methodId => $methodValue) {
/* we ignore this if $methodId == 0 */
if ($methodId) {
$methods[$methodId][$key] = $methodValue;
}
}
}
}
}
99 changes: 43 additions & 56 deletions src/app/code/community/Thebod/Shippingrates/Model/Carrier.php
Expand Up @@ -18,8 +18,11 @@
* @author Bastian Ike <b-ike@b-ike.de>
* @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
*/

class Thebod_Shippingrates_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract {
class Thebod_Shippingrates_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract
{
/**
* @var string Shipping Method Code
*/
protected $_code = 'shippingrates';

/**
Expand All @@ -28,16 +31,17 @@ class Thebod_Shippingrates_Model_Carrier extends Mage_Shipping_Model_Carrier_Abs
* @param string $code
* @return string
*/
public function getNotificationMail($code) {
public function getNotificationMail($code)
{
$data = $this->getConfigData('shippingconfig');

if(!is_array($data)) {
if (!is_array($data)) {
$data = unserialize(base64_decode($data));
}

/* searches correct mail address */
foreach($data['code'] as $k => $v) {
if(($this->_code . '_' . $v) == $code) {
foreach ($data['code'] as $k => $v) {
if (($this->_code . '_' . $v) == $code) {
return $data['email'][$k];
}
}
Expand All @@ -50,59 +54,60 @@ public function getNotificationMail($code) {
* @param Mage_Shipping_Model_Rate_Request $request
* @return boolean
*/
public function checkRate(array $rate, Mage_Shipping_Model_Rate_Request $request) {
if(!Mage::getSingleton('checkout/session')->hasQuote()) {
public function checkRate(array $rate, Mage_Shipping_Model_Rate_Request $request)
{
if (!Mage::getSingleton('checkout/session')->hasQuote()) {
return true;
}

if(!isset($rate['filter'])) {
if (!isset($rate['filter'])) {
return true;
}

$filters = explode(';', $rate['filter']);
$passed = true;
foreach($filters as $filter) {
foreach ($filters as $filter) {
$filter = explode(':', $filter);
$condition = $filter[0];
$value = isset($filter[1]) && $filter[1] ? $filter[1] : false;

if($value === false) {
if ($value === false) {
continue;
}

switch($condition) {
switch ($condition) {
case 'min_qty':
if($request->getPackageQty() < $value) {
if ($request->getPackageQty() < $value) {
$passed = false;
}
break;

case 'max_qty':
if($request->getPackageQty() > $value) {
if ($request->getPackageQty() > $value) {
$passed = false;
}
break;

case 'min_subtotal':
if($request->getPackageValueWithDiscount() < $value) {
if ($request->getPackageValueWithDiscount() < $value) {
$passed = false;
}
break;

case 'max_subtotal':
if($request->getPackageValueWithDiscount() > $value) {
if ($request->getPackageValueWithDiscount() > $value) {
$passed = false;
}
break;

case 'min_weight':
if($request->getPackageWeight() < $value) {
if ($request->getPackageWeight() < $value) {
$passed = false;
}
break;

case 'max_weight':
if($request->getPackageWeight() > $value) {
if ($request->getPackageWeight() > $value) {
$passed = false;
}
break;
Expand All @@ -118,23 +123,22 @@ public function checkRate(array $rate, Mage_Shipping_Model_Rate_Request $request
* @param Mage_Shipping_Model_Rate_Request $request
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
if(!$this->getConfigFlag('active')) {
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}

$result = Mage::getModel('shipping/rate_result');

$rates = $this->getRates($this->getConfigData('shippingconfig'));

foreach($rates as $rate) {
if($this->checkRate($rate, $request)) {
$result = Mage::getModel('shipping/rate_result');
foreach ($rates as $rate) {
if ($this->checkRate($rate, $request)) {
$method = Mage::getModel('shipping/rate_result_method');

$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));

//$method->setMethod($this->_code . '_' . $rate['code']);
$method->setMethod($rate['code']);
$method->setMethodTitle($rate['title']);

Expand All @@ -143,7 +147,6 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
$result->append($method);
}
}

return $result;
}

Expand All @@ -153,42 +156,27 @@ public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
* @param Mage_Shipping_Model_Rate_Request $data
* @return array
*/
public function getRates($data) {
$rates = array();
$methods = array();

if(!is_array($data)) {
public function getRates($data)
{
if (!is_array($data)) {
$data = unserialize(base64_decode($data));
}

/* rearrange array */
/* $data: array(code => array('', 'a', 'b'), price => array('', 10, 20), description => array('', 'desc1', 'desc2')) */
/* $key: code, then price, then data - $value: array*/
foreach($data as $key => $value) {
/* $value: array, $methodid: id of this method, $methodvalue: value of this entry */
foreach($value as $methodid => $methodvalue) {
/* we ignore this if methodid == 0 */
if($methodid) {
/* methods = array(methodid => array(code => 'a', price => 10, description => 'desc1')) ... */
$methods[$methodid][$key] = $methodvalue;
}
}
}

foreach($methods as $method) {
$code = trim($method['code']);
$price = trim($method['price']);
$methods = Mage::helper('shippingrates')->rearrangeShippingRates($data);
$rates = array();
foreach ($methods as $method) {
$code = trim($method['code']);
$price = trim($method['price']);
$filter = trim($method['filter']);
$title = nl2br(trim($method['description']));
$title = nl2br(trim($method['description']));

$rates[] = array(
'code' => $code,
'title' => $title,
'price' => $price,
'code' => $code,
'title' => $title,
'price' => $price,
'filter' => $filter,
);
}

krsort($rates);

return $rates;
Expand All @@ -202,9 +190,8 @@ public function getRates($data) {
public function getAllowedMethods()
{
$allowedMethods = array(
$this->_code => $this->getConfigData('name'),
$this->_code => $this->getConfigData('name')
);

return $allowedMethods;
}
}
}

0 comments on commit 9591631

Please sign in to comment.