forked from Emagister/zend-form-decorators-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Emagister#11 from rafalgalka/pull-request/captcha-…
…decorator Captcha element/decorator
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |||
} | |||
|
|||
} |