/**
  * Test With 
  * curl -i -H "Accept: application/json" -X POST -d "artist=Some Name&title=Some Title" http://zf2.local/album-rest
  * (non-PHPdoc)
  * @see \Zend\Mvc\Controller\AbstractRestfulController::create()
  */
 function create($data)
 {
     $album = new Album();
     $album->exchangeArray($data);
     $this->getAlbumTable()->saveAlbum($album);
     return new JsonModel(array("status" => "ok"));
 }
Beispiel #2
0
 public function addAction()
 {
     $form = new AlbumForm();
     // 56
     $form->get('submit')->setValue('Add');
     // 57
     $request = $this->getRequest();
     if ($request->isPost()) {
         // 58
         $album = new Album();
         $form->setInputFilter($album->getInputFilter());
         // 59
         $form->setData($request->getPost());
         // 60
         if ($form->isValid()) {
             // 61
             $album->exchangeArray($form->getData());
             // 62
             $this->getAlbumTable()->saveAlbum($album);
             // 63
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
             // 64
         }
     }
     return array('form' => $form);
     // 65
 }
 public function addAction()
 {
     $form = new AlbumForm();
     $form->get('submit')->setValue('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());
             // Perform album validation
             if ($form->isValidAlbum($album)) {
                 // If the album passes validation then save it
                 $this->getAlbumTable()->saveAlbum($album);
             } else {
                 // Album did not pass validation!
                 // Post message to user, album not allowed!
                 // TODO: Add message to user did not pass validation.
             }
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
         }
     }
     return array('form' => $form);
 }
