Виртуальный музей волейбола
class Exponat {
private $name;
private $date;
private $source;
private $type;
private $filenames;
function __construct($name, $date, $source, $type, $info, $filenames) {
$this->name = $name;
$this->date = $date;
$this->source = $source;
$this->type = $type;
$this->info = $info;
$this->filenames = is_array($filenames) ? $filenames : [];
}
public function getName() { return $this->name; }
public function getDate() { return $this->date; }
public function getSource() { return $this->source; }
public function getType() { return $this->type; }
public function getInfo() { return $this->info; }
public function getFilenames() { return $this->filenames; }
}
class Exponats {
private $json_file;
private $data;
private $folder;
private $collection_name; // добавим новое свойство для хранения имени коллекции
function __construct($folder, $json_file) {
$this->json_file = $folder . '/' . $json_file;
$this->folder = $folder;
if (file_exists($this->json_file)) {
$this->data = json_decode(file_get_contents($this->json_file), true);
$this->collection_name = array_key_first($this->data); // предполагаем, что у нас есть только один ключ - имя коллекции
}
}
public function delete($index) {
if (isset($this->data[$this->collection_name][$index]['filenames'])) {
foreach($this->data[$this->collection_name][$index]['filenames'] as $filename) {
$file_path = $this->folder . "/" . $filename;
if (file_exists($file_path)) {
unlink($file_path);
}
}
}
unset($this->data[$this->collection_name][$index]);
$this->save();
}
public function deleteWithoutFiles($index) {
unset($this->data[$this->collection_name][$index]);
$this->save();
}
public function deleteWithFiles($index) {
if (isset($this->data[$this->collection_name][$index]['filenames'])) {
foreach($this->data[$this->collection_name][$index]['filenames'] as $filename) {
$file_path = $this->folder . "/" . $filename;
if (file_exists($file_path)) {
unlink($file_path);
}
}
}
unset($this->data[$this->collection_name][$index]);
$this->save();
}
public function merge($merge, $withArray) {
foreach ($withArray as $with) {
if(isset($this->data[$this->collection_name][$merge], $this->data[$this->collection_name][$with])) {
$this->data[$this->collection_name][$merge]['filenames'] = array_merge(
$this->data[$this->collection_name][$merge]['filenames'],
$this->data[$this->collection_name][$with]['filenames']
);
$this->deleteWithoutFiles($with);
}
}
$this->save();
}
public function edit($index, $name, $source, $type, $info, $date) {
$this->data[$this->collection_name][$index]['name'] = $name;
$this->data[$this->collection_name][$index]['source'] = $source;
$this->data[$this->collection_name][$index]['type'] = $type;
$this->data[$this->collection_name][$index]['info'] = $info;
$this->data[$this->collection_name][$index]['date'] = $date;
$this->save();
}
public function save() {
file_put_contents($this->json_file, json_encode([$this->collection_name => $this->data[$this->collection_name]], JSON_UNESCAPED_UNICODE));
}
public function getExponats() {
$exponats = [];
if (isset($this->data[$this->collection_name]) && is_array($this->data[$this->collection_name])) {
foreach ($this->data[$this->collection_name] as $key => $exponatData) {
$exponat = new Exponat($exponatData['name'], $exponatData['date'], $exponatData['source'], $exponatData['type'], $exponatData['info'], $exponatData['filenames']);
$exponats[$key] = $exponat;
}
}
return $exponats;
}
// вывод с дэбагом
// public function getExponats() {
// $exponats = [];
// if (isset($this->data[$this->collection_name]) && is_array($this->data[$this->collection_name])) {
// foreach ($this->data[$this->collection_name] as $key => $exponatData) {
// // Проверка, есть ли элемент 'filenames' в $exponatData
// if (isset($exponatData['filenames'])) {
// $exponat = new Exponat($exponatData['name'], $exponatData['date'], $exponatData['source'], $exponatData['type'], $exponatData['info'], $exponatData['filenames']);
// $exponats[$key] = $exponat;
// } else {
// // Если элемент 'filenames' отсутствует, выведите содержимое $exponatData
// echo "Элемент 'filenames' отсутствует для ключа $key\n";
// var_dump($exponatData); // Вывести содержимое $exponatData
// }
// }
// }
// return $exponats;
// }
public function getExponatsAsArray() {
$exponats = [];
if (isset($this->data[$this->collection_name]) && is_array($this->data[$this->collection_name])) {
foreach ($this->data[$this->collection_name] as $key => $exponatData) {
$exponats[$key] = $exponatData;
}
}
return $exponats;
}
public function split($index) {
if (isset($this->data[$this->collection_name][$index])) {
$exponatData = $this->data[$this->collection_name][$index];
if (count($exponatData['filenames']) > 1) {
foreach($exponatData['filenames'] as $filename) {
$newExponat = $exponatData;
$newExponat['filenames'] = [$filename];
$this->data[$this->collection_name][] = $newExponat;
}
unset($this->data[$this->collection_name][$index]);
$this->save();
}
}
}
}
?>