add shift create model

This commit is contained in:
Philip Häusler 2014-12-07 17:34:29 +01:00
parent 156db1eadd
commit fa0e38ebff
5 changed files with 63 additions and 28 deletions

View File

@ -1,5 +1,25 @@
<?php <?php
/**
* Create a new shift.
* @return new shift id or false
*/
function Shift_create($shift) {
$result = sql_query("INSERT INTO `Shifts` SET
`start`=" . sql_escape($shift['start']) . ",
`end`=" . sql_escape($shift['end']) . ",
`RID`=" . sql_escape($shift['RID']) . ",
`name`=" . sql_null($shift['name']) . ",
`URL`=" . sql_null($shift['URL']) . ",
`PSID`=" . sql_null($shift['PSID']));
if ($result === false)
return false;
return sql_id();
}
/**
* Return users shifts.
*/
function Shifts_by_user($user) { function Shifts_by_user($user) {
return sql_select(" return sql_select("
SELECT * SELECT *

View File

@ -5,16 +5,23 @@
*/ */
function sql_close() { function sql_close() {
global $sql_connection; global $sql_connection;
return $sql_connection->close(); return $sql_connection->close();
} }
/**
* Return NULL if given value is null.
*/
function sql_null($value = null) {
return $value == null ? 'NULL' : ("'" . sql_escape($value) . "'");
}
/** /**
* Start new transaction. * Start new transaction.
*/ */
function sql_transaction_start() { function sql_transaction_start() {
global $sql_nested_transaction_level; global $sql_nested_transaction_level;
if ($sql_nested_transaction_level ++ == 0) if ($sql_nested_transaction_level ++ == 0)
return sql_query("BEGIN"); return sql_query("BEGIN");
else else
@ -26,7 +33,7 @@ function sql_transaction_start() {
*/ */
function sql_transaction_commit() { function sql_transaction_commit() {
global $sql_nested_transaction_level; global $sql_nested_transaction_level;
if (-- $sql_nested_transaction_level == 0) if (-- $sql_nested_transaction_level == 0)
return sql_query("COMMIT"); return sql_query("COMMIT");
else else
@ -38,7 +45,7 @@ function sql_transaction_commit() {
*/ */
function sql_transaction_rollback() { function sql_transaction_rollback() {
global $sql_nested_transaction_level; global $sql_nested_transaction_level;
if (-- $sql_nested_transaction_level == 0) if (-- $sql_nested_transaction_level == 0)
return sql_query("ROLLBACK"); return sql_query("ROLLBACK");
else else
@ -48,17 +55,17 @@ function sql_transaction_rollback() {
/** /**
* Logs an sql error. * Logs an sql error.
* *
* @param string $message * @param string $message
* @return false * @return false
*/ */
function sql_error($message) { function sql_error($message) {
sql_close(); sql_close();
$message = trim($message) . "\n"; $message = trim($message) . "\n";
$message .= debug_string_backtrace() . "\n"; $message .= debug_string_backtrace() . "\n";
error_log('mysql_provider error: ' . $message); error_log('mysql_provider error: ' . $message);
return false; return false;
} }
@ -77,19 +84,19 @@ function sql_error($message) {
*/ */
function sql_connect($host, $user, $pass, $db) { function sql_connect($host, $user, $pass, $db) {
global $sql_connection; global $sql_connection;
$sql_connection = new mysqli($host, $user, $pass, $db); $sql_connection = new mysqli($host, $user, $pass, $db);
if ($sql_connection->connect_errno) if ($sql_connection->connect_errno)
return sql_error("Unable to connect to MySQL: " . $sql_connection->connect_error); return sql_error("Unable to connect to MySQL: " . $sql_connection->connect_error);
$result = $sql_connection->query("SET CHARACTER SET utf8;"); $result = $sql_connection->query("SET CHARACTER SET utf8;");
if (! $result) if (! $result)
return sql_error("Unable to set utf8 character set (" . $sql_connection->errno . ") " . $sql_connection->error); return sql_error("Unable to set utf8 character set (" . $sql_connection->errno . ") " . $sql_connection->error);
$result = $sql_connection->set_charset('utf8'); $result = $sql_connection->set_charset('utf8');
if (! $result) if (! $result)
return sql_error("Unable to set utf8 names (" . $sql_connection->errno . ") " . $sql_connection->error); return sql_error("Unable to set utf8 names (" . $sql_connection->errno . ") " . $sql_connection->error);
return $sql_connection; return $sql_connection;
} }
@ -110,12 +117,12 @@ function sql_select_db($db_name) {
/** /**
* MySQL SELECT query * MySQL SELECT query
* *
* @param string $query * @param string $query
* @return Result array or false on error * @return Result array or false on error
*/ */
function sql_select($query) { function sql_select($query) {
global $sql_connection; global $sql_connection;
$result = $sql_connection->query($query); $result = $sql_connection->query($query);
if ($result) { if ($result) {
$data = array(); $data = array();
@ -129,12 +136,12 @@ function sql_select($query) {
/** /**
* MySQL execute a query * MySQL execute a query
* *
* @param string $query * @param string $query
* @return mysqli_result boolean resource or false on error * @return mysqli_result boolean resource or false on error
*/ */
function sql_query($query) { function sql_query($query) {
global $sql_connection; global $sql_connection;
$result = $sql_connection->query($query); $result = $sql_connection->query($query);
if ($result) { if ($result) {
return $result; return $result;
@ -155,7 +162,7 @@ function sql_id() {
/** /**
* Escape a string for a sql query. * Escape a string for a sql query.
* *
* @param string $query * @param string $query
* @return string * @return string
*/ */
function sql_escape($query) { function sql_escape($query) {
@ -166,7 +173,7 @@ function sql_escape($query) {
/** /**
* Count query result lines. * Count query result lines.
* *
* @param string $query * @param string $query
* @return int Count of result lines * @return int Count of result lines
*/ */
function sql_num_query($query) { function sql_num_query($query) {

View File

@ -1,4 +1,5 @@
<?php <?php
function admin_import_title() { function admin_import_title() {
return _("Frab import"); return _("Frab import");
} }
@ -116,8 +117,11 @@ function admin_import() {
sql_query("DELETE FROM `Room` WHERE `Name`='" . sql_escape($room) . "' LIMIT 1"); sql_query("DELETE FROM `Room` WHERE `Name`='" . sql_escape($room) . "' LIMIT 1");
list($events_new, $events_updated, $events_deleted) = prepare_events($import_file); list($events_new, $events_updated, $events_deleted) = prepare_events($import_file);
foreach ($events_new as $event) foreach ($events_new as $event) {
sql_query("INSERT INTO `Shifts` SET `name`='" . sql_escape($event['name']) . "', `start`=" . sql_escape($event['start']) . ", `end`=" . sql_escape($event['end']) . ", `RID`=" . sql_escape($event['RID']) . ", `PSID`=" . sql_escape($event['PSID']) . ", `URL`='" . sql_escape($event['URL']) . "'"); $result = Shift_create($event);
if ($result === false)
engelsystem_error('Unable to create shift.');
}
foreach ($events_updated as $event) foreach ($events_updated as $event)
sql_query("UPDATE `Shifts` SET `name`='" . sql_escape($event['name']) . "', `start`=" . sql_escape($event['start']) . ", `end`=" . sql_escape($event['end']) . ", `RID`=" . sql_escape($event['RID']) . ", `PSID`=" . sql_escape($event['PSID']) . ", `URL`='" . sql_escape($event['URL']) . "' WHERE `PSID`=" . sql_escape($event['PSID']) . " LIMIT 1"); sql_query("UPDATE `Shifts` SET `name`='" . sql_escape($event['name']) . "', `start`=" . sql_escape($event['start']) . ", `end`=" . sql_escape($event['end']) . ", `RID`=" . sql_escape($event['RID']) . ", `PSID`=" . sql_escape($event['PSID']) . ", `URL`='" . sql_escape($event['URL']) . "' WHERE `PSID`=" . sql_escape($event['PSID']) . " LIMIT 1");
@ -165,7 +169,7 @@ function prepare_rooms($file) {
return array( return array(
$rooms_new, $rooms_new,
$rooms_deleted $rooms_deleted
); );
} }

View File

@ -1,4 +1,5 @@
<?php <?php
function admin_shifts_title() { function admin_shifts_title() {
return _("Create shifts"); return _("Create shifts");
} }
@ -229,14 +230,17 @@ function admin_shifts() {
)) ))
)); ));
} }
} elseif (isset($_REQUEST['submit'])) { } elseif (isset($_REQUEST['submit'])) {
if (! is_array($_SESSION['admin_shifts_shifts']) || ! is_array($_SESSION['admin_shifts_types'])) if (! is_array($_SESSION['admin_shifts_shifts']) || ! is_array($_SESSION['admin_shifts_types']))
redirect(page_link_to('admin_shifts')); redirect(page_link_to('admin_shifts'));
foreach ($_SESSION['admin_shifts_shifts'] as $shift) { foreach ($_SESSION['admin_shifts_shifts'] as $shift) {
sql_query("INSERT INTO `Shifts` SET `start`=" . sql_escape($shift['start']) . ", `end`=" . sql_escape($shift['end']) . ", `RID`=" . sql_escape($shift['RID']) . ", `name`='" . sql_escape($shift['name']) . "'"); $shift['URL'] = null;
$shift_id = sql_id(); $shift['PSID'] = null;
$shift_id = Shift_create($shift);
if ($shift_id === false)
engelsystem_error('Unable to create shift.');
engelsystem_log("Shift created: " . $shift['name'] . " from " . date("Y-m-d H:i", $shift['start']) . " to " . date("Y-m-d H:i", $shift['end'])); engelsystem_log("Shift created: " . $shift['name'] . " from " . date("Y-m-d H:i", $shift['start']) . " to " . date("Y-m-d H:i", $shift['end']));
$needed_angel_types_info = array(); $needed_angel_types_info = array();
foreach ($_SESSION['admin_shifts_types'] as $type_id => $count) { foreach ($_SESSION['admin_shifts_types'] as $type_id => $count) {

View File

@ -82,7 +82,7 @@ $free_pages = array(
'api', 'api',
'credits', 'credits',
'angeltypes', 'angeltypes',
'users' 'users'
); );
// Gewünschte Seite/Funktion // Gewünschte Seite/Funktion
@ -91,10 +91,10 @@ if (! isset($_REQUEST['p']))
$_REQUEST['p'] = isset($user) ? "news" : "login"; $_REQUEST['p'] = isset($user) ? "news" : "login";
if (isset($_REQUEST['p']) && preg_match("/^[a-z0-9_]*$/i", $_REQUEST['p']) && (in_array($_REQUEST['p'], $free_pages) || in_array($_REQUEST['p'], $privileges))) { if (isset($_REQUEST['p']) && preg_match("/^[a-z0-9_]*$/i", $_REQUEST['p']) && (in_array($_REQUEST['p'], $free_pages) || in_array($_REQUEST['p'], $privileges))) {
$p = $_REQUEST['p']; $p = $_REQUEST['p'];
$title = $p; $title = $p;
$content = ""; $content = "";
if ($p == "api") { if ($p == "api") {
require_once realpath(__DIR__ . '/../includes/controller/api.php'); require_once realpath(__DIR__ . '/../includes/controller/api.php');
error("Api disabled temporily."); error("Api disabled temporily.");
@ -222,7 +222,7 @@ echo template_render('../templates/layout.html', array(
'content' => msg() . $content, 'content' => msg() . $content,
'header_toolbar' => header_toolbar(), 'header_toolbar' => header_toolbar(),
'faq_url' => $faq_url, 'faq_url' => $faq_url,
'locale' => $_SESSION['locale'] 'locale' => $_SESSION['locale']
)); ));
counter(); counter();