FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Html.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Html extends Component{
6 
7  public $escape = false;
8  public $break = null;
9  public $backgroundHeightMin = 0; //%
10  public $backgroundHeightMax = 100; //%
11  public $backgroundHeightStep = 10; //%
12  public $backgroundWidthMin = 10; //%
13  public $backgroundWidthMax = 90; //%
14  public $backgroundWidthStep = 5; //%
15 
16  protected function options($content,$value,$assoc){
17  if(is_array($options = $content)){
18  if($assoc === null) $assoc = \Rsi\Record::assoc($options);
19  $content = $this->break;
20  foreach($options as $key => $descr){
21  if(is_array($descr)) $content .= $this->optgroup($this->options($descr,$value,$assoc),['label' => $key]);
22  else{
23  $attribs = [];
24  if($assoc) $attribs['value'] = $key;
25  else $key = $descr;
26  if(is_array($value) ? in_array($key,$value) : !strcmp($key,$value)) $attribs['selected'] = true;
27  $content .= $this->_option($descr,$attribs);
28  }
29  $content .= $this->break;
30  }
31  }
32  return $content;
33  }
34 
35  public function tag($tag,$content = null,$attribs = null){
36  if($content && $this->escape) $content = htmlspecialchars($content);
37  $this->escape = false;
38  $html = '<' . $tag;
39  if($attribs) foreach($attribs as $key => $value) if(!\Rsi::nothing($value)){
40  $html .= ' ' . $key;
41  if($value !== true) $html .= '="' . htmlspecialchars(is_array($value) ? implode($value) : $value) . '"';
42  }
43  return $html . ($content === false ? '>' : ">$content</$tag>");
44  }
45 
46  public function a($content,$attribs = null,$href = null){
47  return $this->tag('a',$content,array_merge(['href' => $href],$attribs ?: []));
48  }
49 
50  public function datalist($content,$attribs = null,$id = null){
51  return $this->tag('datalist',$this->options($content,null,false),array_merge(['id' => $id],$attribs ?: []));
52  }
53 
54  public function div($content,$attribs = null,$class = null,$id = null){
55  return $this->tag('div',$content,array_merge(compact('class','id'),$attribs ?: []));
56  }
57 
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 ?: []));
60  }
61 
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;
72  if($auto_background){
73  unset($attribs['background']);
74  try{
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';
80  for($y = $this->backgroundHeightMin; $y <= $this->backgroundHeightMax; $y += $this->backgroundHeightStep){
81  $red = $green = $blue = $count = 0;
82  for($x = $this->backgroundWidthMin; $x <= $this->backgroundWidthMax; $x += $this->backgroundWidthStep){
83  $rgb = imagecolorsforindex($image,imagecolorat($image,min($x * $width / 100,$width - 1),min($y * $height / 100,$height - 1)));
84  $red += $rgb['red'];
85  $green += $rgb['green'];
86  $blue += $rgb['blue'];
87  $count++;
88  }
89  $background .= ',' . \Rsi\Color::toHex([$red / $count,$green / $count,$blue / $count]) . " $y%";
90  }
91  if(array_key_exists('style',$attribs)) $attribs['style'] .= ';' . $background;
92  else $attribs['style'] = $background;
93  }
94  catch(\Exception $e){
95  $log->info($e);
96  }
97  }
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;
102  }
103  catch(\Exception $e){
104  $log->info($e);
105  if($auto_width) unset($attribs['width']);
106  if($auto_height) unset($attribs['height']);
107  }
108  }
109  return $this->tag('img',false,array_merge(['src' => $content,'alt' => $alt ?: \Rsi\File::basename($content),'title' => $alt],$attribs ?: []));
110  }
111 
112  public function input($content,$attribs = null,$type = null,$name = null,$options = null){
113  if($options){
115  $html = '';
116  foreach($options as $key => $label){
117  if($escape) $label = htmlspecialchars($label);
118  $radio = $type == 'radio';
119  $html .= $this->label(
120  $this->input(
121  \Rsi::nothing($key) ? [''] : $key,
122  array_merge($attribs ?: [],['checked' => is_array($content) ? in_array($key,$content) : !strcmp($key,$content)]),
123  $type,
124  $name . ($radio ? '' : "[$key]")
125  ) .
126  $label
127  );
128  }
129  return $html;
130  }
131  return $this->tag('input',false,array_merge(['value' => $content] + compact('type','name'),$attribs ?: []));
132  }
133 
134  public function meta($content,$attribs = null,$name){
135  return $this->tag('meta',false,array_merge(['name' => $name,'content' => $content],$attribs ?: [])) . $this->break;
136  }
137 
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];
141  $html = '';
142  foreach($srcs as $src) $html .= $this->tag('script',$content,array_merge(compact('src','type'),$attribs ?: [])) . $this->break;
143  return $html;
144  }
145 
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 ?: []));
148  }
149 
150  public function span($content,$attribs = null,$class = null,$id = null){
151  return $this->tag('span',$content,array_merge(compact('class','id'),$attribs ?: []));
152  }
153 
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];
157  $html = '';
158  foreach($srcs as $src) $html .= $this->tag('link',false,array_merge(['rel' => 'stylesheet','type' => $type,'href' => $src],$attribs ?: [])) . $this->break;
159  return $html;
160  }
161 
162  public function textarea($content,$attribs = null,$name = null){
163  return $this->tag('textarea',$content ?: null,array_merge(['name' => $name],$attribs ?: []));
164  }
165 
166  public function th($content,$attribs = null,$scope = 'col'){
167  return $this->tag('th',$content,array_merge(['scope' => $scope],$attribs ?: []));
168  }
169 
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);
174  }
175  else{
176  array_unshift($params,$func_name);
177  $func_name = 'tag';
178  }
179  return call_user_func_array([$this,$func_name],$params);
180  }
181 
182 }
$backgroundWidthMin
Definition: Html.php:12
img($content, $attribs=null, $alt=null)
Definition: Html.php:62
meta($content, $attribs=null, $name)
Definition: Html.php:134
input($content, $attribs=null, $type=null, $name=null, $options=null)
Definition: Html.php:112
options($content, $value, $assoc)
Definition: Html.php:16
style($content, $attribs=null, $srcs=null, $type='text/css')
Definition: Html.php:154
$backgroundWidthMax
Definition: Html.php:13
$backgroundHeightStep
Definition: Html.php:11
div($content, $attribs=null, $class=null, $id=null)
Definition: Html.php:54
$backgroundWidthStep
Definition: Html.php:14
script($content, $attribs=null, $srcs=null, $type='text/javascript')
Definition: Html.php:138
textarea($content, $attribs=null, $name=null)
Definition: Html.php:162
a($content, $attribs=null, $href=null)
Definition: Html.php:46
Basic component class.
Definition: Component.php:8
span($content, $attribs=null, $class=null, $id=null)
Definition: Html.php:150
datalist($content, $attribs=null, $id=null)
Definition: Html.php:50
form($content, $attribs=null, $action=null, $method='post', $enctype='multipart/form-data')
Definition: Html.php:58
__call($func_name, $params)
Definition: Html.php:170
tag($tag, $content=null, $attribs=null)
Definition: Html.php:35
$backgroundHeightMin
Definition: Html.php:9
select($content, $attribs=null, $value=null, $assoc=null, $name=null)
Definition: Html.php:146
$backgroundHeightMax
Definition: Html.php:10
th($content, $attribs=null, $scope='col')
Definition: Html.php:166