public function editAction() { $form = new \Admin\Form\SettingForm(); $id = (int) $this->params()->fromRoute('id', 0); if ($id > 0) { $model = $this->getObjectManager()->find($this->_modelName, $id); $form->setData($model->toArray()); $form->setAttribute('action', '/admin/' . $this->_modelAlias . '/edit/' . $id); $form->get('submit')->setAttribute('value', 'Edit'); $model->setUpdatedDate(new \DateTime()); $viewVariables['action'] = 'Edit'; } else { $model = new Setting(); $form->setAttribute('action', '/admin/' . $this->_modelAlias . '/add'); $form->get('submit')->setAttribute('value', 'Add'); $model->setCreatedDate(new \DateTime()); $viewVariables['action'] = 'Add'; } $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); $model->populate($request->getPost()); if ($form->isValid()) { $this->getObjectManager()->persist($model); $this->getObjectManager()->flush(); return $this->redirect()->toUrl('/admin/' . $this->_modelAlias); } } $viewVariables['form'] = $form; $viewVariables['model'] = $model; $this->_view->setTemplate('admin/' . $this->_modelAlias . '/edit'); $this->_view->setVariables($viewVariables); return $this->_view; }
public function indexAction() { $em = $this->getEntityManager(); $setting = $em->getRepository('Blog\\Entity\\Setting')->findOneByState(true); if (!$setting) { //Create default config if not exist $setting = new Setting(); $setting->setConfigName('Default config'); $setting->setPagination(5); $setting->setBackgroundColor('#ffffff'); $setting->setNavbarColor('#ffffff'); $setting->setState(true); } $settingForm = new SettingForm(); $settingForm->setData((array) $setting); $settingForm->get('submit')->setAttribute('value', 'Modifier'); $request = $this->getRequest(); if ($request->isPost()) { $settingForm->setData($request->getPost()); if ($settingForm->isValid()) { $setting = $this->getHydrator()->hydrate($settingForm->getData(), $setting); //Persist and flush entity Setting $em->persist($setting); $em->flush(); $this->getEventManager()->trigger('setting.edit', null, ['user_id' => $this->zfcUserAuthentication()->getIdentity()->getId(), 'user_email' => $this->zfcUserAuthentication()->getIdentity()->getEmail(), 'config_id' => $setting->getId(), 'config_name' => $setting->getConfigName()]); //Add flash message $this->flashMessenger()->addMessage('La configuration a bien été changé.'); //Redirection return $this->redirect()->toRoute('admin/setting'); } } return new ViewModel(['settingForm' => $settingForm]); }