/
Default.php
116 lines (98 loc) · 2.91 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
<?php
/**
* @copyright Copyright (c) Metaways Infosystems GmbH, 2011
* @license LGPLv3, http://www.arcavias.com/en/license
* @package Controller
* @subpackage ExtJS
*/
/**
* ExtJS product stock controller for admin interfaces.
*
* @package Controller
* @subpackage ExtJS
*/
class Controller_ExtJS_Product_Stock_Default
extends Controller_ExtJS_Abstract
implements Controller_ExtJS_Common_Interface
{
private $_manager = null;
/**
* Initializes the product stock controller.
*
* @param MShop_Context_Item_Interface $context MShop context object
*/
public function __construct( MShop_Context_Item_Interface $context )
{
parent::__construct( $context, 'Product_Stock' );
$manager = MShop_Product_Manager_Factory::createManager( $context );
$this->_manager = $manager->getSubManager( 'stock' );
}
/**
* Creates a new product stock item or updates an existing one or a list thereof.
*
* @param stdClass $params Associative array containing the product stock properties
*/
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();
}
$search = $this->_manager->createSearch();
$search->setConditions( $search->compare( '==', 'product.stock.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,
);
}
/**
* Creates a new product stock item and sets the properties from the given array.
*
* @param array $entry Associative list of name and value properties using the "product.stock" prefix
* @return MShop_Product_Item_Stock_Interface Product stock item
*/
protected function _createItem( array $entry )
{
$item = $this->_manager->createItem();
foreach( $entry as $name => $value )
{
switch( $name )
{
case 'product.stock.id': $item->setId( $value ); break;
case 'product.stock.productid': $item->setProductId( $value ); break;
case 'product.stock.warehouseid': $item->setWarehouseId( $value ); break;
case 'product.stock.stocklevel':
if( $value != '' ) {
$item->setStocklevel( $value );
}
break;
case 'product.stock.dateback':
if( $value != '' )
{
$value = str_replace( 'T', ' ', $value );
$entry->{'product.stock.dateback'} = $value;
$item->setDateBack( $value );
}
break;
}
}
return $item;
}
/**
* Returns the manager the controller is using.
*
* @return MShop_Common_Manager_Interface Manager object
*/
protected function _getManager()
{
return $this->_manager;
}
}