Issue #1123282 by chris.wray: Sorting and Paging links for document search are hooked...
[project/document.git] / document.inc
1 <?php
2
3 define('DOCUMENT_STATUS_UNPUBLISHED', 0);
4 define('DOCUMENT_STATUS_PUBLISHED', 1);
5 define('DOCUMENT_INTERNAL', 0);
6 define('DOCUMENT_EXTERNAL', 1);
7 define('DOCUMENT_SEARCH_AUTHOR', 0);
8 define('DOCUMENT_SEARCH_KEYWORDS', 1);
9 define('DOCUMENT_SEARCH_AUTHOR_KEYWORDS', 2);
10 define('DOCUMENT_SEARCH_NONE', 3);
11
12 function document_get_types($idAsKey = TRUE, $reset = FALSE) {
13 static $types;
14 if (!isset($types) || $reset) {
15 if (!$reset && ($cache = cache_get('document_types')) && !empty($cache->data)) {
16 $types = unserialize($cache->data);
17 }
18 else {
19 $vocid = document_get_vocid();
20 $result = db_query('SELECT * FROM {term_data} WHERE vid = %d', $vocid);
21 $types = array();
22 while ($type = db_fetch_object($result)) {
23 $types[$type->tid] = $type->name;
24 }
25 uasort($types, "_document_type_comparer");
26
27 cache_set('document_types', serialize($types));
28 }
29 }
30
31 $arr = array();
32 foreach ($types as $key => $value) {
33 if ($idAsKey)
34 $arr[$key] = $value;
35 else
36 $arr[$value] = $value;
37 }
38
39 return $arr;
40 }
41
42 function document_get_vocid() {
43 $vocid = variable_get('document_vocabulary', '');
44 if (empty($vocid)) {
45 // Check to see if document vocabulary exists
46 $vocid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='document'"));
47 if ($vocid) {
48 // We found a vocabulary, so make sure it is associated with our content.
49 $vocabulary = (array) taxonomy_vocabulary_load($vocid);
50 $vocabulary['nodes'] = array('document' => 1);
51 $status = taxonomy_save_vocabulary($vocabulary);
52 }
53 else {
54 // Didn't find one, so create vocabulary from scratch.
55 $vocabulary = array(
56 'name' => 'Document types',
57 'multiple' => 0,
58 'required' => 1,
59 'hierarchy' => 2,
60 'relations' => 0,
61 'module' => 'document',
62 'nodes' => array(
63 'document' => 1));
64 $status = taxonomy_save_vocabulary($vocabulary);
65 $vocid = $vocabulary['vid'];
66 }
67 variable_set('document_vocabulary', $vocid);
68 }
69 return $vocid;
70 }
71
72 function document_get_vocab() {
73 $vocid = variable_get('document_vocabulary', '');
74 $vocab = NULL;
75 if (empty($vocid)) {
76 $vocid = document_get_vocid();
77 $vocab = taxonomy_vocabulary_load($vocid);
78 }
79 else {
80 $vocab = taxonomy_vocabulary_load($vocid);
81 if (!$vocab) {
82 $vocid = document_get_vocid();
83 $vocab = taxonomy_vocabulary_load($vocid);
84 }
85 }
86 return ($vocab);
87 }
88
89 function document_get_years() {
90 $years = array();
91 for ($i = 1950; $i <= date('Y'); $i++) {
92 $years[] = $i;
93 }
94
95 return ($years);
96 }
97
98 function document_get_path() {
99 $path = variable_get('document_path', '');
100 $path = file_directory_path() . '/' . $path;
101
102 return ($path);
103 }
104
105 function document_register_status() {
106 drupal_add_js(array(
107 'document' => array(
108 'STATUS_UNPUBLISHED' => DOCUMENT_STATUS_UNPUBLISHED,
109 'STATUS_PUBLISHED' => DOCUMENT_STATUS_PUBLISHED)), 'setting');
110 }
111
112 function document_perform_search($searchFields, $searchText, $searchYear = NULL, $searchDocType = NULL) {
113 $sql = sprintf('SELECT * FROM {node} n INNER JOIN {document} d ON n.vid = d.vid WHERE n.status = %d', DOCUMENT_STATUS_PUBLISHED);
114 $where = '';
115
116 switch ($searchFields) {
117 case 0:
118 $where .= sprintf(" AND author LIKE '%%%1\$s%%' ", db_escape_string($searchText));
119 break;
120 case 1:
121 $where .= sprintf(" AND keywords LIKE '%%%1\$s%%' ", db_escape_string($searchText));
122 break;
123 case 2:
124 $where .= sprintf(" AND author LIKE '%%%1\$s%%' OR keywords LIKE '%%%1\$s%%' ", db_escape_string($searchText));
125 break;
126 case 3:
127 break;
128 default:
129 die('Invalid Input');
130 }
131
132 if (!empty($searchYear)) {
133 $where .= sprintf(" AND publish_year = %1\$s ", db_escape_string($searchYear));
134 }
135
136 if (!empty($searchDocType) > 0) {
137 $where .= sprintf(" AND d.type LIKE '%%%1\$s%%' ", db_escape_string($searchDocType));
138 }
139
140 $sql .= $where;
141
142 //db_query adds parameter substitution, so things like %d, %s are processed as arguments to the db_query call.
143 //A side effect is that the use of % in a query needs to be double so for example "%blah%" would become "%%blah%%"
144 $sql = str_replace('%', '%%', $sql);
145
146 return (document_search_table($sql));
147 }
148
149 function document_search_table($sql) {
150 //http://www.rahulsingla.com/projects/drupal-document-module#comment-194
151 //During Ajax search, the path is document/search, and hence clicking table sort headers after a search
152 //take you to a blank page with only the document table.
153 //So, need to change the query to the document page.
154 $q = $_GET['q'];
155 $_GET['q'] = 'document';
156
157 $headers = array(
158 array(
159 'data' => t('Type'),
160 'field' => 'd.type',
161 'class' => 'search-result-header col-type'),
162 array(
163 'data' => t('Title'),
164 'field' => 'title',
165 'sort' => 'asc',
166 'class' => 'search-result-header col-title'),
167 array(
168 'data' => t('Author'),
169 'field' => 'author',
170 'class' => 'search-result-header col-author'),
171 array(
172 'data' => t('Year of Publication'),
173 'field' => 'publish_year',
174 'class' => 'search-result-header col-publish-year'),
175 array(
176 'data' => t('Keywords'),
177 'class' => 'search-result-header col-keywords'),
178 array(
179 'data' => '',
180 'class' => 'search-result-header col-download'));
181
182 //add the order by clause
183 $sql .= tablesort_sql($headers);
184 $results = pager_query($sql, 10);
185
186 $moderate = user_access('moderate document');
187 if ($moderate) {
188 array_unshift($headers, '');
189 }
190
191 $imgUnpublish = theme_image(document_image_url('spacer.gif'), t('Unpublish'), t('Unpublish'), array(
192 'onclick' => 'doc.changeDocStatus(this, %1$d, \'icon-unpublish\', false);',
193 'class' => 'icon-unpublish',
194 'width' => 16,
195 'height' => 16), FALSE);
196 $imgDelete = theme_image(document_image_url('spacer.gif'), t('Delete'), t('Delete'), array(
197 'onclick' => 'doc.deleteDoc(this, %1$d, \'icon-delete\');',
198 'class' => 'icon-delete',
199 'width' => 16,
200 'height' => 16), FALSE);
201
202 $rows = array();
203 while ($doc = db_fetch_object($results)) {
204 $row = array(
205 $doc->type,
206 l($doc->title, 'node/' . $doc->nid),
207 $doc->author,
208 $doc->publish_year,
209 $doc->keywords,
210 l(t('Download'), $doc->url, array(
211 'attributes' => array(
212 'target' => '_blank'))));
213
214 if ($moderate) {
215 array_unshift($row, sprintf($imgUnpublish . '&nbsp;&nbsp;&nbsp;' . $imgDelete, $doc->nid));
216 }
217 $rows[] = $row;
218 }
219 $table = theme('table', $headers, $rows);
220 $table .= theme('pager', array(), 10, 0);
221
222 //Restore the original query.
223 $_GET['q'] = $q;
224
225 return ($table);
226 }
227
228 function document_image_url($name) {
229 $url = drupal_get_path('module', 'document') . '/images/' . $name;
230 return ($url);
231 }
232
233 function document_mail_text($key, $language = NULL, $variables = array()) {
234 $langcode = isset($language) ? $language->language : NULL;
235
236 if ($admin_setting = variable_get('document_' . $key, FALSE)) {
237 // An admin setting overrides the default string.
238 return strtr($admin_setting, $variables);
239 }
240 else {
241 // No override, return default string.
242 switch ($key) {
243 case 'publish_subject':
244 return t('Your Document got published at !site', $variables, $langcode);
245 case 'publish_body':
246 return t("!username,\n\nThank you for uploading a document at !site. Your document, \"!node_title\" has been published by the moderators. You can now view your document at: !node_title_link.\n\n\n-- !site team", $variables, $langcode);
247 }
248 }
249 }
250
251 function document_mail_tokens($account, $node, $language) {
252 global $base_url;
253 $node_link = 'node/' . $node->nid;
254 $tokens = array(
255 '!username' => $account->name,
256 '!doc_link' => l($node->document_url, $node->document_url),
257 '!node_title' => $node->title,
258 '!node_title_link' => l($node->title, $node_link),
259 '!node_link' => l($node_link, $node_link),
260 '!site' => variable_get('site_name', 'Drupal'),
261 '!uri' => $base_url,
262 '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
263 '!mailto' => $account->mail,
264 '!date' => format_date(time(), 'medium', '', NULL, $language->language));
265 return $tokens;
266 }
267
268 function _document_type_comparer($a, $b) {
269 return (strcasecmp($a['type'], $b['type']));
270 }