public function validateAction()
 {
     // action body
     $form = new Application_Form_WinsAndWants();
     $form->isValid($this->_getAllParams());
     header('Content-type: application/json');
     echo Zend_Json::encode($form->getMessages());
 }
 public function editAction()
 {
     // action body
     $request = $this->getRequest();
     $id = $request->getParam('id');
     if (isset($id)) {
         $form = new Application_Form_WinsAndWants();
         $form->getElement('created')->removeValidator('Db_NoRecordExists');
         $winsandwantsMapper = new Application_Model_WinsAndWantsMapper();
         $auth = Zend_Auth::getInstance();
         $identity = $auth->getIdentity();
         $row = $winsandwantsMapper->fetchByIdAndUserId($id, $identity->id);
         $form->populate($row);
         if ($request->isPost()) {
             if ($form->isValid($request->getPost())) {
                 $winsandwantsMapper = new Application_Model_WinsAndWantsMapper();
                 $winsandwants = new Application_Model_WinsAndWants($form->getValues());
                 $auth = Zend_Auth::getInstance();
                 $identity = $auth->getIdentity();
                 $winsandwants->setUserId($identity->id);
                 $winsandwants->setId($id);
                 /*
                  * The value of the submit buttons are retrieved through the request object parameters
                  * since they do not exist in the $form object
                  */
                 // Check which button was selected
                 if ($form->save->isChecked()) {
                     $winsandwantsMapper->save($winsandwants);
                     $wins = new Zend_Session_Namespace('wins');
                     $data = array('id' => $winsandwants->getId(), 'created' => $winsandwants->getCreated(), 'wins_01' => $winsandwants->getWins01(), 'wins_02' => $winsandwants->getWins02(), 'wins_03' => $winsandwants->getWins03(), 'wins_04' => $winsandwants->getWins04(), 'wins_05' => $winsandwants->getWins05(), 'wants_01' => $winsandwants->getWants01(), 'wants_02' => $winsandwants->getWants02(), 'wants_03' => $winsandwants->getWants03(), 'wants_04' => $winsandwants->getWants04(), 'wants_05' => $winsandwants->getWants05(), 'emails' => $winsandwants->getEmails(), 'user_id' => $winsandwants->getUserId());
                     $wins->data = $data;
                     $this->_redirector->gotoUrl('/wins-and-wants/success/');
                 } elseif ($form->share->isChecked()) {
                     $winsandwantsMapper->save($winsandwants);
                     $wins = new Zend_Session_Namespace('wins');
                     $data = array('id' => $winsandwants->getId(), 'created' => $winsandwants->getCreated(), 'wins_01' => $winsandwants->getWins01(), 'wins_02' => $winsandwants->getWins02(), 'wins_03' => $winsandwants->getWins03(), 'wins_04' => $winsandwants->getWins04(), 'wins_05' => $winsandwants->getWins05(), 'wants_01' => $winsandwants->getWants01(), 'wants_02' => $winsandwants->getWants02(), 'wants_03' => $winsandwants->getWants03(), 'wants_04' => $winsandwants->getWants04(), 'wants_05' => $winsandwants->getWants05(), 'emails' => $winsandwants->getEmails(), 'user_id' => $winsandwants->getUserId());
                     $wins->data = $data;
                     $emails_str = str_replace(" ", "", $winsandwants->getEmails());
                     // strips whitespace from string
                     $emails = explode(",", $emails_str);
                     // splits string into an array using comma to split
                     $validator = new Zend_Validate_EmailAddress();
                     foreach ($emails as $email) {
                         if (!$validator->isValid($email)) {
                             array_shift($emails);
                             // Remove invalid emails
                         }
                     }
                     $mail = new Zend_Mail();
                     $mail->setFrom($identity->email, $identity->username);
                     $mail->addTo($emails);
                     $mail->setSubject('Sharing my winsandwants');
                     try {
                         $shareMapper = new Application_Model_ShareMapper();
                         $share = new Application_Model_Share();
                         $share->setWinsAndWantsId($winsandwants->getId());
                         $share->setWinsAndWantsUserId($winsandwants->getUserId());
                         $checked_wins = array();
                         $checked_wins[] = $winsandwants->getWins01cb() ? '1' : null;
                         $checked_wins[] = $winsandwants->getWins02cb() ? '2' : null;
                         $checked_wins[] = $winsandwants->getWins03cb() ? '3' : null;
                         $checked_wins[] = $winsandwants->getWins04cb() ? '4' : null;
                         $checked_wins[] = $winsandwants->getWins05cb() ? '5' : null;
                         $checked_wins = array_filter($checked_wins);
                         $shared_wins = implode(',', $checked_wins);
                         $share->setSharedWins($shared_wins);
                         $checked_wants = array();
                         $checked_wants[] = $winsandwants->getWants01cb() ? '1' : null;
                         $checked_wants[] = $winsandwants->getWants02cb() ? '2' : null;
                         $checked_wants[] = $winsandwants->getWants03cb() ? '3' : null;
                         $checked_wants[] = $winsandwants->getWants04cb() ? '4' : null;
                         $checked_wants[] = $winsandwants->getWants05cb() ? '5' : null;
                         $checked_wants = array_filter($checked_wants);
                         $shared_wants = implode(',', $checked_wants);
                         $share->setSharedWants($shared_wants);
                         $sharePrimaryKeys = $shareMapper->save($share);
                     } catch (Exception $e) {
                         throw new Exception($e->getMessage(), $e->getCode());
                     }
                     $txt = 'http://winsandwants.com/wins-and-wants/share/id/' . $sharePrimaryKeys['id'];
                     $mail->setBodyText($txt, 'UTF-8');
                     $mail->send();
                     $this->_redirector->gotoUrl('/wins-and-wants/success/');
                 } elseif ($form->print->isChecked()) {
                     $wins = new Zend_Session_Namespace('wins');
                     $data = array('Id' => $winsandwants->getId(), 'Created' => $winsandwants->getCreated(), 'Wins 01' => $winsandwants->getWins01(), 'Wins 02' => $winsandwants->getWins02(), 'Wins 03' => $winsandwants->getWins03(), 'Wins 04' => $winsandwants->getWins04(), 'Wins 05' => $winsandwants->getWins05(), 'Wants 01' => $winsandwants->getWants01(), 'Wants 02' => $winsandwants->getWants02(), 'Wants 03' => $winsandwants->getWants03(), 'Wants 04' => $winsandwants->getWants04(), 'Wants 05' => $winsandwants->getWants05(), 'Emails' => $winsandwants->getEmails(), 'User Id' => $winsandwants->getUserId());
                     $wins->data = $data;
                     $this->_redirector->gotoUrl('/wins-and-wants/print/');
                 }
             }
         }
         $this->view->form = $form;
     } else {
         $this->_redirector->gotoUrl('/dashboard');
     }
 }