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);
12 function document_get_types($idAsKey = TRUE
, $reset = FALSE
) {
14 if (!isset($types) || $reset) {
15 if (!$reset && ($cache = cache_get('document_types')) && !empty($cache->data
)) {
16 $types = unserialize($cache->data
);
19 $vocid = document_get_vocid();
20 $result = db_query('SELECT * FROM {term_data} WHERE vid = %d', $vocid);
22 while ($type = db_fetch_object($result)) {
23 $types[$type->tid
] = $type->name
;
25 uasort($types, "_document_type_comparer");
27 cache_set('document_types', serialize($types));
32 foreach ($types as
$key => $value) {
36 $arr[$value] = $value;
42 function document_get_vocid() {
43 $vocid = variable_get('document_vocabulary', '');
45 // Check to see if document vocabulary exists
46 $vocid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='document'"));
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);
54 // Didn't find one, so create vocabulary from scratch.
56 'name' => 'Document types',
61 'module' => 'document',
64 $status = taxonomy_save_vocabulary($vocabulary);
65 $vocid = $vocabulary['vid'];
67 variable_set('document_vocabulary', $vocid);
72 function document_get_vocab() {
73 $vocid = variable_get('document_vocabulary', '');
76 $vocid = document_get_vocid();
77 $vocab = taxonomy_vocabulary_load($vocid);
80 $vocab = taxonomy_vocabulary_load($vocid);
82 $vocid = document_get_vocid();
83 $vocab = taxonomy_vocabulary_load($vocid);
89 function document_get_years() {
91 for ($i = 1950; $i <= date('Y'); $i++) {
98 function document_get_path() {
99 $path = variable_get('document_path', '');
100 $path = file_directory_path() .
'/' .
$path;
105 function document_register_status() {
108 'STATUS_UNPUBLISHED' => DOCUMENT_STATUS_UNPUBLISHED
,
109 'STATUS_PUBLISHED' => DOCUMENT_STATUS_PUBLISHED
)), 'setting');
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
);
116 switch ($searchFields) {
118 $where .
= sprintf(" AND author LIKE '%%%1\$s%%' ", db_escape_string($searchText));
121 $where .
= sprintf(" AND keywords LIKE '%%%1\$s%%' ", db_escape_string($searchText));
124 $where .
= sprintf(" AND author LIKE '%%%1\$s%%' OR keywords LIKE '%%%1\$s%%' ", db_escape_string($searchText));
129 die('Invalid Input');
132 if (!empty($searchYear)) {
133 $where .
= sprintf(" AND publish_year = %1\$s ", db_escape_string($searchYear));
136 if (!empty($searchDocType) > 0) {
137 $where .
= sprintf(" AND d.type LIKE '%%%1\$s%%' ", db_escape_string($searchDocType));
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);
146 return (document_search_table($sql));
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.
155 $_GET['q'] = 'document';
161 'class' => 'search-result-header col-type'),
163 'data' => t('Title'),
166 'class' => 'search-result-header col-title'),
168 'data' => t('Author'),
170 'class' => 'search-result-header col-author'),
172 'data' => t('Year of Publication'),
173 'field' => 'publish_year',
174 'class' => 'search-result-header col-publish-year'),
176 'data' => t('Keywords'),
177 'class' => 'search-result-header col-keywords'),
180 'class' => 'search-result-header col-download'));
182 //add the order by clause
183 $sql .
= tablesort_sql($headers);
184 $results = pager_query($sql, 10);
186 $moderate = user_access('moderate document');
188 array_unshift($headers, '');
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',
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',
200 'height' => 16), FALSE
);
203 while ($doc = db_fetch_object($results)) {
206 l($doc->title
, 'node/' .
$doc->nid
),
210 l(t('Download'), $doc->url
, array(
211 'attributes' => array(
212 'target' => '_blank'))));
215 array_unshift($row, sprintf($imgUnpublish .
' ' .
$imgDelete, $doc->nid
));
219 $table = theme('table', $headers, $rows);
220 $table .
= theme('pager', array(), 10, 0);
222 //Restore the original query.
228 function document_image_url($name) {
229 $url = drupal_get_path('module', 'document') .
'/images/' .
$name;
233 function document_mail_text($key, $language = NULL
, $variables = array()) {
234 $langcode = isset($language) ?
$language->language
: NULL
;
236 if ($admin_setting = variable_get('document_' .
$key, FALSE
)) {
237 // An admin setting overrides the default string.
238 return strtr($admin_setting, $variables);
241 // No override, return default string.
243 case
'publish_subject':
244 return t('Your Document got published at !site', $variables, $langcode);
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);
251 function document_mail_tokens($account, $node, $language) {
253 $node_link = 'node/' .
$node->nid
;
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'),
262 '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
263 '!mailto' => $account->mail,
264 '!date' => format_date(time(), 'medium', '', NULL
, $language->language
));
268 function _document_type_comparer($a, $b) {
269 return (strcasecmp($a['type'], $b['type']));