Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php |
— | — | @@ -16,6 +16,20 @@ |
17 | 17 | const ENABLE_AUTO_SUGGEST = true; |
18 | 18 | const DISABLE_AUTO_SUGGEST = false; |
19 | 19 | |
| 20 | + /** |
| 21 | + * Builds a read-only #ask of the given query |
| 22 | + * |
| 23 | + * @return string |
| 24 | + */ |
| 25 | + protected function getAskEmbedBox() { |
| 26 | + $result = '<div id="inlinequeryembed"> |
| 27 | + <div id="inlinequeryembedinstruct">' . wfMsg( 'smw_ask_embed_instr' ) . '</div> |
| 28 | + <textarea id="inlinequeryembedarea" readonly="yes" cols="20" rows="6" onclick="this.select()">' . |
| 29 | + $this->m_ui_helper->getAsk() . '</textarea></div>'; |
| 30 | + |
| 31 | + return $result; |
| 32 | + } |
| 33 | + |
20 | 34 | protected function addAutocompletionJavascriptAndCSS() { |
21 | 35 | global $wgOut, $smwgScriptPath, $smwgJQueryIncluded, $smwgJQueryUIIncluded; |
22 | 36 | if ( $this->autocompleteenabled == false ) { |
— | — | @@ -254,7 +268,7 @@ |
255 | 269 | } |
256 | 270 | |
257 | 271 | /** |
258 | | - * A method which generates the form element(s) for the Query-string. Use its |
| 272 | + * Generates the form element(s) for the Query-string. Use its |
259 | 273 | * complement processQueryFormBox() to decode data sent through these elements. |
260 | 274 | * UI's may overload both to change form parameters. |
261 | 275 | * |
— | — | @@ -270,7 +284,7 @@ |
271 | 285 | } |
272 | 286 | |
273 | 287 | /** |
274 | | - * A method which decodes form data sent through form-elements generated by |
| 288 | + * Decodes form data sent through form-elements generated by |
275 | 289 | * its complement, getQueryFormBox. UIs may overload both to change form parameters. |
276 | 290 | * |
277 | 291 | * @param WebRequest $wgRequest |
— | — | @@ -283,7 +297,136 @@ |
284 | 298 | } |
285 | 299 | |
286 | 300 | /** |
287 | | - * A method which generates the form element(s) for PrintOuts. |
| 301 | + * Generates the forms elements(s) for adding sorting options. Use its |
| 302 | + * complement processSortingFormBox() to decode sorting data sent |
| 303 | + * by these elements. |
| 304 | + * |
| 305 | + * @return string |
| 306 | + */ |
| 307 | + protected function addSortingFormBox() { |
| 308 | + $result = ''; |
| 309 | + if ( $smwgQSortingSupport ) { |
| 310 | + global $wgRequest, $wgOut, $smwgJQueryIncluded; |
| 311 | + |
| 312 | + if ( ! array_key_exists( 'sort', $this->m_params ) || ! array_key_exists( 'order', $this->m_params ) ) { |
| 313 | + $orders = array(); // do not even show one sort input here |
| 314 | + } else { |
| 315 | + $sorts = explode( ',', $this->m_params['sort'] ); |
| 316 | + $orders = explode( ',', $this->m_params['order'] ); |
| 317 | + reset( $sorts ); |
| 318 | + } |
| 319 | + |
| 320 | + foreach ( $orders as $i => $order ) { |
| 321 | + $result .= "<div id=\"sort_div_$i\">" . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort[' . $i . ']" value="' . |
| 322 | + htmlspecialchars( $sorts[$i] ) . "\" size=\"35\"/>\n" . '<select name="order[' . $i . ']"><option '; |
| 323 | + if ( $order == 'ASC' ) $result .= 'selected="selected" '; |
| 324 | + $result .= 'value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . '</option><option '; |
| 325 | + if ( $order == 'DESC' ) $result .= 'selected="selected" '; |
| 326 | + |
| 327 | + $result .= 'value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option></select>\n"; |
| 328 | + $result .= '[<a href="javascript:removeInstance(\'sort_div_' . $i . '\')">' . wfMsg( 'delete' ) . '</a>]' . "\n"; |
| 329 | + $result .= "</div>\n"; |
| 330 | + } |
| 331 | + |
| 332 | + $result .= '<div id="sorting_starter" style="display: none">' . wfMsg( 'smw_ask_sortby' ) . ' <input type="text" name="sort_num" size="35" />' . "\n"; |
| 333 | + $result .= ' <select name="order_num">' . "\n"; |
| 334 | + $result .= ' <option value="ASC">' . wfMsg( 'smw_ask_ascorder' ) . "</option>\n"; |
| 335 | + $result .= ' <option value="DESC">' . wfMsg( 'smw_ask_descorder' ) . "</option>\n</select>\n"; |
| 336 | + $result .= "</div>\n"; |
| 337 | + $result .= '<div id="sorting_main"></div>' . "\n"; |
| 338 | + $result .= '<a href="javascript:addInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_add_sortcondition' ) . '</a>' . "\n"; |
| 339 | + |
| 340 | + |
| 341 | + |
| 342 | + |
| 343 | + $this->m_num_sort_values = 0; |
| 344 | + |
| 345 | + if ( !array_key_exists( 'sort', $this->m_params ) ) { |
| 346 | + $sort_values = $wgRequest->getArray( 'sort' ); |
| 347 | + if ( is_array( $sort_values ) ) { |
| 348 | + $this->m_params['sort'] = implode( ',', $sort_values ); |
| 349 | + $this->m_num_sort_values = count( $sort_values ); |
| 350 | + } |
| 351 | + } |
| 352 | + // Javascript code for handling adding and removing the "sort" inputs |
| 353 | + $delete_msg = wfMsg( 'delete' ); |
| 354 | + |
| 355 | + |
| 356 | + $javascript_text = <<<EOT |
| 357 | +<script type="text/javascript"> |
| 358 | +// code for handling adding and removing the "sort" inputs |
| 359 | +var num_elements = {$this->m_num_sort_values}; |
| 360 | + |
| 361 | +function addInstance(starter_div_id, main_div_id) { |
| 362 | + var starter_div = document.getElementById(starter_div_id); |
| 363 | + var main_div = document.getElementById(main_div_id); |
| 364 | + |
| 365 | + //Create the new instance |
| 366 | + var new_div = starter_div.cloneNode(true); |
| 367 | + var div_id = 'sort_div_' + num_elements; |
| 368 | + new_div.className = 'multipleTemplate'; |
| 369 | + new_div.id = div_id; |
| 370 | + new_div.style.display = 'block'; |
| 371 | + |
| 372 | + var children = new_div.getElementsByTagName('*'); |
| 373 | + var x; |
| 374 | + for (x = 0; x < children.length; x++) { |
| 375 | + if (children[x].name) |
| 376 | + children[x].name = children[x].name.replace(/_num/, '[' + num_elements + ']'); |
| 377 | + } |
| 378 | + |
| 379 | + //Create 'delete' link |
| 380 | + var remove_button = document.createElement('span'); |
| 381 | + remove_button.innerHTML = '[<a href="javascript:removeInstance(\'sort_div_' + num_elements + '\')">{$delete_msg}</a>]'; |
| 382 | + new_div.appendChild(remove_button); |
| 383 | + |
| 384 | + //Add the new instance |
| 385 | + main_div.appendChild(new_div); |
| 386 | + num_elements++; |
| 387 | +} |
| 388 | + |
| 389 | +function removeInstance(div_id) { |
| 390 | + var olddiv = document.getElementById(div_id); |
| 391 | + var parent = olddiv.parentNode; |
| 392 | + parent.removeChild(olddiv); |
| 393 | +} |
| 394 | +</script> |
| 395 | + |
| 396 | +EOT; |
| 397 | + |
| 398 | + $wgOut->addScript( $javascript_text ); |
| 399 | + |
| 400 | + if ( !$smwgJQueryIncluded ) { |
| 401 | + $realFunction = array( 'OutputPage', 'includeJQuery' ); |
| 402 | + if ( is_callable( $realFunction ) ) { |
| 403 | + $wgOut->includeJQuery(); |
| 404 | + } else { |
| 405 | + $scripts[] = "$smwgScriptPath/libs/jquery-1.4.2.min.js"; |
| 406 | + } |
| 407 | + |
| 408 | + $smwgJQueryIncluded = true; |
| 409 | + } |
| 410 | + } |
| 411 | + return $result; |
| 412 | + } |
| 413 | + |
| 414 | + protected function processSortingFormBox( WebRequest $wgRequest ) { |
| 415 | + $order_values = $wgRequest->getArray( 'order' ); |
| 416 | + |
| 417 | + if ( is_array( $order_values ) ) { |
| 418 | + $params['order'] = ''; |
| 419 | + |
| 420 | + foreach ( $order_values as $order_value ) { |
| 421 | + if ( $order_value == '' ) $order_value = 'ASC'; |
| 422 | + $params['order'] .= ( $params['order'] != '' ? ',' : '' ) . $order_value; |
| 423 | + } |
| 424 | + return $params; |
| 425 | + } |
| 426 | + return array(); |
| 427 | + } |
| 428 | + |
| 429 | + /** |
| 430 | + * Generates the form element(s) for PrintOuts. |
288 | 431 | * Use its complement processPOFormBox() to decode data sent through these |
289 | 432 | * form elements. UIs may overload both to change the form parameter or the html elements. |
290 | 433 | * |
— | — | @@ -340,7 +483,7 @@ |
341 | 484 | } |
342 | 485 | |
343 | 486 | /** |
344 | | - * A method which decodes form data sent through form-elements generated by |
| 487 | + * Decodes form data sent through form-elements generated by |
345 | 488 | * its complement, getPOFormBox(). UIs may overload both to change form parameters. |
346 | 489 | * |
347 | 490 | * @param WebRequest $wgRequest |
— | — | @@ -367,7 +510,7 @@ |
368 | 511 | } |
369 | 512 | |
370 | 513 | /** |
371 | | - * A method which generates the url parameters based on passed parameters. |
| 514 | + * Generates the url parameters based on passed parameters. |
372 | 515 | * UI implementations need to overload this if they use different form parameters. |
373 | 516 | * |
374 | 517 | * @return string An url-encoded string. |
— | — | @@ -395,7 +538,7 @@ |
396 | 539 | } |
397 | 540 | |
398 | 541 | /** |
399 | | - * Display a form section showing the options for a given format, |
| 542 | + * Displays a form section showing the options for a given format, |
400 | 543 | * based on the getParameters() value for that format's query printer. |
401 | 544 | * |
402 | 545 | * @param string $format |
— | — | @@ -675,8 +818,8 @@ |
676 | 819 | * @return boolean |
677 | 820 | */ |
678 | 821 | protected function usesNavigationBar() { |
679 | | - //hide if no results are found |
680 | | - if($this->m_ui_helper->getResultCount()==0) return false; |
| 822 | + // hide if no results are found |
| 823 | + if ( $this->m_ui_helper->getResultCount() == 0 ) return false; |
681 | 824 | else return true; |
682 | 825 | } |
683 | 826 | |
— | — | @@ -709,7 +852,7 @@ |
710 | 853 | * Various parameters passed by the user which control the format, limit, offset. |
711 | 854 | * @var array of strings |
712 | 855 | */ |
713 | | - protected $parameters = array(); |
| 856 | + protected $parameters = array(); |
714 | 857 | |
715 | 858 | /** |
716 | 859 | * The additional columns to be displayed with results |
— | — | @@ -719,7 +862,7 @@ |
720 | 863 | |
721 | 864 | /** |
722 | 865 | * The The additional columns to be displayed with results in '?property' form |
723 | | - * |
| 866 | + * |
724 | 867 | * @var array of strings |
725 | 868 | */ |
726 | 869 | protected $printOutStrings = array(); |
— | — | @@ -745,6 +888,11 @@ |
746 | 889 | private $errors = array(); |
747 | 890 | |
748 | 891 | /** |
| 892 | + * The default result printer if no format is set at the higher level |
| 893 | + */ |
| 894 | + private $defaultResultPrinter = 'broadtable'; |
| 895 | + |
| 896 | + /** |
749 | 897 | * The Query Result, if it has been fetched. |
750 | 898 | * |
751 | 899 | * @var SMWQueryResult |
— | — | @@ -760,7 +908,7 @@ |
761 | 909 | |
762 | 910 | /** |
763 | 911 | * A list of Query UIs |
764 | | - * |
| 912 | + * |
765 | 913 | * @var array of SpecialPage |
766 | 914 | */ |
767 | 915 | protected static $uiPages = array(); // A list of Query UIs |
— | — | @@ -769,7 +917,7 @@ |
770 | 918 | * Although this constructor is publicly accessible, its use is discouraged. |
771 | 919 | * Instantiation can instead be done by the makeForInfoLink() to handle infolink |
772 | 920 | * requests or makeForUI() to handle requests from a Query form. |
773 | | - * |
| 921 | + * |
774 | 922 | * @param mixed $context SMWQueryUIHelper::SPECIAL_PAGE | SMWQueryUIHelper::WIKI_LINK |
775 | 923 | */ |
776 | 924 | public function __construct( $context = SMWQueryUIHelper::SPECIAL_PAGE ) { |
— | — | @@ -815,11 +963,11 @@ |
816 | 964 | |
817 | 965 | /** |
818 | 966 | * Would there be more query results that were not shown due to a limit? |
819 | | - * |
| 967 | + * |
820 | 968 | * @return boolean |
821 | 969 | */ |
822 | 970 | public function hasFurtherResults() { |
823 | | - if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { //The queryResult may not be set |
| 971 | + if ( is_a( $this->queryResult, 'SMWQueryResult' ) ) { // The queryResult may not be set |
824 | 972 | return $this->queryResult->hasFurtherResults(); |
825 | 973 | } |
826 | 974 | else { |
— | — | @@ -834,12 +982,12 @@ |
835 | 983 | */ |
836 | 984 | public function getResultObject() { |
837 | 985 | return $this->queryResult; |
838 | | - //TODO: see if this method can be removed. |
| 986 | + // TODO: see if this method can be removed. |
839 | 987 | } |
840 | 988 | |
841 | 989 | /** |
842 | 990 | * Returns an array of errors, if any have occured. |
843 | | - * |
| 991 | + * |
844 | 992 | * @return array of strings |
845 | 993 | */ |
846 | 994 | public function getErrors() { |
— | — | @@ -920,7 +1068,7 @@ |
921 | 1069 | /* |
922 | 1070 | * Note: property validation is not very clearly defined yet, so validation is disabled by default |
923 | 1071 | */ |
924 | | - |
| 1072 | + |
925 | 1073 | $errors = array(); |
926 | 1074 | if ( $enable_validation ) { |
927 | 1075 | foreach ( $print_outs as $key => $prop ) { |
— | — | @@ -959,10 +1107,8 @@ |
960 | 1108 | $errors = array(); |
961 | 1109 | |
962 | 1110 | // checking for missing parameters and adding them |
963 | | - if ( !array_key_exists( 'format', $params ) ) |
964 | | - $params[ 'format' ] = $this->getDefaultResultPrinter (); |
965 | | - if ( !array_key_exists( 'order', $params ) ) |
966 | | - $params[ 'order' ] = ''; |
| 1111 | + if ( !array_key_exists( 'format', $params ) or ! array_key_exists ( $params['format'], $smwgResultFormats ) ) |
| 1112 | + $params[ 'format' ] = $this->defaultResultPrinter; |
967 | 1113 | if ( !array_key_exists( 'limit', $params ) ) |
968 | 1114 | $params[ 'limit' ] = 20; |
969 | 1115 | $params[ 'limit' ] = min( $params[ 'limit' ], $smwgQMaxInlineLimit ); |
— | — | @@ -1096,14 +1242,10 @@ |
1097 | 1243 | } |
1098 | 1244 | return $result; |
1099 | 1245 | } |
1100 | | - |
1101 | | - /** |
1102 | | - * $queryString, $parameters, $printOuts are set, returns the relevant #ask query |
1103 | | - */ |
1104 | 1246 | |
1105 | 1247 | /** |
1106 | 1248 | * Returns the query in the #ask format |
1107 | | - * |
| 1249 | + * |
1108 | 1250 | * @return string |
1109 | 1251 | */ |
1110 | 1252 | public function getAsk() { |
— | — | @@ -1129,7 +1271,7 @@ |
1130 | 1272 | |
1131 | 1273 | /** |
1132 | 1274 | * Returns number of available results. |
1133 | | - * |
| 1275 | + * |
1134 | 1276 | * @return int |
1135 | 1277 | */ |
1136 | 1278 | public function getResultCount() { |
— | — | @@ -1142,7 +1284,7 @@ |
1143 | 1285 | |
1144 | 1286 | /** |
1145 | 1287 | * Retuens the param array |
1146 | | - * |
| 1288 | + * |
1147 | 1289 | * @return array |
1148 | 1290 | */ |
1149 | 1291 | public function getParams() { |
— | — | @@ -1179,6 +1321,7 @@ |
1180 | 1322 | $result->execute(); |
1181 | 1323 | return $result; |
1182 | 1324 | } |
| 1325 | + |
1183 | 1326 | /** |
1184 | 1327 | * Constructs a new SMWQueryUIHelper when arguments are extracted from the UI |
1185 | 1328 | * |
— | — | @@ -1199,9 +1342,10 @@ |
1200 | 1343 | // $result->execute(); |
1201 | 1344 | return $result; |
1202 | 1345 | } |
| 1346 | + |
1203 | 1347 | /** |
1204 | 1348 | * Checks if $property exists in the wiki or not |
1205 | | - * |
| 1349 | + * |
1206 | 1350 | * @return bool |
1207 | 1351 | */ |
1208 | 1352 | protected static function validateProperty( $property ) { |
— | — | @@ -1217,16 +1361,5 @@ |
1218 | 1362 | return false; |
1219 | 1363 | } |
1220 | 1364 | } |
1221 | | - |
1222 | | - /** |
1223 | | - * Returns the result printer which should be used if not specified by the user. |
1224 | | - * |
1225 | | - * Overload if necessary. |
1226 | | - * |
1227 | | - * @return string |
1228 | | - */ |
1229 | | - protected static function getDefaultResultPrinter() { |
1230 | | - return 'broadtable'; |
1231 | | - } |
1232 | | - |
| 1365 | + |
1233 | 1366 | } |
Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialQueryCreator.php |
— | — | @@ -40,7 +40,7 @@ |
41 | 41 | */ |
42 | 42 | public function execute( $p ) { |
43 | 43 | global $wgOut, $wgRequest, $smwgQEnabled, $smwgIgnoreQueryErrors; |
44 | | - $smwgIgnoreQueryErrors= false; |
| 44 | + $smwgIgnoreQueryErrors = false; |
45 | 45 | $this->setHeaders(); |
46 | 46 | |
47 | 47 | if ( !$smwgQEnabled ) { |
— | — | @@ -116,7 +116,11 @@ |
117 | 117 | $result .= $this->addSortingOptions( $result ); |
118 | 118 | } |
119 | 119 | $result .= "<br><br>" . $this->getFormatSelectBox( 'broadtable' ); |
120 | | - $result .= '</div>'; |
| 120 | + |
| 121 | + if ( $this->m_ui_helper->getQueryString() != '' ) // hide #ask if there isnt any query defined |
| 122 | + $result .= $this->getAskEmbedBox(); |
| 123 | + |
| 124 | + $result .= '</div>'; // end of hidden additional options |
121 | 125 | $result .= '<br /><input type="submit" value="' . wfMsg( 'smw_ask_submit' ) . '"/>' . |
122 | 126 | '<input type="hidden" name="eq" value="no"/>' . |
123 | 127 | "\n</form>"; |