Difference between revisions of "Extension:SimpleCalendar"

From Organic Design wiki
(adjust alightly to return month table if month arg passed)
(New named parameter version from MW:Extension:Simple Calendar)
Line 1: Line 1:
<?
+
<?php
 
# MediaWiki Simple Calendar Extension {{#Security:*|dev}}{{#Security:view|*}}{{php}}
 
# MediaWiki Simple Calendar Extension {{#Security:*|dev}}{{#Security:view|*}}{{php}}
 
# - Version 1.00 (2007-04-24)
 
# - Version 1.00 (2007-04-24)
Line 20: Line 20:
  
 
# Renders a table of all the individual month tables
 
# Renders a table of all the individual month tables
function wfRenderCalendar(&$parser,$p = '',$y = false,$m = false) {
+
function wfRenderCalendar(&$parser) {
         if ($p) $p .= '/';
+
        $argv = array();
         if ($y == false) $y = date('Y');
+
        foreach (func_get_args() as $arg) if (!is_object($arg)) {
         if ($m) return wfRenderMonth(strftime('%m',strtotime("$y-$m-01")),$y,$p);
+
                if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]]=$match[2];
        $m = 1;
+
                }
 +
         if (isset($argv['title'])) $p = $argv['title'].'/'; else $p = '';
 +
         if (isset($argv['query'])) $q = $argv['query'].'&action=edit'; else $q = 'action=edit';
 +
        if (isset($argv['year'])) $y = $argv['year']; else $y = date('Y');
 +
         if (isset($argv['month'])) {
 +
                $m = $argv['month'];
 +
                return wfRenderMonth(strftime('%m',strtotime("$y-$m-01")),$y,$p,$q);
 +
                } else $m = 1;
 
         $table = "{| class=\"calendar\"\n";
 
         $table = "{| class=\"calendar\"\n";
 
         for ($rows = 3; $rows--; $table .= "|-\n")
 
         for ($rows = 3; $rows--; $table .= "|-\n")
 
                 for ($cols = 0; $cols < 4; $cols++)
 
                 for ($cols = 0; $cols < 4; $cols++)
                         $table .= '|'.wfRenderMonth($m++,$y,$p)."\n";
+
                         $table .= '|'.wfRenderMonth($m++,$y,$p,$q)."\n";
 
         return "$table\n|}\n";
 
         return "$table\n|}\n";
 
         }
 
         }
  
 
# Return a calendar table of the passed month and year
 
# Return a calendar table of the passed month and year
function wfRenderMonth($m,$y,$prefix = '') {
+
function wfRenderMonth($m,$y,$prefix = '',$query = '') {
 
         $thisDay  = date('d');
 
         $thisDay  = date('d');
 
         $thisMonth = date('n');
 
         $thisMonth = date('n');
 
         $thisYear  = date('Y');
 
         $thisYear  = date('Y');
         if (!$d = date('w',$ts = mktime(0,0,0,$m,1,$y))) $d = 7;
+
         if (!$d = date('w',$ts = mktime(1,1,1,$m,1,$y))) $d = 7;
         $month = date('F',$ts);
+
         $month = strftime('%B',$ts);
 
         $table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n";
 
         $table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n";
 
         $table .= "|M||T||W||T||F||S||S\n";
 
         $table .= "|M||T||W||T||F||S||S\n";
         if ($d > 1) $table .= "|-".str_repeat("\n|&nbsp;\n",$d-1);
+
         if ($d > 1) $table .= "|-".str_repeat("\n| \n",$d-1);
 
         for ($i = $day = $d; $day < 32; $i++) {
 
         for ($i = $day = $d; $day < 32; $i++) {
 
                 $day = $i - $d + 1;
 
                 $day = $i - $d + 1;
Line 48: Line 55:
 
                         if ($i%7 == 1) $table .= "\n|-\n";
 
                         if ($i%7 == 1) $table .= "\n|-\n";
 
                         $t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : '';
 
                         $t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : '';
                         $table .= "|$t|[[$prefix$dd $month $y|$day]]\n";
+
                        $ttext = "$prefix$dd $month $y";
 +
                        $title = Title::newFromText($ttext);
 +
                        $class = $title->exists() ? 'day-active' : 'day-empty';
 +
                        $url = $title->getFullURL($title->exists() ? '' : $query);
 +
                         $table .= "|$t|[$url <span class='$class'>$day</span>]\n";
 
                         }
 
                         }
 
                 }
 
                 }

Revision as of 02:23, 2 May 2007

<?php

  1. MediaWiki Simple Calendar Extension {{#Security:*|dev}}{{#Security:view|*}}Template:Php
  2. - Version 1.00 (2007-04-24)
  3. - See http://www.mediawiki.org/wiki/Extension:Simple_Calendar for installation and usage details
  4. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  5. - Author: http://www.organicdesign.co.nz/nad

$wgExtensionFunctions[] = 'wfSetupSimpleCalendar'; $wgHooks['LanguageGetMagic'][] = 'wfCalendarLanguageGetMagic'; function wfCalendarLanguageGetMagic(&$magicWords,$langCode = 0) {

       $magicWords['calendar'] = array(0,'calendar');
       return true;
       }

function wfSetupSimpleCalendar() {

       global $wgParser;
       $wgParser->setFunctionHook('calendar','wfRenderCalendar');
       return true;
       }
  1. Renders a table of all the individual month tables

function wfRenderCalendar(&$parser) {

       $argv = array();
       foreach (func_get_args() as $arg) if (!is_object($arg)) {
               if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]]=$match[2];
               }
       if (isset($argv['title'])) $p = $argv['title'].'/'; else $p = ;
       if (isset($argv['query'])) $q = $argv['query'].'&action=edit'; else $q = 'action=edit';
       if (isset($argv['year']))  $y = $argv['year']; else $y = date('Y');
       if (isset($argv['month'])) {
               $m = $argv['month'];
               return wfRenderMonth(strftime('%m',strtotime("$y-$m-01")),$y,$p,$q);
               } else $m = 1;
       $table = "{| class=\"calendar\"\n";
       for ($rows = 3; $rows--; $table .= "|-\n")
               for ($cols = 0; $cols < 4; $cols++)
                       $table .= '|'.wfRenderMonth($m++,$y,$p,$q)."\n";
       return "$table\n|}\n";
       }
  1. Return a calendar table of the passed month and year

function wfRenderMonth($m,$y,$prefix = ,$query = ) {

       $thisDay   = date('d');
       $thisMonth = date('n');
       $thisYear  = date('Y');
       if (!$d = date('w',$ts = mktime(1,1,1,$m,1,$y))) $d = 7;
       $month = strftime('%B',$ts);
       $table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n";
       $table .= "|M||T||W||T||F||S||S\n";
       if ($d > 1) $table .= "|-".str_repeat("\n| \n",$d-1);
       for ($i = $day = $d; $day < 32; $i++) {
               $day = $i - $d + 1;
               $dd = $day < 10 ? "0$day" : $day;
               if ($day < 29 or checkdate($m,$day,$y)) {
                       if ($i%7 == 1) $table .= "\n|-\n";
                       $t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : ;
                       $ttext = "$prefix$dd $month $y";
                       $title = Title::newFromText($ttext);
                       $class = $title->exists() ? 'day-active' : 'day-empty';
                       $url = $title->getFullURL($title->exists() ?  : $query);
                       $table .= "|$t|[$url $day]\n";
                       }
               }
       return "$table\n|}";
       }

?>