Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
493 ddelon 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
//
4
// +----------------------------------------------------------------------+
5
// | PHP                                                                  |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 1997-2002 The PHP Group                                |
8
// +----------------------------------------------------------------------+
9
// | This source file is subject to version 2.02 of the PHP license,      |
10
// | that is bundled with this package in the file LICENSE, and is        |
11
// | available at through the world-wide-web at                           |
12
// | http://www.php.net/license/3_0.txt.                                  |
13
// | If you did not receive a copy of the PHP license and are unable to   |
14
// | obtain it through the world-wide-web, please send a note to          |
15
// | license@php.net so we can mail you a copy immediately.               |
16
// +----------------------------------------------------------------------+
17
// | Authors: Harry Fuecks <hfuecks@phppatterns.com>                      |
18
// |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
19
// +----------------------------------------------------------------------+
20
//
21
// $Id: Uri.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
22
//
23
/**
24
 * @package Calendar
25
 * @version $Id: Uri.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
26
 */
27
 
28
/**
29
 * Utility to help building HTML links for navigating the calendar<br />
30
 * <code>
31
 * $Day = new Calendar_Day(2003, 10, 23);
32
 * $Uri = & new Calendar_Util_Uri('year', 'month', 'day');
33
 * echo $Uri->prev($Day,'month'); // Displays year=2003&amp;month=10
34
 * echo $Uri->prev($Day,'day'); // Displays year=2003&amp;month=10&amp;day=22
35
 * $Uri->seperator = '/';
36
 * $Uri->scalar = true;
37
 * echo $Uri->prev($Day,'month'); // Displays 2003/10
38
 * echo $Uri->prev($Day,'day'); // Displays 2003/10/22
39
 * </code>
40
 * @package Calendar
41
 * @access public
42
 */
43
class Calendar_Util_Uri
44
{
45
    /**
46
     * Uri fragments for year, month, day etc.
47
     * @var array
48
     * @access private
49
     */
50
    var $uris = array();
51
 
52
    /**
53
     * String to separate fragments with.
54
     * Set to just & for HTML.
55
     * For a scalar URL you might use / as the seperator
56
     * @var string (default XHTML &amp;)
57
     * @access public
58
     */
59
    var $separator = '&amp;';
60
 
61
    /**
62
     * To output a "scalar" string - variable names omitted.
63
     * Used for urls like index.php/2004/8/12
64
     * @var boolean (default false)
65
     * @access public
66
     */
67
    var $scalar = false;
68
 
69
    /**
70
     * Constructs Calendar_Decorator_Uri
71
     * The term "fragment" means <i>name</i> of a calendar GET variables in the URL
72
     * @param string URI fragment for year
73
     * @param string (optional) URI fragment for month
74
     * @param string (optional) URI fragment for day
75
     * @param string (optional) URI fragment for hour
76
     * @param string (optional) URI fragment for minute
77
     * @param string (optional) URI fragment for second
78
     * @access public
79
     */
80
    function Calendar_Util_Uri($y, $m=null, $d=null, $h=null, $i=null, $s=null)
81
    {
82
        $this->setFragments($y, $m, $d, $h, $i, $s);
83
    }
84
 
85
    /**
86
     * Sets the URI fragment names
87
     * @param string URI fragment for year
88
     * @param string (optional) URI fragment for month
89
     * @param string (optional) URI fragment for day
90
     * @param string (optional) URI fragment for hour
91
     * @param string (optional) URI fragment for minute
92
     * @param string (optional) URI fragment for second
93
     * @return void
94
     * @access public
95
     */
96
    function setFragments($y, $m=null, $d=null, $h=null, $i=null, $s=null) {
97
        if (!is_null($y)) $this->uris['Year']   = $y;
98
        if (!is_null($m)) $this->uris['Month']  = $m;
99
        if (!is_null($d)) $this->uris['Day']    = $d;
100
        if (!is_null($h)) $this->uris['Hour']   = $h;
101
        if (!is_null($i)) $this->uris['Minute'] = $i;
102
        if (!is_null($s)) $this->uris['Second'] = $s;
103
    }
104
 
105
    /**
106
     * Gets the URI string for the previous calendar unit
107
     * @param object subclassed from Calendar e.g. Calendar_Month
108
     * @param string calendar unit ( must be year, month, week, day, hour, minute or second)
109
     * @return string
110
     * @access public
111
     */
112
    function prev($Calendar, $unit)
113
    {
114
        $method = 'prev'.$unit;
115
        $stamp  = $Calendar->{$method}('timestamp');
116
        return $this->buildUriString($Calendar, $method, $stamp);
117
    }
118
 
119
    /**
120
     * Gets the URI string for the current calendar unit
121
     * @param object subclassed from Calendar e.g. Calendar_Month
122
     * @param string calendar unit ( must be year, month, week, day, hour, minute or second)
123
     * @return string
124
     * @access public
125
     */
126
    function this($Calendar, $unit)
127
    {
128
       $method = 'this'.$unit;
129
        $stamp  = $Calendar->{$method}('timestamp');
130
        return $this->buildUriString($Calendar, $method, $stamp);
131
    }
132
 
133
    /**
134
     * Gets the URI string for the next calendar unit
135
     * @param object subclassed from Calendar e.g. Calendar_Month
136
     * @param string calendar unit ( must be year, month, week, day, hour, minute or second)
137
     * @return string
138
     * @access public
139
     */
140
    function next($Calendar, $unit)
141
    {
142
        $method = 'next'.$unit;
143
        $stamp  = $Calendar->{$method}('timestamp');
144
        return $this->buildUriString($Calendar, $method, $stamp);
145
    }
146
 
147
    /**
148
     * Build the URI string
149
     * @param string method substring
150
     * @param int timestamp
151
     * @return string build uri string
152
     * @access private
153
     */
154
    function buildUriString($Calendar, $method, $stamp)
155
    {
156
        $uriString = '';
157
        $cE = & $Calendar->getEngine();
158
        $separator = '';
159
        foreach ($this->uris as $unit => $uri) {
160
            $call = 'stampTo'.$unit;
161
            $uriString .= $separator;
162
            if (!$this->scalar) $uriString .= $uri.'=';
163
            $uriString .= $cE->{$call}($stamp);
164
            $separator = $this->separator;
165
        }
166
        return $uriString;
167
    }
168
}
169
?>