00001 <?php
00002
00012 class SMFormInput {
00013
00019 protected $service;
00020
00028 const SEPARATOR = ';';
00029
00037 public function __construct( iMappingService $service ) {
00038 $this->service = $service;
00039 }
00040
00048 protected function getParameterInfo() {
00049 global $smgFIMulti, $smgFIFieldSize;
00050
00051 $params = MapsMapper::getCommonParameters();
00052 $this->service->addParameterInfo( $params );
00053
00054 $params['zoom']->setDefault( false, false );
00055
00056 $params['multi'] = new Parameter( 'multi', Parameter::TYPE_BOOLEAN );
00057 $params['multi']->setDefault( $smgFIMulti, false );
00058
00059 $params['fieldsize'] = new Parameter( 'fieldsize', Parameter::TYPE_INTEGER );
00060 $params['fieldsize']->setDefault( $smgFIFieldSize, false );
00061 $params['fieldsize']->addCriteria( new CriterionInRange( 5, 100 ) );
00062
00063 $params['centre'] = new Parameter( 'centre' );
00064 $params['centre']->setDefault( false, false );
00065 $params['centre']->addAliases( 'center' );
00066 $params['centre']->addCriteria( new CriterionIsLocation() );
00067 $manipulation = new MapsParamLocation();
00068 $manipulation->toJSONObj = true;
00069 $params['centre']->addManipulations( $manipulation );
00070
00071 $params['icon'] = new Parameter( 'icon' );
00072 $params['icon']->setDefault( '' );
00073 $params['icon']->addCriteria( New CriterionNotEmpty() );
00074
00075 $params['locations'] = new ListParameter( 'locations', self::SEPARATOR );
00076 $params['locations']->addCriteria( new CriterionIsLocation() );
00077 $manipulation = new MapsParamLocation();
00078 $manipulation->toJSONObj = true;
00079 $params['locations']->addManipulations( $manipulation );
00080
00081 $params['geocodecontrol'] = new Parameter( 'geocodecontrol', Parameter::TYPE_BOOLEAN );
00082 $params['geocodecontrol']->setDefault( true, false );
00083 $params['geocodecontrol']->setMessage( 'semanticmaps-par-geocodecontrol' );
00084
00085 return $params;
00086 }
00087
00101 public function getInputOutput( $coordinates, $input_name, $is_mandatory, $is_disabled, array $params ) {
00102 $parameters = array();
00103 foreach ( $params as $key => $value ) {
00104 if ( !is_array( $value ) && !is_object( $value ) ) {
00105 $parameters[$key] = $value;
00106 }
00107 }
00108
00109 $parameters['locations'] = $coordinates;
00110
00111 $validator = new Validator( wfMsg( 'maps_' . $this->service->getName() ), false );
00112 $validator->setParameters( $parameters, $this->getParameterInfo() );
00113 $validator->validateParameters();
00114
00115 $fatalError = $validator->hasFatalError();
00116
00117 if ( $fatalError === false ) {
00118 global $wgParser;
00119
00120 $params = $validator->getParameterValues();
00121
00122
00123
00124 if ( $params['zoom'] === false && count( $params['locations'] ) <= 1 ) {
00125 $params['zoom'] = $this->service->getDefaultZoom();
00126 }
00127
00128 $mapName = $this->service->getMapId();
00129
00130 $params['inputname'] = $input_name;
00131
00132 $output = $this->getInputHTML( $params, $wgParser, $mapName ) . $this->getJSON( $params, $wgParser, $mapName );
00133
00134 $this->service->addResourceModules( $this->getResourceModules() );
00135
00136 $configVars = Skin::makeVariablesScript( $this->service->getConfigVariables() );
00137
00138
00139 if ( version_compare( $GLOBALS['wgVersion'], '1.18', '<' ) ) {
00140 $GLOBALS['egMapsGlobalJSVars'] += $this->service->getConfigVariables();
00141 }
00142
00143 if ( true ) {
00144 global $wgOut;
00145 $this->service->addDependencies( $wgOut );
00146 $wgOut->addScript( $configVars );
00147 }
00148 else {
00149 $this->service->addDependencies( $wgParser );
00150 }
00151
00152 return $output;
00153 }
00154 else {
00155 return
00156 '<span class="errorbox">' .
00157 htmlspecialchars( wfMsgExt( 'validator-fatal-error', 'parsemag', $fatalError->getMessage() ) ) .
00158 '</span>';
00159 }
00160 }
00161
00173 protected function getInputHTML( array $params, Parser $parser, $mapName ) {
00174 return Html::element(
00175 'div',
00176 array(
00177 'id' => $mapName . '_forminput',
00178 'style' => 'display: inline'
00179 ),
00180 wfMsg( 'semanticmaps-loading-forminput' )
00181 );
00182 }
00183
00195 protected function getJSON( array $params, Parser $parser, $mapName ) {
00196 $object = $this->getJSONObject( $params, $parser );
00197
00198 if ( $object === false ) {
00199 return '';
00200 }
00201
00202 return Html::inlineScript(
00203 MapsMapper::getBaseMapJSON( $this->service->getName() . '_forminputs' )
00204 . "mwmaps.{$this->service->getName()}_forminputs.{$mapName}=" . FormatJson::encode( $object ) . ';'
00205 );
00206 }
00207
00218 protected function getJSONObject( array $params, Parser $parser ) {
00219 return $params;
00220 }
00221
00227 protected function getResourceModules() {
00228 return array( 'ext.sm.forminputs' );
00229 }
00230
00231 }