Project.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class Project {
  3. private $conn;
  4. private $table_name = "projects";
  5. public $id;
  6. public $customer_id;
  7. public $project_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 customer_id=:customer_id, project_name=:project_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->customer_id = htmlspecialchars(strip_tags($this->customer_id));
  22. $this->project_name = htmlspecialchars(strip_tags($this->project_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(":customer_id", $this->customer_id);
  31. $stmt->bindParam(":project_name", $this->project_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 p.*, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN clients c ON p.customer_id = c.id ORDER BY p.start_date DESC, p.created_at DESC";
  46. $stmt = $this->conn->prepare($query);
  47. $stmt->execute();
  48. return $stmt;
  49. }
  50. public function readOne() {
  51. $query = "SELECT p.*, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN clients c ON p.customer_id = c.id WHERE p.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->customer_id = $row['customer_id'];
  57. $this->project_name = $row['project_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 customer_id=:customer_id, project_name=:project_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->customer_id = htmlspecialchars(strip_tags($this->customer_id));
  70. $this->project_name = htmlspecialchars(strip_tags($this->project_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(":customer_id", $this->customer_id);
  78. $stmt->bindParam(":project_name", $this->project_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 p.*, c.first_name, c.last_name, c.company_name FROM " . $this->table_name . " p LEFT JOIN clients c ON p.customer_id = c.id WHERE
  102. p.project_name LIKE ? OR
  103. p.description LIKE ? OR
  104. c.first_name LIKE ? OR
  105. c.last_name LIKE ? OR
  106. c.company_name LIKE ?
  107. ORDER BY p.start_date DESC, p.created_at DESC";
  108. $stmt = $this->conn->prepare($query);
  109. $search_term = "%{$search_term}%";
  110. $stmt->bindParam(1, $search_term);
  111. $stmt->bindParam(2, $search_term);
  112. $stmt->bindParam(3, $search_term);
  113. $stmt->bindParam(4, $search_term);
  114. $stmt->bindParam(5, $search_term);
  115. $stmt->execute();
  116. return $stmt;
  117. }
  118. public function getSubprojects($project_id) {
  119. $query = "SELECT * FROM subprojects WHERE project_id = ? ORDER BY created_at DESC";
  120. $stmt = $this->conn->prepare($query);
  121. $stmt->bindParam(1, $project_id);
  122. $stmt->execute();
  123. return $stmt;
  124. }
  125. public function getStatusBadge() {
  126. $badges = [
  127. 'planning' => '<span style="background-color: #6c757d; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Planning</span>',
  128. 'in_progress' => '<span style="background-color: #17a2b8; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">In Progress</span>',
  129. 'completed' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Completed</span>',
  130. 'on_hold' => '<span style="background-color: #ffc107; color: black; padding: 2px 6px; border-radius: 4px; font-size: 12px;">On Hold</span>',
  131. 'cancelled' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Cancelled</span>'
  132. ];
  133. return $badges[$this->status] ?? $this->status;
  134. }
  135. public function getProgress() {
  136. if ($this->start_date && $this->end_date) {
  137. $start = new DateTime($this->start_date);
  138. $end = new DateTime($this->end_date);
  139. $now = new DateTime();
  140. if ($now < $start) {
  141. return 0; // Not started yet
  142. } elseif ($now > $end) {
  143. return 100; // Completed
  144. } else {
  145. $total = $end->diff($start)->days;
  146. $elapsed = $now->diff($start)->days;
  147. return min(100, round(($elapsed / $total) * 100));
  148. }
  149. }
  150. return 0;
  151. }
  152. public function getCustomerName() {
  153. $query = "SELECT CONCAT(first_name, ' ', last_name) as customer_name, company_name FROM clients WHERE id = ?";
  154. $stmt = $this->conn->prepare($query);
  155. $stmt->bindParam(1, $this->customer_id);
  156. $stmt->execute();
  157. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  158. if ($row['company_name']) {
  159. return $row['company_name'];
  160. }
  161. return $row['customer_name'];
  162. }
  163. }
  164. ?>