示例#1
0
function has_one_tag(moojon_model_collection $models = null, moojon_base_model $model, moojon_base_column $column, moojon_base_relationship $relationship, $attributes = array())
{
    $return = null;
    $name = $column->get_name();
    $attributes = try_set_name_and_id_attributes($attributes, $model, $column);
    if ($value = moojon_request::get_or_null($name)) {
        $attributes['value'] = $value;
        $return = div_tag(array(hidden_input_tag($attributes), redirection_tag(moojon_server::redirection())));
    } else {
        $foreign_key = $relationship->get_foreign_key();
        $key = $relationship->get_key();
        $relationship_name = $relationship->get_class($model);
        $relationship = new $relationship_name();
        $models = $models ? $models : $relationship->read();
        $options = array();
        if ($column->get_null()) {
            $options['Please select...'] = 0;
        }
        foreach ($models as $option) {
            $options[(string) $option] = $option->{$key};
        }
        $selected = $model->{$name} ? $model->{$name} : moojon_uri::get_or_null($foreign_key);
        $return = select_options($options, $selected, $attributes);
    }
    return $return;
}