16 protected function options($content,$value,$assoc){
17 if(is_array($options = $content)){
18 if($assoc === null) $assoc = \Rsi\Record::assoc($options);
20 foreach($options as $key => $descr){
21 if(is_array($descr)) $content .= $this->optgroup($this->
options($descr,$value,$assoc),[
'label' => $key]);
24 if($assoc) $attribs[
'value'] = $key;
26 if(is_array($value) ? in_array($key,$value) : !strcmp($key,$value)) $attribs[
'selected'] =
true;
27 $content .= $this->_option($descr,$attribs);
35 public function tag($tag,$content = null,$attribs = null){
36 if($content && $this->escape) $content = htmlspecialchars($content);
37 $this->escape =
false;
39 if($attribs)
foreach($attribs as $key => $value)
if(!\Rsi::nothing($value)){
41 if($value !==
true) $html .=
'="' . htmlspecialchars(is_array($value) ? implode($value) : $value) .
'"';
43 return $html . ($content ===
false ?
'>' :
">$content</$tag>");
46 public function a($content,$attribs = null,$href = null){
47 return $this->
tag(
'a',$content,array_merge([
'href' => $href],$attribs ?: []));
50 public function datalist($content,$attribs = null,$id = null){
51 return $this->
tag(
'datalist',$this->
options($content,null,
false),array_merge([
'id' => $id],$attribs ?: []));
54 public function div($content,$attribs = null,$class = null,$id = null){
55 return $this->
tag(
'div',$content,array_merge(compact(
'class',
'id'),$attribs ?: []));
58 public function form($content,$attribs = null,$action = null,$method =
'post',$enctype =
'multipart/form-data'){
59 return $this->
tag(
'form',$content,array_merge(compact(
'action',
'method',
'enctype'),$attribs ?: []));
62 public function img($content,$attribs = null,$alt = null){
63 $auto_width = \Rsi\Record::get($attribs,
'width') ===
true;
64 $auto_height = \Rsi\Record::get($attribs,
'height') ===
true;
65 $auto_background = \Rsi\Record::get($attribs,
'background') ===
true;
66 if($auto_width || $auto_height || $auto_background){
67 $log = $this->_fred->component(
'log');
68 if(strpos($filename = $content,
'//') !==
false) $filename = \Rsi\Http::ensureProtocol($filename);
69 elseif(substr($filename,0,1) ==
'/') $filename = \Rsi\Http::docRoot() . $filename;
70 else $filename = \Rsi\Http::docRoot() . dirname($_SERVER[
'REQUEST_URI'] ?? null) .
'/' . $filename;
71 $width = $height = null;
73 unset($attribs[
'background']);
75 $image = imagecreatefromstring(file_get_contents($filename));
76 $width = imagesx($image);
77 $height = imagesy($image);
78 $log->debug(
"Auto-background generation for '$filename' ($width x $height)",__FILE__,__LINE__);
79 $background =
'background: linear-gradient(to bottom';
81 $red = $green = $blue = $count = 0;
83 $rgb = imagecolorsforindex($image,imagecolorat($image,min($x * $width / 100,$width - 1),min($y * $height / 100,$height - 1)));
85 $green += $rgb[
'green'];
86 $blue += $rgb[
'blue'];
89 $background .=
',' . \Rsi\Color::toHex([$red / $count,$green / $count,$blue / $count]) .
" $y%";
91 if(array_key_exists(
'style',$attribs)) $attribs[
'style'] .=
';' . $background;
92 else $attribs[
'style'] = $background;
98 if($auto_width || $auto_height)
try{
99 if($width === null) list($width,$height) = getimagesize($filename);
100 if($auto_width) $attribs[
'width'] = $width;
101 if($auto_height) $attribs[
'height'] = $height;
105 if($auto_width) unset($attribs[
'width']);
106 if($auto_height) unset($attribs[
'height']);
109 return $this->
tag(
'img',
false,array_merge([
'src' => $content,
'alt' => $alt ?: \
Rsi\File::basename($content),
'title' => $alt],$attribs ?: []));
112 public function input($content,$attribs = null,$type = null,$name = null,$options = null){
116 foreach($options as $key => $label){
117 if(
$escape) $label = htmlspecialchars($label);
118 $radio = $type ==
'radio';
119 $html .= $this->label(
121 \Rsi::nothing($key) ? [
''] : $key,
122 array_merge($attribs ?: [],[
'checked' => is_array($content) ? in_array($key,$content) : !strcmp($key,$content)]),
124 $name . ($radio ?
'' :
"[$key]")
131 return $this->
tag(
'input',
false,array_merge([
'value' => $content] + compact(
'type',
'name'),$attribs ?: []));
134 public function meta($content,$attribs = null,$name){
135 return $this->
tag(
'meta',
false,array_merge([
'name' => $name,
'content' => $content],$attribs ?: [])) .
$this->break;
138 public function script($content,$attribs = null,$srcs = null,$type =
'text/javascript'){
139 if($content) $content =
"\n/* <![CDATA[ */\n$content\n/* ]]> */\n";
140 if(!is_array($srcs)) $srcs = [$srcs];
142 foreach($srcs as $src) $html .= $this->
tag(
'script',$content,array_merge(compact(
'src',
'type'),$attribs ?: [])) .
$this->break;
146 public function select($content,$attribs = null,$value = null,$assoc = null,$name = null){
147 return $this->
tag(
'select',$this->
options($content,$value,$assoc),array_merge([
'name' => $name],$attribs ?: []));
150 public function span($content,$attribs = null,$class = null,$id = null){
151 return $this->
tag(
'span',$content,array_merge(compact(
'class',
'id'),$attribs ?: []));
154 public function style($content,$attribs = null,$srcs = null,$type =
'text/css'){
155 if(!$srcs)
return $this->
tag(
'style',$content,$attribs);
156 if(!is_array($srcs)) $srcs = [$srcs];
158 foreach($srcs as $src) $html .= $this->
tag(
'link',
false,array_merge([
'rel' =>
'stylesheet',
'type' => $type,
'href' => $src],$attribs ?: [])) .
$this->break;
162 public function textarea($content,$attribs = null,$name = null){
163 return $this->
tag(
'textarea',$content ?: null,array_merge([
'name' => $name],$attribs ?: []));
166 public function th($content,$attribs = null,$scope =
'col'){
167 return $this->
tag(
'th',$content,array_merge([
'scope' => $scope],$attribs ?: []));
170 public function __call($func_name,$params){
171 if(substr($func_name,0,1) ==
'_'){
172 $this->escape =
true;
173 $func_name = substr($func_name,1);
176 array_unshift($params,$func_name);
179 return call_user_func_array([$this,$func_name],$params);
img($content, $attribs=null, $alt=null)
meta($content, $attribs=null, $name)
input($content, $attribs=null, $type=null, $name=null, $options=null)
options($content, $value, $assoc)
style($content, $attribs=null, $srcs=null, $type='text/css')
div($content, $attribs=null, $class=null, $id=null)
script($content, $attribs=null, $srcs=null, $type='text/javascript')
textarea($content, $attribs=null, $name=null)
a($content, $attribs=null, $href=null)
span($content, $attribs=null, $class=null, $id=null)
datalist($content, $attribs=null, $id=null)
form($content, $attribs=null, $action=null, $method='post', $enctype='multipart/form-data')
__call($func_name, $params)
tag($tag, $content=null, $attribs=null)
select($content, $attribs=null, $value=null, $assoc=null, $name=null)
th($content, $attribs=null, $scope='col')