This is the development page for the Calendar Link action. The code still needs some cleanup.
 

<?php                                        
/**
 * Displays a link to a calendar entry based on parameter or current calendar entry
 *
 * @package Actions
 * @subpackage  Date and Time
 * @name    calendarlink
 *
 * @authors {@link http://wikkawiki.org/MasinAlDujaili Masin Al-Dujaili}
 * @version 0.2
 * @since   Wikka 1.1.6.2
 *
 * @input   string  $calendarname  optional: prefix for the calendar pages to be created - Must be a CamelCase word;
 *          default: WikiName from the user if logged on, else it's checked if the action sits on a calendar page
 * @input   string  $link optional: one of the following keywords: 'next', 'prev', 'following'
 *          default: 'today'
 *          'next': only available if action sits on a calendar page. Returns the entry in the future relative to the current entry.
 *          'prev': only available if action sits on a calendar page. Returns the entry in the past relative to the current entry.
 *          'following': Returns the next entry in the future relative to the current date.
 *          'latest': Returns the last entry before the current date.
 *          'today': Returns the entry at the calculated date. If no date or offset is given, then it will result to the current date.
 * @input   int $date optional: a date in the form yyyymmdd
 *          If $link is given, $link will be calculated depending to the corresponding date
 * @input   int $offset optional: a number of days to offset
 *          If $date is given, the calculated entry will be $date + $offset. If $date is not given, the current date
 *          will be used. If $link is given, it will be calculated using this calculated date
 *          Example: {{calendarlink calendarname="MyCalendar" link="following" date="20070605" offset="60"}}
 *              The action will then link to the calendar entry following August 4th, 2007. Without link="following", the
 *              action will create a link to MyCalendar20070804 itself, appending the /edit handler if the page does not
 *              exist.
 *
 * @output  Link according to selection in parameter 'link'
 */


// l10n strings
define('LAST_ENTRY','Letzter Eintrag');
define('FIRST_ENTRY','Erster Eintrag');

// format string for locale-specific month (%B) + 4-digit year (%Y) used for caption and title attributes
// NOTE: monthname is locale-specific but order of month and year may need to be switched: hence the double quotes!
define ('USER_NAME',$this->GetUserName());
define ('BASE_URL',$this->config['base_url']);                                                                                                                                                  # i18n

define ('REGEX_CAMEL_WORD','([A-Z]+[a-z]+[A-Z0-9][A-Za-z0-9]*)');
define ('REGEX_CAL_DATE', '([0-9]{8,8})');
define ('REGEX_CAL_ENTRY', REGEX_CAMEL_WORD.REGEX_CAL_ENTRY);
// ***** END CONSTANTS section *****

// Get name of calendar to work with
$current_page = $this->GetPageTag();
if($this->getUser()) // Initialize $calendarname with current user's name if logged in
{
    $cal_name = $this->getUser();
}
else // use current page tag
{
    $cal_name = $current_page;
}

if(isset($vars['calendarname'])) // Check for parameter calendarname
{
    $cal_name = $vars['calendarname'];
}
elseif(preg_match(REGEX_CAL_DATE,$current_page,$cal_date)) // Check if current page is a calendar entry and use the corresponding calendar.
{
//  echo 'currentpage='.$current_page; // debug
//  print_r($cal_date); // debug
    $cal_name = substr($current_page,0,strpos($current_page, $cal_date[0]));
}
else // don't change prior init
{
    $cal_name = $cal_name;
} // END of getting calendar name

// Get link mode and convert to lower case
if(isset($vars['link'])) $mode = $vars['link'];
else $mode = 'today';
$mode=mb_strtolower($mode);
// END link mode acquisition

// check parameter date has been provided
if(isset($vars['date']) && preg_match(REGEX_CAL_DATE,$vars['date'],$cal_date))
{
    $date = $cal_date[0];
}
else // it has not been provided so we will use today as date
{
    $cal_date = getdate();
    $date = sprintf("%4d%02d%02d",$cal_date['year'],$cal_date['mon'],$cal_date['mday']);
}

if(isset($vars['offset']))
{
    $offset = $vars['offset'];
    list($year, $mon, $mday) = sscanf($date, "%4d%02d%02d");
    $cal_date = getdate(mktime(0,0,0,$mon, $mday+$offset, $year));
    $date = sprintf("%4d%02d%02d",$cal_date['year'],$cal_date['mon'],$cal_date['mday']);
}

if($mode!='today')  // we only need the list of calendar entries if we want some other day than (calculated) today
{
    $result = $this->Query('SELECT tag FROM ' . $this->config['table_prefix'] . "pages WHERE tag REGEXP('^$cal_name\[0-9]{8,8}') AND latest = 'Y'");
    if (mysql_num_rows($result)) while ($row = mysql_fetch_array($result)) $tags[] = $row['tag'];
}

if($mode=='following'||$mode=='latest'||$mode=='today')
{
    // creating a virtual calendar entry to use the code for $link=='next' or $link=='prev' on this virtual entry
    $entry = sprintf("%s%8d",$cal_name,$date);  // picking the relative origin based on calculated date
    $tags[] = $entry;
}
else
{
    $entry = $this->GetPageTag(); // picking the relative origin based on current page assuming it is calendar page
}

sort($tags); //sorting tags -- in case of a virtual calendar entry this will be now be in order, else the calendar entries are just sorted
$key = array_search($entry,$tags);  // getting position of our relative origin in the array

$date_found = FALSE;
if($mode=='next'||$mode=='following')
{
    if($key<(count($tags)-1))
    {
        $target_date = $tags[$key+1];
        $date_found = TRUE;
    }
    else
    {
        $target_date = LAST_ENTRY;
        $date_found = FALSE;
    }
}

elseif($mode=='prev'||$mode=='latest')
{
    if($key>0)
    {
        $target_date = $tags[$key-1];
        $date_found = TRUE;
    }
    else
    {
        $target_date = FIRST_ENTRY;
        $date_found = FALSE;
    }
}

elseif($mode=='today')
{
    $target_date = $entry;
    $date_found = TRUE;
}

if ($date_found)
{
    echo $this->Link($target_date);
}
else
{
    echo $target_date;
}


CategoryDevelopmentActions
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki
Page was generated in 0.1547 seconds