Action: Scheduler

bugs:
Compatible with: R5.5
Current version: 0.3
Credits: Author (use ::::)

{{scheduler}}


/actions/scheduler.php

<?php

// {{scheduler mode="[day|month|default]"]}}

if (!function_exists('get_last_day_of_month'))
{
	// get the last day of the month
	function get_last_day_of_month($mon, $year)
	{
		for ($tday = 28; $tday <= 31; $tday++)
		{
			$tdate = getdate(mktime(0, 0, 0, $mon, $tday, $year));

			if ($tdate['mon'] != $mon) break;
		}

		$tday--;
		return $tday;
	}
}

?>
<div align="center">
<p><table border=0 width=100%>
	<tr>
		<td>
			<p>
<?php

$mode_month		= 'month';
$mode_day		= 'day';
$mode_default	= 'default';

$mode			= (isset($_GET['mode'])) ? $_GET['mode'] : $mode_default;
$year			= (!isset($_GET['year'])) ? date('Y', time()) : $_GET['year'];
$month			= (!isset($_GET['month'])) ? date('n', time()) : $_GET['month'];
$today			= (!isset($_GET['day'])) ? date('d', time()) : $_GET['day'];

// $monthname = $month;
$tmpd			= getdate(mktime(0, 0, 0, $month, 1, $year));
$monthname		= $tmpd['month'];

$site_base		= $this->config['base_url'];
$thispage		= $this->get_page_tag();
$username		= $this->get_user_name();
$user_id		= $this->get_user_id();

//  NOTE VERY IMPORTANT,  if the table is totally empty the function will not work, you
//  must add one in maually for this function to work.
//  I could add it in the code but very messy, will come up with better way.

$dayschedule	= isset($_POST['dayscheduleX']) ? $_POST['dayscheduleX'] : '';
#$dayschedule	= BadWordFunction($dayschedule);
$dayschedule	= addslashes($dayschedule);  // for single quotes to work


// replace above function call with line below if wakka.php doesn't have a bad word function code in it
//$dayschedule = preg_replace($badwords, $changeto, $dayschedule);

$query			= "SELECT * FROM ".$this->config['table_prefix']."scheduler";
$result			= $this->sql_query($query);

if (isset($_POST['save']))
{
	// added to delete the empty schedule
	$del		= "DELETE from ".$this->config['table_prefix']."scheduler WHERE dayschedule = ''";
	$delresult	= $this->sql_query($del);
	$up			= 0;

	while ($row = mysqli_fetch_object($result))
	{
		if($row->user_id == $user_id && $row->month == $month && $row->day == $today && $row->year == $year)
		{
			$up	= 1;
			$ro	= $row->scheduler_id;
		}
	}

	if($up == 1)
	{
		$query	= "UPDATE ".$this->config['table_prefix']."scheduler SET
					dayschedule = '$dayschedule'
					WHERE user_id = '$user_id' AND scheduler_id = '$ro'";

		$result	= $this->sql_query($query);
	}
	else
	{
		$query	= "INSERT INTO ".$this->config['table_prefix']."scheduler (user_id, dayschedule, month, day, year) VALUES ('$user_id', '$dayschedule', '$month', '$today', '$year')";
		$result	= $this->sql_query($query);
	}
}
// end

