[PHP] Kalendar Klasse

Dieses Thema im Forum "Webentwicklung" wurde erstellt von MakenX, 27. März 2007 .

Schlagworte:
  1. 27. März 2007
    Kalendar Klasse

    Nabend, hab hier mal eine kleine Kalendarklasse erstellt.
    Ihr könnt euch die Kalendarinformationen als Array, gut wenn ihr Design und Programmierung trennt, ausgeben lassen oder als HTML Tabelle.
    Die pure direkte Ausgabe als HTML würde schneller gehen wenn ich den Umweg über das Array nicht machen würde, da ich jedoch meine Templateengine das Array auswerten lasse ist diese Lösung flexibler.
    Mein Script beachtet natürlich irgendwelche "Ungenauigkeiten" in unserem Kalendar die von Monat zu Monat angepasst werden müssen.

    Kalendar wird je nach Server zwischen 0.0008 Seks und 0.003 gerendert.

    Folgt bitte den Kommentaren für weitere Informationen. Kritik und Verbesserungen würde ich natürlich auch gerne hören... und ich hoffe dass der Code gut lesbar ist ....

    PHP:
    <? php
        
    //Calendar.php

        /**
         * To get the actual month use the construct of this class without parameters or use the parameters to differentiate between months and years. To get monthinformation as an array use Calendar::getMonth(false), to get the information as an html table use Calendar::getMonth(true).
         *
         * @author MakenX
         * @package Page
         * @version 0.03 / 30.09.2007
         */
        
    class  Calendar  {
            

            
    private  $_iFirstDayTimestamp ;

            

            
    private  $_iMonth ;

            

            
    private  $_iYear ;

            

            
    public function  __construct ( $iMonth  0 $iYear  0 ) {
                
    $this -> _iFirstDayTimestamp  mktime ( 0 0 0 ,   $this -> _setMonth ( $iMonth ),  1 $this -> _setYear ( $iYear ));
            }
    //end - public function __construct($iMonth = false, $iYear = FALSE)

            /**
             * This method returns the Month as an Array. The first dimension of the array is the weeknumber. The second dimension is a running number which describes the calendarfields, for example running order ten is the tenth field of the calendar and can be used to associate the value with its dayname, in this example Wednesday. The array in the last dimension is the daynumber (Key iDay) and if its the actual day (Key bIsToday).
             *
             * @access private
             * @return array
             */
            
    private function  _createMonthArray () {
                
    //basic vars
                
    $aWeeks                  = array(); //basic array
                
    $bIsNotFinished          true ; //trigger for the while recursion
                
    $iStarted                0 ; //startvalue for days
                
    $iActualDay              date ( 'd' ); //actual day to mark today in the table
                
    if (! $iFirstDayRepresenter  date ( 'w' $this -> _iFirstDayTimestamp )) {
                    
    $iFirstDayRepresenter  7 ;
                }
                
    $iMonthDays              date ( 't' $this -> _iFirstDayTimestamp ); //days of the month
                
    $iMonthNumber            date ( 'm' ); //actual month to mark today
                
    $iRound                  1 ; //counter for while recursion
                
    $iWeekNumber             date ( 'W' $this -> _iFirstDayTimestamp ); //first weeknumber of the month
                
    $aWeeks [ $iWeekNumber ]   = array(); //basic array for a week

                
    while ( $bIsNotFinished ) { //start - recursion to create the table - calendar body
                    
    if (( $iStarted ) || ( $iFirstDayRepresenter  ==  $iRound )) {
                        
    $iStarted ++; //increment the daynumber if the counter is relative to the name of the first month day or when the daynumber already exists
                    
    }

                    
    //start - marking of today
                    
    $aWeeks [ $iWeekNumber ][ $iRound ][ 'bIsToday' ] = (( $iActualDay   ==  $iStarted ) && ( $this -> _iMonth  ==  $iMonthNumber )) ?  true  false ;
                    
    //start - differentiate between empty and filled calendar field
                    
    $aWeeks [ $iWeekNumber ][ $iRound ][ 'iDay' ]     = ( $iStarted  && ( $iStarted  <=  $iMonthDays )) ?  $iStarted  false ;

                    if (!(
    $iRound % 7 )) { //start - checking if the week is finished
                        
    $iWeekNumber ++; //increment the weeknumber
                        
    if ( $iStarted  >=  $iMonthDays ) {
                            
    $bIsNotFinished  false ; //trigger to stop the recursion if every row is finished and the daynumber is greater than the number of monthdays
                        
    }
                    }
    //end - checking if the week is finished
                    
    $iRound ++;
                }
    //end - recursion to create the table - calendar body

                
    return  $aWeeks ;
            }
    //end -private function _createMonthArray()

            /**
             * Returns the Array $aMonth as a HTML Table. thead and tbody are used. The table itself has the HTML Id "Calendar" and the field for the actual day is marked with the Id "Today".
             *
             * @access private
             * @param array $aMonth
             * @return string
             */
            
    private function  _createMonthTable (array  $aMonth ) {
                
    //start - tableheader
                
    $sTable  '<table id="Calendar"><thead><tr>' ;
                foreach (array(
    'Mon' 'Tue' 'Wed' 'Thu' 'Fri' 'Sat' 'Sun' ) as  $sDay ) {
                    
    $sTable  .=  "<th> { $sDay } </th>" ;
                }
                
    $sTable  .=  '</tr></thead><tbody>' ;
                
    //end - tableheader

                //start - calendar body
                
    foreach ( $aMonth  as  $aWeeks ) { //foreachloop to extract the month weeks
                    
    $sTable  .=  '<tr>' ;
                    foreach (
    $aWeeks  as  $aDay ) { //foreachloop to extract every weekday
                        
    $sTable  .= ( $aDay [ 'bIsToday' ]) ?  '<td id="Today">'  '<td>' ; //mark today
                        
    $sTable  .= ( $aDay [ 'iDay' ]) ?  $aDay [ 'iDay' ] :  '&nbsp;' ; //differentiate between filled and empty calendar fields
                        
    $sTable  .=  '</td>' ;
                    }
    //end - foreachloop to extract every weekday
                    
    $sTable  .=  '</tr>' ;
                }
    //end - foreachloop to extract the month weeks
                //end - calendar body

                
    return  $sTable  .=  '</tbody></table>' ; //table footer
            
    } //end - private function _createMonthTable(array $aMonth)

            /**
             * Returns the name of the day.
             *
             * @access public
             * @param int $iDay -> which day name should be returned, if its 0 the complete day array will returned
             * @param String $sLanguage
             */
            
    public function  getDayName ( $iDay  0 $sLanguage  'de' ) {
                
    $aDayNames  = array( 'de'  => array( =>  'Montag' ,
                                                 
    =>  'Dienstag' ,
                                                 
    =>  'Mittwoch' ,
                                                 
    =>  'Donnerstag' ,
                                                 
    =>  'Freitag' ,
                                                 
    =>  'Samstag' ,
                                                 
    =>  'Sonntag' ));

                if (
    $iDay ) {
                    
    $sDayName   $aDayNames [ $sLanguage ][ $iDay ];
                    return (empty(
    $sDayName )) ?  $aDayNames [ 'de' ][ $iDay ] :  $sDayName ;
                }
                return 
    $aDayNames [ $sLanguage ];
            }
    //end - public function getDayName($iDay, $sLanguage = 'de')

            /**
             * Returns the month as an Array($bToString = false) or as an HTML Table ($bToString = true).
             *
             * The first dimension of the array is the weeknumber. The second dimension is a running number which describes the calendarfields, for example running order ten is the tenth field of the calendar and can be used to associate  the value with its dayname, in this example Wednesday. The array in the last dimension is the daynumber (Key iDay) and if its the actual day (Key bIsToday). thead and tbody are used in the HTML table. The table itself has the HTML Id "Calendar" and the field for the actual day is marked with the Id "Today".
             *
             * @param bool $bToString -> Array or HTML Table
             * @return mixed
             */
            
    public function  getMonth ( $bToString  false ) {
                
    $aMonth  $this -> _createMonthArray ();

                if (
    $bToString ) {
                    return 
    $this -> _createMonthTable ( $aMonth );
                }

                return 
    $aMonth ;
            }
    //end - public function getMonth($bToString = false)

            /**
             * Returns the complete name of the month relative to the parameter $sLanguage. If the language relative to the parameter does not exist, this method will return the german name.
             *
             * @access public
             * @param String $sLanguage
             * @return String
             */
            
    public function  getMonthName ( $sLanguage  'de' ) {
                
    $aMonthNames  = array( 'de'  => array( 'Dummy' 'Januar' 'Februar' 'M&auml;rz' ,
                                                   
    'April' 'Mai' 'Juni' 'Juli' 'August' 'September' ,
                                                   
    'Oktober' 'November' 'Dezember' ));
    @            
    $sMonth  $aMonthNames [ $sLanguage ][ $this -> _iMonth ];
                return (empty(
    $sMonth )) ?  $aMonthNames [ 'de' ][ $this -> _iMonth ] :  $sMonth ;
            }
    //end - public function getMonthName($sLanguage = 'de')

            /**
             * Returns the year number.
             *
             * @access public
             * @return int
             */
            
    public function  getYear () {
                return 
    $this -> _iYear ;
            }
    //end - public function getYear()

            /**
             * Saves the Month to the class. If the Month is no valid month number this method replaces the parametervalue with (int)date('n').
             *
             * @access private
             * @param int $iMonth
             * @return int
             */
            
    private function  _setMonth ( $iMonth  false ) {
                
    settype ( $iMonth 'integer' );
                return 
    $this -> _iMonth  = (! $iMonth  || ( strlen ( $iMonth ) >  2 ) || ( $iMonth  0 ) || ( $iMonth  12 )) ?  date ( 'n' ) :  $iMonth ;
            }
    //end - private function _setMonth($iMonth = false)

            /**
             * Saves the year to the class. If the year is no valid year number this method replaces the parametervalue with (int)date('n').
             *
             * @access private
             * @param int $iYear
             * @return int
             */
            
    private function  _setYear ( $iYear  false ) {
                
    settype ( $iYear 'integer' );
                return 
    $this -> _iYear  = (! $iYear  || ( strlen ( $iYear ) >=  5 )) ?  date ( 'Y' ) :  date ( 'Y' mktime ( 0 0 0 $this -> _iMonth 1 $iYear ));
            }
    //end - private function _setYear($iYear = false)
        
    } //Ende final class calendar
    ?>

     
  2. 27. März 2007
    AW: Kalendar Klasse

    Gab das für sowas nicht nen Sammelthread? Würde auch eher zu Webtechnik gehören, wenn ich mich nicht irre. Naja, trotzdem danke für die Class. Habs noch nicht genau angesehen, werde das aber am Wochenende mal machen und die class ggf. in mein Script einbauen.
    Hatte da noch son paar Ideen, wozu die Class recht hilfreich sein könnte.
    Werde wohl nach kurzer Testzeit mit der Class dann ein kurzes Feedback dazu posten.
    Jedenfalls schön, dass es User gibt, die ihren Code nicht unbedingt für sich behalten wollen
     
  3. 28. März 2007
    AW: Kalendar Klasse

    Naja hatte mir vorgestellt dass es irgendwie in Showcase passt, denn ich denke, viele Scripte zu Kalendern die im Netz vorhanden sind, sind deutlich anders aufgebaut als dieses meine jetzt, die erfahrung hab ich zumindestens gemacht ....
    Vor nem Jahr hab ich mal nen Kalendar gebraucht, was aber ins Wasser viel da jemand anderes dann mein Arbeitspaket übernahm, aber der dort gescriptete kalendar gefiel mir nicht und ich machte einen eigenen. Aber vor einem Jahr hab ich deutlich anders programmiert und deswegen stand dringend eine Refaktorisierung an.
    Hatte heute 4 Version durchprobiert, einmal ne Version wo ich den Kalendar in drei Blöcke geteilt habe die jeweils mit einer for-schleife erarbeitet wurden, danach war ne forschleife dran die statisch bis 35/42 durchlief und anschließend ne Rekursion mit while Schleife. Bei jedem Test war noch die direkte HTML Ausgabe angedacht. Die whileschleife "renderte" am schnellsten und da ich prinzipiell Design und Programmierung trennen will, wurde aus der HTML Ausgabe dann eine Ausgabe als Array ... und aus kompatibiliätsgründen hab ich die HTML Ausgabe doch noch mit rein genommen ....

    Och Code für mich behalten ist doch mies, Wissensaustausch kann nur gut sein. Wollte auch mal ne Boardserie starten "Wie baue ich ein modernes, objektorientiertes mit Ajax gestützes Forum" ... aber da ich nicht so oft so viel posten kann, muss das leider noch warten ....
     
  4. 29. März 2007
    AW: Kalendar Klasse

    Kannste das teil mal nich irgendwo als bsp hochladen, so das man sich das mal angucken kann was das kann?


    Knusperkeks
     
  5. 29. März 2007
    AW: Kalendar Klasse

    Jo kann ich die Tage machen, da wird dann aber die Array Methode verwendet, welches von Smarty als TemplateEngine ausgewertet wird, damit hier wieder die strikte Trennung von Logik und Ausgabe vollzogen wird ...
     
  6. 31. März 2007
    AW: Kalendar Klasse

    schwerwiegender Fehler wurde behoben, der durch die aktuelle Jahreskonstellation nicht auffiel....
     
  7. 30. September 2007
    AW: Kalendar Klasse

    So ich habe erneut einen Bug entfernt. Und zwar zeigte die Klasse wenn man durch die Monate "zappte" immer die maximale Tagesanzahl des aktuellen Monats an, fiel durch die Konstellation der bisherigen Kontrollen nicht auf, zusätzlich habe ich einige Redundanzen entfernt und den Code weiter refaktoriert und sitze an weiteren Änderungen.
     
  8. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.