Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/04/2013, 09:55
Avatar de HiToGoRoShi
HiToGoRoShi
 
Fecha de Ingreso: abril-2008
Mensajes: 849
Antigüedad: 16 años
Puntos: 31
Aporte de calendario para eventos

Acabo de hacer un sistema de calendario para eventos en mi rato de aburrimiento espero que le sea útil a todos

Clase del Calendario y los Eventos
Código PHP:
Ver original
  1. <?php
  2. class Calendario
  3. {
  4.     private $fecha;
  5.     private $mes;
  6.     private $hoy;
  7.     private $w;
  8.     private $eventos = array();
  9.    
  10.     public function __CONSTRUCT($fecha = 0)
  11.     {
  12.         // La fecha actual
  13.         $this->fecha = new DateTime($fecha != 0 ? $fecha : date('Y-m-d'));
  14.         // La cantidad de dias del mes
  15.         $this->mes = $this->fecha->format('t');
  16.         // El dia de hoy
  17.         $this->hoy = $this->fecha->format('j');
  18.         // El valor numerico de la semana del primer dia del mes
  19.         $this->w = $this->fecha->modify('-' .($this->hoy-1). ' day')->format('w');
  20.         // Restamos para saber desde donde va a empezar a pintar, por ejemplo
  21.         // si comienza un miercoles, debe dejar 3 cuadros vacios atraz
  22.         $this->w = $this->w > 0 ? -$this->w+1 : 0;
  23.     }
  24.     public function setEvento(Evento $evento)
  25.     {
  26.         $this->eventos[] = $evento;
  27.     }
  28.     public function Pintar()
  29.     {
  30.         echo '<table class="calendario">';
  31.         echo '
  32.             <tr>
  33.                 <th>Domingo</th>
  34.                 <th>Lunes</th>
  35.                 <th>Martes</th>
  36.                 <th>Miercoles</th>
  37.                 <th>Jueves</th>
  38.                 <th>Viernes</th>
  39.                 <th>Sabado</th>
  40.             <tr>
  41.         ';
  42.         $i=0;
  43.         for($this->w;$this->w<=$this->mes;$this->w++)
  44.         {
  45.             echo ($i == 0) ? '<tr>' : '';
  46.             echo ($i%7 == 0 && $i > 0) ? '</tr><tr>' : '';
  47.            
  48.             if($this->w<=0)
  49.             {
  50.                 echo '<td></td>';
  51.             }else
  52.             {
  53.                 echo '<td ' . ($this->hoy == $i ? 'class="hoy"' : '') . '>'; // Si el dia es hoy
  54.                 // Pintamos el dia actual
  55.                 echo '<span class="dia">' . $this->w . '</span>';
  56.                 // Agregar los eventos
  57.                 foreach($this->eventos as $e)
  58.                 {
  59.                     if($e->getFecha()->format('d')==$i)
  60.                     {
  61.                         echo '<div class="evento">';
  62.                         echo '- <a href="#" class="asunto">' . $e->getAsunto() .  '</a> ';
  63.                         echo 'programado para las ' . $e->getHora() . ' por <b>' . $e->getCreador() . '</b>';
  64.                         echo '</div>';                 
  65.                     }
  66.                 }
  67.                 echo '</td>';
  68.             }
  69.             $i++;
  70.         }
  71.         echo '</table>';
  72.     }
  73. }
  74.  
  75.  
  76. class Evento
  77. {
  78.     private $fecha;
  79.     private $hora;
  80.     private $creador;
  81.     private $asunto;
  82.    
  83.     public function getFecha() { return $this->fecha; }
  84.     public function getHora() { return $this->hora; }
  85.     public function getCreador() { return $this->creador; }
  86.     public function getAsunto() { return $this->asunto; }
  87.     public function setFecha($x)
  88.     {
  89.         $this->fecha = new DateTime($x);
  90.         $this->hora = $this->fecha->format('h:ia');
  91.     }
  92.     public function setCreador($x) { $this->creador = $x; }
  93.     public function setAsunto($x) { $this->asunto = $x; }
  94. }

Como seteamos eventos?
Código PHP:
Ver original
  1. <?php
  2. // Instanciamos el calendario
  3. $c = new Calendario('2013-04-16');
  4.  
  5. // Seteamos un evento
  6. $e = new Evento();
  7. $e->setFecha('2013-04-16 14:02:00');
  8. $e->setCreador('HiToGoRoShi');
  9. $e->setAsunto('Reunion en con nuestro cliente');
  10.  
  11. $c->setEvento($e);
  12.  
  13. // Seteamos otro evento
  14. $e = new Evento();
  15. $e->setFecha('2013-04-18 14:07:00');
  16. $e->setCreador('Kenshin');
  17. $e->setAsunto('Capacitacion en sistema');
  18.  
  19. $c->setEvento($e);
  20.  
  21. // Seteamos otro evento
  22. $e = new Evento();
  23. $e->setFecha('2013-04-18 14:15:00');
  24. $e->setCreador('Raul');
  25. $e->setAsunto('Reunion con los proveedores');
  26.  
  27. $c->setEvento($e);
  28.  
  29. // Seteamos otro evento
  30. $e = new Evento();
  31. $e->setFecha('2013-04-24 14:16:00');
  32. $e->setCreador('Pepe');
  33. $e->setAsunto('Cena con los empleados');
  34.  
  35. $c->setEvento($e);

Como lo pintamos?
Código PHP:
Ver original
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Calendario de eventos creado por HiToGoRoShi</title>
  5.         <link href="calendario.css" rel="stylesheet" />
  6.     </head>
  7. <body>
  8.     <h1>Calendario de eventos creado por HiToGoRoShi</h1>
  9.     <?php $c->Pintar(); ?>
  10. </body>
  11. </html>

Una hoja de estilo para que se vea algo mejor, guardarlo como calendario.css
Código CSS:
Ver original
  1. .calendario
  2. {
  3.     width:100%;
  4.     font-family:arial;
  5.     border-spacing:0;
  6. }
  7. .calendario th
  8. {
  9.     background:#356AA0;
  10.     color:white;
  11.     text-align:left;
  12.     height:30px;
  13.     line-height:30px;
  14. }
  15. .calendario td
  16. {
  17.     width:14%;
  18. }
  19. .calendario td.hoy
  20. {
  21.     background:#C3D9FF;
  22. }
  23. .calendario td span.dia
  24. {
  25.     background:#eee;
  26.     width:20px;
  27.     padding:5px;
  28.     display:block;
  29.     text-align:center;
  30. }
  31. .calendario td.hoy span.dia
  32. {
  33.     background:#356AA0;
  34.     color:white;
  35. }
  36. .calendario td div.evento
  37. {
  38.     display:block;
  39.     padding:4px;
  40.     clear:both;
  41.     font-size:12px;
  42.     margin:2px;
  43.     background:#F9F7ED;
  44.     border:1px solid #d3d3d3;
  45. }
  46. .calendario td div.evento a
  47. {
  48.     color:black;
  49.     text-decoration:none;
  50.     font-weight:bold;
  51. }

Ya lo probre y funciona bien, creo que esta bien facil de entender y modificar .. mas bien la funcion Pintar() no deberia hacer el ECHO, sino devolver una cadena que tenga armado el calendario y recien cuando se invoque el metodo ahi hariamos el ECHO

Demo en linea
http://kimsa-media.com/calendario/

Última edición por HiToGoRoShi; 16/04/2013 a las 12:02