This repository was archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripture.module
More file actions
371 lines (295 loc) · 10.4 KB
/
scripture.module
File metadata and controls
371 lines (295 loc) · 10.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
module_load_include('inc','scripture','scripture.field');
module_load_include('inc','scripture','scripture.db');
module_load_include('inc','scripture','scripture.element');
/**
* General function used during the installation of various translations
*/
function scripture_readandmysqlexecute($myfile) {
$sql = file($myfile, true);
$templine = '';
foreach ($sql as $line) {
if (substr($line, 0, 2) == '--' || $line == '')
continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';') {
db_query($templine);
$templine = '';
}
}
}
/**
* Implements hook_permission().
*/
function scripture_permission() {
$permissions = array();
$permissions['scripture_admin'] = array(
'title' => t('Scripture admin'),
'description' => t('Administer the Scripture module'),
'restrict access' => TRUE,
);
$permissions['scripture_node_listing'] = array(
'title' => t('View scripture node listings'),
'description' => t('Access pages like scripture/* which lists all nodes associated with the specified scripture ranges.'),
);
return $permissions;
}
/**
* Implements hook_menu().
* https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7
*/
function scripture_menu() {
$items = array();
$items['admin/structure/scripture'] = array(
'title' => 'Scripture',
'type' => MENU_NORMAL_ITEM,
'description' => 'Manage the Scripture module',
'page callback' => 'scripture_callback_status',
'access callback' => 'user_access',
'access arguments' => array('scripture_admin'),
'file' => 'scripture.admin.inc',
);
$items['admin/structure/scripture/status'] = array(
'title' => 'Status',
'description' => 'View status and statistics',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 10,
'page callback' => 'scripture_callback_status',
'access callback' => 'user_access',
'access arguments' => array('scripture_admin'),
'file' => 'scripture.admin.inc',
);
$items['admin/structure/scripture/translations'] = array(
'title' => 'Translations',
'description' => 'Manage installed translations',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
'page callback' => 'drupal_get_form',
'page arguments' => array('scripture_callback_translations'),
'access callback' => 'user_access',
'access arguments' => array('scripture_admin'),
'file' => 'scripture.admin.inc',
);
$items['admin/structure/scripture/queries'] = array(
'title' => 'Queries',
'description' => 'Configure query settings',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
'page callback' => 'drupal_get_form',
'page arguments' => array('scripture_callback_queries'),
'access callback' => 'user_access',
'access arguments' => array('scripture_admin'),
'file' => 'scripture.admin.inc',
);
$items['scripture/%'] = array(
'title' => 'Content by scripture',
'page callback' => 'scripture_callback_node_listing',
'page arguments' => array(1),
'access callback' => 'user_access',
'access arguments' => array('scripture_node_listing'),
'file' => 'scripture.pages.inc',
);
return $items;
}
/**
* Implements hook_element_info().
* https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_element_info/7
* Declare a FORM API element for the verse picker
*/
function scripture_element_info() {
return array(
'scripture_verse_picker' => array(
'#input' => TRUE,
'#process' => array('scripture_verse_picker_process'),
'#element_validate' => array('scripture_verse_picker_validate'),
'#theme' => array('scripture_verse_picker'),
'#theme_wrappers' => array('form_element'),
'#tree' => TRUE,
),
);
}
/**
* Implements hook_theme($existing, $type, $theme, $path).
* https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7
* Registers the module's theme implementations
*/
function scripture_theme($existing, $type, $theme, $path) {
return array(
'scripture_verse_picker' => array(
'render element' => 'element',
),
'scripture_inline_wrapper' => array(
'render element' => 'element',
),
);
}
/**
* Update preview for verse(s) via AJAX
*/
function scripture_ajax_preview_verse($form, $form_state) {
return 'MOO';
}
/**
* Load pieces of scripture from database
*/
function scripture_prepare(&$values, $display) {
if (SCRIPTURE_SHOW_TEXT_TRUNC == $display['show_text'] || $display['show_ref']) {
// load first and last verse text in range
$values['from_verse'] = scripture_get_verse($values['from_vid'], $values['translation']);
$values['to_verse'] = scripture_get_verse($values['to_vid'], $values['translation']);
}
if (SCRIPTURE_SHOW_TEXT_ALL == $display['show_text']) {
// load entire verse range
$values['all_verses'] = scripture_get_verses_between($values['from_vid'], $values['to_vid'], $values['translation']);
}
}
/**
* View pieces of scripture from database
*/
function scripture_view($values, $display) {
$output = array();
if (empty($values['from_verse'])) {
drupal_set_message("Cannot view verses, as they were not properly loaded.", "error", FALSE);
} else {
if ($display['show_ref']) {
$output['ref'] = array(
'#prefix' => '<cite>',
'#markup' => scripture_str_verseref($values['from_verse'], $values['to_verse'], NULL, $values['translation']),
'#suffix' => '</cite>'
);
}
switch ($display['show_text']) {
case SCRIPTURE_SHOW_TEXT_TRUNC:
$markup = scripture_shorten($values['from_verse']['versetext'], $values['to_verse']['versetext']);
$output['text'] = array(
'#prefix' => '<q>',
'#markup' => $markup,
'#suffix' => '</q>'
);
break;
case SCRIPTURE_SHOW_TEXT_ALL:
// pull all of the verses into one long string
$alltext = "";
foreach ($values['all_verses'] as $v) {
$alltext .= $v->versetext . " ";
}
$output['text'] = array(
'#prefix' => '<blockquote>',
'#markup' => substr($alltext, 0, - 1),
'#suffix' => '</blockquote>'
);
break;
}
}
return $output;
}
/**
* Generate a human-readable verse range
*/
function scripture_str_verseref($from_verse, $to_verse, $bookname = NULL, $translation_abbr = NULL) {
if (empty($bookname)) {
$book = scripture_get_book($from_verse);
$bookname = $book['bookname'];
}
if ($from_verse['vid'] == $to_verse['vid']) {
$str = "{$bookname} {$from_verse['chapternum']}:{$from_verse['versenum']}";
} else {
$str = "{$bookname} {$from_verse['chapternum']}:{$from_verse['versenum']} - {$to_verse['chapternum']}:{$to_verse['versenum']}";
}
if (!empty($translation_abbr)) {
$translation = scripture_get_translation($translation_abbr);
$str .= " (<abbr title='{$translation['name']}'>".strtoupper($translation['abbr'])."</abbr>)";
}
return $str;
}
/**
* Takes the first few words from string $a and the last few words from string $b and concatenates them with an ellips
*/
function scripture_shorten($a, $b = "", $maxwords = 5) {
$a = explode(" ", $a, $maxwords + 1);
unset($a[$maxwords]);
$a = implode(" ", $a) . " …";
if (! empty($b)) {
$b = explode(" ", $b);
$b = array_slice($b, - $maxwords);
$b = " " . implode(" ", $b);
}
return $a . $b;
}
/**
* Convenience function for previewing verses
* TODO: get a better name for this function??
*/
function scripture_preview($values, $display = array()) {
$display += array(
'show_ref'=>TRUE,
'show_text'=>SCRIPTURE_SHOW_TEXT_ALL,
);
scripture_prepare($values,$display);
return scripture_view($values,$display);
}
/**
* Provide a measure of quality of match, given the verse range for a node and the verse range for a query
*/
function scripture_match_quality($field_start_vid, $field_end_vid, $query_start_vid, $query_end_vid) {
static $weight_q;
static $weight_e;
if (!isset($weight_q)) {
$weight_q = variable_get("scripture_match_quality_weight_query", 1);
$weight_e = variable_get("scripture_match_quality_weight_entity", 1);
}
// FIXME: using the verse ids is crude, because they are not gauranteed to be equally spaced. Although in general, they are.
$overlap_size = max(0, min($field_end_vid, $query_end_vid) - max($field_start_vid - $query_start_vid));
$query_match_quality = $overlap_size / ($query_end_vid - $query_start_vid);
$entity_match_quality = $overlap_size / ($field_end_vid - $field_start_vid);
return $weight_q * $query_match_quality + $weight_e * $entity_match_quality;
}
/**
* Decode a verse range represented as a string into a
* - starting vid
* - stopping vid
*/
function scripture_decode_range($str) {
$from_vid = NULL;
$to_vid = NULL;
$matches = array();
if (preg_match('/^(\d+)(-(\d+))?$/', $str, $matches)) {
// looks valid, i.e. either 1234 or 1234-5678
if (!empty($matches[1])) {
// starting verse is specified
$from_vid = $matches[1];
if (!empty($matches[3])) {
// ending verse is specified
$to_vid = $matches[3];
} else {
// use the starting verse for the ending verse
$to_vid = $from_vid;
}
} // else invalid starting verse
} // else it didn't match the regular expression
return array(
'from_vid' => $from_vid,
'to_vid' => $to_vid,
);
}
function scripture_vidrange2text($startvid, $endvid, $translation = NULL) {
# use the default translation if not explicitly defined
if (empty($translation)) $translation = variable_get('scripture_default_translation', NULL);
$sql = "SELECT chapternum, versenum, bookname
FROM lw_verses
INNER JOIN lw_books using (booknum, translation)
WHERE translation=:trans AND vid in (:vid1, :vid2)
ORDER BY vid";
$rst = db_query($sql, array(':trans' => $translation, ':vid1' => $startvid, ':vid2' => $endvid));
$een = $rst->fetchObject();
$twee = $rst->fetchObject();
$out = "{$een->bookname} {$een->chapternum}:{$een->versenum}";
if ($startvid != $endvid) {
if ($een->chapternum==$twee->chapternum){
$out .= "-{$twee->versenum}";
} else {
$out .= "-{$twee->chapternum}:{$twee->versenum}";
}
}
return $out;
}