2015-08-03 11:35:01 +02:00
|
|
|
<?php
|
|
|
|
class XMPPtoForm{
|
|
|
|
private $fieldset;
|
|
|
|
private $xmpp;
|
|
|
|
private $html;
|
|
|
|
|
|
|
|
public function __construct(){
|
|
|
|
$this->fieldset = 0;
|
2015-11-13 13:37:39 +01:00
|
|
|
$this->html = new \DOMDocument('1.0', 'UTF-8');
|
2015-08-03 11:35:01 +02:00
|
|
|
$this->xmpp = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHTML($xmpp){
|
|
|
|
$this->setXMPP($xmpp);
|
|
|
|
$this->create();
|
2015-11-13 13:37:39 +01:00
|
|
|
return $this->html->saveXML();
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setXMPP($xmpp){
|
|
|
|
$this->xmpp = $xmpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(){
|
|
|
|
$this->xmpp = str_replace('xmlns=', 'ns=', $this->xmpp);
|
|
|
|
$x = new SimpleXMLElement($this->xmpp);
|
|
|
|
|
|
|
|
foreach($x->children() as $element){
|
|
|
|
switch($element->getName()){
|
|
|
|
case "title":
|
|
|
|
$this->outTitle($element);
|
|
|
|
break;
|
|
|
|
case "instructions":
|
|
|
|
$this->outP($element);
|
|
|
|
break;
|
|
|
|
case "field":
|
2015-11-13 13:37:39 +01:00
|
|
|
//if($element['type'] != 'hidden' && $element['type'] != 'fixed')
|
|
|
|
// $this->html .='<div>';
|
2015-08-03 11:35:01 +02:00
|
|
|
switch($element['type']){
|
|
|
|
case "boolean":
|
|
|
|
$this->outCheckbox($element);
|
|
|
|
break;
|
2015-11-13 13:37:39 +01:00
|
|
|
//case "fixed":
|
|
|
|
// $this->outBold($element);
|
|
|
|
// break;
|
2015-08-03 11:35:01 +02:00
|
|
|
case "text-single":
|
|
|
|
$this->outInput($element, "", "");
|
|
|
|
break;
|
|
|
|
case "text-multi":
|
|
|
|
$this->outTextarea($element);
|
|
|
|
break;
|
|
|
|
case "text-private":
|
|
|
|
$this->outInput($element, "password", "");
|
|
|
|
break;
|
|
|
|
case "hidden":
|
|
|
|
$this->outHiddeninput($element);
|
|
|
|
break;
|
|
|
|
case "list-multi":
|
|
|
|
$this->outList($element, "multiple");
|
|
|
|
break;
|
|
|
|
case "list-single":
|
|
|
|
$this->outList($element, "");
|
|
|
|
break;
|
|
|
|
case "jid-multi":
|
|
|
|
$this->outInput($element, "email", "multiple");
|
|
|
|
break;
|
|
|
|
case "jid-single":
|
|
|
|
$this->outInput($element, "email", "");
|
|
|
|
break;
|
|
|
|
default:
|
2015-11-13 13:37:39 +01:00
|
|
|
//$this->html .= "";
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
//if($element['type'] != 'hidden')
|
|
|
|
// $this->html .='</div>';
|
2015-08-03 11:35:01 +02:00
|
|
|
break;
|
|
|
|
case 'url':
|
|
|
|
|
|
|
|
break;
|
|
|
|
/*XML without <x> element*/
|
|
|
|
case 'username':
|
|
|
|
case 'email':
|
|
|
|
case 'password':
|
2015-11-13 13:37:39 +01:00
|
|
|
//$this->html .='<div class="element">';
|
2015-08-03 11:35:01 +02:00
|
|
|
$this->outGeneric($element->getName());
|
2015-11-13 13:37:39 +01:00
|
|
|
//$this->html .='</div>';
|
2015-08-03 11:35:01 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-11-13 13:37:39 +01:00
|
|
|
//$this->html .= "";
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
/*if($this->fieldset>0){
|
2015-08-03 11:35:01 +02:00
|
|
|
$this->html .= '</fieldset>';
|
2015-11-13 13:37:39 +01:00
|
|
|
}*/
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outGeneric($s){
|
2015-11-13 13:37:39 +01:00
|
|
|
$div = $this->html->createElement('div');
|
|
|
|
$div->setAttribute('class', 'element');
|
|
|
|
$this->html->appendChild($div);
|
|
|
|
|
|
|
|
$input = $this->html->createElement('input');
|
|
|
|
$input->setAttribute('type', $s);
|
|
|
|
$input->setAttribute('id', $s);
|
|
|
|
$input->setAttribute('name', 'generic_'.$s);
|
|
|
|
$input->setAttribute('required', 'required');
|
|
|
|
|
|
|
|
$div->appendChild($input);
|
|
|
|
|
|
|
|
$label = $this->html->createElement('label', $s);
|
|
|
|
$label->setAttribute('for', $s);
|
|
|
|
$div->appendChild($label);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
private function outTitle($s){
|
2015-11-13 13:37:39 +01:00
|
|
|
$ul = $this->html->createElement('ul');
|
|
|
|
$ul->setAttribute('class', 'thin simple');
|
|
|
|
$this->html->appendChild($ul);
|
|
|
|
|
|
|
|
$li = $this->html->createElement('li');
|
|
|
|
$li->appendChild($this->html->createElement('span', $s));
|
|
|
|
|
|
|
|
$ul->appendChild($li);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outP($s){
|
2015-11-13 13:37:39 +01:00
|
|
|
$ul = $this->html->createElement('ul');
|
|
|
|
$ul->setAttribute('class', 'thin simple');
|
|
|
|
$this->html->appendChild($ul);
|
|
|
|
|
|
|
|
$li = $this->html->createElement('li');
|
|
|
|
$ul->appendChild($li);
|
|
|
|
|
|
|
|
$li->appendChild($this->html->createElement('p', $s));
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outUrl($s) {
|
2015-11-13 13:37:39 +01:00
|
|
|
$a = $this->html->createElement('a', $s->getName());
|
|
|
|
$a->setAttribute('href', $s->getName());
|
|
|
|
$this->html->appendChild($a);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
/*
|
2015-08-03 11:35:01 +02:00
|
|
|
private function outBold($s){
|
|
|
|
if($this->fieldset > 0){
|
|
|
|
$this->html .= '</fieldset>';
|
|
|
|
}
|
|
|
|
$this->html .= '<fieldset><legend>'.$s->value.'</legend><br />';
|
|
|
|
$this->fieldset ++;
|
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
*/
|
2015-08-03 11:35:01 +02:00
|
|
|
private function outCheckbox($s){
|
2015-11-13 13:37:39 +01:00
|
|
|
$container = $this->html->createElement('div');
|
|
|
|
$this->html->appendChild($container);
|
|
|
|
|
|
|
|
$div = $this->html->createElement('div');
|
|
|
|
$div->setAttribute('class', 'select');
|
|
|
|
$container->appendChild($div);
|
|
|
|
|
|
|
|
$select = $this->html->createElement('select');
|
|
|
|
$select->setAttribute('type', $s['type']);
|
|
|
|
$select->setAttribute('label', $s['label']);
|
|
|
|
$select->setAttribute('id', $s['var']);
|
|
|
|
$select->setAttribute('name', $s['var']);
|
|
|
|
|
|
|
|
if($s->required)
|
|
|
|
$select->setAttribute('required', 'required');
|
|
|
|
|
|
|
|
$div->appendChild($select);
|
|
|
|
|
|
|
|
$option = $this->html->createElement('option', __('button.bool_yes'));
|
|
|
|
$option->setAttribute('value', 'true');
|
|
|
|
if(isset($s->value) || $s->value == "true" || $s->value == "1")
|
|
|
|
$option->setAttribute('selected', 'selected');
|
|
|
|
$select->appendChild($option);
|
|
|
|
|
|
|
|
$option = $this->html->createElement('option', __('button.bool_no'));
|
|
|
|
$option->setAttribute('value', 'false');
|
|
|
|
if(!isset($s->value) || $s->value == "false" || $s->value == "0")
|
|
|
|
$option->setAttribute('selected', 'selected');
|
|
|
|
$select->appendChild($option);
|
|
|
|
|
|
|
|
$label = $this->html->createElement('label', $s['label']);
|
|
|
|
$label->setAttribute('for', $s['var']);
|
|
|
|
$container->appendChild($label);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outTextarea($s){
|
2015-11-13 13:37:39 +01:00
|
|
|
$container = $this->html->createElement('div');
|
|
|
|
$this->html->appendChild($container);
|
|
|
|
|
|
|
|
$textarea = $this->html->createElement('textarea');
|
|
|
|
$textarea->setAttribute('type', $s['type']);
|
|
|
|
$textarea->setAttribute('label', $s['label']);
|
|
|
|
$textarea->setAttribute('id', $s['var']);
|
|
|
|
$textarea->setAttribute('name', $s['var']);
|
|
|
|
|
|
|
|
if($s->required)
|
|
|
|
$textarea->setAttribute('required', 'required');
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
foreach($s->children() as $value){
|
|
|
|
if($value->getName() == "value"){
|
2015-11-13 13:37:39 +01:00
|
|
|
$textarea->nodeValue .= $value . "\n";
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
if(empty($textarea->nodeValue)) {
|
|
|
|
$textarea->nodeValue = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$container->appendChild($textarea);
|
|
|
|
|
|
|
|
$label = $this->html->createElement('label', $s['label']);
|
|
|
|
$label->setAttribute('for', $s['var']);
|
|
|
|
$container->appendChild($label);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outInput($s, $type, $multiple){
|
2015-11-13 13:37:39 +01:00
|
|
|
$container = $this->html->createElement('div');
|
|
|
|
$this->html->appendChild($container);
|
|
|
|
|
|
|
|
$input = $this->html->createElement('input');
|
|
|
|
$input->setAttribute('id', $s['var']);
|
|
|
|
$input->setAttribute('name', $s['var']);
|
|
|
|
$input->setAttribute('type', $type);
|
|
|
|
$input->setAttribute('title', $s->desc);
|
|
|
|
$input->setAttribute('type', $s['type']);
|
|
|
|
$input->setAttribute('label', $s['label']);
|
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
if($s->required)
|
2015-11-13 13:37:39 +01:00
|
|
|
$input->setAttribute('required', 'required');
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
foreach($s->children() as $value){
|
|
|
|
if($value->getName() == "value"){
|
|
|
|
$input->setAttribute('value', $value);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
if($s['var'] == 'username')
|
2015-11-13 13:37:39 +01:00
|
|
|
$input->setAttribute('pattern', '[a-z0-9_-]*');
|
|
|
|
|
|
|
|
$container->appendChild($input);
|
|
|
|
|
|
|
|
$label = $this->html->createElement('label', $s['label']);
|
|
|
|
$label->setAttribute('for', $s['var']);
|
|
|
|
$container->appendChild($label);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outHiddeninput($s){
|
2015-11-13 13:37:39 +01:00
|
|
|
$input = $this->html->createElement('input');
|
|
|
|
$input->setAttribute('name', $s['var']);
|
|
|
|
$input->setAttribute('type', 'hidden');
|
|
|
|
$input->setAttribute('value', $s->value);
|
|
|
|
|
|
|
|
$this->html->appendChild($input);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function outList($s, $multiple){
|
2015-11-13 13:37:39 +01:00
|
|
|
$container = $this->html->createElement('div');
|
|
|
|
$this->html->appendChild($container);
|
|
|
|
|
|
|
|
$div = $this->html->createElement('div');
|
|
|
|
$div->setAttribute('class', 'select');
|
|
|
|
$container->appendChild($div);
|
|
|
|
|
|
|
|
$select = $this->html->createElement('select');
|
|
|
|
$select->setAttribute('type', $s['type']);
|
|
|
|
$select->setAttribute('label', $s['label']);
|
|
|
|
$select->setAttribute('id', $s['var']);
|
|
|
|
$select->setAttribute('name', $s['var']);
|
|
|
|
|
|
|
|
if($s->required)
|
|
|
|
$select->setAttribute('required', 'required');
|
|
|
|
|
|
|
|
$div->appendChild($select);
|
2015-08-03 11:35:01 +02:00
|
|
|
|
|
|
|
if(count($s->xpath('option')) > 0){
|
|
|
|
foreach($s->option as $option){
|
2015-11-13 13:37:39 +01:00
|
|
|
if(isset($option->attributes()->label)) {
|
|
|
|
$opt = $this->html->createElement('option', $option->attributes()->label);
|
|
|
|
} else {
|
|
|
|
$opt = $this->html->createElement('option', $option->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$opt->setAttribute('value', $option->value);
|
|
|
|
if(
|
|
|
|
in_array(
|
|
|
|
(string)$opt->nodeValue,
|
|
|
|
array_map(
|
|
|
|
function($sxml) {
|
|
|
|
return (string)$sxml;
|
|
|
|
},
|
|
|
|
$s->xpath('value')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$opt->setAttribute('selected', 'selected');
|
|
|
|
}
|
|
|
|
$select->appendChild($opt);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
foreach($s->value as $option){
|
2015-11-13 13:37:39 +01:00
|
|
|
$option = $this->html->createElement('option', $option);
|
|
|
|
$option->setAttribute('value', $option['label']);
|
|
|
|
$option->setAttribute('selected', 'selected');
|
|
|
|
$select->appendChild($option);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
$label = $this->html->createElement('label', $s['label']);
|
|
|
|
$label->setAttribute('for', $s['var']);
|
|
|
|
$container->appendChild($label);
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
class FormtoXMPP
|
|
|
|
{
|
|
|
|
private $_form;
|
|
|
|
private $_inputs;
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
public function __construct(array $inputs)
|
|
|
|
{
|
|
|
|
$this->_form = new \DOMDocument('1.0', 'UTF-8');
|
|
|
|
$this->_inputs = $inputs;
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
public function appendToX(\DomDocument $dom)
|
|
|
|
{
|
|
|
|
$fields = $this->_form->getElementsByTagName('field');
|
|
|
|
$list = $dom->getElementsByTagName('x');
|
|
|
|
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$field = $dom->importNode($field, true);
|
|
|
|
$list[0]->appendChild($field);
|
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
foreach($this->_inputs as $key => $value) {
|
|
|
|
$container = $this->_form->createElement('container');
|
|
|
|
$this->_form->appendChild($container);
|
|
|
|
|
|
|
|
$field = $this->_form->createElement('field');
|
|
|
|
$container->appendChild($field);
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
$val = $this->_form->createElement('value');
|
|
|
|
$field->appendChild($val);
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
if($value === 'true') {
|
|
|
|
$val->nodeValue = '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
if($value === 'false') {
|
|
|
|
$val->nodeValue = '0';
|
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
|
2015-11-13 13:37:39 +01:00
|
|
|
if(is_bool($value)) {
|
|
|
|
$val->nodeValue = ($value) ? '1' : '0';
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
|
|
|
|
if(empty($val->nodeValue)
|
|
|
|
&& $value !== 'false' // WTF PHP !!!
|
|
|
|
) {
|
|
|
|
$val->nodeValue = trim($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$field->setAttribute('var', trim($key));
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
|
|
|
|
return $this->_form->saveXML();
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|