r94116 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94115‎ | r94116 | r94117 >
Date:15:44, 9 August 2011
Author:devayon
Status:deferred
Tags:
Comment:
added control for category options
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialQueryCreator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialQueryCreator.php
@@ -136,7 +136,25 @@
137137 $params = array();
138138 $order_values = $wgRequest->getArray( 'order' );
139139 $property_values = $wgRequest->getArray( 'property' );
 140+ $category_values = $wgRequest->getArray( 'category' );
 141+ $category_label_values = $wgRequest->getArray( 'cat_label' );
140142 $po = array();
 143+ if ( is_array( $category_values ) ) {
 144+ foreach ( $category_values as $key => $value ) {
 145+ if ( trim( $value ) == '' ) {
 146+ $po[$key] = '?Category'; // Todo: i18n
 147+ } else {
 148+ $po[$key] = '?Category:' . $value; // Todo: i18n
 149+ }
 150+ }
 151+ }
 152+ if ( is_array( $category_label_values ) ) {
 153+ foreach ( $category_label_values as $key => $value ) {
 154+ if ( trim( $value ) != '' ) {
 155+ $po[$key] .= ' = ' . $value;
 156+ }
 157+ }
 158+ }
141159 if ( is_array( $property_values ) ) {
142160 $params['sort'] = '';
143161 $params['order'] = '';
@@ -154,21 +172,25 @@
155173 $params['order'] .= ( $params['order'] != '' ? ',':'' ) . $order_values[$key];
156174 }
157175 }
 176+ if ( $params['sort'] == '' ) {
 177+ unset ( $params['sort'] );
 178+ }
 179+ if ( $params['order'] == '' ) {
 180+ unset ( $params['order'] );
 181+ }
158182 $display_values = $wgRequest->getArray( 'display' );
159183 if ( is_array( $display_values ) ) {
160184 foreach ( $display_values as $key => $value ) {
161185 if ( $value == '1' and array_key_exists( $key, $property_values ) ) {
162186 $property_values[$key] = trim( $property_values[$key] );
163187 $property_values[$key] = ( $property_values[$key][0] == '?' ) ? $property_values[$key]:'?' . $property_values[$key];
164 - $po[] = $property_values[$key];
 188+ $po[$key] = $property_values[$key];
165189 }
166 -
167190 }
168191 }
169192 }
170 -
 193+ ksort( $po );
171194 $params = array_merge( $params, $po );
172 -
173195 return $params;
174196
175197 }
@@ -197,6 +219,9 @@
198220 $property_values = $wgRequest->getArray( 'property' );
199221 $order_values = $wgRequest->getArray( 'order' );
200222 $display_values = $wgRequest->getArray( 'display' );
 223+ $category_values = $wgRequest->getArray( 'category' );
 224+ $category_label_values = $wgRequest->getArray( 'cat_label' );
 225+
