Page 1 of 1
Patch suggestion for Gavin - mycalendar
Posted: Tue Aug 02, 2005 3:34 am
by Anthem
in serendipity_plugin_mycalendar.php line 104 change
Code: Select all
echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false) . ' - ' . $cont . ' (' . $days . ' ' . DAYS . ')<br />';
to
Code: Select all
echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false) .
' - ' . $cont .
($days > 0 ? ' (' . $days . ' ' . DAYS . ')' : '') .
'<br />';
(or whatever fits your coding style best

) to get rid of the display bug showing "-0 days" left.
Or more accurately: No more negative timespans.
Now back to finding out how to disallow other users to change with my calendar events

Posted: Tue Aug 02, 2005 3:35 am
by Anthem
Make that "Garvin" in the title - d'oh
Posted: Wed Aug 03, 2005 4:40 pm
by garvinhicking
Thanks a lot for your patch! I just committed it.
About your other issue about changing uses: Yes, the plugin is not multi-user safe. It would require quite some work to store the authorid for each event and make only yourself access the events, plus superusers. However a patch would be heartedly welcome
Regards,
garvin
Posted: Wed Aug 03, 2005 11:29 pm
by Anthem
Ok, i've just read part of the documentation on s9y.org regarding the plugin API - nice concept btw

- and made a simple extension on the mycalendar: Autopruning of old entries.
All this code does is delete old events from the database after 3 days.
Why 3 days ? Apparently the sidebar never showed events older than 3 days. I didn't notice that when I started coding though

