r44211 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44210‎ | r44211 | r44212 >
Date:01:10, 4 December 2008
Author:yaron
Status:deferred
Tags:
Comment:
Added handling for 'free text' and 'date range' input types, including the usage
of the Ext Javascript library for a combobox for the former;
Modified paths:
  • /trunk/extensions/SemanticDrilldown/specials/SD_BrowseData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticDrilldown/specials/SD_BrowseData.php
@@ -83,6 +83,10 @@
8484 $url .= urlencode(str_replace(' ', '_', $af->filter->name)) . "[$j]=" . urlencode(str_replace(' ', '_', $fv->text));
8585 }
8686 }
 87+ if ($af->search_term != null) {
 88+ $url .= (strpos($url, '?')) ? '&' : '?';
 89+ $url .= '_search_' . urlencode(str_replace(' ', '_', $af->filter->name)) . "=" . urlencode(str_replace(' ', '_', $af->search_term));
 90+ }
8791 }
8892 return $url;
8993 }
@@ -661,6 +665,16 @@
662666 return $text;
663667 }
664668
 669+ function printFilterLabel($filter_name) {
 670+ $labels_for_filter = sdfGetValuesForProperty($filter_name, SD_NS_FILTER, SD_SP_HAS_LABEL, NS_MAIN);
 671+ if (count($labels_for_filter) > 0) {
 672+ $filter_label = $labels_for_filter[0];
 673+ } else {
 674+ $filter_label = str_replace('_', ' ', $filter_name);
 675+ }
 676+ return $filter_label;
 677+ }
 678+
