Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #11 from rafalgalka/pull-request/captcha-decorator

Captcha element/decorator
  • Loading branch information...
commit de1c82f10901b2d8b7184e4353a1775bf1662fa9 2 parents 7884c4f + 6fc91a4
@theUniC theUniC authored
View
56 Twitter/Bootstrap/Form/Decorator/Captcha.php
@@ -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;
+ }
+
+}
View
66 Twitter/Bootstrap/Form/Element/Captcha.php
@@ -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);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.