Ejemplo n.º 1
0
 /**
  * Helper method to handle create and update of campaigns. Will redirect to
  * the specific campaign on success with a message. If there are any
  * validation or CSRF errors we render the form again with information.
  *
  * @param null|int $id
  *
  * @return CampaignFormView
  */
 private function _updateCampaign($id = null)
 {
     $form = $this->_buildCampaignForm($id);
     $form->hydrate($this->request()->postVariables());
     if ($id == null) {
         $config = Container::config()->get("default_processors");
         $processors = [];
         if ($config != null) {
             $processorKeys = $config->availableKeys();
             foreach ($processorKeys as $key) {
                 $processors[]['processorType'] = $key;
             }
         }
         $form->processors = $processors;
     }
     if ($form->isValid() && $form->csrfCheck(true)) {
         $form->saveChanges();
         if ($form->sendAt == "X * * * *") {
             $campaign = new Campaign($id);
             if ($campaign->exists()) {
                 $cronMinute = $id % 60;
                 $campaign->sendAt = "{$cronMinute} * * * *";
                 $campaign->saveChanges();
             }
         }
         $msg = "Campaign '{$form->name}'";
         $msg .= $id ? " Updated" : " Created";
         return Redirect::to("/campaigns/{$form->getMapper()->id()}")->with("msg", new TransportMessage("info", $msg));
     }
     return $this->renderEdit($id, $form);
 }