110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
|
|
class Multifield {
|
|
public string $name;
|
|
public array $params;
|
|
public $content = "";
|
|
|
|
public function __construct($params = array())
|
|
{
|
|
$this->params = $params;
|
|
}
|
|
|
|
public function set_content($content)
|
|
{
|
|
if(is_array($content)) {
|
|
$this->content = $content;
|
|
return;
|
|
}
|
|
if(!$content) $content = "[]";
|
|
$this->content = json_decode(trim($content), true);
|
|
}
|
|
|
|
public function set_name($name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
public function render_edit()
|
|
{
|
|
global $b;
|
|
|
|
?><div class="bive-multifield"><?php
|
|
|
|
?><div class="bive-multifield__wrapper" data-level="<?= $this->name; ?>"><?php
|
|
foreach ($this->content as $key => $data)
|
|
{
|
|
?><div class="bive-multifield__row"><?php
|
|
?><div class="input-number"><?= $key; ?></div><?php
|
|
|
|
?><div class="input-row"><?php
|
|
foreach ($this->params["fields"] as $arg => $field)
|
|
{
|
|
?><div class="input-group"><?php
|
|
?><div class="input-label">
|
|
<p class="input-label__inner"><?= $field[1]; ?></p>
|
|
<p class="input-label__background"><?= $arg;?></p>
|
|
</div><?php
|
|
|
|
?><div class="input-edit"><?php
|
|
$b->field_render_edit($field[0], $this->name . "[$key][$arg]", $data[$arg]);
|
|
?></div><?php
|
|
?></div><?php
|
|
}
|
|
?></div><?php
|
|
|
|
?><button type="submit" class="btn-remove" name="<?= $this->name . "[$key]"; ?>" value="remove">X</button>
|
|
</div><?php
|
|
}
|
|
|
|
?>
|
|
|
|
</div>
|
|
|
|
<div class="bive-multifield__buttons">
|
|
<button type="submit" class="btn btn-primary mb-1 ml-1 mt-1" name="<?= $this->name . "[new]"; ?>" value="1">Добавить ещё</button>
|
|
<span class="bive-multifield__hide">Скрыть</span>
|
|
</div>
|
|
|
|
<?php
|
|
|
|
?></div><?php
|
|
}
|
|
|
|
public function render_value()
|
|
{
|
|
global $b;
|
|
$array = array();
|
|
foreach ($this->content as $key => $data) {
|
|
foreach ($this->params["fields"] as $arg => $field) {
|
|
$array[$key][$arg] = $b->field_render_value($field[0], $this->name . "[$key][$arg]", $data[$arg]);
|
|
}
|
|
}
|
|
return $array;
|
|
}
|
|
|
|
public function render_db_value()
|
|
{
|
|
global $b;
|
|
|
|
foreach ($this->content as $key => $data) {
|
|
if ((string) $data == "remove") {
|
|
unset($this->content[$key]);
|
|
continue;
|
|
}
|
|
|
|
if ((string) $key == "new") {
|
|
unset($this->content[$key]);
|
|
$array = array();
|
|
foreach ($this->params["fields"] as $arg => $field)
|
|
$array[$arg] = $b->field_render_db_value($field[0], $this->name . "[$key][$arg]", "");
|
|
$this->content[] = $array;
|
|
}
|
|
}
|
|
foreach ($this->content as $key => $data) {
|
|
foreach ($this->params["fields"] as $arg => $field)
|
|
$this->content[$key][$arg] = $b->field_render_db_value($field[0], $this->name . "[$key][$arg]", $data[$arg]);
|
|
}
|
|
return json_encode(array_values($this->content));
|
|
}
|
|
}
|