Perhaps that time interval should be made into a preference itself, or maybe even make mycalendar understand multi-day events...
Feedback is welcome.
diff -uN:
Code: Select all
--- serendipity_plugin_mycalendar.php.old 2005-08-03 20:51:27.000000000 +0200
+++ serendipity_plugin_mycalendar.php 2005-08-03 21:15:16.000000000 +0200
@@ -12,6 +12,8 @@
@define('PLUGIN_MYCALENDAR_SIDE_DESC', 'Zeigt einen eigenen Kalendar mit den nächsten X Terminen');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS', 'Einträge');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS_DESC', 'Wieviele Einträge sollen gezeigt werden?');
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE', 'Automatisch bereinigen');
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Sollen Einträge 3 Tage nach dem Termin automatisch gelöscht werden?');
break;
case 'en':
@@ -20,7 +22,8 @@
@define('PLUGIN_MYCALENDAR_SIDE_DESC', 'Shows your personal calendar with X upcoming scheduled events');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS', 'Amount of items');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS_DESC', 'How many items to show?');
-
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE', 'Pruning');
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Delete events 3 days after they took place?');
break;
}
@@ -37,7 +40,7 @@
'smarty' => '2.6.7',
'php' => '4.1.0'
));
- $propbag->add('configuration', array('title', 'items', 'datefm'));
+ $propbag->add('configuration', array('title', 'items', 'datefm', 'autoprune'));
// Register (multiple) dependencies. KEY is the name of the depending plugin. VALUE is a mode of either 'remove' or 'keep'.
// If the mode 'remove' is set, removing the plugin results in a removal of the depending plugin. 'Keep' meens to
@@ -69,6 +72,13 @@
$propbag->add('default', '%d.%m');
break;
+ case 'autoprune':
+ $propbag->add('type', 'boolean');
+ $propbag->add('name', PLUGIN_MYCALENDAR_SIDE_PRUNE);
+ $propbag->add('description', PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC);
+ $propbag->add('default', false);
+ break;
+
default:
return false;
}
@@ -78,9 +88,16 @@
function generate_content(&$title) {
global $serendipity;
$title = $this->get_config('title', $title);
+ $autoprune = $this->get_config('autoprune', false);
$ts = time();
- $items = serendipity_db_query("SELECT * from {$serendipity['dbPrefix']}mycalendar WHERE eventdate > " . ($ts - 60*60*24*3) . " ORDER BY eventdate LIMIT " . $this->get_config('items', 5));
+ if ($autoprune) {
+ $filter = "";
+ } else {
+ $filter = "WHERE eventdate > ". ($ts - 60*60*24*3);
+ }
+
+ $items = serendipity_db_query("SELECT * from {$serendipity['dbPrefix']}mycalendar " . $filter . " ORDER BY eventdate LIMIT " . $this->get_config('items', 5));
if (!is_array($items)) {
return true;
}
@@ -101,10 +118,15 @@
}
$days = ceil(($item['eventdate'] - $ts) / 86400);
- echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false) .
- ' - ' . $cont .
- ($days > 0 ? ' (' . $days . ' ' . DAYS . ')' : '') .
- '<br />';
+ if ($days > -3) {
+ echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false) .
+ ' - ' . $cont .
+ ($days > 0 ? ' (' . $days . ' ' . DAYS . ')' : '') .
+ '<br />';
+ } elseif ($autoprune) {
+ serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}mycalendar WHERE eventdate < ". ($ts - 60*60*24*3));
+ $autoprune = false;
+ }
}
return true;
}
Posted: Thu Aug 04, 2005 12:57 am
by garvinhicking
Thanks a lot for your contribution! However, some suggestions:
1. Could you please make a diff against the latest version of the plugin? It should be in CVS soon, because there were some changes in the language inclusion method.
2. I suggest to make the delete only run at a certain propability and not on every execution of the plugin; that's usually not needed. I would do a if (mt_rand(0,100) == 1) which would about execute this query every 100.st call of the plugin. As it doesn't matter if the old events get deleted now or some days later, this is well worth the increase of not sending another SQL query.
3. Would you like to make that limit of 3 days configurable?

)
Thanks and regards,
Garvin
Posted: Thu Aug 04, 2005 3:58 am
by Anthem
Ok, I'll update the patch to CVS.
About 2): The delete would only be run when
A) autopurge = true,
B) there is currently at least one event older than x days, and
C) it hasn't already run in the current script execution.
So it is run at most once per day (not including simultaneous script execution i.e. race conditions)
Posted: Thu Aug 04, 2005 10:30 am
by Anthem
Here is the patch against cvs. Do I have to update the UTF-8/lang_de.inc.php as well?
Code: Select all
diff -ruN cvs/lang_de.inc.php patch/lang_de.inc.php
--- cvs/lang_de.inc.php 2005-08-04 08:19:24.000000000 +0200
+++ patch/lang_de.inc.php 2005-08-04 08:24:08.000000000 +0200
@@ -13,3 +13,5 @@
@define('PLUGIN_MYCALENDAR_SIDE_DESC', 'Zeigt einen eigenen Kalendar mit den nächsten X Terminen');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS', 'Einträge');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS_DESC', 'Wieviele Einträge sollen gezeigt werden?');
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE', 'Automatisch bereinigen');
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Sollen Einträge 3 Tage nach dem Termin automatisch gelöscht werden?');
diff -ruN cvs/serendipity_plugin_mycalendar.php patch/serendipity_plugin_mycalendar.php
--- cvs/serendipity_plugin_mycalendar.php 2005-08-04 08:08:52.000000000 +0200
+++ patch/serendipity_plugin_mycalendar.php 2005-08-04 08:24:06.000000000 +0200
@@ -10,6 +10,8 @@
@define('PLUGIN_MYCALENDAR_SIDE_DESC', 'Shows your personal calendar with X upcoming scheduled events');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS', 'Amount of items');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS_DESC', 'How many items to show?');
+@define('PLUGIN_MYCALENDAR_SIDE_PRUNE', 'Pruning');
+@define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Delete events 3 days after they took place?');
class serendipity_plugin_mycalendar extends serendipity_plugin {
function introspect(&$propbag)
@@ -24,7 +26,7 @@
'smarty' => '2.6.7',
'php' => '4.1.0'
));
- $propbag->add('configuration', array('title', 'items', 'datefm'));
+ $propbag->add('configuration', array('title', 'items', 'datefm', 'autoprune'));
// Register (multiple) dependencies. KEY is the name of the depending plugin. VALUE is a mode of either 'remove' or 'keep'.
// If the mode 'remove' is set, removing the plugin results in a removal of the depending plugin. 'Keep' meens to
@@ -56,6 +58,13 @@
$propbag->add('default', '%d.%m');
break;
+ case 'autoprune':
+ $propbag->add('type', 'boolean');
+ $propbag->add('name', PLUGIN_MYCALENDAR_SIDE_PRUNE);
+ $propbag->add('description', PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC);
+ $propbag->add('default', false);
+ break;
+
default:
return false;
}
@@ -65,9 +74,16 @@
function generate_content(&$title) {
global $serendipity;
$title = $this->get_config('title', $title);
+ $autoprune = $this->get_config('autoprune', false);
$ts = time();
- $items = serendipity_db_query("SELECT * from {$serendipity['dbPrefix']}mycalendar WHERE eventdate > " . ($ts - 60*60*24*3) . " ORDER BY eventdate LIMIT " . $this->get_config('items', 5));
+ if ($autoprune) {
+ $filter = "";
+ } else {
+ $filter = "WHERE eventdate > ". ($ts - 60*60*24*3);
+ }
+
+ $items = serendipity_db_query("SELECT * from {$serendipity['dbPrefix']}mycalendar " . $filter . " ORDER BY eventdate LIMIT " . $this->get_config('items', 5));
if (!is_array($items)) {
return true;
}
@@ -88,10 +104,15 @@
}
$days = ceil(($item['eventdate'] - $ts) / 86400);
- echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false)
- . ' - ' . $cont
- . ($days > 0 ? ' (' . $days . ' ' . DAYS . ')' : '')
- . '<br />';
+ if ($days > -3) {
+ echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false) .
+ ' - ' . $cont .
+ ($days > 0 ? ' (' . $days . ' ' . DAYS . ')' : '') .
+ '<br />';
+ } elseif ($autoprune) {
+ serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}mycalendar WHERE eventdate < ". ($ts - 60*60*24*3));
+ $autoprune = false;
+ }
}
return true;
}
Posted: Thu Aug 04, 2005 12:55 pm
by Anthem
Part II of the patch makes the event duration a preference:
(patch -p1 < ...)
Code: Select all
diff -ruN cvspatch/lang_de.inc.php cvspatch2/lang_de.inc.php
--- cvspatch/lang_de.inc.php 2005-08-04 08:38:04.000000000 +0200
+++ cvspatch2/lang_de.inc.php 2005-08-04 10:25:10.000000000 +0200
@@ -13,5 +13,7 @@
@define('PLUGIN_MYCALENDAR_SIDE_DESC', 'Zeigt einen eigenen Kalendar mit den nächsten X Terminen');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS', 'Einträge');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS_DESC', 'Wieviele Einträge sollen gezeigt werden?');
+ @define('PLUGIN_MYCALENDAR_SIDE_SHOWTIME', 'Termindauer');
+ @define('PLUGIN_MYCALENDAR_SIDE_SHOWTIME_DESC', 'Wieviele Tage nach dem Termindatum soll ein Termin noch angezeigt werden?');
@define('PLUGIN_MYCALENDAR_SIDE_PRUNE', 'Automatisch bereinigen');
- @define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Sollen Einträge 3 Tage nach dem Termin automatisch gelöscht werden?');
+ @define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Sollen Einträge anschliessend automatisch gelöscht werden?');
diff -ruN cvspatch/serendipity_plugin_mycalendar.php cvspatch2/serendipity_plugin_mycalendar.php
--- cvspatch/serendipity_plugin_mycalendar.php 2005-08-04 08:38:04.000000000 +0200
+++ cvspatch2/serendipity_plugin_mycalendar.php 2005-08-04 10:49:18.000000000 +0200
@@ -10,8 +10,10 @@
@define('PLUGIN_MYCALENDAR_SIDE_DESC', 'Shows your personal calendar with X upcoming scheduled events');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS', 'Amount of items');
@define('PLUGIN_MYCALENDAR_SIDE_ITEMS_DESC', 'How many items to show?');
+@define('PLUGIN_MYCALENDAR_SIDE_SHOWTIME', 'Event duration');
+@define('PLUGIN_MYCALENDAR_SIDE_SHOWTIME_DESC', 'For how many days should events be shown after the event date?');
@define('PLUGIN_MYCALENDAR_SIDE_PRUNE', 'Pruning');
-@define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Delete events 3 days after they took place?');
+@define('PLUGIN_MYCALENDAR_SIDE_PRUNE_DESC', 'Delete events after they took place?');
class serendipity_plugin_mycalendar extends serendipity_plugin {
function introspect(&$propbag)
@@ -26,7 +28,7 @@
'smarty' => '2.6.7',
'php' => '4.1.0'
));
- $propbag->add('configuration', array('title', 'items', 'datefm', 'autoprune'));
+ $propbag->add('configuration', array('title', 'items', 'datefm', 'showtime', 'autoprune'));
// Register (multiple) dependencies. KEY is the name of the depending plugin. VALUE is a mode of either 'remove' or 'keep'.
// If the mode 'remove' is set, removing the plugin results in a removal of the depending plugin. 'Keep' meens to
@@ -58,6 +60,13 @@
$propbag->add('default', '%d.%m');
break;
+ case 'showtime':
+ $propbag->add('type', 'string');
+ $propbag->add('name', PLUGIN_MYCALENDAR_SIDE_SHOWTIME);
+ $propbag->add('description', PLUGIN_MYCALENDAR_SIDE_SHOWTIME_DESC);
+ $propbag->add('default', 2);
+ break;
+
case 'autoprune':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_MYCALENDAR_SIDE_PRUNE);
@@ -75,12 +84,13 @@
global $serendipity;
$title = $this->get_config('title', $title);
$autoprune = $this->get_config('autoprune', false);
+ $showtime = 1 + (int)$this->get_config('showtime', 2);
$ts = time();
if ($autoprune) {
$filter = "";
} else {
- $filter = "WHERE eventdate > ". ($ts - 60*60*24*3);
+ $filter = "WHERE eventdate > ". ($ts - $showtime*24*60*60);
}
$items = serendipity_db_query("SELECT * from {$serendipity['dbPrefix']}mycalendar " . $filter . " ORDER BY eventdate LIMIT " . $this->get_config('items', 5));
@@ -89,28 +99,25 @@
}
foreach($items AS $item) {
- $cont = '';
+ $cont = htmlspecialchars($item['eventname']);
+
if (!empty($item['eventurl'])) {
if (!empty($item['eventurltitle'])) {
$ltitle = $item['eventurltitle'];
} else {
$ltitle = $item['eventname'];
}
- $cont .= '<a href="' . $item['eventurl'] . '" title="' . htmlspecialchars($ltitle) . '">';
- }
- $cont .= htmlspecialchars($item['eventname']);
- if (!empty($item['eventurl'])) {
- $cont .= '</a>';
+ $cont = '<a href="' . $item['eventurl'] . '" title="' . htmlspecialchars($ltitle) . '">' . $cont . '</a>';
}
$days = ceil(($item['eventdate'] - $ts) / 86400);
- if ($days > -3) {
+ if ($days > -$showtime) {
echo serendipity_strftime($this->get_config('datefm'), $item['eventdate'], false) .
' - ' . $cont .
($days > 0 ? ' (' . $days . ' ' . DAYS . ')' : '') .
'<br />';
} elseif ($autoprune) {
- serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}mycalendar WHERE eventdate < ". ($ts - 60*60*24*3));
+ serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}mycalendar WHERE eventdate < ". ($ts - $showtime*24*60*60));
$autoprune = false;
}
}
Posted: Thu Aug 04, 2005 1:06 pm
by garvinhicking
Thanks a lot, Anthem! I patched the plugin with some minor modifications, please look at the CVS when it's updated.
Regards,
Garvin