This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from ppound/content_model_viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_model_viewer.db.inc
162 lines (148 loc) · 5.33 KB
/
content_model_viewer.db.inc
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
<?php
// $Id$
/**
* @file
*
* A collection of funcitons specific to this module, that make interacting with the database convenient.
*/
/**
* Constants
*/
define("CONTENT_MODEL_VIEWER_SETTINGS_TABLE", 'content_model_viewer_models');
define("CONTENT_MODEL_VIEWER_DATASTREAM_DSID_RULES_TABLE", 'content_model_viewer_datastream_dsid_rules');
define("CONTENT_MODEL_VIEWER_DATASTREAM_MIMETYPE_RULES_TABLE", 'content_model_viewer_datastream_mimetype_rules');
define("CONTENT_MODEL_VIEWER_DATASTREAM_RELS_VIEW_DERIVED_RULES_TABLE", 'content_model_viewer_datastream_rels_view_derived_rules');
define("CONTENT_MODEL_VIEWER_DATASTREAM_DSID_VIEW_DERIVED_RULES_TABLE", 'content_model_viewer_datastream_dsid_view_derived_rules');
define("CONTENT_MODEL_VIEWER_DEFAULT_SETTINGS_MODEL_ID", 1);
define("CONTENT_MODEL_VIEWER_DATASTREAM_DSID_RULE_TYPE_EXACT", 0);
define("CONTENT_MODEL_VIEWER_DATASTREAM_DSID_RULE_TYPE_REGEX", 1);
/**
*
* @param string $pid
* A Fedora Object PID.
* @return boolean
* TRUE if there are settings defined for the given Content Model, FALSE otherwise.
*/
function content_model_viewer_model_has_settings($pid) {
$query = db_query('SELECT count(pid) FROM {content_model_viewer_models} WHERE id > 1 AND pid = "%s"', $pid);
return (db_result($query) > 0) ? TRUE : FALSE;
}
/**
*
* @param string $pid
* A Fedora Object PID.
* @return boolean
* TRUE if there are settings defined for the given Content Model, FALSE otherwise.
*/
function content_model_viewer_model_has_datastream_mimetype_rule($model_id, $mime) {
$query = db_query('SELECT count(id) FROM {%s} WHERE model_id = "%d" AND mime = "%s"', CONTENT_MODEL_VIEWER_DATASTREAM_MIMETYPE_RULES_TABLE, $model_id, $mime);
return (db_result($query) > 0) ? TRUE : FALSE;
}
/**
*
*/
function content_model_viewer_model_get_datastream_mimetype_rule($model_id, $mime) {
$query = db_query('SELECT view, download FROM {%s} WHERE model_id = "%d" AND mime = "%s"', CONTENT_MODEL_VIEWER_DATASTREAM_MIMETYPE_RULES_TABLE, $model_id, $mime);
return db_fetch_array($query);
}
/**
*
*/
function content_model_viewer_model_has_datastream_dsid_rules($model_id) {
$query = db_query('SELECT count(id) FROM {%s} WHERE model_id = "%d"', CONTENT_MODEL_VIEWER_DATASTREAM_DSID_RULES_TABLE, $model_id);
return (db_result($query) > 0) ? TRUE : FALSE;
}
/**
*
*/
function content_model_viewer_model_get_datastream_dsid_rules($model_id) {
$rules = array();
$query = db_query('SELECT * FROM {%s} WHERE model_id = "%d"', CONTENT_MODEL_VIEWER_DATASTREAM_DSID_RULES_TABLE, $model_id);
while ($row = db_fetch_array($query)) {
$rules[] = $row;
}
return $rules;
}
/**
* Gets the custom settings for the given $pid.
*
* If $model is not provided get the default settings.
*
* @param string $model_id
* The model the settings are associated with.
*
* @return array
* The rules associated with this fedora object.
*/
function content_model_viewer_get_settings($model_id = 1) {
$settings = array();
$query = db_query('SELECT * FROM {content_model_viewer_datastream_rules} WHERE model_id = "%d"', $model_id);
while ($data = db_fetch_object($query)) {
$item = array();
foreach ($data as $key => $value) {
$item[$key] = $value;
}
$settings[] = $item;
}
return $settings;
}
/**
* Orders the settings for the given datastream, based on the MIME Type and DSID of the datastream. Also filter out
* settings that don't apply to this datastream.
*
* If MIME type restricts, and DSID passes.
*
* @param array $settings
* @param string $mime
* @param string $dsid
*
* @return array
*/
function content_model_viewer_order_settings(array $settings) {
}
/**
* Gets the model id that is used to identitfy the custom settings for the given content model, identified by $pid.
*
* @param string $pid
* A fedora object pid.
*
* @return int
* Returns the model_id
*/
function content_model_viewer_get_model_id($pid) {
$model_id = db_result(db_query('SELECT id FROM {%s} WHERE id > 1 AND pid = "%s"', CONTENT_MODEL_VIEWER_SETTINGS_TABLE, $pid));
if (!$model_id) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
$content_model = ContentModel::loadFromObject($pid);
$model_id = $content_model ?
db_result(db_query('SELECT id FROM {%s} WHERE id > 1 AND pid = "%s"', CONTENT_MODEL_VIEWER_SETTINGS_TABLE, $content_model->pid)) :
FALSE;
}
return $model_id ? $model_id : CONTENT_MODEL_VIEWER_DEFAULT_SETTINGS_MODEL_ID;
}
/**
* Checks to see if the user can view the given datastream of the given Fedora Object.
*
* @param string $pid
* The Fedora Objects pid.
* @param string $dsid
* The Datastream ID.
*
* @return boolean
* TRUE if the end user is permitted to view the datastream, FALSE otherwise.
*/
function content_model_viewer_is_viewing_permitted($pid, $dsid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'ContentModel');
$item = new Fedora_Item($pid);
$content_model = ContentModel::loadFromObject($pid);
$pid = $content_model->pid;
$model_id = content_model_viewer_model_has_settings($pid) ? content_model_viewer_get_model_id($pid) : 1;
$settings = content_model_viewer_get_settings($model_id);
$permitted = TRUE; // Default TRUE.
foreach ($settings as $setting) {
if (($setting['mime'])) {
}
}
return $permitted;
}