PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/lib/classes/log.php

https://bitbucket.org/designbyheart/original
PHP | 231 lines | 165 code | 22 blank | 44 comment | 13 complexity | 6c601b09ca6df0ac084628b4d35cff51 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: predragjevtic
  5. * Date: 9/15/12
  6. * Time: 2:34 AM
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. class Log extends DatabaseObject
  10. {
  11. protected static $table_name = "log";
  12. protected static $db_fields = array('id', 'eventTime', 'userID', 'note', 'refID', 'type', 'selectedProducts');
  13. public $id;
  14. public $userID;
  15. public $note;
  16. public $refID;
  17. public $type;
  18. public $eventTime;
  19. public $selectedProducts;
  20. // Common Database Methods
  21. public static function find_all()
  22. {
  23. return self::find_by_sql("SELECT * FROM " . self::$table_name);
  24. }
  25. /*
  26. public function find_statistic($id, $date){
  27. return array_shift(self::find_by_sql("SELECT count(id) as note FROM admin_log where id=(select id from users where first_name = '$id') and date = '$date');
  28. } */
  29. public static function find_by_id($id = 0)
  30. {
  31. $result_array = self::find_by_sql("SELECT * FROM " . self::$table_name . " WHERE id={$id} LIMIT 1");
  32. return !empty($result_array) ? array_shift($result_array) : false;
  33. }
  34. public static function removeItemFromCart($productID, $clientID){
  35. $productsArray = explode(',', Log::usersProducts($clientID));
  36. if(in_array($productID, $productsArray)){
  37. unset($productsArray[$productID]);
  38. $log = new Log();
  39. $log->eventTime = time();
  40. $log->note = "Klijent uklonio proizvod". $productID." iz porudžbine";
  41. $log->selectedProducts = implode(',', $productsArray);
  42. $log->userID = $clientID;
  43. $log->save();
  44. }
  45. }
  46. public static function find_by_sql($sql = "")
  47. {
  48. global $database;
  49. $result_set = $database->query($sql);
  50. $object_array = array();
  51. while ($row = $database->fetch_array($result_set)) {
  52. $object_array[] = self::instantiate($row);
  53. }
  54. return $object_array;
  55. }
  56. public static function usersProducts($userID)
  57. {
  58. global $database;
  59. $sql = "SELECT selectedProducts FROM " . self::$table_name . " where userID = '{$userID}' ORDER BY id DESC LIMIT 1";
  60. $result_set = $database->query($sql);
  61. $row = $database->fetch_array($result_set);
  62. if ($row) {
  63. return array_shift($row);
  64. } else {
  65. return false;
  66. }
  67. }
  68. public static function removeAllProducts($clientID){
  69. $log = new Log();
  70. $log->eventTime = time();
  71. $log->note = "Klijent otkazao sve proizvode";
  72. $log->selectedProducts = 0;
  73. $log->userID = $clientID;
  74. $log->save();
  75. }
  76. public static function addProductToBasket($productID, $clientID = NULL)
  77. {
  78. if ($clientID != NULL) {
  79. $products = explode(',',self::usersProducts($clientID));
  80. $products[] = $productID;
  81. $log = new Log();
  82. $log->eventTime = time();
  83. $log->note = "Klijent dodao proizvod u korpu";
  84. $log->selectedProducts = implode(',', $products);
  85. $log->userID = $clientID;
  86. $log->save();
  87. } else {
  88. return false;
  89. }
  90. return $products;
  91. }
  92. public static function count_all()
  93. {
  94. global $database;
  95. $sql = "SELECT COUNT(*) FROM " . self::$table_name;
  96. $result_set = $database->query($sql);
  97. $row = $database->fetch_array($result_set);
  98. return array_shift($row);
  99. }
  100. private static function instantiate($record)
  101. {
  102. // Could check that $record exists and is an array
  103. $object = new self;
  104. // Simple, long-form approach:
  105. // $object->id = $record['id'];
  106. // $object->username = $record['username'];
  107. // $object->password = $record['password'];
  108. // $object->first_name = $record['first_name'];
  109. // $object->last_name = $record['last_name'];
  110. // More dynamic, short-form approach:
  111. foreach ($record as $attribute => $value) {
  112. if ($object->has_attribute($attribute)) {
  113. $object->$attribute = $value;
  114. }
  115. }
  116. return $object;
  117. }
  118. private function has_attribute($attribute)
  119. {
  120. // We don't care about the value, we just want to know if the key exists
  121. // Will return true or false
  122. return array_key_exists($attribute, $this->attributes());
  123. }
  124. protected function attributes()
  125. {
  126. // return an array of attribute names and their values
  127. $attributes = array();
  128. foreach (self::$db_fields as $field) {
  129. if (property_exists($this, $field)) {
  130. $attributes[$field] = $this->$field;
  131. }
  132. }
  133. return $attributes;
  134. }
  135. protected function sanitized_attributes()
  136. {
  137. global $database;
  138. $clean_attributes = array();
  139. // sanitize the values before submitting
  140. // Note: does not alter the actual value of each attribute
  141. foreach ($this->attributes() as $key => $value) {
  142. $clean_attributes[$key] = $database->escape_value($value);
  143. }
  144. return $clean_attributes;
  145. }
  146. public function save()
  147. {
  148. // A new record won't have an id yet.
  149. return isset($this->id) ? $this->update() : $this->create();
  150. }
  151. public function create()
  152. {
  153. global $database;
  154. // Don't forget your SQL syntax and good habits:
  155. // - INSERT INTO table (key, key) VALUES ('value', 'value')
  156. // - single-quotes around all values
  157. // - escape all values to prevent SQL injection
  158. $attributes = $this->sanitized_attributes();
  159. $sql = "INSERT INTO " . self::$table_name . " (";
  160. $sql .= join(", ", array_keys($attributes));
  161. $sql .= ") VALUES ('";
  162. $sql .= join("', '", array_values($attributes));
  163. $sql .= "')";
  164. if ($database->query($sql)) {
  165. $this->id = $database->insert_id();
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. }
  171. public function update()
  172. {
  173. global $database;
  174. // Don't forget your SQL syntax and good habits:
  175. // - UPDATE table SET key='value', key='value' WHERE condition
  176. // - single-quotes around all values
  177. // - escape all values to prevent SQL injection
  178. $attributes = $this->sanitized_attributes();
  179. $attribute_pairs = array();
  180. foreach ($attributes as $key => $value) {
  181. $attribute_pairs[] = "{$key}='{$value}'";
  182. }
  183. $sql = "UPDATE " . self::$table_name . " SET ";
  184. $sql .= join(", ", $attribute_pairs);
  185. $sql .= " WHERE id=" . $database->escape_value($this->id);
  186. $database->query($sql);
  187. return ($database->affected_rows() == 1) ? true : false;
  188. }
  189. public function delete()
  190. {
  191. global $database;
  192. // Don't forget your SQL syntax and good habits:
  193. // - DELETE FROM table WHERE condition LIMIT 1
  194. // - escape all values to prevent SQL injection
  195. // - use LIMIT 1
  196. $sql = "DELETE FROM " . self::$table_name;
  197. $sql .= " WHERE id=" . $database->escape_value($this->id);
  198. $sql .= " LIMIT 1";
  199. $database->query($sql);
  200. return ($database->affected_rows() == 1) ? true : false;
  201. // NB: After deleting, the instance of User still
  202. // exists, even though the database entry does not.
  203. // This can be useful, as in:
  204. // echo $user->first_name . " was deleted";
  205. // but, for example, we can't call $user->update()
  206. // after calling $user->delete().
  207. }
  208. }
  209. ?>