Skip to content

Commit

Permalink
Implement ajax-y updating of metar block.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Aug 13, 2016
1 parent 7c9e22e commit 8cee6a1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
71 changes: 38 additions & 33 deletions horde/lib/Block/Metar.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,14 @@ protected function _params()
*/
public function refreshContent($vars = null)
{
// if (empty($vars) || empty($vars->location)) {
// $this->_refreshParams = Horde_Variables::getDefaultVariables();
// $this->_refreshParams->set('location', $this->_params['location']);
// } else {
// $this->_refreshParams = $vars;
// }

// return $this->_content();
}

/**
*/
private function _row($label, $content)
{
return '<br /><strong>' . $label . ':</strong> ' . $content;
}
if (empty($vars) || empty($vars->location)) {
$this->_refreshParams = Horde_Variables::getDefaultVariables();
$this->_refreshParams->set('location', $this->_params['location']);
} else {
$this->_refreshParams = $vars;
}

/**
*/
private function _sameRow($label, $content)
{
return ' <strong>' . $label . ':</strong> ' . $content;
return $this->_content();
}

/**
Expand All @@ -128,29 +114,44 @@ protected function _content()
global $conf, $injector;
static $metarLocs;

if (empty($this->_params['location'])) {
return _("No location is set.");
}

// @todo - refactor this out.
if (!is_array($metarLocs)) {
$metarLocs = $this->getParams();
}

// Get the data.
$weather = $this->_weather->getCurrentConditions($this->_params['location'])->getRawData();
// Set up the weather driver.
$this->_weather->units = $this->_params['units'];
$units = $this->_weather->getUnits();

// Get the view object.
// Set up the view object.
$view = $injector->getInstance('Horde_View');
$view->weather = $weather;
$view->units = $units;

if (!empty($this->_refreshParams) && !empty($this->_refreshParams->location)) {
$location = $this->_refreshParams->location;
$view->instance = '';
} else {
$view->instance = hash('md5', mt_rand());
$injector->getInstance('Horde_Core_Factory_Imple')->create(
'WeatherLocationAutoCompleter_Metar',
array(
'id' => 'location' . $view->instance,
'instance' => $view->instance
)
);
$view->requested_location = $this->_params['location'];
$location = $this->_params['location'];
}

// Get the data.
$weather = $this->_weather->getCurrentConditions($location)->getRawData();
$view->weather = $weather;

// @todo - Use the station object.
$view->location_title = sprintf('%s, %s (%s)',
$metarLocs['location']['values'][$this->_params['__location']][$this->_params['location']],
$metarLocs['location']['values'][$this->_params['__location']][$location],
$this->_params['__location'],
$this->_params['location']
$location
);

// Wind.
Expand Down Expand Up @@ -331,7 +332,7 @@ protected function _content()

// TAF
if (!empty($this->_params['taf'])) {
$taf = $this->_weather->getForecast($this->_params['location'])->getRawData();
$taf = $this->_weather->getForecast($location)->getRawData();
$view->item = 0;
$view->periods = array();
foreach ($taf['time'] as $time => $entry) {
Expand Down Expand Up @@ -421,7 +422,11 @@ protected function _content()
}
}

return $view->render('block/metar_content');
if (!empty($view->instance)) {
return $view->render('block/metar');
} else {
return $view->render('block/metar_content');
}
}

}
2 changes: 1 addition & 1 deletion horde/lib/Block/Weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function _content()
} else {
$view->instance = hash('md5', mt_rand());
$injector->getInstance('Horde_Core_Factory_Imple')->create(
'WeatherLocationAutoCompleter',
'WeatherLocationAutoCompleter_Weather',
array(
'id' => 'location' . $view->instance,
'instance' => $view->instance
Expand Down
24 changes: 24 additions & 0 deletions horde/templates/block/metar.html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php if (!empty($this->instance)):?>
<div class="horde-content">
<input id="location<?php echo $this->instance?>" name="location<?php echo $this->instance?>" />
<input type="button" id="button<?php echo $this->instance?>" class="horde-default" value="<?php echo _("Change Location")?>" />
<span style="display:none;" id="location<?php echo $this->instance?>_loading_img">
<?php echo Horde_Themes_Image::tag('loading.gif')?>
</span>
</div>
<?php endif;?>

<div>
<?php if (is_array($this->location)): ?>
<?php echo sprintf(_("Several locations possible with the parameter: %s"), $this->requested_location)?><br />
<ul>
<?php foreach ($this->location as $location):?>
<li><?php echo $location->city?>, <?php echo $location->state?>(<?php echo $location->code?>)</li>
<?php endforeach;?>
</ul>
<?php else: ?>
<div id="weathercontent<?php echo $this->instance?>">
<?php echo $this->render('block/metar_content');?>
</div>
<?php endif;?>
</div>

0 comments on commit 8cee6a1

Please sign in to comment.