This repository has been archived by the owner on Sep 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kp_nodes.module
646 lines (547 loc) · 21.2 KB
/
kp_nodes.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
require_once 'theme/template.php';
/**
* Implementation of hook_init()
*/
function kp_nodes_init() {
if (drupal_is_front_page()) {
libraries_load('tether');
libraries_load('shepherd');
drupal_add_css(drupal_get_path('theme','kptheme') . '/css/shepherd_tour.css');
drupal_add_js(drupal_get_path('theme','kptheme') . '/js/shepherdScrollingButtons.js');
drupal_add_js(drupal_get_path('module','kp_nodes') . '/theme/js/siteTour-FrontPage.js');
}
}
/**
* Implements hook_menu().
*/
function kp_nodes_menu() {
$items = array();
/**
$items['contact'] = array(
'title' => 'Contact us',
'page callback' => 'drupal_get_form',
'page arguments' => array('kp_contact_form'),
'access arguments' => array('access content'),
'file' => 'includes/contact_form.inc',
);
*/
// Export germplasm list to csv file for download
// in Projects node.
$items['project/project_germplasm/%'] = array(
'title' => 'Germplasm List',
'page callback' => 'kp_project_germplasm_list',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function kp_nodes_theme($existing, $type, $theme, $path) {
$items = array(
'node__uofs_research_area' => array(
'template' => 'node--generic',
'render element' => 'node',
'base hook' => 'node',
'path' => "$path/theme"
),
'tripal_research_area_teaser' => array(
'template' => 'tripal-research-area-teaser',
'render element' => 'node',
'path' => "$path/theme"
),
'node__jbrowse_instance' => array(
'template' => 'node--jbrowse-instance',
'render element' => 'node',
'base hook' => 'node',
'path' => "$path/theme"
),
'kp_worldmap' => array(
'template' => 'worldmap.svg',
'path' => "$path/theme"
),
'kp_nodes_project_stocks' => array(
'template' => 'kp_nodes_project_stocks',
'render element' => 'node',
'path' => "$path/theme"
),
'kp_nodes_project_stocks_AGILE' => array(
'template' => 'kp_nodes_project_stocks',
'render element' => 'node',
'path' => "$path/theme"
)
);
return $items;
}
/**
* Implements hook_libraries_info().
*/
function kp_nodes_libraries_info() {
$libraries['shepherd'] = array(
'name' => 'Shepherd Site Tour',
'vendor url' => 'http://github.hubspot.com/shepherd/docs/welcome/',
'download url' => 'https://github.com/HubSpot/Shepherd',
'version' => '1.2.0',
'files' => array(
'js' => array('shepherd.min.js'),
'css' => array(
'shepherd-theme-arrows.css'
),
),
);
$libraries['tether'] = array(
'name' => 'Tether',
'vendor url' => 'http://github.hubspot.com/tether/',
'download url' => 'http://github.hubspot.com/tether/',
'version' => '1.1.1',
'files' => array(
'js' => array('tether.min.js'),
'css' => array(
'tether.min.css'
),
),
);
return $libraries;
}
/**
* Implements hook_node_view().
* Acts on all content types.
*/
function kp_nodes_node_view($node, $view_mode, $langcode) {
// Update the breadcrumbs to include link back to projects page.
$breadcrumbs[] = l('Home', '');
$breadcrumbs[] = l('Projects', '/research/projects');
drupal_set_breadcrumb($breadcrumbs);
switch ($node->type) {
case 'uofs_research_area':
if ($view_mode == 'full') {
$node->content['kp_toc'] = array(
'#markup' => "<div id=\"$node->type-tripal-toc-block\" class=\"tripal-toc-block\"></div>"
);
// create a base section including the body and thumbnail image field
$node->content['uofs_research_area_base'] = array(
'#type' => 'container',
'#tripal_toc_id' => 'base',
'#tripal_toc_title' => 'Overview',
'#attributes' => array(
'class' => array('uofs-research-area-base'),
),
'body' => $node->content['body'],
'field_page_thumnail' => $node->content['field_page_thumnail'],
'#weight' => -100,
);
unset($node->content['body'], $node->content['field_page_thumnail']);
} elseif($view_mode == 'teaser') {
$node->content['uofs_research_area_pane'] = array(
'#theme' => 'tripal_research_area_teaser',
'#node' => $node
);
//stop displaying same entry in the same page
unset($node->content['body'],
$node->content['field_page_thumnail'],
$node->content['links']);
}
break;
case 'chado_organism':
if ($view_mode == 'full') {
unset($node->content['tripal_organism_feature_browser']);
// Remove the field image with source since it will be rendered
// in the overview instead of as it's own pane.
unset($node->content['field_image_with_source']);
// Remove the nurtient facts if it's empty.
$field_collection = $node->content['field_nutrition'][0]['entity']['field_collection_item'];
$nutrient_facts = array_shift($field_collection);
if (sizeof($nutrient_facts['field_nutrition_data'][0]['#rows']) == 1) {
unset($node->content['field_nutrition']);
}
$node->content['tripal_organism_feature_counts']['#tripal_toc_title'] = 'Sequence & Variant Data';
$node->content['tripal_organism_feature_counts']['#weight'] = 2;
$node->content['tripal_organism_stocks']['#tripal_toc_title'] = 'Germplasm Data';
$node->content['tripal_organism_stocks']['#weight'] = 1;
}
break;
case 'chado_project':
// Determine if project is AGILE.
$use_theme = ($node->title == 'AGILE: Application of Genomic Innovation in the Lentil Economy')
? 'kp_nodes_project_stocks_AGILE' : 'kp_nodes_project_stocks';
// Germplasm Pane.
$node->content[$use_theme] = array(
'#theme' => $use_theme,
'#node' => $node,
'#tripal_toc_id' => 'germplasm',
'#tripal_toc_title' => 'Germplasm',
'#weight' => 4,
);
// Hide the date field since we will render it manually in the overview.
unset($node->content['field_date_month_year']);
// Change the name of the relationships pane.
$node->content['tripal_project_relationships']['#tripal_toc_title'] = 'Related Projects';
// Remove the Long description since it will be rendered manually in
// the overview.
unset($node->content['field_long_description']);
break;
case 'chado_contact':
// Get rid of Publications list since it's been added onto the overview.
unset($node->content['tripal_contact_publications']);
break;
case 'chado_pub':
unset(
$node->content['tripal_pub_properties'],
$node->content['tripal_pub_authors']
);
break;
}
}
/**
* After the node is built, we want to add instructions to each
* content section letting the administrator know which template
* they can customize
*
* @param $build
*/
function kp_nodes_node_view_alter(&$build) {
global $theme;
$node_types = array('uofs_research_area');
// if the $build['kp_toc'] element is not present, then this is not
// a full node view so we do not want to alter
if (!array_key_exists('kp_toc', $build)) {
return;
}
$cache = cache_get("theme_registry:$theme", 'cache');
$node = $build['#node'];
$toc = array();
$toc_html = '';
// if we are looking at a Tripal node template then we want to
// make some changes to each block of content so that we can associate
// a table of contents and add administrator and curator messages
if (in_array($node->type, $node_types)) {
// iterate through all the elements of the $build array and for those
// that are wanting to provide content for this node
$markup = array();
foreach ($build as $key => $value) {
// examine elements without a '#' prefix as these should be adding
// contents to the page. Skip the table of contents and links as those
// will be placed elsewhere
if (!preg_match('/^#/', $key) and $key != 'kp_toc' and $key != 'links') {
//-----------------------
// INITIALIZE THE CONTENT VARIABLES
//-----------------------
$toc_item_title = $key;
$toc_item_id = $key;
$toc_item_link = '';
// get the title for the table of contents. Tripal templates should
// have a '#tripal_toc_title' element in the build array
if (array_key_exists('#tripal_toc_title', $build[$key])) {
$toc_item_title = $build[$key]['#tripal_toc_title'];
}
// other elements in the $build array may just have a '#title' element,
if (array_key_exists('#title', $build[$key])) {
$toc_item_title = $build[$key]['#title'];
}
$toc_item_title = ucwords($toc_item_title);
if (array_key_exists('#tripal_toc_id', $build[$key])) {
$toc_item_id = $build[$key]['#tripal_toc_id'];
}
$toc_item_link = "<div class=\"tripal_toc_list_item\"><a id=\"$toc_item_id\" class=\"tripal_toc_list_item_link\" href=\"?block=$toc_item_id\">$toc_item_title</a></div>";
//-----------------------
// SET THE WEIGHTS FOR THE TOC ELEMENTS
//-----------------------
// set the weight of the TOC item and add it to our $toc array
// for building of the TOC below
$weight = 0;
if (array_key_exists('#weight', $build[$key])) {
$weight = $build[$key]['#weight'];
}
//-----------------------
// GET THE MARKUP FOR EACH ELEMENT
//-----------------------
$markup = '';
// find the markup. Some fields will have a '#markup' and others, such
// as CCK elements may have a set of '#markup' elements organized by
// numerical keys.
if (array_key_exists('#markup', $build[$key]) and trim($build[$key]['#markup'])) {
$markup = $build[$key]['#markup'];
}
// For backwards copmatibility we should support the '#value' element as well.
elseif (array_key_exists('#value', $build[$key]) and trim($build[$key]['#value'])) {
$markup = $build[$key]['#markup'];
}
// if we have no '#markup' field then this element has not yet
// been rendered. Let's render it and substitute that for markup
if (!$markup) {
$markup = trim(render($build[$key]));
$build[$key] = array(
'#markup' => $markup,
);
}
// if we still don't have markup then skip this one
if (!$markup) {
continue;
}
//-----------------------
// FIND THE TEMPLATE PATH
//-----------------------
// get the template path so we can put it in an admin message box
$path = '';
if (array_key_exists($key, $cache->data) and array_key_exists('path', $cache->data[$key])) {
$path = $cache->data[$key]['path'] . '/' . $key . '.tpl.php';
$path = tripal_set_message("Administrators, you can
customize the way the content above is presented. Tripal provides a template
file for each block of content. To customize, copy the template file to your
site's default theme, edit then " .
l('clear the Drupal cache', 'admin/config/development/performance', array('attributes' => array('target' => '_blank'))) . ".
Currently, the content above is provided by this template: <br><br>$path",
TRIPAL_INFO,
array('return_html' => 1)
);
}
//-----------------------
// CREATE THE DATA BLOCK
//-----------------------
// add a surrounding <div> box around the content
$updated_markup = "
<div id=\"$toc_item_id-tripal-data-block\" class=\"tripal-data-block\">
<div class=\"$toc_item_id-tripal-data-block-title tripal-data-block-title\">$toc_item_title</div>
$markup
$path
</div>
</div>
";
$build[$key]['#markup'] = $updated_markup;
$build[$key]['#weight'] = $weight;
//-----------------------
// Set the TOC Item
//-----------------------
$toc[$weight][$toc_item_title] = $toc_item_link;
}
}
}
//-----------------------
// BUILD THE TABLE OF CONTENTS LINKS
//-----------------------
// first sort the links numerically by their weight
ksort($toc, SORT_NUMERIC);
$toc_html = '';
foreach ($toc as $weight => $links) {
// for links in the same weight, sort them alphabetically
ksort($links);
foreach ($links as $toc_item_title => $toc_item_link) {
$toc_html .= $toc_item_link;
}
}
$build['kp_toc']['#markup'] = "<div id=\"$node->type-tripal-toc-block\" class=\"tripal-toc-block\">$toc_html</div>";
}
/**
* Function callback: create a list of germplasm and export to csv file for download.
*
* @param $project:
* A sting encoded using base64_encode() that comprises the keyword kp_nodes_project: followed by
* a project id number.
*/
function kp_project_germplasm_list($project = null) {
// FOR SECURITY REASON SINCE THIS IS PUBLICLY ACCESSIBLE, WE DEFAULT THE PROJECT TO AGILE,
// TO PREVENT USER FROM MODIFYING LINK AND DOWNLOAD OTHER GERMPLASM LIST OF OTHER PROJECT.
// THIS DOWNLOAD LIST FUNCTIONALITY IS EXCLUSIVE TO AGILE PROJECT ONLY AS REQUESTED.
// SHOULD THIS BE USED TO APPLY TO THE REST OF PROJECTS, DISCARD THE FOLLOWING LINE.
// USE base64_encode('kp_modes_project:project id number') as the href value to generate
// download link for other projects.
// THIS LINE WILL REFERENCE AGILE PROJECT.
$project = base64_encode('kp_nodes_project:63');
if (empty($project)) {
// See if the link contained a query string, if not stop processing.
die('Project not found.');
}
else {
// Decode the query string and extract the project_id.
$p = base64_decode($project);
$project_id = trim(str_replace('kp_nodes_project:', '', $p));
// Test if project id is valid, that is if it is less or 0, not a numeric value or
// the length is 4 or more charactres long.
if ($project_id <= 0 || !is_numeric($project_id) || strlen($project_id) >= 4) {
die('Project not found.');
}
else {
$project_id = (int)$project_id;
// Test if project_stock exists.
$sql = "SELECT relname FROM pg_class WHERE relname = 'project_stock'";
$exists = db_query($sql)
->rowCount();
// Project Id matches a project.
$project = chado_query("SELECT name FROM {project} WHERE project_id = :project_id LIMIT 1", array(
':project_id' => $project_id
));
// Ensure that project stock exists and that the project_id number matches a project
// in chado.projects table.
if ($exists && $project->rowCount() == 1) {
// Fetch the cvterm_id of type the origin of the organism.
$cvterm_id_origin = tripal_get_cvterm(array('name' => 'country_of_origin'));
$sql = "SELECT
s.name,
s.uniquename,
INITCAP(o.genus) || ' ' || LOWER(o.species) AS species,
cvt.name AS type,
STRING_AGG(CASE WHEN sprop.type_id = :origin THEN sprop.value END, '') AS origin
FROM
{stock} AS s
LEFT JOIN {project_stock} AS projs USING(stock_id)
LEFT JOIN {organism} AS o USING(organism_id)
LEFT JOIN {cvterm} AS cvt ON cvt.cvterm_id = s.type_id
LEFT JOIN {stockprop} AS sprop USING(stock_id)
WHERE
projs.project_id = :project_id
GROUP BY s.name, s.uniquename, o.genus, o.species, cvt.name
ORDER BY s.name ASC";
$args = array(
':origin' => $cvterm_id_origin->cvterm_id,
':project_id' => $project_id
);
$stocks = chado_query($sql, $args);
// See if in project_stocks table, a project has been assigned a set of stocks.
if ($stocks->rowCount() > 0) {
// Prepare csv.
// PHP write to output stream (like echo).
$csv = fopen('php://output', 'w');
$csv_filename = 'AGILE_germplasm_list_' . date('Ymd') . '.csv';
drupal_add_http_header('Content-Type', 'text/csv; utf-8');
drupal_add_http_header('Content-Disposition', 'attachment; filename = ' . $csv_filename);
// Warning:
fwrite($csv, "#WARNING: Some germplasm names may have converted into dates in Microsoft Excel.\n");
// Headers.
fputcsv($csv, array(t('NAME'), t('ORIGIN'), t('SPECIES'), t('TYPE')));
foreach($stocks as $s) {
fputcsv($csv, array($s->name, $s->origin, $s->species, $s->type));
}
fclose($csv);
}
else {
die('Project has no stocks assigned...');
}
}
else {
die('Project not found.');
}
}
}
}
/**
* Implements hook_alter().
* Match stock name to stock with AGL prefix in rawphenotypes module.
* Note: only when saving data in Stage 3.
*
* @param $data
* An array containing the stock name and project name.
*
* @return
* A string, stock name with AGL as required.
*
* NOTE: This applies to check stock names in stage 3: save spreadsheet.
*/
function kp_nodes_rawpheno_AGILE_stock_name_alter(&$stock_name, &$project_name) {
if ($project_name == 'AGILE: Application of Genomic Innovation in the Lentil Economy') {
$AGILE_stock_token = 'AGL';
// Only applies when project is AGILE.
if (strpos($stock_name, $AGILE_stock_token) === FALSE) {
// Stock name does not contain AGL.
// Alter stockname to contain AGL.
// Space required!
$stock_name .= ' AGL';
}
}
}
/**
* Implements hook_alter().
* Ignore columns in a spreadsheet file.
*
* @param &$header
* An array, a set of column headers from a spreadsheet file.
* @param &$project_name
* A string value, the name of the project.
*
* @return
* An array, a new set of column headers with the specified header(s) omitted.
* Array will be passed back to processing script ignoring data from indicated headers.
*
* @see
* modules/rawpheno/include/rawpheno.upload.form.inc.
*/
function kp_nodes_rawpheno_ignorecols_newcolumn_alter(&$header, &$project_name) {
// Referenced parameters in functions: rawpheno_upload_form_stage_review() in @see.
if ($project_name == 'AGILE: Application of Genomic Innovation in the Lentil Economy') {
$index = kp_nodes_rawpheno_ignorecols_match($header);
if (count($index) > 0) {
foreach($index as $i) {
unset($header[$i]);
}
}
}
}
/**
* Implements hook_alter().
* Ignore columns in a spreadsheet file.
*
* @param &$skip
* An array, a set of column headers from a spreadsheet file.
* @param &$project_name
* A string value, the name of the project.
*
* @return
* An array, containing the index numbers of headers in reference to the spreadsheet file.
* Array will be passed back to processing script ignoring data from indicated headers.
*
* @see
* modules/rawpheno/include/rawpheno.upload.excel.inc
* kp_nodes_rawpheno_ignorecols().
*/
function kp_nodes_rawpheno_ignorecols_valsave_alter(&$skip, &$project_name) {
// Referenced parameters in functions:
// rawpheno_validate_excel_file() and rawpheno_load_spreadsheet() in @see.
if ($project_name == 'AGILE: Application of Genomic Innovation in the Lentil Economy') {
$t = kp_nodes_rawpheno_ignorecols();
$index = array_map('rawpheno_function_delformat', $t);
if (count($index) > 0) {
$skip = $index;
}
}
}
/**
* Helper function: Search headers for omission from headers in the spreadsheet file.
*
* @param $header
* An array, headers from spreadsheet file.
*
* @return
* An array containing the index numbers of headers that matched header for omission.
*
* @see
* kp_nodes_rawpheno_ignorecols_valsave_alter().
* kp_nodes_rawpheno_ignorecols().
*/
function kp_nodes_rawpheno_ignorecols_match($header) {
// Ignore columns: type in lowercase.
$t = kp_nodes_rawpheno_ignorecols();
$ignore_cols = array_map('rawpheno_function_delformat', $t);
// Index of match found.
$cols_i = array();
// Find columns.
foreach($header as $i => $col) {
$col = rawpheno_function_delformat($col);
$c = (is_array($col)) ? $col['no format'] : strtolower($col);
if (in_array($c, $ignore_cols)) {
$cols_i[] = $i;
}
}
return $cols_i;
}
/**
* Helper function: Define/list column header names for rawphenotypes module to ignore.
*
* @return
* Array of column header names to be ignored.
*/
function kp_nodes_rawpheno_ignorecols() {
// As it appears in the spreadsheet file.
return array('Origin');
}