OptionFactory вынес в cms
This commit is contained in:
parent
c2b9254fd0
commit
8065253db0
2 changed files with 1 additions and 81 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue