· 9 years ago · Jun 19, 2017, 09:08 AM
1<?php
2
3/**
4 * Created by PhpStorm.
5 * Auther @ Mads Roloff - Rights reservede to Author
6 * Date: 17-05-2017
7 */
8class user
9{
10
11 private $db;
12 public $iUserID = -1;
13 public $vcImage;
14 public $vcUserName;
15 public $vcPassword;
16 public $vcFirstName;
17 public $vcLastName;
18 public $iOrgID;
19 public $vcAddress;
20 public $iZip;
21 public $vcCity;
22 public $vcEmail;
23 public $vcPhone1;
24 public $vcPhone2;
25 public $vcPhone3;
26 public $daCreated;
27 public $iSuspended;
28 public $iDeleted;
29
30 public $arrFormElms = array();
31 public $arrLabels = array();
32 public $arrColumns = array();
33 public $arrValues = array();
34 public $unset = array();
35
36 public function __construct()
37 {
38 global $db;
39 $this->db = $db;
40 $this->CreateTable();
41 $this->Table = "user";
42
43 $this->arrFormElm = array(
44 "iUserID" => array("label" => "", "type" => "hidden", "placeholder" => "", "required" => "data-requried='1'", "dbname" => "iUserID", "Filter" => FILTER_SANITIZE_NUMBER_INT),
45 "vcFirstName" => array("label" => "First Name", "type" => "text", "placeholder" => "First name", "required" => "data-requried='1'", "dbname" => "vcFirstName", "Filter" => FILTER_SANITIZE_STRING),
46 "vcLastName" => array("label" => "Last Name", "type" => "text", "placeholder" => "Last name", "required" => "data-requried='1'", "dbname" => "vcLastName", "Filter" => FILTER_SANITIZE_STRING),
47 "vcUserName" => array("label" => "user name", "type" => "text", "placeholder" => "Username", "required" => "data-requried='1'", "dbname" => "vcUserName", "Filter" => FILTER_SANITIZE_STRING),
48 "vcPassword" => array("label" => "password", "type" => "password", "placeholder" => "Password", "required" => "data-requried='1'", "dbname" => "vcPassword", "Filter" => FILTER_SANITIZE_STRING),
49 "vcAddress" => array("label" => "Adresse", "type" => "text", "placeholder" => "Address", "required" => "data-requried='1'", "dbname" => "vcAddress", "Filter" => FILTER_SANITIZE_STRING),
50 "iZip" => array("label" => "Zip code", "type" => "text", "placeholder" => "Zip Code", "required" => "data-requried='1'", "dbname" => "iZip", "Filter" => FILTER_SANITIZE_NUMBER_INT),
51 "vcCity" => array("label" => "City", "type" => "text", "placeholder" => "City", "required" => "data-requried='1'", "dbname" => "vcCity", "Filter" => FILTER_SANITIZE_STRING),
52 "vcEmail" => array("label" => "Email", "type" => "email", "placeholder" => "Email", "required" => "data-requried='1'", "dbname" => "vcEmail", "Filter" => FILTER_SANITIZE_STRING),
53 "vcPhone1" => array("label" => "Phone Number", "type" => "tel", "placeholder" => "Phone Number", "required" => "data-requried='1'", "dbname" => "vcPhone1", "Filter" => FILTER_SANITIZE_NUMBER_INT),
54 "vcPhone2" => array("label" => "Phone Number", "type" => "tel", "placeholder" => "Phone Number", "required" => "data-requried='1'", "dbname" => "vcPhone2", "Filter" => FILTER_SANITIZE_NUMBER_INT),
55 "daCreated" => array("label" => "", "type" => "hidden", "placeholder" => "", "required" => "data-requried='1'", "dbname" => "daCreated", "Filter" => FILTER_SANITIZE_NUMBER_INT),
56
57 //Unneeded fields
58 //"iSuspended" => array(),
59 //"iDeleted" => array(),
60 //"iOrgID" => array("label" => "", "type" => "hidden", "placeholder" => "",),
61 //"vcImage" => array("label" => "Image", "shpwn", "type" => "text", "require" => "", "placeholder" => "Pick a image",),
62
63 );
64
65 }
66
67 public function GetSelect()
68 {
69 $sql = "select * from $this->Table WHERE iDeleted = 0";
70 return $this->db->_fetch_array($sql, array());
71 }
72
73 public function GetList()
74 {
75 $sql = "SELECT * FROM $this->Table WHERE iDeleted = 0";
76 //Shows Column names from "user"
77 $sqlLabels = "SHOW FULL COLUMNS FROM $this->Table";
78 $this->arrLabels = $this->db->_fetch_array($sqlLabels, array());
79 $this->arrValues = $this->db->_fetch_array($sql, array());
80
81 //UNSET = Columns to avoid in LIST
82 $this->unset = array(
83 "iUserID",
84 "vcPassword",
85 "vcPassword2",
86 "iUserRole",
87 "vcPhone2",
88 "vcPhone3",
89 "iOrgID",
90 "daCreated",
91 "iSuspended",
92 "iDeleted",
93 "vcCity",
94 "iZip"
95 );
96 }
97
98 public function getDetails($iUserID)
99 {
100 $sql = "SELECT * FROM $this->Table WHERE iDeleted = 0 AND IUserID = ?";
101 //Shows Column names from "user"
102 $sqlLabels = "SHOW FULL COLUMNS FROM $this->Table";
103 $this->arrLabels = $this->db->_fetch_array($sqlLabels, array());
104 $this->arrValues = $this->db->_fetch_array($sql, array($iUserID));
105
106 //UNSET = Columns to avoid in LIST
107 $this->unset = array(
108 "iUserID",
109 "vcPassword",
110 "vcPassword2",
111 "iUserRole",
112 "vcPhone2",
113 "vcPhone3",
114 "iOrgID",
115 "daCreated",
116 "iSuspended",
117 "iDeleted",
118 "vcCity",
119 "iZip"
120 );
121 }
122
123 public function getUser($iUserID)
124 {
125 $this->iUserID = $iUserID;
126 $sql = "SELECT * FROM $this->Table WHERE iDeleted = 0 AND iUserID = ?";
127 $row = $this->db->_fetch_array($sql, array($this->iUserID));
128 foreach ($row[0] as $key => $value) {
129 $this->$key = $value;
130 }
131 }
132
133 public function UpdateUser($iUserID)
134 {
135 //unsets IUserID from the array
136 unset($this->arrFormElm["iUserID"]);
137
138 //MAkes a foreach that gets "filter" from $arrFormElm
139 foreach ($this->arrFormElm as $key => $value) {
140 $f[$key] = filter_input(INPUT_POST, $key, $value["Filter"]);
141 }
142 //uses the value of the "filter"'s and takes the value from the $_POST and inserts them into an array ordered by the "FIlTER"
143 $params = array_values($f);
144
145 //iUserID = IUserID
146 $this->iUserID = $iUserID;
147
148 //awesome SQL that makes an string that looks like this
149 //"UPDATE user SET vcFirstName = ?, vcLastName = ?, vcUserName = ?, vcPassword = ?, vcAddress = ?, iZip = ?, vcCity = ?, vcEmail = ?, vcPhone1 = ?, vcPhone2 = ? WHERE iUserID = 12"
150 echo $sql = "UPDATE $this->Table SET " . implode(array_keys($this->arrFormElm), " = ?, ") . " = ? WHERE iUserID = $this->iUserID";
151 return $this->db->_query($sql, $params);
152 }
153
154 public function DeleteUser($iUserID)
155 {
156 $this->iUserID = $iUserID;
157 //SQL that sets iDeleted to 1
158 $sql = "UPDATE $this->Table SET iDeleted = 1 WHERE iUserID = ?";
159 return $this->db->_query($sql, array($iUserID));
160 }
161
162 public function SaveUser()
163 {
164
165 }
166
167 Public function CreateUser($iUserID)
168 {
169
170 //unsets IUserID from the array
171 unset($this->arrFormElm["iUserID"]);
172 unset($this->arrFormElm["daCreated"]);
173
174 //Makes a foreach that gets "filter" from $arrFormElm
175 foreach ($this->arrFormElm as $key => $value) {
176 $f[$key] = filter_input(INPUT_POST, $key, $value["Filter"]);
177 }
178
179 //uses the value of the "filter"'s and takes the value from the $_POST and inserts them into an array ordered by the "FIlTER"
180 $params = array_values($f);
181
182 //iUserID = IUserID
183 $this->iUserID = $iUserID;
184
185 //awesome SQL that makes an string that looks like this
186 //"UPDATE user SET vcFirstName = ?, vcLastName = ?, vcUserName = ?, vcPassword = ?, vcAddress = ?, iZip = ?, vcCity = ?, vcEmail = ?, vcPhone1 = ?, vcPhone2 = ? WHERE iUserID = 12"
187 $sql = "INSERT INTO $this->Table SET " . implode(array_keys($this->arrFormElm), " = ?, ") . " = ?, daCreated = " . time();
188 return $this->db->_query($sql, $params);
189 }
190
191
192 public function CreateTable()
193 {
194 $sql = "CREATE TABLE IF NOT EXISTS `user` (
195 `iUserID` BIGINT(20) NOT NULL AUTO_INCREMENT,
196 `vcUserName` VARCHAR(255) NOT NULL COMMENT 'Brugernavn' COLLATE 'utf8_unicode_ci',
197 `vcPassword` VARCHAR(50) NOT NULL COMMENT 'Kodeord' COLLATE 'utf8_unicode_ci',
198 `vcFirstName` VARCHAR(255) NOT NULL COMMENT 'Navn' COLLATE 'utf8_unicode_ci',
199 `vcLastName` VARCHAR(255) NOT NULL COMMENT 'Efternavn' COLLATE 'utf8_unicode_ci',
200 `vcAddress` VARCHAR(255) NOT NULL COMMENT 'Adresse' COLLATE 'utf8_unicode_ci',
201 `iZip` MEDIUMINT(10) NOT NULL COMMENT 'Postnummer',
202 `vcCity` VARCHAR(255) NOT NULL COMMENT 'By' COLLATE 'utf8_unicode_ci',
203 `vcEmail` VARCHAR(255) NOT NULL COMMENT 'Email' COLLATE 'utf8_unicode_ci',
204 `vcPhone1` VARCHAR(255) NOT NULL COMMENT 'Telefon 1' COLLATE 'utf8_unicode_ci',
205 `vcPhone2` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Telefon 2' COLLATE 'utf8_unicode_ci',
206 `vcPhone3` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Telefon 3' COLLATE 'utf8_unicode_ci',
207 `iOrgID` BIGINT(20) NOT NULL DEFAULT '1' COMMENT 'Organisation',
208 `daCreated` BIGINT(20) NOT NULL COMMENT 'Oprettet',
209 `iSuspended` TINYINT(4) NOT NULL DEFAULT '0' COMMENT 'Suspenderet',
210 `iDeleted` TINYINT(4) NOT NULL DEFAULT '0',
211 PRIMARY KEY(`iUserID`))
212 COLLATE = 'utf8_unicode_ci' ENGINE = MyISAM AUTO_INCREMENT = 12";
213
214 $this->db->_query($sql);
215
216
217 $sql = " CREATE TABLE IF NOT EXISTS `usersession` (
218 `vcSessionID` VARCHAR(32) NOT NULL DEFAULT '',
219 `iUserID` BIGINT(20) NOT NULL DEFAULT '0',
220 `iIpAddress` VARCHAR(24) NOT NULL DEFAULT '',
221 `iIsLoggedIn` TINYINT(1) NOT NULL DEFAULT '0',
222 `daLoginCreated` BIGINT(20) NOT NULL DEFAULT '0',
223 `daLastAction` BIGINT(20) NOT NULL DEFAULT '0')
224 COLLATE = 'utf8_general_ci'ENGINE = MyISAM";
225
226 $this->db->_query($sql);
227
228
229 }
230}