public function goalAction() { // get the params to get the goal // display the form to display the friends check box // create a form // process the form // insert multiple values / intelligent save checks for existing rows // let's take it one step at a time so that we make progress // create the link from the goal/view page $request = $this->getRequest(); $goal_id = $request->getParam('id'); $form = new Application_Form_Share(); if ($request->isPost()) { if ($form->isValid($request->getPost())) { // user id is from identity // goal id is from request param // friend_user_id is from form // friends[] contains array of friend_user_ids $auth = Zend_Auth::getInstance(); $identity = $auth->getIdentity(); $shareMapper = new Application_Model_ShareMapper(); $shareList = array(); $post = $form->getValues(); foreach ($post['friends'] as $value) { $share = new Application_Model_Share(); $share->setGoalId($goal_id); $share->setUserId($identity->id); $share->setFriendUserId($value); $shareList[] = $share; } var_dump($shareList); // we need to generate an array of $share objects based on the number of friend_user_ids passed through the form //$share = new Application_Model_Share(); // we need to modify shareMapper->save to work with an array of share objects //$post = $form->getValues(); //var_dump($post['friends']); // array of user ids - working //var_dump($post); // //$shareMapper // we need to do this within the ShareMapper /*$tb = new Table; for ($i = 0; $i < 500; $i++) { $row = $tb->createRow(); $row->blah = 'blah'; $row->save(); }*/ } } $goalMapper = new Application_Model_GoalMapper(); $goal = new Application_Model_Goal(); $goalMapper->find($goal_id, $goal); $data = array('id' => $goal->getId(), 'goal' => $goal->getGoal(), 'notes' => $goal->getNotes(), 'goal_date' => $goal->getGoalDate(), 'done' => $goal->getDone(), 'user_id' => $goal->getUserId()); $this->view->goal = $data; $this->view->form = $form; }
public function save(Application_Model_Share $share) { $data = array('id' => $share->getId(), 'goal_id' => $share->getGoalId(), 'user_id' => $share->getUserId(), 'friend_user_id' => $share->getFriendUserId()); if (null === ($id = $share->getId())) { unset($data['id']); return $this->getDbTable()->insert($data); } else { $this->getDbTable()->update($data, array('id = ?' => $id)); } }
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'); } }