addBehavior('publicHeadContent',array('lunarPhaseBehaviors','addCss'));
class lunarPhaseBehaviors
{
/**
This function add CSS file in the public header
*/
public static function addCss()
{
global $core;
$url = $core->blog->getQMarkURL().'pf='.basename(dirname(__FILE__)).'/style.css';
echo '';
}
}
class lunarPhasePublic
{
/**
Displays lunarphase widget
@param w dcWidget dcWidget object
@return string HTML code of widget
*/
public static function widget($w)
{
global $core;
if ($w->offline)
return;
$lp = new lunarPhase();
if (($w->homeonly == 1 && $core->url->type != 'default') ||
($w->homeonly == 2 && $core->url->type == 'default')) {
return;
}
$res =
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '');
# Get live content
$res .= lunarPhasePublic::getLive($w,$lp);
# Get prevision content
$res .= lunarPhasePublic::getPrevisions($w,$lp);
return $w->renderDiv($w->content_only,'lunarphase '.$w->class,'',$res);
}
/**
Returns "live" part of lunarphase widget
@param w dcWidget dcWidget object
@param lp lunarPhaset lunarPhase object
@return string Live HTML part
*/
public static function getLive($w,$lp)
{
$ul_mask = '
%1$s
';
$li_mask = '
%1$s
';
$live = $lp->getLive();
$res = '';
# Phase
if ($w->phase) {
$res .= sprintf($li_mask,$live['name'],$live['id']);
}
# Illumination
if ($w->illumination) {
$res .=
sprintf($li_mask,sprintf(__('Illumination: %s%%'),
lunarPhasePublic::formatValue('percent',$live['illumination'])),
'illumination');
}
# Moon's age
if ($w->age) {
$res .=
sprintf($li_mask,sprintf(__('Age of moon: %s days'),
lunarPhasePublic::formatValue('int',$live['age'])),
'age');
}
# Distance from earth
if ($w->dist_to_earth) {
$res .=
sprintf($li_mask,sprintf(__('Distance to earth: %s km'),
lunarPhasePublic::formatValue('int',$live['dist_to_earth'])),
'dist_to_earth');
}
# Distance from sun
if ($w->dist_to_sun) {
$res .=
sprintf($li_mask,sprintf(__('Distance to sun: %s km'),
lunarPhasePublic::formatValue('int',$live['dist_to_sun'])),
'dist_to_sun');
}
# Moon's angle
if ($w->moon_angle) {
$res .=
sprintf($li_mask,sprintf(__('Angle of moon: %s deg'),
lunarPhasePublic::formatValue('deg',$live['moon_angle'])),
'moon_angle');
}
# Sun's angle
if ($w->sun_angle) {
$res .=
sprintf($li_mask,sprintf(__('Angle of sun: %s deg'),
lunarPhasePublic::formatValue('deg',$live['sun_angle'])),
'sun_angle');
}
# Parallax
if ($w->parallax) {
$res .=
sprintf($li_mask,sprintf(__('Parallax: %s deg'),
lunarPhasePublic::formatValue('deg',$live['parallax'])),
'parallax');
}
if (strlen($res) > 0) {
return
'
'.__('In live').'
'.
sprintf($ul_mask,$res,'lunarphase');
}
}
/**
Returns "previsions" part of lunarphase widget
@param w dcWidget dcWidget object
@param lp lunarPhaset lunarPhase object
@return string previsions HTML part
*/
public static function getPrevisions($w,$lp)
{
$ul_mask = '
%1$s
';
$li_mask = '
%1$s
';
$res = '';
if ($w->previsions) {
foreach ($lp->getPrevisions() as $k => $v) {
$res .= sprintf($li_mask,lunarPhasePublic::formatValue('date',$v['date']),$k,$v['name']);
}
}
if (strlen($res) > 0) {
return
'
'.__('Previsions').'
'.
sprintf($ul_mask,$res,'lunarphase');
}
}
/**
Returns value passed in argument with a correct format
@param type string Type of convertion
@param value mixed Value to convert
@return mixed Converted value
*/
public static function formatValue($type = '',$value)
{
$res = '';
$format = $GLOBALS['core']->blog->settings->system->date_format.' - ';
$format .= $GLOBALS['core']->blog->settings->system->time_format;
$tz = $GLOBALS['core']->blog->settings->system->blog_timezone;
switch ($type) {
case 'int':
$res = number_format($value,0);
break;
case 'float':
$res = number_format($value,2);
break;
case 'percent':
$res = number_format($value * 100,0);
break;
case 'date':
$res = dt::str($format,$value,$tz);
break;
case 'deg':
$res = number_format(($value * (180.0 / M_PI)),2);
break;
default:
$res = $value;
break;
}
return $res;
}
}