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  public function tag($tag,$content = null,$attribs = null){
17  if($content && $this->escape) $content = htmlspecialchars($content);
18  $html = '<' . $tag;
19  if(is_array($attribs)) foreach($attribs as $key => $value) if(!\Rsi::nothing($value)){
20  $html .= ' ' . $key;
21  if($value !== true) $html .= '="' . htmlspecialchars(is_array($value) ? implode($value) : $value) . '"';
22  }
23  $this->escape = false;
24  return $html . '>' . ($content === false ? '' : (is_array($content) ? implode($this->break,$content) : $content) . "</$tag>");
25  }
26 
27  public function options($content,$value = null,$attribs = null,$assoc = null){
28  if(is_array($options = $content)){
29  if($assoc === null) $assoc = \Rsi\Record::assoc($options);
30  $content = $this->break;
31  foreach($options as $key => $descr) $content .= (is_array($descr)
32  ? $this->optgroup($this->options($descr,$value,$assoc),['label' => $key])
33  : '<option' .
34  ($assoc ? ' value="' . htmlspecialchars($key) . '"' : '') .
35  ($value === null ? '' : (is_array($value) ? in_array($assoc ? $key : $descr,$value) : (strcmp($assoc ? $key : $descr,$value) ? '' : ' selected'))) .
36  '>' . ($this->escape ? htmlspecialchars($descr) : $descr) . '</option>'
37  ) . $this->break;
38  }
39  $this->escape = false;
40  return $content;
41  }
42 
43  public function a($content,$attribs = null,$href = null){
44  return $this->tag('a',$content,array_merge(['href' => $href],$attribs ?: []));
45  }
46 
47  public function datalist($content,$attribs = null,$id = null){
48  return $this->tag('datalist',$this->options($content,null,null,false),array_merge(['id' => $id],$attribs ?: []));
49  }
50 
51  public function div($content,$attribs = null,$class = null,$id = null){
52  return $this->tag('div',$content,array_merge(compact('class','id'),$attribs ?: []));
53  }
54 
55  public function form($content,$attribs = null,$action = null,$method = 'post',$enctype = 'multipart/form-data'){
56  return $this->tag('form',$content,array_merge(compact('action','method','enctype'),$attribs ?: []));
57  }
58 
59  public function img($content,$attribs = null,$alt = null){
60  $auto_width = \Rsi\Record::get($attribs,'width') === true;
61  $auto_height = \Rsi\Record::get($attribs,'height') === true;
62  $auto_background = \Rsi\Record::get($attribs,'background') === true;
63  if($auto_width || $auto_height || $auto_background){
64  $log = $this->_fred->component('log');
65  if(strpos($filename = $content,'//') !== false) $filename = \Rsi\Http::ensureProtocol($filename);
66  elseif(substr($filename,0,1) == '/') $filename = \Rsi\Http::docRoot() . $filename;
67  else $filename = \Rsi\Http::docRoot() . dirname($_SERVER['REQUEST_URI'] ?? null) . '/' . $filename;
68  $width = $height = null;
69  if($auto_background){
70  unset($attribs['background']);
71  try{
72  $image = \Rsi\Image::fromFile($filename);
73  $width = imagesx($image);
74  $height = imagesy($image);
75  $log->debug("Auto-background generation for '$filename' ($width x $height)",__FILE__,__LINE__);
76  $background = 'background: linear-gradient(to bottom';
77  for($y = $this->backgroundHeightMin; $y <= $this->backgroundHeightMax; $y += $this->backgroundHeightStep){
78  $red = $green = $blue = $count = 0;
79  for($x = $this->backgroundWidthMin; $x <= $this->backgroundWidthMax; $x += $this->backgroundWidthStep){
80  $rgb = imagecolorsforindex($image,imagecolorat($image,min($x * $width / 100,$width - 1),min($y * $height / 100,$height - 1)));
81  $red += $rgb['red'];
82  $green += $rgb['green'];
83  $blue += $rgb['blue'];
84  $count++;
85  }
86  $background .= ',' . \Rsi\Color::toHex([$red / $count,$green / $count,$blue / $count]) . " $y%";
87  }
88  $background .= ')';
89  if(array_key_exists('style',$attribs)) $attribs['style'] .= ';' . $background;
90  else $attribs['style'] = $background;
91  }
92  catch(\Exception $e){
93  $log->info($e);
94  }
95  }
96  if($auto_width || $auto_height) try{
97  if($width === null) list($width,$height) = getimagesize($filename);
98  if($auto_width) $attribs['width'] = $width;
99  if($auto_height) $attribs['height'] = $height;
100  }
101  catch(\Exception $e){
102  $log->info($e);
103  if($auto_width) unset($attribs['width']);
104  if($auto_height) unset($attribs['height']);
105  }
106  }
107  return $this->tag('img',false,array_merge(['src' => $content,'alt' => $alt ?: \Rsi\File::basename($content),'title' => $alt],$attribs ?: []));
108  }
109 
110  public function input($content,$attribs = null,$type = null,$name = null,$options = null){
111  if($options){
113  $radio = $type == 'radio';
114  $html = '';
115  foreach($options as $key => $label){
116  if($escape) $label = htmlspecialchars($label);
117  $html .= $this->label(
118  $this->input(
119  \Rsi::nothing($key) ? (is_array($key) ? [''] : '') : $key,
120  array_merge($attribs ?: [],['checked' => is_array($content) ? in_array($key,$content) : is_scalar($content) && !strcmp($key,$content)]),
121  $type,
122  $name . ($radio ? '' : "[$key]")
123  ) .
124  $label
125  );
126  }
127  return $html;
128  }
129  if(is_array($content)){
130  $html = '';
131  foreach($content as $key => $value) $html .= $this->input($value,$attribs,$type,$name ? $name . '[' . $key . ']' : $key) . $this->break;
132  return $html;
133  }
134  return $this->tag('input',false,array_merge(['value' => $content] + compact('type','name'),$attribs ?: []));
135  }
136 
137  public function meta($content,$attribs = null,$name = null){
138  return $this->tag('meta',false,array_merge(['name' => $name,'content' => $content],$attribs ?: [])) . $this->break;
139  }
140 
141  public function script($content,$attribs = null,$srcs = null,$type = 'text/javascript'){
142  if($content) $content = "\n/* <![CDATA[ */\n$content\n/* ]]> */\n";
143  if(!is_array($srcs)) $srcs = [$srcs];
144  $html = '';
145  foreach($srcs as $src) $html .= $this->tag('script',$content,array_merge(compact('src','type'),$attribs ?: [])) . $this->break;
146  return $html;
147  }
148 
149  public function select($content,$attribs = null,$value = null,$assoc = null,$name = null){
150  return $this->tag('select',$this->options($content,$value,null,$assoc),array_merge(['name' => $name],$attribs ?: []));
151  }
152 
153  public function span($content,$attribs = null,$class = null,$id = null){
154  return $this->tag('span',$content,array_merge(compact('class','id'),$attribs ?: []));
155  }
156 
157  public function style($content,$attribs = null,$srcs = null,$type = 'text/css'){
158  if(!$srcs) return $this->tag('style',$content,$attribs);
159  if(!is_array($srcs)) $srcs = [$srcs];
160  $html = '';
161  foreach($srcs as $src) $html .= $this->tag('link',false,array_merge(['rel' => 'stylesheet','type' => $type,'href' => $src],$attribs ?: [])) . $this->break;
162  return $html;
163  }
164 
165  public function table($content,$attribs = null,$columns = null){
166  if(is_array($content)){
167  $header = $footer = $body = '';
168  $headers = $footers = [];
169  if(!$columns) $columns = array_fill_keys(array_keys(\Rsi\Record::value($content)),[]);
170  foreach($columns as $key => $column){
171  $header .= $this->tag('th',$headers[$key] = $column['header'] ?? $key,$column['attribs'] ?? null);
172  $footer .= $this->tag('th',$footers[$key] = $column['footer'] ?? null,$column['attribs'] ?? null);
173  }
174  foreach($content as $record){
175  $row = '';
176  foreach($columns as $key => $column) $row .= $this->tag('td',$record[$key] ?? null,$column['attribs'] ?? null);
177  $body .= $this->tag('tr',$row) . $this->break;
178  }
179  $content =
180  (array_filter($headers) ? $this->tag('thead',$header) . $this->break : '') .
181  (array_filter($footers) ? $this->tag('tfoot',$footer) . $this->break : '') .
182  $this->tag('tbody',$body);
183  }
184  return $this->tag('table',$content,$attribs);
185  }
186 
187  public function textarea($content,$attribs = null,$name = null){
188  return $this->tag('textarea',$content ?: null,array_merge(['name' => $name],$attribs ?: []));
189  }
190 
191  public function th($content,$attribs = null,$scope = 'col'){
192  return $this->tag('th',$content,array_merge(['scope' => $scope],$attribs ?: []));
193  }
194 
195  public function __call($func_name,$params){
196  if(substr($func_name,0,1) == '_'){
197  $this->escape = true;
198  $func_name = substr($func_name,1);
199  }
200  else{
201  array_unshift($params,$func_name);
202  $func_name = 'tag';
203  }
204  return call_user_func_array([$this,$func_name],$params);
205  }
206 
207 }
Rsi\Fred\Html\input
input($content, $attribs=null, $type=null, $name=null, $options=null)
Definition: Html.php:110
Rsi\Fred\Html\$backgroundHeightMin
$backgroundHeightMin
Definition: Html.php:9
Rsi\Fred\Html\table
table($content, $attribs=null, $columns=null)
Definition: Html.php:165
Rsi
Rsi\Fred\Html\style
style($content, $attribs=null, $srcs=null, $type='text/css')
Definition: Html.php:157
Rsi\Fred\Html\tag
tag($tag, $content=null, $attribs=null)
Definition: Html.php:16
Rsi\Fred\Html\$backgroundHeightMax
$backgroundHeightMax
Definition: Html.php:10
Rsi\Fred\Html\form
form($content, $attribs=null, $action=null, $method='post', $enctype='multipart/form-data')
Definition: Html.php:55
Rsi\Fred\Html\script
script($content, $attribs=null, $srcs=null, $type='text/javascript')
Definition: Html.php:141
Rsi\Fred\Html\div
div($content, $attribs=null, $class=null, $id=null)
Definition: Html.php:51
Rsi\Fred\Component
Basic component class.
Definition: Component.php:8
Rsi\Fred\Html\datalist
datalist($content, $attribs=null, $id=null)
Definition: Html.php:47
Rsi\Fred\Html\img
img($content, $attribs=null, $alt=null)
Definition: Html.php:59
Rsi\Fred\Html\$backgroundWidthMin
$backgroundWidthMin
Definition: Html.php:12
Rsi\Fred\Html\__call
__call($func_name, $params)
Definition: Html.php:195
Rsi\Fred\Html\select
select($content, $attribs=null, $value=null, $assoc=null, $name=null)
Definition: Html.php:149
Rsi\Fred\Html\a
a($content, $attribs=null, $href=null)
Definition: Html.php:43
Rsi\Fred\Html\textarea
textarea($content, $attribs=null, $name=null)
Definition: Html.php:187
Rsi\Fred\Html\th
th($content, $attribs=null, $scope='col')
Definition: Html.php:191
Rsi\Fred\Html\$backgroundWidthStep
$backgroundWidthStep
Definition: Html.php:14
Rsi\Fred\Html\$escape
$escape
Definition: Html.php:7
Rsi\Fred\Html\$backgroundHeightStep
$backgroundHeightStep
Definition: Html.php:11
Rsi\Fred\Html\span
span($content, $attribs=null, $class=null, $id=null)
Definition: Html.php:153
Rsi\Fred\Html\options
options($content, $value=null, $attribs=null, $assoc=null)
Definition: Html.php:27
Rsi\Fred\Html
Definition: Html.php:5
Rsi\Fred\Html\$backgroundWidthMax
$backgroundWidthMax
Definition: Html.php:13
Rsi\Fred\Html\meta
meta($content, $attribs=null, $name=null)
Definition: Html.php:137
Rsi\Fred\Exception
Definition: Exception.php:5
Rsi\Fred\Html\$break
$break
Definition: Html.php:8
Rsi\Fred
Definition: Alive.php:3