eZ Publish  [4.0]
ezdateoperatorcollection.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDateOperatorCollection class
00004 //
00005 // Created on: <07-Feb-2003 09:39:55 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 class eZDateOperatorCollection
00032 {
00033     /*!
00034      */
00035     function eZDateOperatorCollection( $monthName = 'month_overview' )
00036     {
00037         $this->MonthOverviewName = $monthName;
00038         $this->Operators = array( $monthName );
00039     }
00040 
00041     /*!
00042      Returns the operators in this class.
00043     */
00044     function operatorList()
00045     {
00046         return $this->Operators;
00047     }
00048 
00049     /*!
00050      \return true to tell the template engine that the parameter list exists per operator type.
00051     */
00052     function namedParameterPerOperator()
00053     {
00054         return true;
00055     }
00056 
00057     /*!
00058      See eZTemplateOperator::namedParameterList()
00059     */
00060     function namedParameterList()
00061     {
00062         return array( 'month_overview' => array( 'field' => array( 'type' => 'string',
00063                                                                    'required' => true,
00064                                                                    'default' => false ),
00065                                                  'date' => array( 'type' => 'integer',
00066                                                                   'required' => true,
00067                                                                   'default' => false ),
00068                                                  'optional' => array( 'type' => 'array',
00069                                                                       'required' => false,
00070                                                                       'default' => false ) ) );
00071     }
00072 
00073     /*!
00074      \reimp
00075     */
00076     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
00077     {
00078         $locale = eZLocale::instance();
00079         if ( $operatorName == $this->MonthOverviewName )
00080         {
00081             $field = $namedParameters['field'];
00082             $date = $namedParameters['date'];
00083             if ( !$field )
00084                 return $tpl->missingParameter( $operatorName, 'field' );
00085             if ( !$date )
00086                 return $tpl->missingParameter( $operatorName, 'date' );
00087             $optional = $namedParameters['optional'];
00088             $dateInfo = getdate( $date );
00089             if ( is_array( $operatorValue ) )
00090             {
00091                 $month = array();
00092                 $month['year'] = $dateInfo['year'];
00093                 $month['month'] = $locale->longMonthName( $dateInfo['mon'] );
00094                 $weekDays = $locale->weekDays();
00095                 $weekDaysMap = array();
00096                 $i = 0;
00097                 $dayNames = array( 0 => 'sun', 1 => 'mon', 2 => 'tue',
00098                                    3 => 'wed', 4 => 'thu', 5 => 'fri', 6 => 'sat' );
00099                 foreach ( $weekDays as $weekDay )
00100                 {
00101                     $weekDaysMap[$weekDay] = $i;
00102                     $weekDayName = $locale->shortDayName( $weekDay );
00103                     $weekDayIdentifier = $dayNames[$weekDay];
00104                     $month['weekdays'][] = array( 'day' => $weekDayName,
00105                                                   'class' => $weekDayIdentifier );
00106                     ++$i;
00107                 }
00108                 $days = array();
00109                 $lastDay = getdate( mktime( 0, 0, 0, $dateInfo['mon']+1, 0, $dateInfo['year'] ) );
00110                 $lastDay = $lastDay['mday'];
00111                 for ( $day = 1; $day <= $lastDay; ++$day )
00112                 {
00113                     $days[$day] = false;
00114                 }
00115                 foreach ( $operatorValue as $item )
00116                 {
00117                     $value = null;
00118                     if ( is_object( $item ) and
00119                          method_exists( $item, 'hasattribute' ) and
00120                          method_exists( $item, 'attribute' ) )
00121                     {
00122                         if ( $item->hasAttribute( $field ) )
00123                             $value = $item->attribute( $field );
00124                     }
00125                     else if ( is_array( $item ) )
00126                     {
00127                         if ( array_key_exists( $field, $item ) )
00128                             $value = $item[$field];
00129                     }
00130                     if ( $value !== null )
00131                     {
00132                         $info = getdate( $value );
00133                         if ( $info['year'] == $dateInfo['year'] and
00134                              $info['mon'] == $dateInfo['mon'] )
00135                         {
00136                             $days[$info['mday']] = true;
00137                         }
00138                     }
00139                 }
00140                 $currentDay = false;
00141                 if ( isset( $optional['current'] ) and $optional['current'] !== false )
00142                 {
00143                     $info = getdate( $optional['current'] );
00144                     $currentDay = $info['yday'];
00145                 }
00146                 $today = time();
00147                 $todayInfo = getdate( $today );
00148                 $todayClass = false;
00149                 if ( isset( $optional['today_class'] ) )
00150                     $todayClass = $optional['today_class'];
00151                 $dayClass = false;
00152                 if ( isset( $optional['day_class'] ) )
00153                     $dayClass = $optional['day_class'];
00154                 $link = false;
00155                 if ( isset( $optional['link'] ) )
00156                     $link = $optional['link'];
00157                 $yearLinkParameter = false;
00158                 $monthLinkParameter = false;
00159                 $dayLinkParameter = false;
00160                 if ( isset( $optional['year_link'] ) )
00161                     $yearLinkParameter = $optional['year_link'];
00162                 if ( isset( $optional['month_link'] ) )
00163                     $monthLinkParameter = $optional['month_link'];
00164                 if ( isset( $optional['day_link'] ) )
00165                     $dayLinkParameter = $optional['day_link'];
00166                 $weeks = array();
00167                 $lastWeek = 0;
00168                 for ( $day = 1; $day <= $lastDay; ++$day )
00169                 {
00170                     $timestamp = mktime( 0, 0, 0, $dateInfo['mon'], $day, $dateInfo['year'] );
00171                     $info = getdate( $timestamp );
00172                     $weekDay = $weekDaysMap[$info['wday']];
00173 
00174                     /*
00175                      * Attention: date('W') returns the week number according to
00176                      * ISO, which states that the week with the first Thursday
00177                      * in the new year is week 1.
00178                      */
00179                     $week = date( 'W', $timestamp );
00180 
00181                     if ( $weekDay == 0 && $weekDaysMap[0] == 0 )
00182                     {
00183                         ++$week;
00184                     }
00185 
00186                     /*
00187                      * This checks for a year switch within a week. Routine
00188                      * takes care that first days in January might still belong
00189                      * to the last week of the old year (according to ISO week
00190                      * number), thus be part of week 52 or 53.
00191                      */
00192                     if ($week >= 52 || $week == 1)
00193                     {
00194                         // See if it's a new year by comparing the year of the previous week with the
00195                         // current one.
00196                         $timestampPrevWeek = mktime( 0, 0, 0, $dateInfo['mon'], $day-7, $dateInfo['year'] );
00197                         $isNewYear = date('Y', $timestampPrevWeek) < date('Y', $timestamp);
00198                         if ($isNewYear && $week != 1)
00199                         {
00200                             // A new year with the first week having last year's final week number (52 or 53),
00201                             // because the week's Thursday lies in the old year.
00202                             $week = $lastWeek;
00203                         }
00204                         else
00205                         {
00206                             // The last week of December having the week number 1, because
00207                             // the week's Thursday lies in the new year.
00208                             $week = $lastWeek;
00209                         }
00210                         if ($weekDay == 0)
00211                         {
00212                             ++$week;
00213                         }
00214                     }
00215 
00216                     $lastWeek = $week;
00217 
00218                     if ( !isset( $weeks[$week] ) )
00219                     {
00220                         for ( $i = 0; $i < 7; ++$i )
00221                         {
00222                             $weeks[$week][] = false;
00223                         }
00224                     }
00225                     $dayData = array( 'day' => $day,
00226                                       'link' => false,
00227                                       'class' => $dayClass,
00228                                       'highlight' => false );
00229                     if ( $currentDay == $info['yday'] )
00230                     {
00231                         if ( isset( $optional['current_class'] ) )
00232                             $dayData['class'] = $optional['current_class'];
00233                         $dayData['highlight'] = true;
00234                     }
00235                     if ( $dateInfo['year'] == $todayInfo['year'] and
00236                          $dateInfo['mon'] == $todayInfo['mon'] and
00237                          $day == $todayInfo['mday'] )
00238                     {
00239                         if ( $dayData['class'] )
00240                             $dayData['class'] .= '-' . $todayClass;
00241                         else
00242                             $dayData['class'] = $todayClass;
00243                     }
00244                     if ( $days[$day] )
00245                     {
00246                         $dayLink = $link;
00247                         if ( $dayLink )
00248                         {
00249                             $dayLink .= '/(year)/' . $info['year'];
00250                             $dayLink .= '/(month)/' . $info['mon'];
00251                             $dayLink .= '/(day)/' . $info['mday'];
00252                         }
00253                         $dayData['link'] = $dayLink;
00254                     }
00255                     $weeks[$week][$weekDay] = $dayData;
00256                 }
00257 
00258                 $next = false;
00259                 if ( isset( $optional['next'] ) )
00260                     $next = $optional['next'];
00261                 if ( $next )
00262                 {
00263                     $nextTimestamp = mktime( 0, 0, 0, $dateInfo['mon'] + 1, 1, $dateInfo['year'] );
00264                     $nextInfo = getdate( $nextTimestamp );
00265                     $month['next'] = array( 'month' => $locale->longMonthName( $nextInfo['mon'] ),
00266                                             'year' => $nextInfo['year'] );
00267                     $nextLink = $next['link'];
00268                     $nextLink .= '/(year)/' . $nextInfo['year'];
00269                     $nextLink .= '/(month)/' . $nextInfo['mon'];
00270                     $month['next']['link'] = $nextLink;
00271                 }
00272                 else
00273                     $month['next'] = false;
00274 
00275                 $month['current'] = array( 'month' => $locale->longMonthName( $info['mon'] ),
00276                                            'year' => $info['year'] );
00277                 $currentLink = $next['link'];
00278                 $currentLink .= '/(year)/' . $info['year'];
00279                 $currentLink .= '/(month)/' . $info['mon'];
00280                 $month['current']['link'] = $currentLink;
00281 
00282                 $previous = false;
00283                 if ( isset( $optional['previous'] ) )
00284                 {
00285                     $previous = $optional['previous'];
00286                 }
00287                 if ( $previous )
00288                 {
00289                     $previousTimestamp = mktime( 0, 0, 0, $dateInfo['mon'] - 1, 1, $dateInfo['year'] );
00290                     $previousInfo = getdate( $previousTimestamp );
00291                     $month['previous'] = array( 'month' => $locale->longMonthName( $previousInfo['mon'] ),
00292                                                 'year' => $previousInfo['year'] );
00293                     $previousLink = $previous['link'];
00294                     $previousLink .= '/(year)/' . $previousInfo['year'];
00295                     $previousLink .= '/(month)/' . $previousInfo['mon'];
00296                     $month['previous']['link'] = $previousLink;
00297                 }
00298                 else
00299                 {
00300                     $month['previous'] = false;
00301                 }
00302                 $month['weeks'] = $weeks;
00303                 $operatorValue = $month;
00304             }
00305         }
00306     }
00307 
00308     /// \privatesection
00309     public $Operators;
00310 };
00311 
00312 ?>