I've written my first little sidebar plugin.
It shows the days/money saved since you've stopped smoking.
I guess it's not perfect, and there are no translation as well, but probably somebody may use/enhance it anyway.
Sample: http://www.suntsu.ch
cheers
manuel
Code: Select all
<?php
/*
* Copyright (c) Manuel Kaderli suntsu@suntsu.ch>
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 or later as published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this code; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
switch ($serendipity['lang']) {
case 'en':
default:
@define('PLUGIN_STOPSMOKING_NAME', 'Stop Smoking');
@define('PLUGIN_STOPSMOKING_GENERAL_DESCRIPTION', 'Display progress after you have stopped smoking');
@define('PLUGIN_STOPSMOKING_PRICE_NAME', 'price');
@define('PLUGIN_STOPSMOKING_PRICE_DESCRIPTION', 'price of one packet cigarette');
@define('PLUGIN_STOPSMOKING_DATE_NAME', 'date_of_stop');
@define('PLUGIN_STOPSMOKING_DATE_DESCRIPTION', 'date when you have stopped smoking dd.mm.yyyy');
@define('PLUGIN_STOPSMOKING_NUMBER_NAME', 'number_of_cigarettes');
@define('PLUGIN_STOPSMOKING_NUMBER_DESCRIPTION', 'number of cigarettes you have smoked per day');
@define('PLUGIN_STOPSMOKING_CURRENCY_NAME', 'currency');
@define('PLUGIN_STOPSMOKING_CURRENCY_DESCRIPTION', 'currency (CHF/EUR/$)');
break;
}
class serendipity_plugin_StopSmoking extends serendipity_plugin
{
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_STOPSMOKING_NAME);
$propbag->add('description', PLUGIN_STOPSMOKING_GENERAL_DESCRIPTION);
$propbag->add('configuration', array('date_of_stop','price','number_of_cigarettes','currency'));
}
function introspect_config_item($name, &$propbag)
{
switch($name) {
case 'price':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_STOPSMOKING_PRICE_NAME);
$propbag->add('description', PLUGIN_STOPSMOKING_PRICE_DESCRIPTION);
break;
case 'date_of_stop':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_STOPSMOKING_DATE_NAME);
$propbag->add('description', PLUGIN_STOPSMOKING_DATE_DESCRIPTION);
break;
case 'number_of_cigarettes':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_STOPSMOKING_NUMBER_NAME);
$propbag->add('description', PLUGIN_STOPSMOKING_NUMBER_DESCRIPTION);
break;
case 'currency':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_STOPSMOKING_CURRENCY_NAME);
$propbag->add('description', PLUGIN_STOPSMOKING_CURRENCY_DESCRIPTION);
break;
default:
return false;
}
return true;
}
function generate_content(&$title)
{
global $serendipity;
$title = PLUGIN_STOPSMOKING_NAME;
$price = $this->get_config('price');
$date_of_stop = $this->get_config('date_of_stop');
$number_of_cigarettes = $this->get_config('number_of_cigarettes');
$smokefreedays;
$currency = $this->get_config('currency');
$day = substr($date_of_stop,0,2);
$month = substr($date_of_stop,3,2);
$year = substr($date_of_stop,6,4);
$dateofstop = date("d.m.Y",mktime(0, 0, 0, $month, $day, $year));
echo 'stopped smoking: ';
echo $dateofstop;
echo '<br />';
echo 'smoke free days: ';
$today1 = strtotime ('now');
$today=date("d.m.Y",$today1);
$smokefreedays = $this->datediff("s",$dateofstop,$today,true);
echo $smokefreedays;
echo '<br />';
echo 'money saved: ';
echo (($number_of_cigarettes * $smokefreedays) / 20) * $price;
echo $currency;
}
function datediff($interval, $datefrom, $dateto, $using_timestamps = false)
{
// http://www.ilovejackdaniels.com/php/php-datediff-function/
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds
switch($interval) {
case 'yyyy': // Number of full years
$years_difference = floor($difference / 31536000);
if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
$years_difference--;
}
if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;
case "q": // Number of full quarters
$quarters_difference = floor($difference / 8035200);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference--;
$datediff = $quarters_difference;
break;
case "m": // Number of full months
$months_difference = floor($difference / 2678400);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference--;
$datediff = $months_difference;
break;
case 'y': // Difference between day numbers
$datediff = date("z", $dateto) - date("z", $datefrom);
break;
case "d": // Number of full days
$datediff = floor($difference / 86400);
break;
case "w": // Number of full weekdays
$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks
$first_day = date("w", $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
if ($odd_days > 7) { // Sunday
$days_remainder--;
}
if ($odd_days > 6) { // Saturday
$days_remainder--;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;
case "ww": // Number of full weeks
$datediff = floor($difference / 604800);
break;
case "h": // Number of full hours
$datediff = floor($difference / 3600);
break;
case "n": // Number of full minutes
$datediff = floor($difference / 60);
break;
default: // Number of full seconds (default)
$datediff = $difference;
break;
}
return $datediff;
}
}
?>