documentManager.inc
9.4 KB
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
273
274
275
276
277
278
279
280
<?php
/**
* Class DocumentManager
*
* Contains all functions required for document management,
* such as the creation/deletion/removal of document types.
*
* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @date 8 January 2003
*
* @todo Complete updateDocumentType() function
*
*/
require_once ("$default->owl_root_url/lib/owl.lib.php");
class DocumentManager {
/**
* Create a new document type
*
* @param $sName Name of new document type
*
* @return true on successful creation, false otherwise and set $default->errorMessage
*/
function createDocumentType($sName) {
//escape all the necessary characters that may affect db query
$sName = addslashes($sName);
//Get hold of the global error string
global $default;
//if the document type doesn't exist
if (!($this->documentTypeExists($sName))) {
$sql = new Owl_DB();
$result = $sql->query("INSERT INTO " . $default->owl_document_types_table . " (name) values ('" . $sName . "')");
if (!$result) {
$default->errorMessage = "Database Error. Failed to insert document type " . $sName;
return false;
}
return true;
}
$default->errorMessage = "A document type with this name already exists";
return false;
}
/**
* Delete an existing document type
*
* @param $sName Name of document type to delete
*
* @return true on successful removal, false otherwise and set $default->errorMessage
*/
function deleteDocumentType($sName) {
//escape all the necessary characters that may affect db query
$sName = addslashes($sName);
//Get hold of the global error string
global $default;
//only remove the document type if it exists
if ($this->documentTypeExists($sName)) {
$sql = new Owl_DB();
$result = $sql->query("DELETE FROM " . $default->owl_document_types_table . " WHERE name = '" . $sName . "'");
if (!$result) {
$default->errorMessage = "Database Error. Failed to delete document type " . $sName;
return false;
}
return true;
}
$default->errorMessage = "There is no document type with this name";
return false;
}
/**
* Update a document type
*
* @param $iDocumentTypeID Primary key of document type to updatee
* @param $sName New document type name
*
* @return true on successful update, false otherwise and set $default->errorMessage
*/
function updateDocumentType($iDocumentTypeID, $sName) {
}
/**
* Get the primary key for a document type
*
* @param $sName Name of document type to get ID for
*
* @return int document type id if the document type exists, false otherwise
*/
function & getDocumentTypeID($sName) {
global $lang_err_document_type_field_does_not_exist, $default;
//escape special characters that may interfere with the db query
$sName = addslashes($sName);
if ($this->documentTypeExists($sName)) {
$sql = new Owl_DB();
$sql->query("SELECT ID FROM " . $default->owl_document_types_table . " WHERE name = '" . $sName . "'");
$sql->next_record();
return $sql->f("ID");
}
$default->errorMessage = $lang_err_document_type_does_not_exist . $sName;
return false;
}
/**
* Checks to see if a document type with a given name
* already exists
*
* @param $sName Name of document type to check
*
* @return true if the document type exists, false otherwise
*/
function documentTypeExists($sName) {
//escape all the necessary characters that may affect db query
$sName = addslashes($sName);
//Get hold of the global error string
global $default;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_document_types_table . " WHERE Name = '" . $sName . "'");
return $sql->next_record();
}
/**
* Link a document type field with a specific document type
*
* @param $iDocumentTypeID Primary key of document type
* @param $iDocumentTypeFieldID Primary key of document field type
* @param $bIsMandatory Whether or not the field is mandatory
*
* @return true on successful creation, false otherwise and set $default->errorMessage
*/
function createDocumentTypeFieldLink($iDocumentTypeID, $iDocumentTypeFieldID, $bIsMandatory) {
//Get hold of the global error string
global $default;
//if the document field type is not associated with the document
if (!($this->documentTypeFieldExistsForDocumentType($iDocumentTypeID, $iDocumentTypeFieldID))) {
$sql = new Owl_DB();
$result = $sql->query("INSERT INTO " . $default->owl_document_type_fields_table . " (document_type_id, field_id, is_mandatory) VALUES (" . $iDocumentTypeID . ", " . $iDocumentTypeFieldID . ", " . $bIsMandatory . ")");
if (!$result) {
$default->errorMessage = "Database Error. Failed to insert document field type with ID " . $iDocumentTypeFieldID;
return false;
}
return true;
}
$default->errorMessage = "This field type is already linked to this document type";
return false;
}
/**
* Delete the link between a document type field and a specific document type
*
* @param $iDocumentTypeID Primary key of document type
* @param $iDocumentTypeFieldID Primary key of document type field
*/
function deleteDocumentTypeFieldLink($iDocumentTypeID, $iDocumentTypeFieldID) {
global $default;
//Get hold of the global error string
global $default;
if ($this->documentTypeFieldExistsForDocumentType($iDocumentTypeID, $iDocumentTypeFieldID)) {
$sql = new Owl_DB();
$result = $sql->query("DELETE FROM " . $default->owl_document_type_fields_table . " where document_type_id = " . $iDocumentTypeID . " AND field_id = " . $iDocumentTypeFieldID);
if (!result) {
$default->errorMessage = "Database Error. Failed to deleted document type field with document_type_id " . $iDocumentTypeID . " and field_id " . $iDocumentTypeFieldID;
return false;
}
return true;
}
$default->errorMessage = "A dcoument field type with document_type_id " . $iDocumentTypeID . " and a document_id " . $iDocumentTypeID . " does not exist";
return false;
}
/**
* Checks to see if the given document type field is already linked to the given
* document type.
*
* @param $iDocumentTypeID Primary key of document type
* @param $iDocumentTypeFieldID Primary key of document field type
*
* @return true is the document field type is linked to the document type, false otherwise
*/
function documentTypeFieldExistsForDocumentType($iDocumentTypeID, $iDocumentTypeFieldID) {
global $default;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_document_type_fields_table . " WHERE document_type_id = " . $iDocumentTypeID . " AND field_id = " . $iDocumentTypeFieldID);
return $sql->next_record();
}
/**
* Creates a document type field
*
* @param $sName Document type field name
* @param $sDataType Field data type
*
* @return true on successful insertion, false otherwise and sets $default->errorMessage
*/
function createDocumentTypeField($sName, $sDataType) {
//escape all the necessary characters that may affect db query
$sName = addslashes($sName);
$sDataType = addslashes($sDataType);
//Get hold of the global error string
global $default;
if (!$this->documentTypeFieldExists($sName)) {
$sql = new Owl_DB();
$result = $sql->query("INSERT INTO " . $default->owl_fields_table . " (name, data_type) VALUES ('" . $sName . "', '" . $sDataType . "')");
if (!$result) {
$default->errorMessage = "Database Error. Could not insert document field " . $sName . " with data type " . $sDataType . " into table " . $default->owl_fields_table;
return false;
}
return true;
}
$default->errorMessage = "A document type field with this name already exists";
return false;
}
/**
* Deletes a document type field
*
* @param $sName Name of document field type to delete
*
* @return true on successful deletion, false otherwise and set $default->errorMessage
*/
function deleteDocumentTypeField($sName) {
//escape any characters that may affect db query
$sName = addslashes($sName);
//Get hold of the global error string
global $default;
if ($this->documentTypeFieldExists($sName)) {
$sql = new Owl_DB();
$result = $sql->query("DELETE FROM " . $default->owl_fields_table . " WHERE Name = '" . $sName . "'");
if (!$result) {
$default->errorMessage = "Database Error. Could not delete document type field " . $sName . " from table " . $default->owl_field_table;
return false;
}
return true;
}
$default->errorMessage = "A document type field with the name " . $sName . " does not exist";
return false;
}
/**
* Get the primary key for a document type field
*
* @param $sName Document type field name to get primary key for
*
* @return ID if document type field exists, false otherwise and sets $default->errorMessage
*/
function & getDocumentTypeFieldID($sName) {
global $lang_err_document_type_field_does_not_exist, $default;
$sName = addslashes($sName);
if ($this->documentTypeFieldExists($sName)) {
$sql = new Owl_DB();
$sql->query("SELECT id FROM " . $default->owl_fields_table . " WHERE name = '" . $sName . "'");
$sql->next_record();
return $sql->f("id");
}
$default->errorMessage = $lang_err_document_type_field_does_not_exist . $sName;
return false;
}
/**
* Checks whether a document type field exists
*
* @param $sName Document type field name to check
*
* @return true if document type field exists, false otherwise
*/
function documentTypeFieldExists($sName) {
global $default;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_fields_table . " WHERE name = '" . $sName . "'");
return $sql->next_record();
}
}
?>