-
Notifications
You must be signed in to change notification settings - Fork 0
/
logcache.php
68 lines (62 loc) · 2.31 KB
/
logcache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
require_once 'libs/models/logentry.php';
require_once 'libs/models/geocache.php';
require_once 'libs/models/trackable.php';
require_once 'libs/utils.php';
if (!loggedIn()) {
showView('login.php', array('error' => "You must login to view this page."));
}
if (isset($_GET['id'])) {
$id = (int) $_GET['id'];
} else {
$id = $_POST['id'];
}
$geocache = Geocache::getGeocacheById($id);
$newLogentry = new Logentry();
//show the form
if (empty($_POST)) {
showView('logcache.php', array('logentry' => $newLogentry, 'geocache' => $geocache));
}
//if there is data in POST, set the fields in the object
$newLogentry->setUser($_SESSION['user']);
$newLogentry->setGeocacheid($geocache->getId());
$newLogentry->setVisittype($_POST['visittype']);
$newLogentry->setComment($_POST['comment']);
$trackablelogs = array();
//create trackablelogs for the selected actions in the form
foreach (array_keys($_POST) as $action) {
if (preg_match('/\d+_action/', $action)){
$expl = explode('_', $action);
$trackableid = $expl[0];
if ($_POST[$action] == 'grab') {
$trackablelogs[] = new Trackablelog('grab', $trackableid, null);
}
if ($_POST[$action] == 'drop') {
$trackablelogs[] = new Trackablelog('drop', $trackableid, null);
}
if ($_POST[$action] == 'visit'){
$trackablelogs[] = new Trackablelog('visit', $trackableid, null);
}
}
}
//check the tracking codes
$trackingcodeErrors = array();
foreach($trackablelogs as $trackablelog) {
if ($trackablelog->getAction() == 'grab') {
$trackable = Trackable::getTrackableById($trackablelog->getTrackableid());
if (!($trackable->getTrackingcode() ===
$_POST[$trackable->getId().'_trackingcode'])) {
$trackingcodeErrors[] = "Wrong tracking code for trackable ".$trackable->getName();
}
}
}
if ($newLogentry->isValid() && empty($trackingcodeErrors)) {
$newLogentry->insertIntoDb();
foreach ($trackablelogs as $trackablelog) {
$trackablelog->insertIntoDb($newLogentry);
}
header('Location: geocacheview.php?id=' . $geocache->getId());
} else {
$logerrors = $newLogentry->errors;
showView('logcache.php', array('logentry' => $newLogentry, 'geocache' => $geocache, 'errors' => array_merge($logerrors, $trackingcodeErrors)));
}