// OUTPUT
$dayschedule	= $this->load_single(
				"SELECT dayschedule
				FROM ".$this->config["table_prefix"]."scheduler
				WHERE user_id = '".$user_id."'
				AND day = '".$today."'
				AND month = '".$month."'
				AND year = '".$year."'");

		$dayschedule = $dayschedule['dayschedule'];


if ($mode == 'day')
{
	$printout	= str_replace("\n", "<hr></td></tr><tr ALIGN='left'><td>", $dayschedule);
	$printowner	= $username."'s Termine und Aufgaben für ";
	?>
	<div align="center">
	<center>      <a href="<?php echo $this->href('', '', "mode=".$mode."&day="); echo (($today - 1) < 1) ? $lastday : $today - 1 ; ?>&year=<?php echo $year; ?>&month=<?php echo $month; ?>"><<</a>
	        <b><?php echo "$printowner$monthname $today, $year:"; ?></b>
	        <a href="<?php echo $this->href('', '', "mode=".$mode."&day="); echo (($today + 1) > $lastday) ? 1 : $today + 1 ; ?>&year=<?php echo $year; ?>&month=<?php echo $month; ?>">>></a>

	<table class='box' width='600' border='1' CELLSPACING='1' CELLPADDING='7' BGCOLOR='#DDccbb'>
	   <tr>
	      <td>
	<P><table class='box' width='100%' border='0' CELLSPACING='0' CELLPADDING='2' BGCOLOR='#ffffff'>
	   <tr ALIGN='left'>
	      <td>
	         <p>
	<?php
	if ($user = $this->get_user())
	{
		if(!empty($printout))
		{
			echo $printout;
		}
		else
		{
			echo "There are no entries on this day.";
		}
	}
	?>
		</p>
	      </td>
	   </tr>
	</table>
	      </td>
	   </tr>
	</table>
	<center>
	<a href=<?php echo $this->href('', '', "mode=".$mode_default."&month=".$month."&day=".$today."&year=".$year); ?>><small>Monatsübersicht</small></a>
	 | 
	<a href=<?php echo $this->href('', '', "mode=".$mode_month."&month=".$month."&day=".$today."&year=".$year); ?>><small>Monatskalender</small></a></center>
	</center></div>
<?php
}
else if ($mode == 'month')
{
	?>
	<table>
		<tr>
			<td valign="top">
	<?php
		// get what weekday the first is on
		$tmpd		= getdate(mktime(0, 0, 0, $month, 1, $year));
		$monthnamesgerman = array("", "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
		//$monthname	= $tmpd['month'];
		$monthname	= $monthnamesgerman[$month];
		$firstwday	= $tmpd['wday'];
		$lastday	= get_last_day_of_month($month, $year);
	?>
	<table cellpadding="2" cellspacing="0" border="1">
		<tr>
			<td colspan="7">
				<table cellpadding="0" cellspacing="0" border="0" width="100%">
					<tr>
						<td width="20"><a href="<?php echo $this->href('', '', "mode=".$mode."&month="); echo (($month - 1) < 1) ? 12 : $month-1 ; ?>&year=<?php echo (($month - 1) < 1) ? $year - 1 : $year ; ?>"><<<</a></td>
						<td align="center"><font size="2"><strong><? echo ucfirst(strtolower($username)); ?>s Kalender für <?php echo "$monthname $year"; ?></strong></font></td>
						<td width="20"><a href="<?php echo $this->href('', '', "mode=".$mode."&month="); echo (($month + 1) > 12) ? 1 : $month+1 ; ?>&year=<?php echo (($month + 1) > 12) ? $year + 1 : $year ; ?>">>>></a></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr BGCOLOR="#DDccbb">
			<?php // starting at Monday?>
			<td>Montag</td>
			<td>Dienstag</td>
			<td>Mittwoch</td>
			<td>Donnerstag</td>
			<td>Freitag</td>
			<td>Samstag</td>
			<td>Sonntag</td>
		</tr>

	<?php
		$day		= 1;
		//shift one left circular, now we calculate with 1..7
		if ($firstwday == 0) $firstwday = 7;
		//$firstwday = (($firstwday + 7) % 7);
		$wday		= $firstwday;
		$firstweek	= true;

		// loop through all the days of the month
		while ($day <= $lastday)
		{
			// set up blank days for first week
			if ($firstweek)
			{
				echo "    <tr>";

				// firstwday contains the starting day of the current month
				for ($i = 1; $i < $firstwday; $i++)
				{
					echo "        <td VALIGN=top ALIGN=left HEIGHT=70><font size=\"2\"> </font></td>";
				}

				$firstweek = false;
			}

			// nn Monday start a new row
			if ($wday == 1)
			{
				echo "    <tr>\n";
			}

			// check for event
			echo "        <td VALIGN=top ALIGN=left WIDTH=150 HEIGHT=70>";

			if($day < 10)
			{
				if($month < 10)
				{
					$tag = "$year:0$month:0$day";
				}
				else
				{
					$tag = "$year:$month:0$day";
				}
			}
			else
			{
				if($month < 10)
				{
					$tag = "$year:0$month:$day";
				}
				else
				{
					$tag = "$year:$month:$day";
				}
			}

			$todaydate = date("Y:m:d", time());

			if($tag == $todaydate)
			{
				$font1 = "<font color=\"#FF0000\"><b>";
				$font2 = "</b></font>";
				$token = 'yes';
			}
			else
			{
				$font1 = '';
				$font2 = '';
				$token = 'no';
			}

			// code to determine what data should be entered into each cell
			$thisdayschedule = $this->load_single(
					"SELECT dayschedule
					FROM ".$this->config["table_prefix"]."scheduler
					WHERE user_id = '".$user_id."'
					AND day = '".$day."'
					AND month = '".$month."'
					AND year = '".$year."'");

			$dayoutput	= $thisdayschedule['dayschedule'];
			//replace <some text>@...\n with <some text> \n
			$dayoutput	= preg_replace("/(.*?\w+?.*?)@(.*?)\n+?/", "$1\n", $dayoutput);
			//replace @...\n with nothing
			$dayoutput	= preg_replace("/(.*?)@(.*?)\n/", "", $dayoutput);
			$dayoutput	= preg_replace("/(.*?)@(.*)/", "$1", $dayoutput);
			//replace <newline> with <br>
			$dayoutput	= str_replace("\n", "<br>", $dayoutput);

			if ($token == 'yes')
			{
				// $printme = "<a href=".$site_base."DaySchedule&month=".$month."&day=".$today."&year=".$year."><small>[Print Day]</small></a>";
			}
			else
			{
				$printme = '';
			}

			echo "<table width=\"100%\"><tr BGCOLOR=\"#E4DFDA\"><td><a href=".$this->href('', '', "day=$day&month=$month&year=$year#EntryBox> $font1$day$font2")."</a> $printme</td</tr><tr><td><small>$dayoutput</small></td></tr></table>";
			echo "</td>\n";

			// Sunday -> next row
			if ($wday == 7)
			{
				echo "    </tr>\n";
			}

			// $wday++;
			$wday = ($wday % 7) + 1;
			$day++;
		}
	?>
		</tr>
	</table>

			</td>
		</tr>
	</table>

	<center>
	<a href=<?php echo $this->href('', '', "mode=".$mode_day."&month=".$month."&day=".$today."&year=".$year); ?>><small>Formatted Day Page</small></a>
	 | 
	<a href=<?php echo $this->href('', '', "mode=".$mode_default."&month=".$month."&day=".$today."&year=".$year); ?>><small>Small Monthly Calendar</small></a></center>

			</td>
		</tr><tr>
			<td widht=500>
		<p>
	<?php
	if ($user_id = $this->get_user_id())
	{
		// title over textarea box
		$dayschedule = $this->load_single(
					"SELECT dayschedule
					FROM ".$this->config["table_prefix"]."scheduler
					WHERE user_id = '".$user_id."'
						AND day = '".$today."'
						AND month = '".$month."'
						AND year = '".$year."'");

		$dayschedule = $dayschedule['dayschedule'];
	?>
	<center><a name=EntryBox></a>
			<a href="<?php echo $this->href('', '', "day="); echo (($today - 1) < 1) ? $lastday : $today - 1 ; ?>&year=<?php echo $year; ?>&month=<?php echo $month; ?>"><<</a>
			<b><?php echo ucfirst(strtolower($username))."s Eintrag für den ".$today.". ".$monthname." ".$year.":"; ?></b>
			<a href="<?php echo $this->href('', '', "day="); echo (($today + 1) > $lastday) ? 1 : $today + 1 ; ?>&year=<?php echo $year; ?>&month=<?php echo $month; ?>">>></a>
			<?php echo "<br><i><small>Kommentare mit @ einleiten.</small></i>";?>
			<center>
				<form action="" method="post">
					<textarea cols="90" rows="10" name="dayscheduleX"><?php echo $dayschedule; ?></textarea><br>
					<input type="submit" value="Senden" />
					<input type="hidden" name="save" value="true" />
				</form>
			</center>
		</center>
<?php
	}
}
else if ($mode == 'default')
{
	?>
	<table BGCOLOR="#DDccbb">
		<tr>
			<td valign="top">
	<?php
		// get what weekday the first is on
		$tmpd		= getdate(mktime(0, 0, 0, $month, 1, $year));
		$monthname	= $tmpd['month'];
		$firstwday	= $tmpd['wday'];
		$lastday	= get_last_day_of_month($month, $year);
	?>
	<table cellpadding="2" cellspacing="0" border="1">
		<tr BGCOLOR="#ffffff">
			<td colspan="7">
				<table cellpadding="0" cellspacing="0" border="0" width="100%">
					<tr  BGCOLOR="#ffffff">
						<td width="20"><a href="<?php echo $this->href('', '', "mode=".$mode."&month="); echo (($month - 1) < 1) ? 12 : $month-1 ; ?>&year=<?php echo (($month - 1) < 1) ? $year - 1 : $year ; ?>"><<<</a></td>
						<td align="center"><font size="2"><?php echo "$monthname $year"; ?></font></td>
						<td width="20"><a href="<?php echo $this->href('', '', "mode=".$mode."&month="); echo (($month + 1) > 12) ? 1 : $month+1 ; ?>&year=<?php echo (($month + 1) > 12) ? $year + 1 : $year ; ?>">>>></a></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr BGCOLOR="#DDccbb">
			<td width="22">Su</td>
			<td width="22">Mo</td>
			<td width="22">Tu</td>
			<td width="22">We</td>
			<td width="22">Th</td>
			<td width="22">Fr</td>
			<td width="22">Sa</td>
		</tr>

	<?php
		$day		= 1;
		$wday		= $firstwday;
		$firstweek	= true;

		// loop through all the days of the month
		while ($day <= $lastday)
		{
			// set up blank days for first week
			if ($firstweek)
			{
				echo "    <tr  BGCOLOR=\"#ffffff\">";

				for ($i = 1; $i <= $firstwday; $i++)
				{
					echo "        <td><font size=\"2\"> </font></td>";
				}

				$firstweek = false;
			}

			// Sunday start week with <tr>
			if ($wday == 0)
			{
				echo "    <tr BGCOLOR=\"#ffffff\">\n";
			}

			// check for event
			echo "        <td>";

			if($day < 10)
			{
				if($month < 10)
				{
					$tag = "$year:0$month:0$day";
				}
				else
				{
					$tag = "$year:$month:0$day";
				}
			}
			else
			{
				if($month < 10)
				{
					$tag = "$year:0$month:$day";
				}
				else
				{
					$tag = "$year:$month:$day";
				}
			}

			$todaydate = date("Y:m:d", time());

			if($tag == $todaydate)
			{
				$font1 = "<font color=\"#FF0000\"><b>";
				$font2 = "</b></font>";
			}
			else
			{
				$font1 = '';
				$font2 = '';
			}

			echo "<a href=".$this->href('', '', "day=$day&month=$month&year=$year").">".$font1.$day.$font2."</a>";
			echo "</td>\n";

			// Saturday week with </tr>
			if ($wday == 6)
			{
				echo "    </tr>\n";
			}

			$wday++;
			$wday = $wday % 7;
			$day++;
		}
	?>
		</tr>
	</table>

			</td>
		</tr>
	</table>

	<!-- Comment line below out if you don't want to link to the formatted schedule page-->
	<center>
	<a href=<?php echo $this->href('', '', "mode=".$mode_day."&month=".$month."&day=".$today."&year=".$year); ?>><small>Tagesansicht</small></a>
	 | 
	<a href=<?php echo $this->href('', '', "mode=".$mode_month."&month=".$month."&day=".$today."&year=".$year); ?>><small>Monatskalender</small></a></center>

			</td>
			<td WIDTH=500>
		<p>

	<?php
	if ($user_id = $this->get_user_id())
	{
		#$dayschedule = str_replace("\n", ",", $_POST['dayschedule']);

		// title over textarea box
		$printowner		= $username."'s Termine und Aufgaben für ";


		// replaced a comma with XXZXX below....I don't know what the purpose of the code is...
		$dayschedule	= str_replace("XXZXX", "\n", $dayschedule);
		$output			= str_replace(",", " ", $dayschedule);
	?>
		<br>
		<a href="<?php echo $this->href('', '', "mode=".$mode."&day="); echo (($today - 1) < 1) ? $lastday : $today - 1 ; ?>&year=<?php echo $year; ?>&month=<?php echo $month; ?>"><<</a>
		<b><?php echo "$printowner$monthname $today, $year:"; ?></b>
		<a href="<?php echo $this->href('', '', "mode=".$mode."&day="); echo (($today + 1) > $lastday) ? 1 : $today + 1 ; ?>&year=<?php echo $year; ?>&month=<?php echo $month; ?>">>></a>
			<form action="" method="post">
				<textarea cols="65" rows="12" name="dayscheduleX"><?php echo $dayschedule; ?></textarea>
				<input type="submit" value="Submit" />
				<input type="hidden" name="save" value="true" />
			</form>
	<?php
	}
}
else
{
	echo "<em>Der Kalender funktioniert nur bei angemeldeten Nutzern.</em>";
}
?>
</p>
		</td>
	</tr>
</table>
</div>


create extra table:


CREATE TABLE `yourwiki_scheduler` (
  `scheduler_id` INT(10) NOT NULL auto_increment,
  `user_id` INT(10) NOT NULL default '',
  `day` tinyint(2) NOT NULL default '0',
  `month` tinyint(2) NOT NULL default '0',
  `year` mediumint(4) NOT NULL default '0',
  `dayschedule` text,
  PRIMARY KEY  (`id`)
);

Documentation

How can I use this Calendar for group events?


Note:
By default only events of the (one) logged in person respectivly his actual account will be shown.


work around:
Share an account. Eg. "GroupXYAccount" which will be used for event accord only.


Login: GroupXYAccount
Password: onepasswordforallgroupmembers


To Do

  • localization
  • how to [doc]
  • month, week, day 
  • refactor

Referring pages:

Dev/PatchesHacks