forked from zotero/dataserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemTypes.inc.php
272 lines (218 loc) · 7.19 KB
/
ItemTypes.inc.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the Zotero Data Server.
Copyright © 2010 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
class Zotero_ItemTypes {
private static $typeIDs = array();
private static $typeNames = array();
private static $customTypeCheck = array();
private static $localizedStrings = array(
"note" => "Note",
"attachment" => "Attachment",
"book" => "Book",
"bookSection" => "Book Section",
"journalArticle" => "Journal Article",
"magazineArticle" => "Magazine Article",
"newspaperArticle" => "Newspaper Article",
"thesis" => "Thesis",
"letter" => "Letter",
"manuscript" => "Manuscript",
"interview" => "Interview",
"film" => "Film",
"artwork" => "Artwork",
"webpage" => "Web Page",
"report" => "Report",
"bill" => "Bill",
"case" => "Case",
"hearing" => "Hearing",
"patent" => "Patent",
"statute" => "Statute",
"email" => "E-mail",
"map" => "Map",
"blogPost" => "Blog Post",
"instantMessage" => "Instant Message",
"forumPost" => "Forum Post",
"audioRecording" => "Audio Recording",
"presentation" => "Presentation",
"videoRecording" => "Video Recording",
"tvBroadcast" => "TV Broadcast",
"radioBroadcast" => "Radio Broadcast",
"podcast" => "Podcast",
"computerProgram" => "Computer Program",
"conferencePaper" => "Conference Paper",
"document" => "Document",
"encyclopediaArticle" => "Encyclopedia Article",
"dictionaryEntry" => "Dictionary Entry",
"nsfReviewer" => "NSF Reviewer"
);
public static function getID($typeOrTypeID) {
if (!$typeOrTypeID) {
throw new Exception("An item type id or name must be provided");
}
if (isset(self::$typeIDs[$typeOrTypeID])) {
return self::$typeIDs[$typeOrTypeID];
}
$cacheKey = "itemTypeID_" . $typeOrTypeID;
$typeID = Z_Core::$MC->get($cacheKey);
if ($typeID) {
// casts are temporary until memcached reload
self::$typeIDs[$typeOrTypeID] = (int) $typeID;
return (int) $typeID;
}
$sql = "(SELECT itemTypeID FROM itemTypes WHERE itemTypeID=?) UNION
(SELECT itemTypeID FROM itemTypes WHERE itemTypeName=?) LIMIT 1";
$typeID = Zotero_DB::valueQuery($sql, array($typeOrTypeID, $typeOrTypeID));
self::$typeIDs[$typeOrTypeID] = $typeID ? (int) $typeID : false;
Z_Core::$MC->set($cacheKey, (int) $typeID);
return $typeID ? (int) $typeID : false;
}
public static function getName($typeOrTypeID) {
if (!$typeOrTypeID) {
throw new Exception("An item type id or name must be provided");
}
if (isset(self::$typeNames[$typeOrTypeID])) {
return self::$typeNames[$typeOrTypeID];
}
$cacheKey = "itemTypeName_" . $typeOrTypeID;
$typeName = Z_Core::$MC->get($cacheKey);
if ($typeName) {
self::$typeNames[$typeOrTypeID] = $typeName;
return $typeName;
}
$sql = "(SELECT itemTypeName FROM itemTypes WHERE itemTypeID=?) UNION
(SELECT itemTypeName FROM itemTypes WHERE itemTypeName=?) LIMIT 1";
$typeName = Zotero_DB::valueQuery($sql, array($typeOrTypeID, $typeOrTypeID));
self::$typeNames[$typeOrTypeID] = $typeName;
Z_Core::$MC->set($cacheKey, $typeName);
return $typeName;
}
public static function getLocalizedString($typeOrTypeID, $locale='en-US') {
if ($locale != 'en-US') {
throw new Exception("Locale not yet supported");
}
$itemType = self::getName($typeOrTypeID);
return self::$localizedStrings[$itemType];
}
public static function getAll($locale=false) {
$sql = "SELECT itemTypeID AS id, itemTypeName AS name FROM itemTypes";
// TEMP - skip nsfReviewer and attachment
$sql .= " WHERE itemTypeID NOT IN (14,10001)";
$rows = Zotero_DB::query($sql);
// TODO: cache
if (!$locale) {
return $rows;
}
foreach ($rows as &$row) {
$row['localized'] = self::getLocalizedString($row['id'], $locale);
}
usort($rows, function ($a, $b) {
return strcmp($a["localized"], $b["localized"]);
});
return $rows;
}
public static function getImageSrc($itemType, $linkMode=false, $mimeType=false) {
$prefix = "/static/i/itemType/treeitem";
if ($itemType == 'attachment') {
if ($mimeType == 'application/pdf') {
$itemType .= '-pdf';
}
else {
switch ($linkMode) {
case 0:
$itemType .= '-file';
break;
case 1:
$itemType .= '-file';
break;
case 2:
$itemType .= '-snapshot';
break;
case 3:
$itemType .= '-web-link';
break;
}
}
}
// DEBUG: only have icons for some types so far
switch ($itemType) {
case 'attachment-file':
case 'attachment-link':
case 'attachment-snapshot':
case 'attachment-web-link':
case 'attachment-pdf':
case 'artwork':
case 'audioRecording':
case 'blogPost':
case 'book':
case 'bookSection':
case 'computerProgram':
case 'conferencePaper':
case 'email':
case 'film':
case 'forumPost':
case 'interview':
case 'journalArticle':
case 'letter':
case 'magazineArticle':
case 'manuscript':
case 'map':
case 'newspaperArticle':
case 'note':
case 'podcast':
case 'radioBroadcast':
case 'report':
case 'thesis':
case 'tvBroadcast':
case 'videoRecording':
case 'webpage':
return $prefix . '-' . $itemType . ".png";
}
return $prefix . ".png";
}
public static function isCustomType($itemTypeID) {
if (isset(self::$customTypeCheck)) {
return self::$customTypeCheck;
}
$sql = "SELECT custom FROM itemTypes WHERE itemTypeID=?";
$isCustom = Zotero_DB::valueQuery($sql, $itemTypeID);
if ($isCustom === false) {
throw new Exception("Invalid itemTypeID '$itemTypeID'");
}
self::$customTypesCheck[$itemTypeID] = !!$isCustom;
return !!$isCustom;
}
public static function addCustomType($name) {
if (self::getID($name)) {
throw new Exception("Item type '$name' already exists");
}
if (!preg_match('/^[a-z][^\s0-9]+$/', $name)) {
throw new Exception("Invalid item type name '$name'");
}
// TODO: make sure user hasn't added too many already
throw new Exception("Unimplemented");
// TODO: add to cache
Zotero_DB::beginTransaction();
$sql = "SELECT NEXT_ID(itemTypeID) FROM itemTypes";
$itemTypeID = Zotero_DB::valueQuery($sql);
$sql = "INSERT INTO itemTypes (?, ?, ?)";
Zotero_DB::query($sql, array($itemTypeID, $name, 1));
Zotero_DB::commit();
return $itemTypeID;
}
}
?>