View source for Action: Scheduler

#||
||
bugs:
Compatible with: !!(green)**R6.0**!!
Current version: 0.4
Credits: //Author// (use ""::::"")
|{{toc numerate=1}} ||
||#

##""{{scheduler}}""##

**~/action/scheduler.php**
%%(php)
<?php

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

// get the last day of the month
$get_last_day_of_month	= function ($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
$prefix			= $this->db->table_prefix;
$mode_month		= 'month';
$mode_day		= 'day';
$mode_default	= 'default';

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

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

$username		= $this->get_user_name();
$user_id		= $this->get_user_id();

$dayschedule	= $_POST['dayscheduleX'] ?? '';

if (isset($_POST['save']))
{
	$update			= 0;

	$result = $this->db->load_single(
		"SELECT scheduler_id
		FROM {$prefix}scheduler
		WHERE user_id	= '" . (int) $user_id . "'
			AND day		= '" . (int) $today . "'
			AND month	= '" . (int) $month . "'
			AND year	= '" . (int) $year . "'");

	// added to delete the empty schedule
	$this->db->sql_query("DELETE FROM {$prefix}scheduler WHERE dayschedule = ''");

	if (!empty($result['scheduler_id']))
	{
		$update			= 1;
		$scheduler_id	= $result['scheduler_id'];
	}

	if ($update)
	{
		$this->db->sql_query(
			"UPDATE {$prefix}scheduler SET
				dayschedule = " . $this->db->q($dayschedule) . "
			WHERE user_id = '" . (int) $user_id . "'
				AND scheduler_id = '" . (int) $scheduler_id . "'");
	}
	else
	{
		$this->db->sql_query(
			"INSERT INTO {$prefix}scheduler (
				user_id,
				dayschedule,
				month,
				day,
				year
			)
			VALUES (" .
				(int) $user_id . ", " .
				$this->db->q($dayschedule) . ", " .
				(int) $month . ", " .
				(int) $today . ", " .
				(int) $year . "
			)");
	}
}
// end

// OUTPUT
$dayschedule = $this->db->load_single(
	"SELECT dayschedule
	FROM {$prefix}scheduler
	WHERE user_id	= '" . (int) $user_id . "'
		AND day		= '" . (int) $today . "'
		AND month	= '" . (int) $month . "'
		AND year	= '" . (int) $year . "'");

$dayschedule = $dayschedule['dayschedule'] ?? '';

if ($mode == 'day')
{
	$lastday	= $get_last_day_of_month($month, $year);
	$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' => (($today - 1) < 1) ? $lastday : $today - 1, 'year' => $year, 'month' => $month]); ?>"><<</a>
			<b><?php echo  $printowner . $monthname . ' ' . $today . ', ' . $year . ':'; ?></b>
			<a href="<?php echo $this->href('', '', ['mode' => $mode, 'day' => (($today + 1) > $lastday) ? 1 : $today + 1, 'year' => $year, 'month' => $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));
		$month_de = ['', 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
		# $monthname	= $tmpd['month'];
		$monthname	= $month_de[$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' => (($month - 1) < 1) ? 12 : $month - 1, 'year' => (($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' => (($month + 1) > 12) ? 1 : $month + 1, 'year' => (($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->db->load_single(
				"SELECT dayschedule
				FROM {$prefix}scheduler
				WHERE user_id	= '" . (int) $user_id . "'
					AND day		= '" . (int) $day . "'
					AND month	= '" . (int) $month . "'
					AND year	= '" . (int) $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="' . $this->href('', '', ['mode' => $mode_day, 'day' => $day, 'month' => $month, 'year' => $year, '#' => 'EntryBox']) . '"><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>';

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

			// $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->db->load_single(
			"SELECT dayschedule
			FROM {$prefix}scheduler
			WHERE user_id = '" . (int) $user_id . "'
				AND day = '" . (int) $today . "'
				AND month = '" . (int) $month . "'
				AND year = '" . (int) $year . "'");

		$dayschedule = $dayschedule['dayschedule'] ?? '';
	?>
	<center><a name="EntryBox"></a>
		<a href="<?php echo $this->href('', '', ['day' => (($today - 1) < 1) ? $lastday : $today - 1, 'year' => $year, 'month' => $month]); ?>"><<</a>
		<b><?php echo ucfirst(strtolower($username)) . 's Eintrag für den ' . $today . '. ' . $monthname . ' ' . $year . ':'; ?></b>
		<a href="<?php echo $this->href('', '', ['day' => (($today + 1) > $lastday) ? 1 : $today + 1, 'year' => $year, 'month' => $month]); ?>">>></a>
		<?php echo "<br><i><small>Kommentare mit @ einleiten.</small></i>";?>
		<center>
		<?php
			#echo '<form action="" method="post">';
			echo $this->form_open('day_schedule', ['href_param' => ['mode' => $mode_default, 'month' => $month, 'day' => $today, 'year' => $year]]);
			?>
				<input type="hidden" name="save" value="true" />
				<textarea cols="90" rows="10" name="dayscheduleX"><?php echo $dayschedule; ?></textarea><br>
				<input type="submit" value="Senden" />

			</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' => (($month - 1) < 1) ? 12 : $month - 1, 'year' => (($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' => (($month + 1) > 12) ? 1 : $month + 1, 'year' => (($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">';
			}

			// 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>';

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

			$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())
	{
		// title over textarea box
		$printowner		= $username . "'s Termine und Aufgaben für ";

	?>
		<br>
		<a href="<?php echo $this->href('', '', ['mode' => $mode, 'day' => (($today - 1) < 1) ? $lastday : $today - 1, 'year' => $year, 'month' => $month]); ?>"><<</a>
		<b><?php echo "$printowner$monthname $today, $year:"; ?></b>
		<a href="<?php echo $this->href('', '', ['mode' => $mode, 'day' => (($today + 1) > $lastday) ? 1 : $today + 1, 'year' => $year, 'month' => $month]); ?>">>></a>

		<?php
		#echo '<form action="" method="post">';
		echo $this->form_open('day_schedule', ['href_param' => ['mode' => $mode_default, 'month' => $month, 'day' => $today, 'year' => $year]]);
		?>
			<input type="hidden" name="save" value="true" />
			<textarea cols="65" rows="12" name="dayscheduleX"><?php echo $dayschedule; ?></textarea>
			<input type="submit" value="Submit" />
		</form>
	<?php
	}
}
else
{
	echo "<em>Der Kalender funktioniert nur bei angemeldeten Nutzern.</em>";
}
?>
</p>
		</td>
	</tr>
</table>
</div>

%%

create extra table:

%%
CREATE TABLE `wacko_scheduler` (
  `scheduler_id` INT(10) NOT NULL AUTO_INCREMENT,
  `user_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  `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  (`scheduler_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
  * add template

{{backlinks}}