| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
define('PATFORMS_RENDERER_PHPTAL_ERROR_NO_CLASS', 'Renderer:PhpTal:001'); |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
class patForms_Renderer_PhpTal extends patForms_Renderer |
|---|
| 29 |
{ |
|---|
| 30 |
|
|---|
| 31 |
* Stores forms data |
|---|
| 32 |
* |
|---|
| 33 |
* @access private |
|---|
| 34 |
* @var array |
|---|
| 35 |
*/ |
|---|
| 36 |
var $forms = array(); |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
* Stores the template object |
|---|
| 40 |
* |
|---|
| 41 |
* @access private |
|---|
| 42 |
* @var object PHPTal |
|---|
| 43 |
*/ |
|---|
| 44 |
var $_tmpl = null; |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
* Stores the name of the element attributes that will |
|---|
| 48 |
* be replaced in the template. |
|---|
| 49 |
* |
|---|
| 50 |
* @access private |
|---|
| 51 |
* @var array |
|---|
| 52 |
*/ |
|---|
| 53 |
var $_attributes = array( |
|---|
| 54 |
'label', |
|---|
| 55 |
'title', |
|---|
| 56 |
'display', |
|---|
| 57 |
); |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
* Sets the PHPTal object, that will be used to render the page. |
|---|
| 61 |
* |
|---|
| 62 |
* @access public |
|---|
| 63 |
* @param object PHPTal |
|---|
| 64 |
*/ |
|---|
| 65 |
function setTemplate(&$tmpl) |
|---|
| 66 |
{ |
|---|
| 67 |
$this->_tmpl = &$tmpl; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* Sets a list of attributes to replace in the template |
|---|
| 72 |
* in addition of the default attributes list. |
|---|
| 73 |
* |
|---|
| 74 |
* @access public |
|---|
| 75 |
* @param array The list of attributes |
|---|
| 76 |
* @see $_attributes |
|---|
| 77 |
*/ |
|---|
| 78 |
function setAttributes($attributes) |
|---|
| 79 |
{ |
|---|
| 80 |
$this->_attributes = array_merge( $this->_attributes, $attributes ); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
* Serializes the form elements and sets them to a PHPTal |
|---|
| 85 |
* object. |
|---|
| 86 |
* |
|---|
| 87 |
* Arguments that can be passed: |
|---|
| 88 |
* - template = an instance of PHPTal |
|---|
| 89 |
* - tmplFile = name of the template file |
|---|
| 90 |
* - tmplDir = directory in which templates are stored |
|---|
| 91 |
* |
|---|
| 92 |
* @access public |
|---|
| 93 |
* @param object Reference to the patForms object |
|---|
| 94 |
* @param array optional arguments for the renderer |
|---|
| 95 |
* @return object PHPTal |
|---|
| 96 |
*/ |
|---|
| 97 |
function &render(&$patForms, $args = array()) |
|---|
| 98 |
{ |
|---|
| 99 |
if (is_object($args)) { |
|---|
| 100 |
$args = array('template' => $args); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
if (isset($args['template'])) { |
|---|
| 105 |
$this->_tmpl = $args['template']; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
if ($this->_tmpl === null) { |
|---|
| 110 |
if (!class_exists('PHPTAL')) { |
|---|
| 111 |
return patErrorManager::raiseError(PATFORMS_RENDERER_PHPTAL_ERROR_NO_CLASS, 'No instance of PHPTal has been passed, nor has the class been loaded.'); |
|---|
| 112 |
} |
|---|
| 113 |
$this->_tmpl = new PHPTal(); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
if (isset($args['tmplDir'])) { |
|---|
| 118 |
$this->_tmpl->setTemplateRepository($args['tmplDir']); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
if (isset($args['tmplFile'])) { |
|---|
| 123 |
$result = $this->_tmpl->setTemplate($args['tmplFile']); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
$data = array( |
|---|
| 128 |
'start' => $patForms->serializeStart(), |
|---|
| 129 |
'end' => $patForms->serializeEnd(), |
|---|
| 130 |
'elements' => array() |
|---|
| 131 |
); |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
foreach ($patForms->getElements() as $element) { |
|---|
| 135 |
|
|---|
| 136 |
$ptr = &$data['elements'][$element->getName()]; |
|---|
| 137 |
$ptr['tag'] = $element->serialize(); |
|---|
| 138 |
|
|---|
| 139 |
foreach ($this->_attributes as $name) { |
|---|
| 140 |
$ptr[$name] = (string) $element->getAttribute($name); |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
if ($validationErrors = $patForms->getValidationErrors()) { |
|---|
| 146 |
foreach($validationErrors as $name => $errors) { |
|---|
| 147 |
if (empty($errors)) { |
|---|
| 148 |
continue; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
foreach($errors as $error) { |
|---|
| 152 |
$data['errors'][$name] = $error; |
|---|
| 153 |
$data['errors'][$name]['field'] = $name; |
|---|
| 154 |
$data['errors'][$name]['isFormError'] = ($name == '__form'); |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
$this->forms[$patForms->getName()] = $data; |
|---|
| 160 |
$this->_tmpl->set('forms', $this->forms); |
|---|
| 161 |
|
|---|
| 162 |
return $this->_tmpl; |
|---|
| 163 |
} |
|---|
| 164 |
} |
|---|
| 165 |
?> |
|---|