id); $info = $this->db->fetchAssoc($sql); if (!$info) throw new Exception("Cannot determine usage"); $sql = <<id); $screws = $this->db->fetchAssocList($sql); $html = Template::render("usage", ['info' => $info, 'screws' => $screws]); return ['html' => $html]; } public function ajaxConnect(Array $data) { if (!$this->id) throw new Exception("No componend selected"); if (empty($data['compound']) || empty($data['starttime'])) throw new Exception("Insufficient data transmitted"); $starttime = format_date($data['starttime']); $sql = sprintf("SELECT id,compound,endtime FROM hw_screw WHERE component = %d AND starttime < '%s'::date ORDER BY starttime DESC LIMIT 1", $this->id, $starttime); $row = $this->db->fetchAssoc($sql); if (empty($row['endtime'])) { $sql = sprintf("UPDATE hw_screw SET endtime='%s',sys_edit=now(),sys_user=%s WHERE id = %d", $starttime, $this->db->quote($_SESSION['sys']['login']), $row['id']); $this->db->execute($sql); } $usage = 'NULL'; if (!empty($data['usage'])) $usage = $this->db->quote($data['usage']); $endtime = 'NULL'; if (!empty($data['endtime'])) $endtime = "'" . format_date($data['endtime']) . "'"; $sql = sprintf("INSERT INTO hw_screw (component,compound,usage,starttime,endtime,sys_user,sys_edit) " . "VALUES (%d,%d,%s,'%s',%s,%s,now())", $this->id, $data['compound'], $usage, $starttime, $endtime, $this->db->quote($_SESSION['sys']['login'])); return $this->db->execute($sql); } public function ajaxDisconnect(Array $data) { if (!$this->id) throw new Exception("No componend selected"); if (empty($data['termtime'])) throw new Exception("Insufficient data transmitted"); $termtime = format_date($data['termtime']); $sql = sprintf("SELECT id,endtime FROM hw_screw WHERE component = %d AND starttime < '%s'::date ORDER BY starttime DESC LIMIT 1", $this->id, $termtime); $row = $this->db->fetchAssoc($sql); if (empty($row['endtime'])) { $sql = sprintf("UPDATE hw_screw SET endtime='%s',sys_edit=now(),sys_user=%s WHERE id = %d", $termtime, $this->db->quote($_SESSION['sys']['login']), $row['id']); $this->db->execute($sql); } if (!empty($data['comment'])) { $sql = sprintf("SELECT comment FROM hw_component WHERE id = %d", $this->id); $row = $this->db->fetchAssoc($sql); if (!empty($row['comment'])) $data['comment'] = $row['comment'] . "\n\n" . $data['comment']; $sql = sprintf("UPDATE hw_component SET comment=%s WHERE id = %d", $this->db->quote($data['comment']), $this->id); $this->db->execute($sql); } $sql = sprintf("UPDATE hw_component SET endtime='%s',status=%d,sys_edit=now(),sys_user=%s WHERE id = %d", $termtime, STATUS_DEFUNCT, $this->db->quote($_SESSION['sys']['login']), $this->id); return $this->db->execute($sql); } }