665679 /**
666680 * Print the line showing 'OR' values for a filter that already has
667681 * at least one value set
@@ -669,12 +683,7 @@
670684 global $sdgScriptPath;
671685
672686 $results_line = "";
673 - $labels_for_filter = sdfGetValuesForProperty($af->filter->name, SD_NS_FILTER, SD_SP_HAS_LABEL, false, NS_MAIN);
674 - if (count($labels_for_filter) > 0) {
675 - $filter_label = $labels_for_filter[0];
676 - } else {
677 - $filter_label = str_replace('_', ' ', $af->filter->name);
678 - }
 687+ $filter_label = $this->printFilterLabel($af->filter->name);
679688 foreach ($this->applied_filters as $af2) {
680689 if ($af->filter->name == $af2->filter->name)
681690 $current_filter_values = $af2->values;
@@ -683,6 +692,19 @@
684693 $or_values = $af->filter->allowed_values;
685694 else
686695 $or_values = $this->getAllOrValues($af);
 696+ if ($af->search_term != null) {
 697+ // HACK - printFreeTextInput() needs values as the
 698+ // *keys* of the array
 699+ $filter_values = array();
 700+ foreach ($or_values as $or_value) {
 701+ $filter_values[$or_value] = '';
 702+ }
 703+ $results_line = "<div class=\"drilldown-filter-label\">$filter_label:</div> " . $this->printFreeTextInput($af->filter->name, $filter_values, $af->search_term);
 704+ return $results_line;
 705+ } elseif ($af->lower_date != null || $af->upper_date != null) {
 706+ $results_line = "<div class=\"drilldown-filter-label\">$filter_label:</div> " . $this->printDateRangeInput($af->filter->name, $af->lower_date, $af->upper_date);
 707+ return $results_line;
 708+ }
687709 // add 'Other' and 'None', regardless of whether either has
688710 // any results - add 'Other' only if it's not a date field
689711 if ($af->filter->time_period == null)
@@ -742,6 +764,152 @@
743765 return $text;
744766 }
745767
 768+ function printUnappliedFilterValues($cur_url, $f, $filter_values) {
 769+ $results_line = "";
 770+ // set font-size values for filter "tag cloud", if the
 771+ // appropriate global variables are set
 772+ if ($sdgFiltersSmallestFontSize > 0 && $sdgFiltersLargestFontSize > 0) {
 773+ $lowest_num_results = min($filter_values);
 774+ $highest_num_results = max($filter_values);
 775+ $num_results_midpoint = ($lowest_num_results + $highest_num_results) / 2;
 776+ $font_size_midpoint = ($sdgFiltersSmallestFontSize + $sdgFiltersLargestFontSize) / 2;
 777+ $num_results_per_font_pixel = ($highest_num_results + 1 - $lowest_num_results) / ($sdgFiltersLargestFontSize + 1 - $sdgFiltersSmallestFontSize);
 778+ }
 779+ // now print the values
 780+ $num_printed_values = 0;
 781+ foreach ($filter_values as $value_str => $num_results) {
 782+ if ($num_printed_values++ > 0) { $results_line .= " &middot; "; }
 783+ // if it's boolean, display something nicer than "0" or "1"
 784+ if ($value_str === '_other')
 785+ $filter_text = wfMsg('sd_browsedata_other');
 786+ elseif ($value_str === '_none')
 787+ $filter_text = wfMsg('sd_browsedata_none');
 788+ elseif ($f->is_boolean)
 789+ $filter_text = sdfBooleanToString($value_str);
 790+ else
 791+ $filter_text = str_replace('_', ' ', $value_str);
 792+ $filter_text .= " ($num_results)";
 793+ $filter_url = $cur_url . urlencode(str_replace(' ', '_', $f->name)) . '=' . urlencode(str_replace(' ', '_', $value_str));
 794+ if ($sdgFiltersSmallestFontSize > 0 && $sdgFiltersLargestFontSize > 0) {
 795+ $font_size = round($font_size_midpoint + (($num_results - $num_results_midpoint) / $num_results_per_font_pixel));
 796+ $results_line .= "\n " . '<a href="' . $filter_url . '" title="' . wfMsg('sd_browsedata_filterbyvalue') . '" style="font-size: ' . $font_size . 'px">' . $filter_text . '</a>';
 797+ } else {
 798+ $results_line .= "\n " . '<a href="' . $filter_url . '" title="' . wfMsg('sd_browsedata_filterbyvalue') . '">' . $filter_text . '</a>';
 799+ }
 800+ }
 801+ return $results_line;
 802+ }
 803+
 804+ function printFreeTextInput($filter_name, $filter_values, $cur_value = null) {
 805+ global $wgRequest;
 806+
 807+ $input_id = "_search_$filter_name";
 808+ $text =<<<END
 809+
 810+<script>
 811+Ext.onReady(function(){
 812+ var {$filter_name}_values = [
 813+
 814+END;
 815+ foreach ($filter_values as $value => $num_instances) {
 816+ if ($value != '_other' && $value != '_none') {
 817+ $display_value = str_replace('_', ' ', $value);
 818+ $display_value = str_replace('\'', '\\\'', $display_value);
 819+ $text .= " ['$display_value', '$display_value'],\n";
 820+ }
 821+ }
 822+ $text .=<<<END
 823+ ]
 824+
 825+ var comboFromArray = new Ext.form.ComboBox({
 826+ store: {$filter_name}_values,
 827+ emptyText: '$cur_value',
 828+ applyTo: '$input_id'
 829+ });
 830+});
 831+</script>
 832+<form method="get">
 833+<input type="text" name="$input_id" id="$input_id" value="">
 834+
 835+END;
 836+
 837+ foreach ($wgRequest->getValues() as $key => $val) {
 838+ if ($key != $input_id)
 839+ $text .=<<<END
 840+<input type="hidden" name="$key" value="$val" />
 841+
 842+END;
 843+ }
 844+ $search_label = wfMsg('searchresultshead');
 845+ $text .=<<<END
 846+<input type="submit" value="$search_label" />
 847+</form>
 848+
 849+END;
 850+ return $text;
 851+ }
 852+
 853+ function printDateInput($input_name, $cur_value = null) {
 854+ $month_names = array(
 855+ wfMsgForContent('january'),
 856+ wfMsgForContent('february'),
 857+ wfMsgForContent('march'),
 858+ wfMsgForContent('april'),
 859+ wfMsgForContent('may'),
 860+ wfMsgForContent('june'),
 861+ wfMsgForContent('july'),
 862+ wfMsgForContent('august'),
 863+ wfMsgForContent('september'),
 864+ wfMsgForContent('october'),
 865+ wfMsgForContent('november'),
 866+ wfMsgForContent('december')
 867+ );
 868+
 869+ if (is_array($cur_value) && array_key_exists('month', $cur_value))
 870+ $selected_month = $cur_value['month'];
 871+ else
 872+ $selected_month = null;
 873+ $text = ' <select name="' . $input_name . "[month]\">\n";
 874+ foreach ($month_names as $i => $name) {
 875+ // pad out month to always be two digits
 876+ $month_value = ($wgAmericanDates == true) ? $name : str_pad($i + 1, 2, "0", STR_PAD_LEFT);
 877+ $selected_str = ($i + 1 == $selected_month) ? "selected" : "";
 878+ $text .= " <option value=\"$month_value\" $selected_str>$name</option>\n";
 879+ }
 880+ $text .= " </select>\n";
 881+ $text .= ' <input name="' . $input_name . '[day]" type="text" size="2" value="' . $cur_value['day'] . '" />' . "\n";
 882+ $text .= ' <input name="' . $input_name . '[year]" type="text" size="4" value="' . $cur_value['year'] . '" />' . "\n";
 883+ return $text;
 884+ }
 885+
 886+ function printDateRangeInput($filter_name, $lower_date = null, $upper_date = null) {
 887+ global $wgRequest;
 888+
 889+ $start_label = wfMsg('sd_browsedata_daterangestart');
 890+ $end_label = wfMsg('sd_browsedata_daterangeend');
 891+ $start_month_input = $this->printDateInput("_lower_$filter_name", $lower_date);
 892+ $end_month_input = $this->printDateInput("_upper_$filter_name", $upper_date);
 893+ $text =<<<END
 894+<form method="get">
 895+<p>$start_label $start_month_input
 896+$end_label $end_month_input</p>
 897+
 898+END;
 899+ foreach ($wgRequest->getValues() as $key => $val) {
 900+ $text .=<<<END
 901+<input type="hidden" name="$key" value="$val" />
 902+
 903+END;
 904+ }
 905+ $search_label = wfMsg('searchresultshead');
 906+ $text .=<<<END
 907+<p><input type="submit" value="$search_label" /></p>
 908+</form>
 909+
 910+END;
 911+ return $text;
 912+ }
 913+
746914 /**
747915 * Print the line showing 'AND' values for a filter that has not
748916 * been applied to the drilldown
@@ -750,7 +918,6 @@
751919 global $sdgScriptPath;
752920 global $sdgFiltersSmallestFontSize, $sdgFiltersLargestFontSize;
753921
754 - $results_line = "";
755922 $f->createTempTable();
756923 $found_results_for_filter = false;
757924 if (count($f->allowed_values) == 0) {
@@ -794,49 +961,36 @@
795962 $f->dropTempTable();
796963 return "";
797964 }
798 - // set font-size values for filter "tag cloud", if the
799 - // appropriate global variables are set
800 - if ($sdgFiltersSmallestFontSize > 0 && $sdgFiltersLargestFontSize > 0) {
801 - $lowest_num_results = min($filter_values);
802 - $highest_num_results = max($filter_values);
803 - $num_results_midpoint = ($lowest_num_results + $highest_num_results) / 2;
804 - $font_size_midpoint = ($sdgFiltersSmallestFontSize + $sdgFiltersLargestFontSize) / 2;
805 - $num_results_per_font_pixel = ($highest_num_results + 1 - $lowest_num_results) / ($sdgFiltersLargestFontSize + 1 - $sdgFiltersSmallestFontSize);
806 - }
807 - // now print the values
808 - $num_printed_values = 0;
809 - foreach ($filter_values as $value_str => $num_results) {
810 - if ($num_printed_values++ > 0) { $results_line .= " &middot; "; }
811 - // if it's boolean, display something nicer than "0" or "1"
812 - if ($value_str === '_other')
813 - $filter_text = wfMsg('sd_browsedata_other');
814 - elseif ($value_str === '_none')
815 - $filter_text = wfMsg('sd_browsedata_none');
816 - elseif ($f->is_boolean)
817 - $filter_text = sdfBooleanToString($value_str);
818 - else
819 - $filter_text = str_replace('_', ' ', $value_str);
820 - $filter_text .= " ($num_results)";
821 - $filter_url = $cur_url . urlencode(str_replace(' ', '_', $f->name)) . '=' . urlencode(str_replace(' ', '_', $value_str));
822 - if ($sdgFiltersSmallestFontSize > 0 && $sdgFiltersLargestFontSize > 0) {
823 - $font_size = round($font_size_midpoint + (($num_results - $num_results_midpoint) / $num_results_per_font_pixel));
824 - $results_line .= "\n " . '<a href="' . $filter_url . '" title="' . wfMsg('sd_browsedata_filterbyvalue') . '" style="font-size: ' . $font_size . 'px">' . $filter_text . '</a>';
825 - } else {
826 - $results_line .= "\n " . '<a href="' . $filter_url . '" title="' . wfMsg('sd_browsedata_filterbyvalue') . '">' . $filter_text . '</a>';
827 - }
828 - }
 965+
 966+ $filter_name = urlencode(str_replace(' ', '_', $f->name));
 967+ $normal_filter = true;
 968+ if ($f->input_type == wfMsgForContent('sd_filter_freetext')) {
 969+ $results_line = $this->printFreeTextInput($filter_name, $filter_values);
 970+ $normal_filter = false;
 971+ } elseif ($f->input_type == wfMsgForContent('sd_filter_daterange')) {
 972+ $results_line = $this->printDateRangeInput($filter_name);
 973+ $normal_filter = false;
 974+ } else
 975+ $results_line = $this->printUnappliedFilterValues($cur_url, $f, $filter_values);
 976+
829977 $text = "";
830978 // TODO - this check might no longer be necessary
831979 if ($results_line != "") {
832 - $labels_for_filter = sdfGetValuesForProperty($f->name, SD_NS_FILTER, SD_SP_HAS_LABEL, false, NS_MAIN);
833 - if (count($labels_for_filter) > 0) {
834 - $filter_label = $labels_for_filter[0];
835 - } else {
836 - $filter_label = str_replace('_', ' ', $f->name);
837 - }
 980+ $filter_label = $this->printFilterLabel($f->name);
838981 $results_div_id = strtolower(str_replace(' ', '_', $filter_label)) . "_values";
839982 $text .=<<<END
840 - <div class="drilldown-filter-label"><a onclick="toggleFilterDiv('$results_div_id', this)" style="cursor: default;"><img src="$sdgScriptPath/skins/down-arrow.png"></a>
 983+ <div class="drilldown-filter-label">
 984+
 985+END;
 986+ // no point showing "minimize" arrow if it's just a
 987+ // single text or date input
 988+ if ($normal_filter) {
 989+ $text .=<<<END
 990+ <a onclick="toggleFilterDiv('$results_div_id', this)" style="cursor: default;"><img src="$sdgScriptPath/skins/down-arrow.png"></a>
 991+
 992+END;
 993+ }
 994+ $text .=<<<END
841995 $filter_label:
842996 </div>
843997 <div class="drilldown-filter-values" id="$results_div_id">$results_line
@@ -882,12 +1036,7 @@
8831037 }
8841038 foreach ($this->applied_filters as $i => $af) {
8851039 $header .= (! $this->subcategory && $i == 0) ? " > " : "\n <span class=\"drilldown-header-value\">&</span> ";
886 - $labels_for_filter = sdfGetValuesForProperty($af->filter->name, SD_NS_FILTER, SD_SP_HAS_LABEL, false, NS_MAIN);
887 - if (count($labels_for_filter) > 0) {
888 - $filter_label = $labels_for_filter[0];
889 - } else {
890 - $filter_label = str_replace('_', ' ', $af->filter->name);
891 - }
 1040+ $filter_label = $this->printFilterLabel($af->filter->name);
8921041 // add an "x" to remove this filter, if it has more
8931042 // than one value
8941043 if (count($this->applied_filters[$i]->values) > 1) {
@@ -915,6 +1064,16 @@
9161065 array_splice($temp_filters_array[$i]->values, $j, 0, $removed_values);
9171066 $header .= "\n " . ' <span class="drilldown-header-value">' . $filter_text . '</span> <a href="' . $remove_filter_url . '" title="' . wfMsg('sd_browsedata_removefilter') . '"><img src="' . $sdgScriptPath . '/skins/filter-x.png" /></a>';
9181067 }
 1068+ if ($af->search_term != null) {
 1069+ $temp_filters_array = $this->applied_filters;
 1070+ $removed_search_term = $temp_filters_array[$i]->search_term;
 1071+ $temp_filters_array[$i]->search_term = null;
 1072+ $remove_filter_url = $this->makeBrowseURL($this->category, $temp_filters_array, $this->subcategory);
 1073+ $temp_filters_array[$i]->search_term = $removed_search_term;
 1074+ $header .= "\n " . ' <span class="drilldown-header-value">~ \'' . $af->search_term . '\'</span> <a href="' . $remove_filter_url . '" title="' . wfMsg('sd_browsedata_removefilter') . '"><img src="' . $sdgScriptPath . '/skins/filter-x.png" /></a>';
 1075+ } elseif ($af->lower_date != null || $af->upper_date != null) {
 1076+ $header .= "\n <span class=\"drilldown-header-value\">" . $af->lower_date_string . " - " . $af->upper_date_string . "</span>";
 1077+ }
9191078 }
9201079 $header .= "</div>\n";
9211080 $drilldown_description = wfMsg('sd_browsedata_docu');
@@ -1102,13 +1261,36 @@
11031262 global $wgRequest, $wgOut, $sdgScriptPath, $sdgContLang, $sdgNumResultsPerPage;
11041263 $sd_props = $sdgContLang->getSpecialPropertiesArray();
11051264
1106 - $mainCssUrl = $sdgScriptPath . '/skins/SD_main.css';
 1265+ $mainCssDir = $sdgScriptPath . '/skins/';
11071266 $wgOut->addLink( array(
11081267 'rel' => 'stylesheet',
11091268 'type' => 'text/css',
11101269 'media' => "screen, projection",
1111 - 'href' => $mainCssUrl
 1270+ 'href' => $mainCssDir . 'SD_main.css'
11121271 ));
 1272+ $wgOut->addLink( array(
 1273+ 'rel' => 'stylesheet',
 1274+ 'type' => 'text/css',
 1275+ 'media' => "screen, projection",
 1276+ 'href' => $mainCssDir . 'ext-all.css'
 1277+ ));
 1278+ $wgOut->addLink( array(
 1279+ 'rel' => 'stylesheet',
 1280+ 'type' => 'text/css',
 1281+ 'media' => "screen, projection",
 1282+ 'href' => $mainCssDir . 'xtheme-gray.css'
 1283+ ));
 1284+ $wgOut->addLink( array(
 1285+ 'rel' => 'stylesheet',
 1286+ 'type' => 'text/css',
 1287+ 'media' => "screen, projection",
 1288+ 'href' => $mainCssDir . 'combos.css'
 1289+ ));
 1290+ // overwrite style from ext-all.css, to set the correct image for
 1291+ // the combobox arrow
 1292+ $wgOut->addScript("<style>.x-form-field-wrap .x-form-trigger{background:transparent url($sdgScriptPath/skins/trigger.gif) no-repeat 0 0;}</style>\n");
 1293+ $wgOut->addScript('<script type="text/javascript" src="' . $sdgScriptPath . '/libs/ext-base.js"></script>' . "\n");
 1294+ $wgOut->addScript('<script type="text/javascript" src="' . $sdgScriptPath . '/libs/ext-all.js"></script>' . "\n");
11131295 $javascript_text =<<<END
11141296 function toggleFilterDiv(element_id, label_element) {
11151297 element = document.getElementById(element_id);
@@ -1148,7 +1330,7 @@
11491331 if (! $category) {
11501332 $category_title = wfMsg('browsedata');
11511333 } else {
1152 - $titles_for_category = sdfGetValuesForProperty($category, NS_CATEGORY, SD_SP_HAS_DRILLDOWN_TITLE, false, NS_MAIN);
 1334+ $titles_for_category = sdfGetValuesForProperty($category, NS_CATEGORY, SD_SP_HAS_DRILLDOWN_TITLE, NS_MAIN);
11531335 if (count($titles_for_category) > 0) {
11541336 $category_title = str_replace('_', ' ', $titles_for_category[0]);
11551337 } else {
@@ -1175,18 +1357,28 @@
11761358 $applied_filters = array();
11771359 $remaining_filters = array();
11781360 foreach ($filters as $i => $filter) {
1179 - if ($vals_array = $wgRequest->getArray(str_replace(' ', '_', $filter->name))) {
 1361+ $filter_name = str_replace(' ', '_', $filter->name);
 1362+ $search_term = $wgRequest->getVal('_search_' . $filter_name);
 1363+ $lower_date = $wgRequest->getArray('_lower_' . $filter_name);
 1364+ $upper_date = $wgRequest->getArray('_upper_' . $filter_name);
 1365+ if ($vals_array = $wgRequest->getArray($filter_name)) {
11801366 foreach ($vals_array as $j => $val) {
11811367 $vals_array[$j] = str_replace('_', ' ', $val);
11821368 }
11831369 $applied_filters[] = SDAppliedFilter::create($filter, $vals_array);
11841370 $filter_used[$i] = true;
 1371+ } elseif ($search_term != null) {
 1372+ $applied_filters[] = SDAppliedFilter::create($filter, array(), $search_term);
 1373+ $filter_used[$i] = true;
 1374+ } elseif ($lower_date != null || $upper_date != null) {
 1375+ $applied_filters[] = SDAppliedFilter::create($filter, array(), null, $lower_date, $upper_date);
 1376+ $filter_used[$i] = true;
11851377 }
11861378 }
11871379 // add every unused filter to the $remaining_filters array, unless
11881380 // it requires some other filter that hasn't been applied
11891381 foreach ($filters as $i => $filter) {
1190 - $required_filters = sdfGetValuesForProperty($filter->name, SD_NS_FILTER, SD_SP_REQUIRES_FILTER, true, SD_NS_FILTER);
 1382+ $required_filters = sdfGetValuesForProperty($filter->name, SD_NS_FILTER, SD_SP_REQUIRES_FILTER, SD_NS_FILTER);
11911383 $matched_all_required_filters = true;
11921384 foreach ($required_filters as $required_filter) {
11931385 $found_match = false;

Status & tagging log