class Otchet
{
private $name;
private $boss;
private $directors = array();
public function __construct($name)
{
$this->name = $name;
}
public function setBoss($name)
{
$this->boss = $name;
}
public function replaceBoss($name)
{
if(!empty($boss))
{
$this->boss = $boss;
}
}
public function addDirector($name)
{
$this->directors[] = $name;
$this->directors = array_unique($this->directors);
}
public function delDirector($name)
{
if(($key = array_search($name, $this->directors)) !== false)
{
unset($this->directors[$key]);
}
}
private function generate_otchet_name()
{
return date('Y-m-d-H-i').'.txt';
}
public function save($filename = '')
{
if(empty($filename))
{
$filename = $this->generate_otchet_name();
}
$content = "Название отчета: ".$this->name."\n";
$content .= "Директор: ".$this->boss."\n";
if(count($this->directors) > 0)
{
$content .= "Зам директора: ".implode(', ', $this->directors);
}
if(file_put_contents($filename, $content))
{
return 'Отчет сохранен';
}
else
{
return 'Ошибка сохранения';
}
}
}
if(isset($_POST['name'], $_POST['boss'], $_POST['director']))
{
$f = new Otchet($_POST['name']);
$f->setBoss($_POST['boss']);
if(is_array($_POST['director']) && count($_POST['director']) > 0)
{
foreach($_POST['director'] as $v)
{
$f->addDirector($v);
}
}
echo $f->save();
}
<form method="post" action="#">
<input type="text" name="name" placeholder="Введите название отчета" /><br />
<input type="text" name="boss" placeholder="Ген директор" /><br />
<input type="text" name="director[]" placeholder="1-й зам директора" />
<input type="text" name="director[]" placeholder="2-й зам директора" />
<input type="text" name="director[]" placeholder="3-й зам директора" />
<input type="submit" value="Создать" />
</form>
public function replaceBoss($name)
{
if(!empty($boss))
{
$this->boss = $boss;
}
}
public function setBoss($name)
{
$this->boss = $name;
}
if (...)
{
//
}
if (...) {
//
}
if(file_put_contents($filename, $content))
{
return 'Отчет сохранен';
}
else
{
return 'Ошибка сохранения';
}