Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HtmlTable, refs 2387 #2409

Merged
merged 1 commit into from
Apr 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 78 additions & 32 deletions src/MediaWiki/Specials/Browse/ContentsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SMWDataValue as DataValue;
use SMW\DataValues\ValueFormatters\DataValueFormatter;
use SMW\RequestOptions;
use SMW\Utils\HtmlTable;

/**
* @license GNU GPL v2+
Expand Down Expand Up @@ -252,9 +253,9 @@ private function doGenerateHtml() {
private function displayData( SemanticData $data, $left = true, $incoming = false, $isLoading = false ) {
// Some of the CSS classes are different for the left or the right side.
// In this case, there is an "i" after the "smwb-". This is set here.
$ccsPrefix = $left ? 'smwb-' : 'smwb-i';
$dirPrefix = $left ? 'smwb-' : 'smwb-i';

$html = "<div class=\"smw-table {$ccsPrefix}factbox smwb-bottom\">";
$html = HtmlTable::open( array( 'class' => "{$dirPrefix}factbox smwb-bottom" ) );
$noresult = true;

$contextPage = $data->getSubject();
Expand Down Expand Up @@ -282,8 +283,8 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals
continue;
}

$head = "<div class=\"smw-table-cell smwb-cell smwb-prophead\">" . $propertyLabel . "</div>\n";
$body = "<div class=\"smw-table-cell smwb-cell smwb-propval\">\n";
$head = HtmlTable::cell( $propertyLabel, array( "class" => 'smwb-cell smwb-prophead' ) );
$propertyValue = '';

$values = $data->getPropertyValues( $diProperty );

Expand All @@ -299,7 +300,7 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals
if ( $first ) {
$first = false;
} else {
$body .= ', ';
$propertyValue .= ', ';
}

if ( $incoming ) {
Expand All @@ -308,14 +309,14 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals
$dv = DataValueFactory::getInstance()->newDataValueByItem( $di, $diProperty );
}

$body .= "<span class=\"{$ccsPrefix}value\">" .
$propertyValue .= "<span class=\"{$dirPrefix}value\">" .
$this->displayValue( $dvProperty, $dv, $incoming ) . "</span>\n";
}

// Added in 2.3
// link to the remaining incoming pages
if ( $moreIncoming && \Hooks::run( 'SMW::Browse::BeforeIncomingPropertyValuesFurtherLinkCreate', array( $diProperty, $this->subject->getDataItem(), &$body ) ) ) {
$body .= \Html::element(
if ( $moreIncoming && \Hooks::run( 'SMW::Browse::BeforeIncomingPropertyValuesFurtherLinkCreate', array( $diProperty, $this->subject->getDataItem(), &$propertyValue ) ) ) {
$propertyValue .= \Html::element(
'a',
array(
'href' => \SpecialPage::getSafeTitleFor( 'SearchByProperty' )->getLocalURL( array(
Expand All @@ -328,22 +329,37 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals

}

$body .= "</div>\n";
$body = HtmlTable::cell( $propertyValue, array( "class" => 'smwb-cell smwb-propval' ) );

// display row
$html .= "<div class=\"smw-table-row {$ccsPrefix}propvalue\">\n" .
( $left ? ( $head . $body ):( $body . $head ) ) . "</div>\n";
$html .= HtmlTable::row(
( $left ? ( $head . $body ):( $body . $head ) ),
array(
"class" => "{$dirPrefix}propvalue"
)
);
$noresult = false;
} // end foreach properties

if ( $noresult ) {
$noMsgKey = $incoming ? 'smw_browse_no_incoming':'smw_browse_no_outgoing';
$rColumn = "<div class=\"smw-table-cell smwb-cell smwb-prophead\"></div>";
$lColumn = "<div class=\"smw-table-cell smwb-cell smwb-propval\">" . wfMessage( $isLoading ? 'smw-browse-from-backend' : $noMsgKey )->escaped() . "</div>";
$html .= "<div class=\"smw-table-row {$ccsPrefix}propvalue\">" . ( $left ? ( $rColumn . $lColumn ):( $lColumn . $rColumn ) ) . "</div>";
$rColumn = HtmlTable::cell( '', array( "class" => 'smwb-cell smwb-prophead' ) );
$lColumn = HtmlTable::cell(
wfMessage( $isLoading ? 'smw-browse-from-backend' : $noMsgKey )->escaped(),
array(
"class" => 'smwb-cell smwb-propval'
)
);

$html .= HtmlTable::row(
( $left ? ( $rColumn . $lColumn ):( $lColumn . $rColumn ) ),
array(
"class" => "{$dirPrefix}propvalue"
)
);
}

$html .= "</div>\n";
$html .= HtmlTable::close();

return $html;
}
Expand Down Expand Up @@ -375,12 +391,17 @@ private function displayHead() {

$label = ValueFormatter::getFormattedSubject( $this->subject );

$html = "<div class=\"smw-table smwb-factbox\">" .
"<div class=\"smw-table-header smwb-title\"><div class=\"smw-table-row\">" .
$label . "\n" .
"</div></div></div>";

return $html;
return HtmlTable::table(
HtmlTable::row(
$label,
array(
'class' => 'smwb-title'
)
),
array(
'class' => 'smwb-factbox'
)
);
}

/**
Expand Down Expand Up @@ -408,12 +429,19 @@ private function displayCenter( $article ) {
$linkMsg = 'smw_browse_show_incoming';
}

$html = FormHelper::createLink( $linkMsg, $parameters );
$html = FormHelper::createLinkFromMessage( $linkMsg, $parameters );

return "<a name=\"smw_browse_incoming\"></a>\n" .
"<div class=\"smw-table smwb-factbox\">" .
"<div class=\"smw-table-row smwb-center\"><div >" . $html .
"&#160;\n" . "</div></div>\n" . "</div>\n";
return "<a name=\"smw_browse_incoming\"></a>" . HtmlTable::table(
HtmlTable::row(
$html . "&#160;\n",
array(
'class' => 'smwb-center'
)
),
array(
'class' => 'smwb-factbox'
)
);
}

/**
Expand All @@ -429,11 +457,23 @@ private function displayBottom( $more ) {
$open = "<div class=\"smw-table smwb-factbox\">" .
"<div class=\"smw-table-row smwb-center\"><div >";

$close = "&#160;\n" . "</div></div>\n" . "</div>\n";
$html = '';
$open = HtmlTable::open(
array(
'class' => 'smwb-factbox'
)
);

$html = HtmlTable::row(
'&#160;',
array(
'class' => 'smwb-center'
)
);

$close = HtmlTable::close();

if ( $this->getOption( 'showAll' ) ) {
return $open . $close;
return $open . $html . $close;
}

if ( ( $this->offset > 0 ) || $more ) {
Expand All @@ -447,7 +487,7 @@ private function displayBottom( $more ) {

$linkMsg = 'smw_result_prev';

$html .= ( $this->offset == 0 ) ? wfMessage( $linkMsg )->escaped() : FormHelper::createLink( $linkMsg, $parameters );
$html .= ( $this->offset == 0 ) ? wfMessage( $linkMsg )->escaped() : FormHelper::createLinkFromMessage( $linkMsg, $parameters );

$offset = $this->offset + $this->incomingPropertiesCount - 1;

Expand All @@ -459,10 +499,16 @@ private function displayBottom( $more ) {

$linkMsg = 'smw_result_next';

// @todo FIXME: i18n patchwork.
$html .= " &#160;&#160;&#160; <strong>" . wfMessage( 'smw_result_results' )->escaped() . " " . ( $this->offset + 1 ) .
" – " . ( $offset ) . "</strong> &#160;&#160;&#160; ";
$html .= $more ? FormHelper::createLink( $linkMsg, $parameters ) : wfMessage( $linkMsg )->escaped();
$html .= $more ? FormHelper::createLinkFromMessage( $linkMsg, $parameters ) : wfMessage( $linkMsg )->escaped();

$html = HtmlTable::row(
$html,
array(
'class' => 'smwb-center'
)
);
}

return $open . $html . $close;
Expand Down
2 changes: 1 addition & 1 deletion src/MediaWiki/Specials/Browse/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function getQueryForm( $articletext = '' ) {
*
* @return string
*/
public static function createLink( $linkMsg, array $parameters ) {
public static function createLinkFromMessage( $linkMsg, array $parameters ) {

$title = SpecialPage::getSafeTitleFor( 'Browse' );
$fragment = $linkMsg === 'smw_browse_show_incoming' ? '#smw_browse_incoming' : '';
Expand Down
145 changes: 145 additions & 0 deletions src/Utils/HtmlTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace SMW\Utils;

use Html;

/**
* @license GNU GPL v2+
* @since 3.0
*
* @author mwjames
*/
class HtmlTable {

/**
* @since 3.0
*
* @param string $html
* @param array $attributes
*
* @return string
*/
public static function table( $html = '', array $attributes = array() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such static

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, its better than having span and div's being sprinkled all over the code. Nothing wrong on using statics where an instance isn't required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static

Like this you cannot provide an alternative implementation though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly one of those cases where using static is not a total disaster, but still. Esp since you delegate to MediaWiki's Html... I'd be using an instance

return self::open( $attributes ) . $html . self::close();
}

/**
* @since 3.0
*
* @param array $attributes
*
* @return string
*/
public static function open( array $attributes = array() ) {
return Html::openElement(
'div',
self::mergeAttributes( 'smw-table', $attributes ),
''
);
}

/**
* @since 3.0
*
* @param string $html
* @param array $attributes
*
* @return string
*/
public static function header( $html = '', array $attributes = array() ) {
return Html::rawElement(
'div',
self::mergeAttributes( 'smw-table-header', $attributes ),
$html
);
}

/**
* @since 3.0
*
* @param string $html
* @param array $attributes
*
* @return string
*/
public static function body( $html = '', array $attributes = array() ) {
return Html::rawElement(
'div',
self::mergeAttributes( 'smw-table-body', $attributes ),
$html
);
}

/**
* @since 3.0
*
* @param string $html
* @param array $attributes
*
* @return string
*/
public static function footer( $html = '', array $attributes = array() ) {
return Html::rawElement(
'div',
self::mergeAttributes( 'smw-table-footer', $attributes ),
$html
);
}

/**
* @since 3.0
*
* @param string $html
* @param array $attributes
*
* @return string
*/
public static function row( $html = '', array $attributes = array() ) {
return Html::rawElement(
'div',
self::mergeAttributes( 'smw-table-row', $attributes ),
$html
);
}

/**
* @since 3.0
*
* @param string $html
* @param array $attributes
*
* @return string
*/
public static function cell( $html = '', array $attributes = array() ) {
return Html::rawElement(
'div',
self::mergeAttributes( 'smw-table-cell', $attributes ),
$html
);
}

/**
* @since 3.0
*
* @return string
*/
public static function close() {
return Html::closeElement(
'div'
);
}

private static function mergeAttributes( $class, $attributes ) {

if ( isset( $attributes['class'] ) ) {
$class .= ' ' . $attributes['class'];
unset( $attributes['class'] );
}

$attributes['class'] = $class;

return $attributes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testCreateLink() {

$this->assertInternalType(
'string',
FormHelper::createLink( 'Foo', $parameters )
FormHelper::createLinkFromMessage( 'Foo', $parameters )
);
}

Expand Down