Modified:
trunk/edit-entry.php
trunk/schoorbs-includes/database/entry.class.php
trunk/schoorbs-includes/database/repeat.class.php
trunk/schoorbs-includes/lang/schoorbs.pot
Log:
Finished edit-entry.php
Modified: trunk/edit-entry.php
==============================================================================
--- trunk/edit-entry.php (original)
+++ trunk/edit-entry.php Wed Mar 4 14:46:14 2009
@@ -28,6 +28,10 @@
// Get the booking
$oEntry = Entry::getById($nId);
+if ($oEntry === null) {
+ SchoorbsTPL::error(Lang::_('Specified entry does not exist.'));
+ exit(1);
+}
// Only allow the owner or an administrator to change the booking
if (!getAuthorised(1) || !getWritable($oEntry->getCreateBy(),
getUserName())) {
@@ -124,8 +128,8 @@
exit(1);
}
- $nPeriod = intval($_REQUEST['period']);
- if (($nPeriod >= count($GLOBALS['periods'])) || ($nPeriod < 0)) {
+ $nNewPeriod = intval($_REQUEST['period']);
+ if (($nNewPeriod >= count($GLOBALS['periods'])) || ($nPeriod < 0)) {
SchoorbsTPL::error(Lang::_('Supplied value for the period is not
valid.'));
exit(1);
}
@@ -142,30 +146,42 @@
exit(1);
}
- $nHour = intval($_REQUEST['hour']);
- $nMinute = intval($_REQUEST['minute']);
- if (($nHour < 0) || ($nHour > 23)) {
+ $nNewHour = intval($_REQUEST['hour']);
+ $nNewMinute = intval($_REQUEST['minute']);
+ if (($nNewHour < 0) || ($nNewHour > 23)) {
SchoorbsTPL::error(Lang::_('Supplied value for the hour is not
valid.'));
exit(1);
}
- if (($nMinute < 0) || ($nHour > 59)) {
+ if (($nNewMinute < 0) || ($nNewHour > 59)) {
SchoorbsTPL::error(Lang::_('Supplied value for the minute is not
valid.'));
exit(1);
}
- if (!in_array($_REQUEST['dur_units'],
array('periods', 'days', 'minutes', 'hours', 'weeks'))) {
+ if (!in_array($_REQUEST['dur_units'],
array('days', 'minutes', 'hours', 'weeks'))) {
SchoorbsTPL::error(Lang::_('Supplied value for the duration unit is not
valid.'));
exit(1);
}
}
+ $sNewDurationUnit = $_REQUEST['dur_units'];
+
+ if (!isset($_REQUEST['duration'])) {
+ SchoorbsTPL::error(Lang::_('The duration of the edited was not
supplied.'));
+ exit(1);
+ }
+ $nNewDuration = intval($_REQUEST['duration']);
+ if ($nNewDuration < 1) {
+ SchoorbsTPL::error(Lang::_('No valid value for the duration of this
entry was supplied.'));
+ exit(1);
+ }
+
if (!isset($_REQUEST['room'])) {
SchoorbsTPL::error(Lang::_('The room for the edited entry was not
supplied.'));
exit(1);
}
- $oRoom = Room::getById(intval($_REQUEST['room']));
- if ($oRoom === null) {
+ $oNewRoom = Room::getById(intval($_REQUEST['room']));
+ if ($oNewRoom === null) {
SchoorbsTPL::error(Lang::_('The specified room does not exist.'));
exit(1);
}
@@ -178,8 +194,27 @@
SchoorbsTPL::error(Lang::_('No valid value for the type of this entry
was supplied.'));
exit(1);
}
- $cType = $_REQUEST['type'];
+ $sNewType = $_REQUEST['type'];
// After the input validation is done, let's commit these changes to the
// database.
+ $oEntry->setName($sNewName);
+ $oEntry->setDescription($sNewDescription);
+ $oEntry->setRoom($oNewRoom);
+ // Set Start- and Endtime
+ if (Entry::perioded()) {
+ $oEntry->setPeriodStartTime($nNewYear, $nNewMonth, $nNewDay,
$nNewPeriod);
+ } else {
+ $oEntry->setNonPeriodStarttime($nNewYear, $nNewMonth, $nNewDay,
$nNewHour, $nNewMinute);
+ }
+ $oEntry->setImplicitDuration($nNewDuration, $sNewDurationUnit);
+ $oEntry->setType($sNewType);
+ $oEntry->commit();
+
+ // Use only returl from POST requests to prohibit XSS attacks
+ if (isset($_POST['returl'])) {
+ header('Location: '.$returl);
+ } else {
+ header('Location: view-entry.php?id='.$oEntry->getId());
+ }
}
Modified: trunk/schoorbs-includes/database/entry.class.php
==============================================================================
--- trunk/schoorbs-includes/database/entry.class.php (original)
+++ trunk/schoorbs-includes/database/entry.class.php Wed Mar 4 14:46:14
2009
@@ -358,6 +358,84 @@
}
/**
+ * Saves the object in the database.
+ *
+ * If this is a new obejct we will run an insert-query and get the created
+ * Id, otherwise we will run an update-query.
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ */
+ public function commit() {
+ if ($this->nId == -1) {
+ // new object, so we will insert it as a new row
+ $oIdgen = $this->oDB->getConnection()->getIdGenerator();
+ // prepare the INSERT startement which will be the same in both cases
+ //
+ // Example query:
+ // INSERT INTO schoorbs_entry (start_time, end_time,
+ // entry_type, repeat_id, room_id, create_by, name,
+ // type, description) VALUES (1223455500, 1223628300,
+ // 0, 0, 1, 'admin', 'Test', 'E', 'Brunch time');
+ $oStatement = $this->oDB->getConnection()->prepareStatement(
+ 'INSERT INTO '.$this->oDB->getTableName('entry')
+ .' (start_time, end_time, entry_type, repeat_id,'
+ .'room_id, create_by, name, type, description) '
+ .'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);'
+ );
+ $oStatement->setInt(1, $this->nStartTime);
+ $oStatement->setInt(2, $this->nEndTime);
+ $oStatement->setInt(3, $this->nEntryType);
+ $oStatement->setInt(4, $this->nRepeatId);
+ $oStatement->setInt(5, $this->oRoom->getId());
+ $oStatement->setString(6, $this->sCreateBy);
+ $oStatement->setString(7, $this->sName);
+ $oStatement->setString(8, $this->sType);
+ $oStatement->setString(9, $this->sDescription);
+ // do we get id before or after performing insert?
+ if($oIdgen->isBeforeInsert()) {
+ $this->nId = $oIdgen->getId($this->oDB->getTableName('entry')
+ .'_id_seq');
+ // now add that ID to SQL and perform INSERT
+ $oStatement->executeUpdate();
+ } else { // isAfterInsert()
+ // first perform INSERT
+ $oStatement->executeUpdate();
+ $this->nId = $oIdgen->getId();
+ }
+ } else {
+ // Update the already existing object
+ //
+ // Example query:
+ // UPDATE schoorbs_entry SET start_time = 1223455500,
+ // end_time = 1223628300, entry_type = 0,
+ // repeat_id = 0, room_id = 1, create_by = 'admin',
+ // name = 'Test', type = 'I',
+ // description = 'Brunch time' WHERE id = 24
+ $oStatement = $this->oDB->getConnection()->prepareStatement(
+ 'UPDATE '.$this->oDB->getTableName('entry')
+ .' SET start_time = ?, end_time = ?, '
+ .'entry_type = ?, repeat_id = ?, room_id = ?,'
+ .'create_by = ?, name = ?, type = ?,'
+ .'description = ? WHERE id = ?'
+ );
+ $oStatement->setInt(1, $this->nStartTime);
+ $oStatement->setInt(2, $this->nEndTime);
+ $oStatement->setInt(3, $this->nEntryType);
+ $oStatement->setInt(4, $this->nRepeatId);
+ $oStatement->setInt(5, $this->oRoom->getId());
+ $oStatement->setString(6, $this->sCreateBy);
+ $oStatement->setString(7, $this->sName);
+ $oStatement->setString(8, $this->sType);
+ $oStatement->setString(9, $this->sDescription);
+ $oStatement->setInt(10, $this->nId);
+ $oStatement->executeUpdate();
+ }
+ // We have commited all current changes, so there are no changes left in
+ // this object.
+ $this->bChanged = false;
+ }
+
+ /**
* Return the starttime of this entry
*
* @author Uwe L. Korn <uw...@xhochy.org>
@@ -516,6 +594,50 @@
}
/**
+ * Set the room where this entry is placed.
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $oRoom Room
+ */
+ public function setRoom(Room $oRoom) {
+ $this->changed = true;
+ $this->oRoom = $oRoom;
+ }
+
+ /**
+ * Set the name
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $sName string
+ */
+ public function setName($sName) {
+ $this->changed = true;
+ $this->sName = $sName;
+ }
+
+ /**
+ * Set the description
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $sDescription string
+ */
+ public function setDescription($sDescription) {
+ $this->changed = true;
+ $this->sDescription = $sDescription;
+ }
+
+ /**
+ * Set the type
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $sType string
+ */
+ public function setType($sType) {
+ $this->changed = true;
+ $this->sType = $sType;
+ }
+
+ /**
* Return the time of the last modification
*
* @author Uwe L. Korn <uw...@xhochy.org>
@@ -625,9 +747,70 @@
* @author Uwe L. Korn <uw...@xhochy.org>
*/
public function getStartPeriod() {
+ global $periods;
+
$aTime = getdate($this->nStartTime);
$nPnum = $aTime['minutes'];
if($nPnum >= count($periods) - 1) $nPnum = count($periods) - 1;
return $nPnum;
+ }
+
+ /**
+ * Set the starttime using the period method.
+ *
+ * The period should not exceed the range of periods.
+ * We assume $nPeriod < count($periods).
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $nYear int
+ * @param $nMonth int
+ * @param $nDay int
+ * @param $nPeriod int
+ */
+ public function setPeriodStartTime($nYear, $nMonth, $nDay, $nPeriod) {
+ $this->nStartTime = mktime(12, $nPeriod, 0, $nMonth, $nDay, $nYear);
+ }
+
+ /**
+ * Set the starttime using the common time methods
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $nYear int
+ * @param $nMonth int
+ * @param $nDay int
+ * @param $nHour int
+ * @param $nMinute int
+ */
+ public function setNonPeriodStarttime($nYear, $nMonth, $nDay, $nHour,
$nMinute) {
+ $this->nStartTime = mktime($nHour, $nMinute, 0, $nMonth, $nDay, $nYear);
+ }
+
+ /**
+ * Set the endtime by specifying the duration.
+ *
+ * Attention! If you intend to change duration and starttime at the same
+ * step, please change the starttime first as the duration is not saved
+ * explicitly, only starttime and endtime are saved.
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @param $nDuration int
+ * @param $sDurationUnit string
+ */
+ public function setImplicitDuration($nDuration, $sDurationUnit) {
+ global $periods;
+
+ if ($sDurationUnit == 'days') {
+ $this->nEndTime = $this->nStartTime + 86400 * $nDuration;
+ } elseif ($sDurationUnit == 'minutes') {
+ $this->nEndTime = $this->nStartTime + 60 * $nDuration;
+ } elseif ($sDurationUnit == 'hours') {
+ $this->nEndTime = $this->nStartTime + 3600 * $nDuration;
+ } elseif ($sDurationUnit == 'weeks') {
+ $this->nEndTime = $this->nStartTime + 604800 * $nDuration;
+ } elseif ($sDurationUnit == 'periods') {
+ $this->nEndTime = $this->nStartTime
+ + 86400 * floor($nDuration / count($periods))
+ + 60 * ($nDuration % count($periods));
+ }
}
}
Modified: trunk/schoorbs-includes/database/repeat.class.php
==============================================================================
--- trunk/schoorbs-includes/database/repeat.class.php (original)
+++ trunk/schoorbs-includes/database/repeat.class.php Wed Mar 4 14:46:14
2009
@@ -245,6 +245,16 @@
}
/**
+ * Return the unique identifier
+ *
+ * @author Uwe L. Korn <uw...@xhochy.org>
+ * @return int
+ */
+ public function getId() {
+ return $this->nId;
+ }
+
+ /**
* Return the date where this repetition ends.
*
* @author Uwe L. Korn <uw...@xhochy.org>
Modified: trunk/schoorbs-includes/lang/schoorbs.pot
==============================================================================
--- trunk/schoorbs-includes/lang/schoorbs.pot (original)
+++ trunk/schoorbs-includes/lang/schoorbs.pot Wed Mar 4 14:46:14 2009
@@ -944,4 +944,13 @@
msgstr ""
msgid "No valid value for the type of this entry was supplied."
+msgstr ""
+
+msgid "Specified entry does not exist."
+msgstr ""
+
+msgid "The duration of the edited was not supplied."
+msgstr ""
+
+msgid "No valid value for the duration of this entry was supplied."
msgstr ""