aboutsummaryrefslogtreecommitdiffstats
path: root/include/functions_calendar.inc.php
blob: 9260bbbed86c2c4607fe91cbf7859fbe85f572fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch        : BSF (Best So Far)
// | file          : $RCSfile$
// | last update   : $Date: 2006-02-12 16:52:16 -0500 (Sun, 12 Feb 2006) $
// | last modifier : $Author: plg $
// | revision      : $Revision: 1036 $
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

class BaseCalendarLevel
{
  function BaseCalendarLevel($sql, $allow_any=true, $labels=null)
  {
    $this->sql = $sql;
    $this->allow_any = $allow_any;
    $this->labels = $labels;
  }
  
  function sql()
  {
    return $this->sql;
  }
  function sql_equal($item)
  {
    return $this->sql.'='.$item;
  }
  function allow_any()
  {
    return $this->allow_any;
  }
  function get_label($item)
  {
    if ( isset($this->labels[$item]) )
    {
      return $this->labels[$item];
    }
    return $item;
  }

  var $sql;
  var $allow_any;
  var $labels;
}

class YearMonthCalendarLevel extends BaseCalendarLevel
{
  function YearMonthCalendarLevel()
  {
    global $conf;
    parent::BaseCalendarLevel('DATE_FORMAT('.$conf['calendar_datefield'].',"%Y%m")', false);
  }

  function sql_equal($item)
  {
    global $conf;
    $y = (int)($item/100);
    $m = (int)$item%100;
    // There seems to be much difference in performance between these:
    return $conf['calendar_datefield']." BETWEEN '$y-$m-01' AND '$y-$m-31'"; 
/*    return '(YEAR('.$conf['calendar_datefield'].')='.$y.' 
             AND MONTH('.$conf['calendar_datefield'].')='.$m.')';*/
//    return parent::sql_equal($item);
  }

  function get_label($item)
  {
    global $lang;
    if ( preg_match( '/(\d{4})(\d{2})/', $item, $matches) )
    {
      return $lang['month'][(int)$matches[2]].' '.$matches[1];
    }
    return $item;
  }
}

// just to optimize MySql query so that it uses the index
class YearCalendarLevel extends BaseCalendarLevel
{
  function YearCalendarLevel($sql, $allow_any=true)
  {
    parent::BaseCalendarLevel($sql, $allow_any);
  }

  function sql_equal($item)
  {
    global $conf;
    return $conf['calendar_datefield']." BETWEEN '$item-01-01' AND '$item-12-31'"; 
  }
}

/**
 * Parses $param and returns an array of calendar levels
 * @param requested array of requested items for each calendar level
 * @param cal_type is the requested calendar type
 */
function get_calendar_params($param, &$requested, &$cal_type)
{
  global $conf, $lang;
  $requested = explode('-', $param);
  $cal_struct = array();
  if ($requested[0]=='ywd')
  {
    array_push($cal_struct, new YearCalendarLevel( 
        'YEAR('.$conf['calendar_datefield'].')'  ) );
    array_push($cal_struct, new BaseCalendarLevel( 
        'WEEK('.$conf['calendar_datefield'].')+1' ) );
    array_push($cal_struct, new BaseCalendarLevel( 
        'DAYOFWEEK('.$conf['calendar_datefield'].')-1', true, $lang['day'] ) );
    $cal_type=array_shift($requested);
  }
  else if ($requested[0]=='md')
  {
    array_push($cal_struct, new YearMonthCalendarLevel() );
    array_push($cal_struct, new BaseCalendarLevel( 
        'DAY('.$conf['calendar_datefield'].')'  ) );
    $cal_type=array_shift($requested);
  }
  else
  {
    array_push($cal_struct, new YearCalendarLevel( 
        'YEAR('.$conf['calendar_datefield'].')'  ) );
    array_push($cal_struct, new BaseCalendarLevel( 
        'MONTH('.$conf['calendar_datefield'].')', true, $lang['month'] ) );
    array_push($cal_struct, new BaseCalendarLevel( 
        'DAY('.$conf['calendar_datefield'].')'  ) );

    if ($requested[0]=='ymd')
    {
      $cal_type=array_shift($requested);
    }
    else
    {
      $cal_type='';
    }
  }
  
  // perform a sanity check on $requested
  while (count($requested)>count($cal_struct))
  {
    array_pop($requested);
  }
  
  $any_count = 0;
  for ($i=0; $i<count($requested); $i++)
  {
    if ($requested[$i]=='any')
    {
      if (! $cal_struct[$i]->allow_any() )
      {
        while ($i<count($requested))
        {
          array_pop( $requested );
        }
        break;
      }
      $any_count++;
    }
    elseif ( empty($requested[$i]) )
    {
      while ($i<count($requested))
      {
        array_pop( $requested );
      }
    }
  }
  if ($any_count==count($cal_struct))
  {
    array_pop($requested);
  }
  return $cal_struct;
}



