forked from quaderno/quaderno-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquaderno_model.php
More file actions
120 lines (99 loc) · 4.02 KB
/
quaderno_model.php
File metadata and controls
120 lines (99 loc) · 4.02 KB
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
<?php
/* Interface for every model */
abstract class QuadernoModel extends QuadernoClass {
//// Find for QuadernoModel objects
// If $params is a single value, it returns a single object
// If $params is null or an array, it returns an array of objects
// When request fails, it returns false
static function find($params=array('page' => 1)) {
$return = false;
$class = get_called_class();
if (!is_array($params)) {
// Searching for an ID
$response = QuadernoBase::findByID(static::$MODEL, $params);
if (QuadernoBase::responseIsValid($response)) $return = new $class($response['data']);
}
else {
$response = QuadernoBase::find(static::$MODEL, $params);
if (QuadernoBase::responseIsValid($response)) {
$return = array();
for ($i=0; $i<count($response['data']); $i++) $return[$i] = new $class($response['data'][$i]);
}
}
return $return;
}
//// Save for QuadernoModel objects
// Export object data to the model
// Returns true or false whether the request is accepted or not
public function save() {
$response = null;
$newobject = false;
$return = false;
////////// 1st step - New object to be created
// Check if the current object has not been created yet
if (is_null($this->id)) {
// Not yet created, let's do it
$response = QuadernoBase::save(static::$MODEL, $this->data, $this->id);
$newobject = true;
// Update data with the response
if (QuadernoBase::responseIsValid($response)) {
$this->data = $response['data'];
$return = true;
}
elseif (isset($response['data']['errors'])) $this->errors = $response['data']['errors'];
}
$response = null;
$newdata = false;
/////////// 2nd step - Payments to be created
// Check if there are any payments stored and not yet created
if (isset($this->paymentsArray) && count($this->paymentsArray)) {
foreach ($this->paymentsArray as $index => $p) {
if (is_null($p->id)) {
// The payment does not have ID -> Not yet created
$response = QuadernoBase::saveNested(static::$MODEL, $this->id, 'payments', $p->data);
if (QuadernoBase::responseIsValid($response)) {
$p->data = $response['data'];
$newdata = self::find($this->id);
}
elseif (isset($response['data']['errors'])) $this->errors = $response['data']['errors'];
}
if ($p->markToDelete) {
// The payment is marked to delete -> Let's do it.
$deleteResponse = QuadernoBase::deleteNested(static::$MODEL, $this->id, 'payments', $p->id);
if (QuadernoBase::responseIsValid($deleteResponse)) {
array_splice($this->paymentsArray, $index, 1);
}
elseif (isset($response['data']['errors'])) $this->errors = $response['data']['errors'];
}
}
// If this object has received new data, let's update data field.
if ($newdata) $this->data = $newdata->data;
}
////////// 3rd step - Update object
// Update object - This is only necessary when it's not a new object,
// or new payments have been created.
if (!$newobject || $newdata) {
$response = QuadernoBase::save(static::$MODEL, $this->data, $this->id);
if (QuadernoBase::responseIsValid($response)) {
$return = true;
$this->data = $response['data'];
}
elseif (isset($response['data']['errors'])) $this->errors = $response['data']['errors'];
}
return $return;
}
//// Delete for QuadernoModel objects
// Delete object from the model
// Returns true or false whether the request is accepted or not
public function delete() {
$return = false;
$response = QuadernoBase::delete(static::$MODEL, $this->id);
if (QuadernoBase::responseIsValid($response)) {
$return = true;
$this->data = array();
}
elseif (isset($response['data']['errors'])) $this->errors = $response['data']['errors'];
return $return;
}
}
?>