accountModel.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. class accountModel extends Staple_Model
  3. {
  4. private $db;
  5. private $id;
  6. private $username;
  7. private $password;
  8. private $pin;
  9. private $tempPin;
  10. private $firstName;
  11. private $lastName;
  12. private $authLevel;
  13. private $batchId;
  14. private $supervisorId;
  15. private $type;
  16. private $status;
  17. /**
  18. * @return mixed
  19. */
  20. public function getId()
  21. {
  22. return $this->id;
  23. }
  24. /**
  25. * @param mixed $id
  26. */
  27. public function setId($id)
  28. {
  29. $this->id = $id;
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function getUsername()
  35. {
  36. return $this->username;
  37. }
  38. /**
  39. * @param mixed $username
  40. */
  41. public function setUsername($username)
  42. {
  43. $this->username = $username;
  44. }
  45. /**
  46. * @return mixed
  47. */
  48. public function getPassword()
  49. {
  50. return $this->password;
  51. }
  52. /**
  53. * @param mixed $password
  54. */
  55. public function setPassword($password)
  56. {
  57. $this->password = $password;
  58. }
  59. /**
  60. * @return mixed
  61. */
  62. public function getPin()
  63. {
  64. return $this->pin;
  65. }
  66. /**
  67. * @param mixed $pin
  68. */
  69. public function setPin($pin)
  70. {
  71. $this->pin = $pin;
  72. }
  73. /**
  74. * @return mixed
  75. */
  76. public function getTempPin()
  77. {
  78. return $this->tempPin;
  79. }
  80. /**
  81. * @param mixed $tempPin
  82. */
  83. public function setTempPin($tempPin)
  84. {
  85. $this->tempPin = $tempPin;
  86. }
  87. /**
  88. * @return mixed
  89. */
  90. public function getFirstName()
  91. {
  92. return $this->firstName;
  93. }
  94. /**
  95. * @param mixed $firstName
  96. */
  97. public function setFirstName($firstName)
  98. {
  99. $this->firstName = $firstName;
  100. }
  101. /**
  102. * @return mixed
  103. */
  104. public function getLastName()
  105. {
  106. return $this->lastName;
  107. }
  108. /**
  109. * @param mixed $lastName
  110. */
  111. public function setLastName($lastName)
  112. {
  113. $this->lastName = $lastName;
  114. }
  115. /**
  116. * @return mixed
  117. */
  118. public function getAuthLevel()
  119. {
  120. return $this->authLevel;
  121. }
  122. /**
  123. * @param mixed $authLevel
  124. */
  125. public function setAuthLevel($authLevel)
  126. {
  127. $this->authLevel = $authLevel;
  128. }
  129. /**
  130. * @return mixed
  131. */
  132. public function getBatchId()
  133. {
  134. return $this->batchId;
  135. }
  136. /**
  137. * @param mixed $batchId
  138. */
  139. public function setBatchId($batchId)
  140. {
  141. $this->batchId = $batchId;
  142. }
  143. /**
  144. * @return mixed
  145. */
  146. public function getSupervisorId()
  147. {
  148. return $this->supervisorId;
  149. }
  150. /**
  151. * @param mixed $supervisorId
  152. */
  153. public function setSupervisorId($supervisorId)
  154. {
  155. $this->supervisorId = $supervisorId;
  156. }
  157. /**
  158. * @return mixed
  159. */
  160. public function getType()
  161. {
  162. return $this->type;
  163. }
  164. /**
  165. * @param mixed $type
  166. */
  167. public function setType($type)
  168. {
  169. $this->type = $type;
  170. }
  171. /**
  172. * @return mixed
  173. */
  174. public function getStatus()
  175. {
  176. return $this->status;
  177. }
  178. /**
  179. * @param mixed $status
  180. */
  181. public function setStatus($status)
  182. {
  183. $this->status = $status;
  184. }
  185. function __construct()
  186. {
  187. $this->db = Staple_DB::get();
  188. }
  189. function load($id)
  190. {
  191. $sql = "SELECT id, username, firstName, lastName, authLevel, batchId, supervisorId, type, status FROM accounts WHERE id = '".$this->db->real_escape_string($id)."'";
  192. $query = $this->db->query($sql);
  193. $result = $query->fetch_assoc();
  194. $data = array();
  195. $data['id'] = $result['id'];
  196. $data['username'] = $result['username'];
  197. $data['firstName'] = $result['firstName'];
  198. $data['lastName'] = $result['lastName'];
  199. $data['level'] = $result['authLevel'];
  200. $data['supervisor'] = $result['supervisorId'];
  201. $data['type'] = $result['type'];
  202. $data['status'] = $result['status'];
  203. return $data;
  204. }
  205. function save()
  206. {
  207. if(isset($this->id))
  208. {
  209. //Check if username already exists
  210. $sql = "SELECT username FROM accounts WHERE username = '".$this->db->real_escape_string($this->username)."' AND id <> '".$this->db->real_escape_string($this->id)."'";
  211. $query = $this->db->query($sql);
  212. if($query->num_rows == 0)
  213. {
  214. $sql = "
  215. UPDATE accounts SET
  216. username = '".$this->db->real_escape_string($this->username)."',
  217. firstName = '".$this->db->real_escape_string($this->firstName)."',
  218. lastName = '".$this->db->real_escape_string($this->lastName)."',
  219. authLevel = '".$this->db->real_escape_string($this->authLevel)."',
  220. supervisorId = '".$this->db->real_escape_string($this->supervisorId)."',
  221. type = '".$this->db->real_escape_string($this->type)."',
  222. status = '".$this->db->real_escape_string($this->status)."'
  223. WHERE id = '".$this->db->real_escape_string($this->id)."'
  224. ";
  225. if($this->db->query($sql))
  226. {
  227. return true;
  228. }
  229. }
  230. }
  231. else
  232. {
  233. //Build username
  234. $username = strtolower(substr($this->firstName,0,1).$this->lastName);
  235. //Check if username already exists
  236. $sql = "SELECT username FROM accounts WHERE username = '".$this->db->real_escape_string($username)."'";
  237. $query = $this->db->query($sql);
  238. if($query->num_rows == 0)
  239. {
  240. //Check if PIN already exists
  241. $sql = "SELECT pin FROM accounts WHERE pin = '".$this->db->real_escape_string(sha1($this->pin))."'";
  242. $query = $this->db->query($sql);
  243. if($query->num_rows > 0)
  244. {
  245. $pin = $this->generatePin();
  246. }
  247. else
  248. {
  249. $pin = $this->pin;
  250. }
  251. $sql = "
  252. INSERT INTO accounts (username,password,pin,firstName,lastName,authLevel,batchId,supervisorId,type,status)
  253. VALUES (
  254. '".$this->db->real_escape_string($username)."',
  255. '".$this->db->real_escape_string(sha1('taketime'))."',
  256. '".$this->db->real_escape_string(sha1($pin))."',
  257. '".$this->db->real_escape_string($this->firstName)."',
  258. '".$this->db->real_escape_string($this->lastName)."',
  259. '".$this->db->real_escape_string($this->authLevel)."',
  260. '".$this->db->real_escape_string('0')."',
  261. '".$this->db->real_escape_string($this->supervisorId)."',
  262. '".$this->db->real_escape_string($this->type)."',
  263. '".$this->db->real_escape_string('1')."'
  264. );
  265. ";
  266. if($this->db->query($sql))
  267. {
  268. $id = $this->db->insert_id;
  269. $this->tempPin = $pin;
  270. $account = new userModel();
  271. $userInfo = $account->userInfo($id);
  272. $audit = new auditModel();
  273. $audit->setUserId($userInfo['id']);
  274. $audit->setAction('New Account Created');
  275. $audit->setItem($account->getUsername()." created account.");
  276. $audit->save();
  277. return true;
  278. }
  279. }
  280. }
  281. }
  282. function generatePin()
  283. {
  284. $pin = array();
  285. for($i=0;$i<4;$i++)
  286. {
  287. $pin[$i] = rand(0,9);
  288. }
  289. $pin = implode("",$pin);
  290. $sql = "SELECT pin FROM accounts WHERE pin = '".$this->db->real_escape_string(sha1($pin))."'";
  291. $query = $this->db->query($sql);
  292. if($query->num_rows == 0)
  293. {
  294. return $pin;
  295. }
  296. else
  297. {
  298. $this->generatePin();
  299. }
  300. }
  301. function resetPin($id)
  302. {
  303. $pin = $this->generatePin();
  304. $this->tempPin = $pin;
  305. $sql = "UPDATE accounts SET pin='".$this->db->real_escape_string(sha1($pin))."' WHERE id = '".$this->db->real_escape_string($id)."'";
  306. if($this->db->query($sql))
  307. {
  308. $account = new userModel();
  309. $userInfo = $account->userInfo($id);
  310. $audit = new auditModel();
  311. $audit->setUserId($userInfo['id']);
  312. $audit->setAction('PIN Reset');
  313. $audit->setItem($account->getUsername()." reset users PIN.");
  314. $audit->save();
  315. return true;
  316. }
  317. }
  318. }
  319. ?>