[1053] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | |
---|
[1086] | 24 | define('CAL_VIEW_LIST', 'list'); |
---|
| 25 | define('CAL_VIEW_CALENDAR', 'calendar'); |
---|
[1053] | 26 | |
---|
| 27 | function initialize_calendar() |
---|
| 28 | { |
---|
[1677] | 29 | global $page, $conf, $user, $template, $filter; |
---|
[1053] | 30 | |
---|
| 31 | //------------------ initialize the condition on items to take into account --- |
---|
| 32 | $inner_sql = ' FROM ' . IMAGES_TABLE; |
---|
[1059] | 33 | |
---|
[1861] | 34 | if ($page['section']=='categories') |
---|
[1053] | 35 | { // we will regenerate the items by including subcats elements |
---|
| 36 | $page['items'] = array(); |
---|
| 37 | $inner_sql .= ' |
---|
| 38 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id'; |
---|
[1059] | 39 | |
---|
[1861] | 40 | if ( isset($page['category']) ) |
---|
[1053] | 41 | { |
---|
| 42 | $sub_ids = array_diff( |
---|
[1861] | 43 | get_subcat_ids(array($page['category']['id'])), |
---|
[1053] | 44 | explode(',', $user['forbidden_categories']) |
---|
| 45 | ); |
---|
[1059] | 46 | |
---|
[1053] | 47 | if (empty($sub_ids)) |
---|
| 48 | { |
---|
| 49 | return; // nothing to do |
---|
| 50 | } |
---|
| 51 | $inner_sql .= ' |
---|
| 52 | WHERE category_id IN ('.implode(',',$sub_ids).')'; |
---|
[1759] | 53 | $inner_sql .= ' |
---|
| 54 | '.get_sql_condition_FandF |
---|
| 55 | ( |
---|
| 56 | array |
---|
| 57 | ( |
---|
| 58 | 'visible_images' => 'id' |
---|
| 59 | ), |
---|
| 60 | 'AND', false |
---|
| 61 | ); |
---|
[1053] | 62 | } |
---|
| 63 | else |
---|
| 64 | { |
---|
| 65 | $inner_sql .= ' |
---|
[1677] | 66 | '.get_sql_condition_FandF |
---|
| 67 | ( |
---|
| 68 | array |
---|
| 69 | ( |
---|
| 70 | 'forbidden_categories' => 'category_id', |
---|
| 71 | 'visible_categories' => 'category_id', |
---|
[1759] | 72 | 'visible_images' => 'id' |
---|
[1677] | 73 | ), |
---|
| 74 | 'WHERE', true |
---|
| 75 | ); |
---|
[1053] | 76 | } |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | if ( empty($page['items']) ) |
---|
| 81 | { |
---|
| 82 | return; // nothing to do |
---|
| 83 | } |
---|
| 84 | $inner_sql .= ' |
---|
| 85 | WHERE id IN (' . implode(',',$page['items']) .')'; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | //-------------------------------------- initialize the calendar parameters --- |
---|
| 89 | pwg_debug('start initialize_calendar'); |
---|
[1057] | 90 | |
---|
| 91 | $fields = array( |
---|
| 92 | // Created |
---|
| 93 | 'created' => array( |
---|
| 94 | 'label' => l10n('Creation date'), |
---|
| 95 | ), |
---|
| 96 | // Posted |
---|
| 97 | 'posted' => array( |
---|
[1059] | 98 | 'label' => l10n('Post date'), |
---|
[1057] | 99 | ), |
---|
| 100 | ); |
---|
| 101 | |
---|
| 102 | $styles = array( |
---|
[1056] | 103 | // Monthly style |
---|
[1057] | 104 | 'monthly' => array( |
---|
[1053] | 105 | 'include' => 'calendar_monthly.class.php', |
---|
| 106 | 'view_calendar' => true, |
---|
| 107 | ), |
---|
[1059] | 108 | // Weekly style |
---|
[1057] | 109 | 'weekly' => array( |
---|
[1053] | 110 | 'include' => 'calendar_weekly.class.php', |
---|
[1056] | 111 | 'view_calendar' => false, |
---|
[1053] | 112 | ), |
---|
| 113 | ); |
---|
| 114 | |
---|
[1086] | 115 | $views = array(CAL_VIEW_LIST,CAL_VIEW_CALENDAR); |
---|
[1057] | 116 | |
---|
| 117 | // Retrieve calendar field |
---|
[2502] | 118 | isset( $fields[ $page['chronology_field'] ] ) or fatal_error('bad chronology field'); |
---|
[1059] | 119 | |
---|
[1057] | 120 | // Retrieve style |
---|
[1090] | 121 | if ( !isset( $styles[ $page['chronology_style'] ] ) ) |
---|
[1086] | 122 | { |
---|
[1090] | 123 | $page['chronology_style'] = 'monthly'; |
---|
[1086] | 124 | } |
---|
[1090] | 125 | $cal_style = $page['chronology_style']; |
---|
[1057] | 126 | include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']); |
---|
| 127 | $calendar = new Calendar(); |
---|
| 128 | |
---|
| 129 | // Retrieve view |
---|
[1086] | 130 | |
---|
[1090] | 131 | if ( !isset($page['chronology_view']) or |
---|
| 132 | !in_array( $page['chronology_view'], $views ) ) |
---|
[1053] | 133 | { |
---|
[1090] | 134 | $page['chronology_view'] = CAL_VIEW_LIST; |
---|
[1053] | 135 | } |
---|
| 136 | |
---|
[1090] | 137 | if ( CAL_VIEW_CALENDAR==$page['chronology_view'] and |
---|
[1086] | 138 | !$styles[$cal_style]['view_calendar'] ) |
---|
| 139 | { |
---|
| 140 | |
---|
[1090] | 141 | $page['chronology_view'] = CAL_VIEW_LIST; |
---|
[1086] | 142 | } |
---|
| 143 | |
---|
[1053] | 144 | // perform a sanity check on $requested |
---|
[1086] | 145 | if (!isset($page['chronology_date'])) |
---|
[1053] | 146 | { |
---|
[1086] | 147 | $page['chronology_date'] = array(); |
---|
[1053] | 148 | } |
---|
[1086] | 149 | while ( count($page['chronology_date']) > 3) |
---|
| 150 | { |
---|
| 151 | array_pop($page['chronology_date']); |
---|
| 152 | } |
---|
[1053] | 153 | |
---|
| 154 | $any_count = 0; |
---|
[1086] | 155 | for ($i = 0; $i < count($page['chronology_date']); $i++) |
---|
[1053] | 156 | { |
---|
[1086] | 157 | if ($page['chronology_date'][$i] == 'any') |
---|
[1053] | 158 | { |
---|
[1090] | 159 | if ($page['chronology_view'] == CAL_VIEW_CALENDAR) |
---|
[1053] | 160 | {// we dont allow any in calendar view |
---|
[1086] | 161 | while ($i < count($page['chronology_date'])) |
---|
[1053] | 162 | { |
---|
[1086] | 163 | array_pop($page['chronology_date']); |
---|
[1053] | 164 | } |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | $any_count++; |
---|
| 168 | } |
---|
[1086] | 169 | elseif ($page['chronology_date'][$i] == '') |
---|
[1053] | 170 | { |
---|
[1086] | 171 | while ($i < count($page['chronology_date'])) |
---|
[1053] | 172 | { |
---|
[1086] | 173 | array_pop($page['chronology_date']); |
---|
[1053] | 174 | } |
---|
| 175 | } |
---|
[1059] | 176 | else |
---|
| 177 | { |
---|
[1086] | 178 | $page['chronology_date'][$i] = (int)$page['chronology_date'][$i]; |
---|
[1059] | 179 | } |
---|
[1053] | 180 | } |
---|
| 181 | if ($any_count == 3) |
---|
| 182 | { |
---|
[1086] | 183 | array_pop($page['chronology_date']); |
---|
[1053] | 184 | } |
---|
[1059] | 185 | |
---|
[1086] | 186 | $calendar->initialize($inner_sql); |
---|
[1053] | 187 | |
---|
[1059] | 188 | //echo ('<pre>'. var_export($calendar, true) . '</pre>'); |
---|
| 189 | |
---|
[1057] | 190 | $must_show_list = true; // true until calendar generates its own display |
---|
[1690] | 191 | if (script_basename() != 'picture') // basename without file extention |
---|
[1053] | 192 | { |
---|
[1086] | 193 | if ($calendar->generate_category_content()) |
---|
[1053] | 194 | { |
---|
[1820] | 195 | $page['items'] = array(); |
---|
[1053] | 196 | $must_show_list = false; |
---|
| 197 | } |
---|
[2502] | 198 | |
---|
[2231] | 199 | $page['comment'] = ''; |
---|
| 200 | $template->assign('FILE_CHRONOLOGY_VIEW', 'month_calendar.tpl'); |
---|
[1059] | 201 | |
---|
[1057] | 202 | foreach ($styles as $style => $style_data) |
---|
[1053] | 203 | { |
---|
[1086] | 204 | foreach ($views as $view) |
---|
[1053] | 205 | { |
---|
[1057] | 206 | if ( $style_data['view_calendar'] or $view != CAL_VIEW_CALENDAR) |
---|
[1053] | 207 | { |
---|
[2231] | 208 | $selected = false; |
---|
[1086] | 209 | |
---|
| 210 | if ($style!=$cal_style) |
---|
[1057] | 211 | { |
---|
[1086] | 212 | $chronology_date = array(); |
---|
| 213 | if ( isset($page['chronology_date'][0]) ) |
---|
[1057] | 214 | { |
---|
[1086] | 215 | array_push($chronology_date, $page['chronology_date'][0]); |
---|
[1057] | 216 | } |
---|
| 217 | } |
---|
| 218 | else |
---|
| 219 | { |
---|
[1086] | 220 | $chronology_date = $page['chronology_date']; |
---|
[1057] | 221 | } |
---|
[1086] | 222 | $url = duplicate_index_url( |
---|
| 223 | array( |
---|
[1090] | 224 | 'chronology_style' => $style, |
---|
| 225 | 'chronology_view' => $view, |
---|
[1086] | 226 | 'chronology_date' => $chronology_date, |
---|
| 227 | ) |
---|
| 228 | ); |
---|
| 229 | |
---|
[1090] | 230 | if ($style==$cal_style and $view==$page['chronology_view'] ) |
---|
[1086] | 231 | { |
---|
[2231] | 232 | $selected = true; |
---|
[1086] | 233 | } |
---|
| 234 | |
---|
[2231] | 235 | $template->append( |
---|
| 236 | 'chronology_views', |
---|
[1057] | 237 | array( |
---|
| 238 | 'VALUE' => $url, |
---|
[1090] | 239 | 'CONTENT' => l10n('chronology_'.$style.'_'.$view), |
---|
[1057] | 240 | 'SELECTED' => $selected, |
---|
| 241 | ) |
---|
| 242 | ); |
---|
[1053] | 243 | } |
---|
| 244 | } |
---|
| 245 | } |
---|
[1086] | 246 | $url = duplicate_index_url( |
---|
[1090] | 247 | array(), array('start', 'chronology_date') |
---|
[1086] | 248 | ); |
---|
| 249 | $calendar_title = '<a href="'.$url.'">' |
---|
[1090] | 250 | .$fields[$page['chronology_field']]['label'].'</a>'; |
---|
[1062] | 251 | $calendar_title.= $calendar->get_display_name(); |
---|
[2231] | 252 | $template->assign('chronology', |
---|
[1820] | 253 | array( |
---|
| 254 | 'TITLE' => $calendar_title |
---|
| 255 | ) |
---|
| 256 | ); |
---|
[1053] | 257 | } // end category calling |
---|
| 258 | |
---|
| 259 | if ($must_show_list) |
---|
| 260 | { |
---|
[6668] | 261 | $query = 'SELECT DISTINCT id '; |
---|
[4398] | 262 | $query .= ','.$calendar->date_field; |
---|
[1062] | 263 | $query .= $calendar->inner_sql.' |
---|
| 264 | '.$calendar->get_date_where(); |
---|
[1053] | 265 | if ( isset($page['super_order_by']) ) |
---|
| 266 | { |
---|
| 267 | $query .= ' |
---|
| 268 | '.$conf['order_by']; |
---|
| 269 | } |
---|
| 270 | else |
---|
| 271 | { |
---|
[1163] | 272 | if ( count($page['chronology_date'])==0 |
---|
| 273 | or in_array('any', $page['chronology_date']) ) |
---|
| 274 | {// selected period is very big so we show newest first |
---|
| 275 | $order = ' DESC, '; |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | {// selected period is small (month,week) so we show oldest first |
---|
| 279 | $order = ' ASC, '; |
---|
| 280 | } |
---|
[1053] | 281 | $order_by = str_replace( |
---|
| 282 | 'ORDER BY ', |
---|
[1163] | 283 | 'ORDER BY '.$calendar->date_field.$order, $conf['order_by'] |
---|
[1053] | 284 | ); |
---|
[1062] | 285 | $query .= ' |
---|
| 286 | '.$order_by; |
---|
[1053] | 287 | } |
---|
[4385] | 288 | $page['items'] = array_from_query($query, 'id'); |
---|
[1053] | 289 | } |
---|
| 290 | pwg_debug('end initialize_calendar'); |
---|
| 291 | } |
---|
[4781] | 292 | |
---|
[4833] | 293 | function array_to_int($array) |
---|
[4781] | 294 | { |
---|
[5283] | 295 | $result = array(); |
---|
[4833] | 296 | foreach ($array as $k => $v) |
---|
| 297 | { |
---|
| 298 | if ((int)$k==$k) |
---|
| 299 | { |
---|
| 300 | $key = (int) $k; |
---|
| 301 | } |
---|
| 302 | else |
---|
| 303 | { |
---|
| 304 | $key = $k; |
---|
| 305 | } |
---|
| 306 | if (is_array($v)) |
---|
| 307 | { |
---|
| 308 | $result[$key] = array_to_int($v); |
---|
| 309 | } |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | if (is_int($v)) |
---|
| 313 | { |
---|
| 314 | $value = (int) $v; |
---|
| 315 | } |
---|
| 316 | else |
---|
| 317 | { |
---|
| 318 | $value = $v; |
---|
| 319 | } |
---|
| 320 | $result[$key] = $value; |
---|
| 321 | } |
---|
[4781] | 322 | } |
---|
[4833] | 323 | return $result; |
---|
[4781] | 324 | } |
---|
[1047] | 325 | ?> |
---|