3 /* Copyright (C) 2009 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_api. If not, see <http://www.gnu.org/licenses/>.
20 include_once (RYAPI_PATH
.'/common/ryformBases.php');
22 class ryVar
extends basicRyForm
{
23 public $formName = '';
25 public $varValue = '';
27 function getFormDefs() {
29 new ryFormDef('varName', DEF_TYPE_TEXT
),
30 new ryFormDef('varValue', DEF_TYPE_TEXT
),
34 function getHtmlRepr() {
35 return $this->varName
.' => '.$this->varValue
;
42 private $defines = array();
43 private $template = '';
45 static private $forms;
46 static private $ryformsIcons = array();
48 function __construct($dir, $name) {
50 self
::$forms[$dir.'/'.$name] = $this;
53 static function addRyFormIcon($ryform_name, $icon) {
54 self
::$ryformsIcons[$ryform_name] = $icon;
57 function addDefine($def) {
58 $this->defines
[$def->name
] = $def;
61 function setTemplate($template) {
62 $this->template
= $template;
65 function getTemplate() {
66 return $this->template
;
69 function addValue($name, $value) {
70 if (array_key_exists($name, $this->defines
))
71 $this->defines
[$name]->value
= $value;
77 function addExtraValues($name, $value) {
78 if (array_key_exists($name, $this->defines
))
79 $this->defines
[$name]->extraValues
= $value;
85 function getForm($params) {
87 if (isset($params['ryform_action']) && $params['ryform_action']) {
88 $res = $this->doAction($params);
92 if (@$params['validate'] == $this->name
)
93 return array(DATA_FORM_VALUES
, $_POST);
95 if (isset($params['ryform_parent']))
96 $parent_ryform_name = $params['ryform_parent'].'/';
98 $parent_ryform_name = '';
100 $action =_url(ryzom_get_params(), array('validate' => $this->name
));
102 $ret .= '<form action="'.$action.'" method="POST">'."\n";
104 if (!$this->getTemplate()) {
105 $ret .= ' <table width="100%" cellpadding="0" cellspacing="0">'."\n";
106 $ret .= ' '._s('t header', '<td height="24px">'._t('parameter').'</td><td>'._t('value').'</td><td></td>')."\n";
109 $tmpl = $this->getTemplate();
114 foreach ($this->defines
as $def_id => $def) {
115 if ($def->name
== 'name')
116 $def->name
= '_name';
118 $deffullname = $def->name
;
119 $url_params = ryzom_get_params();
121 $infos = $def->infos
;
122 $value = ($def->value
!== NULL)?
$def->value
:$def->defaultValue
;
124 if (!is_object($value) && !is_array($value))
125 $str_value = _h(strval($value));
130 $type = DEF_TYPE_HIDDEN
;
136 case DEF_TYPE_HIDDEN
:
137 $input = '<input type="hidden" name="'.$def->name
.'" value="'.$str_value.'" />'."\n";
142 $input = '<input style="width:250px" type="text" name="'.$def->name
.'" value="'.$str_value.'" size="25'.(_user()->ig?
'0':'').'" />';
144 case DEF_TYPE_NAMEID
:
145 $input = '<input style="width:250px" type="text" name="'.$def->name
.'" value="'.getNameId($str_value).'" size="25'.(_user()->ig?
'0':'').'" />';
151 $input = '<input style="width:100px" type="text" name="'.$def->name
.'" value="'.$str_value.'" size="10'.(_user()->ig?
'0':'').'" />';
155 $input = '<select name="'.$def->name
.'">'."\n";
157 $input .= '<option selected="selected" value="on">'._t('yes').'</option>'."\n".'<option value="off">'._t('no').'</option>';
159 $input .= '<option value="on">'._t('yes').'</option>'."\n".'<option selected="selected" value="off">'._t('no').'</option>';
160 $input .= '</select>';
163 case DEF_TYPE_OPTION_FUNCTION
:
164 case DEF_TYPE_OPTION
:
165 if ($type == DEF_TYPE_OPTION
)
166 $options = $def->params
;
168 if (is_array($def->defaultValue
))
169 $options = call_user_func_array($def->params
, $def->defaultValue
);
171 $options = call_user_func($def->params
);
173 $input = '<select name="'.$def->name
.'">'."\n";
175 foreach ($options as $option => $text) {
176 $option = strval($option);
177 if ($option && $option[0] === '<' && $option[1] === '/')
178 $options_html .= '</optgroup>';
179 else if ($option && $option[0] === '<')
180 $options_html .= '<optgroup label="'.$text.'">';
182 if ($value !== NULL and (strval($value) == $option))
183 $options_html .= '<option selected="selected" value="'.$option.'">'.$text.'</option>'."\n";
185 $options_html .= '<option value="'.$option.'">'.$text.'</option>'."\n";
188 $input .= $options_html;
189 $input .= '</select>';
192 case DEF_TYPE_COMBO_FUNCTION
:
194 if ($type == DEF_TYPE_COMBO
)
195 $options = $def->params
;
197 if (is_array($def->defaultValue
))
198 $options = call_user_func_array($def->params
, $def->defaultValue
);
200 $options = call_user_func($def->params
);
203 // TODO : try to do it with lua
205 $input .= '<input style="width:400px" type="text" id="'.$def->name
.'" name="'.$def->name
.'" value="'.$str_value.'" />
206 <select onChange="combo(this, \''.$def->name
.'\')" onMouseOut="comboInit(this, \''.$def->name
.'\')" >';
208 $have_selected = false;
209 foreach ($options as $option => $text) {
210 if ($option && $option[0] === '<' && $option[1] === '/')
211 $options_html .= '</optgroup>';
212 else if ($option && $option[0] === '<')
213 $options_html .= '<optgroup label="'.$text.'">';
216 if ($value and ($value == $option)) {
217 $have_selected = true;
218 $options_html .= '<option selected="selected" value="'.$option.'">'.$text.'</option>'."\n";
220 $options_html .= '<option value="'.$option.'">'.$text.'</option>'."\n";
224 $input .= '<option value=""></option>';
226 $input .= '<option selected="selected" value=""></option>';
227 $input .= $options_html;
228 $input .= '</select>';
232 case DEF_TYPE_TEXTAREA
:
235 $input = '<pre>'.($type == DEF_TYPE_BBCODE?
'<font color="orange">- BBCode -</font><br />':'').'<textarea name="'.$def->name
.'" rows="3">'._h($value).'</textarea></pre>';
241 $value = array_merge(array('en' => '', 'fr' => '', 'de' => '', 'ru' => '', 'es' => ''), $value);
242 $base = ryzom_get_param('select_base', '');
243 $edit = $display = $input_header = '';
244 foreach (array('en', 'fr', 'de', 'ru', 'es') as $lang) {
245 if (_user()->lang
== $lang)
246 $edit = _i($lang == 'en'?API_URL
.'data/img/lang/us.png':API_URL
.'data/img/lang/'.$lang.'.png').' <textarea style="width: 90%" rows="3" name="'.$param.'['.$lang.']">'._h($value[$lang]).'</textarea>';
247 if ((!$base && $value[$lang]) ||
$base == $lang) {
249 $display = strtoupper($lang).' = <font color="orange">'.str_replace("\n", '<br />', _h($value[$lang])).'</font>';
251 $input .= '<input type="hidden" name="'.$param.'['.$lang.']" value="'.$value[$lang].'" />';
252 $input_header .= _l(_i($lang == 'en'?API_URL
.'data/img/lang/us.png':API_URL
.'data/img/lang/'.$lang.'.png'), $url_params, array('select_base' => $lang)).' ';
255 $input = $input_header.$input.' '.$display.'<br />'.$edit;
258 case DEF_TYPE_RYFORM
:
259 case DEF_TYPE_RYFORMS_ARRAY
:
260 $savedRyform = $value;
261 if (is_array($savedRyform)) {
263 foreach ($savedRyform as $id => $ryform) {
264 if (!is_object($ryform))
267 foreach ($to_clean as $id)
268 unset($savedRyform[$id]);
269 $savedRyform = array_values($savedRyform);
270 } else if (is_object($savedRyform)) {
271 $savedRyform = array($savedRyform);
273 $savedRyform = array();
275 $input .= '<table width="100%" cellspacing="0" cellpadding="0" >';
277 foreach ($savedRyform as $id => $ryform) {
278 if (!is_object($ryform)) {
279 p('!!! ERROR !!!', $ryform);
283 if (!isset($ryform->formName
) ||
!$ryform->formName
)
284 $ryform->formName
= 'Element '.$id;
285 if (count($savedRyform) > 1)
286 $display_id = '<font size="12px" style="font-weight: bold; font-size: 14px" color="#FFAA55">'.strval(intval($id)+
1).'</font>';
290 $script_up = ($id != 0)?
_l(_i('16/arrow_up', _t('up')), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.$id, 'ryform_action' => 'up')).' ':'';
291 $script_down = ($id != count($savedRyform)-1)?
_l(_i('16/arrow_down', _t('down')), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.$id, 'ryform_action' => 'down')).' ':'';
293 $icon = (isset(self
::$ryformsIcons[get_class($ryform)]))?self
::$ryformsIcons[get_class($ryform)]:_i('32/brick');
294 $input .= _s('t row '.($id%2
),
295 '<td width="36px">'._l(($def->type
== DEF_TYPE_RYFORM?
_i('16/arrow_redo', _t('change')):_i('16/add', _t('add'))), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.strval(intval($id)+
1), 'ryform_action' => 'list')).' '.$display_id.'</td>'.
296 '<td width="10px">'.$script_up.$script_down.'</td>'.
297 '<td ><table width="100%"><tr>
298 <td width="40px">'.$icon.'</td>
299 <td valign="middle" width="300px"><font size="12px" style="font-size: 13px;font-weight: bold;" color="#FFAA55">'.
300 _l($ryform->formName
, $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.$id, 'ryform_action' => 'edit')).' '.
301 '</font><br />'._t(get_class($ryform).'_short_description').'</td>
302 <td align="left" valign="middle" bgcolor="#000000">'.$ryform->getHtmlRepr().'</td>
303 </tr></table><td width="70px" align="right">'.
304 _l(_i('16/script_edit', _t('edit')), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.$id, 'ryform_action' => 'edit')).' '.
305 _l(_i('16/script_code', _t('edit_source')), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.$id, 'ryform_action' => 'source')).' '.
306 _l(_i('16/script_delete', _t('del')), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':'.$id, 'ryform_action' => 'del')).'</td><td> </td>');
309 $input .= '</table>';
310 if (count($savedRyform) == 0 ||
$def->type
!= DEF_TYPE_RYFORM
) {
311 if (is_string($def->params
))
312 $infos = _l(_i('16/add', _t('add')), $url_params, array('new_ryform' => $def->params
, 'ryform_name' => $parent_ryform_name.$deffullname.':0', 'ryform_action' => 'add'));
313 else if (count($def->params
) == 1)
314 $infos = _l(_i('16/add', _t('add')), $url_params, array('new_ryform' => $def->params
[0], 'ryform_name' => $parent_ryform_name.$deffullname.':0', 'ryform_action' => 'add'));
317 $infos = _l(_i('16/add', _t('add')), $url_params, array('ryform_name' => $parent_ryform_name.$deffullname.':0', 'ryform_action' => 'list'));
319 if ($type == DEF_TYPE_RYFORMS_ARRAY
)
320 $infos .= ' '._l(_i('16/application_form_add', _t('multiadd')), $url_params, array('ryform_name' => $deffullname, 'ryform_action' => 'list_multiadd'));
324 case DEF_TYPE_FUNCTION
:
325 if (is_array($def->defaultValue
))
326 list($result_type, $value) = call_user_func_array($def->params
, $def->defaultValue
);
328 list($result_type, $value) = call_user_func($def->params
);
329 if ($result_type == DATA_HTML_FORM
) {
330 return array(DATA_HTML_FORM
, $value);
332 unset($url_params[$deffullname.'_action']);
338 $input = '<input type="hidden" name="'.$def->name
.'" value="'.$value.'" />'.$value."\n";
348 $tmpl = str_replace('{'.$def->name
.'}', '<font '.(_user()->ig?
'color="orange" size="11"':'style="color:orange;"').'>'._t($def->prefixTrad
.$def->name
).'</font>', $tmpl);
349 $tmpl = str_replace('{'.$def->name
.'.input}', $input, $tmpl);
350 $tmpl = str_replace('{'.$def->name
.'.infos}', $infos, $tmpl);
352 $ret .= _s('t row '.strval($i %
2), '<td height="32px" width="200px"> '.(!$def->optional?
'*':'').($def->superAdmin?
'##':'').($def->admin?
'#':'')._t($def->prefixTrad
.$def->name
).'</td><td valign="center">'.$input.'</td><td>'.$infos.'</td>')."\n";
358 $tmpl = str_replace('{submit.input}', '<input type="submit" value="'._t('submit').'" />', $tmpl);
360 $ret .= '<table width="100%" cellspacing="0" cellpadding="0" ><tr>'._s('t row '.strval($i %
2), '<td height="32px"> '._t('required_fields').'</td><td></td><td align="middle"><input type="submit" value="'._t('submit').'" /></td>').'</tr></table>';
362 $ret .= _s('t row '.strval($i %
2), '<td height="32px"> '._t('required_fields').'</td><td></td><td><input type="submit" value="'._t('submit').'" /></td>');
365 $ret .= '</form><br />';
366 return array(DATA_HTML_FORM
, $ret."\n");
370 function doAction($url_params) {
371 if (!$url_params['ryform_name'])
372 return array(DATA_RYFORM_VALUE
, array());
374 $ryforms = explode('/', $url_params['ryform_name']);
375 $this_ryform_name = array_shift($ryforms);
376 list($ryform_name,$ryform_pos) = explode(':', $this_ryform_name);
377 if (!isset($this->defines
[$ryform_name]))
378 return 'Bad ryform name';
379 $def = $this->defines
[$ryform_name];
383 $next_action = $url_params['ryform_action'];
385 $action = $url_params['ryform_action'];
391 if (isset($url_params['ryform_parent']) && $url_params['ryform_parent'])
392 $ryform_parent = $url_params['ryform_parent'].'/';
395 $ret .= '<table cellpadding="0" cellspacing="0" width="100%">';
396 foreach ($def->params
as $id => $ryform) {
397 if (is_array($ryform)) {
398 $ret .= _s('t row 1', '<td height="28px" width="22px" bgcolor='.ryzom_get_color_style('section').'></td><td bgcolor='.ryzom_get_color_style('section').'>'.ryzom_font(_t('ryform_cat_'.$id), '', '12').'</td><td bgcolor='.ryzom_get_color_style('section').'></td>');
399 foreach ($ryform as $subid => $subryform) {
400 $ret .= _s('t row '.($subid%2
), '<td width="22px" height="34px">'.self
::$ryformsIcons[$subryform].'</td><td> '.
401 _l(_t($subryform.'_short_description'), $url_params, array('ryform_name' => $ryform_parent.$url_params['ryform_name'], 'ryform_action' => 'add', 'new_ryform' => $subryform)).'</td><td>'.$subryform.'</td>');
404 $ret .= _s('t row '.($id%2
), '<td width="22px" height="34px">'.self
::$ryformsIcons[$ryform].'</td><td> '.
405 _l(_t($ryform.'_short_description'), $url_params, array('ryform_name' => $ryform_parent.$url_params['ryform_name'], 'ryform_action' => 'add', 'new_ryform' => $ryform)).'</td><td>'.$ryform.'</td>');
408 return array(DATA_HTML_FORM
, $ret);
411 case 'list_multiadd':
414 unset($url_params[$deffullname.'_action']);
415 $ret .= '<table cellpadding="5"><tr>';
416 foreach ($def->params as $ryform) {
417 $ret .= '<td bgcolor="#000000">'._l($ryform, $url_params, array('ryform_action' => 'multiadd', 'new_ryform' => $ryform)).'</td>';
419 $ret .= '</tr></table>';
420 return array(DATA_HTML_FORM, $ret);*/
424 $new_ryform = ryzom_get_param('new_ryform');
425 $valid_ryform = false;
428 if ((is_string($def->params
) && $new_ryform == $def->params
) ||
in_array($new_ryform, $def->params
))
429 $valid_ryform = true;
431 foreach ($def->params
as $param) {
432 if (is_array($param) && in_array($new_ryform, $param))
433 $valid_ryform = true;
437 return array(DATA_HTML_FORM
, 'Bad ryform');
440 $new_ryform = $def->params
;
442 $ryform = new $new_ryform($new_ryform, '');
443 $ryform->preSerialization();
444 if ($def->type
!= DEF_TYPE_RYFORM
) {
446 if (!is_array($def->value
))
447 $savedRyform = array($def->value
);
449 $savedRyform = $def->value
;
450 if ($ryform_pos === 0) {
451 $value = array_values(array_merge(array($ryform), $savedRyform));
452 } else if ($ryform_pos !== NULL) {
453 $begin = array_slice($savedRyform, 0, $ryform_pos);
454 $end = array_slice($savedRyform, $ryform_pos, count($savedRyform)-$ryform_pos);
455 $value = array_values(array_merge($begin, array($ryform), $end));
458 p($ryform_name, $value);
459 //return array(DATA_RYFORM_VALUE, array('stages' => array()));
460 return array(DATA_RYFORM_VALUE
, array($ryform_name => $value));
462 p($ryform_name, $ryform);
463 return array(DATA_RYFORM_VALUE
, array($ryform_name => $ryform));
468 $a_ryforms = $def->value
;
469 if (is_array($a_ryforms))
470 $ryform = $a_ryforms[$ryform_pos];
472 $ryform = $a_ryforms;
473 $ryform->postSerialization();
474 $validate = isset($url_params['validate']) && $url_params['validate'];
476 $form = new ryForm('', $def->name
);
477 foreach ($ryform->getFormDefs() as $form_def) {
478 $form->addDefine($form_def);
479 $name = $form_def->name
;
480 // Init form with ryfom values
481 if (property_exists($ryform, $name)) {
482 $form->addValue($form_def->name
, $ryform->$name);
485 foreach ($ryform->getFormDefsExtraValues() as $def_name => $extra_values)
486 $form->addExtraValues($def_name, $extra_values);
487 $form->setTemplate($ryform->getTemplate());
489 list($result_type, $value) = $form->getForm(array('action' => $url_params['action'], 'script' => $url_params['script'], 'ryform_action' => $next_action, 'ryform_parent' => $this_ryform_name, 'ryform_name' => implode('/', $ryforms), 'validate' => $validate));
490 if ($result_type == DATA_HTML_FORM
) {
491 return array(DATA_HTML_FORM
, $value);
493 if ($result_type == DATA_FORM_VALUES
)
494 $value = $form->validateFormPost($value);
495 $ryform->setFormParams($value);
496 $ryform->preSerialization();
499 if (is_array($a_ryforms))
500 $a_ryforms[$ryform_pos] = $ryform;
502 $a_ryforms = $ryform;
504 $value = array($ryform_name => $a_ryforms);
505 return array(DATA_RYFORM_VALUE
, $value);
512 if (!is_array($def->value
))
513 $def->value
= array();
515 unset($def->value
[$id]);
516 $value = array_values($def->value
);
517 return array(DATA_RYFORM_VALUE
, array($ryform_name => $value));
521 $a_ryforms = $def->value
;
522 if (!is_array($a_ryforms))
523 ryzom_redirect(_url($url_params, array('ryform_action' => '')));
525 $temp_ryform = $a_ryforms[$ryform_pos-1];
526 $a_ryforms[$ryform_pos-1] = $a_ryforms[$ryform_pos];
527 $a_ryforms[$ryform_pos] = $temp_ryform;
528 $a_ryforms = array_values($a_ryforms);
529 p($ryform_name, $a_ryforms);
530 return array(DATA_RYFORM_VALUE
, array($ryform_name => $a_ryforms));
534 $a_ryforms = $def->value
;
535 if (!is_array($a_ryforms))
536 ryzom_redirect(_url($url_params, array('ryform_action' => '')));
538 $temp_ryform = $a_ryforms[$ryform_pos+
1];
539 $a_ryforms[$ryform_pos+
1] = $a_ryforms[$ryform_pos];
540 $a_ryforms[$ryform_pos] = $temp_ryform;
541 $a_ryforms = array_values($a_ryforms);
542 return array(DATA_RYFORM_VALUE
, array($ryform_name => $a_ryforms));
546 $a_ryforms = $def->value
;
547 if (is_array($a_ryforms))
548 $ryform = $a_ryforms[$ryform_pos];
550 $ryform = $a_ryforms;
551 $ryform->postSerialization();
553 $form = new ryForm('', $def->name
);
554 $form->addDefine(new ryFormDef('ryform_source', DEF_TYPE_TEXTAREA
, '', base64_encode(serialize($ryform))));
555 $validate = isset($url_params['validate']) && $url_params['validate'];
556 list($result_type, $value) = $form->getForm(array('ryform_action' => $next_action, 'ryform_parent' => $this_ryform_name, 'ryform_name' => implode('/', $ryforms), 'validate' => $validate));
557 if ($result_type == DATA_HTML_FORM
) {
558 return array(DATA_HTML_FORM
, $value);
560 if ($result_type == DATA_FORM_VALUES
)
561 $params = $form->validateFormPost($value);
564 $ryform = unserialize(base64_decode($params['ryform_source']));
565 if (!is_object($ryform)) {
566 unset($url_params['validate']);
567 ryzom_redirect(_url($url_params, array('ryform_action' => '', 'message' => 'bad_paste')));
572 foreach ($def->params
as $id => $ryform_class) {
573 if (is_array($ryform_class)) {
574 if (in_array(get_class($ryform), array_values($ryform_class)))
576 } else if (get_class($ryform) == $ryform_class)
580 p(get_class($ryform), $def->params
);
581 ryzom_redirect(_url($url_params, array($deffullname.'_action' => '', 'message' => 'not_valid_stage')));
584 if (is_array($a_ryforms))
585 $a_ryforms[$ryform_pos] = $ryform;
587 $a_ryforms = $ryform;
588 $value = array($ryform_name => $a_ryforms);
589 return array(DATA_RYFORM_VALUE
, $value);
598 function validateFormPost($params, $use_default=true) {
600 foreach ($this->defines
as $def) {
602 if ($def->name
== 'name')
603 $def->name
= '_name';
607 $type = DEF_TYPE_HIDDEN
;
609 if (isset($params[$def->name
])) {
610 $value = $params[$def->name
];
611 } else if (!$use_default) {
620 case DEF_TYPE_HIDDEN
:
622 case DEF_TYPE_OPTION
:
623 case DEF_TYPE_TEXTAREA
:
624 $final[$name] = $value;
628 if (!$value[_user()->lang
] && $value['europeanunion'])
629 $value[_user()->lang
] = $value['europeanunion'];
630 $final[$name] = $value;
633 case DEF_TYPE_NAMEID
:
634 $final[$name] = cleanNameID($value);
638 $final[$name] = $value;
642 case DEF_TYPE_INT
: // TODO
643 $final[$name] = intval($value);
645 case DEF_TYPE_FLOAT
: // TODO
646 $final[$name] = floatval($value);
650 $final[$name] = $value == 'on';
653 case DEF_TYPE_RYFORM
:
654 if (is_array($value))
655 $final[$name] = $value[0];
657 case DEF_TYPE_RYFORMS_ARRAY
:
661 $final[$name] = $value;
671 // Clean all temp files
672 $userDatas = _tools()->listAppDataFiles($this->dir);
673 foreach ($userDatas as $userData) {
674 if (substr($userData, 0, strlen($this->name)) == $this->name)
675 _tools()->saveAppData($this->dir.'/'.$userData, NULL);