~openerp-community/openobjectworks/magentoconnector

« back to all changes in this revision

Viewing changes to OpenERPSync/Model/Store/Api.php

  • Committer: Sharoon Thomas
  • Date: 2009-06-02 16:30:49 UTC
  • Revision ID: sharoonthomas@teagarden.in-20090602163049-zsbek6lukohwbmgq
Added stores, websites and groups enumeration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * @author 
 
5
 * @copyright 2008
 
6
 */
 
7
 
 
8
class Smile_OpenERPSync_Model_Store_Api extends Mage_Catalog_Model_Api_Resource
 
9
{
 
10
        /*
 
11
        <param><value><string>92mliq7v01g1k9ofv14v8d0gd2</string></value></param>
 
12
        <param><value><string>catalog_store.websites</string></value></param>
 
13
        optional: <param><value><array><data><value><i4>0</i4></value></data></array></value></param>
 
14
        */
 
15
        public function websites($websiteId = null)
 
16
        {
 
17
                $websites = array();
 
18
                
 
19
                if(is_integer($websiteId))
 
20
                {
 
21
                        try{
 
22
                                return $this->_websiteToArray(Mage::app()->getWebsite($websiteId));
 
23
                        } catch (Mage_Core_Exception $e) {
 
24
                $this->_fault('website_not_exists');
 
25
            }
 
26
                } else {
 
27
                        foreach(Mage::app()->getWebsites(true) as $websiteId => $website)
 
28
                        {
 
29
                                $websites[] = $this->_websiteToArray($website);
 
30
                        }
 
31
                        
 
32
                        return $websites;
 
33
                }
 
34
        }
 
35
        
 
36
        protected function _websiteToArray($website)
 
37
        {
 
38
                return array(   'website_id'            => $website->getWebsiteId(),
 
39
                                                'code'                          => $website->getCode(),         
 
40
                                                'name'                          => $website->getName(),         
 
41
                                                'sort_order'            => $website->getSortOrder(),
 
42
                                                'default_group_id'      => $website->getDefaultGroupId(),
 
43
                                                'is_default'            => $website->getIsDefault(),
 
44
                                                'group_ids'                     => $website->getGroupIds()
 
45
                                                );
 
46
        }
 
47
        
 
48
        /*
 
49
        <param><value><string>92mliq7v01g1k9ofv14v8d0gd2</string></value></param>
 
50
        <param><value><string>catalog_store.stores</string></value></param>
 
51
        */
 
52
        public function stores($storeIds = null)
 
53
        {
 
54
                $stores = array();
 
55
                
 
56
                if(is_array($storeIds))
 
57
                {
 
58
                        foreach($storeIds as $storeId)
 
59
                        {
 
60
                                try{
 
61
                                        $stores[] = $this->_storeToArray(Mage::app()->getStore($storeId));
 
62
                                } catch (Mage_Core_Exception $e) {
 
63
                        $this->_fault('store_not_exists');
 
64
                    }
 
65
                        }
 
66
                        
 
67
                        return $stores;
 
68
                } 
 
69
                elseif(is_numeric($storeIds))
 
70
                {
 
71
                        try{
 
72
                                return $this->_storeToArray(Mage::app()->getStore((int)$storeIds));
 
73
                        } catch (Mage_Core_Exception $e) {
 
74
                $this->_fault('store_not_exists');
 
75
            }
 
76
                } else {
 
77
                        foreach(Mage::app()->getStores(true) as $storeId => $store)
 
78
                        {
 
79
                                $stores[] = $this->_storeToArray($store);
 
80
                        }
 
81
                        
 
82
                        return $stores; 
 
83
                }
 
84
        }
 
85
        
 
86
        protected function _storeToArray($store)
 
87
        {
 
88
                return array(   'store_id'                                      => $store->getStoreId(),
 
89
                                                'code'                                          => $store->getCode(),
 
90
                                                'website_id'                            => $store->getWebsiteId(),
 
91
                                                'group_id'                                      => $store->getGroupId(),
 
92
                                                'name'                                          => $store->getName(),
 
93
                                                'sort_order'                            => $store->getSortOrder(),
 
94
                                                'is_active'                                     => $store->getIsActive(),
 
95
                                                'is_admin'                                      => $store->isAdmin(),
 
96
                                                'is_can_delete'                         => $store->isCanDelete(),
 
97
                                                'url'                                           => $store->getUrl(),
 
98
                                                'is_currently_secure'           => $store->isCurrentlySecure(),
 
99
                                                'current_currency_code'         => $store->getCurrentCurrencyCode(),
 
100
                                                'root_category_id'                      => $store->getRootCategoryId()
 
101
                                                );      
 
102
        }
 
103
        
 
104
        public function groups($groupIds)
 
105
        {
 
106
                if(is_array($groupIds))
 
107
                {
 
108
                        $groups = array();
 
109
                        foreach($groupIds as $groupId)
 
110
                        {
 
111
                                $groups[] = $this->_groupToArray($groupId);
 
112
                        }
 
113
                        return $groups;
 
114
                } 
 
115
                elseif(is_integer($groupIds))
 
116
                {
 
117
                        return $this->_groupToArray($groupIds); 
 
118
                } else {
 
119
                        $this->_fault('group_not_exists');      
 
120
                }
 
121
        }
 
122
        
 
123
        protected function _groupToArray($groupId)
 
124
        {
 
125
                try
 
126
                {
 
127
                        $group = Mage::app()->getGroup($groupId);
 
128
                
 
129
                        return array(   'group_id'                      => $group->getGroupId(),
 
130
                                                        'website_id'            => $group->getWebsiteId(),
 
131
                                                        'name'                          => $group->getName(),
 
132
                                                        'root_category_id'      => $group->getRootCategoryId(),
 
133
                                                        'default_store_id'      => $group->getDefaultStoreId(),
 
134
                                                        'store_ids'                     => $group->getStoreIds(),
 
135
                                                        'is_can_delete'         => $group->isCanDelete()
 
136
                                                );
 
137
                } 
 
138
                catch(Mage_Core_Exception $e) 
 
139
                {
 
140
                        $this->_fault('group_not_exists');
 
141
                }
 
142
        }
 
143
        
 
144
        public function tree()
 
145
        {
 
146
                $tree = array();
 
147
                
 
148
                $websites = $this->websites();
 
149
                
 
150
                foreach($websites as $website)
 
151
                {
 
152
                        $groups = $this->groups($website['group_ids']); 
 
153
                        $tree[$website['code']] = $website;
 
154
                        foreach($groups as $group)
 
155
                        {
 
156
                                $stores = $this->stores($group["store_ids"]);
 
157
                                
 
158
                                $tree[$website['code']]['groups']['group_'.$group['group_id']] = $group;
 
159
                                
 
160
                                foreach($stores as $store)
 
161
                                {
 
162
                                        $tree[$website['code']]['groups']['group_'.$group['group_id']]['stores'][$store['code']] = $store;
 
163
                                }
 
164
                        }
 
165
                }
 
166
 
 
167
                return $tree;
 
168
        }
 
169
        
 
170
}
 
171
 
 
172
?>