r91825 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91824‎ | r91825 | r91826 >
Date:12:26, 10 July 2011
Author:devayon
Status:deferred
Tags:
Comment:
added methods to generate format-options
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -17,7 +17,7 @@
1818 protected function addAutocompletionJavascriptAndCSS(){
1919 global $wgOut, $smwgScriptPath, $smwgJQueryIncluded, $smwgJQueryUIIncluded;
2020 if($this->autocompleteenabled==false){
21 - $wgOut->addExtensionStyle( "$smwgScriptPath/skins/jquery-ui/base/jquery.ui.all.css" );
 21+ $wgOut->addExtensionStyle( "$smwgScriptPath/skins/jquery-ui/base/jquery.ui.all.css" );
2222
2323 $scripts = array();
2424
@@ -103,7 +103,7 @@
104104 $htmloutput="";
105105 $htmloutput.= $this->getForm();
106106 $param=array();
107 -
 107+
108108 $this->m_ui_helper = $helper = new SMWQueryUIHelper; //or some factory method
109109 //here come some driver lines for testing; this is very temporary
110110
@@ -123,7 +123,7 @@
124124 if($this->usesNavigationBar()){
125125 $htmloutput.= $this->getNavigationBar ($helper->getLimit(),$helper->getOffset(),$helper->hasFurtherResults()); //? can we preload offset and limit?
126126 }
127 -
 127+
128128 $htmloutput.= $helper->getHTMLResult();
129129
130130 if($this->usesNavigationBar()){
@@ -223,12 +223,12 @@
224224
225225 return $navigation;
226226 }
227 -
 227+
228228 /**
229 - * Creates the form elements and populates them with parameters.
 229+ * Creates the form elements and populates them with parameters.
230230 * UI implementations need to overload this if a different layout and form
231231 * elements are desired
232 - *
 232+ *
233233 * @return string Form elements in HTML
234234 */
235235 protected function getForm(){
@@ -237,7 +237,7 @@
238238 * then be placed in wOut as pleased, they will
239239 * also write javascript (if relevant) directly to wgOut.
240240 */
241 -
 241+
242242 //$result="";
243243 //$result.= getQueryFormBox($contents, $errors);
244244 //$result.= getPOFormBox($content, $enableAutoComplete);
@@ -265,7 +265,7 @@
266266 protected function getPOFormBox($content, $enableAutocomplete=true){
267267 if($enableAutocomplete){
268268 global $wgOut;
269 -
 269+
270270 if(!$this->autocompleteenabled) addAutocompletionJavascriptAndCSS();
271271 $javascript_autocomplete_text = <<<END
272272 <script type="text/javascript">
@@ -303,7 +303,7 @@
304304 END;
305305
306306 $wgOut->addScript( $javascript_autocomplete_text );
307 -
 307+
308308 }
309309 $result="";
310310 $result=Html::element('textarea',array('id'=> 'add_property', 'name'=> 'po', 'cols'=>'20', 'rows'=> '6'),$content);
@@ -417,7 +417,7 @@
418418 *
419419 * @return Parameter
420420 */
421 - protected function toValidatorParam( $param ) {
 421+ private function toValidatorParam( $param ) {
422422 static $typeMap = array(
423423 'int' => Parameter::TYPE_INTEGER
424424 );
@@ -447,7 +447,7 @@
448448 }
449449 }
450450
451 - /**
 451+ /**
452452 * Get the HTML for a single parameter input.
453453 *
454454 * @since 1.6
@@ -457,7 +457,7 @@
458458 *
459459 * @return string
460460 */
461 - protected function showFormatOption( Parameter $parameter, $currentValue ) {
 461+ private function showFormatOption( Parameter $parameter, $currentValue ) {
462462 $input = new ParameterInput( $parameter );
463463 $input->setInputName( 'p[' . $parameter->getName() . ']' );
464464
@@ -468,6 +468,106 @@
469469 return $input->getHtml();
470470 }
471471
 472+ /**
 473+ * Creates form elements for choosing the result-format and their associated
 474+ * format. Use in conjunction with handleFormatOptions() to supply format
 475+ * options using ajax.
 476+ * @return string
 477+ */
 478+ protected function makeFormatSelectBox( $defaultformat = 'broadtable' ) {
 479+
 480+ global $smwgResultFormats, $smwgJQueryIncluded, $wgOut;
 481+
 482+ if ( !$smwgJQueryIncluded ) {
 483+ $realFunction = array( 'OutputPage', 'includeJQuery' );
 484+ if ( is_callable( $realFunction ) ) {
 485+ $wgOut->includeJQuery();
 486+ } else {
 487+ $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js";
 488+ }
 489+ $smwgJQueryIncluded = true;
 490+ }
 491+
 492+ // checking argument
 493+ $default_format = 'broadtable';
 494+ if ( array_key_exists( $defaultformat, $smwgResultFormats ) ) {
 495+ $default_format = $defaultformat;
 496+ }
 497+
 498+ $result = "";
 499+ $printer = SMWQueryProcessor::getResultPrinter( $default_format, SMWQueryProcessor::SPECIAL_PAGE );
 500+ $url = $this->getTitle()->getLocalURL( "showformatoptions=' + this.value + '" );
 501+
 502+ foreach ( $this->m_ui_helper->getParams() as $param => $value ) {
 503+ if ( $param !== 'format' ) {
 504+ $url .= '&params[' . Xml::escapeJsString( $param ) . ']=' . Xml::escapeJsString( $value );
 505+ }
 506+ }
 507+
 508+ $result .= "\n<p>" . wfMsg( 'smw_ask_format_as' ) . "\n" .
 509+ '<select id="formatSelector" name="p[format]" onChange="JavaScript:updateOtherOptions(\'' . $url . '\')">' . "\n" .
 510+ ' <option value="' . $default_format . '">' . $printer->getName() . ' (' . wfMsg( 'smw_ask_defaultformat' ) . ')</option>' . "\n";
 511+
 512+ $formats = array();
 513+
 514+ foreach ( array_keys( $smwgResultFormats ) as $format ) {
 515+ // Special formats "count" and "debug" currently not supported.
 516+ if ( $format != $default_format && $format != 'count' && $format != 'debug' ) {
 517+ $printer = SMWQueryProcessor::getResultPrinter( $format, SMWQueryProcessor::SPECIAL_PAGE );
 518+ $formats[$format] = $printer->getName();
 519+ }
 520+ }
 521+
 522+ natcasesort( $formats );
 523+ $params = $this->m_ui_helper->getParams();
 524+ foreach ( $formats as $format => $name ) {
 525+ $result .= ' <option value="' . $format . '"' . ( $params['format'] == $format ? ' selected' : '' ) . '>' . $name . "</option>\n";
 526+ }
 527+ $result .= "</select>";
 528+ $result .= "</p>\n";
 529+ $result .= '<fieldset><legend>' . wfMsg( 'smw_ask_otheroptions' ) . "</legend>\n";
 530+ $result .= "<div id=\"other_options\">" . $this->showFormatOptions( $params['format'], $params ) . " </div>";
 531+ $result .= "</fieldset>\n";
 532+
 533+ // BEGIN: add javascript for updating formating options by ajax
 534+ global $wgOut;
 535+ // $default_format_url = SpecialPage::getSafeTitleFor( 'Ask' )->getLocalURL( "showformatoptions=broadtable" );
 536+ // $default_format_url .= '&params[title]=Special:Ask&params[offset]=0&params[limit]=20';
 537+ $javascript = <<<END
 538+<script type="text/javascript">
 539+function updateOtherOptions(strURL) {
 540+ jQuery.ajax({ url: strURL, context: document.body, success: function(data){
 541+ jQuery("#other_options").html(data);
 542+ }});
 543+}
 544+</script>
 545+END;
 546+
 547+ $wgOut->addScript( $javascript );
 548+ // END: add javascript for updating formating options by ajax
 549+
 550+ return $result;
 551+ }
 552+
 553+ /**
 554+ * Returns form elements for a requested format. Use in conjunction with makeFormatSelectBox()
 555+ *
 556+ * @param WebRequest $wgRequest
 557+ * @return boolean Returns true if formatoptions were requested and returned, else false
 558+ */
 559+ protected function handleFormatOptions( $wgRequest ) {
 560+ global $wgOut;
 561+ if ( $wgRequest->getCheck( 'showformatoptions' ) ) {
 562+ // handle Ajax action
 563+ $format = $wgRequest->getVal( 'showformatoptions' );
 564+ $params = $wgRequest->getArray( 'params' );
 565+ $wgOut->disable();
 566+ echo $this->showFormatOptions( $format, $params );
 567+ return true;
 568+ } else {
 569+ return false;
 570+ }
 571+ }
472572 protected function usesNavigationBar() {
473573 return true;
474574 }
@@ -533,7 +633,7 @@
534634 return false;
535635 }
536636 }
537 -
 637+
538638 public function getResultObject(){
539639 return $this->getResultObject();
540640 }
@@ -711,7 +811,7 @@
712812 }
713813 // END: Try to be smart for rss/ical if no description/title is given and we have a concept query
714814 }
715 -
 815+
716816 public function getHTMLResult(){
717817 $result = '';
718818 $res= $this->queryresult;