OptionFactory вынес в cms

This commit is contained in:
CORP\phedor 2018-04-26 12:31:43 +03:00
parent c2b9254fd0
commit 8065253db0
2 changed files with 1 additions and 81 deletions

View file

@ -7,7 +7,6 @@ use ctiso\HttpRequest,
ctiso\Path,
ctiso\File,
ctiso\Form\Form,
ctiso\Form\OptionFactory,
ctiso\View\Composite,
ctiso\Database,
ctiso\Database\PDOStatement,
@ -196,10 +195,9 @@ class Component
/**
* Генерация интерфейса для выбора галлереи фотографии
*/
public function setParameters(/*.Composite.*/ $view)
public function setParameters(/*.Composite.*/ $view, $options = null)
{
$form = new Form();
$options = new OptionFactory($this->db, $this->config);
$settings = $this->getInfo();
$form->addFieldList($settings['parameter'], $options);

View file

@ -1,78 +0,0 @@
<?php
namespace ctiso\Form;
use ctiso\Form\Select,
App\Model\Resources,
ctiso\Model\Factory;
class OptionFactory {
public $db;
public $registry;
function __construct($db, $registry = null) {
$this->db = $db;
$this->registry = $registry;
}
function create(Select $field, $input) {
if (isset($input['options.resid'])) {
$type = $input['options.resid'];
$res = new Resources($this->db);
$field->options = $this->optionsArray('id_section', 'title', $res->getSubsections('', $type));
} else if (isset($input['options.res'])) {
$type = $input['options.res'];
$res = new Resources($this->db);
$field->options = $this->optionsArray('path', 'title', $res->getSubsections('', $type));
} else if (isset($input['options.all_res'])) {
$type = $input['options.all_res'];
$res = new Resources($this->db);
$field->options = $this->optionsArray('id_resource', 'subtitle', $res->getAllResource($type));
} else if (isset($input['options.db'])) {
list($table, $keyvalue) = explode(":", $input['options.db']);
list($key, $value) = explode(",", $keyvalue);
try {
$query_result = $this->db->executeQuery("SELECT * FROM $table");
$field->options = $this->optionsDB($key, $value, $query_result);
} catch(\Exception $ex) {
$field->options = [];
}
} elseif (isset($input['options.pair'])) {
$field->options = $this->optionsPair($input['options.pair']);
} elseif (isset($input['options.model'])) {
$factory = new Factory($this->db, $this->registry);
$model = $factory->getModel($input['options.model']);
$field->options = $model->getAllAsOptions();
} else {
$field->options = $input['options'];
}
}
public function optionsDB($key, $val, $res) {
$result = array();
while($res->next()) {
$result[] = array('value' => $res->getInt($key), 'name' => $res->getString($val));
}
return $result;
}
public function optionsArray($key, $val, $res) {
$result = array();
foreach($res as $item) {
$result[] = array('value' => $item->{$key}, 'name' => $item->{$val});
}
return $result;
}
public function optionsPair($list, $selected = false) {
$result = array();
foreach ($list as $key => $value) {
$result [] = array('value' => $key, 'name' => $value, 'selected' => $key == $selected);
}
return $result;
}
}