aboutsummaryrefslogtreecommitdiffstats
path: root/include/calendar_base.class.php
blob: f79b4680098ef7b4a9b0d2907bfd4fa905c97de7 (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
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | 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.                                                                  |
// +-----------------------------------------------------------------------+

/**
 * @package functions\calendar
 */


/**
 * Base class for monthly and weekly calendar styles
 */
abstract class CalendarBase
{
  /** db column on which this calendar works */
  var $date_field;
  /** used for queries (INNER JOIN or normal) */
  var $inner_sql;
  /** used to store db fields */
  var $calendar_levels;

  /**
   * Generate navigation bars for category page.
   *
   * @return boolean false indicates that thumbnails where not included
   */
  abstract function generate_category_content();

  /**
   * Returns a sql WHERE subquery for the date field.
   *
   * @param int $max_levels (e.g. 2=only year and month)
   * @return string
   */
  abstract function get_date_where($max_levels);

  /**
   * Initialize the calendar.
   *
   * @param string $inner_sql
   */
  function initialize($inner_sql)
  {
    global $page;
    if ($page['chronology_field']=='posted')
    {
      $this->date_field = 'date_available';
    }
    else
    {
      $this->date_field = 'date_creation';
    }
    $this->inner_sql = $inner_sql;
  }

  /**
   * Returns the calendar title (with HTML).
   *
   * @return string
   */
  function get_display_name()
  {
    global $conf, $page;
    $res = '';

    for ($i=0; $i<count($page['chronology_date']); $i++)
    {
      $res .= $conf['level_separator'];
      if ( isset($page['chronology_date'][$i+1]) )
      {
        $chronology_date = array_slice($page['chronology_date'],0, $i+1);
        $url = duplicate_index_url(
            array( 'chronology_date'=>$chronology_date ),
            array( 'start' )
            );
        $res .=
          '<a href="'.$url.'">'
          .$this->get_date_component_label($i, $page['chronology_date'][$i])
          .'</a>';
      }
      else
      {
        $res .=
          '<span class="calInHere">'
          .$this->get_date_component_label($i, $page['chronology_date'][$i])
          .'</span>';
      }
    }
    return $res;
  }

  /**
   * Returns a display name for a date component optionally using labels.
   *
   * @return string
   */
  protected function get_date_component_label($level, $date_component)
  {
    $label = $date_component;
    if (isset($this->calendar_levels[$level]['labels'][$date_component]))
    {
      $label = $this->calendar_levels[$level]['labels'][$date_component];
    }
    elseif ('any' === $date_component )
    {
      $label = l10n('All');
    }
    return $label;
  }

  /**
   * Gets a nice display name for a date to be shown in previous/next links
   *
   * @param string $date
   * @return string
   */
  protected function get_date_nice_name($date)
  {
    $date_components = explode('-', $date);
    $res = '';
    for ($i=count($date_components)-1; $i>=0; $i--)
    {
      if ('any' !== $date_components[$i])
      {
        $label = $this->get_date_component_label($i, $date_components[$i] );
        if ( $res!='' )
        {
          $res .= ' ';
        }
        $res .= $label;
      }
    }
    return $res;
  }

  /**
   * Creates a calendar navigation bar.
   *
   * @param array $date_components
   * @param array $items - hash of items to put in the bar (e.g. 2005,2006)
   * @param bool $show_any - adds any link to the end of the bar
   * @param bool $show_empty - shows all labels even those without items
   * @param array $labels - optional labels for items (e.g. Jan,Feb,...)
   * @return string
   */
  protected function get_nav_bar_from_items($date_components, $items,
                                  $show_any,
                                  $show_empty=false, $labels=null)
  {
    global $conf, $page, $template;

    $nav_bar_datas=array();

    if ($conf['calendar_show_empty'] and $show_empty and !empty($labels) )
    {
      foreach ($labels as $item => $label)
      {
        if ( ! isset($items[$item]) )
        {
          $items[$item] = -1;
        }
      }
      ksort($items);
    }

    foreach ($items as $item => $nb_images)
    {
      $label = $item;
      if (isset($labels[$item]))
      {
        $label = $labels[$item];
      }
      if ($nb_images==-1)
      {
        $tmp_datas=array(
          'LABEL'=> $label
        );
      }
      else
      {
        $url = duplicate_index_url(
          array('chronology_date'=>array_merge($date_components,array($item))),
          array( 'start' )
            );
        $tmp_datas=array(
          'LABEL'=> $label,
          'URL' => $url
        );
      }
      if ($nb_images > 0)
      {
        $tmp_datas['NB_IMAGES']=$nb_images;
      }
      $nav_bar_datas[]=$tmp_datas;

    }

    if ($conf['calendar_show_any'] and $show_any and count($items)>1 and
          count($date_components)<count($this->calendar_levels)-1 )
    {
      $url = duplicate_index_url(
        array('chronology_date'=>array_merge($date_components,array('any'))),
        array( 'start' )
          );
      $nav_bar_datas[]=array(
        'LABEL' => l10n('All'),
        'URL' => $url
      );
    }

    return $nav_bar_datas;
  }

  /**
   * Creates a calendar navigation bar for a given level.
   *
   * @param int $level - 0-year, 1-month/week, 2-day
   */
  protected function build_nav_bar($level, $labels=null)
  {
    global $template, $conf, $page;

    $query = '
SELECT DISTINCT('.$this->calendar_levels[$level]['sql'].') as period,
  COUNT(DISTINCT id) as nb_images'.
$this->inner_sql.
$this->get_date_where($level).'
  GROUP BY period;';

    $level_items = simple_hash_from_query($query, 'period', 'nb_images');

    if ( count($level_items)==1 and
         count($page['chronology_date'])<count($this->calendar_levels)-1)
    {
      if ( ! isset($page['chronology_date'][$level]) )
      {
        list($key) = array_keys($level_items);
        $page['chronology_date'][$level] = (int)$key;

        if ( $level<count($page['chronology_date']) and
             $level!=count($this->calendar_levels)-1 )
        {
          return;
        }
      }
    }

    $dates = $page['chronology_date'];
    while ($level<count($dates))
    {
      array_pop($dates);
    }

    $nav_bar = $this->get_nav_bar_from_items(
      $dates,
      $level_items,
      true,
      true,
      isset($labels) ? $labels : $this->calendar_levels[$level]['labels']
      );

    $template->append(
      'chronology_navigation_bars',
      array(
        'items' => $nav_bar,
        )
      );
  }

  /**
   * Assigns the next/previous link to the template with regards to
   * the currently choosen date.
   */
  protected function build_next_prev()
  {
    global $template, $page;

    $prev = $next =null;
    if ( empty($page['chronology_date']) )
      return;
    
    $sub_queries = array();
    $nb_elements = count($page['chronology_date']);
    for ($i=0; $i<$nb_elements; $i++)
    {
      if ( 'any' === $page['chronology_date'][$i] )
      {
        $sub_queries[] = '\'any\'';
      }
      else
      {
        $sub_queries[] = pwg_db_cast_to_text($this->calendar_levels[$i]['sql']);
      }
    }
    $query = 'SELECT '.pwg_db_concat_ws($sub_queries, '-').' AS period';
    $query .= $this->inner_sql .'
AND ' . $this->date_field . ' IS NOT NULL
GROUP BY period';
    
    $current = implode('-', $page['chronology_date'] );
    $upper_items = array_from_query( $query, 'period');

    usort($upper_items, 'version_compare');
    $upper_items_rank = array_flip($upper_items);
    if ( !isset($upper_items_rank[$current]) )
    {
      $upper_items[] = $current;// just in case (external link)
      usort($upper_items, 'version_compare');
      $upper_items_rank = array_flip($upper_items);
    }
    $current_rank = $upper_items_rank[$current];

    $tpl_var = array();

    if ( $current_rank>0 )
    { // has previous
      $prev = $upper_items[$current_rank-1];
      $chronology_date = explode('-', $prev);
      $tpl_var['previous'] =
        array(
          'LABEL' => $this->get_date_nice_name($prev),
          'URL' => duplicate_index_url(
                array('chronology_date'=>$chronology_date), array('start')
                )
        );
    }

    if ( $current_rank < count($upper_items)-1 )
    { // has next
      $next = $upper_items[$current_rank+1];
      $chronology_date = explode('-', $next);
      $tpl_var['next'] =
        array(
          'LABEL' => $this->get_date_nice_name($next),
          'URL' => duplicate_index_url(
                array('chronology_date'=>$chronology_date), array('start')
                )
        );
    }

    if ( !empty($tpl_var) )
    {
      $existing = $template->smarty->getVariable('chronology_navigation_bars');
      if (! ($existing instanceof Undefined_Smarty_Variable))
      {
        $existing->value[ sizeof($existing->value)-1 ] =
          array_merge( $existing->value[ sizeof($existing->value)-1 ], $tpl_var);
      }
      else
      {
        $template->append( 'chronology_navigation_bars', $tpl_var );
      }
    }
  }
}

?>