3 /* Copyright (C) 2012 Winch Gate Property Limited
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) {
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')) )
65 function cleanNameID($name) {
67 for ($i=0; $i<strlen($name); $i++
) {
68 $c = substr($name, $i, 1);
69 if ($c == ' ' ||
$c == '_')
71 else if ( ((ord(strtolower($c)) >= ord('a')) && (ord(strtolower($c)) <= ord('z'))) ||
72 (ord($c) >= ord('0')) && (ord($c) <= ord('9')) )
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) {
87 $trad = $value[$lang];
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()) {
98 $src = str_replace("\r", '', $src);
99 $ssrc = explode("\n", $src);
102 $current_ryform = NULL;
103 foreach ($ssrc as $line) {
106 if ($mode != 'ryform')
107 $c .= str_repeat("\t", $indent);
110 if ($line[0] == "\t") {
111 $cache .= substr($line, 1)."\n";
118 if ($line == ')' ||
$line == '}') {
119 if ($cache && $current_ryform) {
120 $c .= $current_ryform->setSource($cache, $indent+
1);
123 $array[] = $current_ryform;
125 $array = $current_ryform;
127 if (array_key_exists($var_name, $protecteds))
128 call_user_func(array($object, $protecteds[$var_name]), $array);
130 $object->$var_name = $array;
134 } else if ($line[0] == '[') {
135 if ($cache && $current_ryform) {
136 $c .= $current_ryform->setSource($cache, $indent+
1);
137 $array[] = $current_ryform;
140 $ryform_name = substr($line, 1, strlen($line)-2);
141 $c .= 'New Ryform: '.$ryform_name."\n";
143 $current_ryform = new $ryform_name();
150 $sep = strpos($line, '=');
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";
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));
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;
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();
194 public $type = DEF_TYPE_UNKNOWN
;
195 public $params = array();
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='') {
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
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() {
247 function getFormDefsExtraValues() {
251 /*function preSerialization() {
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))
267 function postSerialization($vars=array()) {
270 function getTemplate() {
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)) {
284 foreach ($var as $element) {
285 if (is_object($element))
286 $c .= $element->getSource($indent+
1);
288 $c .= '#'.str_replace("\r", '', str_replace("\n", '\\\\n', var_export($element, true)));
290 $c .= str_repeat("\t", $indent).")";
292 $c .= '= '.str_replace("\r", '', str_replace("\n", '\\\\n', var_export($var, true)));
298 function setSource($src, $indent=0) {
299 $this->preSerialization();
300 return setRyformSource($this, $src, $indent);