-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkp_frontpage.module
376 lines (316 loc) · 10.6 KB
/
kp_frontpage.module
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
372
373
374
375
376
<?php
/**
* @file
* The main code for KP Frontpage.
*/
// Field bundle and info implementation of this module.
module_load_include('inc', 'kp_frontpage', 'includes/TripalFields/kp_frontpage.fields');
/**
* Implements hook_menu().
*/
function kp_frontpage_menu() {
$items = array();
// KP overview and help pages.
// menu key overview and help correspond to context menu
// in the frontend.
// @see templates/page--front.tpl.php
// Each page has template file.
// @see hook_theme() below.
$items['overview'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('kp_frontpage_pages'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['help'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('kp_frontpage_pages'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
// Update help topics in KP knowledgebase page.
$items['admin/update/help'] = array(
'title' => t('KnowPulse Help Page'),
'page callback' => 'drupal_get_form',
'page arguments' => array('kp_frontpage_pages'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_form.
* Page callback;
*/
function kp_frontpage_pages($form, &$form_state) {
$m = arg();
// This only applies to admin/update/help menu item above.
if ($m[0] == 'admin' && $m[1] == 'update' && $m[2] == 'help') {
$form['description'] = array(
'#type' => 'item',
'#description' => t('Upload TSV file to update help topics in KP_frontpage Help page.')
);
$rows = array(
array('<strong>title</strong>', 'The title of the help topic (Text)', 'required'),
array('<strong>new</strong>', 'Mark help topic as new item (T or F)', 'optional - set to F'),
array('<strong>content_link</strong>', 'Link to help topic page (include http://)', 'required'),
array('<strong>update_link</strong>', 'Link to help topic update page (include http://)', 'optional')
);
$head = array('Column Header', 'Content', 'Required/Optional');
$table = theme('table', array('header' => $head, 'rows' => $rows));
// Table to outline expected column headers.
$form['table_headers'] = array(
'#markup' => $table,
);
// File field - browse file.
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload Help Topics TSV file'),
'#size' => 48,
);
// Submit upload csv file.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
);
}
$topics = kp_frontpage_get_help_topics();
if ($topics) {
$rows = $topics['values'];
$head = $topics['headers'];
$table = theme('table', array('header' => $head, 'rows' => $rows));
$form['table_topics'] = array(
'#markup' => $table,
);
}
return $form;
}
/**
* Implements hook_form_submit - upload csv file.
*/
function kp_frontpage_pages_submit($form, $form_state) {
// Allow only csv file.
$validators = array('file_validate_extensions' => array('tsv'));
$file = file_save_upload('file', $validators, 'public://');
if ($file) {
// Rename file.
if ($file->filename != 'kp_help_topics.tsv') {
file_move($file, 'public://kp_help/kp_help_topics.tsv', FILE_EXISTS_REPLACE);
// Make this tsv file permanent.
// Update new location.
$file->uri = 'public://kp_help/kp_help_topics.tsv';
// File is permanent.
$file->status = FILE_STATUS_PERMANENT;
// Rename file to this, matches callback that reads the file.
$file->filename = 'kp_help_topics.tsv';
// Instruct Drupal kp_frontpage uses this file.
// Save.
file_save($file);
file_usage_add($file, 'kp_frontpage', 'file', 1);
drupal_set_message('File uploaded successufully', 'status');
}
}
}
/**
* Get all help topics available in the csv file.
*/
function kp_frontpage_get_help_topics() {
// Source file.
$file = drupal_realpath('public://kp_help/kp_help_topics.tsv');
$handle = fopen($file, 'r');
$help = array();
if ($handle) {
$i = 0;
while (!feof($handle)) {
$row = fgets($handle);
// Line broken down to individual values.
$values = str_getcsv($row, "\t");
if (strlen($row) > 2) {
if ($i == 0) {
// Save headers.
$help['headers'] = $values;
}
else {
$help['values'][] = $values;
}
}
$i++;
}
}
return (empty($help)) ? '' : $help;
}
/**
* Implements hook_preprocess_page().
*/
function kp_frontpage_preprocess_page(&$vars) {
if (isset($vars['page'])) {
$args = arg();
$page = $args[0];
if ($page == 'overview' || $page == 'help') {
// Template variables.
$path = drupal_get_path('module', 'kp_frontpage');
// Image directory. images/kp-page/.
$vars['path_images'] = $GLOBALS['base_url'] . '/' . $path . '/images/kp-page/';
// Host address.
$vars['path_host'] = $GLOBALS['base_url'] . '/';
// Used to indicate which content to load in the template.
$vars['page_content'] = '';
// JS
// This is a reset code to ensure that page is a blank and white canvas,
// at the foot of the page, after DOM has been rendered.
$is_logged_in = user_is_logged_in();
drupal_add_js(array('kp_frontpage' => array('is_logged_in' => $is_logged_in)), 'setting');
drupal_add_js ($path . '/js/kp_page_reset.js', array('scope' => 'footer'));
// JS
// Overall script.
drupal_add_js ($path . '/js/kp_page.js', array('scope' => 'footer'));
if ($page == 'overview') {
// When it is overview page.
// JS
// Particle JS - paticles ascending effect.
drupal_add_js ($path . '/js/particles-js.min.js');
drupal_add_js ($path . '/js/particles-js.js', array('scope' => 'footer'));
// CSS
drupal_add_css($path . '/css/overview.css', array('group' => CSS_THEME, 'weight' => -10));
// Content.
$vars['page_content'] = 'overview';
}
else {
// When it is help page.
// JS
// CSS
drupal_add_css($path . '/css/help.css', array('group' => CSS_THEME, 'weight' => -10));
// Help topics.
$vars['help_topics'] = kp_frontpage_get_help_topics();
// Content.
$vars['page_content'] = 'help';
}
// Suggest theme - use kppage template.
$suggests = &$vars['theme_hook_suggestions'];
// Template file, template name.
$type = 'page__kppage';
unset($args[0]);
$suggests = array_merge($suggests, array($type), theme_get_suggestions($args, $type));
}
}
}
/**
* Implements hook_preprocess_page().
*/
function kptheme_preprocess_page(&$vars, $hook) {
// Embed style, JS file, prepare preprocess vars and site stats,
// only when it is the frontpage.
if (drupal_is_front_page()) {
$vars['path_host'] = $GLOBALS['base_url'];
$path = drupal_get_path('module', 'kp_frontpage');
drupal_add_css($path . '/kp_frontpage.css');
drupal_add_js ($path . '/js/kp_frontpage.js', array('scope' => 'footer'));
$vars['path_images'] = $vars['path_host'] . '/' . $path . '/images/';
// Stats about data.
$sql = "SELECT * FROM {kp_frontpage_stats}";
$result = chado_query($sql);
$arr_stats_bar = array();
foreach($result as $stat) {
// Bar chart.
$arr_stats_bar[$stat->type] = array(
'short_value' => kp_frontpage_shortval($stat->count),
'long_value' => number_format($stat->count),
);
}
$vars['data_stats'] = $arr_stats_bar;
}
}
/**
* Implements hook_contextual_links_view_alter().
* Credits: https://www.drupal.org/forum/support/post-installation/2011-01-26/turn-off-contextual-links-in-d7.
*/
function kp_frontpage_contextual_links_view_alter(&$element, $items) {
// Disable contextual links on views as it messes up markup.
// For example: the link uses ul in the news section which uses ul as well
// and trying to find the actual number of li in news includes the li in contextual links.
if (isset($element['#element']['#views_contextual_links_info'])) {
unset($element['#links']);
}
}
/**
* Implements hook_theme_registry_alter().
*/
function kp_frontpage_theme_registry_alter(&$theme_registry) {
// Defined path to the current module.
$module_path = drupal_get_path('module', 'kp_frontpage') . '/templates';
// Find all .tpl.php files in this module's folder recursively.
$template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path);
// Iterate through all found template file objects.
foreach ($template_file_objects as $key => $template_file_object) {
// If the template has not already been overridden by a theme.
if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
// Alter the theme path and template elements.
$theme_registry[$key]['theme path'] = $module_path;
$theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
$theme_registry[$key]['type'] = 'module';
}
}
}
/**
* Implementation of hook_views_api()
*/
function kp_frontpage_views_api() {
return array('api' => 3.0);
}
/**
* Implements hook_node_info().
*/
function kp_frontpage_node_info() {
$content_type = array(
// Machine name.
'kp_frontpage_cms' => array(
// Human-readable name.
'name' => t('KP Frontpage Content'),
// Description.
'description' => t('Manage KP frontpage content.'),
// Title.
'has_title' => TRUE,
'title_label' => t('Content Title:'),
// Base name for implementation. Usually the module name.
'base' => 'kp_frontpage',
// Disable option to let administrators change the machine
// name of this content type.
'locked' => TRUE,
)
);
return $content_type;
}
/**
* Implements hook_form().
*/
function kp_frontpage_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Shortens a number in K/M/B.
*
* @param $value
* An integer containing the number to be formatted.
*
* @return string
* Shortened result.
*/
function kp_frontpage_shortval($value) {
if ($value < 1000) {
// Hundred and less.
$value = $value;
}
else if ($value < 1000000) {
// Thousand.
$value = round($value / 1000, 0) . 'K';
}
else if ($value < 1000000000) {
// Million.
$value = round($value / 1000000, 0) . 'M';
}
else {
// Billion.
$value = round($value / 1000000000, 0) . 'B';
}
return $value;
}