public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('album', array('action' => 'add')); } // Get the Album with the specified id. An exception is thrown // if it cannot be found, in which case go to the index page. try { $album = $this->getServiceLocator()->get('Album\\Model\\AlbumTable')->getAlbum($id); } catch (\Exception $ex) { return $this->redirect()->toRoute('album', array('action' => 'index')); } $form = new AlbumForm(); $form->bind($album); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($album->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getServiceLocator()->get('Album\\Model\\AlbumTable')->saveAlbum($album); // Redirect to list of albums return $this->redirect()->toRoute('album'); } } return array('id' => $id, 'form' => $form); }
public function addAction() { $auth = new AuthenticationService(); if (!$auth->hasIdentity()) { return $this->redirect()->toRoute('home'); } $form = new AlbumForm(); //$form=new AlbumForm(); $form->get('submit')->setAttribute('value', 'Add'); $request = $this->getRequest(); $sms = $this->getServiceLocator(); $categoryTable = $sms->get('Application\\Model\\CategoryTable'); $categorys = $categoryTable->fetchAllCategory(); if ($request->isPost()) { $album = new Album(); $form->setInputFilter($album->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $album->exchangeArray($form->getData()); $this->getAdminTable()->saveAlbum($album); $this->flashmessenger()->addMessage('Album added successfully'); return $this->redirect()->toRoute('album'); } } $cat = array(0 => '--Select Category--'); foreach ($categorys as $category) { $cat[(int) $category->id] = $category->name; } //print_r($cat); die; $viewModel = new ViewModel(array('form' => $form, 'categorys' => $cat)); $viewModel->setTerminal(true); return $viewModel; }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); // id that we editing, defaults to zero $form = new AlbumForm(); // form used for the edit $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $album = $em->getRepository('Application\\Entity\\Album')->find($id); // if we do not have an entry for the album, i.e id not found or not defined // send them to add if (!$album) { return $this->redirect()->toRoute('admin/album', array('action' => 'add')); } // setup form // (validation, data and button) $form->setInputFilter($album->getInputFilter())->setData($album->toArray())->get('submit')->setAttribute('value', 'Edit'); // process a submission if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); // set the form with the submitted values // is valid? if ($form->isValid()) { $album->setOptions($form->getData()); // set the data $album->id = $id; $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); // entity manager $em->persist($album); // set data $em->flush(); // save // set messages //$this->flashMessenger()->addMessage('You must do something.'); //$this->flashMessenger()->addMessage(array('alert-info'=>'Soon this changes.')); $this->flashMessenger()->addMessage(array('alert-success' => 'Updated!')); //$this->flashMessenger()->addMessage(array('alert-error'=>'Sorry, Error.')); // Redirect to list of albums return $this->redirect()->toRoute('admin/album'); } else { //$this->flashMessenger()->addMessage(array('alert-error'=>'Form error')); } } return array('id' => $id, 'form' => $form); }
public function addAction() { $auth = new AuthenticationService(); if (!$auth->hasIdentity()) { return $this->redirect()->toRoute('home'); } $form = new AlbumForm(); $form->get('submit')->setAttribute('value', 'Add'); $request = $this->getRequest(); if ($request->isPost()) { $album = new Album(); $form->setInputFilter($album->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $album->exchangeArray($form->getData()); $this->getAdminTable()->saveAlbum($album); return $this->redirect()->toRoute('album'); } } $viewModel = new ViewModel(array('form' => $form)); $viewModel->setTerminal(true); return $viewModel; }
public function update($id, $data) { // identical to create, the only change is the id is manually added after // getting the form data, this is becuase the form does not have the id $form = new AlbumForm(); $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $album = $em->getRepository('Application\\Entity\\Album')->find($id); $form->setInputFilter($album->getInputFilter()); $form->setData($data); // album was found, add the form data, and reinstate the id if ($album && $form->isValid()) { $album->setOptions($form->getData()); // set the data $album->id = $id; $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); // entity manager $em->persist($album); // set data $em->flush(); // save } return new JsonModel(array('data' => $album->toArray())); }