Newer
Older
pukiwikiplugin / datelink.inc.php
<?php
// $Id: datelink.inc.php, v 0.4 2017/03/31
// Copyright (C)
//   2016 Kwansei Gakuin University comp-staff
// License; GPL v2
// Link with Date plugin

// replacing "YYYY" to the current year, "MM" to the month, "DD" to the date, "NNNN" to the financial year (Nendo), and "SS" to a/b (prior/latter semester), and make a link to the page.


function prior_semester($n, $e) {
	$m = intval($n);
	$d = intval($e);
	return $m >= 4 && $m < 9
	    || $m == 9 && $d < 20;
// 4/1   -> prior
// 5/17  -> prior
// 9/20  -> prior
// 9/21  -> latter (in this plugin: not typical)
// 10/10 -> latter
// 1/2   -> latter
// 3/31  -> latter
}



function plugin_datelink_inline() {
	$args	= func_get_args();
	$result = FALSE;
	$argnum = func_num_args();
	
	// timestamp in pukiwiki
	$tstamp = UTIME;
	
	// description for some days beyond
	if($argnum == 2) {
		// timestamp after $args[0] days
		$tstamp += 24 * 60 * 60 * $args[0];
		$argnum = 1;
	}
	$year = get_date('Y', $tstamp);
	$month = get_date('m', $tstamp);
	$day = get_date('d', $tstamp);

	// for test -- link with given date
	if($argnum == 4) {
		$year = $args[0];
		$month = $args[1];
		$day = $args[2];
		$argnum = 1;
	}
	if($argnum == 1) {
		$nendo = $year;
		if($month == '01' || $month == '02' || $month == '03') {
			  $nendo -= 1;
		}
		$semester = 'b';
		if(prior_semester($month, $day)) {
			$semester = 'a';
		}
		$result = $args[func_num_args()-1];
		$result = str_replace('YYYY', $year, $result);
		$result = str_replace('MM', $month, $result);
		$result = str_replace('DD', $day, $result);
		$result = str_replace('NNNN', $nendo, $result);
		$result = str_replace('SS', $semester, $result);
		$result = str_replace(array('[', ']'), '', $result);
		$result = convert_html('[['.$result.']]');
		//convert_html (always?) adds p-tag and newline. We don't need.
		$result = str_replace(array("<p>", "</p>", "\r", "\n"), '', $result);
	}

	return $result;
}
?>