Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / api / common / actionPage.php
blob6760121464572bf6fbaa9ae09d3d70eeb88e7a8d
1 <?php
3 /* Copyright (C) 2012 Winch Gate Property Limited
4 *
5 * This file is part of ryzom_api.
6 * ryzom_api is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * ryzom_api 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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with ryzom_app. If not, see <http://www.gnu.org/licenses/>.
20 class ryActionClass {
21 public $classname;
22 public $instance;
23 public $args;
24 public $requires;
26 function __construct($classname, $instance, $args, $requires) {
27 $this->classname = $classname;
28 $this->instance = $instance;
29 $this->args = $args;
30 $this->requires = $requires;
35 class ryActionPage {
37 private static $classesArgs = array();
38 private static $myClasses = array();
39 private static $aliases = array();
40 private static $messages;
41 private static $haveMessage;
42 protected static $id;
44 public $instanceName;
45 public $myMethods = array();
47 function __construct() {
50 function addMethods($child_class) {
51 if (is_array($child_class)) {
52 foreach ($child_class as $c_class)
53 $this->myMethods = array_merge($this->myMethods, get_class_methods($c_class));
54 } else {
55 $this->myMethods = get_class_methods($child_class);
59 static function addClass($name, $classname, $args=array(), $requires=NULL) {
60 self::$myClasses[$name] = new ryActionClass($classname, NULL, $args, $requires);
63 static function addAlias($aliasname, $name) {
64 self::$aliases[$aliasname] = $name;
67 static function initInstance($listener) {
68 $i = self::$myClasses[$listener];
69 if (!$i->instance) {
70 // requires
71 if ($i->requires) {
72 self::initInstance($i->requires);
74 if ($i->args)
75 $i->instance = new $i->classname($listener, $i->args);
76 else
77 $i->instance = new $i->classname($listener);
78 $i->instance->addMethods($i->classname);
79 $i->instance->instanceName = $listener;
82 return $i->instance;
85 static function getInstance($listener) {
86 return self::initInstance($listener);
89 static function _addMSG($type='OK', $message='') {
90 self::$messages[] = array($type, $message);
91 return '';
94 function addMSG($type='OK', $action='', $message='') {
95 self::$messages[] = array($type, $message);
96 $this->haveMessage = $action;
97 return '';
100 static function getMSGs() {
101 return self::$messages;
104 static function call($action, $url_params) {
105 $action_params = explode('_', $action);
107 if (count($action_params) != 2)
108 return self::_addMSG('ERR', 'Action call error : bad params of ['.$action.']');
110 list($listener, $call) = $action_params;
111 if (array_key_exists($listener,self::$aliases))
112 $listener = self::$aliases[$listener];
114 if (!array_key_exists($listener, self::$myClasses))
115 return self::_addMSG('ERR', 'Action call error : class ['. $listener .'] not found');
117 $i = self::initInstance($listener);
119 if (in_array('action'.$call, $i->myMethods)) {
120 $i->haveMessage = NULL;
121 $ret = call_user_func(array($i, 'action'.$call), $url_params);
122 if (!isset($_SESSION['last_action']) or $action != $_SESSION['last_action'])
123 $_SESSION['last_action'] = $action;
124 $msg = $i->haveMessage;
125 if ($msg and ($msg != $action)) {
126 $ret = self::call($msg, $url_params);
127 return self::_addMSG('OK', $ret);
129 return self::_addMSG('OK', $ret);
130 } else
131 return self::_addMSG('ERR', 'Action call error : action ['. $call .'] of ['. $listener .'] not found');
135 function callAction($action) {
136 $c = '';
137 ryActionPage::call($action, ryzom_get_params());
138 $msgs = ryActionPage::getMSGs();
140 foreach ($msgs as $msg) {
141 if ($msg[0] == 'HEADER')
142 $c .= $msg[1];
145 foreach ($msgs as $msg) {
146 if ($msg[0] == 'ERR')
147 $c .= _s('message error', $msg[1]);
148 else if ($msg[0] == 'MSG')
149 $c .= _s('message', $msg[1]);
150 else if ($msg[0] == 'WARNING')
151 $c .= _s('message warning', $msg[1]);
152 else if ($msg[0] != 'HEADER')
153 $c .= $msg[1];
155 return $c;