201226 if ( is_array( $property_values ) ) {
202227 // removing empty values
203228 foreach ( $property_values as $key => $property_value ) {
@@ -247,52 +272,81 @@
248273 }
249274 }
250275 }
251 - $num_sort_values = count( $property_values );
252 - foreach ( $property_values as $i => $property_value ) {
 276+ $i = 0;
 277+ $additional_POs = array();
 278+ if ( is_array( $property_values ) ) {
 279+ $additional_POs = array_merge( $additional_POs, $property_values );
 280+ }
 281+ if ( is_array( $category_values ) ) {// same as testing $category_label_values
 282+ $additional_POs = array_merge( $additional_POs, $category_values );
 283+ }
 284+ ksort( $additional_POs );
 285+ foreach ( $additional_POs as $key => $value ) {
 286+ if ( array_key_exists( $key, $property_values ) ) {
 287+ // make a element for additional properties
 288+ $result .= Html::openElement( 'div', array( 'id' => "sort_div_$i", 'class' => 'smw-sort' ) );
 289+ $result .= '<span class="smw-remove"><a href="javascript:removePOInstance(\'sort_div_' . $i . '\')"><img src="' . $smwgScriptPath . '/skins/images/close-button.png" alt="' . wfMsg( 'smw_qui_delete' ) . '"></a></span>';
 290+ $result .= wfMsg( 'smw_qui_property' );
 291+ $result .= Html::input( 'property[' . $i . ']', $property_values[$key], 'text', array( 'size' => '35', 'id' => "property$i" ) ) . "\n";
 292+ $result .= Html::openElement( 'select', array( 'name' => "order[$i]" ) );
253293
254 - $result .= Html::openElement( 'div', array( 'id' => "sort_div_$i", 'class' => 'smw-sort' ) );
255 - $result .= '<span class="smw-remove"><a href="javascript:removePOInstance(\'sort_div_' . $i . '\')"><img src="' . $smwgScriptPath . '/skins/images/close-button.png" alt="' . wfMsg( 'smw_qui_delete' ) . '"></a></span>';
256 - $result .= wfMsg( 'smw_qui_property' );
257 - $result .= Html::input( 'property[' . $i . ']', $property_value, 'text', array( 'size' => '35', 'id' => "property$i" ) ) . "\n";
258 - $result .= Html::openElement( 'select', array( 'name' => "order[$i]" ) );
 294+ $if1 = ( !is_array( $order_values ) or !array_key_exists( $key, $order_values ) or $order_values[$key] == 'NONE' );
 295+ $result .= Xml::option( wfMsg( 'smw_qui_nosort' ), "NONE", $if1 );
259296
260 - $if1 = ( !is_array( $order_values ) or !array_key_exists( $i, $order_values ) or $order_values[$i] == 'NONE' );
261 - $result .= Xml::option( wfMsg( 'smw_qui_nosort' ), "NONE", $if1 );
 297+ $if2 = ( is_array( $order_values ) and array_key_exists( $key, $order_values ) and $order_values[$key] == 'ASC' );
 298+ $result .= Xml::option( wfMsg( 'smw_qui_ascorder' ), "ASC", $if2 );
262299
263 - $if2 = ( is_array( $order_values ) and array_key_exists( $i, $order_values ) and $order_values[$i] == 'ASC' );
264 - $result .= Xml::option( wfMsg( 'smw_qui_ascorder' ), "ASC", $if2 );
 300+ $if3 = ( is_array( $order_values ) and array_key_exists( $key, $order_values ) and $order_values[$key] == 'DESC' );
 301+ $result .= Xml::option( wfMsg( 'smw_qui_descorder' ), "DESC", $if3 );
265302
266 - $if3 = ( is_array( $order_values ) and array_key_exists( $i, $order_values ) and $order_values[$i] == 'DESC' );
267 - $result .= Xml::option( wfMsg( 'smw_qui_descorder' ), "DESC", $if3 );
 303+ $result .= Xml::closeElement( 'select' );
268304
269 - $result .= Xml::closeElement( 'select' );
 305+ $if4 = ( is_array( $display_values ) and array_key_exists( $key, $display_values ) );
 306+ $result .= Xml::checkLabel( wfMsg( 'smw_qui_shownresults' ), "display[$i]", "display$i", $if4 );
 307+ $result .= ' <a id="more' . $i . '" "class="smwq-more" href="javascript:smw_makePropDialog(\'' . $i . '\')"> options </a> '; // TODO: i18n
270308
271 - $if4 = ( is_array( $display_values ) and array_key_exists( $i, $display_values ) );
272 - $result .= Xml::checkLabel( wfMsg( 'smw_qui_shownresults' ), "display[$i]", "display$i", $if4 );
273 - $result .= ' <a id="more' . $i . '" "class="smwq-more" href="javascript:smw_makeDialog(\'' . $i . '\')"> options </a> '; // TODO: i18n
274 -
275 - $result .= Xml::closeElement( 'div' );
 309+ $result .= Xml::closeElement( 'div' );
 310+ $i++;
 311+ }
 312+ if ( is_array( $category_values ) and array_key_exists( $key, $category_values ) ) {
 313+ $result .= Html::openElement( 'div', array( 'id' => "sort_div_$i", 'class' => 'smw-sort' ) );
 314+ $result .= '<span class="smw-remove"><a href="javascript:removePOInstance(\'sort_div_' . $i . '\')"><img src="' . $smwgScriptPath . '/skins/images/close-button.png" alt="' . wfMsg( 'smw_qui_delete' ) . '"></a></span>' .
 315+ 'Category (optional)' . // todo: i18n
 316+ Xml::input( "category[$i]", '20', $category_values[$key] ) . " " .
 317+ 'Label' . // todo: i18n
 318+ Xml::input( "cat_label[$i]", '20', array_key_exists( $key, $category_label_values ) ? $category_label_values[$key]:false ) . " " .
 319+ ' <a id="more' . $i . '" "class="smwq-more" href="javascript:smw_makeCatDialog(\'' . $i . '\')"> options </a> ' . // TODO: i18n
 320+ Xml::closeElement( 'div' );
 321+ $i++;
 322+ }
276323 }
 324+ $num_sort_values = $i;
277325 // END: create form elements already submitted earlier via form
278326
279327 // create hidden form elements to be cloned later
280 - $hidden = Html::openElement( 'div', array( 'id' => 'sorting_starter', 'class' => 'smw-sort', 'style' => 'display:none' ) ) .
 328+ $hiddenproperty = Html::openElement( 'div', array( 'id' => 'property_starter', 'class' => 'smw-sort', 'style' => 'display:none' ) ) .
281329 '<span class="smw-remove"><a><img src="' . $smwgScriptPath . '/skins/images/close-button.png" alt="' . wfMsg( 'smw_qui_delete' ) . '"></a></span>' .
282330 wfMsg( 'smw_qui_property' ) .
283 - Xml::input( "property_num", '35' ) . " ";
 331+ Xml::input( "property_num", '35' ) . " " .
 332+ Html::openElement( 'select', array( 'name' => 'order_num' ) ) .
 333+ Xml::option( wfMsg( 'smw_qui_nosort' ), 'NONE' ) .
 334+ Xml::option( wfMsg( 'smw_qui_ascorder' ), 'ASC' ) .
 335+ Xml::option( wfMsg( 'smw_qui_descorder' ), 'DESC' ) .
 336+ Xml::closeElement( 'select' ) .
 337+ Xml::checkLabel( wfMsg( 'smw_qui_shownresults' ), "display_num", '', true ) .
 338+ Xml::closeElement( 'div' );
 339+ $hiddenproperty = json_encode( $hiddenproperty );
284340
285 - $hidden .= Html::openElement( 'select', array( 'name' => 'order_num' ) );
286 - $hidden .= Xml::option( wfMsg( 'smw_qui_nosort' ), 'NONE' );
287 - $hidden .= Xml::option( wfMsg( 'smw_qui_ascorder' ), 'ASC' );
288 - $hidden .= Xml::option( wfMsg( 'smw_qui_descorder' ), 'DESC' );
289 - $hidden .= Xml::closeElement( 'select' );
 341+ $hiddencategory = Html::openElement( 'div', array( 'id' => 'category_starter', 'class' => 'smw-sort', 'style' => 'display:none' ) ) .
 342+ '<span class="smw-remove"><a><img src="' . $smwgScriptPath . '/skins/images/close-button.png" alt="' . wfMsg( 'smw_qui_delete' ) . '"></a></span>' .
 343+ 'Category (optional)' . // todo: i18n
 344+ Xml::input( "category_num", '20' ) . " " .
 345+ 'Label' . // todo: i18n
 346+ Xml::input( "cat_label_num", '20' ) . " " .
 347+ Xml::closeElement( 'div' );
 348+ $hiddencategory = json_encode( $hiddencategory );
290349
291 - $hidden .= Xml::checkLabel( wfMsg( 'smw_qui_shownresults' ), "display_num", '', true );
292 - $hidden .= Xml::closeElement( 'div' );
293 -
294 - $hidden = json_encode( $hidden );
295 -
296 - $dialogbox = Xml::openElement( 'div', array( 'id' => 'dialog', 'title' => 'Property Options', 'class' => 'smw-prop-dialog' ) ) . // todo i18n
 350+ $propertydialogbox = Xml::openElement( 'div', array( 'id' => 'prop-dialog', 'title' => 'Property Options', 'class' => 'smw-prop-dialog' ) ) . // todo i18n
297351 Xml::inputLabel( 'Property:', '', 'd-property', 'd-property' ) . '<br/>' . // todo i18n
298352 Xml::inputLabel( 'Label:', '', 'd-property-label', 'd-property-label' ) . '<br/>' . // todo i18n
299353 'Format: ' . Html::openElement( 'select', array( 'name' => 'd-format', 'id' => 'd-format' ) ) . // todo i18n
@@ -303,9 +357,18 @@
304358 // Xml::inputLabel( 'Limit:', 'd-property-limit', 'd-property-limit' ) . '<br/>' . // todo i18n
305359 '<input type="hidden" name="d-property-code" id="d-property-code">' .
306360 Xml::closeElement( 'div' );
 361+ $categorydialogbox = Xml::openElement( 'div', array( 'id' => 'cat-dialog', 'title' => 'Category Options', 'class' => 'smw-cat-dialog' ) ) . // todo i18n
 362+ Xml::inputLabel( 'Category:', '', 'd-category', 'd-category' ) . '<br/>' . // todo i18n
 363+ Xml::inputLabel( 'Label:', '', 'd-category-label', 'd-category-label' ) . '<br/>' . // todo i18n
 364+ Xml::inputLabel( 'Yes:', '', 'd-category-yes', 'd-category-yes' ) . '<br/>' . // todo i18n
 365+ Xml::inputLabel( 'No:', '', 'd-category-no', 'd-category-no' ) . '<br/>' . // todo i18n
 366+ 'Code :<input type="text" name="d-category-code" id="d-category-code">' . // todo hide
 367+ Xml::closeElement( 'div' );
307368
308369 $result .= '<div id="sorting_main"></div>' . "\n";
309 - $result .= '[<a href="javascript:addPOInstance(\'sorting_starter\', \'sorting_main\')">' . wfMsg( 'smw_qui_addnprop' ) . '</a>]' . "\n";
 370+ $result .= '[<a href="javascript:smw_addPropertyInstance(\'property_starter\', \'sorting_main\')">' . wfMsg( 'smw_qui_addnprop' ) . '</a>]' .
 371+ '[<a href="javascript:smw_addCategoryInstance(\'category_starter\', \'sorting_main\')">' . 'Add additional category' . '</a>]' . // todo i18n
 372+ "\n";
310373
311374 // Javascript code for handling adding and removing the "sort" inputs
312375 if ( $enableAutocomplete == SMWQueryUI::ENABLE_AUTO_SUGGEST ) {
@@ -334,11 +397,28 @@
335398 }
336399 });
337400 }
 401+function smw_category_autocomplete(){
 402+ jQuery('[name*="category"]').autocomplete({
 403+ minLength: 2,
 404+ source: function(request, response) {
 405+ url=wgScriptPath+'/api.php?action=opensearch&limit=10&namespace='+wgNamespaceIds['category']+'&format=jsonfm';
 406+
 407+ jQuery.getJSON(url, 'search='+request.term, function(data){
 408+ //remove the namespace prefix 'Property:' from returned data
 409+ for(i=0;i<data[1].length;i++) data[1][i]=data[1][i].substr(data[1][i].indexOf(':')+1);
 410+ response(data[1]);
 411+ });
 412+
 413+ }
 414+ });
 415+}
