1
|
|
|
<?php |
|
|
|
|
2
|
|
|
require_once("/var/www/secure_settings/class.FlipsideSettings.php"); |
3
|
|
|
class ThemeDB |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private $dataSet; |
6
|
|
|
private $tables = array(); |
7
|
|
|
|
8
|
|
|
function __construct() |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
$this->dataSet = \DataSetFactory::getDataSetByName('registration'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
function getCurrentYear() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
if(!isset($this->tables['vars'])) |
16
|
|
|
{ |
17
|
|
|
$this->tables['vars'] = $this->dataSet['vars']; |
18
|
|
|
} |
19
|
|
|
$data = $this->tables['vars']->read(new \Data\Filter('name eq year')); |
20
|
|
|
if(empty($data)) |
21
|
|
|
{ |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
|
|
return $data[0]['value']; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function getAllFromCollection($collection, $year = false, $uid = false, $fields = false) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
if($year === false) |
30
|
|
|
{ |
31
|
|
|
$year = $this->getCurrentYear(); |
32
|
|
|
} |
33
|
|
|
$table = $this->dataSet[$collection]; |
34
|
|
|
$filter = false; |
35
|
|
|
if($year !== '*' && $uid !== false) |
36
|
|
|
{ |
37
|
|
|
$filter = new \Data\Filter("year eq $year and registrars eq $uid"); |
38
|
|
|
} |
39
|
|
|
else if($year !== '*') |
40
|
|
|
{ |
41
|
|
|
$filter = new \Data\Filter("year eq $year"); |
42
|
|
|
} |
43
|
|
|
else if($uid !== false) |
44
|
|
|
{ |
45
|
|
|
$filter = new \Data\Filter("registrars eq $uid"); |
46
|
|
|
} |
47
|
|
|
$data = $table->read($filter, $fields); |
48
|
|
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function searchFromCollection($collection, $criteria, $fields = false) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$col = $this->db->selectCollection($collection); |
|
|
|
|
54
|
|
|
foreach($criteria as $key=>$value) |
55
|
|
|
{ |
56
|
|
|
if($value[0] === '/') |
57
|
|
|
{ |
58
|
|
|
$criteria[$key] = array('$regex'=>new MongoRegex("$value")); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$cursor = $col->find($criteria); |
62
|
|
|
if($fields !== false) |
63
|
|
|
{ |
64
|
|
|
$cursor->fields($fields); |
65
|
|
|
} |
66
|
|
|
$ret = array(); |
67
|
|
|
foreach($cursor as $doc) |
68
|
|
|
{ |
69
|
|
|
array_push($ret, $doc); |
70
|
|
|
} |
71
|
|
|
return $ret; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
function getObjectFromCollectionByID($collection, $id) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$table = $this->dataSet[$collection]; |
77
|
|
|
$data = $table->read(new \Data\Filter("_id eq $id")); |
78
|
|
|
if(empty($data)) |
79
|
|
|
{ |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
return $data[0]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
function addObjectToCollection($collection, $obj) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
unset($obj['_id']); |
88
|
|
|
$table = $this->dataSet[$collection]; |
89
|
|
|
$res = $table->create($obj); |
90
|
|
|
if($res !== false) |
91
|
|
|
{ |
92
|
|
|
return $res; |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
function updateObjectInCollection($collection, $obj) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$id = $obj['_id']; |
100
|
|
|
unset($obj['_id']); |
101
|
|
|
$table = $this->dataSet[$collection]; |
102
|
|
|
$res = $table->update(new \Data\Filter("_id eq $id"), $obj); |
103
|
|
|
if($res !== false) |
104
|
|
|
{ |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
function deleteObjectFromCollection($collection, $obj) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$id = $obj['_id']; |
113
|
|
|
unset($obj['_id']); |
114
|
|
|
$table = $this->dataSet[$collection]; |
115
|
|
|
return $table->delete(new \Data\Filter("_id eq $id")); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
function getAllThemes($year = false) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return $this->getAllFromCollection('themes', $year); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
function getAllThemesForUser($uid, $year = false) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
return $this->getAllFromCollection('themes', $year, $uid); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
function getThemeByID($id) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
return $this->getObjectFromCollectionByID('themes', $id); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
function deleteTheme($theme) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return $this->db->themes->remove(array('_id'=>new MongoId($theme['_id']))); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
function addTheme($theme) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
unset($theme['_id']); |
141
|
|
|
$res = $this->db->themes->insert($theme); |
142
|
|
|
if($res['ok'] === true) |
143
|
|
|
{ |
144
|
|
|
return $theme['_id']; |
145
|
|
|
} |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function updateTheme($theme) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$id = new MongoId($theme['_id']); |
152
|
|
|
unset($theme['_id']); |
153
|
|
|
$res = $this->db->themes->update(array('_id' => $id), $theme); |
154
|
|
|
if($res['ok'] === true) |
155
|
|
|
{ |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
// vim: set tabstop=4 shiftwidth=4 expandtab: |
162
|
|
|
?> |
|
|
|
|
163
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.