|
1 | | -<?php |
| 1 | +<?php |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * StatusColors Observer Model |
|
9 | 9 | * @license Open Software License (OSL 3.0) |
10 | 10 | * @author James Anelay @ TheExtensionLab <james@theextensionlab.com> |
11 | 11 | */ |
12 | | - |
13 | 12 | class TheExtensionLab_StatusColors_Model_Observer |
14 | 13 | { |
15 | | - /** |
16 | | - * @param Varien_Event_Observer $observer |
17 | | - * @return $this |
18 | | - */ |
| 14 | + protected $_currentOrderGridBlockClass = 'Mage_Adminhtml_Block_Sales_Order_Grid'; |
| 15 | + |
19 | 16 | public function adminhtmlBlockHtmlBefore(Varien_Event_Observer $observer) |
20 | 17 | { |
21 | 18 | $block = $observer->getEvent()->getBlock(); |
22 | 19 |
|
23 | | - if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid) { |
| 20 | + $this->_catchRewrittenOrderGridThatDoesntExtendOriginalClass(); |
24 | 21 |
|
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')); |
28 | 24 | return $this; |
29 | 25 | } |
30 | 26 |
|
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)) { |
33 | 28 | $form = $block->getForm(); |
34 | 29 | $elements = $form->getElements(); |
35 | 30 | 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); |
54 | 34 | } |
55 | 35 | } |
56 | 36 | } |
57 | 37 |
|
58 | 38 | return $this; |
59 | 39 | } |
60 | 40 |
|
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 | + |
69 | 93 | public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $observer) |
70 | 94 | { |
71 | 95 | $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); |
72 | 104 |
|
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); |
86 | 106 | } |
87 | 107 |
|
88 | 108 | return $this; |
89 | 109 | } |
90 | 110 |
|
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() |
95 | 125 | { |
96 | 126 | return Mage::helper('theextensionlab_statuscolors'); |
97 | 127 | } |
|
0 commit comments