Skip to content

Commit cbb5ef7

Browse files
authored
Merge pull request #14 from UCBoulder/md-1172
Md 1172
2 parents 5d75d06 + 4758d3a commit cbb5ef7

5 files changed

Lines changed: 86 additions & 96 deletions

File tree

js/table_search.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
(function ($) {
22
$(document).ready(function () {
33
if ( $( ".table-search" ).length ) {
4-
$('.table-search').next().attr('id', 'gdoc-table');
5-
$('.table-search').before('<label for="searchtable"><strong>Enter keyword to search </strong></label><input id="searchtable" type="search" autosave="csrsearch" results="1" placeholder="search" name="s">');
6-
$('#searchtable').keyup(function () {
7-
searchTable($(this).val());
8-
});
9-
}
10-
if ( $( ".page-node-1485" ).length ) {
11-
$('.table-search').before('<select class="cat_search" style="margin-left: .5em;" name="cat"><option value="">Choose From Categories</option><option value="">--- Room Features ---</option><option value="RCC">Remote-Capable Classrooms (RCC)</option><option value="ME ">Media Equipped (ME)</option><option value="SC">Smart Classrooms (SC)</option><option value="ALR">Active Learning Rooms (ALR)</option><option value="LLH">Large Lecture Hall (LLH)</option><option value="DL">Distance Learning (DL)</option><option value="CC">Classroom Capture (CC)</option><option value="CR">CUClickers Receivers (CR)</option><option value="WM">Wireless Microphones (WM)</option><option value="LAB">Computing Labs (LAB)</option><option value="">--- Building Type ---</option><option value="academic">Academic</option></select>');
12-
$( ".cat_search" ).change(function () {
13-
var selection = this.value;
14-
if (selection) {
15-
$('#searchtable').val(selection);
16-
searchTable($(this).val());
17-
} else {
18-
$('#searchtable').val('');
19-
searchTable($(this).val());
4+
$('.table-search').each(function (i, elem) {
5+
var $ts = $(elem);
6+
var tableId = $ts.next().attr('id');
7+
var inputId = 'searchtable-' + i;
8+
$ts.before('<label for="' + inputId + '"><strong>Enter keyword to search </strong></label><input id="' + inputId + '" type="search" autosave="csrsearch" results="1" placeholder="search" name="s">');
9+
$('#' + inputId).keyup(function () {
10+
searchTable($(this).val(), tableId);
11+
});
12+
13+
if ( $( ".page-node-1485" ).length ) {
14+
var selectHtml = '<select class="cat_search" style="margin-left: .5em;" name="cat"><option value="">Choose From Categories</option><option value="">--- Room Features ---</option><option value="RCC">Remote-Capable Classrooms (RCC)</option><option value="ME ">Media Equipped (ME)</option><option value="SC">Smart Classrooms (SC)</option><option value="ALR">Active Learning Rooms (ALR)</option><option value="LLH">Large Lecture Hall (LLH)</option><option value="DL">Distance Learning (DL)</option><option value="CC">Classroom Capture (CC)</option><option value="CR">CUClickers Receivers (CR)</option><option value="WM">Wireless Microphones (WM)</option><option value="LAB">Computing Labs (LAB)</option><option value="">--- Building Type ---</option><option value="academic">Academic</option></select>';
15+
$ts.before(selectHtml);
16+
$ts.prev('.cat_search').change(function () {
17+
var selection = this.value;
18+
if (selection) {
19+
$('#' + inputId).val(selection);
20+
searchTable(selection, tableId);
21+
} else {
22+
$('#' + inputId).val('');
23+
searchTable(selection, tableId);
24+
}
25+
}).change();
2026
}
21-
}).change();
27+
});
2228
}
2329
});
2430

25-
function searchTable(inputVal) {
26-
var table = $('#gdoc-table');
31+
function searchTable(inputVal, tableId) {
32+
var table = $('#' + tableId);
2733
table.find('tr').each(function (index, row) {
2834
var allCells = $(row).find('td');
2935
var found = 0; // Declare found here, outside the inner each function

src/Plugin/GoogleSheetsApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class GoogleSheetsApi {
2828
/**
2929
* Grabbing the sheet data.
3030
*/
31-
public function sheetDefined($key, $sheet_letters, $gid = 0, $shift = 0) {
32-
$fetchData = new GoogleSheetsFetch($key, $gid, $shift);
31+
public function sheetDefined($key, $sheet_letters, $gid = 0, $shift = 0, $shentity = FALSE) {
32+
$fetchData = new GoogleSheetsFetch($key, $gid, $shift, $shentity);
3333
$newArray = $fetchData->getFetchedSheet();
3434
$processData = new GoogleSheetsProcess($newArray, $sheet_letters);
3535
$gSheetData = $processData->getProcessedData();

src/Plugin/GoogleSheetsFetch.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Drupal\oit\Plugin;
44

5+
use Drupal\Component\Utility\Xss;
6+
57
/**
68
* Fetches data from google sheets.
79
*
@@ -36,9 +38,17 @@ class GoogleSheetsFetch {
3638
/**
3739
* Fetch google sheet.
3840
*/
39-
public function __construct($key, $gid, $shift = 0) {
40-
// See https://gist.github.com/pamelafox/770584
41-
$feed = "https://docs.google.com/spreadsheets/d/$key/pub?gid=$gid&single=true&output=csv";
41+
public function __construct($key, $gid, $shift = 0, $shentity = FALSE) {
42+
if ($shentity) {
43+
// Check the url starts with 'https://docs.google.com'.
44+
$feed = !empty($key) && strpos($key, 'https://docs.google.com') === 0 ? $key : NULL;
45+
}
46+
else {
47+
$key = !empty($key) ? Xss::filter($key) : NULL;
48+
$gid = ctype_digit((string) $gid) && (int) $gid >= 0 ? (int) $gid : NULL;
49+
// See https://gist.github.com/pamelafox/770584
50+
$feed = "https://docs.google.com/spreadsheets/d/$key/pub?gid=$gid&single=true&output=csv";
51+
}
4252
// Arrays we'll use later.
4353
$newArray = [];
4454
// Do it.

src/Plugin/GoogleSheetsProcess.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ class GoogleSheetsProcess {
2525
* Process google sheet.
2626
*/
2727
public function __construct($gsheet_returned_data, $sheet_letters, $process = 'ss') {
28+
// Validate input data.
29+
if (!is_array($gsheet_returned_data) || empty($gsheet_returned_data)) {
30+
$this->processedData = ['rows' => [], 'header' => []];
31+
return;
32+
}
33+
34+
// Validate and sanitize process parameter.
35+
$allowed_processes = ['ss', 'custom'];
36+
$process = in_array($process, $allowed_processes, TRUE) ? $process : 'ss';
37+
38+
// Validate sheet_letters is a string before processing.
39+
// If not a string (e.g., NULL, array), default to empty string
40+
// which results in no columns being selected for processing.
41+
if (!is_string($sheet_letters)) {
42+
$sheet_letters = '';
43+
}
44+
45+
// Sanitize sheet letters input.
2846
$sheet_letters = strtolower($sheet_letters);
2947
$sheet_letters = str_replace(' ', '', $sheet_letters);
3048
$sheet_letters = explode(',', Xss::filter($sheet_letters));
@@ -54,50 +72,67 @@ public function __construct($gsheet_returned_data, $sheet_letters, $process = 's
5472
'v' => 21,
5573
'w' => 22,
5674
'x' => 23,
57-
'y' => 25,
58-
'z' => 26,
75+
'y' => 24,
76+
'z' => 25,
5977
];
78+
$sheet_items = [];
6079
foreach ($sheet_letters as $sheet_letter) {
61-
$sheet_items[] = $alphabet[$sheet_letter];
80+
if (isset($alphabet[$sheet_letter])) {
81+
$sheet_items[] = $alphabet[$sheet_letter];
82+
}
6283
}
63-
// @todo looking for $process but there may be a better way to clean this up
64-
// later. Fix some day.
65-
if ($process == 'custom') {
84+
if ($process === 'custom') {
85+
$sheet_header = [];
6686
foreach ($gsheet_returned_data[0] as $key => $value) {
6787
$sheet_header[] = $key;
6888
$i++;
6989
}
90+
$headers = [];
7091
foreach ($sheet_items as $value) {
71-
$headers[] = $sheet_header[$value];
92+
if (isset($sheet_header[$value])) {
93+
$headers[] = $sheet_header[$value];
94+
}
7295
}
7396

7497
$format = "markdown";
98+
$rows = [];
7599
foreach ($gsheet_returned_data as $key => $value) {
100+
$item = [];
76101
foreach ($headers as $key => $header) {
77-
$item[$key] = isset($value[$header]) ? check_markup($value[$header], $format) : '';
102+
// Sanitize data from external spreadsheet.
103+
$raw_value = $value[$header] ?? '';
104+
$item[$key] = check_markup(Xss::filter($raw_value), $format);
78105
}
79106
$rows[] = [
80107
'data' => $item,
81108
];
82109
}
83110
}
84111
else {
112+
$sheet_header = [];
85113
foreach ($gsheet_returned_data[0] as $key => $value) {
86114
$sheet_header[] = $value;
87115
$i++;
88116
}
117+
$headers = [];
89118
foreach ($sheet_items as $value) {
90-
$headers[] = $sheet_header[$value];
119+
if (isset($sheet_header[$value])) {
120+
$headers[] = $sheet_header[$value];
121+
}
91122
}
92123

93124
$format = "markdown";
125+
$rows = [];
94126
$rows_exist = isset($gsheet_returned_data[1]) ? TRUE : FALSE;
95127
if ($rows_exist) {
96128
foreach ($gsheet_returned_data as $key => $value) {
97129
// Skip first header row.
98-
if ($key != 0) {
130+
if ($key !== 0) {
131+
$item = [];
99132
foreach ($sheet_items as $key => $header) {
100-
$item[$key]['data']['#markup'] = isset($value[$header]) ? check_markup($value[$header], $format) : '';
133+
// Sanitize data from external spreadsheet.
134+
$raw_value = $value[$header] ?? '';
135+
$item[$key]['data']['#markup'] = check_markup(Xss::filter($raw_value), $format);
101136
}
102137
$rows[] = $item;
103138
}

src/Plugin/GoogleSheetsTopLinks.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)