config = $config; if ($id > 0) { $this->id = $id; $rows = $this->config->select("select id, name, code, result, quality, language from services where id = ?", $id); if (isset($rows[0]) && $rows[0]->id > 0) { $this->name = $rows[0]->name; $this->code = $rows[0]->code; $this->result = $rows[0]->result; $this->quality = $rows[0]->quality; $this->language = new Language($this->config, $rows[0]->language); } else { error_log("Service not found"); return false; } } return true; } /** * Create a new service if there's no service with that name yet. * Construct object first. * $service->create(); * * param $name The service name * param $language The language ID it's service uses * param $code The code to run * param $result The result regular expression, if match status is UP, else is DOWN * param $quality The quality regular expression, the first match will be the quality * return $id Newly created service's ID */ function create ($name, $language, $code, $result="", $quality="") { if ($this->config->select("select id from services where name = ?", $name)) return false; else { $values = array($name, $code, $result, $quality, $language); $this->config->query("insert into services values (NULL, ?, ?, ?, ?, ?)", $values); $rows = $this->config->select("select last_insert_id() as id"); $this->id = $rows[0]->id; $this->name = $name; $this->language = new Language($this->config, $language); $this->code = $code; $this->result = $result; $this->quality = $quality; return $this->id; } } /** * Update the current service on the database using the current parameters. * $service->name = "new service's name"; * $service->update() */ function update () { $values = array( $this->name, $this->code, $this->result, $this->quality, $this->language, $this->id ); return $this->config->query("update services set name = ?, code = ?, result = ?, quality = ?, language = ? where id = ?", $values); } /** * Delete the current service IF and only IF there's no probe using it. * $service->delete(); */ function delete () { $values = array( $this->id ); if ($this->config->select("select id from probes where service_id = ?", $values)) return null; return $this->config->query("delete from services where id = ?", $values); } /** * Dumper. * $service->dump(); */ function dump () { print "\t\t* Service: $this->name\n"; print "\t\t Code: $this->code\n"; print "\t\t Result: $this->result\n"; print "\t\t Quality: $this->quality\n"; print "\t\t Delay: $this->delay\n"; $this->language->dump(); } } ?>