KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
tags.php
Ir a la documentación de este archivo.
1 <?php
32 function xhtml_start_tag($tag, $attrs=null) {
33  $params = is_array($tag) ? $tag : Util::getParams(func_get_args());
34  $xw = new xmlWriter();
35  $xw->openMemory();
36 
37  if(APP_CHARSET != 'UTF-8'){
38  $params = utf8_encode($params);
39  }
40  if(isset($params[1]) && is_array($params[1])) {
41  $attrs = $params[1];
42  unset($params[1]);
43  $attrs = array_merge($attrs, $params);
44  } else {
45  $attrs = $params;
46  }
47 
48  $xw->startElement($tag);
49 
50  foreach ($attrs as $k => $v) {
51  if (! is_numeric($k)) {
52  $xw->writeAttribute($k, $v);
53  }
54  }
55  //$xw->endElement();
56  return $xw->outputMemory(true) . '>';
57 }
58 
65 function xhtml_end_tag($tag) {
66  $params = is_array($tag) ? $tag : Util::getParams(func_get_args());
67  $tag = $params[0];
68  return "</$tag>";
69 }
70 
86 function xhtml_tag($tag, $attrs=null) {
87  $params = is_array($tag) ? $tag : Util::getParams(func_get_args());
88  $xw = new xmlWriter();
89  $xw->openMemory();
90 
91  if(APP_CHARSET != 'UTF-8'){
92  $params = utf8_encode($params);
93  }
94 
98  $short_close = array('input', 'link', 'img');
102  $need_cdata = array('script', 'style','a');
103 
104  $tag = $params[0];
105  unset($params[0]);
106 
110  if(isset($params['content'])) {
111  $content = $params['content'];
112  unset($params['content']);
113  } else {
114  $content = '';
115  }
116 
117  if(isset($params[1]) && is_array($params[1])) {
118  $attrs = $params[1];
119  unset($params[1]);
120  $attrs = array_merge($attrs, $params);
121  } else {
122  $attrs = $params;
123  }
124  $xw->startElement($tag);
125  foreach($attrs as $k=>$v) {
126  if (! is_numeric($k)) {
127  $xw->writeAttribute($k,$v);
128  }
129  }
130  if($content || !in_array($tag, $short_close)) {
131  if($tag == 'select' || in_array($tag, $need_cdata)) {
132  $xw->writeRaw($content);
133  } else {
134  $xw->text($content);
135  }
136  }
137  $xw->endElement();
138  return $xw->outputMemory(true);
139 }
140 
152 function link_to($action, $text=''){
153  $params = is_array($action) ? $action : Util::getParams(func_get_args());
154 
155  if(isset($params['confirm'])&&$params['confirm']){
156  if(isset($params['onclick'])) {
157  $params['onclick'] = "if(!confirm(\"{$params['confirm']}\")) { return false; }; ".$params['onclick'];
158  } else {
159  $params['onclick'] = "if(!confirm(\"{$params['confirm']}\")) { return false; };";
160  }
161  unset($params['confirm']);
162  }
163 
164  if(isset($params['text'])) {
165  $params[1] = $params['text'];
166  unset($params['text']);
167  }
168 
169  if(!isset($params[1])) {
170  $text = strtr($params[0], '_/', ' ');
171  $params[1] = ucwords($text);
172  }
173 
174  $params['href'] = get_kumbia_url($params[0]);
175 
176  return xhtml_tag('a', $params, "content: {$params[1]}");
177 }
178 
189 function link_to_action($action, $text=''){
190  $params = is_array($action) ? $action : Util::getParams(func_get_args());
191 
192  if(isset($params['confirm'])&&$params['confirm']){
193  if(isset($params['onclick'])) {
194  $params['onclick'] = "if(!confirm(\"{$params['confirm']}\")) { return false; }; ".$params['onclick'];
195  } else {
196  $params['onclick'] = "if(!confirm(\"{$params['confirm']}\")) { return false; };";
197  }
198  unset($params['confirm']);
199  }
200 
201  if(isset($params['text'])) {
202  $params[1] = $params['text'];
203  unset($params['text']);
204  }
205 
206  if(!isset($params[1])) {
207  $text = strtr($params[0], '_/', ' ');
208  $params[1] = ucwords($text);
209  }
210 
211  $module_name = Router::get('module');
212  $controller_name = Router::get('controller');
213  if($module_name) {
214  $path = "$module_name/$controller_name";
215  } else {
216  $path = $controller_name;
217  }
218  $params['href'] = get_kumbia_url("$path/{$params[0]}");
219 
220  return xhtml_tag('a', $params, "content: {$params[1]}");
221 }
222 
237 function link_to_remote($action){
238  $params = is_array($action) ? $action : Util::getParams(func_get_args());
239 
240  if(!isset($params['update'])||!$params['update']){
241  $update = isset($params[2]) ? $params[2] : "";
242  } else {
243  $update = $params['update'];
244  }
245  if(!isset($params['text'])||!$params['text']){
246  $text = isset($params[1]) ? $params[1] : "";
247  } else {
248  $text = $params['text'];
249  }
250  if(!$text){
251  $text = $params[0];
252  }
253  if(!isset($params['action'])||!$params['action']){
254  $action = $params[0];
255  } else {
256  $action = $params['action'];
257  }
258 
259  $code = '';
260  if(isset($params['confirm'])){
261  $code.= "if(confirm('{$params['confirm']}')) {";
262  }
263  $action = PUBLIC_PATH . $action;
264  $code.= "new AJAX.viewRequest({action: '$action', container: '$update'";
265 
266  $call = array();
267  if(isset($params['before'])){
268  $call["before"] = "before: function(){ {$params['before']} }";
269  }
270  if(isset($params['oncomplete'])){
271  $call["oncomplete"] = "oncomplete: function(){ {$params['oncomplete']} }";
272  }
273  if(isset($params['success'])){
274  $call["success"] = "success: function(){ {$params['success']} }";
275  }
276  if(count($call)){
277  $code.=", callbacks: { ";
278  $code.=join(",", $call);
279  $code.="}";
280  }
281  $code.="})";
282  if(isset($params['confirm'])){
283  $code.=" }";
284  }
285  $code.="; return false;";
286 
287  $params['onclick'] = $code;
288  $params['href'] = '#';
289 
290  unset($params['action']);
291  unset($params['before']);
292  unset($params['oncomplete']);
293  unset($params['success']);
294  unset($params['loading']);
295  unset($params['update']);
296  unset($params['confirm']);
297 
298  return xhtml_tag('a',$params, "content: $text");
299 }
300 
311 function javascript_include_tag($src=''){
312  $params = is_array($src) ? $src : Util::getParams(func_get_args());
313 
314  if(isset($params['cache']) && $params['cache']=='false') {
315  $cache = false;
316  unset($params['cache']);
317  } else {
318  $cache = true;
319  }
320 
321  if(!isset($params[0])) {
322  $params[0] = Router::get('controller');
323  }
324 
325  $code = '';
326  foreach($params as $src) {
327  $src.=".js";
328  if(!$cache) {
329  $src.="?nocache=".md5(uniqid());
330  }
331  $src = PUBLIC_PATH."javascript/$src";
332  $code.=xhtml_tag('script', $params, 'type: text/javascript', "src: $src");
333  }
334 
335  return $code;
336 }
337 
344 function javascript_library_tag($src){
345  $params = is_array($src) ? $src : Util::getParams(func_get_args());
346  $code = '';
347  foreach($params as $src) {
348  $src = PUBLIC_PATH."javascript/kumbia/$src.js";
349  $code.=xhtml_tag('script', 'type: text/javascript', "src: $src");
350  }
351  return $code;
352 }
353 
364 function stylesheet_link_tag($name){
365  $params = is_array($name) ? $name : Util::getParams(func_get_args());
366  $params['rel'] = 'stylesheet';
367  $params['type'] = 'text/css';
368 
369  $code = '';
370  for($i=0; isset($params[$i]); $i++){
371  $src = $params[$i];
372  $params['href'] = PUBLIC_PATH."css/$src.css";
373  $code.=xhtml_tag('link',$params);
374  }
375 
376  if(!$i){ //$i=0 si no se especificaron hojas de estilo
377  $src = $_REQUEST['action'];
378  $params['href'] = PUBLIC_PATH."css/$src.css";
379  $code.=xhtml_tag('link',$params);
380  }
381  TagsData::$data['KUMBIA_CSS_IMPORTS'][]=$code;
382  return;
383 }
384 
396 function img_tag($img){
397  $params = is_array($img) ? $img : Util::getParams(func_get_args());
398 
399  if(!isset($params['src']) && isset($params[0])){
400  $params['src'] = PUBLIC_PATH."img/{$params[0]}";
401  }
402  if(!isset($params['alt'])) {
403  $params['alt'] = '';
404  }
405 
406  if(isset($params['drag'])&&$params['drag']) {
407  $drag = true;
408  unset($params['drag']);
409  } else {
410  $drag = false;
411  }
412  if(isset($params['reflect'])&&$params['reflect']) {
413  $reflect = true;
414  unset($params['reflect']);
415  } else{
416  $reflect = false;
417  }
418 
419  $code = xhtml_tag('img', $params);
420  if($drag){
421  $code.=xhtml_tag('script', 'type: text/javascript', "content: new Draggable('{$atts['id']}', {revert:true})");
422  }
423  if($reflect){
424  $code.=xhtml_tag('script', 'type: text/javascript', "content: new Reflector.reflect('{$atts['id']}')");
425  }
426 
427  return $code;
428 }
429 
442 function form_remote_tag($data){
443  $params = is_array($data) ? $data : Util::getParams(func_get_args());
444 
445  if(!isset($params['action'])||!$params['action']) {
446  $params['action'] = $params[0];
447  }else{
448  $params['action'] = $params['action'];
449  }
450 
451  if(!isset($params['method'])||!$params['method']) {
452  $params['method'] = 'post';
453  }
454 
455  if(isset($params['update'])) {
456  $update = $params['update'];
457  unset($params['update']);
458  } else {
459  $update = '';
460  }
461 
462  $callbacks = array();
463  $id = Router::get('id');
464  if(isset($params['complete'])&&$params['complete']){
465  $callbacks[] = " complete: function(){ ".$params['complete']." }";
466  unset($params['complete']);
467  }
468  if(isset($params['before'])&&$params['before']){
469  $callbacks[] = " before: function(){ ".$params['before']." }";
470  unset($params['before']);
471  }
472  if(isset($params['success'])&&$params['success']){
473  $callbacks[] = " success: function(){ ".$params['success']." }";
474  unset($params['sucess']);
475  }
476  if(isset($params['required'])&&$params['required']){
477  $requiredFields = Util::encomillar($params['required']);
478  $params['onsubmit'] = "if(validaForm(this,new Array({$requiredFields}))){ return ajaxRemoteForm(this,\"{$update}\",{".join(",",$callbacks)."}); } else{ return false; }";
479  unset($params['required']);
480  } else{
481  $params['onsubmit'] = "return ajaxRemoteForm(this, \"{$update}\", { ".join(",", $callbacks)." });";
482  }
483  $params['action'] = get_kumbia_url("{$params['action']}/$id");
484 
485  return xhtml_start_tag('form', $params);
486 }
487 
488 
498 function form_tag($action){
499  $params = is_array($action) ? $action : Util::getParams(func_get_args());
500  if(!isset($params['action']) && isset($params[0])) {
501  $params['action'] = $params[0];
502  }
503  if(isset($params['action'])) {
504  $params['action'] = get_kumbia_url("{$params['action']}");
505  }
506  if(!isset($params['method'])||!$params['method']) {
507  $params['method'] = "post";
508  }
509  if(isset($params['confirm'])&&$params['confirm']){
510  if(isset($params['onsubmit'])) {
511  $params['onsubmit'].=";if(!confirm(\"{$params['confirm']}\")) { return false; }";
512  } else {
513  $params['onsubmit'] = "if(!confirm(\"{$params['confirm']}\")) { return false; }";
514  }
515  unset($params['confirm']);
516  }
517  return xhtml_start_tag('form', $params);
518 }
519 
520 
521 
527 function end_form_tag(){
528  $str = "</form>\r\n";
529  return $str;
530 }
531 
538 function submit_tag($caption){
539  $params = is_array($caption) ? $caption : Util::getParams(func_get_args());
540  if(isset($params['caption'])) {
541  $params['value'] = $params['caption'];
542  unset($params['caption']);
543  } elseif(isset($params[0])) {
544  $params['value'] = $params[0];
545  }
546  return xhtml_tag('input', $params, 'type: submit');
547 }
548 
562 function submit_remote_tag($caption){
563  $params = is_array($caption) ? $caption : Util::getParams(func_get_args());
564 
565  if(isset($params['caption'])) {
566  $params['value'] = $params['caption'];
567  unset($params['caption']);
568  } elseif(isset($params[0])) {
569  $params['value'] = $params[0];
570  }
571 
572  if(isset($params['update'])) {
573  $update = $params['update'];
574  unset($params['update']);
575  } else {
576  $update = '';
577  }
578 
579  $callbacks = array();
580  if(isset($params['complete']) && $params['complete']){
581  $callbacks[] = " complete: function(){ ".$params['complete']." }";
582  unset($params['complete']);
583  }
584  if(isset($params['before']) && $params['before']){
585  $callbacks[] = " before: function(){ ".$params['before']." }";
586  unset($params['before']);
587  }
588  if(isset($params['success']) && $params['success']){
589  $callbacks[] = " success: function(){ ".$params['success']." }";
590  unset($params['success']);
591  }
592 
593  if(isset($params['onclick'])) {
594  $params['onclick'].= "; return ajaxRemoteForm(this.form, \"$update\", { ".join(",", $callbacks)." });";
595  } else {
596  $params['onclick'] = "return ajaxRemoteForm(this.form, \"$update\", { ".join(",", $callbacks)." });";
597  }
598 
599 
600  return xhtml_tag('input', $params, 'type: submit');
601 }
602 
612 function submit_image_tag($caption, $src=''){
613  $params = is_array($caption) ? $caption : Util::getParams(func_get_args());
614  if(isset($params['caption'])) {
615  $params['value'] = $params['caption'];
616  unset($params['caption']);
617  } elseif(isset($params[0])) {
618  $params['value'] = $params[0];
619  }
620  if(!isset($params['src']) && isset($params[1])) {
621  $params['src'] = PUBLIC_PATH."img/{$params[1]}";
622  }
623  return xhtml_tag('input', $params, 'type: image');
624 }
625 
634 function button_tag($caption=''){
635  $params = is_array($caption) ? $caption : Util::getParams(func_get_args());
636  if(isset($params['caption'])) {
637  $params['value'] = $params['caption'];
638  unset($params['caption']);
639  } elseif(isset($params[0])) {
640  $params['value'] = $params[0];
641  }
642  return xhtml_tag('input', $params, 'type: button');
643 }
644 
654 function get_value_from_action($name){
655  $p = explode('.', $name);
656  if(count($p)>1) {
657  $value = get_value_from_action($p[0]);
658  if(is_object($value) && isset($value->$p[1])) {
659  return $value->$p[1];
660  }elseif(is_array($value) && isset($value[$p[1]])) {
661  return $value[$p[1]];
662  }else {
663  return null;
664  }
665  } else {
666  return View::getVar($name);
667  }
668 }
669 
676 function get_id_and_name($value){
677  $p = explode('.', $value);
678  if(count($p)>1) {
679  $id = "{$p[0]}_{$p[1]}";
680  $name = "{$p[0]}[{$p[1]}]";
681  } else {
682  $id = $name = $value;
683  }
684  return array('id'=>$id, 'name'=>$name);
685 }
686 
687 # Helpers
688 
693 function input_field_tag($name) {
694  $params = is_array($name) ? $name : Util::getParams(func_get_args());
698  if(isset($params[0])) {
699  $params = array_merge(get_id_and_name($params[0]), $params);
700  $value = get_value_from_action($params[0]);
701  if(!isset($params['value'])) {
702  if(!is_null($value)) {
703  $params['value'] = $value;
704  }
705  } elseif($params['type']=='radio' || $params['type']=='checkbox') {
706  if($params['value']==$value) {
707  $params['checked'] = 'checked';
708  }
709  }
710  }
711  return xhtml_tag('input', $params);
712 }
713 
720 function text_field_tag($name){
721  $params = is_array($name) ? $name : Util::getParams(func_get_args());
722  $params['type'] = 'text';
723  return input_field_tag($params);
724 }
725 
732 function checkbox_field_tag($name){
733  $params = is_array($name) ? $name : Util::getParams(func_get_args());
734  $params['type'] = 'checkbox';
735  return input_field_tag($params);
736 }
737 
744 function numeric_field_tag($name){
745  $params = is_array($name) ? $name : Util::getParams(func_get_args());
746  if(!isset($params['onkeydown'])) {
747  $params['onkeydown'] = "valNumeric(event)";
748  } else {
749  $params['onkeydown'].=";valNumeric(event)";
750  }
751  return text_field_tag($params);
752 }
753 
760 function textupper_field_tag($name){
761  $params = is_array($name) ? $name : Util::getParams(func_get_args());
762  if(!isset($params['onblur'])) {
763  $params['onblur'] = "keyUpper2(this)";
764  } else {
765  $params['onblur'].=";keyUpper2(this)";
766  }
767  return text_field_tag($params);
768 }
769 
779 function date_field_tag($name){
780  static $i = false;
781  $params = is_array($name) ? $name : Util::getParams(func_get_args());
782 
783  if(isset($params['format'])){
784  $format = $params['format'];
785  unset($params['format']);
786  } else {
787  $format = "d-m-Y";
788  }
789 
790  if(isset($params['language'])){
791  $lang = $params['language'];
792  unset($params['language']);
793  } else {
794  $lang = "es";
795  }
796 
797  if(isset($params[0])) {
798  $params = array_merge(get_id_and_name($params[0]), $params);
799  }
800 
801  $code = '';
802  if($i == false){
803  $i = true;
804  $code .= javascript_include_tag("datepicker/lang/$lang");
805  $code .= javascript_include_tag('datepicker/datepicker');
806  stylesheet_link_tag('datepicker');
807  }
808 
809  $data = get_id_and_name($name);
810  $format = str_replace('-', '-ds-', $format);
811  $code .= "
812  <script type=\"text/javascript\">
813  var opts = {
814  formElements:{'{$data['id']}':'$format'}
815  };
816  datePickerController.createDatePicker(opts);
817  </script>
818  ";
819 
820  return text_field_tag($params) . $code;
821 }
822 
829 function file_field_tag($name){
830  $params = is_array($name) ? $name : Util::getParams(func_get_args());
831  $params['type'] = 'file';
832  return input_field_tag($params);
833 }
834 
841 function radio_field_tag($name){
842  $params = is_array($name) ? $name : Util::getParams(func_get_args());
843  $params['type'] = 'radio';
844  return input_field_tag($params);
845 }
846 
854 function textarea_tag($name, $value=null){
855  $params = is_array($name) ? $name : Util::getParams(func_get_args());
859  $params = array_merge(get_id_and_name($name), $params);
860 
861  if(isset($params[1])) {
862  $value = $params[1];
863  } else {
864  $value = get_value_from_action($name);
865  }
866 
867  $value = htmlspecialchars($value, ENT_QUOTES, APP_CHARSET);
868 
869  if(!isset($params['rows'])) {
870  $params['rows'] = '25';
871  }
872  if(!isset($params['cols'])) {
873  $params['cols'] = '50';
874  }
875 
876  $code = xhtml_start_tag('textarea', $params);
877  $code.= $value;
878  $code.= xhtml_end_tag('textarea');
879  return $code;
880 }
881 
888 function password_field_tag($name){
889  $params = is_array($name) ? $name : Util::getParams(func_get_args());
890  $params['type'] = 'password';
891  return input_field_tag($params);
892 }
893 
900 function hidden_field_tag($name){
901  $params = is_array($name) ? $name : Util::getParams(func_get_args());
902  $params['type'] = 'hidden';
903  return input_field_tag($params);
904 }
905 
928 function select_tag($name, $data=array()){
929  $params = is_array($name) ? $name : Util::getParams(func_get_args());
930 
934  $params = array_merge(get_id_and_name($params[0]), $params);
935  if(!isset($params['selected'])) {
936  $value = get_value_from_action($params[0]);
937  if(!is_null($value)) {
938  $params['selected'] = $value;
939  }
940  }
941 
942  if(!isset($params[1])) {
943  return xhtml_start_tag('select', $params);
944  }
945 
946  if(isset($params['selected'])) {
947  $selected = $params['selected'];
948  unset($params['selected']);
949  }
950  if(isset($params['separator'])) {
951  $separator = $params['separator'];
952  unset($params['separator']);
953  } else {
954  $separator = '';
955  }
956 
957  $options = '';
958  if(isset($params['include_blank'])) {
959  $options.="\t".xhtml_tag('option', array('value'=>''), "content: {$params['include_blank']}");
960  unset($params['include_blank']);
961  }
962 
963  if(is_array($params[1])){
964  if(isset($params[1][0]) && is_object($params[1][0])){
965  if(isset($params['option'])) {
966  $fields = array_map('trim', explode(',', $params['option']));
967  unset($params['option']);
968  } else {
969  $fields = array('id');
970  }
971 
972  foreach($params[1] as $item) {
973  $value = $item->primary_key[0];
974  $vals = array();
975  foreach($fields as $option) {
976  array_push($vals, $item->$option);
977  }
978 
979  $k = $item->$value;
980  $v = implode($vals, $separator);
981 
982  if(isset($selected) && $selected==$k) {
983  $options.="\t".option_tag($k, $v, 'selected: selected');
984  } else {
985  $options.="\t".option_tag($k, $v);
986  }
987  }
988  } else {
989  foreach($params[1] as $k=>$v){
990  if(isset($selected) && $selected==$k) {
991  $options.= option_tag($k, $v, 'selected: selected');
992  } else {
993  $options.= option_tag($k, $v);
994  }
995  }
996  }
997  } elseif(is_string($params[1])) {
998  if(isset($params['option'])) {
999  $fields = array_map('trim', explode(',', $params['option']));
1000  unset($params['option']);
1001  } else {
1002  $fields = array('id');
1003  }
1004 
1008  $m = ActiveRecord::get($params[1]);
1009 
1010  if(isset($params['value'])) {
1011  $value = $params['value'];
1012  unset($params['value']);
1013  } else {
1014  $m2 = clone $m;
1015  $m2->dump_model();
1016  $value = $m2->primary_key[0];
1017  }
1018 
1019  if(isset($params[2]) && $params[2]) {
1020  $items = $m2->find_all_by_sql($params[2]);
1021  } else {
1025  $find_args = array();
1026 
1030  if(isset($params['conditions'])) {
1031  array_push($find_args, "conditions: {$params['conditions']}");
1032  unset($params['conditions']);
1033  }
1034  if(isset($params['columns'])) {
1035  array_push($find_args, "columns: {$params['columns']}");
1036  unset($params['columns']);
1037  }
1038  if(isset($params['join'])) {
1039  array_push($find_args, "join: {$params['join']}");
1040  unset($params['join']);
1041  }
1042  if(isset($params['group'])) {
1043  array_push($find_args, "group: {$params['group']}");
1044  unset($params['group']);
1045  }
1046  if(isset($params['having'])) {
1047  array_push($find_args, "having: {$params['having']}");
1048  unset($params['having']);
1049  }
1050  if(isset($params['order'])) {
1051  array_push($find_args, "order: {$params['order']}");
1052  unset($params['order']);
1053  }
1054  if(isset($params['distinct'])) {
1055  array_push($find_args, "distinct: {$params['distinct']}");
1056  unset($params['distinct']);
1057  }
1058  if(isset($params['limit'])) {
1059  array_push($find_args, "limit: {$params['limit']}");
1060  unset($params['limit']);
1061  }
1062  if(isset($params['offset'])) {
1063  array_push($find_args, "limit: {$params['offset']}");
1064  unset($params['offset']);
1065  }
1066 
1067  $items = call_user_func_array(array($m2, 'find'), $find_args);
1068  }
1069 
1070  foreach($items as $item) {
1071  $vals = array();
1072  foreach($fields as $option) {
1073  array_push($vals, $item->$option);
1074  }
1075 
1076  $k = $item->$value;
1077  $v = implode($vals, $separator);
1078 
1079  if(isset($selected) && $selected==$k) {
1080  $options.="\t".option_tag($k, $v, 'selected: selected');
1081  } else {
1082  $options.="\t".option_tag($k, $v);
1083  }
1084  }
1085  }
1086  return xhtml_tag('select', $params, "content: $options");
1087 }
1088 
1096 function option_tag($value, $text=''){
1097  $params = is_array($value) ? $value : Util::getParams(func_get_args());
1098 
1099  $params['value'] = $params[0];
1100  if(isset($params[1])) {
1101  $text = $params[1];
1102  } else {
1103  $text = '';
1104  }
1105  return xhtml_tag('option', $params, "content: $text");
1106 }
1107 
1108 
1115 function upload_image_tag($name){
1116  $opts = is_array($name) ? $name : Util::getParams(func_get_args());
1117  $code = '';
1118 
1119  if(isset($opts[0])){
1120  $opts['name'] = $opts[0];
1121  } else {
1122  $opts['name'] = '';
1123  }
1124  if(isset($opts['value'])){
1125  $opts['value'] = $opts[1];
1126  } else {
1127  $opts['value'] = '';
1128  }
1129 
1130  $path = PUBLIC_PATH;
1131 
1132  $code.="<span id='{$opts['name']}_span_pre'>
1133  <select name='{$opts[0]}' id='{$opts[0]}' onchange='show_upload_image(this, \"$path\")'>";
1134  $code.="<option value='@'>Seleccione...\n";
1135  foreach(scandir("public/img/upload") as $file){
1136  if($file!='index.html'&&$file!='.'&&$file!='..'&&$file!='Thumbs.db'&&$file!='desktop.ini'){
1137  $nfile = str_replace('.gif', '', $file);
1138  $nfile = str_replace('.jpg', '', $nfile);
1139  $nfile = str_replace('.png', '', $nfile);
1140  $nfile = str_replace('.bmp', '', $nfile);
1141  $nfile = strtr($nfile, '_', ' ');
1142  $nfile = ucfirst($nfile);
1143  if(urlencode("upload/$file")==$opts['value']){
1144  $code.="<option selected='selected' value='upload/$file' style='background: #EAEAEA'>$nfile</option>\n";
1145  } else {
1146  $code.="<option value='upload/$file'>$nfile</option>\n";
1147  }
1148  }
1149  }
1150  $code.="</select> <a href='#{$opts['name']}_up' name='{$opts['name']}_up' id='{$opts['name']}_up' onclick='enable_upload_file(\"{$opts['name']}\")'>Subir Imagen</a></span>
1151  <span style='display:none' id='{$opts['name']}_span'>
1152  <input type='file' id='{$opts['name']}_file' onchange='upload_file(\"{$opts['name']}\")' />
1153  <a href='#{$opts['name']}_can' name='{$opts['name']}_can' id='{$opts['name']}_can' style='color:red' onclick='cancel_upload_file(\"{$opts['name']}\")'>Cancelar</a></span>
1154  ";
1155  if(!isset($opts['width'])) {
1156  $opts['width'] = 128;
1157  }
1158  if(!isset($opts['value'])){
1159  $opts['style']="border: 1px solid black;margin: 5px;".$opts['value'];
1160  } else {
1161  $opts['style']="border: 1px solid black;display:none;margin: 5px;".$opts['value'];
1162  }
1163  $code.="<div>".img_tag(urldecode($opts['value']), 'width: '.$opts['width'], 'style: '.$opts['style'], 'id: '.$opts['name']."_im")."</div>";
1164  return $code;
1165 }
1166 
1176 function set_droppable($obj, $action=''){
1177  $params = is_array($obj) ? $obj : Util::getParams(func_get_args());
1178  if(!isset($params['name']) || !$params['name']){
1179  $params['name'] = $params[0];
1180  }
1181  if(!isset($params['action']) || !$params['action']){
1182  $params['action'] = $params[1];
1183  }
1184  return xhtml_tag('script', 'type: text/javascript', "content: Droppables.add('{$params['name']}', {hoverclass: '{$params['hover_class']}',onDrop:{$params['action']}})");
1185 }
1186 
1187 function tr_break($x=''){
1188  static $l;
1189  if($x=='') {
1190  $l = 0;
1191  return;
1192  }
1193  if(!$l) {
1194  $l = 1;
1195  } else {
1196  $l++;
1197  }
1198  if(($l%$x)==0) {
1199  print "</tr><tr>";
1200  }
1201 }
1202 
1203 function br_break($x=''){
1204  static $l;
1205  if($x=='') {
1206  $l = 0;
1207  return;
1208  }
1209  if(!$l) {
1210  $l = 1;
1211  } else {
1212  $l++;
1213  }
1214  if(($l%$x)==0) {
1215  print "<br/>\n";
1216  }
1217 }
1222 function tr_color(){
1223  static $i;
1224  if(func_num_args()>1){
1225  $params = Util::getParams(func_get_args());
1226  }
1227  if(!$i) {
1228  $i = 1;
1229  }
1230  print "<tr bgcolor=\"{$params[$i-1]}\"";
1231  if(count($params)==$i) {
1232  $i = 1;
1233  } else {
1234  $i++;
1235  }
1236  if(isset($params)){
1237  if(is_array($params)){
1238  foreach($params as $key => $value){
1239  if(!is_numeric($key)){
1240  print " $key = '$value'";
1241  }
1242  }
1243  }
1244  }
1245  print ">";
1246 }
1253 function tr_color_class(){
1254  static $i;
1255  static $c = true;
1256  $id = "";
1257  //$code = "";
1258  $params = Util::getParams(func_get_args());
1259  if(isset($params['id'])){
1260  $id = " id=\"{$params['id']}\"";
1261  }
1262  if($c){
1263  $code = "<tr class='$params[0]' $id";
1264  $c = false;
1265  } else {
1266  $code = "<tr class='$params[1]' $id";
1267  $c = true;
1268  }
1269  $code .= ">";
1270  return $code;
1271 }
1272 
1286 function button_to_action($caption, $action='', $classCSS=''){
1287  $params= is_array($caption) ? $caption : Util::getParams(func_get_args());
1288 
1289  if(isset($params['caption'])) {
1290  $caption = $params['caption'];
1291  unset($params['caption']);
1292  } elseif(isset($params[0])) {
1293  $caption = $params[0];
1294  } else {
1295  $caption = '';
1296  }
1297  if(isset($params['action'])) {
1298  $action = $params['action'];
1299  unset($params['action']);
1300  } elseif(isset($params[1])) {
1301  $action = $params[1];
1302  } else {
1303  $action = '';
1304  }
1305  if(isset($params[2])) {
1306  $params['class'] = $params[2];
1307  }
1308 
1309  if(isset($params['onclick'])) {
1310  $params['onclick'].=';window.location="'.get_kumbia_url($action).'";';
1311  } else {
1312  $params['onclick'] = 'window.location="'.get_kumbia_url($action).'";';
1313  }
1314 
1315  return xhtml_tag('button', $params, "content: $caption");
1316 }
1317 
1327 function button_to_remote_action($caption, $action='', $classCSS=''){
1328  $opts = is_array($caption) ? $caption : Util::getParams(func_get_args());
1329  if(func_num_args()==2){
1330  $opts['action'] = $opts[1];
1331  $opts['caption'] = $opts[0];
1332  } else {
1333  if(!isset($opts['action'])||!$opts['action']) {
1334  $opts['action'] = $opts[1];
1335  }
1336  if(!isset($opts['caption'])||!$opts['caption']) {
1337  $opts['caption'] = $opts[0];
1338  }
1339  }
1340 
1341  $opts['action'] = PUBLIC_PATH . $opts['action'];
1342 
1343  if(!isset($opts['update'])){
1344  $opts['update'] = "";
1345  }
1346 
1347  if(!isset($opts['success'])){
1348  $opts['success'] = '';
1349  }
1350  if(!isset($opts['before'])){
1351  $opts['before'] = '';
1352  }
1353  if(!isset($opts['complete'])){
1354  $opts['complete'] = '';
1355  }
1356 
1357  $code = "<button onclick='AJAX.execute({action:\"{$opts['action']}\", container:\"{$opts['update']}\", callbacks: { success: function(){{$opts['success']}}, before: function(){{$opts['before']}} } })'";
1358  unset($opts['action']);
1359  unset($opts['success']);
1360  unset($opts['before']);
1361  unset($opts['complete']);
1362  foreach($opts as $k => $v){
1363  if(!is_numeric($k)&&$k!='caption'){
1364  $code.=" $k='$v' ";
1365  }
1366  }
1367  $code.=">{$opts['caption']}</button>";
1368  return $code;
1369 }
1370 
1386 function updater_select($name, $data=array()){
1387  $params = is_array($name) ? $name : Util::getParams(func_get_args());
1388 
1392  if(isset($params[0])) {
1393  $params = array_merge(get_id_and_name($params[0]), $params);
1394  if(!isset($params['selected'])) {
1395  $value = get_value_from_action($params[0]);
1396  if(!is_null($value)) {
1397  $params['selected'] = $value;
1398  }
1399  }
1400  }
1401 
1402  if(isset($params['container'])) {
1403  $update = $params['container'];
1404  unset($params['container']);
1405  } elseif(isset($params['update'])) {
1406  $update = $params['update'];
1407  unset($params['update']);
1408  } else {
1409  $update = '';
1410  }
1411 
1412  if(isset($params['action'])) {
1413  $action = $params['action'];
1414  unset($params['action']);
1415  } else {
1416  $action = '';
1417  }
1418 
1419  $action = PUBLIC_PATH . $action;
1420 
1421  $onchange = "AJAX.viewRequest({action: '$action/'+$(\"{$params['id']}\").value, container: '$update'";
1422  $call = array();
1423  if(isset($params['before'])){
1424  $call["before"] = "before: function(){ {$params['before']} }";
1425  unset($params['before']);
1426  }
1427  if(isset($params['oncomplete'])){
1428  $call["oncomplete"] = "oncomplete: function(){ {$params['oncomplete']} }";
1429  unset($params['oncomplete']);
1430  }
1431  if(isset($params['success'])){
1432  $call["success"] = "success: function(){ {$params['success']} }";
1433  unset($params['success']);
1434  }
1435  if(count($call)){
1436  $onchange.=", callbacks: { ";
1437  $onchange.=implode(",", $call);
1438  $onchange.="}";
1439  }
1440  $onchange.="})";
1441 
1442  if(isset($params['onchange'])) {
1443  $params['onchange'].=';'.$onchange;
1444  } else {
1445  $params['onchange'] = $onchange;
1446  }
1447 
1448  return select_tag($params);
1449 }
1450 
1463  $params = is_array($name) ? $name : Util::getParams(func_get_args());
1464 
1468  if(isset($params[0])) {
1469  $params = array_merge(get_id_and_name($params[0]), $params);
1470  if(!isset($params['value'])) {
1471  $value = get_value_from_action($params[0]);
1472  if(!is_null($value)) {
1473  $params['value'] = $value;
1474  }
1475  }
1476  }
1477 
1478  $hash = md5(uniqid());
1479 
1480  if(isset($params['after_update'])) {
1481  $after_update = $params['after_update'];
1482  unset($params['after_update']);
1483  }else {
1484  $after_update = 'function(){}';
1485  }
1486  if(isset($params['action'])) {
1487  $action = $params['action'];
1488  unset($params['action']);
1489  }else {
1490  $action = '';
1491  }
1492  if(isset($params['message'])) {
1493  $message = $params['message'];
1494  unset($params['message']);
1495  }else {
1496  $message = 'Consultando..';
1497  }
1498 
1499  $code = text_field_tag($params);
1500 
1501  $code.= "
1502  <span id='indicator$hash' style='display: none'><img src='".PUBLIC_PATH."img/spinner.gif' alt='$message'/></span>
1503  <div id='{$params[0]}_choices' class='autocomplete'></div>
1504  <script type='text/javascript'>
1505  <!-- <![CDATA[
1506  new Ajax.Autocompleter(\"{$params['id']}\", \"{$params['id']}_choices\", \"".get_kumbia_url($action)."\", { minChars: 2, indicator: 'indicator$hash', afterUpdateElement : {$after_update}});
1507  // ]]> -->
1508  </script>
1509  ";
1510  return $code;
1511 }
1512 
1518 function xhtml_template($template='template'){
1519  print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1520  <html xmlns="http://www.w3.org/1999/xhtml">
1521  <head>
1522  <title>Kumbia PHP Framework</title>
1523  <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />';
1524  print stylesheet_link_tag("style", 'use_variables: true');
1526  print '</head>
1527  <body class="'.$template.'">';
1528  content();
1529  print '
1530  </body>
1531  </html>';
1532 
1533 }
1534 
1535 function tab_tag($tabs, $color='green', $width=800){
1536 
1537  switch($color){
1538  case 'blue':
1539  $col1 = '#E8E8E8'; $col2 = '#C0c0c0'; $col3 = '#000000';
1540  break;
1541 
1542  case 'pink':
1543  $col1 = '#FFE6F2'; $col2 = '#FFCCE4'; $col3 = '#FE1B59';
1544  break;
1545 
1546  case 'orange':
1547  $col1 = '#FCE6BC'; $col2 = '#FDF1DB'; $col3 = '#DE950C';
1548  break;
1549 
1550  case 'green':
1551  $col2 = '#EAFFD7'; $col1 = '#DAFFB9'; $col3 = '#008000';
1552  break;
1553  }
1554 
1555 
1556  print "
1557  <table cellspacing=0 cellpadding=0 width=$width>
1558  <tr>";
1559  $p = 1;
1560  $w = $width;
1561  foreach($tabs as $tab){
1562  if($p==1) $color = $col1;
1563  else $color = $col2;
1564  $ww = (int) ($width * 0.22);
1565  $www = (int) ($width * 0.21);
1566  print "<td align='center'
1567  width=$ww style='padding-top:5px;padding-left:5px;padding-right:5px;padding-bottom:-5px'>
1568  <div style='width:$www"."px;border-top:1px solid $col3;border-left:1px solid $col3;border-right:1px solid $col3;background:$color;padding:2px;color:$col3;cursor:pointer' id='spanm_$p'
1569  onclick='showTab($p, this)'
1570  >".$tab['caption']."</div></td>";
1571  $p++;
1572  $w-=$ww;
1573  }
1574  print "
1575  <script>
1576  function showTab(p, obj){
1577  for(i=1;i<=$p-1;i++){
1578  $('tab_'+i).hide();
1579  $('spanm_'+i).style.background = '$col2';
1580  }
1581  $('tab_'+p).show();
1582  obj.style.background = '$col1'
1583  }
1584  </script>
1585  ";
1586  $p = $p + 1;
1587  //$w = $width/2;
1588  print "<td width=$w></td><tr>";
1589  print "<td colspan=$p style='border:1px solid $col3;background:$col1;padding:10px'>";
1590  $p = 1;
1591  foreach($tabs as $tab){
1592  if($p!=1){
1593  print "<div id='tab_$p' style='display:none'>";
1594  } else {
1595  print "<div id='tab_$p'>";
1596  }
1597  View::partial($tab['partial']);
1598  print "</div>";
1599  $p++;
1600  }
1601  print "<br></td><td width=30></td>";
1602  print "</table>";
1603 }
1604 
1609 function js_execute($s) {
1610  return xhtml_tag('script', 'type: text/javascript', "content: $s");
1611 }
1612 
1617 function js_alert($s) {
1618  return js_execute('alert("'.addslashes($s).'");');
1619 }
1620 
1629 function time_field_tag($name='') {
1630  $params = is_array($name) ? $name : Util::getParams(func_get_args());
1631 
1632  $hours = array ('00' => '00', '01' => '01', '02' => '02', '03' => '03', '04' => '04', '05' => '05' , '06' => '06',
1633  '07' => '07', '08' => '08', '09' => '09', '10' => '10', '11' => '11', '12' => '12', '13' => '13',
1634  '14' => '14', '15' => '15', '16' => '16', '17' => '17', '18' => '18', '19' => '19', '20' => '20',
1635  '21' => '21', '22' => '22', '23' => '23');
1636 
1637  $mins = array ('00' => '00', '01' => '01', '02' => '02', '03' => '03', '04' => '04', '05' => '05' , '06' => '06',
1638  '07' => '07', '08' => '08', '09' => '09', '10' => '10', '11' => '11', '12' => '12', '13' => '13',
1639  '14' => '14', '15' => '15', '16' => '16', '17' => '17', '18' => '18', '19' => '19', '20' => '20',
1640  '21' => '21', '22' => '22', '23' => '23', '24' => '24', '25' => '25', '26' => '26', '27' => '27',
1641  '28' => '28', '29' => '29', '30' => '30', '31' => '31', '32' => '32', '33' => '33', '34' => '34',
1642  '35' => '35', '36' => '36', '37' => '37', '38' => '38', '39' => '39', '40' => '40', '41' => '41',
1643  '42' => '42', '43' => '43', '44' => '44', '45' => '45', '46' => '46', '47' => '47', '48' => '48', '49' => '49',
1644  '50' => '50', '51' => '51' , '52' => '52', '53' => '53', '54' => '54', '55' => '55', '56' => '56',
1645  '57' => '57', '58' => '58', '59' => '59' );
1646 
1647  if(isset($params['value'])) {
1648  $value = $params['value'];
1649  unset($params['value']);
1650  } else {
1651  $value = get_value_from_action($name);
1652  }
1653 
1654  if(is_null($value)) {
1655  $value = '00:00:00';
1656  }
1657 
1658  $format = isset($params['format']) ? $params['format']: '%h:%m';
1659  $hidden = hidden_field_tag($params[0], "value: $value");
1660 
1661  if($value) {
1662  $value = explode(':', $value);
1663  }
1664 
1665  $data = get_id_and_name($params[0]);
1666  $code = '';
1667  $format = explode(':', $format);
1668 
1669  $onchange = "
1670  var hora = document.getElementById('{$data['id']}_h');
1671  var min = document.getElementById('{$data['id']}_m');
1672  var seg = document.getElementById('{$data['id']}_s');
1673  var value = '';
1674  if(hora) {
1675  value+=hora.value;
1676  } else {
1677  value+='00';
1678  }
1679  value+=':';
1680  if(min) {
1681  value+=min.value;
1682  } else {
1683  value+='00';
1684  }
1685  value+=':';
1686  if(seg) {
1687  value+=seg.value;
1688  } else {
1689  value+='00';
1690  }
1691  document.getElementById('{$data['id']}').value = value;
1692  ";
1693 
1694  if(isset($params['onchange'])) {
1695  $params['onchange'].=$onchange;
1696  } else {
1697  $params['onchange'] = $onchange;
1698  }
1699 
1700  foreach($format as $f) {
1701  if($f=='%h') {
1702  if($code) {
1703  $code.=':';
1704  }
1705  if($value) {
1706  $params['selected'] = $value[0];
1707  }
1708  $params[1] = $hours;
1709  $code.=select_tag(array_merge($params, array('name'=>'', 'id'=>$data['id'].'_h')));
1710  unset($params['selected']);
1711  } elseif($f=='%m') {
1712  if($code) {
1713  $code.=':';
1714  }
1715  if($value) {
1716  $params['selected'] = $value[1];
1717  }
1718  $params[1] = $mins;
1719  $code.=select_tag(array_merge($params, array('name'=>'', 'id'=>$data['id'].'_m')));
1720  unset($params['selected']);
1721  } elseif($f=='%s') {
1722  if($code) {
1723  $code.=':';
1724  }
1725  if($value) {
1726  $params['selected'] = $value[2];
1727  }
1728  $params[1] = $mins;
1729  $code.=select_tag(array_merge($params, array('name'=>'', 'id'=>$data['id'].'_s')));
1730  unset($params['selected']);
1731  }
1732  }
1733 
1734  return $code.$hidden;
1735 }
1736 
1743 function month_field_tag($name) {
1744  $params = is_array($name) ? $name : Util::getParams(func_get_args());
1745  if(isset($params['use_month_numbers'])) {
1746  $meses = array('01'=>'01', '02'=>'02', '03'=>'03', '04'=>'04', '05'=>'05','06'=>'06',
1747  '07'=>'07', '08'=>'08', '09'=>'09', '10'=>'10', '11'=>'11', '12'=>'12');
1748  unset($params['use_month_numbers']);
1749  } else {
1750  $meses = array('01'=>'Enero', '02'=>'Febrero', '03'=>'Marzo', '04'=>'Abril', '05'=>'Mayo','06'=>'Junio',
1751  '07'=>'Julio', '08'=>'Agosto', '09'=>'Septiembre', '10'=>'Octubre', '11'=>'Noviembre', '12'=>'Diciembre');
1752  }
1753  $params[1] = $meses;
1754  return select_tag($params);
1755 }
1756 
1768 function swf_tag($data){
1769  $params = is_array($data) ? $data : Util::getParams(func_get_args());
1770 
1771  if(!isset($params['data']) && isset($params[0])){
1772  $temp = str_replace(".swf", "", $params[0]);
1773  $params['data'] = PUBLIC_PATH."swf/{$temp}.swf";
1774  unset($params[0]);
1775  }else{
1776  $temp = str_replace(".swf", "", $params['data']);
1777  $params['data'] = PUBLIC_PATH."swf/{$temp}.swf";
1778  }
1779 
1780  if(!isset($params['type'])){
1781  $params['type'] = 'application/x-shockwave-flash';
1782  }
1783 
1784  if(!isset($params['wmode'])){
1785  $wmode = 'transparent';
1786  }else{
1787  $wmode = $params['wmode'];
1788  unset($params['wmode']);
1789  }
1790 
1791  $code = xhtml_start_tag('object', $params);
1792  $code .= '<param name="movie" value="'.$params['data'].'" />';
1793  $code .= '<param name="wmode" value="'.$wmode.'" />';
1794  $code .= xhtml_end_tag('object');
1795 
1796  return $code;
1797 }
1805  $return_url = PUBLIC_PATH;
1806 
1807  $action = $url;
1808  $module = '';
1809  if(is_array($url)){
1810  $action = $url[0];
1811  if(isset($url['module'])){
1812  $module = $url['module'];
1813  }
1814  }
1815  if($module){
1816  $return_url.=$module.'/';
1817  }
1818  $return_url.=$action;
1819  return $return_url;
1820 }
1827 {
1828  if(isset(TagsData::$data['KUMBIA_CSS_IMPORTS'])){
1829  $imports = TagsData::$data['KUMBIA_CSS_IMPORTS'];
1830  if (is_array($imports)) {
1831  foreach ($imports as $css) {
1832  echo $css;
1833  }
1834  } else {
1835  echo $imports;
1836  }
1837  }
1838 }
1839 // Añadida para permitir la compatibilidad de los helpers antiguos
1840 class TagsData {
1841  public static $data = array();
1842 }