00001 <?php
00002
00013 class SMMapPrinter extends SMWResultPrinter {
00014
00020 protected $service;
00021
00027 protected $fatalErrorMsg = false;
00028
00036 public function __construct( $format, $inline, iMappingService $service ) {
00037 $this->service = $service;
00038
00039 parent::__construct( $format, $inline );
00040 $this->useValidator = true;
00041 }
00042
00050 protected function getParameterInfo() {
00051 global $egMapsDefaultLabel, $egMapsDefaultTitle;
00052 global $smgQPForceShow, $smgQPShowTitle, $smgQPTemplate;
00053
00054 $params = MapsMapper::getCommonParameters();
00055 $this->service->addParameterInfo( $params );
00056
00057 $params['zoom']->setDefault( false );
00058 $params['zoom']->setDoManipulationOfDefault( false );
00059
00060 $params['staticlocations'] = new ListParameter( 'staticlocations', ';' );
00061 $params['staticlocations']->addAliases( 'locations' );
00062 $params['staticlocations']->addCriteria( new CriterionIsLocation( '~' ) );
00063 $params['staticlocations']->addManipulations( new MapsParamLocation( '~' ) );
00064 $params['staticlocations']->setDefault( array() );
00065 $params['staticlocations']->setMessage( 'semanticmaps-par-staticlocations' );
00066
00067 $params['centre'] = new Parameter( 'centre' );
00068 $params['centre']->setDefault( false );
00069 $params['centre']->addAliases( 'center' );
00070 $params['centre']->addCriteria( new CriterionIsLocation() );
00071 $params['centre']->setDoManipulationOfDefault( false );
00072 $manipulation = new MapsParamLocation();
00073 $manipulation->toJSONObj = true;
00074 $params['centre']->addManipulations( $manipulation );
00075 $params['centre']->setMessage( 'semanticmaps-par-centre' );
00076
00077 $params['icon'] = new Parameter(
00078 'icon',
00079 Parameter::TYPE_STRING,
00080 '',
00081 array(),
00082 array(
00083 New CriterionNotEmpty()
00084 )
00085 );
00086 $params['icon']->setMessage( 'maps-displaypoints-par-icon' );
00087
00088 $params['forceshow'] = new Parameter(
00089 'forceshow',
00090 Parameter::TYPE_BOOLEAN,
00091 $smgQPForceShow,
00092 array( 'force show' )
00093 );
00094 $params['forceshow']->setMessage( 'semanticmaps-par-forceshow' );
00095
00096 $params['showtitle'] = new Parameter(
00097 'showtitle',
00098 Parameter::TYPE_BOOLEAN,
00099 $smgQPShowTitle,
00100 array( 'show title' )
00101 );
00102 $params['showtitle']->setMessage( 'semanticmaps-par-showtitle' );
00103
00104 $params['template'] = new Parameter(
00105 'template',
00106 Parameter::TYPE_STRING,
00107 $smgQPTemplate,
00108 array(),
00109 array(
00110 New CriterionNotEmpty()
00111 )
00112 );
00113 $params['template']->setDoManipulationOfDefault( false );
00114 $params['template']->setMessage( 'semanticmaps-par-template' );
00115
00116 $params['title'] = new Parameter(
00117 'title',
00118 Parameter::TYPE_STRING,
00119 $egMapsDefaultTitle
00120 );
00121 $params['title']->setMessage( 'maps-displaypoints-par-title' );
00122
00123 $params['label'] = new Parameter(
00124 'label',
00125 Parameter::TYPE_STRING,
00126 $egMapsDefaultLabel,
00127 array( 'text' )
00128 );
00129 $params['label']->setMessage( 'maps-displaypoints-par-label' );
00130
00131 return $params;
00132 }
00133
00142 public final function getResultText( SMWQueryResult $res, $outputmode ) {
00143 if ( $this->fatalErrorMsg === false ) {
00144 global $wgParser;
00145
00146 $params = $this->params;
00147
00148 $queryHandler = new SMQueryHandler( $res, $outputmode );
00149 $queryHandler->setShowSubject( $params['showtitle'] );
00150 $queryHandler->setTemplate( $params['template'] );
00151
00152 $this->handleMarkerData( $params, $queryHandler->getLocations() );
00153 $locationAmount = count( $params['locations'] );
00154
00155 if ( $params['forceshow'] || $locationAmount > 0 ) {
00156
00157
00158 if ( $params['zoom'] === false && $locationAmount <= 1 ) {
00159 $params['zoom'] = $this->service->getDefaultZoom();
00160 }
00161
00162 $mapName = $this->service->getMapId();
00163
00164
00165 if ( version_compare( $GLOBALS['wgVersion'], '1.18', '<' ) ) {
00166 $GLOBALS['egMapsGlobalJSVars'] += $this->service->getConfigVariables();
00167 }
00168
00169 SMWOutputs::requireHeadItem(
00170 $mapName,
00171 $this->service->getDependencyHtml() .
00172 $configVars = Skin::makeVariablesScript( $this->service->getConfigVariables() )
00173 );
00174
00175 foreach ( $this->service->getResourceModules() as $resourceModule ) {
00176 SMWOutputs::requireResource( $resourceModule );
00177 }
00178
00179 $result = $this->getMapHTML( $params, $wgParser, $mapName ) . $this->getJSON( $params, $wgParser, $mapName );
00180
00181 return array(
00182 $result,
00183 'noparse' => true,
00184 'isHTML' => true
00185 );
00186 }
00187 else {
00188 return '';
00189 }
00190 }
00191 else {
00192 return $this->fatalErrorMsg;
00193 }
00194 }
00195
00207 protected function getMapHTML( array $params, Parser $parser, $mapName ) {
00208 return Html::element(
00209 'div',
00210 array(
00211 'id' => $mapName,
00212 'style' => "width: {$params['width']}; height: {$params['height']}; background-color: #cccccc; overflow: hidden;",
00213 ),
00214 wfMsg( 'maps-loading-map' )
00215 );
00216 }
00217
00229 protected function getJSON( array $params, Parser $parser, $mapName ) {
00230 $object = $this->getJSONObject( $params, $parser );
00231
00232 if ( $object === false ) {
00233 return '';
00234 }
00235
00236 return Html::inlineScript(
00237 MapsMapper::getBaseMapJSON( $this->service->getName() )
00238 . "mwmaps.{$this->service->getName()}.{$mapName}=" . json_encode( $object ) . ';'
00239 );
00240 }
00241
00252 protected function getJSONObject( array $params, Parser $parser ) {
00253 return $params;
00254 }
00255
00265 protected function handleMarkerData( array &$params, array $queryLocations ) {
00266 global $wgParser;
00267
00268 $parser = version_compare( $GLOBALS['wgVersion'], '1.18', '<' ) ? $wgParser : clone $wgParser;
00269
00270 $iconUrl = MapsMapper::getFileUrl( $params['icon'] );
00271 $params['locations'] = array();
00272
00273 foreach ( $params['staticlocations'] as $location ) {
00274 if ( $location->isValid() ) {
00275 $jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl );
00276
00277 $jsonObj['title'] = $parser->parse( $jsonObj['title'], $parser->getTitle(), new ParserOptions() )->getText();
00278 $jsonObj['text'] = $parser->parse( $jsonObj['text'], $parser->getTitle(), new ParserOptions() )->getText();
00279
00280 $hasTitleAndtext = $jsonObj['title'] !== '' && $jsonObj['text'] !== '';
00281 $jsonObj['text'] = ( $hasTitleAndtext ? '<b>' . $jsonObj['title'] . '</b><hr />' : $jsonObj['title'] ) . $jsonObj['text'];
00282 $jsonObj['title'] = strip_tags( $jsonObj['title'] );
00283
00284 $params['locations'][] = $jsonObj;
00285 }
00286 }
00287
00288 foreach ( $queryLocations as $location ) {
00289 if ( $location->isValid() ) {
00290 $jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl );
00291
00292 $jsonObj['title'] = strip_tags( $jsonObj['title'] );
00293
00294 $params['locations'][] = $jsonObj;
00295 }
00296 }
00297
00298 unset( $params['staticlocations'] );
00299 }
00300
00310 public final function getResult( SMWQueryResult $results, array $params, $outputmode ) {
00311 $this->handleParameters( $params, $outputmode );
00312 return $this->getResultText( $results, SMW_OUTPUT_HTML );
00313 }
00314
00320 public final function getName() {
00321 return wfMsg( 'maps_' . $this->service->getName() );
00322 }
00323
00329 public function getParameters() {
00330 $params = parent::getParameters();
00331 $paramInfo = $this->getParameterInfo();
00332
00333
00334
00335 unset( $paramInfo['mappingservice'] );
00336
00337 $params = array_merge( $params, $paramInfo );
00338
00339 return $params;
00340 }
00341
00351 protected function getMappedParamType( $type ) {
00352 static $typeMap = array(
00353 Parameter::TYPE_STRING => 'string',
00354 Parameter::TYPE_BOOLEAN => 'boolean',
00355 Parameter::TYPE_CHAR => 'int',
00356 Parameter::TYPE_FLOAT => 'int',
00357 Parameter::TYPE_INTEGER => 'int',
00358 Parameter::TYPE_NUMBER => 'int',
00359 );
00360
00361 return $typeMap[$type];
00362 }
00363
00364 }