Beispiel #4
0
 public function testExchangeArraySetsPropertiesToNullIfKeysAreNotPresent()
 {
     $album = new Album();
     $album->exchangeArray(array('artist' => 'some artist', 'id' => 123, 'title' => 'some title'));
     $album->exchangeArray(array());
     $this->assertNull($album->artist, '"artist" should have defaulted to null');
     $this->assertNull($album->id, '"title" should have defaulted to null');
     $this->assertNull($album->title, '"title" should have defaulted to null');
 }
 public function testInputFiltersAreSetCorrectly()
 {
     $album = new Album();
     $inputFilter = $album->getInputFilter();
     $this->assertSame(3, $inputFilter->count());
     $this->assertTrue($inputFilter->has('artist'));
     $this->assertTrue($inputFilter->has('id'));
     $this->assertTrue($inputFilter->has('title'));
 }
 public function create($data)
 {
     $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $album = new Album();
     $album->setArtist($data['artist']);
     $album->setTitle($data['title']);
     $em->persist($album);
     $em->flush();
     return new JsonModel(array('data' => $album->getId()));
 }
 public function create($data)
 {
     $form = new AlbumForm();
     $album = new Album();
     $form->setInputFilter($album->getInputFilter());
     $form->setData($data);
     if ($form->isValid()) {
         $album->exchangeArray($form->getData());
         $id = $this->getAlbumTable()->saveAlbum($album);
     }
     return $this->get($id);
 }
 public function create($data)
 {
     $form = new AlbumForm();
     $album = new Album();
     $form->setInputFilter($album->getInputFilter());
     $form->setData($data);
     if ($form->isValid()) {
         $album->exchangeArray($form->getData());
         $id = $this->getAlbumTable()->saveAlbum($album);
         return new JsonModel(array('code' => 0, 'ret' => $this->get($id)));
     } else {
         return new JsonModel(array('code' => -1, 'ret' => 'invalid'));
     }
 }
 public function testSaveAlbumWillUpdateExistingAlbumsIfTheyAlreadyHaveAnId()
 {
     $albumData = array('id' => 123, 'artist' => 'The Military Wives', 'title' => 'In My Dreams');
     $album = new Album();
     $album->exchangeArray($albumData);
     $resultSet = new ResultSet();
     $resultSet->setArrayObjectPrototype(new Album());
     $resultSet->initialize(array($album));
     $mockTableGateway = $this->getMock('Zend\\Db\\TableGateway\\TableGateway', array('select', 'update'), array(), '', false);
     $mockTableGateway->expects($this->once())->method('select')->with(array('id' => 123))->will($this->returnValue($resultSet));
     $mockTableGateway->expects($this->once())->method('update')->with(array('artist' => 'The Military Wives', 'title' => 'In My Dreams'), array('id' => 123));
     $albumTable = new AlbumTable($mockTableGateway);
     $albumTable->saveAlbum($album);
 }
 public function create($data)
 {
     $form = new AlbumForm();
     $album = new Album();
     $form->setInputFilter($album->getInputFilter());
     $form->setData($data);
     // should set empty id value so form does not mark as invalid if id is not found
     if ($form->isValid()) {
         $album->exchangeArray($form->getData());
         $id = $this->getAlbumTable()->saveAlbum($album);
     }
     // should throw error if form is invalid for validation error, 400 with errors in Reason-Phase
     return new JsonModel(array('data' => $this->getAlbumTable()->getAlbum($id)));
 }
 public function addAction()
 {
     $form = new AlbumForm();
     $form->get('submit')->setValue('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->getAlbumTable()->saveAlbum($album);
             return $this->redirect()->toRoute('album');
         }
     }
     return array('form' => $form);
 }
 public function create($data)
 {
     $form = new AlbumForm();
     $album = new Album();
     $form->setInputFilter($album->getInputFilter());
     $form->setData($data);
     $id = null;
     if ($form->isValid()) {
         $album->exchangeArray($form->getData());
         $id = $this->getAlbumTable()->saveAlbum($album);
         return $this->get($id);
     } else {
         var_dump($form->getData());
         die;
     }
     return new JsonModel(array());
 }
 public function addAction()
 {
     $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->post());
         if ($form->isValid()) {
             $album->populate($form->getData());
             $this->getAlbumTable()->saveAlbum($album);
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
         }
     }
     return array('form' => $form);
 }
 public function addAction()
 {
     $form = new AlbumForm();
     $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->getAlbumTable()->saveAlbum($album, $this->getUserId());
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
         }
     }
     $this->layout()->setVariable('header_title', 'Ajouter un Album');
     return array('form' => $form);
 }
 public function addAction()
 {
     $viewModel = $this->getViewModel();
     $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->getAlbumTable()->saveAlbum($album);
             // Redirect to list of albums
             return $this->redirect()->toRoute('album-front-album');
         }
     }
     return $viewModel->setVariables(array('form' => $form));
 }
 public function addAction()
 {
     $form = new AlbumForm();
     $form->get('submit')->setValue('Add');
     $form->get('title')->setAttribute('class', 'form-control');
     $form->get('artist')->setAttribute('class', 'form-control');
     $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->getAlbumTable()->saveAlbum($album);
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
         }
     }
     return array('form' => $form);
 }
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     //Get the id from url parmas
     if (!$id) {
         return $this->redirect()->toRoute('album', array('action' => 'add'));
         //Wheather id is null then redirect to add function
     }
     try {
         $album = $this->getAlbumTable()->getAlbum($id);
         //get the album data of that particular id
     } catch (Exception $e) {
         return $this->redirect()->toRoute('album', array('action' => 'add'));
     }
     $form = new AlbumForm();
     //Create a Instance of AlbumForm
     $form->bind($album);
     //Bind the album data to the form
     $form->get('submit')->setAttribute('value', 'Edit');
     //Change submit to Edit Button
     $request = $this->getRequest();
     //Generate Request
     if ($request->isPost()) {
         //check wheather request is POST or Not
         $album = new Album();
         //create a instance of Album Model
         $form->setInputFilter($album->getInputFilter());
         //Validation filter set of type album on form
         $form->setData($request->getPost());
         //set data on form
         if ($form->isValid()) {
             //Check wheather form data is valid or not
             $this->getAlbumTable()->saveAlbum($form->getData());
             //Save updated data from form
             //Redirect to list of album
             return $this->redirect()->toRoute('album');
         }
     }
     return array('id' => $id, 'form' => $form);
 }
 public function addAction()
 {
     // Instanciation d'un formulaire, on récupère un bouton submit que l'on renomme "Add"
     $form = new AlbumForm();
     $form->get('submit')->setValue('Add');
     // On regarde si les données on été envoyées
     $request = $this->getRequest();
     if ($request->isPost()) {
         // On créer un nouvel album (une ligne) et on récupère les données envoyées
         $album = new Album();
         $form->setInputFilter($album->getInputFilter());
         $form->setData($request->getPost());
         // Si les données envoyeés sont valides on sauvegarde les données dans le nouvel album
         if ($form->isValid()) {
             $album->exchangeArray($form->getData());
             $this->getAlbumTable()->saveAlbum($album);
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
         }
     }
     return array('form' => $form);
 }
 public function create($data)
 {
     //     	 $response = $this->getResponseWithHeader()
     //     	    	           ->setContent( __METHOD__.'  POST DATA');
     //     	 return $response;
     //         $form = new AlbumForm();
     //         $album = new Album();
     //         $form->setInputFilter($album->getInputFilter());
     //         $form->setData($data);
     //         if ($form->isValid()) {
     //             $album->exchangeArray($form->getData());
     //             $id = $this->getAlbumTable()->saveAlbum($album);
     //         }
     $idsave = '';
     if (!$data['id']) {
         //     		return new JsonModel(array(
         //     		    	'data' => 'Update data',
         //     		   ));
         // Update data
         $album = new Album();
         $album->exchangeArray($data);
         $idsave = $this->getAlbumTable()->saveAlbum($album);
     } else {
         // Insert data
         $album = new Album();
         $album->exchangeArray($data);
         $idsave = $this->getAlbumTable()->saveAlbum($album);
     }
     if ($idsave != 0) {
         $status = 'Success!';
     } else {
         $status = 'Error!';
     }
     $data_resphone = array('id' => $idsave, 'status' => $status);
     return new JsonModel(array('data' => $data_resphone));
 }
