00001 <?php
00002
00003
00004
00005
00006
00007
00008 class InputWidgetBaseBase implements IWidget {
00009 protected $name;
00010 protected $label;
00011 protected $value;
00012 protected $params;
00013
00014
00015
00016
00017 public function __construct($name, $label, $value, $params) {
00018 $this->name = $name;
00019 $this->label = $label;
00020 $this->value = $value;
00021 $this->params = Arr::force($params, false);
00022 }
00023
00024
00025
00026
00027
00028
00029 protected function get_input_type() {
00030 $cls = get_class($this);
00031 $cls = str_replace('InputWidget', '', $cls);
00032 $cls = strtolower($cls);
00033 return $cls;
00034 }
00035
00036
00037
00038
00039 public function render($policy = self::NONE) {
00040 $ret = '';
00041 $item = Arr::get_item($this->params, 'item', false);
00042 $name = array_pop(Arr::extract_array_keys($this->name));
00043 $can_edit = ($item) ? AccessControl::is_allowed('edit', $item, $name) : true;
00044 if ($can_edit) {
00045 $ret = $this->input_build_widget($this->params, $this->name, $this->label, $this->value, $policy);
00046 }
00047 else {
00048 $ret = $this->input_build_noedit($this->params, $this->name, $this->label, $this->value, $policy);
00049 }
00050 return $ret;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 protected function input_build_widget($params, $name, $title, $value, $policy) {
00062 $ret = '';
00063
00064 $html_attrs = $this->create_default_attributes($params, $name, $policy);
00065 $this->extend_attributes($html_attrs, $params, $name, $title, $value, $policy);
00066
00067 $widget = $this->render_input($html_attrs, $params, $name, $title, $value, $policy);
00068 $widget = $this->render_label($widget, $html_attrs, $params, $name, $title, $value, $policy);
00069 $ret = $this->render_postprocess($widget, $policy);
00070
00071 return $ret;
00072 }
00073
00074
00075
00076
00077 protected function create_default_attributes($params, $name, $policy) {
00078 $id = strtr(arr::get_item($params, 'id', $name), '[]', '__');
00079 $attrs = array(
00080 'id' => $id
00081 );
00082
00083 $attrs = array_merge($attrs, $params);
00084
00085 unset($attrs['label:class']);
00086 unset($attrs['notes']);
00087 unset($attrs['item']);
00088 return $attrs;
00089 }
00090
00091
00092
00093
00094 protected function extend_attributes(&$attrs, $params, $name, $title, $value, $policy) {
00095 }
00096
00097
00098
00099
00100 protected function render_input($attrs, $params, $name, $title, $value, $policy) {
00101 }
00102
00103
00104
00105
00106 protected function render_label($widget, $html_attrs, $params, $name, $title, $value, $policy) {
00107 $ret = '';
00108 $id = Arr::get_item($html_attrs, 'id', '');
00109 $lbl_class = Arr::get_item($params, 'label:class', '');
00110 $label = '';
00111 if ($title) {
00112 $notes = Cast::int(arr::get_item($params, 'notes', 0));
00113 if ($notes > 0) {
00114 $title .= ' '. html::span(str_repeat('*', $notes), 'notes');
00115 }
00116 if (Common::flag_is_set($policy, WidgetInput::NO_LABEL)) {
00117
00118 $ret = html::div($title, 'label ' . $lbl_class) . $widget;
00119 }
00120 else if (Common::flag_is_set($policy, WidgetInput::WRAP_LABEL)) {
00121
00122 $ret = html::label($widget. ' ' . $title, $id, 'spanning ' . $lbl_class);
00123 }
00124 else {
00125
00126 $ret = html::label($title, $id, 'outside ' . $lbl_class) . $widget;
00127 }
00128 }
00129 else {
00130 $ret = $widget;
00131 }
00132 return $ret;
00133 }
00134
00135
00136
00137
00138 protected function render_postprocess($output, $policy) {
00139 $ret = $output;
00140 if (!Common::flag_is_set($policy, WidgetInput::NO_BREAK)) {
00141 $ret .= html::br();
00142 }
00143 return $ret;
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153 protected function input_build_noedit($params, $name, $title, $value, $policy) {
00154 $ret = '';
00155 $ret .= html::span($title, 'label') . ' ';
00156 $ret .= html::span($value, 'value');
00157 $ret = html::p($ret, 'noedit');
00158 return $ret;
00159 }
00160 }