Calendario adaptado

Hola a todos. Quisiera adaptar un calendario para que muestre los días de la semana de una forma especial. Actualmente tengo una aplicación hecha con CodeCharge. En la página de citas tengo un calendario que solamente muestra los días de la semana que tienen por lo menos una cita. Para ello, el número del día se recupera del campo ‘cita_fecha’ de la tabla ‘cita’, agrupado por cita_fecha como muestra la imagen.

2707

agenda.png

Quisiera saber si existe alguna forma de implementar este funcionamiento con algún widget como cal.

Muchas gracias a todos.

He podido hacer algo con el widget simpleCalendar, pero tengo un problema para recuperar la fecha de la base de datos. No me deja seleccionar la fecha de la cita en formato ‘Y-m’ sino que tiene que ser ‘Y-m-d’.

El código en el widget:




class SimpleCalendarWidget extends CWidget {

    

    protected $_day             = null;

    protected $_month           = null;

    protected $_year            = null;

    public    $cita_array       = null;

    

    public function init() {

        $this->initDate();

        $this->getCitas();

        parent::init();

    }

    

    protected function initDate() {

        if(isset($_GET['year'])) {

            $this->year = $_GET['year'];

        }

        

        if(isset($_GET['month'])) {

            $this->month = $_GET['month'];

        }

        

        if(isset($_GET['day'])) {

            $this->day = $_GET['day'];

        }

    }

    

    protected function getCitas(){

        $cita=null;

        $fecha="$this->year-$this->month-27"; //This works

        //$fecha="$this->year-$this->month"; //This don't work!!!!

        $criteria=new CDbCriteria;

        $criteria->addCondition("cita_fecha=:fecha");

        $criteria->params=(array(':fecha'=>$fecha));

        $criteria->distinct='cita_fecha';

        $cita=Cita::model()->findAll($criteria);

        $this->cita_array=CHtml::listData($cita,'cita_fecha','cita_id');

    }

..............



El código en la vista:




<table id="calendar">

    <thead>

        <tr class="month-year-row">

            <th class="previous-month"><?php echo CHtml::link('<<', $this->previousLink); ?></th>

            <th colspan="5"><?php echo $this->monthName.', '.$this->year; ?></th>

            <th class="next-month"><?php echo CHtml::link('>>', $this->nextLink); ?></th>

        </tr>

        <tr class="weekdays-row">

            <?php foreach(Yii::app()->locale->getWeekDayNames('narrow') as $weekDay): ?>

                <th><?php echo $weekDay; ?></th>

            <?php endforeach; ?>

        </tr>

    </thead>

    <tbody>

        <tr>

        <?php $daysStarted = false; $day = 1; ?>

        <?php for($i = 1; $i <= $this->daysInCurrentMonth+$this->firstDayOfTheWeek; $i++): ?>

            <?php if(!$daysStarted) $daysStarted = ($i == $this->firstDayOfTheWeek+1); ?>

            <td <?php if($day == $this->day) echo 'class="calendar-selected-day"';?>>

                <?php if($daysStarted && $day <= $this->daysInCurrentMonth): ?>

                <?php if (strlen($this->month) == 1) $mes="0$this->month"; ?>

                <?php if (strlen($day) == 1) $dia="0$day"; ?>

                <?php $fecha = "$this->year-$mes-$dia";?>

                    <?php echo CHtml::link($day, $this->getDayLink($day)); $day++; $dia++; ?>

                    <?php if (isset($this->cita_array[$fecha])){

                        while (list ($key, $value) = each($this->cita_array)){

                            echo '<div>--</div>';

                        }

                    }

                        else{

                            echo '<div>&nbsp;</div>';

                        }

                    ?>

                <?php endif; ?>

            </td>

            <?php if($i % 7 == 0): ?>

                </tr><tr>

            <?php endif; ?>

        <?php endfor; ?>

        </tr>

    </tbody>

    <?php var_dump ($this->cita_array); ?>

</table>



Por favor, una ayudita.

Muchas gracias.

Aquí dejo el código que ya funciona. Por si a alguien le viene bien.

SimpleCalendarWidget.php modificado:




    protected function getCitas(){

        $cita=null;

        $cita=Yii::app()->db->createCommand("select cita_fecha, cita_id from cita group by cita_fecha")

            ->queryAll();

        $this->citas=CHtml::listData($cita,'cita_fecha','cita_id');

    }



simple-calendar.php modificado:




<table id="calendar">

    <thead>

        <tr class="month-year-row">

            <th class="previous-month"><?php echo CHtml::link('<<', $this->previousLink); ?></th>

            <th colspan="5"><?php echo $this->monthName.', '.$this->year; ?></th>

            <th class="next-month"><?php echo CHtml::link('>>', $this->nextLink); ?></th>

        </tr>

        <tr class="weekdays-row">

            <?php foreach(Yii::app()->locale->getWeekDayNames('narrow') as $weekDay): ?>

                <th><?php echo $weekDay; ?></th>

            <?php endforeach; ?>

        </tr>

    </thead>

    <tbody>

        <tr>

        <?php $daysStarted = false; $day = 1; $mes = null; $dia = null?>

        <?php for($i = 1; $i <= $this->daysInCurrentMonth+$this->firstDayOfTheWeek; $i++): ?>

            <?php if(!$daysStarted) $daysStarted = ($i == $this->firstDayOfTheWeek+1); ?>

            <td <?php if($day == $this->day) echo 'class="calendar-selected-day"';?>

                <?php if($daysStarted && $day <= $this->daysInCurrentMonth): ?>

                <?php if (strlen("$this->month") == 1)

                                {$mes="0$this->month";} else $mes=$this->month; ?>

                <?php if (strlen($day) == 1) $dia="0$day"; ?>

                <?php $fecha = "$this->year-$mes-$dia";?>

                    <?php if (isset($this->citas)){

                        foreach ($this->citas as $cita=>$clave){

                            if ($cita == $fecha){echo 'bgcolor="#A3E3E3"';}

                        }

                    }?>>

                    <?php echo CHtml::link($day, $this->getDayLink($day)); $day++; $dia++; ?>

                <?php endif; ?>

            </td>

            <?php if($i % 7 == 0): ?>

                </tr><tr>

            <?php endif; ?>

        <?php endfor; ?>

        </tr>

    </tbody>

</table>



Saludos

Gracias por el aporte, con mi colega tenemos que hacer una aplicación que incluye agenda de citas, y esto podría sernos útil.

Gracias a ti, Mauricio. Ya ves que soy nuevo en esto y ando mas perdido que un pulpo en un garaje.

Había empezado a pensar que estaba solo o con una enfermedad contagiosa.

Saludos desde España.

Usando el código de ejemplo me encontré con este error:

Property "SimpleCalendarWidget.citas" is read only

¿alguien sabe a que se debe? Please.

Ya encontré el error; Faltaba declarar la variable al principio.

gracias por el aporte amigo.