Admin_Form_Theme_OutputSettings::setValues PHP Method

setValues() public method

Set values for the form elements
public setValues ( array $defaults, array $values = null )
$defaults array Default template files
$values array Default input values
    public function setValues($defaults, $values = null)
    {
        foreach ($this->getElements() as $elem) {
            /* @var $elem Zend_Form_Element_Select */
            if ($elem instanceof Zend_Form_Element_Select) {
                $elem->addMultiOptions($defaults)->setValue($values[$elem->getName()]);
            } elseif (isset($values[$elem->getName()])) {
                $elem->setValue($values[$elem->getName()]);
            }
        }
    }

Usage Example

 /**
  *
  * called by wizard template action
  */
 public function outputEditAction()
 {
     $thmServ = $this->getThemeService();
     $translator = \Zend_Registry::get('container')->getService('translator');
     // getting the theme entity
     $themeId = $this->_request->getParam('themeid');
     $theme = $thmServ->findById($themeId);
     // getting selected output
     $outputId = $this->_request->getParam('outputid');
     $output = $this->getOutputService()->getById($outputId);
     /* @var $settings Newscoop\Entity\Output */
     $templates = array();
     // getting all available templates
     foreach ($thmServ->getTemplates($theme) as $tpl) {
         /* @var $tpl Newscoop\Entity\Resource */
         $templates[$tpl->getPath()] = $tpl->getName();
         // couldn't get id cause it's null :) :) :)
     }
     // making the form
     $outputForm = new Admin_Form_Theme_OutputSettings();
     $outputForm->setAction($this->_helper->url('output-edit'));
     // getting theme's output settings
     $settings = $thmServ->findOutputSetting($theme, $output);
     /* @var $settings Newscoop\Entity\OutputSettings */
     $settingVals = array("frontpage" => null, "articlepage" => null, "sectionpage" => null, "errorpage" => null, "outputid" => $outputId, "themeid" => $themeId);
     if ($settings instanceof OutputSettings) {
         $settingVals["frontpage"] = $settings->getFrontPage();
         $settingVals["articlepage"] = $settings->getArticlePage();
         $settingVals["sectionpage"] = $settings->getSectionPage();
         $settingVals["errorpage"] = $settings->getErrorPage();
     }
     $outputForm->setValues($templates, $settingVals);
     try {
         // @todo maybe implement this a little smarter, little less code?
         if ($this->_request->isPost()) {
             if ($outputForm->isValid($this->_request->getPost())) {
                 $settings->setFrontPage(new Resource($outputForm->getValue('frontpage')));
                 $settings->setSectionPage(new Resource($outputForm->getValue('sectionpage')));
                 $settings->setArticlePage(new Resource($outputForm->getValue('articlepage')));
                 $settings->setErrorPage(new Resource($outputForm->getValue('errorpage')));
                 $this->getThemeService()->assignOutputSetting($settings, $theme);
                 $msg = $translator->trans('Theme settings saved.', array(), 'themes');
                 $this->view->success = $msg;
                 $this->_helper->flashMessenger($msg);
             } else {
                 throw new \Exception();
             }
         }
     } catch (\Exception $e) {
         //            $this->_helper->flashMessenger( ( $this->view->error = $translator->trans( 'Saving settings failed.' ) ) );
     }
     $this->view->outputForm = $outputForm;
     // disabling layout for ajax and hide the submit button
     if ($this->_request->isXmlHttpRequest()) {
         $this->_helper->layout->disableLayout();
         $outputForm->getElement('submit')->clearDecorators()->setAttrib('style', 'display:none');
     }
 }
Admin_Form_Theme_OutputSettings