1<?php
2/**
3 * Federated Login for DokuWiki - base command handler class
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @link       http://www.dokuwiki.org/plugin:fedauth
7 * @author     Aoi Karasu <aoikarasu@gmail.com>
8 */
9
10/**
11 * Federated login base command handler class. Handles all requests
12 * from the plugin object  and renders the results of current action.
13 *
14 * @author     Aoi Karasu <aoikarasu@gmail.com>
15 */
16class fa_base {
17
18    /**
19     * Plugin object owning this instance.
20     */
21    var $manager = null;
22
23    /**
24     * Locale data.
25     */
26    var $lang = array();
27
28    /**
29     * Identifier of a command to handle.
30     */
31    var $cmd = '';
32
33    /**
34     * Identifier of currently processed authorization service, if any.
35     */
36    var $provid = '';
37
38    /**
39     * Processing result.
40     */
41    var $success = false;
42
43    /**
44     * Creates the class instance bound to a DokuWiki plugin class instance and an authorization provider.
45     *
46     * @param objref $manager object reference to a plugin
47     * @param string $cmd name of the command to handle
48     * @param string $provid (optional) an authorization provider id
49     */
50    function __construct(&$manager, $cmd, $provid='') {
51        $this->manager =& $manager;
52        $this->lang =& $manager->lang;
53        $this->cmd = $cmd;
54        $this->provid = $provid;
55    }
56
57    /**
58     * Performs an action depending on current command (and function).
59     *
60     * @return array processing status information
61     */
62    function process() {
63        $method = 'process_' . $this->cmd;
64        if (method_exists($this, $method)) {
65            return $this->$method();
66        }
67        $this->success = false;
68        return array('msg' => $this->manager->getLang('unknowncmd').' <b>'.$this->cmd.'</b>', 'code' => -1);
69    }
70
71    /**
72     * Outputs data for AJAX call.
73     *
74     * @return bool true on success
75     */
76    function ajax() {
77        $method = 'handle_ajax_' . $this->cmd;
78        if (method_exists($this, $method)) {
79            return $this->$method();
80        }
81        return false;
82    }
83
84    /**
85     * Outputs the XHTML as the command result.
86     */
87    function html() {
88        $method = 'html_' . $this->cmd;
89        if (method_exists($this, $method)) {
90            $this->$method();
91        }
92    }
93
94    /**
95     * Returns processing error array using localized string.
96     *
97     * @param string $msg localized string id
98     * @param array $params replacements array
99     * @return processing status array
100     * @see fa_base::_status($msg, $code, $params)
101     */
102    function error($msg, $params=null) {
103        return $this->_status($msg, -1, $params);
104    }
105
106    /**
107     * Returns processing success array using localized string.
108     *
109     * @param string $msg localized string id
110     * @param array $params replacements array
111     * @return processing status array
112     * @see fa_base::_status($msg, $code, $params)
113     */
114    function success($msg, $params=null) {
115        return $this->_status($msg, 1, $params);
116    }
117
118    /**
119     * Returns processing warning array using localized string.
120     *
121     * @param string $msg localized string id
122     * @param array $params replacements array
123     * @return processing status array
124     * @see fa_base::_status($msg, $code, $params)
125     */
126    function warn($msg, $params=null) {
127        return $this->_status($msg, 2, $params);
128    }
129
130    /**
131     * Returns processing info array using localized string.
132     *
133     * @param string $msg localized string id
134     * @param array $params replacements array
135     * @return processing status array
136     * @see fa_base::_status($msg, $code, $params)
137     */
138    function info($msg, $params=null) {
139        return $this->_status($msg, 0, $params);
140    }
141
142    /**
143     * Creates processing status array using localized string
144     * with optional replacement of additional parameters.
145     *
146     * @param string $msg localized string id
147     * @param int $code status code: -1 error, 0 info, 1 success, 2 warning
148     * @param array $params replacements array; keys are chunks to replace,
149     *                      values are the replacacements
150     * @return processing status array
151     */
152    function _status($msg, $code, $params) {
153        $msg = $this->manager->getLang($msg);
154        if (is_array($params)) {
155            foreach($params as $key => $val) {
156                $msg = str_replace($key, $val, $msg);
157            }
158        }
159        return array('msg' => $msg, 'code' => $code);
160    }
161
162    /**
163     * Displays Dokuwiki message using message data array.
164     */
165    function msg($data) {
166        msg($data['msg'], $data['code']);
167    }
168
169} /* fa_base */
170
171/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
172