コード例 #1
0
 public function executeNew(sfWebRequest $request)
 {
     if (!$this->getGuardUser()->hasCampaigns()) {
         $this->redirect($this->getContext()->getRouting()->generate('dashboard', array(), true) . '?no_campaign=1');
     }
     $petition = new Petition();
     $petition->setHomepage(1);
     $petition->setWithCountry(1);
     $petition->setKind(Petition::KIND_PETITION);
     $petition->setStartAt(gmdate('Y-m-d'));
     $petition->setEndAt(gmdate('Y-m-d', strtotime('next year')));
     $petition->setFromName($this->getGuardUser()->getOrganisation() ?: $this->getGuardUser()->getName());
     $petition->setFromEmail($this->getGuardUser()->getEmailAddress());
     $campaign_id = $request->getGetParameter('campaign');
     if (is_numeric($campaign_id)) {
         $campaign = CampaignTable::getInstance()->findById($campaign_id, $this->userIsAdmin());
         if ($campaign) {
             $petition->setCampaign($campaign);
         }
     }
     $this->form = new NewPetitionForm($petition, array(NewPetitionForm::OPTION_USER => $this->getGuardUser()));
     if ($request->isMethod('post')) {
         $this->form->bind($request->getPostParameter($this->form->getName()));
         if ($this->form->isValid()) {
             $con = PetitionTable::getInstance()->getConnection();
             $con->beginTransaction();
             try {
                 $this->form->save();
                 $petition = $this->form->getObject();
                 $pr = new PetitionRights();
                 $pr->setPetition($petition);
                 $pr->setUser($this->getGuardUser());
                 $pr->setActive(1);
                 $pr->setAdmin(0);
                 $pr->setMember(1);
                 $pr->save();
                 $con->commit();
             } catch (Exception $e) {
                 $con->rollback();
             }
             return $this->ajax()->redirectRotue('petition_edit_', array('id' => $petition->getId()))->render();
         } else {
             return $this->ajax()->form($this->form)->render();
         }
     }
     $this->includeChosen();
 }