5b294cb34665733e92e45d9068be52a028e4f307
[platal.git] / classes / xorgplugin.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 /**
23 * XOrg Plugins class
24 *
25 * this class is used for plugins whose behavior depends only from the GET.
26 *
27 * @category XOrgCore
28 * @package XOrgCore
29 * @author Pierre Habouzit <pierre.habouzit@polytechnique.org>
30 * @access public
31 * @since Classe available since 0.9.2
32 */
33 class XOrgPlugin
34 {
35 /** have to override, contents the fields names used to drive the plugin */
36 var $_get_vars = array();
37 /** some polymorphism at low cost, may be used, or not */
38 var $_callback;
39
40 /** constructor.
41 * the constructor override $_get_vars settings by prefixing the names with $prefix
42 */
43 function XOrgPlugin($funcname='', $prefix='')
44 {
45 $this->_callback = $funcname;
46 $this->_prefix = $prefix;
47 foreach ($this->_get_vars as $key=>$val) {
48 $this->_get_vars[$key] = $prefix.$val;
49 }
50 }
51
52 /** transparent access to $_GET, wrt the right $prefix
53 */
54 function get_value($key)
55 {
56 return Get::v($this->_prefix.$key);
57 }
58
59 /** construct an url with the given parameters to drive the plugin.
60 * leave all other GET params alone
61 */
62 function make_url($params)
63 {
64 $get = Array();
65 $args = isset($params) ? $params : Array();
66
67 if (!is_array($args)) {
68 $args = array($this->_get_vars[0]=>$params);
69 }
70
71 foreach ($_GET as $key=>$val) {
72 if ($key == 'n') {
73 continue;
74 }
75 if (in_array($key, $this->_get_vars) && array_key_exists($key, $args)) {
76 continue;
77 }
78 $get[] = urlencode($key) . '=' . urlencode($val);
79 }
80
81 foreach ($this->_get_vars as $key) {
82 if (array_key_exists($key, $args)) {
83 if ($args[$key]) {
84 $get[] = urlencode($key) . '=' . urlencode($args[$key]);
85 }
86 } elseif (Get::has('key')) {
87 $get[] = urlencode($key) . '=' . urlencode(Get::v($key));
88 }
89 }
90
91 return pl_self() . '?' . join('&amp;', $get);
92 }
93
94 /** need to be overriden. */
95 function show ()
96 {
97 return '';
98 }
99 }
100
101 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
102 ?>