/
Default.php
211 lines (171 loc) · 5.28 KB
/
Default.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* @copyright Copyright (c) Metaways Infosystems GmbH, 2011
* @license LGPLv3, http://www.arcavias.com/en/license
* @package Controller
* @subpackage ExtJS
*/
/**
* ExtJS product controller for admin interfaces.
*
* @package Controller
* @subpackage ExtJS
*/
class Controller_ExtJS_Product_Default
extends Controller_ExtJS_Abstract
implements Controller_ExtJS_Common_Interface
{
private $_manager = null;
private $_context = null;
/**
* Initializes the product controller.
*
* @param MShop_Context_Item_Interface $context MShop context object
*/
public function __construct( MShop_Context_Item_Interface $context )
{
parent::__construct( $context, 'Product' );
$this->_manager = MShop_Product_Manager_Factory::createManager( $context );
$this->_context = $context;
}
/**
* Executes tasks after processing the items.
*
* @param stdClass $params Associative list of parameters
* @return array Associative list with success value
*/
public function finish( stdClass $params )
{
$this->_checkParams( $params, array( 'site', 'items' ) );
$this->_setLocale( $params->site );
$catalogManager = MShop_Catalog_Manager_Factory::createManager( $this->_context );
$indexManager = $catalogManager->getSubManager( 'index' );
$search = $this->_manager->createSearch();
$search->setConditions( $search->compare( '==', 'product.id', $params->items ) );
$search->setSlice( 0, count( $params->items ) );
foreach( $this->_manager->searchItems( $search ) as $item ) {
$indexManager->saveItem( $item );
}
$this->_clearCache( (array) $params->items );
return array(
'success' => true,
);
}
/**
* Creates a new product item or updates an existing one or a list thereof.
*
* @param stdClass $params Associative array containing the product properties
* @return array Associative list with nodes and success value
*/
public function saveItems( stdClass $params )
{
$this->_checkParams( $params, array( 'site', 'items' ) );
$this->_setLocale( $params->site );
$ids = array();
$items = ( !is_array( $params->items ) ? array( $params->items ) : $params->items );
foreach( $items as $entry )
{
$item = $this->_createItem( (array) $entry );
$this->_manager->saveItem( $item );
$ids[] = $item->getId();
}
$this->_clearCache( $ids );
$search = $this->_manager->createSearch();
$search->setConditions( $search->compare( '==', 'product.id', $ids ) );
$search->setSlice( 0, count( $ids ) );
$items = $this->_toArray( $this->_manager->searchItems( $search ) );
return array(
'items' => ( !is_array( $params->items ) ? reset( $items ) : $items ),
'success' => true,
);
}
/**
* Deletes an item or a list of items.
*
* @param stdClass $params Associative list of parameters
* @return array Associative list with success value
*/
public function deleteItems( stdClass $params )
{
$this->_checkParams( $params, array( 'site', 'items' ) );
$this->_setLocale( $params->site );
$ids = (array) $params->items;
$context = $this->_getContext();
$manager = $this->_getManager();
$manager->deleteItems( $ids );
foreach( array( 'catalog', 'product' ) as $domain )
{
$manager = MShop_Factory::createManager( $context, $domain . '/list' );
$search = $manager->createSearch();
$expr = array(
$search->compare( '==', $domain.'.list.refid', $ids ),
$search->compare( '==', $domain.'.list.domain', 'product' )
);
$search->setConditions( $search->combine( '&&', $expr ) );
$search->setSortations( array( $search->sort( '+', $domain.'.list.id' ) ) );
$start = 0;
do
{
$result = $manager->searchItems( $search );
$manager->deleteItems( array_keys( $result ) );
$count = count( $result );
$start += $count;
$search->setSlice( $start );
}
while( $count >= $search->getSliceSize() );
}
$this->_clearCache( $ids );
return array(
'items' => $params->items,
'success' => true,
);
}
/**
* Creates a new product item and sets the properties from the given array.
*
* @param array $entry Associative list of name and value properties using the "product" prefix
* @return MShop_Product_Item_Interface Product item
*/
protected function _createItem( array $entry )
{
$item = $this->_manager->createItem();
foreach( $entry as $name => $value )
{
switch( $name )
{
case 'product.id': $item->setId( $value ); break;
case 'product.code': $item->setCode( $value ); break;
case 'product.label': $item->setLabel( $value ); break;
case 'product.typeid': $item->setTypeId( $value ); break;
case 'product.status': $item->setStatus( $value ); break;
case 'product.suppliercode': $item->setSupplierCode( $value ); break;
case 'product.datestart':
if( $value != '' )
{
$value = str_replace( 'T', ' ', $value );
$entry->{'product.datestart'} = $value;
$item->setDateStart( $value );
}
break;
case 'product.dateend':
if( $value != '' )
{
$value = str_replace( 'T', ' ', $value );
$entry->{'product.dateend'} = $value;
$item->setDateEnd( $value );
}
break;
}
}
return $item;
}
/**
* Returns the manager the controller is using.
*
* @return MShop_Common_Manager_Interface Manager object
*/
protected function _getManager()
{
return $this->_manager;
}
}