forked from elabftw/elabftw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
187 lines (155 loc) · 5.49 KB
/
database.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
<?php
/**
* database.php
*
* @author Nicolas CARPi <[email protected]>
* @copyright 2012 Nicolas CARPi
* @see https://www.elabftw.net Official website
* @license AGPL-3.0
* @package elabftw
*/
namespace Elabftw\Elabftw;
use Exception;
use Symfony\Component\HttpFoundation\Response;
/**
* Entry point for database things
*
*/
require_once 'app/init.inc.php';
$App->pageTitle = _('Database');
try {
// show nothing to anon if admin didn't set the DB as public
if ($App->Session->has('anon') && ($App->teamConfigArr['public_db'] === '0')) {
throw new Exception(Tools::error(true));
}
$Entity = new Database($App->Users);
// VIEW
if ($Request->query->get('mode') === 'view') {
// set id
$Entity->setId($Request->query->get('id'));
// check permissions
$Entity->canOrExplode('read');
$UploadsView = new UploadsView($Entity->Uploads);
// the mode parameter is for the uploads tpl
$template = 'view.html';
$renderArr = array(
'Entity' => $Entity,
'Uv' => $UploadsView,
'mode' => 'view'
);
// EDIT
} elseif ($Request->query->get('mode') === 'edit') {
// set id
$Entity->setId($Request->query->get('id'));
// check permissions
$Entity->canOrExplode('write');
// a locked item cannot be edited
if ($Entity->entityData['locked']) {
throw new Exception(_('<strong>This item is locked.</strong> You cannot edit it.'));
}
$ItemsTypes = new ItemsTypes($Entity->Users);
$categoryArr = $ItemsTypes->readAll();
$Revisions = new Revisions($Entity);
$UploadsView = new UploadsView($Entity->Uploads);
$template = 'edit.html';
$renderArr = array(
'Entity' => $Entity,
'Categories' => $ItemsTypes,
'Revisions' => $Revisions,
'Uv' => $UploadsView,
'categoryArr' => $categoryArr,
'mode' => 'edit',
'maxUploadSize' => Tools::returnMaxUploadSize()
);
// DEFAULT MODE IS SHOW
} else {
// if this variable is not empty the error message shown will be different if there are no results
$searchType = null;
// CATEGORY FILTER
if (Tools::checkId($Request->query->get('cat'))) {
$Entity->categoryFilter = "AND items_types.id = " . $Request->query->get('cat');
$searchType = 'category';
}
// TAG FILTER
if ($Request->query->get('tag') != '') {
$tag = filter_var($Request->query->get('tag'), FILTER_SANITIZE_STRING);
$tag = $tag;
$Entity->tagFilter = "AND tagt.tag LIKE '" . $tag . "'";
$searchType = 'tag';
}
// QUERY FILTER
if ($Request->query->get('q') != '') {
$query = filter_var($Request->query->get('q'), FILTER_SANITIZE_STRING);
$Entity->queryFilter = "AND (
title LIKE '%$query%' OR
date LIKE '%$query%' OR
body LIKE '%$query%')";
$searchType = 'query';
}
// ORDER
$order = '';
// load the pref from the user
if (isset($Entity->Users->userData['orderby'])) {
$order = $Entity->Users->userData['orderby'];
}
// now get pref from the filter-order-sort menu
if ($Request->query->has('order')) {
$order = $Request->query->get('order');
}
if ($order === 'cat') {
$Entity->order = 'items_types.ordering';
} elseif ($order === 'date' || $order === 'rating' || $order === 'title') {
$Entity->order = 'items.' . $order;
}
// SORT
$sort = '';
// load the pref from the user
if (isset($Entity->Users->userData['sort'])) {
$sort = $Entity->Users->userData['sort'];
}
// now get pref from the filter-order-sort menu
if ($Request->query->has('sort')) {
$sort = $Request->query->get('sort');
}
if ($sort === 'asc' || $sort === 'desc') {
$Entity->sort = $sort;
}
// PAGINATION
$limit = $App->Users->userData['limit_nb'];
if ($Request->query->has('limit') && Tools::checkId($Request->query->get('limit'))) {
$limit = $Request->query->get('limit');
}
$offset = 0;
if ($Request->query->has('offset') && Tools::checkId($Request->query->get('offset'))) {
$offset = $Request->query->get('offset');
}
$showAll = true;
if ($Request->query->get('limit') !== 'over9000') {
$Entity->setOffset($offset);
$Entity->setLimit($limit);
$showAll = false;
}
// END PAGINATION
$ItemsTypes = new ItemsTypes($Entity->Users);
$categoryArr = $ItemsTypes->readAll();
$itemsArr = $Entity->read();
$template = 'show.html';
$renderArr = array(
'Entity' => $Entity,
'Request' => $Request,
'categoryArr' => $categoryArr,
'itemsArr' => $itemsArr,
'offset' => $offset,
'searchType' => $searchType,
'showAll' => $showAll
);
}
} catch (Exception $e) {
$template = 'error.html';
$renderArr = array('error' => $e->getMessage());
} finally {
$Response = new Response();
$Response->prepare($Request);
$Response->setContent($App->render($template, $renderArr));
$Response->send();
}