-
Notifications
You must be signed in to change notification settings - Fork 3
/
Level.php
executable file
·241 lines (208 loc) · 5.99 KB
/
Level.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Vehicle Fits (http://www.vehiclefits.com for more information.)
* @copyright Copyright (c) Vehicle Fits, llc
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class VF_Level implements VF_Configurable
{
protected $id;
protected $title;
protected $type;
/** @var VF_Level_Finder */
protected $finder;
/** @var VF_Schema */
protected $schema;
/** @var Zend_Config */
protected $config;
function __construct($type, $id = 0, $schema = null)
{
$this->schema = $schema ? $schema : new VF_Schema;
if ($id && !in_array($type, $this->getSchema()->getLevels())) {
throw new VF_Level_Exception_InvalidLevel('[' . $type . '] is an invalid level');
}
$this->type = $type;
$this->id = $id;
}
function identityMap()
{
return VF_Level_IdentityMap::getInstance();
}
function getConfig()
{
if (!$this->config instanceof Zend_Config) {
$this->config = VF_Singleton::getInstance()->getConfig();
}
return $this->config;
}
function setConfig(Zend_Config $config)
{
$this->config = $config;
}
/** @return VF_Level_Finder|VF_Level_Finder_Selector */
function getFinder()
{
if (!($this->finder instanceof VF_Level_Finder)) {
$this->finder = new VF_Level_Finder($this->getSchema());
}
$this->finder->setConfig($this->getConfig());
return $this->finder;
}
function getId()
{
return $this->id;
}
function setId($id)
{
if (0 == $this->id) {
$this->id = $id;
} else {
throw new Exception('cannot set id if its previously already set');
}
}
function setTitle($title)
{
$this->title = trim($title);
return $this;
}
function getTitle()
{
return (string)$this->title;
}
function getLabel()
{
return $this->getType();
}
function getNextLevel()
{
return $this->getSchema()->getNextLevel($this->getType());
}
function getPrevLevel()
{
return $this->getSchema()->getPrevLevel($this->getType());
}
function createEntity($level, $id = 0)
{
return new VF_Level($level, $id);
}
function getType()
{
return $this->type;
}
function getChildCount()
{
return $this->getFinder()->getChildCount($this);
}
function getChildren()
{
return $this->getFinder()->getChildren($this);
}
/**
* Save the model
*
* @param mixed $parent_id optional parent_id this model is being saved under
* @return int primary key id of created entity
*/
function save($parent_id = 0, $requestedSaveId = null, $createDefinition = true)
{
if ('' == trim($this->getTitle())) {
throw new Exception('Must have a non blank title to save ' . $this->getType());
}
$levelId = $this->findEntityIdByTitle();
if ($levelId) {
$this->id = $levelId;
return $levelId;
}
if ($requestedSaveId && $this->requestedIdCorrespondsToExistingRecord($requestedSaveId)) {
$this->id = $requestedSaveId;
}
if ($this->getId()) {
$sql = sprintf(
"UPDATE %s SET `title` = %s WHERE id = %d",
$this->getReadAdapter()->quoteIdentifier($this->getTable()),
$this->getReadAdapter()->quote($this->getTitle()),
(int)$this->getId()
);
$this->query($sql);
return $this->id;
}
$data = array('title' => $this->getTitle());
if ($requestedSaveId) {
$data['id'] = $requestedSaveId;
}
$this->getReadAdapter()->insert($this->getTable(), $data);
$levelId = $this->getReadAdapter()->lastInsertId();
$this->setId($levelId);
return $levelId;
}
function vehicleFinder()
{
return new VF_Vehicle_Finder($this->getSchema());
}
function requestedIdCorrespondsToExistingRecord($requestedSaveId)
{
$select = $this->getReadAdapter()->select()
->from($this->getTable(), 'count(*)')
->where('id=?', (int)$requestedSaveId);
$result = $this->getReadAdapter()->query($select);
return (bool)$result->fetchColumn();
}
function listAll($parent_id = 0)
{
return $this->getFinder()->listAll($this, $parent_id);
}
function getSortOrder()
{
if ($sort = $this->getSchema()->getSorting($this->getType())) {
return $sort;
}
return "ASC";
}
function listInUse($parents = array(), $product_id = 0)
{
return $this->getFinder()->listInUse($this, $parents, $product_id);
}
function listInUseByTitle($parents = array(), $product_id = 0)
{
return $this->getFinder()->listInUseByTitle($this, $parents, $product_id);
}
function getTable()
{
return 'elite_level_' . $this->getSchema()->id() . '_' . str_replace(' ', '_', $this->getType());
}
function getLevels()
{
return $this->getSchema()->getLevels();
}
function getLeafLevel()
{
return $this->getSchema()->getLeafLevel();
}
function getRootLevel()
{
return $this->getSchema()->getRootLevel();
}
/** @return integer ID */
function findEntityIdByTitle($parent_id = 0)
{
return $this->getFinder()->findEntityIdByTitle($this->getType(), $this->getTitle(), $parent_id);
}
function getSchema()
{
return $this->schema;
}
function __toString()
{
return $this->getTitle();
}
/** @return Zend_Db_Statement_Interface */
function query($sql)
{
return $this->getReadAdapter()->query($sql);
}
/** @return Zend_Db_Adapter_Abstract */
function getReadAdapter()
{
return VF_Singleton::getInstance()->getReadAdapter();
}
}