[1053] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
| 5 | // | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
| 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[1053] | 23 | |
---|
| 24 | /** |
---|
| 25 | * Base class for monthly and weekly calendar styles |
---|
| 26 | */ |
---|
| 27 | class CalendarBase |
---|
| 28 | { |
---|
| 29 | // db column on which this calendar works |
---|
| 30 | var $date_field; |
---|
| 31 | // used for queries (INNER JOIN or normal) |
---|
| 32 | var $inner_sql; |
---|
[1059] | 33 | // |
---|
| 34 | var $calendar_levels; |
---|
[1053] | 35 | |
---|
| 36 | /** |
---|
| 37 | * Initialize the calendar |
---|
| 38 | * @param string inner_sql used for queries (INNER JOIN or normal) |
---|
| 39 | */ |
---|
[1086] | 40 | function initialize($inner_sql) |
---|
[1053] | 41 | { |
---|
[1086] | 42 | global $page; |
---|
[1090] | 43 | if ($page['chronology_field']=='posted') |
---|
[1086] | 44 | { |
---|
| 45 | $this->date_field = 'date_available'; |
---|
| 46 | } |
---|
| 47 | else |
---|
| 48 | { |
---|
| 49 | $this->date_field = 'date_creation'; |
---|
| 50 | } |
---|
[1053] | 51 | $this->inner_sql = $inner_sql; |
---|
| 52 | } |
---|
| 53 | |
---|
[1059] | 54 | function get_display_name() |
---|
| 55 | { |
---|
[1086] | 56 | global $conf, $page; |
---|
[1059] | 57 | $res = ''; |
---|
| 58 | |
---|
[1086] | 59 | for ($i=0; $i<count($page['chronology_date']); $i++) |
---|
[1059] | 60 | { |
---|
| 61 | $res .= $conf['level_separator']; |
---|
[1086] | 62 | if ( isset($page['chronology_date'][$i+1]) ) |
---|
[1069] | 63 | { |
---|
[1086] | 64 | $chronology_date = array_slice($page['chronology_date'],0, $i+1); |
---|
| 65 | $url = duplicate_index_url( |
---|
| 66 | array( 'chronology_date'=>$chronology_date ), |
---|
| 67 | array( 'start' ) |
---|
| 68 | ); |
---|
[1059] | 69 | $res .= |
---|
| 70 | '<a href="'.$url.'">' |
---|
[1086] | 71 | .$this->get_date_component_label($i, $page['chronology_date'][$i]) |
---|
[1059] | 72 | .'</a>'; |
---|
| 73 | } |
---|
| 74 | else |
---|
| 75 | { |
---|
| 76 | $res .= |
---|
| 77 | '<span class="calInHere">' |
---|
[1086] | 78 | .$this->get_date_component_label($i, $page['chronology_date'][$i]) |
---|
[1059] | 79 | .'</span>'; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | return $res; |
---|
| 83 | } |
---|
| 84 | |
---|
[1053] | 85 | //--------------------------------------------------------- private members --- |
---|
| 86 | /** |
---|
[1057] | 87 | * Returns a display name for a date component optionally using labels |
---|
| 88 | */ |
---|
[1059] | 89 | function get_date_component_label($level, $date_component) |
---|
[1057] | 90 | { |
---|
| 91 | $label = $date_component; |
---|
[1059] | 92 | if (isset($this->calendar_levels[$level]['labels'][$date_component])) |
---|
[1057] | 93 | { |
---|
[1059] | 94 | $label = $this->calendar_levels[$level]['labels'][$date_component]; |
---|
[1057] | 95 | } |
---|
[1069] | 96 | elseif ('any' === $date_component ) |
---|
[1057] | 97 | { |
---|
| 98 | $label = l10n('calendar_any'); |
---|
| 99 | } |
---|
| 100 | return $label; |
---|
| 101 | } |
---|
[1059] | 102 | |
---|
[1057] | 103 | /** |
---|
[1062] | 104 | * Gets a nice display name for a date to be shown in previos/next links. |
---|
| 105 | */ |
---|
| 106 | function get_date_nice_name($date) |
---|
| 107 | { |
---|
| 108 | $date_components = explode('-', $date); |
---|
| 109 | $res = ''; |
---|
| 110 | for ($i=count($date_components)-1; $i>=0; $i--) |
---|
| 111 | { |
---|
[1069] | 112 | if ('any' !== $date_components[$i]) |
---|
[1062] | 113 | { |
---|
[1069] | 114 | $label = $this->get_date_component_label($i, $date_components[$i] ); |
---|
| 115 | if ( $res!='' ) |
---|
[1062] | 116 | { |
---|
[1069] | 117 | $res .= ' '; |
---|
[1062] | 118 | } |
---|
[1069] | 119 | $res .= $label; |
---|
[1062] | 120 | } |
---|
| 121 | } |
---|
| 122 | return $res; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
[1053] | 126 | * Creates a calendar navigation bar. |
---|
| 127 | * |
---|
[1086] | 128 | * @param array date_components |
---|
[1053] | 129 | * @param array items - hash of items to put in the bar (e.g. 2005,2006) |
---|
| 130 | * @param string class_prefix - html class attribute prefix for span elements |
---|
[1059] | 131 | * @param bool show_any - adds any link to the end of the bar |
---|
| 132 | * @param bool show_empty - shows all labels even those without items |
---|
[1053] | 133 | * @param array labels - optional labels for items (e.g. Jan,Feb,...) |
---|
| 134 | * @return string the navigation bar |
---|
| 135 | */ |
---|
[2101] | 136 | function get_nav_bar_from_items($date_components, $items, |
---|
[1059] | 137 | $class_prefix, $show_any, |
---|
| 138 | $show_empty=false, $labels=null) |
---|
[1053] | 139 | { |
---|
[1086] | 140 | global $conf, $page; |
---|
[1059] | 141 | |
---|
[1053] | 142 | $nav_bar = ''; |
---|
[1059] | 143 | |
---|
| 144 | if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) ) |
---|
| 145 | { |
---|
| 146 | foreach ($labels as $item => $label) |
---|
| 147 | { |
---|
| 148 | if ( ! isset($items[$item]) ) |
---|
| 149 | { |
---|
| 150 | $items[$item] = -1; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | ksort($items); |
---|
| 154 | } |
---|
| 155 | |
---|
[1053] | 156 | foreach ($items as $item => $nb_images) |
---|
| 157 | { |
---|
| 158 | $label = $item; |
---|
| 159 | if (isset($labels[$item])) |
---|
| 160 | { |
---|
| 161 | $label = $labels[$item]; |
---|
| 162 | } |
---|
[2101] | 163 | if ($nb_images==-1) |
---|
[1053] | 164 | { |
---|
[1059] | 165 | $nav_bar .= '<span class="'.$class_prefix.'Empty">'; |
---|
| 166 | $nav_bar .= $label; |
---|
| 167 | } |
---|
[1053] | 168 | else |
---|
| 169 | { |
---|
| 170 | $nav_bar .= '<span class="'.$class_prefix.'">'; |
---|
[1086] | 171 | $url = duplicate_index_url( |
---|
[1107] | 172 | array('chronology_date'=>array_merge($date_components,array($item))), |
---|
[1086] | 173 | array( 'start' ) |
---|
| 174 | ); |
---|
[1053] | 175 | $nav_bar .= '<a href="'.$url.'">'; |
---|
| 176 | $nav_bar .= $label; |
---|
| 177 | $nav_bar .= '</a>'; |
---|
| 178 | } |
---|
| 179 | if ($nb_images > 0) |
---|
| 180 | { |
---|
| 181 | $nav_bar .= '('.$nb_images.')'; |
---|
| 182 | } |
---|
| 183 | $nav_bar.= '</span>'; |
---|
| 184 | } |
---|
[1059] | 185 | |
---|
[1163] | 186 | if ($conf['calendar_show_any'] and $show_any and count($items)>1 and |
---|
| 187 | count($date_components)<count($this->calendar_levels)-1 ) |
---|
[1053] | 188 | { |
---|
| 189 | $label = l10n('calendar_any'); |
---|
[2101] | 190 | $nav_bar .= '<span class="'.$class_prefix.'">'; |
---|
| 191 | $url = duplicate_index_url( |
---|
| 192 | array('chronology_date'=>array_merge($date_components,array('any'))), |
---|
| 193 | array( 'start' ) |
---|
| 194 | ); |
---|
| 195 | $nav_bar .= '<a href="'.$url.'">'; |
---|
| 196 | $nav_bar .= $label; |
---|
| 197 | $nav_bar .= '</a>'; |
---|
[1053] | 198 | $nav_bar.= '</span>'; |
---|
| 199 | } |
---|
| 200 | return $nav_bar; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Creates a calendar navigation bar for a given level. |
---|
| 205 | * |
---|
[1059] | 206 | * @param int level - the level (0-year,1-month/week,2-day) |
---|
[1053] | 207 | * @return void |
---|
| 208 | */ |
---|
[1059] | 209 | function build_nav_bar($level, $labels=null) |
---|
[1053] | 210 | { |
---|
[1086] | 211 | global $template, $conf, $page; |
---|
[1059] | 212 | |
---|
[1053] | 213 | $query = ' |
---|
[1059] | 214 | SELECT DISTINCT('.$this->calendar_levels[$level]['sql'] |
---|
[1053] | 215 | .') as period'; |
---|
| 216 | $query.= $this->inner_sql; |
---|
[1057] | 217 | $query.= $this->get_date_where($level); |
---|
[1053] | 218 | $query.= ' |
---|
| 219 | GROUP BY period |
---|
| 220 | ;'; |
---|
| 221 | |
---|
| 222 | $level_items = array(); |
---|
| 223 | $result = pwg_query($query); |
---|
| 224 | while ($row = mysql_fetch_array($result)) |
---|
| 225 | { |
---|
| 226 | $level_items[$row['period']] = 0; |
---|
| 227 | } |
---|
| 228 | |
---|
[1062] | 229 | if ( count($level_items)==1 and |
---|
[1086] | 230 | count($page['chronology_date'])<count($this->calendar_levels)-1) |
---|
[1059] | 231 | { |
---|
[1086] | 232 | if ( ! isset($page['chronology_date'][$level]) ) |
---|
[1059] | 233 | { |
---|
| 234 | list($key) = array_keys($level_items); |
---|
[1086] | 235 | $page['chronology_date'][$level] = (int)$key; |
---|
[1059] | 236 | |
---|
[1086] | 237 | if ( $level<count($page['chronology_date']) and |
---|
[1062] | 238 | $level!=count($this->calendar_levels)-1 ) |
---|
| 239 | { |
---|
| 240 | return; |
---|
| 241 | } |
---|
[1059] | 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
[1092] | 245 | $dates = $page['chronology_date']; |
---|
| 246 | while ($level<count($dates)) |
---|
| 247 | { |
---|
| 248 | array_pop($dates); |
---|
| 249 | } |
---|
| 250 | |
---|
[1053] | 251 | $nav_bar = $this->get_nav_bar_from_items( |
---|
[1092] | 252 | $dates, |
---|
[1053] | 253 | $level_items, |
---|
[1059] | 254 | 'calItem', |
---|
[1053] | 255 | true, |
---|
[1059] | 256 | true, |
---|
| 257 | isset($labels) ? $labels : $this->calendar_levels[$level]['labels'] |
---|
[1053] | 258 | ); |
---|
| 259 | |
---|
[2231] | 260 | $template->append( |
---|
| 261 | 'chronology_navigation_bars', |
---|
[1053] | 262 | array( |
---|
[2231] | 263 | 'CONTENT' => $nav_bar, |
---|
[1053] | 264 | ) |
---|
| 265 | ); |
---|
| 266 | } |
---|
[1062] | 267 | |
---|
| 268 | /** |
---|
| 269 | * Assigns the next/previous link to the template with regards to |
---|
| 270 | * the currently choosen date. |
---|
| 271 | */ |
---|
| 272 | function build_next_prev() |
---|
| 273 | { |
---|
[1086] | 274 | global $template, $page; |
---|
[1062] | 275 | $prev = $next =null; |
---|
[1086] | 276 | if ( empty($page['chronology_date']) ) |
---|
[1062] | 277 | return; |
---|
| 278 | $query = 'SELECT CONCAT_WS("-"'; |
---|
[1086] | 279 | for ($i=0; $i<count($page['chronology_date']); $i++) |
---|
[1062] | 280 | { |
---|
[1163] | 281 | if ( 'any' === $page['chronology_date'][$i] ) |
---|
[1062] | 282 | { |
---|
[1069] | 283 | $query .= ','.'"any"'; |
---|
[1062] | 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
[1069] | 287 | $query .= ','.$this->calendar_levels[$i]['sql']; |
---|
[1062] | 288 | } |
---|
| 289 | } |
---|
[1086] | 290 | $current = implode('-', $page['chronology_date'] ); |
---|
[1062] | 291 | |
---|
| 292 | $query.=') as period' . $this->inner_sql .' |
---|
| 293 | AND ' . $this->date_field . ' IS NOT NULL |
---|
| 294 | GROUP BY period'; |
---|
[1069] | 295 | |
---|
[1062] | 296 | $upper_items = array_from_query( $query, 'period'); |
---|
[1069] | 297 | |
---|
[1062] | 298 | usort($upper_items, 'version_compare'); |
---|
| 299 | $upper_items_rank = array_flip($upper_items); |
---|
[1069] | 300 | if ( !isset($upper_items_rank[$current]) ) |
---|
| 301 | { |
---|
| 302 | array_push($upper_items, $current);// just in case (external link) |
---|
| 303 | usort($upper_items, 'version_compare'); |
---|
| 304 | $upper_items_rank = array_flip($upper_items); |
---|
| 305 | } |
---|
[1062] | 306 | $current_rank = $upper_items_rank[$current]; |
---|
[1069] | 307 | |
---|
[2231] | 308 | $tpl_var = array(); |
---|
| 309 | |
---|
[1062] | 310 | if ( $current_rank>0 ) |
---|
| 311 | { // has previous |
---|
| 312 | $prev = $upper_items[$current_rank-1]; |
---|
[1086] | 313 | $chronology_date = explode('-', $prev); |
---|
[2231] | 314 | $tpl_var['previous'] = |
---|
[1062] | 315 | array( |
---|
| 316 | 'LABEL' => $this->get_date_nice_name($prev), |
---|
[1086] | 317 | 'URL' => duplicate_index_url( |
---|
| 318 | array('chronology_date'=>$chronology_date), array('start') |
---|
| 319 | ) |
---|
[1062] | 320 | ); |
---|
| 321 | } |
---|
[2231] | 322 | |
---|
[1062] | 323 | if ( $current_rank < count($upper_items)-1 ) |
---|
[1086] | 324 | { // has next |
---|
[1062] | 325 | $next = $upper_items[$current_rank+1]; |
---|
[1086] | 326 | $chronology_date = explode('-', $next); |
---|
[2231] | 327 | $tpl_var['next'] = |
---|
[1062] | 328 | array( |
---|
| 329 | 'LABEL' => $this->get_date_nice_name($next), |
---|
[1086] | 330 | 'URL' => duplicate_index_url( |
---|
| 331 | array('chronology_date'=>$chronology_date), array('start') |
---|
| 332 | ) |
---|
[1062] | 333 | ); |
---|
| 334 | } |
---|
[2231] | 335 | |
---|
| 336 | if ( !empty($tpl_var) ) |
---|
| 337 | { |
---|
| 338 | $existing = & $template->get_template_vars('chronology_navigation_bars'); |
---|
| 339 | if ( !empty($existing) ) |
---|
| 340 | { |
---|
| 341 | $existing[ sizeof($existing)-1 ] = |
---|
| 342 | array_merge( $existing[ sizeof($existing)-1 ], $tpl_var); |
---|
| 343 | } |
---|
| 344 | else |
---|
| 345 | { |
---|
| 346 | $template->append( 'chronology_navigation_bars', $tpl_var ); |
---|
| 347 | } |
---|
| 348 | } |
---|
[1062] | 349 | } |
---|
[1053] | 350 | } |
---|
[1903] | 351 | ?> |
---|