Advertisement
Guest User

Untitled

a guest
Mar 13th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2.  
  3. class View_SmartyPlugin_Form {
  4.  
  5.     const
  6.         DEFAULT_FORM_SNIPPET_DIRECTORY  = 'Default',
  7.         DEFAULT_FORM_SNIPPET_TEMPLATE   = 'Snippets/Forms/%s/%s.tpl';
  8.  
  9.     protected
  10.         $forms_stack                = array(),
  11.         $snippet_default_directory  = self::DEFAULT_FORM_SNIPPET_DIRECTORY,
  12.         $snippet_template           = self::DEFAULT_FORM_SNIPPET_TEMPLATE;
  13.  
  14.     public function setFormSnippetTemplate($sprintf_tpl) {
  15.         $this->snippet_template = $sprintf_tpl ?: self::DEFAULT_FORM_SNIPPET_TEMPLATE;
  16.         return $this;
  17.     }
  18.  
  19.     public function setDefaultFormSnippetDirectory($directory) {
  20.         $this->snippet_default_directory = $directory ?: self::DEFAULT_FORM_SNIPPET_DIRECTORY;
  21.         return $this;
  22.     }
  23.  
  24.     public function register($Smarty) {
  25.         $Smarty
  26.             ->registerPlugin('block',    'form',        array($this, 'parseFormBlock'))
  27.             ->registerPlugin('function', 'form_field',  array($this, 'parseFormField'));
  28.     }
  29.  
  30.     public function parseFormBlock($params, $content, Smarty_Internal_Template $template, &$repeat) {
  31.         if (null === $content) {
  32.             return $this->parseFormOpenTag($params, $template);
  33.         } else {
  34.             return $this->parseFormCloseTag($params, $content, $template);
  35.         }
  36.     }
  37.  
  38.     protected function parseFormOpenTag($params, Smarty_Internal_Template $template) {
  39.         if (empty($params['src']) || !is_array($params['src'])) {
  40.             throw new SmartyException("Malformed {form} tag: missing 'src' parameter");
  41.         }
  42.  
  43.         $form_data = $params['src'];
  44.         unset($params['src']);
  45.  
  46.         $snippet_template = sprintf($this->snippet_template, !empty($params['use']) ? $params['use'] : $this->snippet_default_directory, '%s');
  47.         unset($params['use']);
  48.  
  49.         $form = Carcass_ArrayTools::filterAssoc($params, function($k) { return !is_int($k); });
  50.  
  51.         $form['data'] = $form_data;
  52.         $form['snippet_template'] = $snippet_template;
  53.  
  54.         array_push($this->forms_stack, $form);
  55.     }
  56.  
  57.     protected function parseFormCloseTag($params, $content, Smarty_Internal_Template $template) {
  58.         $form = array_pop($this->forms_stack);
  59.         if (!$form) {
  60.             throw new SmartyException("Invalid {/form} tag: no matching opening {form} tag");
  61.         }
  62.         return $this->parseFormSnippetTemplate(
  63.             sprintf($form['snippet_template'], 'form'),
  64.             $template,
  65.             array(
  66.                 'args' => array_filter($form['data'], 'is_scalar'),
  67.                 'content' => $content
  68.             )
  69.         );
  70.     }
  71.  
  72.     public function parseFormField($params, Smarty_Internal_Template $template) {
  73.         $form = $this->getCurrentForm();
  74.         if (!$form) {
  75.             throw new SmartyException("{form_field} tags must be placed inside a {form} tag");
  76.         }
  77.  
  78.         foreach (array('name', 'type') as $required_parameter) {
  79.             if (empty($params[$required_parameter]) || !is_scalar($params[$required_parameter])) {
  80.                 throw new SmartyException("Malformed {form_field} tag: missing '${required_parameter}' parameter");
  81.             }
  82.             $$required_parameter = $params[$required_parameter];
  83.             unset($params[$required_parameter]);
  84.         }
  85.  
  86.         $field_params = Carcass_ArrayTools::filterAssoc($params, function($k) { return !is_int($k); });
  87.  
  88.         if (isset($form['data']['Fields'][$name])) {
  89.             $field_params += $form['data']['Fields'][$name];
  90.         }
  91.  
  92.         $field_params['args'] = array_filter($field_params, 'is_scalar');
  93.  
  94.         return $this->parseFormSnippetTemplate(
  95.             sprintf($form['snippet_template'], $type),
  96.             $template,
  97.             $field_params
  98.         );
  99.     }
  100.  
  101.     protected function getCurrentForm() {
  102.         return end($this->forms_stack);
  103.     }
  104.  
  105.     protected function parseFormSnippetTemplate($filename, Smarty_Internal_Template $template, array $params = array()) {
  106.         return $template->createTemplate($filename, $params)->fetch();
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement