accountModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * @param mixed $id
  19. */
  20. public function setId($id)
  21. {
  22. $this->id = $id;
  23. }
  24. /**
  25. * @param mixed $username
  26. */
  27. public function setUsername($username)
  28. {
  29. $this->username = $username;
  30. }
  31. /**
  32. * @param mixed $password
  33. */
  34. public function setPassword($password)
  35. {
  36. $this->password = $password;
  37. }
  38. /**
  39. * @param mixed $pin
  40. */
  41. public function setPin($pin)
  42. {
  43. $this->pin = $pin;
  44. }
  45. /**
  46. * @param mixed $firstName
  47. */
  48. public function setFirstName($firstName)
  49. {
  50. $this->firstName = $firstName;
  51. }
  52. /**
  53. * @param mixed $lastName
  54. */
  55. public function setLastName($lastName)
  56. {
  57. $this->lastName = $lastName;
  58. }
  59. /**
  60. * @param mixed $authLevel
  61. */
  62. public function setAuthLevel($authLevel)
  63. {
  64. $this->authLevel = $authLevel;
  65. }
  66. /**
  67. * @param mixed $batchId
  68. */
  69. public function setBatchId($batchId)
  70. {
  71. $this->batchId = $batchId;
  72. }
  73. /**
  74. * @param mixed $supervisorId
  75. */
  76. public function setSupervisorId($supervisorId)
  77. {
  78. $this->supervisorId = $supervisorId;
  79. }
  80. /**
  81. * @param mixed $type
  82. */
  83. public function setType($type)
  84. {
  85. $this->type = $type;
  86. }
  87. /**
  88. * @param mixed $status
  89. */
  90. public function setStatus($status)
  91. {
  92. $this->status = $status;
  93. }
  94. /**
  95. * @return mixed
  96. */
  97. public function getTempPin()
  98. {
  99. return $this->tempPin;
  100. }
  101. /**
  102. * @return mixed
  103. */
  104. public function getFirstName()
  105. {
  106. return $this->firstName;
  107. }
  108. /**
  109. * @return mixed
  110. */
  111. public function getLastName()
  112. {
  113. return $this->lastName;
  114. }
  115. function __construct()
  116. {
  117. $this->db = Staple_DB::get();
  118. }
  119. function save()
  120. {
  121. if(isset($this->id))
  122. {
  123. //Edit user
  124. }
  125. else
  126. {
  127. //Build username
  128. $username = strtolower(substr($this->firstName,0,1).$this->lastName);
  129. //Check if username already exists
  130. $sql = "SELECT username FROM accounts WHERE username = '".$this->db->real_escape_string($username)."'";
  131. $query = $this->db->query($sql);
  132. if($query->num_rows == 0)
  133. {
  134. //Check if PIN already exists
  135. $sql = "SELECT pin FROM accounts WHERE pin = '".$this->db->real_escape_string(sha1($this->pin))."'";
  136. $query = $this->db->query($sql);
  137. if($query->num_rows > 0)
  138. {
  139. $pin = $this->generatePin();
  140. }
  141. else
  142. {
  143. $pin = $this->pin;
  144. }
  145. $sql = "
  146. INSERT INTO accounts (username,password,pin,firstName,lastName,authLevel,batchId,supervisorId,type,status)
  147. VALUES (
  148. '".$this->db->real_escape_string($username)."',
  149. '".$this->db->real_escape_string(sha1('taketime'))."',
  150. '".$this->db->real_escape_string(sha1($pin))."',
  151. '".$this->db->real_escape_string($this->firstName)."',
  152. '".$this->db->real_escape_string($this->lastName)."',
  153. '".$this->db->real_escape_string($this->authLevel)."',
  154. '".$this->db->real_escape_string('0')."',
  155. '".$this->db->real_escape_string($this->supervisorId)."',
  156. '".$this->db->real_escape_string($this->type)."',
  157. '".$this->db->real_escape_string('1')."'
  158. );
  159. ";
  160. if($this->db->query($sql))
  161. {
  162. $this->tempPin = $pin;
  163. return true;
  164. }
  165. }
  166. }
  167. }
  168. function generatePin()
  169. {
  170. $pin = array();
  171. for($i=0;$i<4;$i++)
  172. {
  173. $pin[$i] = rand(0,9);
  174. }
  175. $pin = implode("",$pin);
  176. $sql = "SELECT pin FROM accounts WHERE pin = '".$this->db->real_escape_string(sha1($pin))."'";
  177. $query = $this->db->query($sql);
  178. if($query->num_rows == 0)
  179. {
  180. return $pin;
  181. }
  182. else
  183. {
  184. $this->generatePin();
  185. }
  186. }
  187. }
  188. ?>