This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libs.php
176 lines (146 loc) · 5.48 KB
/
libs.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
//report everything, except notices.
error_reporting(E_ALL & ~E_NOTICE);
//start the session
session_start();
if (!isset($_SESSION['CREATED'])) {
$_SESSION['CREATED'] = time();
}
// session started more than 30 minutes ago
else if (time() - $_SESSION['CREATED'] > 1800) {
session_regenerate_id(true); // change session ID for the current session and invalidate old session ID
$_SESSION['CREATED'] = time();
}
//Using autoloader to fetch models
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
// Initialize db using medoo - a PHP library for DB
//http://medoo.in/
$db = new medoo([
'database_type' => 'mysql',
'database_name' => 'sector',
'server' => 'localhost',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
]);
/**
* Check what should we do with the request
*
* @param $db
*/
function storeData($db) {
if((isset($_POST['inputName']) && $_POST['inputName'] !== "")
&& (isset($_POST['inputSectors']) && $_POST['inputSectors'] !== "")
&& (isset($_POST['inputChecked']) && $_POST['inputChecked'] !== ""))
{
if(isset($_SESSION['data'])) {
updateData($db);
} else {
createData($db);
}
}
}
/**
* Update user or user_categories
*
* @param $db
*/
function updateData($db) {
//update user
if($_POST['inputName'] !== $_SESSION['name']) {
if((new Category($db))->updateUserName($_SESSION['data']['user'],$_POST['inputName'])) {
$_SESSION['name'] = $_POST['inputName'];
}
}
//update user_categories
if($_POST['inputSectors'] !== $_SESSION['sectors']) {
//find which ID's are different
$adding = array_diff($_POST['inputSectors'],$_SESSION['data']['categories']);
$deleting = array_diff($_SESSION['data']['categories'],$_POST['inputSectors']);
if(!empty($adding)) $toAdd = array_diff($adding,$_SESSION['data']['categories']);
if(!empty($deleting)) $toDelete = array_diff($deleting,$_POST['inputSectors']);
//there are some ID's that we have to remove
if(!empty($toDelete)) {
if(!(new Category($db))->deleteCategories($_SESSION['data'],$toDelete)) {
//die(var_dump($toDelete));
$_SESSION['data']['categories'] = array_diff($_SESSION['data']['categories'],$toDelete);
}
}
//there are ID's we have to add
if(!empty($toAdd)) {
if((new Category($db))->insertCategories($_SESSION['data'],$toAdd)) {
array_push($_SESSION['data']['categories'],$toAdd);
}
}
//update the data we have in the session
$_SESSION['sectors'] = $_POST['inputSectors'];
}
}
/**
* Creates a new record in the db and init session data
*
* @param $db
*/
function createData($db) {
$success = (new Category($db))->store($_POST['inputName'], $_POST['inputSectors'], (($_POST['inputChecked'][0] === "on") ? 1 : 0));
$_SESSION['data'] = $success;
$_SESSION['name'] = $_POST['inputName'];
$_SESSION['sectors'] = $_POST['inputSectors'];
$_SESSION['checked'] = $_POST['inputChecked'];
}
/**
* Display errors or success messages
*/
function getErrors() {
if(isset($_POST['save']) && isset($success)) {
if($success) {
echo '<div class="alert alert-success" role="alert"><strong>Well done!</strong> Your data is stored.</div>';
} else if(!$success) {
echo '<div class="alert alert-warning" role="alert"><strong>Oops!</strong> There is an error in your form.</div>';
}
}
}
/**
* Populate select with options
*
* @param $db
*/
function getOptions($db) {
$cat = new Category($db);
//parent category
foreach($cat->getCategories(['parentID' => null, 'ORDER' => 'name']) as $parent) {
$level = 0; //because a optgroup can't be nested, use css padding fix
echo "<optgroup value='".$parent['ID']."' style='padding-left:". (15 * $level). "px' label='".$parent['name']."'>";
//child category
foreach($cat->getCategories(['parentID' => $parent['ID'], 'ORDER' => 'name']) as $child) {
$level = 1;
$data = $cat->getCategories(['parentID' => $child['ID'], 'ORDER' => 'name']);
//grandchild category
if($data) {
echo "<optgroup value='".$child['ID']."' style='padding-left:". (15 * $level). "px' label='".$child['name']."'>";
foreach($data as $grandChild) {
//check if the option is in the session
echo "<option value='".$grandChild['ID']."'";
if(isset($_SESSION['sectors'])) {
if(in_array($grandChild['ID'],$_SESSION['sectors'])) {
echo " selected='selected'";
}
}
echo ">".$grandChild['name']."</option>";
}
echo "</optgroup>";
} else {
echo "<option value='".$child['ID']."'";
if(isset($_SESSION['sectors'])) {
if(in_array($child['ID'],$_SESSION['sectors'])) {
echo " selected='selected'";
}
}
echo ">".$child['name']."</option>";
}
}
echo "</optgroup>";
}
}