73 lines
2.6 KiB
PHP
73 lines
2.6 KiB
PHP
<?php
|
|
|
|
class Form_OptionFactory {
|
|
public $db;
|
|
public $registry;
|
|
function __construct($db, $registry = null) {
|
|
$this->db = $db;
|
|
$this->registry = $registry;
|
|
}
|
|
|
|
function create(Form_Select $field, $input) {
|
|
if (isset($input['options.resid'])) {
|
|
$type = $input['options.resid'];
|
|
|
|
$res = new Model_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 Model_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 Model_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 Model_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;
|
|
}
|
|
}
|