Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class View_SmartyPlugin_Form {
- const
- DEFAULT_FORM_SNIPPET_DIRECTORY = 'Default',
- DEFAULT_FORM_SNIPPET_TEMPLATE = 'Snippets/Forms/%s/%s.tpl';
- protected
- $forms_stack = array(),
- $snippet_default_directory = self::DEFAULT_FORM_SNIPPET_DIRECTORY,
- $snippet_template = self::DEFAULT_FORM_SNIPPET_TEMPLATE;
- public function setFormSnippetTemplate($sprintf_tpl) {
- $this->snippet_template = $sprintf_tpl ?: self::DEFAULT_FORM_SNIPPET_TEMPLATE;
- return $this;
- }
- public function setDefaultFormSnippetDirectory($directory) {
- $this->snippet_default_directory = $directory ?: self::DEFAULT_FORM_SNIPPET_DIRECTORY;
- return $this;
- }
- public function register($Smarty) {
- $Smarty
- ->registerPlugin('block', 'form', array($this, 'parseFormBlock'))
- ->registerPlugin('function', 'form_field', array($this, 'parseFormField'));
- }
- public function parseFormBlock($params, $content, Smarty_Internal_Template $template, &$repeat) {
- if (null === $content) {
- return $this->parseFormOpenTag($params, $template);
- } else {
- return $this->parseFormCloseTag($params, $content, $template);
- }
- }
- protected function parseFormOpenTag($params, Smarty_Internal_Template $template) {
- if (empty($params['src']) || !is_array($params['src'])) {
- throw new SmartyException("Malformed {form} tag: missing 'src' parameter");
- }
- $form_data = $params['src'];
- unset($params['src']);
- $snippet_template = sprintf($this->snippet_template, !empty($params['use']) ? $params['use'] : $this->snippet_default_directory, '%s');
- unset($params['use']);
- $form = Carcass_ArrayTools::filterAssoc($params, function($k) { return !is_int($k); });
- $form['data'] = $form_data;
- $form['snippet_template'] = $snippet_template;
- array_push($this->forms_stack, $form);
- }
- protected function parseFormCloseTag($params, $content, Smarty_Internal_Template $template) {
- $form = array_pop($this->forms_stack);
- if (!$form) {
- throw new SmartyException("Invalid {/form} tag: no matching opening {form} tag");
- }
- return $this->parseFormSnippetTemplate(
- sprintf($form['snippet_template'], 'form'),
- $template,
- array(
- 'args' => array_filter($form['data'], 'is_scalar'),
- 'content' => $content
- )
- );
- }
- public function parseFormField($params, Smarty_Internal_Template $template) {
- $form = $this->getCurrentForm();
- if (!$form) {
- throw new SmartyException("{form_field} tags must be placed inside a {form} tag");
- }
- foreach (array('name', 'type') as $required_parameter) {
- if (empty($params[$required_parameter]) || !is_scalar($params[$required_parameter])) {
- throw new SmartyException("Malformed {form_field} tag: missing '${required_parameter}' parameter");
- }
- $$required_parameter = $params[$required_parameter];
- unset($params[$required_parameter]);
- }
- $field_params = Carcass_ArrayTools::filterAssoc($params, function($k) { return !is_int($k); });
- if (isset($form['data']['Fields'][$name])) {
- $field_params += $form['data']['Fields'][$name];
- }
- $field_params['args'] = array_filter($field_params, 'is_scalar');
- return $this->parseFormSnippetTemplate(
- sprintf($form['snippet_template'], $type),
- $template,
- $field_params
- );
- }
- protected function getCurrentForm() {
- return end($this->forms_stack);
- }
- protected function parseFormSnippetTemplate($filename, Smarty_Internal_Template $template, array $params = array()) {
- return $template->createTemplate($filename, $params)->fetch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement