Skip to content

Commit

Permalink
Merge pull request Emagister#11 from rafalgalka/pull-request/captcha-…
Browse files Browse the repository at this point in the history
…decorator

Captcha element/decorator
  • Loading branch information
theUniC committed Mar 31, 2012
2 parents 7884c4f + 6fc91a4 commit de1c82f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Twitter/Bootstrap/Form/Decorator/Captcha.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* @category Forms
* @package Twitter_Bootstrap_Form
* @subpackage Decorator
* @author Rafał Gałka <rafal.galka@modernweb.pl>
*/

/**
* Captcha decorator.
*
* Surrounds <img> with the <label> tag for extra line spacing.
*
* @category Forms
* @package Twitter_Bootstrap_Form
* @subpackage Decorator
* @author Rafał Gałka <rafal.galka@modernweb.pl>
*/
class Twitter_Bootstrap_Form_Decorator_Captcha extends Zend_Form_Decorator_Captcha
{
/**
* Render captcha
*
* @param string $content
* @return string
*/
public function render($content)
{
$element = $this->getElement();
if (!method_exists($element, 'getCaptcha')) {
return $content;
}

$view = $element->getView();
if (null === $view) {
return $content;
}

$placement = $this->getPlacement();
$separator = $this->getSeparator();

$captcha = $element->getCaptcha();
$markup = '<label>' . $captcha->render($view) . '</label>';
switch ($placement) {
case 'PREPEND':
$content = $markup . $separator . $content;
break;
case 'APPEND':
default:
$content = $content . $separator . $markup;
}
return $content;
}

}
66 changes: 66 additions & 0 deletions Twitter/Bootstrap/Form/Element/Captcha.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @category Forms
* @package Twitter_Bootstrap_Form
* @subpackage Element
* @author Rafał Gałka <rafal.galka@modernweb.pl>
*/

/**
* Captcha Form element.
*
* Supports the order of decorator FieldSize and duplicate text box.
*
* @category Forms
* @package Twitter_Bootstrap_Form
* @subpackage Element
* @author Rafał Gałka <rafal.galka@modernweb.pl>
*/
class Twitter_Bootstrap_Form_Element_Captcha extends Zend_Form_Element_Captcha
{
/**
* Render form element
*
* @param Zend_View_Interface $view
* @return string
*/
public function render(Zend_View_Interface $view = null)
{
$captcha = $this->getCaptcha();
$captcha->setName($this->getFullyQualifiedName());

if (!$this->loadDefaultDecoratorsIsDisabled()) {

// fieldSize decorator mus be first
$fieldSize = $this->getDecorator('FieldSize');
$this->removeDecorator('FieldSize');

// duplicated text field generated by ViewHelper decorator
$this->removeDecorator('ViewHelper');

$decorators = $this->getDecorators();
$decorator = $captcha->getDecorator();
$key = get_class($this->_getDecorator($decorator, null));

if (!empty($decorator) && !array_key_exists($key, $decorators)) {
array_unshift($decorators, $decorator);
}

$decorator = array('Captcha', array('captcha' => $captcha));
$key = get_class($this->_getDecorator($decorator[0], $decorator[1]));

if ($captcha instanceof Zend_Captcha_Word && !array_key_exists($key, $decorators)) {
array_unshift($decorators, $decorator);
}

array_unshift($decorators, $fieldSize);
$this->setDecorators($decorators);
}

$this->setValue($this->getCaptcha()->generate());

return parent::render($view);
}

}

0 comments on commit de1c82f

Please sign in to comment.