source: ccsweb/trunk/ccs_mods/asset/types.php @ 903

Last change on this file since 903 was 903, checked in by mglb1, 7 years ago

Huge refactor of XMLRPC call handling in the web interface

  • Abstract the details of the XMLRPC message and request away into ccs_xmlrpc
  • Provide two different XMLRPC backends (xmlrpc-epi and useful inc's version)
  • Update every call to do_xmlrpc in the code to use the new helper functions and not call enc()

We need unit tests...

  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1<?php
2/* Copyright (C) 2006  The University of Waikato
3 *
4 * This file is part of ccsweb - CRCnet Configuration System Web Interface
5 *
6 * Asset Management Interface - Type Management
7 *
8 * Author:       Matt Brown <matt@crc.net.nz>
9 * Version:      $Id$
10 *
11 * ccsweb is free software; you can redistribute it and/or modify it under the
12 * terms of the GNU General Public License version 2 as published by the Free
13 * Software Foundation.
14 *
15 * ccsweb is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * ccsweb; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 */
24require("lib/ccs_init.php");
25require("asset/asset_common.php");
26
27if (!isset($_REQUEST["type"]) || $_REQUEST["type"]!="subasset") {
28    $type = "asset";
29} else {
30    $type = "subasset";
31}
32
33function display_type_list()
34{
35    global $xmlrpc_conn, $smarty;
36   
37    /* Check asset manager privs */
38    page_view_requires(AUTH_ASSET_MANAGER);
39   
40    /* Retrieve list of asset types */
41    assignAssetTypes();
42   
43    display_page("mods/asset/typelist.tpl");
44
45}
46
47function display_subasset_type_list()
48{
49    global $xmlrpc_conn, $smarty;
50   
51    /* Check asset manager privs */
52    page_view_requires(AUTH_ASSET_MANAGER);
53   
54    assignSubassetTypes();
55
56    display_page("mods/asset/subassettypelist.tpl");
57
58}
59
60if (!isset($_REQUEST["do"]) || $_REQUEST["do"] == "") {
61    if ($type == "subasset") {
62        display_subasset_type_list();
63    } else {
64        display_type_list();
65    }
66}
67
68/********************************************************************
69 * Delete an Asset Type
70 * do=delete
71 * Required Paramters
72 * asset_type_id
73 * confirm
74 * If confirm is not set a page is displayed asking the user to confirm
75 * they wish to delete this asset type.
76 ********************************************************************/
77if ($_REQUEST["do"] == "delete" && $type=="asset") {
78    page_edit_requires(AUTH_ASSET_MANAGER);
79    if ($_REQUEST["confirm"] != "yes") {
80        /* Get the data */
81        $smarty->assign("asset_type_id", $_REQUEST["asset_type_id"]);
82        $smarty->assign("description", 
83            getAssetType($_REQUEST["asset_type_id"]));
84        /* Ask the user to confirm deletion */
85        display_page("mods/asset/assettypedel.tpl");
86    } else {
87        /* Delete Asset Type */
88        $r = do_xmlrpc("delAssetType", array($_REQUEST["asset_type_id"]),
89            $xmlrpc_conn, TRUE, FALSE);
90        if (xmlrcp_has_error($r)) {
91            ccs_notice("Could not delete asset type: " .
92                xmlrpc_error_string($r));
93        } else {
94            ccs_notice("Asset Type successfully deleted!");
95        }
96        /* Display group list */
97        display_type_list();
98    }
99
100/********************************************************************
101 * Add / Edit an Asset Type
102 * do=add|edit
103 * Required Paramters
104 * asset_type_id        If editing, the id of the type
105 * description          The description of the type
106 ********************************************************************/
107} else if (($_REQUEST["do"] == "add" || $_REQUEST["do"] == "edit") &&
108        $type=="asset") {
109    page_edit_requires(AUTH_ASSET_MANAGER);
110    /* Include the javascript for the subtype form */
111    $smarty->assign("scripts", array("assettype.js"));
112    $do = $_REQUEST["do"];
113    if (isset($_REQUEST["process"]) && $_REQUEST["process"]=="yes") {
114        /* Process submitted form */
115        $asset_type_id = $_REQUEST["asset_type_id"];
116        $description = $_REQUEST["description"];
117        $atid = do_xmlrpc("${do}AssetType", array($asset_type_id,$description),
118            $xmlrpc_conn, TRUE, FALSE);
119        if (xmlrpc_has_error($atid)) {
120            ccs_notice("Could not $do asset type: " .
121                xmlrpc_error_string($atid));
122            $action = $do;
123            $atid = $asset_type_id;
124        } else {
125            $action = "edit";
126            ccs_notice("Asset type ${do}ed successfuly$warn.");
127        }
128        $smarty->assign("asset_type_id", $atid);
129        $smarty->assign("description", $description);
130        assignAssetTypeAllowMod($atid);
131        assignSubassetTypes();
132        assignLinkedSubassets($atid);
133    } else {
134        if ($do == "edit") {
135            $smarty->assign("asset_type_id", $_REQUEST["asset_type_id"]);
136            $smarty->assign("description", 
137                getAssetType($_REQUEST["asset_type_id"]));
138            assignAssetTypeAllowMod($_REQUEST["asset_type_id"]);
139            assignSubassetTypes();
140            assignLinkedSubassets($_REQUEST["asset_type_id"]);
141        }
142        $action = $do;
143    }
144    $smarty->assign("action", $action);
145    /* Show the form */
146    display_page("mods/asset/assettypeform.tpl");
147
148/********************************************************************
149 * Delete a Subasset Type
150 * do=delete
151 * Required Paramters
152 * subasset_type_id
153 * confirm
154 * If confirm is not set a page is displayed asking the user to confirm
155 * they wish to delete this subasset type.
156 ********************************************************************/
157 } else if ($_REQUEST["do"] == "delete" && $type=="subasset") {
158    page_edit_requires(AUTH_ASSET_MANAGER);
159    if ($_REQUEST["confirm"] != "yes") {
160        /* Get the data */
161        $smarty->assign("subasset_type_id", $_REQUEST["subasset_type_id"]);
162        $smarty->assign("description", 
163            getSubassetType($_REQUEST["subasset_type_id"]));
164        /* Ask the user to confirm deletion */
165        display_page("mods/asset/subassettypedel.tpl");
166    } else {
167        /* Delete Subasset Type */
168        $r = do_xmlrpc("delSubassetType", 
169            array($_REQUEST["subasset_type_id"]), $xmlrpc_conn, TRUE, FALSE);
170        if (xmlrpc_has_error($r)) {
171            ccs_notice("Could not delete subasset type: " .
172                xmlrpc_error_string($r));
173        } else {
174            ccs_notice("Subasset Type successfully deleted!");
175        }
176        /* Display group list */
177        display_subasset_type_list();
178    }
179
180/********************************************************************
181 * Add / Edit an Subasset Type
182 * do=add|edit
183 * Required Paramters
184 * subasset_type_id     If editing, the id of the type
185 * description          The description of the type
186 ********************************************************************/
187} else if (($_REQUEST["do"] == "add" || $_REQUEST["do"] == "edit") && 
188        $type=="subasset") {
189    page_edit_requires(AUTH_ASSET_MANAGER);
190    /* Include the javascript for the properties form */
191    $smarty->assign("scripts", array("assetprop.js"));
192    $do = $_REQUEST["do"];
193    if (isset($_REQUEST["process"]) && $_REQUEST["process"]=="yes") {
194        /* Process submitted form */
195        $subasset_type_id = $_REQUEST["subasset_type_id"];
196        $description = $_REQUEST["description"];
197        $rv = do_xmlrpc("${do}SubassetType", 
198            array($subasset_type_id,$description), $xmlrpc_conn, TRUE, FALSE);
199        if (xmlrpc_has_error($rv)) {
200            ccs_notice("Could not $do subasset type: " .
201                xmlrpc_error_string($rv));
202            $action = $do;
203            $smarty->assign("subasset_type_id", $subasset_type_id);
204            $smarty->assign("description", $description);
205        } else {
206            $action = "edit";
207            $warn = "";
208            if (strlen($rv["errMsg"])>0) {
209                $warn = "  - " . htmlspecialchars($rv["errMsg"]);
210            } 
211            if ($do == "add") {
212                $subasset_type_id = $rv["subasset_type_id"];
213            }
214            ccs_notice("Subasset type ${do}ed successfuly$warn.");
215            $smarty->assign("subasset_type_id", $subasset_type_id);
216            $smarty->assign("description", $description);
217        }
218        assignSubassetTypeAllowMod($subasset_type_id);
219        assignProperties();
220        assignLinkedProperties($subasset_type_id);
221    } else {
222        if ($do == "edit") {
223            $smarty->assign("subasset_type_id", 
224                $_REQUEST["subasset_type_id"]);
225            $smarty->assign("description", 
226                getSubassetType($_REQUEST["subasset_type_id"]));
227            assignSubassetTypeAllowMod($_REQUEST["subasset_type_id"]);
228            assignProperties();
229            assignLinkedProperties($_REQUEST["subasset_type_id"]);
230        }
231        $action = $do;
232    }
233    $smarty->assign("action", $action);
234    /* Show the form */
235    display_page("mods/asset/subassettypeform.tpl");
236   
237/********************************************************************
238 * Adds a subasset type to an asset type.
239 * This function is called via an XMLHttpRequest object from the
240 * javascript on the page. Hence it returns XML rather than a normal
241 * page
242 * do=addSubassetLink
243 * Required Paramters
244 * asset_type_id    The asset type gaining a subasset type
245 * subasset_type_id The type of subasset type to create
246 * name             A descriptive name for this instance
247 * required         Wether the subasset type is optional or reuqired
248 * callback         Name of javascript function that results should be
249 *                  processed by.
250 ********************************************************************/
251} else if ($_REQUEST["do"] == "addSubassetLink") {
252    page_edit_requires(AUTH_ASSET_MANAGER);
253    $res = "";
254    $p = array($_REQUEST["asset_type_id"], $_REQUEST["subasset_type_id"],
255        $_REQUEST["name"], $_REQUEST["required"]);
256    $rv = do_xmlrpc("addSubassetLink", $p, $xmlrpc_conn, TRUE, FALSE);
257    if (xmlrpc_has_error($rv)) {
258        $res .= "<success>" . xmlrpc_error_code($rv) . "</success>";
259        $res .= "<errMsg>" . xmlrpc_error_string($rv) . "</errMsg>";
260    } else {
261        if ($rv["asset_type_subasset_id"] > 0) {
262            $res .= "<success>0</success>";
263            $res .= "<asset_type_id>" . $_REQUEST["asset_type_id"] . 
264                "</asset_type_id>";
265            $res .= "<subasset_type_id>" . $_REQUEST["subasset_type_id"] . 
266                "</subasset_type_id>";
267            $res .= "<asset_type_subasset_id>" . 
268                $rv["asset_type_subasset_id"] . "</asset_type_subasset_id>";
269            $res .= "<name>" . $_REQUEST["name"] . "</name>";
270            $res .= "<required>" . $_REQUEST["required"] . "</required>";
271        } else {
272            $res .= "<success>1</success><errMsg>" . $rv["errMsg"] . 
273                "</errMsg>";
274        }
275    }
276    /* Return XML result */
277    header("Content-type: text/xml");
278    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" .
279        "<response>\n" .
280        "<method>" . $_REQUEST["callback"] . "</method>\n" .
281        "<result>$res</result>\n" .
282        "</response>\n";
283    exit;
284
285/********************************************************************
286 * Removes the subasset type from an asset type.
287 * This function is called via an XMLHttpRequest object from the
288 * javascript on the page. Hence it returns XML rather than a normal
289 * page
290 * do=removeSubassetLink
291 * Required Paramters
292 * asset_type_subasset_id   The id of the record to remove
293 * required                 Whether the record is required or optional
294 * callback                 Name of javascript function that results
295 *                          should be processed by.
296 ********************************************************************/
297} else if ($_REQUEST["do"] == "removeSubassetLink") {
298    page_edit_requires(AUTH_ASSET_MANAGER);
299    $res = "";
300    $p = array($_REQUEST["asset_type_subasset_id"]);
301    $rv = do_xmlrpc("removeSubassetLink", $p, $xmlrpc_conn, TRUE, FALSE);
302    if (xmlrpc_has_error($rv) {
303        $res .= "<success>" . xmlrpc_error_code($rv) . "</success>";
304        $res .= "<errMsg>" . xmlrpc_error_string($rv) . "</errMsg>";
305    } else {
306        if ($rv["asset_type_subasset_id"] > 0) {
307            $res .= "<success>0</success>";
308            $res .= "<asset_type_subasset_id>" . 
309                $rv["asset_type_subasset_id"] . "</asset_type_subasset_id>";
310            $res .= "<required>" . $_REQUEST["required"] . "</required>";
311        } else {
312            $res .= "<success>1</success><errMsg>" . $rv["errMsg"] . 
313                "</errMsg>";
314        }
315    }
316    /* Return XML result */
317    header("Content-type: text/xml");
318    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" .
319        "<response>\n" .
320        "<method>" . $_REQUEST["callback"] . "</method>\n" .
321        "<result>$res</result>\n" .
322        "</response>\n";
323    exit;
324   
325/********************************************************************
326 * Updates the status of a subasset link.
327 * This function is called via an XMLHttpRequest object from the
328 * javascript on the page. Hence it returns XML rather than a normal
329 * page
330 * do=updateSubassetLink
331 * Required Paramters
332 * asset_type_subasset_id   The id of the record to update
333 * required                 Whether the record is required or optional
334 * callback                 Name of javascript function that results
335 *                          should be processed by.
336 ********************************************************************/
337} else if ($_REQUEST["do"] == "updateSubassetLink") {
338    page_edit_requires(AUTH_ASSET_MANAGER);
339    $res = "";
340    $p = array($_REQUEST["asset_type_subasset_id"], $_REQUEST["required"]);
341    $rv = do_xmlrpc("updateSubassetLink", $p, $xmlrpc_conn, TRUE, FALSE);
342    if (xmlrpc_has_error($rv)) {
343        $res .= "<success>" . xmlrpc_error_code($rv) . "</success>";
344        $res .= "<errMsg>" . xmlrpc_error_string($rv) . "</errMsg>";
345    } else {
346        if ($rv["asset_type_subasset_id"] > 0) {
347            $res .= "<success>0</success>";
348            $res .= "<asset_type_subasset_id>" . 
349                $rv["asset_type_subasset_id"] . "</asset_type_subasset_id>";
350            $res .= "<required>" . $_REQUEST["required"] . "</required>";
351        } else {
352            $res .= "<success>1</success><errMsg>" . $rv["errMsg"] . 
353                "</errMsg>";
354        }
355    }
356    /* Return XML result */
357    header("Content-type: text/xml");
358    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" .
359        "<response>\n" .
360        "<method>" . $_REQUEST["callback"] . "</method>\n" .
361        "<result>$res</result>\n" .
362        "</response>\n";
363    exit;
364
365}
366
367/* If we get here the page has been called incorrectly, display the list */
368ccs_notice("Invalid Action Requested: " . $_REQUEST["do"]);
369display_type_list();
370
371?>
Note: See TracBrowser for help on using the repository browser.