Created
December 13, 2012 16:31
-
-
Save anonymous/4277702 to your computer and use it in GitHub Desktop.
Fieldset for Forms (aka DisplayGroup) the FormViewHelper is for my needs, you may need to change it.
This file contains hidden or 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
<?php | |
namespace cwdAdmin\Form; | |
/** | |
* DisplayGroup | |
* | |
* @category cwdAdmin | |
* @package CWD | |
* @subpackage Form | |
* | |
*/ | |
class DisplayGroup | |
{ | |
/** | |
* | |
* @var array of Elements in this displaygroup | |
*/ | |
protected $elements = array(); | |
/** | |
* | |
* @var label for displaygroup | |
*/ | |
protected $label = null; | |
/** | |
* | |
* @var id of displaygroup | |
*/ | |
protected $id = null; | |
/** | |
* | |
* @param string $id | |
* @param string $label | |
*/ | |
public function __construct($id, $label) | |
{ | |
$this->setId($id); | |
$this->setLabel($label); | |
} | |
/** | |
* | |
* @return the $elements | |
*/ | |
public function getElements () | |
{ | |
return $this->elements; | |
} | |
/** | |
* Add Element (by id) to this displayGroup | |
* @param string $element | |
* @return \cwdAdmin\Form\DisplayGroup | |
*/ | |
public function add($element) | |
{ | |
$this->elements[] = $element; | |
return $this; | |
} | |
/** | |
* | |
* @return the $label | |
*/ | |
public function getLabel () | |
{ | |
return $this->label; | |
} | |
/** | |
* | |
* @return the $id | |
*/ | |
public function getId () | |
{ | |
return $this->id; | |
} | |
/** | |
* set Elements | |
* | |
* @param array $elements | |
* @return \cwdAdmin\Form\DisplayGroup | |
*/ | |
public function setElements ($elements) | |
{ | |
$this->elements = $elements; | |
return $this; | |
} | |
/** | |
* Set Label | |
* | |
* @param string $label | |
* @return \cwdAdmin\Form\DisplayGroup | |
*/ | |
public function setLabel ($label) | |
{ | |
$this->label = $label; | |
return $this; | |
} | |
/** | |
* set ID | |
* | |
* @param string $id | |
* @return \cwdAdmin\Form\DisplayGroup | |
*/ | |
public function setId ($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace cwdAdmin\Form; | |
use Zend\Form\Form as ZendForm; | |
/** | |
* Form | |
* | |
* @category cwdAdmin | |
* @package CWD | |
* @subpackage Form | |
**/ | |
abstract class Form extends ZendForm | |
{ | |
/** | |
* @var array; | |
*/ | |
protected $displaygroup = array(); | |
/** | |
* Add DisplayGroup to this Form | |
* @param DisplayGroup $dg | |
* @return \cwdAdmin\Form\Form | |
*/ | |
public function addDisplayGroup(DisplayGroup $dg) | |
{ | |
$this->displaygroup[] = $dg; | |
return $this; | |
} | |
/** | |
* Get Displaygroups | |
* @return array | |
*/ | |
public function getDisplayGroups() | |
{ | |
return $this->displaygroup; | |
} | |
} |
This file contains hidden or 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
<?php | |
/** | |
* cwdAdmin - Zend Framwork 2 Admin Template | |
* | |
* @author Ludwig Ruderstaller | |
* @copyright Copyright (c)2011 CWD - Customized Web Development. (http://www.cwd.at) | |
* @category CWD | |
* @license http://framework.zend.com/license/new-bsd New BSD License | |
* | |
*/ | |
namespace cwdAdmin\View\Helper; | |
use cwdAdmin\Form\Footer; | |
use Zend\Form\ElementInterface; | |
use Zend\Form\FieldsetInterface; | |
use Zend\View\Helper\AbstractHelper; | |
use cwdAdmin\Form\FormInterface; | |
/** | |
* Form | |
* | |
* @category cwdAdmin | |
* @package CWD | |
* @subpackage ViewHelper | |
**/ | |
class Form extends AbstractHelper | |
{ | |
/** | |
* @var FormElement | |
*/ | |
protected $elementHelper; | |
protected $rendered = array(); | |
/** | |
* Form | |
* @param FormInterface $form | |
* @return string | |
*/ | |
public function __invoke(FormInterface $form) | |
{ | |
#echo "Form hat "; | |
#echo count($form->getElements()); | |
#echo " elemente<br /><br />"; | |
$form->setAttribute('enctype', 'multipart/form-data'); | |
$html = $this->getView()->form()->openTag($form); | |
if (count ($form->getDisplayGroups()) > 0) { | |
foreach ($form->getDisplayGroups() as $displayGroup) { | |
$elements = $displayGroup->getElements(); | |
$html .= sprintf('<fieldset class="fieldset fields-list"><legend class="legend">%s</legend>', $this->getView()->translate($displayGroup->getLabel(), 'admin')); | |
foreach ($elements as $elementName) { | |
try{ | |
$element = $form->get($elementName); | |
$html .= $this->render($element); | |
} catch (\Exception $e) { | |
// Element not found, ignore | |
} | |
} | |
$html .= '</fieldset>'; | |
} | |
} else { | |
$html .= $this->renderElements($form); | |
} | |
$footer = $form->add(array( | |
'type' => 'cwdAdmin\Form\Footer', | |
'name' => 'footer' | |
)); | |
$html .= $this->renderFooter($form->get('footer')); | |
$html .= $this->getView()->form($form)->closeTag(); | |
return $html; | |
} | |
public function renderElements($form) | |
{ | |
$html = ''; | |
foreach($form as $entry) { | |
if($entry instanceof Footer) { | |
$html .= $this->renderFooter($entry); | |
} elseif($entry instanceof FieldsetInterface) { | |
$fieldset = $entry; | |
if ($fieldset->getLabel()) { | |
$html .= sprintf('<fieldset class="fieldset fields-list '.$fieldset->getAttribute('class').'"><legend class="legend">%s</legend>', $fieldset->getLabel()); | |
} | |
foreach ($fieldset->getElements() as $element) { | |
$html .= $this->render($element); | |
} | |
if ($fieldset->getLabel()) { | |
$html .= '</fieldset>'; | |
} | |
} elseif($entry instanceof ElementInterface) { | |
$html .= $this->render($entry); | |
} | |
} | |
return $html; | |
} | |
public function renderFooter($fieldset) | |
{ | |
$html = '<div class="align-center button-height">'; | |
foreach ($fieldset->getElements() as $element) { | |
$html .= $this->render($element); | |
} | |
$html .= '</div>'; | |
return $html; | |
} | |
public function render($element) | |
{ | |
$html = ''; | |
$type = $element->getAttribute('type'); | |
if (!$element->getAttribute('id')) { | |
$element->setAttribute('id', $element->getName()); | |
} | |
switch($type) { | |
case 'submit': | |
case 'reset': | |
case 'button': | |
$html .= $this->getView()->adminFormElementButton($element); | |
break; | |
case 'select': | |
$html .= $this->getView()->adminFormElementSelect($element); | |
break; | |
case 'datepicker': | |
$html .= $this->getView()->adminFormElementDatepicker($element); | |
break; | |
case 'bool': | |
$element->setAttribute('type', 'checkbox'); | |
$html .= $this->getView()->adminFormElementBool($element); | |
break; | |
default: | |
$html .= $this->getView()->adminFormElementInput($element); | |
break; | |
} | |
return $html; | |
} | |
/** | |
* Retrieve the FormElement helper | |
* | |
* @return FormElement | |
*/ | |
protected function getElementHelper() | |
{ | |
if ($this->elementHelper) { | |
return $this->elementHelper; | |
} | |
if (method_exists($this->view, 'plugin')) { | |
$this->elementHelper = $this->view->plugin('form_element'); | |
} | |
if (!$this->elementHelper instanceof FormElement) { | |
$this->elementHelper = new FormElement(); | |
} | |
return $this->elementHelper; | |
} | |
} |
This file contains hidden or 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
<? echo $this->adminForm($form); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment