Класс Document
Объект $Document:
Объект «Форма ввода данных»
Исходный код
class Document extends \DOMDocument { … }
Свойства
$id
$Document->id
Исходный код
var $id;
$Page
$Document->Page
Исходный код
var $Page;
$html
$Document->html
Исходный код
var $html;
$fields
$Document->fields = array();
Список полей формы по именам
Исходный код
var $fields = array();
$fieldList
$Document->fieldList = array();
Список полей формы по номерам
Исходный код
var $fieldList = array();
$error
$Document->error = null;
Исходный код
var $error = null;
Методы
__construct()
new Document($data=null, $Page=null);
Исходный код
function __construct($data = null, $Page = null) {
parent::__construct('1.0', 'UTF-8');
$this->Page = $Page;
if (is_string($data)) {
$r = $this->loadForm($data);
if ($r !== true) {
$this->error = $r;
}
} elseif (is_array($data)) {
$this->html = $data;
foreach ($data as $Field) {
if (is_object($Field)) {
if ($Field->name) {
$this->fields[$Field->name] = $Field;
}
$this->fieldList[] = $Field;
}
}
}
}
App()
$Document->App();
Исходный код
function App() {
return \Cms\Site\App::getInstance();
}
Form()
$Document->Form();
Исходный код
function Form() {
return $this->App()->Form();
}
Page()
$Document->Page();
Исходный код
function Page() {
return $this->Page;
}
id()
$Document->id();
Исходный код
function id() {
static $ids = array();
if ($id = $this->id) {
return $id;
}
if ($this->documentElement) {
if ($id = $this->documentElement->getAttribute('id')) {
if (!isset($ids[$id])) {
$ids[$id] = $id;
}
return $id;
}
}
$id = 'form';
if (!isset($ids[$id])) {
$ids[$id] = $id;
$this->id = $id;
return $id;
}
$n = 2;
while (isset($ids[$id . $n])) {
$n ++;
}
$id = $id . $n;
$ids[$id] = $id;
$this->id = $id;
return $id;
}
loadForm()
$Document->loadForm($xml);
Исходный код
function loadForm($xml) {
$App = \Cms\Site\App::getInstance();
$root = $App->root();
$return = true;
$this->registerNodeClass('DOMElement', '\Cms\Site\Form\Element');
$libxml_use_internal_errors = \libxml_use_internal_errors(true);
\libxml_clear_errors();
if ($xml[0] !== '<') {
if (($_xml = @file_get_contents($xml)) === false) {
$result = parent::load($xml);
} else {
$xml = $_xml;
}
}
if ($result !== false) {
if (strpos($xml, 'http://www.w3.org/1999/XSL/Transform') !== false) {
$xmlDoc = new \DOMDocument;
if ($xmlDoc->loadXML($xml) !== false) {
$nullDoc = new \DOMDocument;
$nullDoc->loadXML('<form/>');
$xsltDoc = new \DOMDocument;
$xsltDoc->loadXML(
<<<X
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" cdata-section-elements="script" />
<xsl:preserve-space elements="*" />
<xsl:template match="/">
<xsl:element name="xsl:stylesheet"><xsl:attribute name="version">1.0</xsl:attribute>
<xsl:for-each select="//xsl:import">
<xsl:element name="xsl:import"><xsl:attribute name="href">.<xsl:value-of select="@href" /></xsl:attribute></xsl:element>
</xsl:for-each>
<xsl:element name="xsl:output"><xsl:attribute name="method">xml</xsl:attribute><xsl:attribute name="encoding">UTF-8</xsl:attribute><xsl:attribute name="omit-xml-declaration">yes</xsl:attribute><xsl:attribute name="cdata-section-elements">script</xsl:attribute></xsl:element>
<xsl:element name="xsl:preserve-space"><xsl:attribute name="elements">*</xsl:attribute></xsl:element>
<xsl:element name="xsl:template"><xsl:attribute name="match">/</xsl:attribute><xsl:apply-templates /></xsl:element>
<xsl:for-each select="//xsl:template">
<xsl:element name="xsl:template"><xsl:copy-of select="@*" /><xsl:apply-templates /></xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="xsl:template"></xsl:template>
<xsl:template match="xsl:import"></xsl:template>
<xsl:template match="*">
<xsl:copy><xsl:copy-of select="@*" /><xsl:apply-templates /></xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('php')">
<xsl:element name="xsl:processing-instruction"><xsl:attribute name="name">php</xsl:attribute><xsl:value-of select="." /></xsl:element>
</xsl:template>
<xsl:template match="xsl:*/processing-instruction('php')">
<xsl:value-of select="php:function('\Cms\Site\Form\Document::xslEval', .)" disable-output-escaping="yes" />
</xsl:template>
</xsl:stylesheet>
X
);
$xsltProc = new \XSLTProcessor();
$xsltProc->registerPHPFunctions();
if ($result !== false) if (!$xsltProc->importStyleSheet($xsltDoc)) $result = false;
if ($result !== false) if (!$xsltProc->importStyleSheet($xsltProc->transformToDoc($xmlDoc))) $result = false;
if ($result !== false) $xml = $xsltProc->transformToXml($nullDoc);
} else {
$result = false;
}
}
if ($result !== false) {
if (strpos($xml, '<' . '?php') !== false) {
$xml = preg_replace('~>\s+<' . '?php~s', '><' . '?php', $xml);
}
$result = parent::loadXML($xml);
}
}
if ($result === false) {
$return = array();
foreach (\libxml_get_errors() as $error) {
$return[] = htmlspecialchars($error->message) . ' in ' . $error->file . ' on line ' . $error->line;
}
} else {
$xpath = new \DOMXPath($this);
$nested = array();
foreach ($xpath->query('//field//*[self::field or self::input or self::select or self::textarea]') as $node) {
$nested[] = $node;
}
foreach ($xpath->query('//*[self::field or self::input or self::select or self::textarea]') as $node) {
if (in_array($node, $nested, true)) continue;
$Field = $this->Form()->loadField($node);
if ($Field->name) {
$this->fields[$Field->name] = $Field;
}
$this->fieldList[] = $Field;
}
}
\libxml_use_internal_errors($libxml_use_internal_errors);
return $return;
}
display()
$Document->display();
Исходный код
function display() {
if (is_array($this->html) && !empty($this->html)) {
$html = array();
foreach ($this->html as $Field) {
if (is_object($Field)) $html[] = $Field->display();
elseif (is_scalar($Field)) $html[] = $Field;
}
return implode('', $html);
}
if ($this->documentElement) {
/**
* @if false
* @var Element $documentElement
* @endif
*/
$documentElement = $this->documentElement;
return $documentElement->display(false);
} elseif ($this->error) {
return $this->Form()->Main()->displayError($this->error);
}
}
keys()
$Document->keys();
Исходный код
function keys() {
return array_keys($this->fields);
}
count()
$Document->count();
Исходный код
function count() {
return count($this->fieldList);
}
xslEval()
Document::xslEval($nodeList);
Исходный код
static function xslEval($nodeList) {
global $_RESULT; if (!is_array($_RESULT)) $_RESULT = array();
$App = \Cms\Site\App::getInstance();
$root = $App->root();
$Conf = $App->Conf();
$Data = $App->Data();
$Auth = $App->Auth();
$Main = $App->Main();
$Form = $App->Form();
$Page = $Main->Page;
$Request = $Main->Request();
$Storage = $Main->Storage();
$Modules = $Main->Modules();
$Display = $Main->Display();
$code = $nodeList[0]->data;
ob_start();
$r = eval($code . ';');
$r = ob_get_clean() . (is_bool($r) ? '' : htmlspecialchars($r));
return $r;
}