00001 <?php
00016 abstract class Iu{
00017
00018
00019
00020 public $formatoFechaHora = 'd/m/Y H:i:s';
00021 const NS_PAX = "http://www.ilkebenson.com/pax";
00022 const CABECERA_XHTML = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
00023 const CABECERA_HTTP_XHTML = "Content-Type: application/xhtml+xml; charset:utf-8; Cache-control: no-cache";
00024 const CABECERA_HTTP_AJAX = "Content-Type: application/xml; charset:utf-8; Cache-control: no-cache;";
00025 const DIR_JS = DIR_JS;
00026 const DIR_CSS = DIR_CSS;
00027
00031 public function cabecerasCampoFecha(){
00032 $xhtml = '<script type="text/JavaScript" src="iu/js/calendario/calendar.js"></script>'."\n";
00033 $xhtml .= '<script type="text/JavaScript" src="iu/js/calendario/lang/calendar-sp.js"></script>'."\n";
00034 $xhtml .= '<script type="text/JavaScript" src="iu/js/calendario/calendar-setup.js"></script>'."\n";
00035 $xhtml .= '<link href="iu/css/calendario/calendar-win2k-cold-1.css" rel="stylesheet" type="text/css"/>'."\n";
00036
00037 return $xhtml;
00038 }
00046 protected function campoFecha($id,$clase,$valor){
00047 $xhtml = '<input type="text" size="10" maxlength="10"';
00048 if (isset($id)) $xhtml .= " id=\"$id\" name=\"$id\" ";
00049 if ($valor=='hoy')
00050 $xhtml .= "value=\"".date($this->formatoFecha)."\" ";
00051 else
00052 $xhtml .= "value=\"$valor\" ";
00053
00054 $xhtml .= "/>";
00055 $xhtml .= '<script type="text/javascript">';
00056 $xhtml .= ' Calendar.setup({';
00057 $xhtml .= ' inputField : "'.$id.'",';
00058
00059 $xhtml .= ' ifFormat : "%d/%m/%Y",';
00060 $xhtml .= ' button : "'.$id.'"})';
00061 $xhtml .= '</script>';
00062
00063 return $xhtml;
00064 }
00073 protected function cargarInterfazEnPlantilla($ficheroInterfaz, &$plantilla){
00074 $docInterfaz = new DOMDocument();
00075 if (!$docInterfaz->load($ficheroInterfaz)) throw new ExcepcionIuCargarXML($ficheroInterfaz);
00076 $pagina = $docInterfaz->documentElement;
00077 $trozos = explode("/",$ficheroInterfaz);
00078 $trozos = explode(".", $trozos[sizeof($trozos)-1]);
00079 $nombre= $trozos[0];
00080 if ($pagina->hasAttribute("título")){
00081 $nodoTitulo = $plantilla->getElementsByTagName("title")->item(0);
00082 $nodoTitulo->childNodes->item(0)->nodeValue .= $pagina->getAttribute("título");
00083 }
00084 if ($pagina->hasAttribute("scriptPropio"))
00085 if ($pagina->getAttribute("scriptPropio") == "sí")
00086 $this->ponScript($plantilla, self::DIR_JS.$nombre.".js");
00087 if ($pagina->hasAttribute("estiloPropio"))
00088 if ($pagina->getAttribute("estiloPropio") == "sí")
00089 $this->ponCSS($plantilla, self::DIR_CSS.$nombre.".css");
00090
00091
00092 $nodoInterfaz = $plantilla->getElementsByTagNameNS(self::NS_PAX, "interfaz")->item(0);
00093
00094
00095 for ($i=0; $i<$pagina->childNodes->length; $i++){
00096 $nuevoNodo = $plantilla->importNode($pagina->childNodes->item($i),true);
00097 $nodoInterfaz->parentNode->insertBefore($nuevoNodo, $nodoInterfaz);
00098 }
00099 $nodoInterfaz->parentNode->removeChild($nodoInterfaz);
00100 }
00105 protected function cargarPlantilla($plantilla){
00106 $docXML = new DOMDocument();
00107 if (!$docXML->load($plantilla)) throw new ExcepcionIuCargarXML($fichero);
00108 return $docXML;
00109
00110 }
00117 private function cerrarTag($tag, $xml){
00118 $indice = 0;
00119 while ($indice< strlen($xml)){
00120 $pos = strpos($xml, "<$tag ", $indice);
00121 if ($pos){
00122 $posCierre = strpos($xml, ">", $pos);
00123 if ($xml[$posCierre-1] == "/"){
00124 $xml = substr_replace($xml, "></$tag>", $posCierre-1, 2);
00125 }
00126 $indice = $posCierre;
00127 }
00128 else break;
00129 }
00130 return $xml;
00131 }
00135 protected function enviarXHTML($xhtml){
00136 header(CABECERA_HTTP_XHTML);
00137 echo ($xhtml);
00138 }
00143 private function ponCSS(&$xml,$path){
00144 $head = $xml->getElementsByTagName("head")->item(0);
00145 $link = $xml->createElement("link");
00146 $link->setAttribute("rel", "stylesheet");
00147 $link->setAttribute("type", "text/css");
00148 $link->setAttribute("href", $path);
00149 $head->appendChild($link);
00150 }
00157 private function ponFecha(&$xml, &$nodo){
00158
00159 $campoTexto = $xml->createElement("input");
00160 $campoTexto->setAttribute("type", "text");
00161 $campoTexto->setAttribute("size", "10");
00162 $campoTexto->setAttribute("maxlength", "10");
00163 $id = $nodo->getAttribute("id");
00164 $campoTexto->setAttribute("id", $id);
00165 $campoTexto->setAttribute("name", $id);
00166 $fecha = date($this->formatoFecha);
00167 if ($nodo->hasAttribute("valor"))
00168 if ($nodo->getAttribute("valor") != "hoy")
00169 $fecha = $nodo->getAttribute("valor");
00170 $campoTexto->setAttribute("value", $fecha);
00171
00172
00173 $script = $xml->createElement("script");
00174 $script->setAttribute("type", "text/JavaScript");
00175 $codigo = ' Calendar.setup({';
00176 $codigo .= ' inputField : "'.$id.'",';
00177
00178 $codigo .= ' ifFormat : "%d/%m/%Y",';
00179 $codigo .= ' button : "'.$id.'"})';
00180 $script->appendChild($xml->createTextNode($codigo));
00181
00182
00183 $span = $xml->createElement("span");
00184 $span->appendChild($campoTexto);
00185 $span->appendChild($script);
00186
00187
00188 $padre = $nodo->parentNode;
00189 $padre->replaceChild($span, $nodo);
00190
00191
00192 $this->ponScript($xml, self::DIR_JS.'calendario/calendar.js');
00193 $this->ponScript($xml, self::DIR_JS.'calendario/lang/calendar-sp.js');
00194 $this->ponScript($xml, self::DIR_JS.'calendario/calendar-setup.js');
00195
00196
00197 $this->ponCSS($xml, self::DIR_CSS.'calendario/calendar-win2k-cold-1.css');
00198 }
00203 private function ponScript(&$xml,$path){
00204 $head = $xml->getElementsByTagName("head")->item(0);
00205 $script = $xml->createElement("script");
00206 $script->setAttribute("type", "text/JavaScript");
00207 $script->setAttribute("src", $path);
00208
00209
00210 $head->appendChild($script);
00211 }
00218 protected function procesar(&$xml, $ns){
00219 if (!isset($ns)) $ns = self::NS_PAX;
00220 $nodos = $xml->getElementsByTagNameNS($ns, "*");
00221
00222 while ($nodos->length > 0){
00223 $nodo = $nodos->item(0);
00224
00225 if ($nodo->nodeName == "pax:página") continue;
00226 $trozos = explode(":",$nodo->nodeName);
00227 $clase = ucfirst($trozos[1]);
00228 $campo = new ${'clase'}($nodo);
00229
00230
00231 $nuevoNodo = $xml->importNode($campo->verNodo(), true);
00232 $padre = $nodo->parentNode;
00233 $padre->replaceChild($nuevoNodo, $nodo);
00234 }
00235 }
00240 public function responderPorAjax($objeto){
00241 $respuesta = new Xml($objeto);
00242 header(self::CABECERA_HTTP_AJAX);
00243 echo $respuesta->verXML();
00244 }
00251 protected function sustituirTexto(&$dom, $id, $texto){
00252 $xp = new DomXPath($dom);
00253 $res = $xp->query("//*[@id = '$id']");
00254 $nodo = $res->item(0);
00255 if ($nodo->hasChildNodes())
00256 $nodo->removeChild($nodo->firstChild);
00257 $nodoTexto = $dom->createTextNode($texto);
00258 $nodo->appendChild($nodoTexto);
00259 }
00264 protected function verXHTML($doc){
00265 $xhtml = self::CABECERA_XHTML."\n";
00266 $xhtml .= $doc->saveXML($doc->documentElement);
00267
00268
00269
00270 $xhtml = $this->cerrarTag("script", $xhtml);
00271 $xhtml = $this->cerrarTag("select", $xhtml);
00272 return $xhtml;
00273 }
00274 }
00275
00276
00277 class ExcepcionIuCargarXML extends Excepcion{
00278 public function __construct($fichero){
00279 $titulo = "El fichero XML '$fichero' no pudo ser cargado";
00280 $texto = "La aplicación no ha podido cargar un fichero del tipo XML.";
00281 $solucionProgramador = "Compruebe que el fichero existe, que hay permisos para leerlo y que su sintaxis XML es correcta.";
00282 parent::__construct($titulo,$texto,$solucionProgramador);
00283 }
00284 }
00285 class ExcepcionIuInsertarInterfaz extends Excepcion{
00286 public function __construct($fichero){
00287 $titulo = "El interfaz correspondiente a '$fichero' no pudo insertarse en la plantilla";
00288 $texto = "La aplicación no ha podido sustituir el nodo de la plantilla con el nodo del interfaz.";
00289 $solucionProgramador = "Compruebe que la plantilla contiene un nodo de interfaz.";
00290 parent::__construct($titulo,$texto,$solucionProgramador);
00291 }
00292 }
00293
00294 return true;
00295 ?>