/
item.php
108 lines (81 loc) · 3.05 KB
/
item.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
<?php
use Joomla\Registry\Registry;
/**
* Created by PhpStorm.
* User: Ironman
* Date: 7/18/2015
* Time: 6:11 PM
*/
/** todo: prevent changing order after paymentId */
class Sp4kAppsOrderItem extends Sp4kAppsAbstractItem
{
use Sp4kAppsOrderTrait;
public $id;
public $state;
public $created;
public $account_id;
public $total;
public $items;
public $source;
protected $_toJson = [];
public $error = false;
protected function load()
{
$result = false;
// Load the table object, empty if _state->data->key = null;
$this->getTable()->load(
$this->getState()->get($this->_key,null)
);
//if we have incoming data, bind it to the table object
$this->_table->bind(
$this->getState()->toObject()
);
// set the state to the table data so
// that any empty variables in the incoming data are populated with table data.
$this->getState()->loadArray(get_object_vars($this->_table),true);
}
protected function bind()
{
$stateProperties = $this->getState()->toArray();
foreach( get_object_vars($this) as $index=>$property){//get the defined properties for this class.
if(array_key_exists($index,$stateProperties)){//if the state contains an index of the same name
if(in_array($index,$this->_toJson) && !is_string($stateProperties[$index]))
$stateProperties[$index] = json_encode($stateProperties[$index]);
$this->$index = $stateProperties[$index];//assign its value to this instance.
}
}
$this->items =Sp4kAppsOrderLinesApp::getInstance(
new Registry(
[
'data'=>$this->getState()->get('items',false),
'filters'=>[
'order_id'=>$this->getState()->get('id')
]
]
)
)->getItems();
$this->parent = Sp4kAppsParentApp::getInstance(
new Registry([
'filters'=>['account_id'=>$this->getState()->get('account_id'),'primary'=>1],
'keys'=>false
])
)->getItems()[0];
}
public function update()
{
if(!isset($this->created)){
$this->created = time();
}
$this->state = 1;
$this->getTable()->addJsonEncodedField('data')->save((array)$this);
$this->getState()->loadArray(get_object_vars($this->_table));
$this->bind();
foreach($this->items as &$item){
$item->order_id = $this->id;
$item->update();
}
$this->getState()->set('items',$this->items);
//$this->bind();
return $this;
}
}