338416 EOT;
339417 } else {
340418 $javascript_text .= <<<EOT
341419 function smw_property_autocomplete(){
342420 }
 421+function smw_category_autocomplete(){
 422+}
343423 EOT;
344424 }
345425
@@ -355,8 +435,8 @@
356436 \$j('#d-property-code')[0].value= code;
357437 }
358438 }
359 -function smw_makeDialog(prop_id){
360 - jQuery('#dialog input').attr('value','');
 439+function smw_makePropDialog(prop_id){
 440+ jQuery('#prop-dialog input').attr('value','');
361441 prop=val=\$j('#property'+prop_id)[0].value;
362442 if(val[0]='?') val=prop=prop.substr(1);
363443 if((i=val.indexOf('='))!=-1) prop=prop.substring(0, i);
@@ -376,11 +456,12 @@
377457 \$j('#d-property').attr('value', prop.trim());
378458 \$j('#d-property-label').attr('value', label);
379459 \$j('#d-property-format-custom').attr('value', format.trim());
380 - \$j('#dialog').dialog.id=prop_id;
381 - \$j('#dialog').dialog('open');
 460+ \$j('#prop-dialog').dialog.id=prop_id;
 461+ \$j('#prop-dialog').dialog('open');
382462 }
383463 // code for handling adding and removing the "sort" inputs
384 -function addPOInstance(starter_div_id, main_div_id) {
 464+
 465+function smw_addPropertyInstance(starter_div_id, main_div_id) {
385466 var starter_div = document.getElementById(starter_div_id);
386467 var main_div = document.getElementById(main_div_id);
387468
@@ -402,7 +483,7 @@
403484
404485 //Create 'more' link
405486 var more_button =document.createElement('span');
406 - more_button.innerHTML = ' <a class="smwq-more" href="javascript:smw_makeDialog(\'' + num_elements + '\')">options</a> '; //TODO: i18n
 487+ more_button.innerHTML = ' <a class="smwq-more" href="javascript:smw_makePropDialog(\'' + num_elements + '\')">options</a> '; //TODO: i18n
407488 more_button.id = 'more'+num_elements;
408489 new_div.appendChild(more_button);
409490
@@ -416,6 +497,42 @@
417498 smw_property_autocomplete();
418499 }
419500
 501+function smw_addCategoryInstance(starter_div_id, main_div_id) {
 502+ var starter_div = document.getElementById(starter_div_id);
 503+ var main_div = document.getElementById(main_div_id);
 504+
 505+ //Create the new instance
 506+ var new_div = starter_div.cloneNode(true);
 507+ var div_id = 'sort_div_' + num_elements;
 508+ new_div.id = div_id;
 509+ new_div.style.display = 'block';
 510+ jQuery(new_div.getElementsByTagName('label')).attr('for', 'display'+num_elements);
 511+ var children = new_div.getElementsByTagName('*');
 512+ var x;
 513+ for (x = 0; x < children.length; x++) {
 514+ if (children[x].for) children[x].for="display"+num_elements;
 515+ if (children[x].name){
 516+ children[x].id = children[x].name.replace(/_num/, ''+num_elements);
 517+ children[x].name = children[x].name.replace(/_num/, '[' + num_elements + ']');
 518+ }
 519+ }
 520+
 521+ //Create 'more' link
 522+ var more_button =document.createElement('span');
 523+ more_button.innerHTML = ' <a class="smwq-more" href="javascript:smw_makeCatDialog(\'' + num_elements + '\')">options</a> '; //TODO: i18n
 524+ more_button.id = 'more'+num_elements;
 525+ new_div.appendChild(more_button);
 526+
 527+ //Add the new instance
 528+ main_div.appendChild(new_div);
 529+
 530+ // initialize delete button
 531+ st='sort_div_'+num_elements;
 532+ jQuery('#'+new_div.id).find(".smw-remove a")[0].href="javascript:removePOInstance('"+st+"')";
 533+ num_elements++;
 534+ smw_category_autocomplete();
 535+}
 536+
420537 function removePOInstance(div_id) {
421538 var olddiv = document.getElementById(div_id);
422539 var parent = olddiv.parentNode;
@@ -423,35 +540,46 @@
424541 }
425542
426543 jQuery(function(){
427 - jQuery('$hidden').appendTo(document.body);
428 - jQuery('$dialogbox').appendTo(document.body);
429 - jQuery('#dialog').dialog({
 544+ jQuery('$hiddenproperty').appendTo(document.body);
 545+ jQuery('$hiddencategory').appendTo(document.body);
 546+ jQuery('$propertydialogbox').appendTo(document.body);
 547+ jQuery('$categorydialogbox').appendTo(document.body);
 548+ jQuery('#cat-dialog').dialog({
430549 autoOpen: false,
431550 modal: true,
432551 resizable: true,
433552 minHeight: 200,
434553 minWidth: 400,
 554+ });
 555+ jQuery('#prop-dialog').dialog({
 556+ autoOpen: false,
 557+ modal: true,
 558+ resizable: true,
 559+ minHeight: 200,
 560+ minWidth: 400,
435561 buttons: {
436562 "Ok": function(){ //todo: i18n
437563 smw_prop_code_update();
438 - \$j('#property'+\$j('#dialog').dialog.id)[0].value=\$j('#d-property-code')[0].value;
 564+ \$j('#property'+\$j('#prop-dialog').dialog.id)[0].value=\$j('#d-property-code')[0].value;
439565 jQuery(this).dialog("close");
440566 },
441567 "Cancel": function(){ //todo: i18n
442 - jQuery('#dialog input').attr('value','');
 568+ jQuery('#prop-dialog input').attr('value','');
443569 jQuery(this).dialog("close");
444570 }
445571 }
446572 });
447 - jQuery('#sort-more').click(function(){jQuery('#dialog').dialog("open");});
448 - jQuery('#dialog input').bind('keyup click', smw_prop_code_update );
 573+ jQuery('#sort-more').click(function(){jQuery('#prop-dialog').dialog("open");});
 574+ jQuery('#prop-dialog input').bind('keyup click', smw_prop_code_update );
449575
450576 jQuery('#d-format').bind('change', function(event){
451577 jQuery('#d-property-format-custom').attr('value', jQuery('#d-format').attr('value'));
452578 smw_prop_code_update();
453579 });
454580 });
455 -
 581+function smw_makeCatDialog(cat_id){
 582+ \\\$j('#cat-dialog').dialog('open');
 583+}
456584 jQuery(document).ready(smw_property_autocomplete);
457585 </script>
458586

Follow-up revisions

RevisionCommit summaryAuthorDate
r94126follow up: r94116devayon17:03, 9 August 2011
r94231follow-up: r94116devayon07:22, 11 August 2011
r94336follow-up: r94233, r94116.devayon10:31, 12 August 2011
r94342follow-up:r94340, r94116devayon12:41, 12 August 2011