Lanot_CleverWidget - Version 1.0.0.0

Version Notes

Added select box with customer groups to widget form. Prepared for MAgento Connect 2.0

Download this release

Release Info

Developer Lanot
Extension Lanot_CleverWidget
Version 1.0.0.0
Comparing to
See all releases


Version 1.0.0.0

app/code/community/Lanot/CleverWidget/Block/Adminhtml/Widget/Instance/Edit/Tab/Main.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ /**
22
+ * adminhtml widget instance main edit form
23
+ *
24
+ * @author Lanot
25
+ */
26
+ class Lanot_CleverWidget_Block_Adminhtml_Widget_Instance_Edit_Tab_Main
27
+ extends Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Tab_Main
28
+ {
29
+ /**
30
+ * @return Lanot_CleverWidget_Block_Adminhtml_Widget_Instance_Edit_Tab_Main
31
+ */
32
+ protected function _prepareForm()
33
+ {
34
+ parent::_prepareForm();
35
+
36
+ //----------------------- customization -----------------------
37
+ Mage::dispatchEvent('adminhtml_widget_instance_edit_prepare_form', array('tab_object' => $this));
38
+ //-----------------------/customization -----------------------
39
+
40
+ return $this;
41
+ }
42
+ }
app/code/community/Lanot/CleverWidget/Helper/Data.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ /**
22
+ * clever widget helper
23
+ *
24
+ * @author Lanot
25
+ */
26
+
27
+ class Lanot_CleverWidget_Helper_Data extends Mage_Core_Helper_Abstract
28
+ {
29
+ const XML_NODE_PATH_ENABLED = 'lanot_cleverwidget/view/enabled';
30
+
31
+ /**
32
+ * @return bool
33
+ */
34
+ public function isEnabled()
35
+ {
36
+ return (bool) Mage::getStoreConfig(self::XML_NODE_PATH_ENABLED);
37
+ }
38
+ }
app/code/community/Lanot/CleverWidget/Model/Mysql4/Layout.php ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ /**
22
+ * Resource model for laout
23
+ *
24
+ * @author Lanot
25
+ */
26
+ class Lanot_CleverWidget_Model_Mysql4_Layout
27
+ extends Mage_Core_Model_Mysql4_Layout
28
+ {
29
+ /**
30
+ * Retrieve layout updates by handle
31
+ *
32
+ * @param string $handle
33
+ * @param array $params
34
+ * @return string
35
+ */
36
+ public function fetchUpdatesByHandle($handle, $params = array())
37
+ {
38
+ $bind = array(
39
+ 'store_id' => Mage::app()->getStore()->getId(),
40
+ 'area' => Mage::getSingleton('core/design_package')->getArea(),
41
+ 'package' => Mage::getSingleton('core/design_package')->getPackageName(),
42
+ 'theme' => Mage::getSingleton('core/design_package')->getTheme('layout')
43
+ );
44
+
45
+ foreach ($params as $key => $value) {
46
+ if (isset($bind[$key])) {
47
+ $bind[$key] = $value;
48
+ }
49
+ }
50
+ $bind['layout_update_handle'] = $handle;
51
+ $result = '';
52
+
53
+ $readAdapter = $this->_getReadAdapter();
54
+ if ($readAdapter) {
55
+ $select = $readAdapter->select()
56
+ ->from(array('layout_update' => $this->getMainTable()), array('xml'))
57
+ ->join(array('link'=>$this->getTable('core/layout_link')),
58
+ 'link.layout_update_id=layout_update.layout_update_id',
59
+ '')
60
+ ->where('link.store_id IN (0, :store_id)')
61
+ ->where('link.area = :area')
62
+ ->where('link.package = :package')
63
+ ->where('link.theme = :theme')
64
+ ->where('layout_update.handle = :layout_update_handle')
65
+ ->order('layout_update.sort_order ' . Varien_Db_Select::SQL_ASC);
66
+
67
+ //----------------------- customization -----------------------
68
+ Mage::dispatchEvent('layout_update_fetch_handle_prepare_select', array('select' => $select, 'object' => $this));
69
+ //-----------------------/customization -----------------------
70
+
71
+ $result = join('', $readAdapter->fetchCol($select, $bind));
72
+ }
73
+ return $result;
74
+ }
75
+ }
app/code/community/Lanot/CleverWidget/Model/Mysql4/Widget/Instance.php ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ /**
22
+ * Resource model
23
+ *
24
+ * @author Lanot
25
+ */
26
+ class Lanot_CleverWidget_Model_Mysql4_Widget_Instance
27
+ extends Mage_Widget_Model_Mysql4_Widget_Instance
28
+ {
29
+ /**
30
+ * @return string
31
+ */
32
+ protected function _getLinkTableWidgetInstance()
33
+ {
34
+ return $this->getTable('lanot_cleverwidget/widget_customer_group');
35
+ }
36
+
37
+ /**
38
+ * @return string
39
+ */
40
+ protected function _getLinkTableLayoutUpdate()
41
+ {
42
+ return $this->getTable('lanot_cleverwidget/layout_update_customer_group');
43
+ }
44
+
45
+ /**
46
+ * @param Lanot_CleverWidget_Model_Widget_Instance $object
47
+ * @return Lanot_CleverWidget_Model_Mysql4_Widget_Instance
48
+ */
49
+ public function assignCustomerGroupsToWidgetInstance($object)
50
+ {
51
+ $newCustomerGroupIds = $object->getData('customer_group_ids');
52
+ if (null === $newCustomerGroupIds || !is_array($newCustomerGroupIds)) {
53
+ return $this;
54
+ }
55
+
56
+ //#1. Widget
57
+ $oldCustomerGroupIds = $this->getAssignedCustomerGroupsToWidgetInstance($object);
58
+ $insertCustomerGroups = array_diff($newCustomerGroupIds, $oldCustomerGroupIds);
59
+ $deleteCustomerGroups = array_diff($oldCustomerGroupIds, $newCustomerGroupIds);
60
+
61
+ //#2. Layout Updates
62
+ $layoutUpdateIds = $this->getAssignedLayoutUpdatesToWidgetInstance($object);
63
+ //$oldLayoutUpdateIds = $object->getData('layout_update_ids');
64
+
65
+ //#3. Insert new customer groups for widget
66
+ if (!empty($insertCustomerGroups)) {
67
+ $this->_insertCustomerGroupsToWidgetInstance($insertCustomerGroups, $object);
68
+ }
69
+
70
+ //#4. Delete useless customer groups for widget
71
+ if (!empty($deleteCustomerGroups)) {
72
+ $this->_deleteCustomerGroupsToWidgetInstance($deleteCustomerGroups, $object);
73
+ }
74
+
75
+ //#5. Insert new customer groups for layout updates
76
+ if (!empty($layoutUpdateIds)) {
77
+ $this->_deleteCustomerGroupsToLayoutUpdate($layoutUpdateIds);
78
+ $this->_insertCustomerGroupsToLayoutUpdate($newCustomerGroupIds, $layoutUpdateIds);
79
+ }
80
+
81
+ return $this;
82
+ }
83
+
84
+ /**
85
+ * @param Lanot_CleverWidget_Model_Widget_Instance $object
86
+ * @return array
87
+ */
88
+ public function getAssignedLayoutUpdatesToWidgetInstance($object)
89
+ {
90
+ if (!$object->getId()) {
91
+ return array();
92
+ }
93
+
94
+ $pageTable = $this->getTable('widget/widget_instance_page');
95
+ $pageLayoutTable = $this->getTable('widget/widget_instance_page_layout');
96
+
97
+ $select = $this->_getReadAdapter()->select()
98
+ ->from(array('p' => $pageTable), array())
99
+ ->joinInner(array('pl' => $pageLayoutTable), 'pl.page_id = p.page_id', array('layout_update_id'))
100
+ ->where('p.instance_id = ?', (int)$object->getId());
101
+
102
+ return $this->_getReadAdapter()->fetchCol($select);
103
+ }
104
+
105
+ /**
106
+ * @param Mage_Widget_Model_Widget_Instance $object
107
+ * @return array
108
+ */
109
+ public function getAssignedCustomerGroupsToWidgetInstance(Mage_Widget_Model_Widget_Instance $object)
110
+ {
111
+ $select = $this->_getWriteAdapter()
112
+ ->select()
113
+ ->from($this->_getLinkTableWidgetInstance(), array('customer_group_id'))
114
+ ->where('instance_id=?', $object->getId());
115
+
116
+ return $this->_getWriteAdapter()->fetchCol($select);
117
+ }
118
+
119
+ /**
120
+ * @param array $customerGroupIds
121
+ * @param Mage_Widget_Model_Widget_Instance $object
122
+ * @return Lanot_CleverWidget_Model_Mysql4_Widget_Instance
123
+ */
124
+ protected function _insertCustomerGroupsToWidgetInstance(array $customerGroupIds, Mage_Widget_Model_Widget_Instance $object)
125
+ {
126
+ $data = array();
127
+ foreach($customerGroupIds as $customerGroupId) {
128
+ $data[] = array('customer_group_id' => $customerGroupId, 'instance_id' => $object->getId());
129
+ }
130
+ $this->_getWriteAdapter()->insertArray($this->_getLinkTableWidgetInstance(), array('customer_group_id', 'instance_id'), $data);
131
+ return $this;
132
+ }
133
+
134
+ /**
135
+ * @param array $customerGroupIds
136
+ * @param Mage_Widget_Model_Widget_Instance $object
137
+ * @return Lanot_CleverWidget_Model_Mysql4_Widget_Instance
138
+ */
139
+ protected function _deleteCustomerGroupsToWidgetInstance(array $customerGroupIds, Mage_Widget_Model_Widget_Instance $object)
140
+ {
141
+ $where = $this->_getWriteAdapter()->quoteInto('instance_id = ?', $object->getId());
142
+ $where.= $this->_getWriteAdapter()->quoteInto(' AND customer_group_id IN (?)', $customerGroupIds);
143
+ $this->_getWriteAdapter()->delete($this->_getLinkTableWidgetInstance(), $where);
144
+ return $this;
145
+ }
146
+
147
+ /**
148
+ * @param array $customerGroupIds
149
+ * @param array $layoutUpdateIds
150
+ * @return Lanot_CleverWidget_Model_Mysql4_Widget_Instance
151
+ */
152
+ protected function _insertCustomerGroupsToLayoutUpdate(array $customerGroupIds, array $layoutUpdateIds)
153
+ {
154
+ $data = array();
155
+ foreach($layoutUpdateIds as $layoutUpdateId) {
156
+ foreach($customerGroupIds as $customerGroupId) {
157
+ $data[] = array('customer_group_id' => $customerGroupId, 'layout_update_id' => $layoutUpdateId);
158
+ }
159
+ }
160
+ $this->_getWriteAdapter()->insertArray($this->_getLinkTableLayoutUpdate(), array('customer_group_id', 'layout_update_id'), $data);
161
+ return $this;
162
+ }
163
+
164
+ /**
165
+ * @param array $layoutUpdateIds
166
+ * @return Lanot_CleverWidget_Model_Mysql4_Widget_Instance
167
+ */
168
+ protected function _deleteCustomerGroupsToLayoutUpdate(array $layoutUpdateIds)
169
+ {
170
+ if (!empty($layoutUpdateIds)) {
171
+ $where = $this->_getWriteAdapter()->quoteInto('layout_update_id IN(?)', $layoutUpdateIds);
172
+ $this->_getWriteAdapter()->delete($this->_getLinkTableLayoutUpdate(), $where);
173
+ }
174
+ return $this;
175
+ }
176
+ }
app/code/community/Lanot/CleverWidget/Model/Observer.php ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ /**
22
+ * clever widget observer
23
+ *
24
+ * @author Lanot
25
+ */
26
+ class Lanot_CleverWidget_Model_Observer
27
+ {
28
+ /**
29
+ * @param Varien_Object $observer
30
+ * @return Lanot_CleverWidget_Model_Observer
31
+ */
32
+ public function widgetInstanceSaveBefore($observer)
33
+ {
34
+ if ($this->_getHelper()->isEnabled() && $observer->getEvent()->getWidgetInstance()) {
35
+ //is not actual - because magento deletes every time all core layout updates assignet to widget
36
+ $layoutUpdateIds = $this->_getResourceModel()->getAssignedLayoutUpdatesToWidgetInstance($observer->getEvent()->getWidgetInstance());
37
+ $observer->getEvent()->getWidgetInstance()->setData('layout_update_ids', $layoutUpdateIds);
38
+ //set customer groups for object
39
+ $observer->getEvent()->getWidgetInstance()->setData('customer_group_ids', $this->_getRequest()->getPost('customer_group_ids', array()));
40
+ }
41
+ return $this;
42
+ }
43
+
44
+ /**
45
+ * @param Varien_Object $observer
46
+ * @return Lanot_CleverWidget_Model_Observer
47
+ */
48
+ public function widgetInstanceSaveAfter($observer)
49
+ {
50
+ if ($this->_getHelper()->isEnabled() && $observer->getEvent()->getWidgetInstance()) {
51
+ $this->_getResourceModel()->assignCustomerGroupsToWidgetInstance($observer->getEvent()->getWidgetInstance());
52
+ }
53
+ return $this;
54
+ }
55
+
56
+ /**
57
+ * @param Varien_Object $observer
58
+ * @return Lanot_CleverWidget_Model_Observer
59
+ */
60
+ public function widgetInstanceLoadAfter($observer)
61
+ {
62
+ if ($this->_getHelper()->isEnabled() && $observer->getEvent()->getWidgetInstance()) {
63
+ $ids = $this->_getResourceModel()->getAssignedCustomerGroupsToWidgetInstance($observer->getEvent()->getWidgetInstance());
64
+ $observer->getEvent()->getWidgetInstance()->setData('customer_group_ids', $ids);
65
+ }
66
+ return $this;
67
+ }
68
+
69
+ /**
70
+ * @param Varien_Object $observer
71
+ * @return Lanot_CleverWidget_Model_Observer
72
+ */
73
+ public function widgetInstancePrepareForm($observer)
74
+ {
75
+ if ($this->_getHelper()->isEnabled() && $observer->getEvent()->getTabObject()) {
76
+ /** @var $fieldset Varien_Data_Form_Element_Fieldset */
77
+ $fieldset = $observer->getEvent()->getTabObject()->getForm()->getElement('base_fieldset');
78
+
79
+ $fieldset->addField('customer_group_ids', 'multiselect', array(
80
+ 'name' => 'customer_group_ids[]',
81
+ 'label' => $this->_getHelper()->__('Assign to Customer Groups'),
82
+ 'title' => $this->_getHelper()->__('Assign to Customer Groups'),
83
+ 'values' => $this->_getCustomerGroupSourceModel()->toOptionArray(),
84
+ 'required' => true,
85
+ ), 'title');
86
+ }
87
+ return $this;
88
+ }
89
+
90
+ /**
91
+ * @param Varien_Object $observer
92
+ * @return Lanot_CleverWidget_Model_Observer
93
+ */
94
+ public function prepareLayoutUpdateSelect($observer)
95
+ {
96
+ if ($this->_getHelper()->isEnabled() && $observer->getEvent()->getSelect() && $observer->getEvent()->getObject()) {
97
+ /** @var $select Varien_Db_Select */
98
+ $select = $observer->getEvent()->getSelect();
99
+
100
+ /** @var $object Lanot_CleverWidget_Model_Resource_Layout */
101
+ $object = $observer->getEvent()->getObject();
102
+
103
+ //join customer group table
104
+ $gId = 0;
105
+ if ($this->_getSession()->isLoggedIn()) {
106
+ $gId = (int) $this->_getSession()->getCustomerGroupId();
107
+ }
108
+
109
+ $cond = 'update_cg.layout_update_id=layout_update.layout_update_id AND update_cg.customer_group_id=' . $gId;
110
+ $select->join(array('update_cg'=> $object->getTable('lanot_cleverwidget/layout_update_customer_group')), $cond, array());
111
+ }
112
+ return $this;
113
+ }
114
+
115
+ /**
116
+ * Retrieve request object
117
+ *
118
+ * @return Mage_Core_Controller_Request_Http
119
+ */
120
+ protected function _getRequest()
121
+ {
122
+ return Mage::app()->getRequest();
123
+ }
124
+
125
+ /**
126
+ * @return Lanot_CleverWidget_Helper_Data
127
+ */
128
+ protected function _getHelper()
129
+ {
130
+ return Mage::helper('lanot_cleverwidget');
131
+ }
132
+
133
+ /**
134
+ * @return Lanot_CleverWidget_Model_Mysql4_Widget_Instance
135
+ */
136
+ protected function _getResourceModel()
137
+ {
138
+ return Mage::getSingleton('lanot_cleverwidget/mysql4_widget_instance');
139
+ }
140
+
141
+ /**
142
+ * @return Mage_Adminhtml_Model_System_Config_Source_Customer_Group
143
+ */
144
+ protected function _getCustomerGroupSourceModel()
145
+ {
146
+ return Mage::getSingleton('lanot_cleverwidget/source_customer_group');
147
+ }
148
+
149
+ /**
150
+ * @return Mage_Customer_Model_Session
151
+ */
152
+ protected function _getSession()
153
+ {
154
+ return Mage::getSingleton('customer/session');
155
+ }
156
+ }
app/code/community/Lanot/CleverWidget/Model/Source/Customer/Group.php ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ class Lanot_CleverWidget_Model_Source_Customer_Group
22
+ {
23
+ protected $_options;
24
+
25
+ public function toOptionArray()
26
+ {
27
+ if (!$this->_options) {
28
+ $this->_options = Mage::getResourceModel('customer/group_collection')->toOptionArray();
29
+ }
30
+ return $this->_options;
31
+ }
32
+ }
app/code/community/Lanot/CleverWidget/Model/Widget/Instance.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ /**
22
+ * magento core model rewrite
23
+ *
24
+ * @author Lanot
25
+ */
26
+ class Lanot_CleverWidget_Model_Widget_Instance extends Mage_Widget_Model_Widget_Instance
27
+ {
28
+ protected $_eventPrefix = 'widget_instance';
29
+ protected $_eventObject = 'widget_instance';
30
+ }
app/code/community/Lanot/CleverWidget/etc/adminhtml.xml ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * Magento admin config
5
+ *
6
+ * @author Lanot
7
+ */
8
+ -->
9
+ <config>
10
+
11
+
12
+ <acl>
13
+ <resources>
14
+ <admin>
15
+ <children>
16
+
17
+ <system>
18
+ <children>
19
+ <config>
20
+ <children>
21
+ <lanot_cleverwidget translate="title" module="lanot_cleverwidget">
22
+ <title>Lanot Clever Widget Configuration</title>
23
+ </lanot_cleverwidget>
24
+ </children>
25
+ </config>
26
+ </children>
27
+ </system>
28
+
29
+ </children>
30
+ </admin>
31
+ </resources>
32
+ </acl>
33
+
34
+ </config>
app/code/community/Lanot/CleverWidget/etc/config.xml ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+
4
+ <modules>
5
+ <Lanot_CleverWidget>
6
+ <version>1.0.0.0</version>
7
+ </Lanot_CleverWidget>
8
+ </modules>
9
+
10
+ <global>
11
+ <helpers>
12
+ <lanot_cleverwidget>
13
+ <class>Lanot_CleverWidget_Helper</class>
14
+ </lanot_cleverwidget>
15
+ </helpers>
16
+
17
+ <blocks>
18
+ <lanot_cleverwidget>
19
+ <class>Lanot_CleverWidget_Block</class>
20
+ </lanot_cleverwidget>
21
+ </blocks>
22
+
23
+ <models>
24
+ <lanot_cleverwidget>
25
+ <class>Lanot_CleverWidget_Model</class>
26
+ <resourceModel>lanot_cleverwidget_resource</resourceModel>
27
+ </lanot_cleverwidget>
28
+
29
+ <lanot_cleverwidget_resource>
30
+ <class>Lanot_CleverWidget_Model_Mysql4</class>
31
+ <entities>
32
+ <layout_update_customer_group><table>lanot_clever_widget_layout_update_cg</table></layout_update_customer_group>
33
+ <widget_customer_group><table>lanot_clever_widget_widget_instance_cg</table></widget_customer_group>
34
+ </entities>
35
+ </lanot_cleverwidget_resource>
36
+
37
+ <widget>
38
+ <rewrite>
39
+ <widget_instance>Lanot_CleverWidget_Model_Widget_Instance</widget_instance>
40
+ </rewrite>
41
+ </widget>
42
+
43
+ <core_resource>
44
+ <rewrite>
45
+ <layout>Lanot_CleverWidget_Model_Mysql4_Layout</layout>
46
+ </rewrite>
47
+ </core_resource>
48
+ <core_mysql4>
49
+ <rewrite>
50
+ <layout>Lanot_CleverWidget_Model_Mysql4_Layout</layout>
51
+ </rewrite>
52
+ </core_mysql4>
53
+ </models>
54
+
55
+ <!-- MySQL INSTALL SCRIPTS -->
56
+ <resources>
57
+ <lanot_cleverwidget_setup>
58
+ <setup>
59
+ <module>Lanot_CleverWidget</module>
60
+ </setup>
61
+ </lanot_cleverwidget_setup>
62
+ </resources>
63
+ <!-- /MySQL INSTALL SCRIPTS -->
64
+ </global>
65
+
66
+ <adminhtml>
67
+ <!-- OBSERVER EVENTS -->
68
+ <events>
69
+ <!-- ADMIN HTML WIDGET EVENTS -->
70
+ <widget_instance_save_before>
71
+ <observers>
72
+ <lanot_cleverwidget>
73
+ <type>singleton</type>
74
+ <class>lanot_cleverwidget/observer</class>
75
+ <method>widgetInstanceSaveBefore</method>
76
+ </lanot_cleverwidget>
77
+ </observers>
78
+ </widget_instance_save_before>
79
+ <widget_instance_save_after>
80
+ <observers>
81
+ <lanot_cleverwidget>
82
+ <type>singleton</type>
83
+ <class>lanot_cleverwidget/observer</class>
84
+ <method>widgetInstanceSaveAfter</method>
85
+ </lanot_cleverwidget>
86
+ </observers>
87
+ </widget_instance_save_after>
88
+ <widget_instance_load_after>
89
+ <observers>
90
+ <lanot_cleverwidget>
91
+ <type>singleton</type>
92
+ <class>lanot_cleverwidget/observer</class>
93
+ <method>widgetInstanceLoadAfter</method>
94
+ </lanot_cleverwidget>
95
+ </observers>
96
+ </widget_instance_load_after>
97
+ <adminhtml_widget_instance_edit_prepare_form>
98
+ <observers>
99
+ <lanot_cleverwidget>
100
+ <type>singleton</type>
101
+ <class>lanot_cleverwidget/observer</class>
102
+ <method>widgetInstancePrepareForm</method>
103
+ </lanot_cleverwidget>
104
+ </observers>
105
+ </adminhtml_widget_instance_edit_prepare_form>
106
+ <!-- /ADMIN HTML WIDGET EVENTS -->
107
+ </events>
108
+ <!--/OBSERVER EVENTS -->
109
+
110
+ <layout>
111
+ <updates>
112
+ <lanot_cleverwidget>
113
+ <file>lanot_cleverwidget.xml</file>
114
+ </lanot_cleverwidget>
115
+ </updates>
116
+ </layout>
117
+ </adminhtml>
118
+
119
+
120
+ <frontend>
121
+ <!-- OBSERVER EVENTS -->
122
+ <events>
123
+ <layout_update_fetch_handle_prepare_select>
124
+ <observers>
125
+ <lanot_cleverwidget>
126
+ <type>singleton</type>
127
+ <class>lanot_cleverwidget/observer</class>
128
+ <method>prepareLayoutUpdateSelect</method>
129
+ </lanot_cleverwidget>
130
+ </observers>
131
+ </layout_update_fetch_handle_prepare_select>
132
+ </events>
133
+ <!--/OBSERVER EVENTS -->
134
+ </frontend>
135
+
136
+ <default>
137
+ <lanot_cleverwidget>
138
+ <view>
139
+ <enabled>1</enabled>
140
+ </view>
141
+ </lanot_cleverwidget>
142
+ </default>
143
+ </config>
app/code/community/Lanot/CleverWidget/etc/system.xml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <tabs>
4
+ <lanot translate="label">
5
+ <label>Lanot Extensions</label>
6
+ <sort_order>200</sort_order>
7
+ </lanot>
8
+ </tabs>
9
+ <sections>
10
+ <lanot_cleverwidget>
11
+ <class>separator-top</class>
12
+ <label>Clever Widget</label>
13
+ <tab>lanot</tab>
14
+ <frontend_type>text</frontend_type>
15
+ <sort_order>900</sort_order>
16
+ <show_in_default>1</show_in_default>
17
+ <show_in_website>0</show_in_website>
18
+ <show_in_store>0</show_in_store>
19
+ <groups>
20
+ <view translate="label">
21
+ <label>Clever Widget Settings</label>
22
+ <frontend_type>text</frontend_type>
23
+ <sort_order>10</sort_order>
24
+ <show_in_default>1</show_in_default>
25
+ <show_in_website>0</show_in_website>
26
+ <show_in_store>0</show_in_store>
27
+ <fields>
28
+ <enabled translate="label">
29
+ <label>Enabled at Frontend</label>
30
+ <frontend_type>select</frontend_type>
31
+ <source_model>adminhtml/system_config_source_yesno</source_model>
32
+ <sort_order>10</sort_order>
33
+ <show_in_default>1</show_in_default>
34
+ <show_in_website>0</show_in_website>
35
+ <show_in_store>0</show_in_store>
36
+ </enabled>
37
+ </fields>
38
+ </view>
39
+ </groups>
40
+ </lanot_cleverwidget>
41
+ </sections>
42
+ </config>
app/code/community/Lanot/CleverWidget/sql/lanot_cleverwidget_setup/mysql4-install-1.0.0.0.php ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Lanot
16
+ * @package Lanot_CleverWidget
17
+ * @copyright Copyright (c) 2012 Lanot
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ $installer = $this;
22
+
23
+ //tables definition
24
+ $tableUpdateLayoutCW = $this->getTable('lanot_cleverwidget/layout_update_customer_group');
25
+ $tableWidgetCW = $this->getTable('lanot_cleverwidget/widget_customer_group');
26
+
27
+ $customerGroupTable = $this->getTable('customer/customer_group');
28
+ $layoutTable = $this->getTable('core/layout_update');
29
+ $widgetInstanceTable = $this->getTable('widget/widget_instance');
30
+
31
+ $installer->startSetup();
32
+
33
+ //create table for widget_customer_group
34
+ $this->run("
35
+ DROP TABLE IF EXISTS `{$tableWidgetCW}`;
36
+ CREATE TABLE `{$tableWidgetCW}` (
37
+ `instance_id` int(10) unsigned NOT NULL COMMENT 'Widget Instance ID',
38
+ `customer_group_id` smallint NOT NULL COMMENT 'Customer Group ID',
39
+ PRIMARY KEY (`instance_id`, `customer_group_id`),
40
+ CONSTRAINT `FK_CW_WIDGET_INSTANCE_ID_WIDGET_INSTANCE_INSTANCE_ID` FOREIGN KEY (`instance_id`) REFERENCES `{$widgetInstanceTable}` (`instance_id`) ON DELETE CASCADE ON UPDATE CASCADE,
41
+ CONSTRAINT `FK_CW_WIDGET_CUSTOMER_GROUP_ID_CUSTOMER_GROUP_CUSTOMER_GROUP_ID` FOREIGN KEY (`customer_group_id`) REFERENCES `{$customerGroupTable}` (`customer_group_id`) ON DELETE CASCADE ON UPDATE CASCADE
42
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Widget Instance To Customer Group';
43
+ ");
44
+
45
+ //create table for layout_update_customer_group
46
+ $this->run("
47
+ DROP TABLE IF EXISTS `{$tableUpdateLayoutCW}`;
48
+ CREATE TABLE `{$tableUpdateLayoutCW}` (
49
+ `layout_update_id` int(10) unsigned NOT NULL COMMENT 'Core Layout Update ID',
50
+ `customer_group_id` smallint NOT NULL COMMENT 'Customer Group ID',
51
+ PRIMARY KEY (`layout_update_id`, `customer_group_id`),
52
+ CONSTRAINT `FK_CW_LAYOUT_LAYOUT_ID_CORE_LAYOUT_LAYOUT_ID` FOREIGN KEY (`layout_update_id`) REFERENCES `{$layoutTable}` (`layout_update_id`) ON DELETE CASCADE ON UPDATE CASCADE,
53
+ CONSTRAINT `FK_CW_LAYOUT_CUSTOMER_GROUP_ID_CUSTOMER_GROUP_CUSTOMER_GROUP_ID` FOREIGN KEY (`customer_group_id`) REFERENCES `{$customerGroupTable}` (`customer_group_id`) ON DELETE CASCADE ON UPDATE CASCADE
54
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Core Layout Update To Customer Group';
55
+ ");
56
+
57
+ //populate table by data
58
+ //add all groups to existed widgets
59
+ $this->run("INSERT INTO `{$tableWidgetCW}`
60
+ SELECT wi.instance_id, cg.customer_group_id FROM `{$widgetInstanceTable}` as wi INNER JOIN `{$customerGroupTable}` as cg
61
+ ORDER BY wi.instance_id, cg.customer_group_id
62
+ ");
63
+
64
+ //add all groups to existed layout updates
65
+ $this->run("INSERT INTO `{$tableUpdateLayoutCW}`
66
+ SELECT lu.layout_update_id, cg.customer_group_id FROM `{$layoutTable}` as lu INNER JOIN `{$customerGroupTable}` as cg
67
+ ORDER BY lu.layout_update_id, cg.customer_group_id
68
+ ");
69
+
70
+ $this->endSetup();
app/design/adminhtml/default/default/layout/lanot_cleverwidget.xml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * Magento backend layout
5
+ *
6
+ * @author Lanot
7
+ */
8
+ -->
9
+
10
+ <layout>
11
+
12
+ <!-- ADMIN WIDGET -->
13
+ <adminhtml_widget_instance_edit>
14
+ <reference name="widget_instance_edit_tabs">
15
+ <!-- remove old tab -->
16
+ <action method="removeTab"><name>main_section</name></action>
17
+ <!-- /remove old tab -->
18
+
19
+ <!-- add new tab -->
20
+ <block type="lanot_cleverwidget/adminhtml_widget_instance_edit_tab_main" name="widget_instance_edit_tab_main" before="-"/>
21
+ <action method="addTabAfter"><name>main_section</name><block>widget_instance_edit_tab_main</block><after>settings_section</after></action>
22
+ <!-- /add new tab -->
23
+ </reference>
24
+ </adminhtml_widget_instance_edit>
25
+ <!-- /ADMIN WIDGET -->
26
+ </layout>
app/etc/modules/Lanot_CleverWidget.xml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Lanot_CleverWidget>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends>
8
+ <Mage_Core />
9
+ <Mage_Widget />
10
+ </depends>
11
+ </Lanot_CleverWidget>
12
+ </modules>
13
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Lanot_CleverWidget</name>
4
+ <version>1.0.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>OSL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Extension Adds Ability Administrator choose target customer group for showing widget to the users assigned to the group.</summary>
10
+ <description>Extension Adds Ability Administrator choose target customer group for showing widget to the users assigned to the group.</description>
11
+ <notes>Added select box with customer groups to widget form. Prepared for MAgento Connect 2.0</notes>
12
+ <authors><author><name>Lanot</name><user>Lanot</user><email>lanot.biz@gmail.com</email></author></authors>
13
+ <date>2013-01-10</date>
14
+ <time>07:11:26</time>
15
+ <contents><target name="mageetc"><dir name="modules"><file name="Lanot_CleverWidget.xml" hash="10fa9adfeae45f380dee2b70ee3f0646"/></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><file name="lanot_cleverwidget.xml" hash="c54d2484c9fd85ef874e667a0a446437"/></dir></dir></dir></dir></target><target name="magecommunity"><dir name="Lanot"><dir name="CleverWidget"><dir name="Block"><dir name="Adminhtml"><dir name="Widget"><dir name="Instance"><dir name="Edit"><dir name="Tab"><file name="Main.php" hash="03988afc16904c987a554564467376d7"/></dir></dir></dir></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="fe4c47eb4d44de0ebfecd8a4596d7d8d"/></dir><dir name="Model"><dir name="Mysql4"><file name="Layout.php" hash="f16354a97d84edbac2f858b2a240cc5e"/><dir name="Widget"><file name="Instance.php" hash="ce4e22a534356a8707b02508a32a86ae"/></dir></dir><file name="Observer.php" hash="304ddc30b3c445bb73898bf65bd251f3"/><dir name="Source"><dir name="Customer"><file name="Group.php" hash="d1cd9f7bbe2ba0bc568a5957a402f0d9"/></dir></dir><dir name="Widget"><file name="Instance.php" hash="24ff244dbf96300a80943abaf3efbc03"/></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="c3b2a29a2c98f238031c2a7f52a5f27e"/><file name="config.xml" hash="d3e86fff4584d7b71a4bdcd653bab1bf"/><file name="system.xml" hash="6cf8913573c37ab0a247932515daed8d"/></dir><dir name="sql"><dir name="lanot_cleverwidget_setup"><file name="mysql4-install-1.0.0.0.php" hash="8b3f3230a45d9169914edb4bed746a55"/></dir></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>