Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / api / common / ryformBases.php
blob07762953f0cddf15a238cc7b833b1551f5bdf30b
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 define('DEF_TYPE_UNKNOWN', 0);
21 define('DEF_TYPE_HIDDEN', 1);
22 define('DEF_TYPE_TEXT', 2);
23 define('DEF_TYPE_ID', 3);
24 define('DEF_TYPE_INT', 4);
25 define('DEF_TYPE_FLOAT', 5);
26 define('DEF_TYPE_BOOL', 6);
27 define('DEF_TYPE_OPTION', 7);
28 define('DEF_TYPE_TEXTAREA', 8);
29 define('DEF_TYPE_TRAD', 9);
30 define('DEF_TYPE_FORM', 10);
31 define('DEF_TYPE_ICON', 11);
32 define('DEF_TYPE_RYFORM', 12);
33 define('DEF_TYPE_RYFORMS_ARRAY', 13);
34 define('DEF_TYPE_BBCODE', 14);
35 define('DEF_TYPE_FUNCTION', 15);
36 define('DEF_TYPE_COMBO', 16);
37 define('DEF_TYPE_OPTION_FUNCTION', 17);
38 define('DEF_TYPE_NAMEID', 18);
39 define('DEF_TYPE_COMBO_FUNCTION', 19);
40 define('DEF_TYPE_DATE', 20);
43 define('DEF_TYPE_ICON_UNKNOWN', 0);
44 define('DEF_TYPE_ICON_SHARED', 1);
45 define('DEF_TYPE_ICON_RYZITEM', 2);
46 define('DEF_TYPE_ICON_URL', 3);
48 define('DATA_HTML_FORM', 0);
49 define('DATA_FORM_VALUES', 1);
50 define('DATA_FORM_VALID_VALUES', 2);
51 define('DATA_RYFORM_VALUE', 3);
53 function cleanFormName($name) {
54 $final_name = '';
55 for ($i=0; $i<strlen($name); $i++) {
56 $c = substr($name, $i, 1);
57 if ( ((ord(strtolower($c)) >= ord('a')) && (ord(strtolower($c)) <= ord('z'))) ||
58 (in_array($c, array('-', '.', '_'))) ||
59 (ord(strtolower($c)) >= ord('0')) && (ord(strtolower($c)) <= ord('9')) )
60 $final_name .= $c;
62 return $final_name;
65 function cleanNameID($name) {
66 $final_name = '';
67 for ($i=0; $i<strlen($name); $i++) {
68 $c = substr($name, $i, 1);
69 if ($c == ' ' || $c == '_')
70 $final_name .= '_';
71 else if ( ((ord(strtolower($c)) >= ord('a')) && (ord(strtolower($c)) <= ord('z'))) ||
72 (ord($c) >= ord('0')) && (ord($c) <= ord('9')) )
73 $final_name .= $c;
75 return $final_name;
78 function getNameId($name) {
79 return str_replace('_', ' ', $name);
82 function getTrad($value) {
83 if ($value[_user()->lang])
84 $trad = $value[_user()->lang];
85 foreach (array('en', 'fr', 'de', 'ru', 'es') as $lang) {
86 if ($value[$lang]) {
87 $trad = $value[$lang];
88 break;
91 if (substr($trad, 0, 2) == '//')
92 $trad = strstr(str_replace("\r", '', $trad), "\n");
93 return substr($trad, 1);
96 function setRyformSource($object, $src, $indent=0, $protecteds=array()) {
97 $c = '';
98 $src = str_replace("\r", '', $src);
99 $ssrc = explode("\n", $src);
100 $mode = 'var';
101 $cache = '';
102 $current_ryform = NULL;
103 foreach ($ssrc as $line) {
104 if (!$line)
105 continue;
106 if ($mode != 'ryform')
107 $c .= str_repeat("\t", $indent);
108 switch ($mode) {
109 case 'ryform':
110 if ($line[0] == "\t") {
111 $cache .= substr($line, 1)."\n";
112 break;
113 } else {
114 $mode = 'array';
117 case 'array':
118 if ($line == ')' || $line == '}') {
119 if ($cache && $current_ryform) {
120 $c .= $current_ryform->setSource($cache, $indent+1);
121 $c .= 'SET SOURCE ';
122 if ($line == ')')
123 $array[] = $current_ryform;
124 else
125 $array = $current_ryform;
127 if (array_key_exists($var_name, $protecteds))
128 call_user_func(array($object, $protecteds[$var_name]), $array);
129 else
130 $object->$var_name = $array;
131 $mode = 'var';
132 $cache = '';
133 continue;
134 } else if ($line[0] == '[') {
135 if ($cache && $current_ryform) {
136 $c .= $current_ryform->setSource($cache, $indent+1);
137 $array[] = $current_ryform;
138 $c .= 'SET SOURCE ';
140 $ryform_name = substr($line, 1, strlen($line)-2);
141 $c .= 'New Ryform: '.$ryform_name."\n";
142 $cache = '';
143 $current_ryform = new $ryform_name();
144 $mode = 'ryform';
145 continue;
147 break;
149 default:
150 $sep = strpos($line, '=');
151 if ($sep) {
152 $var_name = substr($line, 0, $sep-1);
153 $value = substr($line, $sep+2);
154 if ($value == '(' || $value == '{') {
155 $c .= $var_name.' is ARRAY'."\n";
156 $mode = 'array';
157 $array = array();
158 } else {
159 $c .= $var_name .' = '.$value."\n";
160 if ($value[0] == '\'' && $value[strlen($value)-1] == '\'')
161 $object->$var_name = str_replace('\n', "\n", substr($value, 1, -1));
162 else {
163 if (is_numeric($value))
164 $object->$var_name = eval('return '.$value.';');
165 else if ($value == 'false')
166 $object->$var_name = false;
167 else if ($value == 'true')
168 $object->$var_name = true;
169 else if ($value == 'NULL')
170 $object->$var_name = NULL;
174 break;
177 return $c;
180 interface iRyForm {
181 function getForm($url_params);
182 function setFormParams($params);
183 function getHtmlRepr();
184 function getFormDefs();
185 function getFormDefsExtraValues();
186 function preSerialization();
187 function postSerialization($vars=array());
188 function getTemplate();
191 class ryFormDef {
193 public $name = '';
194 public $type = DEF_TYPE_UNKNOWN;
195 public $params = array();
196 public $infos = '';
197 public $defaultValue = NULL;
198 public $value = NULL;
199 public $extraValues = array();
200 public $hidden = false;
201 public $optional = false;
202 public $admin = false;
203 public $superAdmin = false;
204 public $prefixTrad = '';
206 function __construct($name, $type, $params=array(), $defaultValue=NULL, $optional=false, $infos='') {
207 $this->name = $name;
208 $this->type = $type;
209 $this->params = $params;
210 $this->defaultValue = $defaultValue;
211 $this->optional = $optional;
212 $this->infos = $infos;
217 class basicRyForm implements iRyForm {
218 public $formName = ''; // Used by Form
219 public $id = 0;
221 function __construct($name='', $title='') {
224 function getForm($url_params) {
225 $form = new ryForm($this->formName);
226 $form_defs = $this->getFormDefs();
227 foreach ($form_defs as $def)
228 $form->addDefine($def);
229 return $form->getForm(_s('section', $this->formName.' ('.get_class($this).')'));
232 function setFormParams($params) {
233 foreach ($params as $name => $value) {
234 if (property_exists($this, $name))
235 $this->$name = $value;
239 function getHtmlRepr() {
240 return $this->formName.' ('.get_class($this).')';
243 function getFormDefs() {
244 return array();
247 function getFormDefsExtraValues() {
248 return array();
251 /*function preSerialization() {
252 unset($this->tools);
255 function preSerialization() {
256 $all_defs = array('class_name');
257 $this->author = _user()->id;
258 foreach ($this->getFormDefs() as $def)
259 $all_defs[] = $def->name;
261 foreach (get_object_vars($this) as $name => $value) {
262 if (!in_array($name, $all_defs))
263 unset($this->$name);
267 function postSerialization($vars=array()) {
270 function getTemplate() {
271 return '';
274 function getSource($indent=0) {
275 $attrs = $this->getFormDefs();
276 $c = str_repeat("\t", $indent-1).'['.get_class($this).']'."\n";
277 foreach ($attrs as $attr) {
278 $c .= str_repeat("\t", $indent).$attr->name.' ';
279 $var = $this->{$attr->name};
280 if (is_object($var)) {
281 $c .= "= {\n".substr($var->getSource($indent+1), 0, -1)."\n".str_repeat("\t", $indent).'}';
282 } else if (is_array($var)) {
283 $c .= '= ('."\n";
284 foreach ($var as $element) {
285 if (is_object($element))
286 $c .= $element->getSource($indent+1);
287 else if ($element)
288 $c .= '#'.str_replace("\r", '', str_replace("\n", '\\\\n', var_export($element, true)));
290 $c .= str_repeat("\t", $indent).")";
291 } else
292 $c .= '= '.str_replace("\r", '', str_replace("\n", '\\\\n', var_export($var, true)));
293 $c .= "\n";
295 return $c;
298 function setSource($src, $indent=0) {
299 $this->preSerialization();
300 return setRyformSource($this, $src, $indent);