PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 74ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/webElements.inc

https://bitbucket.org/joaoborsoi/avalanche
PHP | 602 lines | 543 code | 15 blank | 44 comment | 31 complexity | d8a26d1c32a7a039fed1c37419a53e81 MD5 | raw file
  1<?php
  2/******************************************************************************/
  3/********************************** WebElements *******************************/
  4/******************** por JoĂŁo Makray <www.joaomak.net> ******************/
  5/********************************** VersĂŁo 2.5 ********************************/
  6
  7// ATTRIBUTES
  8function parseAttributes($params){
  9    if(is_array($params))return $params;
 10    $params=str_replace('&amp;','<Eamp;>',$params);
 11    $params=str_replace('\=','<Eq;>',$params);
 12    $out=array();
 13    $sep=explode('&',$params);
 14    foreach($sep as $attr){
 15        $attr=str_replace('<Eamp;>','&amp;',$attr);    
 16        $kv=explode('=',$attr);
 17	  if(count($kv)==1 && count($sep)==1 && $kv[0]!='')
 18		$out['id']=$kv[0];
 19        if(count($kv)==2)
 20            $out[$kv[0]]= str_replace('<Eq;>','=',$kv[1]);
 21    }
 22    return $out;
 23}
 24
 25///////////////////////////////// BASIC ////////////////////////////////////////
 26
 27class div{
 28    var $str;
 29    function div($params=''){$params = parseAttributes($params);
 30        $this->str = "\n".'<div ';
 31        foreach($params as $key=>$value)
 32        	$this->str .= $key.'="'.$value.'" ';
 33        $this->str .= '>';
 34    } 
 35    function add($txt){
 36    	  $this->str .= $txt;
 37    }
 38    function get_html(){
 39        return $this->str . "\n".'</div>';      
 40    }
 41}
 42function div($txt, $params=''){$params = parseAttributes($params);
 43    $str="\n<div ";
 44    foreach($params as $key=>$value)
 45        $str .= $key.'="'.$value.'" ';    
 46    $str .= '>'.$txt."\n".'</div>';
 47    return $str;
 48}
 49class span{
 50    var $str;
 51    function span($params=''){$params = parseAttributes($params);
 52        $this->str = '<span ';
 53        foreach($params as $key=>$value)
 54        	$this->str .= $key.'="'.$value.'" ';
 55        $this->str .= '>';
 56    } 
 57    function add($txt){
 58    	  $this->str .= $txt;
 59    }
 60    function get_html(){
 61        return $this->str . '</span>';      
 62    }
 63}
 64function span($txt, $params=''){$params = parseAttributes($params);
 65    $str="<span ";
 66    foreach($params as $key=>$value)
 67        $str .= $key.'="'.$value.'" ';
 68    $str .= '>'.$txt.'</span>';
 69    return $str;
 70}
 71function small($txt, $params=''){$params = parseAttributes($params);
 72    $str="<small ";
 73    foreach($params as $key=>$value)
 74        $str .= $key.'="'.$value.'" ';
 75    $str .= '>'.$txt.'</small>';
 76    return $str;
 77}
 78function big($txt, $params=''){$params = parseAttributes($params);
 79    $str="<big ";
 80    foreach($params as $key=>$value)
 81        $str .= $key.'="'.$value.'" ';
 82    $str .= '>'.$txt.'</big>';
 83    return $str;
 84}
 85function a($txt, $params=''){$params = parseAttributes($params);
 86    $str="<a ";
 87    if(array_key_exists('href',$params)){
 88    		$tmp=str_replace('&amp;','&',$params['href']);
 89    		$params['href']=str_replace('&','&amp;',$tmp);
 90    }		
 91    foreach($params as $key=>$value)
 92        $str .= $key.'="'.$value.'" ';
 93    $str .= '>'.$txt.'</a>';
 94    return $str;
 95}
 96function img($params=''){$params = parseAttributes($params);
 97    $str="<img ";
 98    if(!array_key_exists('alt',$params)) $params['alt']='';
 99    if($params['alt']!='' && !array_key_exists('title',$params)) $params['title']=''; //correct ie 
100    foreach($params as $key=>$value)
101        $str .= $key.'="'.$value.'" ';
102    return $str.'/>';
103} 
104function p($txt, $params=''){$params = parseAttributes($params);
105    $str="\n<p ";
106    foreach($params as $key=>$value)
107        $str .= $key.'="'.$value.'" ';
108    $str .= '>'.$txt.'</p>';
109    return $str;
110}
111function br($params=''){$params = parseAttributes($params);
112    $str="<br ";
113    foreach($params as $key=>$value)
114        $str .= $key.'="'.$value.'" ';
115    return $str.'/>'."\n";
116}                  
117function hr($params=''){$params = parseAttributes($params);
118    $str="\n<hr ";
119    foreach($params as $key=>$value)
120        $str .= $key.'="'.$value.'" ';
121    return $str.'/>'."\n";
122}
123function script($script, $params=''){  $params = parseAttributes($params); 
124	//<![CDATA[
125    if(!array_key_exists('type',$params)) $params['type']='text/javascript';
126    $str= '<script ';  
127    foreach($params as $key=>$value)
128        $str .= $key.'="'.$value.'" ';   
129    return $str.'>'.$script.'</script>'."\n";  //]]
130}
131function del($txt, $params=''){$params = parseAttributes($params);
132    $str="<del ";
133    foreach($params as $key=>$value)
134        $str .= $key.'="'.$value.'" ';
135    $str .= '>'.$txt.'</del>';
136    return $str;
137}
138function blockquote($txt, $params=''){$params = parseAttributes($params);
139    $str="\n<blockquote ";
140    foreach($params as $key=>$value)
141        $str .= $key.'="'.$value.'" ';
142    $str .= '>'.$txt."\n</blockquote>";
143    return $str;
144}
145function cite($txt, $params=''){$params = parseAttributes($params);
146    $str="<cite ";
147    foreach($params as $key=>$value)
148        $str .= $key.'="'.$value.'" ';
149    $str .= '>'.$txt.'</cite>';
150    return $str;
151}
152function b($txt){
153    return '<b>'.$txt.'</b>';
154} 
155function i($txt){
156    return '<i>'.$txt.'</i>';
157}
158function strong($txt, $params=''){$params = parseAttributes($params);
159	$str="<strong ";
160	foreach($params as $key=>$value)
161        $str .= $key.'="'.$value.'" ';
162    	$str .= '>'.$txt.'</strong>';
163    	return $str;
164}
165function em($txt, $params=''){$params = parseAttributes($params);
166	$str="<em ";
167	foreach($params as $key=>$value)
168        $str .= $key.'="'.$value.'" ';
169    	$str .= '>'.$txt.'</em>';
170    	return $str;
171}
172function iframe($params=''){$params = parseAttributes($params);
173    $str="\n<iframe ";
174    foreach($params as $key=>$value)
175        $str .= $key.'="'.$value.'" ';
176    return $str.'></iframe>';
177}                  
178function address($txt, $params=''){$params = parseAttributes($params);
179    $str="\n<address ";
180    foreach($params as $key=>$value)
181        $str .= $key.'="'.$value.'" ';    
182    $str .= '>'.$txt."\n".'</address>';
183    return $str;
184}
185function comment($txt){
186    return "\n<!-- ".$txt." -->\n";
187}
188///////////////////////////////// HEADINGS /////////////////////////////////////
189function h1($txt, $params=''){$params = parseAttributes($params);
190    $str="\n<h1";
191    foreach($params as $key=>$value)
192        $str .= ' '.$key.'="'.$value.'"';
193    $str .= '>'.$txt.'</h1>';
194    return $str;
195}
196function h2($txt, $params=''){$params = parseAttributes($params);
197    $str="\n<h2";
198    foreach($params as $key=>$value)
199        $str .= ' '.$key.'="'.$value.'" ';
200    $str .= '>'.$txt.'</h2>';
201    return $str;
202}
203function h3($txt, $params=''){$params = parseAttributes($params);
204    $str="\n<h3";
205    foreach($params as $key=>$value)
206        $str .= ' '.$key.'="'.$value.'" ';
207    $str .= '>'.$txt.'</h3>';
208    return $str;
209}
210function h4($txt, $params=''){$params = parseAttributes($params);
211    $str="\n<h4";
212    foreach($params as $key=>$value)
213        $str .= ' '.$key.'="'.$value.'" ';
214    $str .= '>'.$txt.'</h4>';
215    return $str;
216}
217function h5($txt, $params=''){$params = parseAttributes($params);
218    $str="\n<h5";
219    foreach($params as $key=>$value)
220        $str .= ' '.$key.'="'.$value.'" ';
221    $str .= '>'.$txt.'</h5>';
222    return $str;
223}
224function h6($txt, $params=''){$params = parseAttributes($params);
225    $str="\n<h6";
226    foreach($params as $key=>$value)
227        $str .= ' '.$key.'="'.$value.'" ';
228    $str .= '>'.$txt.'</h6>';
229    return $str;
230}
231///////////////////////////////// FORMS //////////////////////////////////////// 
232function form($txt, $params=''){$params = parseAttributes($params);
233    $str="\n<form ";
234    if(!array_key_exists('method',$params)) $params['method']='post';
235    if(array_key_exists('action',$params)) $params['action']=str_replace('&','&amp;',$params['action']);
236    foreach($params as $key=>$value)
237        $str .= $key.'="'.$value.'" ';
238    $str .= '>'.$txt."\n".'</form>';
239    return $str;
240}
241class form{
242    var $str;
243    function form($params=''){$params = parseAttributes($params);
244        $this->str = "\n".'<form ';
245        if(!array_key_exists('method',$params)) $params['method']='post';
246        if(array_key_exists('action',$params)) $params['action']=str_replace('&','&amp;',$params['action']);
247        foreach($params as $key=>$value)
248        	$this->str .= $key.'="'.$value.'" ';
249        $this->str .= '>';
250    } 
251    function add($txt){
252    	  $this->str .= $txt;
253    }
254    function get_html(){
255        return $this->str . "\n".'</form>';      
256    }
257}
258class fieldset{
259    var $str;
260    function fieldset($params=''){$params = parseAttributes($params);
261        $this->str = "\n".'<fieldset ';
262        foreach($params as $key=>$value)
263            $this->str .= $key.'="'.$value.'" ';
264        $this->str .= '>';    
265    } 
266    function add_legend($txt, $params=''){$params = parseAttributes($params);
267        $this->str .= "\n".'<legend ';
268        foreach($params as $key=>$value)
269            $this->str .= $key.'="'.$value.'" ';
270        $this->str .= '>'.$txt.'</legend>'."\n";        
271    } 
272    function add($txt){
273    	  $this->str .= $txt;
274    }    
275    function get_html(){
276        return $this->str . "\n".'</fieldset>';      
277    }
278}//end fieldset class
279
280function fieldset($txt, $params=''){$params = parseAttributes($params);
281    $str="<fieldset ";
282    foreach($params as $key=>$value)
283        $str .= $key.'="'.$value.'" ';
284    $str .= '>'.$txt.'</fieldset>';
285    return $str."\n";
286}
287function legend($txt, $params=''){$params = parseAttributes($params);
288    $str="<legend ";
289    foreach($params as $key=>$value)
290        $str .= $key.'="'.$value.'" ';
291    $str .= '>'.$txt.'</legend>';
292    return $str."\n";
293}
294function button($txt, $params=''){$params = parseAttributes($params);
295    $str="<button ";
296    foreach($params as $key=>$value)
297        $str .= $key.'="'.$value.'" ';
298    $str .= '>'.$txt.'</button>';
299    return $str;
300}
301function label($txt, $params=''){$params = parseAttributes($params);
302    $str="<label ";
303    foreach($params as $key=>$value)
304        $str .= $key.'="'.$value.'" ';
305    $str .= '>'.$txt.'</label>';
306    return $str;
307}
308function input($params=''){$params = parseAttributes($params);
309    $str="<input ";
310    if(!array_key_exists('type',$params)) $params['type']='text';
311    foreach($params as $key=>$value)
312        $str .= $key.'="'.$value.'" ';
313    return $str.'/>';
314}
315function textarea($txt, $params=''){$params = parseAttributes($params);
316    $str="<textarea ";
317    foreach($params as $key=>$value)
318        $str .= $key.'="'.$value.'" ';
319    $str .= '>'.$txt.'</textarea>';
320    return $str."\n";
321}   
322class select{
323    var $str ;
324    var $empty;
325    /*
326    Select
327    */
328    function select($params=''){$params = parseAttributes($params);
329        $this->str = "\n<select "; 
330        foreach($params as $key=>$value)
331        	$this->str .= $key.'="'.$value.'" ';
332        $this->str .= '>'."\n";
333        $this->empty = true;
334    }
335    function add_option($txt, $params=''){$params = parseAttributes($params);
336   	  $this->str.="<option ";
337    	  foreach($params as $key=>$value)
338        	$this->str .= $key.'="'.$value.'" ';
339	  $this->str .= '>'.$txt.'</option>'."\n";
340	  $this->empty = false;
341    }    
342    function get_html(){    
343        if(!$this->empty) return $this->str .= '</select>'."\n";      
344        else return '<!-- select empty -->'."\n";
345    }
346}
347///////////////////////////////// LISTS ////////////////////////////////////////   
348 
349class ul{
350    var $str ;
351    var $empty;
352    /*
353    Unordered List
354    */
355    function ul($params=''){$params = parseAttributes($params);
356        $this->str = "\n<ul "; 
357        foreach($params as $key=>$value)
358        	$this->str .= $key.'="'.$value.'" ';
359        $this->str .= '><!--'."\n";
360        $this->empty = true;
361    }
362    function add_li($txt, $params=''){
363        $this->str .= li($txt, $params);
364        $this->empty = false;
365    }    
366    function get_html(){    
367        if(!$this->empty) return $this->str .= '--></ul>'."\n\n";      
368        else return '<!-- ul empty -->'."\n";
369    }
370}
371function get_ul($items, $params=''){
372    $ul=new ul($params);
373    foreach($items as $value){
374        $ul->add_li($value);
375    }
376    return $ul->get_html();          
377}
378
379// LI
380function li($txt, $params=''){$params = parseAttributes($params);
381    $str = '--><li';
382    foreach($params as $key=>$value)
383        	$str .= ' '.$key.'="'.$value.'" ';
384    $str .= '>';
385    $str .= $txt;               
386    return $str ."</li><!--\n"; 
387} 
388class ol{
389    var $str ;
390    var $empty;
391    /*
392    Unordered List
393    */
394    function ol($params=''){$params = parseAttributes($params);
395        $this->str = "\n<ol ";
396        foreach($params as $key=>$value)
397        	$this->str .= $key.'="'.$value.'" ';
398	  $this->str .= '><!--'."\n";
399        $this->empty = true;
400    }
401    function add_li($txt, $params=''){
402        $this->str .= li($txt, $params);
403        $this->empty = false;
404    }    
405    function get_html(){ 
406    	  if(!$this->empty) return $this->str .= '--></ol>'."\n\n";  
407    	  else return '<!-- ol empty -->'."\n";    
408    }
409}
410function get_ol($items, $params=''){
411    $ol=new ol($id,$params);
412    foreach($items as $value){
413        $ol->add_li($value);
414    }
415    return $ol->get_html();          
416}
417
418class dl{
419    var $str ;
420    var $empty;
421    /*
422    Definition List
423    */
424    function dl($params=''){$params = parseAttributes($params);
425        $this->str = "\n<dl ";
426        foreach($params as $key=>$value)
427        	$this->str .= $key.'="'.$value.'" ';
428        $this->str .= '>'."\n";
429        $this->empty = true;
430    }
431    
432    function add_dt($txt, $params=''){$params = parseAttributes($params);
433        // dt
434        $this->str .= "<dt ";
435        foreach($params as $key=>$value)
436        	$this->str .= $key.'="'.$value.'" ';
437        $this->str .= '>'.$txt.'</dt>'."\n";
438        $this->empty = false;
439    }        
440    function add_dd($txt, $params=''){$params = parseAttributes($params);
441        // dd
442        $this->str .= "<dd ";
443        foreach($params as $key=>$value)
444        	$this->str .= $key.'="'.$value.'" ';
445        $this->str .= '>'.$txt.'</dd>'."\n";
446        $this->empty = false;
447    }
448    function get_html(){    
449    	  if(!$this->empty) return $this->str .= '</dl>'."\n\n";  
450        else return $this->str .= '<!-- dl empty -->'."\n";      
451    }           
452}
453function get_dl ($items, $params=''){
454    /*
455    Definition List
456    - entra array tipo 'titulo' => 'descricao',
457    */ 
458    $dl=new dl($params='');         
459    foreach($items as $key => $value){
460        $dl->add_dt($key);
461        $dl->add_dd($value);
462    }
463    return $dl->get_html();
464}
465///////////////////////////////// TABLE ////////////////////////////////////////   
466class table{
467    var $str ;
468    var $is_first_tr = true;
469    /*
470    Table
471    */
472    function table($params=''){$params = parseAttributes($params);
473        $this->str = "\n<table ";
474        foreach($params as $key=>$value)
475        	$this->str .= $key.'="'.$value.'" ';
476        $this->str .= '>'."\n";
477    }
478    function add_caption($txt, $params=''){$params = parseAttributes($params);
479        // caption
480        $this->str .= "<caption ";
481        foreach($params as $key=>$value)
482        	$this->str .= $key.'="'.$value.'" ';
483        $this->str .= '>'.$txt.'</caption>';
484    }
485    function new_tr($params=''){$params = parseAttributes($params);
486        // tr 
487        if(!$this->is_first_tr)$this->str .= "</tr>\n";
488        $this->str .= "<tr ";
489        foreach($params as $key=>$value)
490        	$this->str .= $key.'="'.$value.'" ';
491        $this->str .= '>'."\n";
492        $this->is_first_tr = false;
493    }
494    function add_th($txt, $params=''){$params = parseAttributes($params);
495        // td
496        $this->str .= "<th ";
497        foreach($params as $key=>$value)
498        	$this->str .= $key.'="'.$value.'" ';
499        $this->str .= '>'.$txt.'</th>';
500    }        
501    function add_td($txt, $params=''){$params = parseAttributes($params);
502        // td
503        $this->str .= "<td ";
504     	  foreach($params as $key=>$value)
505        	$this->str .= $key.'="'.$value.'" ';
506        $this->str .= '>'.$txt.'</td>';
507    }
508    function get_html(){    
509        if(!$this->is_first_tr)$this->str .= "</tr>\n";
510        return $this->str .= "</table>\n\n";      
511    }           
512}
513function get_table($items, $params=''){
514    $table = new table($params);
515    foreach($items as $row){
516        $table->new_tr();
517        foreach($row as $cell){
518            $table->add_td($cell);
519        }
520    }
521    return $table->get_html();
522}
523///////////////////////////////// Compostos //////////////////////////////
524
525function flash($src, $params=''){$params = parseAttributes($params);
526	$alt='<small><a href="http://adobe.com/go/getflash">get Flash</a></small>';
527	$flash=array('src'=>$src,'id'=>'we_flash','alt'=>$alt,'width'=>'100%','height'=>'100%','version'=>6,'bgcolor'=>'#ffffff');
528	foreach($flash as $attr=>$value){
529		if(array_key_exists($attr,$params)) $flash[$attr]=$params[$attr];
530	}
531	//$w='100%', $h='100%', $bgcolor='#ffffff', $version = 6){ 
532	$str = '<div id="'.$flash['id'].'" class="flash">'.$flash['alt'].'</div>';
533	$str .= script('var we_flash=new SWFObject("'.$flash['src'].'", "'.$flash['id'].'", "'.$flash['width'].
534	'", "'.$flash['height'].'", '.$flash['version'].', "'.$flash['bgcolor'].'");we_flash.addParam("wmode","transparent");we_flash.write("'.$flash['id'].'");');
535	return $str;  
536}
537function swf($txt='', $params=''){$params = parseAttributes($params);
538    $str="<object ";
539    if(!array_key_exists('type',$params)) $params['type']='application/x-shockwave-flash';
540    if(!array_key_exists('data',$params)) $params['data']='';
541    // data, width, height
542    foreach($params as $key=>$value)
543        $str .= $key.'="'.$value.'" ';
544    $str .= '><param name="movie" value="'.$params['data'].'" />';
545    $str .= $txt.'</object>';
546    return $str;
547}
548
549
550///////////////////////////////// RSS ///////////////////////////////////
551
552class item{
553    var $str ;
554    var $empty;
555    /*
556    RSS Item
557    */
558    function item($params=''){$params = parseAttributes($params);
559        $this->str = "\n<item ";
560        foreach($params as $key=>$value)
561        	$this->str .= $key.'="'.$value.'" ';
562        $this->str .= '>'."\n";
563        $this->empty = true;
564    }
565    
566    function add_title($txt, $params=''){$params = parseAttributes($params);
567        $this->str .= "<title ";
568        foreach($params as $key=>$value)
569        	$this->str .= $key.'="'.$value.'" ';
570        $this->str .= '>'.$txt.'</title>'."\n";
571        $this->empty = false;
572    }        
573    function add_link($txt, $params=''){$params = parseAttributes($params);
574        $this->str .= "<link ";
575        foreach($params as $key=>$value)
576        	$this->str .= $key.'="'.$value.'" ';
577        $this->str .= '>'.$txt.'</link>'."\n";
578        $this->empty = false;
579    }
580    function add_description($txt, $params=''){$params = parseAttributes($params);
581        $this->str .= "<description ";
582        foreach($params as $key=>$value)
583        	$this->str .= $key.'="'.$value.'" ';
584        $this->str .= '><![CDATA['.$txt.']]></description>'."\n";
585        $this->empty = false;
586    }
587    function add_pubDate($txt, $params=''){$params = parseAttributes($params);
588        $this->str .= "<pubDate ";
589        foreach($params as $key=>$value)
590        	$this->str .= $key.'="'.$value.'" ';
591        	
592        if(is_numeric($txt))$txt=gmdate("D, d M Y H:i:s T", $txt);	
593        $this->str .= '>'.$txt.'</pubDate>'."\n";
594        $this->empty = false;
595    }
596    function get_html(){    
597    	  if(!$this->empty) return $this->str .= '</item>'."\n\n";  
598        else return $this->str .= '<!-- item empty -->'."\n";      
599    }           
600}
601
602?>