config = $config; if ($id > 0) { $this->id = $id; $rows = $this->config->select("select id, name, contact from companies where id = ?", $id); if (isset($rows[0]) && $rows[0]->id > 0) { $this->name = $rows[0]->name; $this->contact = $rows[0]->contact; } else { error_log("Company not found"); return false; } } return true; } /** * Get all probes of this company. * * $probeList = $company->getProbes(); * return $probeList A list of all probes of this company */ function getProbes () { $list = array(); $rows = $this->config->select("select id from probes where company_id = ?", $this->id); if (isset($rows[0]) && $rows[0]->id > 0) { foreach ($rows as $item) array_push($list, new Probe($this->config, $item->id)); } return $list; } /** * Get all hosts of this company. * * $probeList = $company->getHosts(); * return $hostList A list of all hosts of this company */ function getHosts () { $list = array(); $rows = $this->config->select("select id from hosts where company_id = ?", $this->id); if (isset($rows[0]) && $rows[0]->id > 0) { foreach ($rows as $item) array_push($list, new Host($this->config, $item->id)); } return $list; } /** * Create a new company if there's no host with that name yet. * Construct object first. * $host->create("my company", "myserver.mycompany.com", "127.0.0.1"); * * param $company_id The company this host is part * param $name The hostname * param $ip Its IP, if not provided try to check it * return $id Newly created company's ID */ function create ($name, $contact="") { if ($this->config->select("select id from companies where name = ?", $name)) return false; else { $values = array($name, $contact); $this->config->query("insert into companies values (NULL, ?, ?)", $values); $rows = $this->config->select("select last_insert_id() as id"); $this->id = $rows[0]->id; $this->name = $name; $this->contact = $contact; return $this->id; } } /** * Update the current company on the database using the current parameters. * $company->name = "changedname.mycompany.com"; * $company->update() */ function update () { $values = array( $this->name, $this->contact, $this->id ); return $this->config->query("update companies set name = ?, contact = ? where id = ?", $values); } /** * Delete the current company and it's probes and hosts. * $company->delete(); */ function delete () { $values = array( $this->id ); $this->config->query("delete from companies where id = ?", $values); foreach ($this->getProbes() as $probe) $probe->delete(); foreach ($this->getHosts() as $host) $host->delete(); } /** * Clone the current company and all it's probes and hosts. * $company = new Company($config, $id); * $newCompany = $company->clone(); */ function clone () { $newCompany = new Company($this->config); $newCompany->create($company->name.'-clone', $company->contact); foreach ($this->getProbes() as $probe) { $host = new Host($config); $host->create($this->id, $probe->host->name, $probe->host->ip); $probe = new Probe($config); $probe->create($this->id, $probe->service->id, $host->id); } return $newCompany; } /** * Dumper. * $company->dump(); */ function dump () { print "\n################################ Company ".$this->name."'s Dump\n"; print "##\tContact: $this->contact\n"; print "##\tProbes:\n"; foreach ($this->getProbes() as $probe) { $probe->dump(); } print "\n##\tHosts:\n"; foreach ($this->getHosts() as $host) { $host->dump(); } print "\n######### End of company ".$this->name."'s Dump\n"; } } ?>