Subproject.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class Subproject {
  3. private $conn;
  4. private $table_name = "subprojects";
  5. public $id;
  6. public $project_id;
  7. public $subproject_name;
  8. public $description;
  9. public $status;
  10. public $start_date;
  11. public $end_date;
  12. public $budget;
  13. public $created_at;
  14. public $updated_at;
  15. public function __construct($db) {
  16. $this->conn = $db;
  17. }
  18. public function create() {
  19. $query = "INSERT INTO " . $this->table_name . " SET project_id=:project_id, subproject_name=:subproject_name, description=:description, status=:status, start_date=:start_date, end_date=:end_date, budget=:budget, created_at=:created_at, updated_at=:updated_at";
  20. $stmt = $this->conn->prepare($query);
  21. $this->project_id = htmlspecialchars(strip_tags($this->project_id));
  22. $this->subproject_name = htmlspecialchars(strip_tags($this->subproject_name));
  23. $this->description = htmlspecialchars(strip_tags($this->description));
  24. $this->status = htmlspecialchars(strip_tags($this->status));
  25. $this->start_date = htmlspecialchars(strip_tags($this->start_date));
  26. $this->end_date = htmlspecialchars(strip_tags($this->end_date));
  27. $this->budget = htmlspecialchars(strip_tags($this->budget));
  28. $this->created_at = date('Y-m-d H:i:s');
  29. $this->updated_at = date('Y-m-d H:i:s');
  30. $stmt->bindParam(":project_id", $this->project_id);
  31. $stmt->bindParam(":subproject_name", $this->subproject_name);
  32. $stmt->bindParam(":description", $this->description);
  33. $stmt->bindParam(":status", $this->status);
  34. $stmt->bindParam(":start_date", $this->start_date);
  35. $stmt->bindParam(":end_date", $this->end_date);
  36. $stmt->bindParam(":budget", $this->budget);
  37. $stmt->bindParam(":created_at", $this->created_at);
  38. $stmt->bindParam(":updated_at", $this->updated_at);
  39. if($stmt->execute()) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. public function read() {
  45. $query = "SELECT * FROM " . $this->table_name . " ORDER BY created_at DESC";
  46. $stmt = $this->conn->prepare($query);
  47. $stmt->execute();
  48. return $stmt;
  49. }
  50. public function readOne() {
  51. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  52. $stmt = $this->conn->prepare($query);
  53. $stmt->bindParam(1, $this->id);
  54. $stmt->execute();
  55. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  56. $this->project_id = $row['project_id'];
  57. $this->subproject_name = $row['subproject_name'];
  58. $this->description = $row['description'];
  59. $this->status = $row['status'];
  60. $this->start_date = $row['start_date'];
  61. $this->end_date = $row['end_date'];
  62. $this->budget = $row['budget'];
  63. $this->created_at = $row['created_at'];
  64. $this->updated_at = $row['updated_at'];
  65. }
  66. public function update() {
  67. $query = "UPDATE " . $this->table_name . " SET project_id=:project_id, subproject_name=:subproject_name, description=:description, status=:status, start_date=:start_date, end_date=:end_date, budget=:budget, updated_at=:updated_at WHERE id=:id";
  68. $stmt = $this->conn->prepare($query);
  69. $this->project_id = htmlspecialchars(strip_tags($this->project_id));
  70. $this->subproject_name = htmlspecialchars(strip_tags($this->subproject_name));
  71. $this->description = htmlspecialchars(strip_tags($this->description));
  72. $this->status = htmlspecialchars(strip_tags($this->status));
  73. $this->start_date = htmlspecialchars(strip_tags($this->start_date));
  74. $this->end_date = htmlspecialchars(strip_tags($this->end_date));
  75. $this->budget = htmlspecialchars(strip_tags($this->budget));
  76. $this->updated_at = date('Y-m-d H:i:s');
  77. $stmt->bindParam(":project_id", $this->project_id);
  78. $stmt->bindParam(":subproject_name", $this->subproject_name);
  79. $stmt->bindParam(":description", $this->description);
  80. $stmt->bindParam(":status", $this->status);
  81. $stmt->bindParam(":start_date", $this->start_date);
  82. $stmt->bindParam(":end_date", $this->end_date);
  83. $stmt->bindParam(":budget", $this->budget);
  84. $stmt->bindParam(":updated_at", $this->updated_at);
  85. $stmt->bindParam(":id", $this->id);
  86. if($stmt->execute()) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. public function delete() {
  92. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  93. $stmt = $this->conn->prepare($query);
  94. $stmt->bindParam(1, $this->id);
  95. if($stmt->execute()) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. public function search($search_term) {
  101. $query = "SELECT * FROM " . $this->table_name . " WHERE
  102. subproject_name LIKE ? OR
  103. description LIKE ?
  104. ORDER BY created_at DESC";
  105. $stmt = $this->conn->prepare($query);
  106. $search_term = "%{$search_term}%";
  107. $stmt->bindParam(1, $search_term);
  108. $stmt->bindParam(2, $search_term);
  109. $stmt->execute();
  110. return $stmt;
  111. }
  112. public function getStatusBadge() {
  113. $badges = [
  114. 'planning' => '<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Planning</span>',
  115. 'in_progress' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">In Progress</span>',
  116. 'completed' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Completed</span>',
  117. 'on_hold' => '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">On Hold</span>',
  118. 'cancelled' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Cancelled</span>'
  119. ];
  120. return $badges[$this->status] ?? $this->status;
  121. }
  122. public function getProgress() {
  123. if ($this->start_date && $this->end_date) {
  124. $start = new DateTime($this->start_date);
  125. $end = new DateTime($this->end_date);
  126. $now = new DateTime();
  127. if ($now < $start) {
  128. return 0; // Not started yet
  129. } elseif ($now > $end) {
  130. return 100; // Completed
  131. } else {
  132. $total = $end->diff($start)->days;
  133. $elapsed = $now->diff($start)->days;
  134. return min(100, round(($elapsed / $total) * 100));
  135. }
  136. }
  137. return 0;
  138. }
  139. }
  140. ?>