Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. class workStats {
  4.  
  5. private $id = null;
  6. private $date = null;
  7. private $fields = array();
  8. protected $db = null;
  9. protected $data = array();
  10. private $work_hours = ["day_start", "fixed_day_start",
  11. "day_end", "fixed_day_end",
  12. "work_hours", "last_job_time"];
  13. private $free_hours = ["free_hours", "free_minutes"];
  14. private $free_days = ["free_day"];
  15. private $violations = ["violation"];
  16.  
  17. public function __construct($id, $date, array $fields, Database $db) {
  18. $this->id = $id;
  19. $this->date = $date;
  20. $this->fields = $fields;
  21. $this->db = $db;
  22. $this->init();
  23. }
  24.  
  25. private function join_fields($glue) {
  26. $array = [];
  27. foreach ($this->fields as $field) {
  28. foreach ($this->{$field} as $value) {
  29. $array[] = $value;
  30. }
  31. }
  32. return join($glue, $array);
  33. }
  34.  
  35. private function get_query() {
  36. $query = "SELECT date, ";
  37.  
  38. //Laukeliai kurie bus imami
  39. $query .= $this->join_fields(", ");
  40. $query .= " FROM `employee_work_stats` WHERE `user` = '{$this->id}'"
  41. . " AND `date` LIKE '%{$this->date}%' ORDER BY `date` ASC";
  42. return $query;
  43. }
  44.  
  45. private function set_data($result) {
  46. $arrayToReturn = array();
  47. foreach ($result as $array) {
  48. foreach ($array as $key => $value) {
  49. if ($key !== "date") {
  50. $arrayToReturn[$array['date']][$key] = $value;
  51. }
  52. }
  53. }
  54. return $arrayToReturn;
  55. }
  56.  
  57. private function init() {
  58. $query = $this->get_query();
  59. $result = $this->db->select($query);
  60. $this->data = $this->set_data($result);
  61. var_dump($this->data);
  62. }
  63.  
  64. public function get_data($domain) {
  65. $array = array();
  66. foreach ($domain as $field) {
  67. foreach ($this->{$field} as $value) {
  68. $this->data['date'][$field][$value] = $this->data[$domain][$value];
  69. }
  70. }
  71. return $array;
  72. }
  73.  
  74. }
  75.  
  76. require '../../controller/class/database.php';
  77. $work = new workStats(28, "2017-02", ["free_days", "work_hours"], new Database());
  78.  
  79. $data = $work->get_data(["free_days"]);
  80. foreach ($data as $key => $value) {
  81. echo "$key => ";
  82. print_r($value);
  83. echo "<br>";
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement