-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.ThemeDB.php
162 lines (148 loc) · 4.03 KB
/
class.ThemeDB.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
<?php
//require_once("/var/www/secure_settings/class.FlipsideSettings.php");
class ThemeDB
{
private $dataSet;
private $tables = array();
function __construct()
{
$this->dataSet = \DataSetFactory::getDataSetByName('registration');
}
function getCurrentYear()
{
if(!isset($this->tables['vars']))
{
$this->tables['vars'] = $this->dataSet['vars'];
}
$data = $this->tables['vars']->read(new \Data\Filter('name eq year'));
if(empty($data))
{
return false;
}
return $data[0]['value'];
}
function getAllFromCollection($collection, $year = false, $uid = false, $fields = false)
{
if($year === false)
{
$year = $this->getCurrentYear();
}
$table = $this->dataSet[$collection];
$filter = false;
if($year !== '*' && $uid !== false)
{
$filter = new \Data\Filter("year eq $year and registrars eq $uid");
}
else if($year !== '*')
{
$filter = new \Data\Filter("year eq $year");
}
else if($uid !== false)
{
$filter = new \Data\Filter("registrars eq $uid");
}
$data = $table->read($filter, $fields);
return $data;
}
function searchFromCollection($collection, $criteria, $fields = false)
{
$col = $this->db->selectCollection($collection);
foreach($criteria as $key=>$value)
{
if($value[0] === '/')
{
$criteria[$key] = array('$regex'=>new MongoRegex("$value"));
}
}
$cursor = $col->find($criteria);
if($fields !== false)
{
$cursor->fields($fields);
}
$ret = array();
foreach($cursor as $doc)
{
array_push($ret, $doc);
}
return $ret;
}
function getObjectFromCollectionByID($collection, $id)
{
$table = $this->dataSet[$collection];
$data = $table->read(new \Data\Filter("_id eq $id"));
if(empty($data))
{
return false;
}
return $data[0];
}
function addObjectToCollection($collection, $obj)
{
unset($obj['_id']);
$table = $this->dataSet[$collection];
$res = $table->create($obj);
if($res !== false)
{
return $res;
}
return false;
}
function updateObjectInCollection($collection, $obj)
{
$id = $obj['_id'];
unset($obj['_id']);
$table = $this->dataSet[$collection];
$res = $table->update(new \Data\Filter("_id eq $id"), $obj);
if($res !== false)
{
return true;
}
return false;
}
function deleteObjectFromCollection($collection, $obj)
{
$id = $obj['_id'];
unset($obj['_id']);
$table = $this->dataSet[$collection];
return $table->delete(new \Data\Filter("_id eq $id"));
}
function getAllThemes($year = false)
{
return $this->getAllFromCollection('themes', $year);
}
function getAllThemesForUser($uid, $year = false)
{
return $this->getAllFromCollection('themes', $year, $uid);
}
function getThemeByID($id)
{
return $this->getObjectFromCollectionByID('themes', $id);
}
function deleteTheme($theme)
{
return $this->db->themes->remove(array('_id'=>new MongoId($theme['_id'])));
}
function addTheme($theme)
{
unset($theme['_id']);
$res = $this->db->themes->insert($theme);
if($res['ok'] === true)
{
return $theme['_id'];
}
return false;
}
function updateTheme($theme)
{
$id = new MongoId($theme['_id']);
unset($theme['_id']);
$res = $this->db->themes->update(array('_id' => $id), $theme);
if($res['ok'] === true)
{
return true;
}
return false;
}
}
// vim: set tabstop=4 shiftwidth=4 expandtab:
?>