Skip to content

Commit 9a53e73

Browse files
committed
Merge pull request #7 from TheExtensionLab/develop
Develop
2 parents f38ac6c + ffd4314 commit 9a53e73

4 files changed

Lines changed: 118 additions & 97 deletions

File tree

app/code/community/TheExtensionLab/StatusColors/Helper/Data.php

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* StatusColors Data Helper
45
*
@@ -12,70 +13,56 @@ class TheExtensionLab_StatusColors_Helper_Data extends Mage_Core_Helper_Abstract
1213
{
1314
protected $_statusCollection = null;
1415

15-
/**
16-
* @return array
17-
*
18-
* Note: This is called from layout XML <arguments helper="theextensionlab_statuscolors/data/getStatusColorColumn"/>
19-
*/
2016
public function getStatusColorColumn()
2117
{
2218
$column = array(
23-
'header' => Mage::helper('sales')->__('Status Color'),
24-
'type' => 'text',
25-
'index' => 'color',
26-
'width' => '200px',
19+
'header' => Mage::helper('sales')->__('Status Color'),
20+
'type' => 'text',
21+
'index' => 'color',
22+
'width' => '200px',
2723
'frame_callback' => array($this, 'decorateStatusUsingRowData')
2824
);
2925

3026
return $column;
3127
}
3228

33-
/**
34-
* Decorate status column values - but don't load collection as we don't need to.
35-
* @return string
36-
*/
3729
public function decorateStatusUsingRowData($value)
3830
{
39-
$statusHtml = '<span class="custom-color" style="background-color:'.$value.';">
40-
<span>'.$value.'</span>
31+
$statusHtml = '<span class="custom-color" style="background-color:' . $value . ';">
32+
<span>' . $value . '</span>
4133
</span>';
4234
return $statusHtml;
4335
}
4436

45-
/**
46-
* Decorate status column values
47-
*
48-
* @return string
49-
*/
5037
public function decorateStatus($value, $row)
5138
{
52-
//Get the status of this row
5339
$rowStatus = $row->getStatus();
54-
55-
//Get full collection of statuses (cached)
5640
$statusCollection = $this->_getStatusCollection();
41+
$customColor = null;
5742

58-
//Run through status collection and when it matches the current row set $customColor
5943
foreach ($statusCollection as $status) {
6044
if ($status->getStatus() == $rowStatus) {
6145
$customColor = $this->getColorOrDefault($status->getColor());
6246
}
6347
}
6448

65-
//Wrap our status within a span to be styled with css
66-
$statusHtml = '<span class="custom-color" style="background-color:'.$customColor.';">
67-
<span>'.$value.'</span>
68-
</span>';
49+
$statusHtml = $this->_wrapInBackgroundColorSpan($value, $customColor);
6950

7051
return $statusHtml;
7152
}
7253

73-
/**
74-
* Retrieve status color
75-
*
76-
* @param string $code
77-
* @return string
78-
*/
54+
private function _wrapInBackgroundColorSpan($value, $backgroundColor)
55+
{
56+
if (!$backgroundColor) {
57+
return $value;
58+
}
59+
60+
$html = '<span class="custom-color" style="background-color:' . $backgroundColor . ';">
61+
<span>' . $value . '</span>
62+
</span>';
63+
return $html;
64+
}
65+
7966
public function getStatusColor($code)
8067
{
8168
$status = Mage::getModel('sales/order_status')
@@ -85,15 +72,12 @@ public function getStatusColor($code)
8572

8673
public function getColorOrDefault($color)
8774
{
88-
if(empty($color)){
75+
if (empty($color)) {
8976
return Mage::getStoreConfig('admin/order_grid/default_status_color');
9077
}
9178
return $color;
9279
}
9380

94-
/**
95-
* @return Mage_Sales_Model_Resource_Order_Status_Collection|null
96-
*/
9781
protected function _getStatusCollection()
9882
{
9983
if ($this->_statusCollection === null) {

app/code/community/TheExtensionLab/StatusColors/Model/Observer.php

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
/**
44
* StatusColors Observer Model
@@ -9,89 +9,119 @@
99
* @license Open Software License (OSL 3.0)
1010
* @author James Anelay @ TheExtensionLab <james@theextensionlab.com>
1111
*/
12-
1312
class TheExtensionLab_StatusColors_Model_Observer
1413
{
15-
/**
16-
* @param Varien_Event_Observer $observer
17-
* @return $this
18-
*/
14+
protected $_currentOrderGridBlockClass = 'Mage_Adminhtml_Block_Sales_Order_Grid';
15+
1916
public function adminhtmlBlockHtmlBefore(Varien_Event_Observer $observer)
2017
{
2118
$block = $observer->getEvent()->getBlock();
2219

23-
if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
20+
$this->_catchRewrittenOrderGridThatDoesntExtendOriginalClass();
2421

25-
//Get the status column and add a frame_callback which adds the colour to the html
26-
$column = $block->getColumn('status');
27-
$column->setFrameCallback(array($this->getHelper(), 'decorateStatus'));
22+
if ($this->_isBlockOrderGrid($block)) {
23+
$this->_addDecorateStatusFrameCallback($block->getColumn('status'));
2824
return $this;
2925
}
3026

31-
//Adds a new feild to the new/edit status forms
32-
if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Status_Edit_Form || $block instanceof Mage_Adminhtml_Block_Sales_Order_Status_New_Form) {
27+
if ($this->_isStatusFormBlock($block)) {
3328
$form = $block->getForm();
3429
$elements = $form->getElements();
3530
foreach ($elements as $element) {
36-
switch($element->getId()){
37-
case "base_fieldset":
38-
//Add a color field to the fieldset
39-
$element->addField('color', 'text',
40-
array(
41-
'name' => 'color',
42-
'label' => Mage::helper('sales')->__('Status Color'),
43-
'class' => 'color {hash:true,adjust:false}'
44-
)
45-
);
46-
47-
//Once we have added a new field we need to set the form values again to populate this feild
48-
$model = Mage::registry('current_status');
49-
if ($model) {
50-
$form->addValues($model->getData());
51-
}
52-
53-
break;
31+
if ($this->_isBaseFieldset($element)) {
32+
$this->_addColorInputFeild($element);
33+
$this->_populateFormWithNewFeild($form);
5434
}
5535
}
5636
}
5737

5838
return $this;
5939
}
6040

61-
/**
62-
* This function adds the span (color) around the status using preg_replace
63-
* could have used a template but that would mean if the template was edited
64-
* we would need manually update it, using preg_replace there isn't a need for that.
65-
*
66-
* @param Varien_Event_Observer $observer
67-
* @return $this
68-
*/
41+
private function _catchRewrittenOrderGridThatDoesntExtendOriginalClass()
42+
{
43+
$rewriteNode = (string)Mage::getConfig()->getNode('global/blocks/adminhtml/rewrite/sales_order_grid');
44+
45+
if ($rewriteNode) {
46+
$this->_currentOrderGridBlockClass = $rewriteNode;
47+
}
48+
49+
return $this->_currentOrderGridBlockClass;
50+
}
51+
52+
private function _isBlockOrderGrid(Mage_Core_Block_Abstract $block)
53+
{
54+
return $block instanceof $this->_currentOrderGridBlockClass;
55+
}
56+
57+
private function _addDecorateStatusFrameCallback($column)
58+
{
59+
$column->setFrameCallback(array($this->getHelper(), 'decorateStatus'));
60+
}
61+
62+
63+
private function _isStatusFormBlock(Mage_Core_Block_Abstract $block)
64+
{
65+
return $block instanceof Mage_Adminhtml_Block_Sales_Order_Status_Edit_Form
66+
|| $block instanceof Mage_Adminhtml_Block_Sales_Order_Status_New_Form;
67+
}
68+
69+
private function _isBaseFieldset($element){
70+
return $element->getId() == "base_fieldset";
71+
}
72+
73+
private function _addColorInputFeild($fieldset)
74+
{
75+
$fieldset->addField(
76+
'color', 'text',
77+
array(
78+
'name' => 'color',
79+
'label' => Mage::helper('sales')->__('Status Color'),
80+
'class' => 'color {hash:true,adjust:false}'
81+
)
82+
);
83+
}
84+
85+
private function _populateFormWithNewFeild($form)
86+
{
87+
$model = Mage::registry('current_status');
88+
if ($model) {
89+
$form->addValues($model->getData());
90+
}
91+
}
92+
6993
public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $observer)
7094
{
7195
$block = $observer->getEvent()->getBlock();
96+
$transport = $observer->getEvent()->getTransport();
97+
98+
if($this->_isOrderInfoBlock($block)){
99+
$customColor = Mage::helper('theextensionlab_statuscolors')->getStatusColor(
100+
$block->getOrder()->getStatus()
101+
);
102+
103+
$html = $this->_addBackgroundColorToStatusElement($transport->getHtml(),$customColor);
72104

73-
switch($block->getNameInLayout()) {
74-
case "order_info":
75-
$transport = $observer->getEvent()->getTransport();
76-
$html = $transport->getHtml();
77-
$customColor = Mage::helper('theextensionlab_statuscolors')->getStatusColor($block->getOrder()->getStatus());
78-
$html = preg_replace(
79-
'/id="order_status"/',
80-
'$0 class="custom-color" style="background-color:'.$customColor.';"',
81-
$html
82-
);
83-
84-
$transport->setHtml($html);
85-
break;
105+
$transport->setHtml($html);
86106
}
87107

88108
return $this;
89109
}
90110

91-
/**
92-
* @return TheExtensionLab_StatusColors_Helper_Data
93-
*/
94-
public function getHelper()
111+
private function _addBackgroundColorToStatusElement($html,$backgroundColor){
112+
$html = preg_replace(
113+
'/id="order_status"/',
114+
'$0 class="custom-color" style="background-color:' . $backgroundColor . ';"',
115+
$html
116+
);
117+
return $html;
118+
}
119+
120+
private function _isOrderInfoBlock($block){
121+
return $block->getNameInLayout() == "order_info";
122+
}
123+
124+
protected function getHelper()
95125
{
96126
return Mage::helper('theextensionlab_statuscolors');
97127
}

app/code/community/TheExtensionLab/StatusColors/Model/Resource/Setup.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
{
33
public function addInstallationSuccessfulNotification(){
44
$docUrl = "http://docs.theextensionlab.com/status-colors/installation.html";
5-
Mage::getModel('adminnotification/inbox')->addNotice(
6-
'<strong>You have successfully installed TheExtensionLab_StatusColors:</strong>
5+
6+
$inboxModel = Mage::getModel('adminnotification/inbox');
7+
8+
if(!method_exists($inboxModel,'addNotice')){
9+
return;
10+
}
11+
12+
$inboxModel->addNotice(
13+
'You have successfully installed TheExtensionLab_StatusColors:
714
Status colors can be configured in System > Order Statuses and
8-
other config options found at System > Configuration > Advanced > Admin > Order Grid</a>',
15+
other config options found at System > Configuration > Advanced > Admin > Order Grid',
916
'For full up to date documenation see <a href="'.$docUrl.'" target="_blank">'.$docUrl.'</a>',
1017
'http://docs.theextensionlab.com/status-colors/configuration.html',
1118
true

app/code/community/TheExtensionLab/StatusColors/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<config>
1111
<modules>
1212
<TheExtensionLab_StatusColors>
13-
<version>1.2.0</version>
13+
<version>1.3.0</version>
1414
</TheExtensionLab_StatusColors>
1515
</modules>
1616

0 commit comments

Comments
 (0)