* @revision 01 * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons Attribution-Share Alike 3.0 Germany * @homepage http://oss.tiggerswelt.net/xmpp * @copyright Copyright © 2009 tiggersWelt.net */ require_once ('tiggerXMPP/extension.php'); require_once ('tiggerXMPP/xep/0004/form.php'); /** * Basic interface to define Data Forms * * @package tiggerXMPP * @class XMPP_Data_Form * @todo Add support for reported / list of results */ class tiggerXMPP_Data_Form extends tiggerXMPP_Extension { const XEP_NAMESPACE = 'jabber:x:data'; const TYPE_FORM = "form"; const TYPE_SUBMIT = "submit"; const TYPE_RESULT = "result"; const TYPE_CANCEL = "cancel"; // A describtive text of what to do with this form (optional) public $Instructions = ""; // Title of this form (optional) public $Title = ""; // Type of this form (see constants) private $Type = XMPP_Data_Form::TYPE_FORM; // Internal Field-storage private $Fields = array (); // {{{ __construct /** * Create a new object and pre-fill it * * @param string $Title (optional) * @param stirng $Instructions (optional) * @param enum $Type (optional) * * @access public * @return void */ public function __construct ($Title = "", $Instructions = "", $Type = self::TYPE_FORM) { $this->Title = $Title; $this->Instructions = $Instructions; $this->setType ($Type); } // }}} // {{{ fromXML /** * Construct a form-structure from an XML-Packet * * @param object $Packet * * @access public * @return object */ public static function fromXML ($Packet) { // Validate the incoming packet if (($Packet->getName () != 'x') || ($Packet->getNamespace () != self::XEP_NAMESPACE)) return false; if (is_object ($t = $Packet->getSubtagByName ('title'))) $Title = $t->getValue (); else $Title = ''; if (is_object ($t = $Packet->getSubtagByName ('instructions'))) $Instructions = $t->getValue (); else $Instructions = ''; $Form = new tiggerXMPP_Data_Form ($Title, $Instructions, $Packet->getAttribute ('type')); if ($Packet->haveSubtags ('field')) foreach ($Packet->getSubtagsByName ('field') as $Field) { if (is_object ($t = $Field->getSubtagByName ('title'))) $Title = $t->getValue (); else $Title = ''; if (is_object ($t = $Field->getSubtagByName ('discription'))) $Description = $t->getValue (); else $Description = ''; $Form->addField ( $Field->getAttribute ('var'), $Field->getAttribute ('type'), $Title, $Description, ($Field->getAttribute ('required', false) ? true : false) ); foreach ($Field->getSubtagsByName ('option') as $Option) $Field->addOption ($Option->label, $Option->value->getValue ()); foreach ($Field->getSubtagsByName ('value') as $Value) $Field->addValue ($Value->getValue ()); } return $Form; } // }}} // {{{ setType /** * Set type of this form * * @param enum $Type * * @access public * @return bool */ public function setType ($Type) { if (!in_array ($Type, array (self::TYPE_FORM, self::TYPE_SUBMIT, self::TYPE_RESULT, self::TYPE_CANCEL))) return false; $this->Type = $Type; return true; } // }}} // {{{ xmlObject /** * Generate an XML-Object from this object * * @param object $Parent * * @access public * @return object */ public function xmlObject ($Parent) { $Tag = new phpEvents_Socket_Stream_XML_Tag ('x', $Parent); $Tag->setNamespace (self::XEP_NAMESPACE); $Tag->setAttribute ('type', $this->Type); $Title = new phpEvents_Socket_Stream_XML_Tag ('title', $Tag); $Title->setValue ($this->Value); if ((strlen ($this->Instructions) > 0) && ($this->Type == self::TYPE_FORM)) { $Instructions = new phpEvents_Socket_Stream_XML_Tag ('instructions', $Tag); $Instructions->setValue ($this->Instructions); } if (($this->Type != self::TYPE_CANCEL) && (($this->Type != self::TYPE_RESULT) || (!$this->MultiResult))) foreach ($this->Fields as $Field) $Field->xmlObject ($Tag); elseif (($this->Type == self::TYPE_RESULT) && $this->MultiResult) $Report = new phpEvents_Socket_Stream_XML_Tag ('report', $Tag); if (($Parent->getName () == 'iq') && (($this->Type == self::TYPE_SUBMIT) || ($this->Type == self::TYPE_CANCEL))) $Parent->setAttribute ('type', 'set'); return $Tag; } // }}} // {{{ addField /** * Create a new field in this form * * @param mixed $Field Name of field or existing handle * @param enum $Type Type of Field * @param string $Title (optional) * @param string $Description (optional) * @param bool $Required (optional) * * @access public * @return object */ public function addField ($Field, $Type, $Title = "", $Description = "", $Required = false) { if ($Field === "") return false; if (!is_object ($Field)) $Field = new tiggerXMPP_Data_Field ($Field, $Type, $Title, $Description, $Required); # TODO: Check for a duplicate name? $this->Fields [] = $Field; return $Field; } // }}} } // Rename the class just to match our own standard ;-) /** * Basic interface to define Data Forms * * @package tiggerXMPP * @class XEP_0004 * @see XMPP_Data_Form */ class tiggerXMPP_XEP_0004 extends tiggerXMPP_Data_Form { } /** * Describe simple Data-Fields in Forms * * @package tiggerXMPP * @class XMPP_Data_Field */ class tiggerXMPP_Data_Field { const TYPE_BOOLEAN = "boolean"; const TYPE_FIXED = "fixed"; const TYPE_HIDDEN = "hidden"; const TYPE_JIDS = "jid-multi"; const TYPE_JID = "jid-signle"; const TYPE_LIST = "list-multi"; const TYPE_SELECT = "list-single"; const TYPE_TEXT = "text-multi"; const TYPE_INPUT = "text-single"; const TYPE_PASSWORD = "text-private"; public $Name = ""; public $Title = ""; // aka label public $Description = ""; public $Required = false; private $Type = tiggerXMPP_Data_Field::TYPE_BOOLEAN; private $Values = array (); private $Options = array (); private $sValues = array (); private $sOptions = array (); // {{{ __construct /** * Create a new field in this form * * @param mixed $Field Name of field or existing handle * @param enum $Type Type of Field * @param string $Title (optional) * @param string $Description (optional) * @param bool $Required (optional) * * @access public * @return void */ public function __construct ($Name, $Type, $Title = "", $Description = "", $Required = false) { $this->Name = $Name; $this->Title = $Title; $this->Description = $Description; $this->Required = $Required; $this->setType ($Type); } // }}} // {{{ addOption /** * Add an option to this field * * @param string $Title * @param string $Value * @param bool $Selected (optional) * * @access public * @return void */ public function addOption ($Title, $Value, $Selected = false) { $this->Options [$Value] = $Title; $this->sOptions [$Value] = $Selected; } // }}} // {{{ addValue /** * Add a value to this field * * @param string $Value * @param bool $Selected (optional) * * @access public * @return void */ public function addValue ($Value, $Selected = false) { $this->Values [] = $Value; $this->sValues [] = $Selected; } // }}} // {{{ setType /** * Set Type of this field * * @param enum $Type * * @access public * @return bool */ public function setType ($Type) { if (!in_array ($Type, array (self::TYPE_BOOLEAN, self::TYPE_FIXED, self::TYPE_HIDDEN, self::TYPE_JIDS, self::TYPE_JID, self::TYPE_LIST, self::TYPE_SELECT, self::TYPE_TEXT, self::TYPE_INPUT, self::TYPE_PASSWORD))) return false; $this->Type = $Type; return true; } // }}} // {{{ xmlObject /** * Create an XML-Object * * @param object $Parent * * @access public * @return object */ public function xmlObject ($Parent) { if (strlen ($this->Name) == 0) return; $Tag = new phpEvents_Socket_Stream_XML_Tag ('field', $Parent); $Tag->setAttribute ('var', $this->Name); $Tag->setAttribute ('type', $this->Type); if (strlen ($this->Title) > 0) $Tag->setAttribute ('label', $this->Title); if (strlen ($this->Description) > 0) $Desc = new phpEvents_Socket_Stream_XML_Tag ('desc', $Tag, $this->Description); if ($this->Required && ($Parent->getAttribute ('type') == tiggerXMPP_Data_Form::TYPE_FORM)) new phpEvents_Socket_Stream_XML_Tag ('required', $Tag); $All = ($Parent->getAttribute ('type') == tiggerXMPP_Data_Form::TYPE_FORM); if (count ($this->Values) > 0) { $Tag->value = array (); foreach ($this->Values as $ID=>$Value) if ($All || $sValues [$ID]) new phpEvents_Socket_Stream_XML_Tag ('value', $Tag, $Value); } if ((count ($this->Options) > 0) && (in_array ($this->Type, array (self::TYPE_LIST, self::TYPE_SELECT)))) { $Tag->option = array (); foreach ($this->Options as $Val=>$Option) if ($All || $sOptions [$Val]) { $Subtag = new phpEvents_Socket_Stream_XML_Tag ('option', $Tag, $Val); $Subtag->setAttribute ('label', $Option); } } return $Tag; } // }}} } ?>