function initialize_calendar()
{
  global $page, $conf, $user, $template;

  if ( !isset($page['cat']) or is_numeric($page['cat']) )
  { // we will regenerate the items by including subcats elements
    $page['cat_nb_images']=0;
    $page['items']=array();
    if ( is_numeric($page['cat']) )
    {
      $sub_ids = get_subcat_ids(array($page['cat']));
      $sub_ids = array_diff($sub_ids, 
                            explode(',', $user['forbidden_categories']) );
      if (empty($sub_ids))
      {
        return; // nothing to do
      }
      $category_restriction .= ' IN ('.implode(',',$sub_ids).')';
    }
    else
    {
      $category_restriction = ' NOT IN ('.$user['forbidden_categories'].')';
    }
  }
  else
  {
    if ( empty($page['items']) )
    {
      return; // nothing to do
    }
  }

  pwg_debug('start initialize_calendar');
  
  $cal_struct = get_calendar_params($_GET['calendar'], $requested, $cal_type);

  //echo ('<pre>'. var_export($cal_struct, true) . '</pre>');
  //echo ('<pre>'. var_export($requested, true) . '</pre>');
  
  $category_calling = false;
  if (basename($_SERVER["PHP_SELF"]) == 'category.php')
  {
    $category_calling = true;
  }
  
  if ($category_calling)
  {
    $template->assign_block_vars('calendar', array());

    $url_base = get_query_string_diff(array('start','calendar'));
    $url_base .= empty($url_base) ? '?' : '&';
    $url_base .= 'calendar=';
    $url_base = PHPWG_ROOT_PATH.'category.php'.$url_base;
    
    // Build navigation bar for calendar styles
    $nav_bar = 'Styles: ';
    foreach ( array('ymd','md','ywd') as $type)
    {
      if ( $type==$cal_type or ($cal_type=='' and $type=='ymd') )
      {
        $nav_bar .= $type.' ';
      }
      else
      {
      	$nav_bar .= '<a href="'. $url_base.$type . '">'.$type.'</a> ';
      }
    }
    $template->assign_block_vars( 'calendar.navbar',  
           array( 'BAR' => $nav_bar) 
           );
           
    $url_base .= $cal_type;
    if ($cal_type!='')
    {
      $url_base .= '-';
    }
    
    
    $prev_level_query='
AND '.$conf['calendar_datefield'].' IS NOT NULL ';

    for ($i=0; $i<count($cal_struct); $i++)
    {
      $crt_cal_level = $cal_struct[$i];
      $query = '
SELECT DISTINCT('.$crt_cal_level->sql().') AS period, COUNT(id) as count
FROM '.IMAGES_TABLE;
      if ( isset($category_restriction) )
      {
        $query.= '
INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id 
WHERE category_id' . $category_restriction;
      }
      else
      {
        $query.= '
WHERE id IN (' . implode(',',$page['items']) .')';
      }
      $query.= $prev_level_query;
      $query.= '
GROUP BY period';

      $level_items=array();
      $result = pwg_query($query);
      $total_pics = 0;
      while ($row = mysql_fetch_array($result))
      {
        $level_items[$row['period']] = (int)$row['count'];
        $total_pics += $row['count'];
      }
      //echo ('<pre>'. var_export($level_items, true) . '</pre>');

      if ( $requested[$i] == 'any' and ! $crt_cal_level->allow_any() )
      {
        unset($requested[$i]);
      }
      
      // --- Build the navigation bar
      if ( $crt_cal_level->allow_any() )
      {
        $level_items['any'] = $total_pics;
      }
      $nav_bar='';
      foreach ($level_items as $item => $nb_images)
      {
        $label = $crt_cal_level->get_label($item);
        if ( $item==$requested[$i] )
        {
          $nav_bar .= ' <span class="dateSelected">';
          $nav_bar .= $label;
          $nav_bar.= '</span>';
        }
        else
        {
          $url = $url_base . $item;
          $nav_bar .= '<a href="'.$url.'">';
          $nav_bar .= $label;
          $nav_bar .= '</a>';
        }
        $nav_bar .= ' ';
      }
      $template->assign_block_vars( 'calendar.navbar',  
           array( 'BAR' => $nav_bar) 
           );

      if ( !isset($requested[$i]) )
        break;
      if ($requested[$i]!='any')
      {
        $prev_level_query.= ' AND '.$crt_cal_level->sql_equal($requested[$i]);
      }
      $url_base .= $requested[$i].'-';
    } // end for each calendar level


    if ( $i < count($cal_struct) )
    {
      $template->assign_block_vars('thumbnails', array());
      $template->assign_block_vars('thumbnails.line', array());
      foreach ($level_items as $level_item => $nb_pics)
      {
        if ($level_item=='any')
          continue;
        $query = '
SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
FROM '.IMAGES_TABLE;
        if ( isset($category_restriction) )
        {
          $query.= '
INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id 
WHERE category_id' . $category_restriction;
        }
        else
        {
          $query.= '
WHERE id IN (' . implode(',',$page['items']) .')';
        }
        $query.= $prev_level_query;
        $query.= ' AND '.$crt_cal_level->sql_equal($level_item);
        $query.= '
ORDER BY RAND()
LIMIT 0,1';
        $row = mysql_fetch_array(pwg_query($query));
        
        $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
        $thumbnail_title = $crt_cal_level->get_label($level_item);
        $name = $thumbnail_title .' ('.$nb_pics.')';
        
        $template->assign_block_vars(
          'thumbnails.line.thumbnail',
          array(
            'IMAGE'=>$thumbnail_src,
            'IMAGE_ALT'=>$row['file'],
            'IMAGE_TITLE'=>$thumbnail_title,
            'U_IMG_LINK'=>$url_base.$level_item
           )
         );
        $template->assign_block_vars(
          'thumbnails.line.thumbnail.category_name',
          array(
            'NAME' => $name
            )
          );
      }
      unset( $page['thumbnails_include'] ); // maybe move everything to a new include file ?
      pwg_debug('end initialize_calendar for thumbs');
      return;
    }
  }
  
  if (!$category_calling or $i==count($cal_struct) )
  {
    $query = 'SELECT DISTINCT(id) FROM '.IMAGES_TABLE;
    if ( isset($category_restriction) )
    {
      $query.= '
INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id 
WHERE category_id' . $category_restriction;
    }
    else
    {
      $query.= '
WHERE id IN ('.implode(',',$page['items']).')';
    }
    $query.= '
AND '.$conf['calendar_datefield'].' IS NOT NULL ';

    for ($i=0; $i<count($cal_struct); $i++)
    {
      assert( isset($requested[$i]) ); // otherwise we should not be here
      if ($requested[$i]!='any')
      {
      $query.= '
AND '. $cal_struct[$i]->sql_equal($requested[$i]);
      }
      
    }
    
    $page['items'] = array_from_query($query, 'id');
    $page['cat_nb_images'] = count($page['items']);
    $page['thumbnails_include'] = 'include/category_default.inc.php';
  }
  pwg_debug('end initialize_calendar');
}

?>