| Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialAsk.php |
| — | — | @@ -692,7 +692,7 @@ |
| 693 | 693 | * based on the getParameters() value for that format's query printer. |
| 694 | 694 | * |
| 695 | 695 | * @param string $format |
| 696 | | - * @param array $paramValues |
| | 696 | + * @param array $paramValues The current values for the parameters (name => value) |
| 697 | 697 | * |
| 698 | 698 | * @return string |
| 699 | 699 | */ |
| — | — | @@ -705,40 +705,50 @@ |
| 706 | 706 | |
| 707 | 707 | $optionsHtml = array(); |
| 708 | 708 | |
| 709 | | - foreach ( $params as $i => $param ) { |
| 710 | | - $param_name = $param['name']; |
| 711 | | - $type = $param['type']; |
| 712 | | - $desc = $param['description']; |
| | 709 | + foreach ( $params as $param ) { |
| | 710 | + $param = $this->toValidatorParam( $param ); |
| | 711 | + $currentValue = array_key_exists( $param->getName(), $paramValues ) ? $paramValues[$param->getName()] : false; |
| 713 | 712 | |
| 714 | | - $cur_value = ( array_key_exists( $param_name, $paramValues ) ) ? $paramValues[$param_name] : ''; |
| 715 | | - |
| 716 | | - //$optionsHtml[] = $optionsHtml[$i] = "<div style=\"width: 30%; padding: 5px; float: left;\">$param_name:\n" |
| 717 | | - // . $this->showFormatOption( $this->toValidatorParam( $param ) ); |
| 718 | | - |
| 719 | | - // 3 values per row, with alternating colors for rows |
| 720 | | - if ( $i % 3 == 0 ) { |
| 721 | | - $bgcolor = ( $i % 6 ) == 0 ? '#dddddd' : 'white'; |
| 722 | | - $text .= "<div style=\"background: $bgcolor;\">"; |
| 723 | | - } |
| 724 | | - |
| 725 | | - $text .= "<div style=\"width: 30%; padding: 5px; float: left;\">$param_name:\n"; |
| 726 | | - |
| 727 | | - $this->addOptionInput( $text, $type, $param_name, $cur_value, $param, $paramValues ); |
| 728 | | - |
| 729 | | - $text .= "\n <br /><em>$desc</em>\n</div>\n"; |
| 730 | | - |
| 731 | | - if ( $i % 3 == 2 || $i == count( $params ) - 1 ) { |
| 732 | | - $text .= "<div style=\"clear: both\";></div></div>\n"; |
| 733 | | - } |
| | 713 | + $optionsHtml[] = |
| | 714 | + Html::rawElement( |
| | 715 | + 'div', |
| | 716 | + array( |
| | 717 | + 'style' => 'width: 30%; padding: 5px; float: left;' |
| | 718 | + ), |
| | 719 | + htmlspecialchars( $param->getName() ) . ': ' . |
| | 720 | + $this->showFormatOption( $param, $currentValue ) . |
| | 721 | + '<br />' . |
| | 722 | + Html::element( 'em', array(), $param->getDescription() ) |
| | 723 | + ); |
| 734 | 724 | } |
| 735 | | - /* |
| | 725 | + |
| 736 | 726 | for ( $i = 0, $n = count( $optionsHtml ); $i < $n; $i++ ) { |
| 737 | 727 | if ( $i % 3 == 2 || $i == $n - 1 ) { |
| 738 | | - $optionsHtml[$i] .= "<div style=\"clear: both\";></div></div>\n"; |
| 739 | | - } |
| | 728 | + $optionsHtml[$i] .= "<div style=\"clear: both\";></div>\n"; |
| | 729 | + } |
| 740 | 730 | } |
| 741 | | - */ |
| 742 | | - return $text; |
| | 731 | + |
| | 732 | + $i = 0; |
| | 733 | + $rowHtml = ''; |
| | 734 | + $resultHtml = ''; |
| | 735 | + |
| | 736 | + while ( $option = array_shift( $optionsHtml ) ) { |
| | 737 | + $rowHtml .= $option; |
| | 738 | + $i++; |
| | 739 | + |
| | 740 | + if ( $i % 3 == 0 ) { |
| | 741 | + $resultHtml .= Html::rawElement( |
| | 742 | + 'div', |
| | 743 | + array( |
| | 744 | + 'style' => 'background: ' . ( $i % 6 == 0 ? 'white' : '#dddddd' ) . ';' |
| | 745 | + ), |
| | 746 | + $rowHtml |
| | 747 | + ); |
| | 748 | + $rowHtml = ''; |
| | 749 | + } |
| | 750 | + } |
| | 751 | + |
| | 752 | + return $resultHtml; |
| 743 | 753 | } |
| 744 | 754 | |
| 745 | 755 | /** |
| — | — | @@ -747,10 +757,38 @@ |
| 748 | 758 | * |
| 749 | 759 | * @since 1.6 |
| 750 | 760 | * |
| | 761 | + * @param mixed $param |
| | 762 | + * |
| 751 | 763 | * @return Parameter |
| 752 | 764 | */ |
| 753 | | - protected function toValidatorParam() { |
| 754 | | - // TODO |
| | 765 | + protected function toValidatorParam( $param ) { |
| | 766 | + static $typeMap = array( |
| | 767 | + 'int' => Parameter::TYPE_INTEGER |
| | 768 | + ); |
| | 769 | + |
| | 770 | + if ( !( $param instanceof Parameter ) ) { |
| | 771 | + if ( !array_key_exists( 'type', $param ) ) { |
| | 772 | + $param['type'] = 'string'; |
| | 773 | + } |
| | 774 | + |
| | 775 | + $paramClass = $param['type'] == 'enum-list' ? 'ListParameter' : 'Parameter'; |
| | 776 | + $paramType = array_key_exists( $param['type'], $typeMap ) ? $typeMap[$param['type']] : Parameter::TYPE_STRING; |
| | 777 | + |
| | 778 | + $parameter = new $paramClass( $param['name'], $paramType ); |
| | 779 | + |
| | 780 | + if ( array_key_exists( 'description', $param ) ) { |
| | 781 | + $parameter->setDescription( $param['description'] ); |
| | 782 | + } |
| | 783 | + |
| | 784 | + if ( array_key_exists( 'values', $param ) && is_array( $param['values'] ) ) { |
| | 785 | + $parameter->addCriteria( new CriterionInArray( $param['values'] ) ); |
| | 786 | + } |
| | 787 | + |
| | 788 | + return $parameter; |
| | 789 | + } |
| | 790 | + else { |
| | 791 | + return $param; |
| | 792 | + } |
| 755 | 793 | } |
| 756 | 794 | |
| 757 | 795 | /** |
| — | — | @@ -759,76 +797,19 @@ |
| 760 | 798 | * @since 1.6 |
| 761 | 799 | * |
| 762 | 800 | * @param Parameter $parameter |
| | 801 | + * @param mixed $currentValue |
| 763 | 802 | * |
| 764 | 803 | * @return string |
| 765 | 804 | */ |
| 766 | | - protected function showFormatOption( Parameter $parameter ) { |
| 767 | | - // TODO |
| 768 | | - } |
| 769 | | - |
| 770 | | - /** |
| 771 | | - * Adds a an input for a result format parameter to $text. |
| 772 | | - * |
| 773 | | - * @since 1.5.3 |
| 774 | | - * |
| 775 | | - * @param string $text |
| 776 | | - * @param string $type |
| 777 | | - * @param string $param_name |
| 778 | | - * @param string $cur_value |
| 779 | | - * @param array $param |
| 780 | | - * @param array $paramValues |
| 781 | | - */ |
| 782 | | - protected function addOptionInput( &$text, $type, $param_name, $cur_value, array $param, array $paramValues ) { |
| 783 | | - switch ( $type ) { |
| 784 | | - case 'int': |
| 785 | | - $text .= Html::input( |
| 786 | | - "p[$param_name]", |
| 787 | | - $cur_value, |
| 788 | | - 'text', |
| 789 | | - array( |
| 790 | | - 'size' => 6 |
| 791 | | - ) |
| 792 | | - ); |
| 793 | | - break; |
| 794 | | - case 'string': |
| 795 | | - $text .= Html::input( |
| 796 | | - "p[$param_name]", |
| 797 | | - $cur_value, |
| 798 | | - 'text', |
| 799 | | - array( |
| 800 | | - 'size' => 32 |
| 801 | | - ) |
| 802 | | - ); |
| 803 | | - break; |
| 804 | | - case 'enumeration': |
| 805 | | - $text .= '<select name="p[' . htmlspecialchars( $param_name ) . ']">'; |
| 806 | | - $text .= "\n <option value=''></option>\n"; |
| 807 | | - |
| 808 | | - $parts = array(); |
| 809 | | - foreach ( $param['values'] as $value ) { |
| 810 | | - $parts[] = '<option value="' . htmlspecialchars( $value ) . '"' . |
| 811 | | - ( $cur_value == $value ? ' selected' : '' ) . '>' . |
| 812 | | - htmlspecialchars( $value ) . '</option>'; |
| 813 | | - } |
| 814 | | - |
| 815 | | - $text .= implode( "\n", $parts ) . "\n</select>"; |
| 816 | | - break; |
| 817 | | - case 'enum-list': |
| 818 | | - $cur_values = explode( ',', $cur_value ); |
| 819 | | - |
| 820 | | - foreach ( $param['values'] as $val ) { |
| 821 | | - $text .= '<span style="white-space: nowrap; padding-right: 5px;"><input type="checkbox" name="p[' . |
| 822 | | - htmlspecialchars( $param_name ) . '][' . htmlspecialchars( $val ). ']" value="true"' . |
| 823 | | - ( in_array( $val, $cur_values ) ? ' checked' : '' ) . '/> <tt>' . htmlspecialchars( $val ) . "</tt></span>\n"; |
| 824 | | - } |
| 825 | | - break; |
| 826 | | - case 'boolean': |
| 827 | | - $text .= |
| 828 | | - '<input type="checkbox" name="p[' . htmlspecialchars( $param_name ) . ']" value="true" ' . |
| 829 | | - array_key_exists( $param_name, $paramValues ) ? 'checked' : '' . |
| 830 | | - ' />'; |
| 831 | | - break; |
| | 805 | + protected function showFormatOption( Parameter $parameter, $currentValue ) { |
| | 806 | + $input = new ParameterInput( $parameter ); |
| | 807 | + $input->setInputName( 'p[' . $parameter->getName() . ']' ); |
| | 808 | + |
| | 809 | + if ( $currentValue !== false ) { |
| | 810 | + $input->setCurrentValue( $currentValue ); |
| 832 | 811 | } |
| | 812 | + |
| | 813 | + return $input->getHtml(); |
| 833 | 814 | } |
| 834 | 815 | |
| 835 | 816 | } |
| Index: trunk/extensions/SemanticMediaWiki/SemanticMediaWiki.php |
| — | — | @@ -15,4 +15,14 @@ |
| 16 | 16 | * @defgroup SMW Semantic MediaWiki |
| 17 | 17 | */ |
| 18 | 18 | |
| | 19 | +// Include the Validator extension if that hasn't been done yet, since it's required for SMW to work. |
| | 20 | +if ( !defined( 'Validator_VERSION' ) ) { |
| | 21 | + @include_once( dirname( __FILE__ ) . '/../Validator/Validator.php' ); |
| | 22 | +} |
| | 23 | + |
| | 24 | +// Only initialize the extension when all dependencies are present. |
| | 25 | +if ( !defined( 'Validator_VERSION' ) ) { |
| | 26 | + die( '<b>Error:</b> You need to have <a href="http://www.mediawiki.org/wiki/Extension:Validator">Validator</a> installed in order to use <a href="http://www.semantic-mediawiki.org">Semantic MediaWiki</a>.<br />' ); |
| | 27 | +} |
| | 28 | + |
| 19 | 29 | require_once dirname( __FILE__ ) . '/SMW_Settings.php'; |