forked from opendream/hotri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiblioField.php
265 lines (255 loc) · 8.61 KB
/
BiblioField.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
<?php
/* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
* See the file COPYRIGHT.html for more details.
*/
require_once("../classes/Localize.php");
/******************************************************************************
* UsmarcField represents a library bibliography subfield. Contains business rules for
* subfield data validation.
*
* @author David Stevens <[email protected]>;
* @version 1.0
* @access public
******************************************************************************
*/
class BiblioField {
var $_bibid = "";
var $_fieldid = "";
var $_tag = "";
var $_tagError = "";
var $_ind1Cd = "";
var $_ind2Cd = "";
var $_subfieldCd = "";
var $_subfieldCdError = "";
var $_fieldData = "";
var $_fieldDataError = "";
var $_isRequired = false;
var $_isRepeatable = false;
/****************************************************************************
* @return boolean true if data is valid, otherwise false.
* @access public
****************************************************************************
*/
function validateData() {
$loc = new Localize(OBIB_LOCALE,"classes");
$valid = true;
if (($this->_isRequired ) and ($this->_fieldData == "")) {
$valid = false;
$this->_fieldDataError = $loc->getText("biblioFieldError1");
}
if ($this->_tag == "") {
$valid = false;
$this->_tagError = $loc->getText("biblioFieldError1");
} else if (!is_numeric($this->_tag)) {
$valid = false;
$this->_tagError = $loc->getText("biblioFieldError2");
}
if ($this->_subfieldCd == "") {
$valid = false;
$this->_subfieldCdError = $loc->getText("biblioFieldError1");
}
// Check for image
if ($this->getTag() == "902" && $this->getSubfieldCd() == "a") {
$fieldData = $this->getFieldData();
if (!empty($fieldData)) {
if ($fieldData['uselookup'] === true) { // Override with cover lookup
$isbn = $fieldData['isbn'];
require_once("BiblioCoverQuery.php");
$cq = new BiblioCoverQuery();
$path = $cq->lookup($isbn, "large");
if (!$path) {
$this->_fieldDataError = $loc->getText("biblioFieldErrorCoverLookupFailed");
$valid = false;
}
else {
$filename = $cq->save($path, 0);
$this->setFieldData($filename);
}
}
else {
$index = $this->getTag().$this->getSubfieldCd();
if (!empty($fieldData["tmp_name"][$index])) {
if ($info = getimagesize($fieldData["tmp_name"][$index])) {
$filename = $fieldData["name"][$index];
$filename_parts = explode(".", $filename);
unset($filename_parts[count($filename_parts) - 1]);
$filename = implode("-", $filename_parts);
$allow_types = array(
'image/jpeg',
'image/png',
'image/gif',
);
// If file type is allowed
if (in_array($info["mime"], $allow_types)) {
// Create directory when necessary, raise error when failed to create "not exist" one.
if (!(is_dir('../' . COVER_PATH) || is_dir('../' . COVER_PATH_TMP))) {
$dir_error = FALSE;
if (is_dir('../' . dirname(COVER_PATH))) {
// Create new one.
$cover_path = @mkdir('../' . COVER_PATH, 0777);
$tmp_path = @mkdir('../' . COVER_PATH_TMP, 0777);
if (!$cover_path) {
if (is_dir('../' . COVER_PATH)) {
if (decoct(fileperms('../' . COVER_PATH)) != 0777) {
$force_chmod = @chmod('../' . COVER_PATH, 0777);
if (!$force_chmod) {
$dir_error = TRUE;
}
}
}
else {
$dir_error = TRUE;
}
}
if (!$tmp_path) {
if (is_dir('../' . COVER_PATH_TMP)) {
if (decoct(fileperms('../' . COVER_PATH_TMP)) != 0777) {
$force_chmod = @chmod('../' . COVER_PATH_TMP, 0777);
if (!$force_chmod) {
$dir_error = TRUE;
}
}
}
else {
$dir_error = TRUE;
}
}
if ($dir_error) {
print_r('<span style="color: red"><strong>Error:</strong> Failed to save book cover! please set chmod 777 to /media directory.</span><br />');
return false;
}
}
}
$ext = image_type_to_extension($info[2]);
$tmp = md5($filename.session_id().time());
$filename = $filename."_".substr($tmp, strlen($tmp) - 7, strlen($tmp)).$ext;
$filepath = "../" . COVER_PATH . "/$filename";
copy($fieldData["tmp_name"][$index], $filepath);
make_thumbnail($filepath, array('height' => 160));
$this->setFieldData($filename);
}
else {
$valid = false;
$this->_fieldDataError = $loc->getText("biblioFieldErrorPictureType");
}
}
else {
$valid = false;
$this->_fieldDataError = $loc->getText("biblioFieldErrorPictureLoadFailed");
}
}
}
}
else {
}
}
/* Check existing ISBN items */
else if ($this->getTag() == "20" && $this->getSubfieldCd() == "a") {
require_once("../classes/BiblioQuery.php");
$biblio = new BiblioQuery();
$existBibId = $biblio->ISBNExists($this->getFieldData());
if ($existBibId && $this->_bibid != $existBibId) {
$valid = false;
$this->_fieldDataError = $loc->getText("biblioFieldErrorDuplicatedISBN") . ' <a href="../shared/biblio_view.php?bibid=' . $existBibId . '&tab=cataloging">' . $loc->getText("biblioFieldViewExistingISBN") . '</a>';
}
}
unset($loc);
return $valid;
}
/****************************************************************************
* Getter methods for all fields
* @return string
* @access public
****************************************************************************
*/
function getBibid() {
return $this->_bibid;
}
function getFieldid() {
return $this->_fieldid;
}
function getTag() {
return $this->_tag;
}
function getTagError() {
return $this->_tagError;
}
function getInd1Cd() {
return $this->_ind1Cd;
}
function getInd2Cd() {
return $this->_ind2Cd;
}
function getSubfieldCd() {
return $this->_subfieldCd;
}
function getSubfieldCdError() {
return $this->_subfieldCdError;
}
function getFieldData() {
return $this->_fieldData;
}
function getFieldDataError() {
return $this->_fieldDataError;
}
function isRequired() {
if ($this->_isRequired) {
return true;
} else {
return false;
}
}
function isRepeatable() {
if ($this->_isRepeatable) {
return true;
} else {
return false;
}
}
/****************************************************************************
* Setter methods for all fields
* @param string $value new value to set
* @return void
* @access public
****************************************************************************
*/
function setBibid($value) {
$this->_bibid = trim($value);
}
function setFieldid($value) {
$this->_fieldid = trim($value);
}
function setTag($value) {
$this->_tag = trim($value);
}
function setInd1Cd($value) {
$this->_ind1Cd = substr(trim($value),0,1);
}
function setInd2Cd($value) {
$this->_ind2Cd = substr(trim($value),0,1);
}
function setSubfieldCd($value) {
$this->_subfieldCd = substr(trim($value),0,1);
}
function setFieldData($value) {
$this->_fieldData = is_array($value) ? $value : trim($value);
}
function setFieldDataError($value) {
$this->_fieldDataError = trim($value);
}
function setIsRequired($value) {
if ($value) {
$this->_isRequired = true;
} else {
$this->_isRequired = false;
}
}
function setIsRepeatable($value) {
if ($value) {
$this->_isRepeatable = true;
} else {
$this->_isRepeatable = false;
}
}
}
?>