Beispiel #20
0
 public function addAction()
 {
     $auth = new AuthenticationService();
     $acl = $this->getAcl();
     if (!$auth->hasIdentity()) {
         return $this->redirect()->toRoute('album', array('action' => 'login'));
     }
     if (!$acl->isAllowed($this->getRole(), null, 'add')) {
         return $this->redirect()->toRoute('album', array('action' => 'index'));
     }
     $form = new AlbumForm();
     $form->get('submit')->setValue('Add');
     $request = $this->getRequest();
     // If form submitted
     if ($request->isPost()) {
         // If back button clicked
         if ($request->getPost('submit2')) {
             return $this->redirect()->toRoute('album');
         }
         // Add new album
         $album = new Album();
         $form->setInputFilter($album->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $album->exchangeArray($form->getData());
             $this->getAlbumTable()->saveAlbum($album);
             $notification = $form->getData();
             $form->get('title')->setValue('');
             $form->get('artist')->setValue('');
         }
     }
     return array('form' => $form, 'notification' => $notification);
 }
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('album', array('action' => 'add'));
     }
     $resp = $this->getRestResponse(sprintf($this->domain . "/album-rest/%s", $id));
     $respData = Json::decode($resp->getBody());
     $album = new Album();
     $album->exchangeArray(get_object_vars($respData->album));
     $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()) {
             $resp = $this->getRestResponse(sprintf($this->domain . "/album-rest/%s", $id), "POST", get_object_vars($form->getData()));
             // Redirect to list of albums
             return $this->redirect()->toRoute('album');
         }
     }
     $model = new ViewModel(array('id' => $id, 'form' => $form));
     //         $model->setTemplate("album/album/edit.phtml");
     return $model;
 }
Beispiel #22
0
 public function indexAction()
 {
     $request = $this->getRequest();
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         if ($request->isPost()) {
             $album = new Album();
             $album->exchangeArray(array('title' => explode('=', explode('&', $request->getContent())[0])[1], 'artist' => explode('=', explode('&', $request->getContent())[1])[1]));
             $this->getAlbumTable()->saveAlbum($album);
             return new JsonModel(array('album' => $album));
         }
         // Get data from table into paginator and conigure the paginator
         $paginator = $this->getAlbumTable()->fetchAll(true);
         $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
         $paginator->setItemCountPerPage(10);
         // Return necessary variables
         return new JsonModel($paginator);
     } else {
         try {
             $album = $this->getAlbumTable()->getAlbum($id);
             $x = 1;
         } catch (\Exception $ex) {
             $x = 0;
             $album = new Album();
             if ($request->isPut()) {
                 $album->exchangeArray(array('title' => explode('=', explode('&', $request->getContent())[0])[1], 'artist' => explode('=', explode('&', $request->getContent())[1])[1]));
                 $this->getAlbumTable()->saveAlbum($album);
             }
             return new JsonModel(array('album' => $album));
         }
         if ($x) {
             if ($request->isDelete()) {
                 $this->getAlbumTable()->getAlbum($id)->title;
                 $this->getAlbumTable()->deleteAlbum($id);
             }
             if ($request->isPut()) {
                 $album->exchangeArray(array('id' => $album->id, 'title' => explode('=', explode('&', $request->getContent())[0])[1], 'artist' => explode('=', explode('&', $request->getContent())[1])[1]));
                 $this->getAlbumTable()->saveAlbum($album);
             }
             return new JsonModel(array('album' => $album));
         }
     }
 }