Ejemplo n.º 1
0
 protected function saveDBData()
 {
     $schedule = true;
     $relocate = true;
     $message = null;
     // Note: DeliveranceMailChimpList expects campaign send dates to be in
     // local time. Send date must be set on the newsletter so that its
     // internal campaign can get the correct send_date.
     if ($this->ui->getWidget('send_date')->value instanceof SwatDate) {
         $message_text = Deliverance::ngettext('“%1$s” will be sent to one subscriber on %3$s at %4$s %5$s.', '“%1$s” will be sent to %2$s subscribers on %3$s at %4$s %5$s.', $this->send_count);
         // Preserve all the date fields
         $send_date = $this->ui->getWidget('send_date')->value;
         $send_date->setTZ($this->app->default_time_zone);
     } else {
         $schedule = false;
         $message_text = Deliverance::ngettext('“%1$s” was sent to one subscriber.', '“%1$s” was sent to %2$s subscribers.', $this->send_count);
         // Convert all the date fields to the timezone
         $send_date = new SwatDate();
         $send_date->setTimezone($this->app->default_time_zone);
     }
     // build the success message before date conversion to prevent needing
     // to convert to UTC for saving and then back to local time for display.
     $locale = SwatI18NLocale::get();
     $message = new SwatMessage(sprintf($message_text, $this->newsletter->subject, $locale->formatNumber($this->send_count), $send_date->formatLikeIntl(SwatDate::DF_DATE), $send_date->formatLikeIntl(SwatDate::DF_TIME), $send_date->formatTZ(SwatDate::TZ_CURRENT_SHORT)));
     $message->secondary_content = Deliverance::_('Subscriber counts are ' . 'estimates. Full statistics will be available once the newsletter ' . 'has been sent.');
     $campaign_type = $this->newsletter->instance instanceof SiteInstance ? $this->newsletter->instance->shortname : null;
     // grab campaign with old send date to clear out the resources.
     $campaign = $this->newsletter->getCampaign($this->app, $campaign_type);
     // If not a draft, remove the resources first in case they came from an
     // old directory. Don't delete draft newsletter resources as they are
     // shared across all drafts.
     if ($this->newsletter->isScheduled()) {
         DeliveranceCampaign::removeResources($this->app, $campaign);
     }
     // Finally set the date with the local timezone.
     // As DeliveranceMailChimpList expects.
     $this->newsletter->send_date = $send_date;
     // re-grab the campaign with the new send_date;
     $campaign = $this->newsletter->getCampaign($this->app, $campaign_type);
     try {
         // resave campaign so that resource urls are rewritten.
         $this->list->saveCampaign($campaign, false);
         // save/update campaign resources.
         DeliveranceCampaign::uploadResources($this->app, $campaign);
         if ($schedule) {
             $this->list->scheduleCampaign($campaign);
         } else {
             $this->list->sendCampaign($campaign);
         }
         // Before we save the newsletter we need to convert it to UTC.
         $this->newsletter->send_date->toUTC();
         $this->newsletter->save();
     } catch (DeliveranceAPIConnectionException $e) {
         // send date needs to be reset to null so the page titles stay
         // correct.
         $this->newsletter->send_date = null;
         $relocate = false;
         // log api connection exceptions in the admin to keep a track of how
         // frequent they are.
         $e->processAndContinue();
         $message = new SwatMessage(Deliverance::_('There was an issue connecting to the email ' . 'service provider.'), 'error');
         $message->content_type = 'text/xml';
         if ($schedule) {
             $message->secondary_content = sprintf('<strong>%s</strong><br />%s', sprintf(Deliverance::_('“%s” has not been scheduled.'), $this->newsletter->subject), Deliverance::_('Connection issues are typically ' . 'short-lived  and attempting to schedule the ' . 'newsletter again after a delay will usually be ' . 'successful.'));
         } else {
             $message->secondary_content = sprintf('<strong>%s</strong><br />%s', sprintf(Deliverance::_('“%s” has not been sent.'), $this->newsletter->subject), Deliverance::_('Connection issues are typically ' . 'short-lived and attempting to send the newsletter ' . 'again after a delay will usually be successful.'));
         }
     } catch (Exception $e) {
         // send date needs to be reset to null so the page titles stay
         // correct.
         $this->newsletter->send_date = null;
         $relocate = false;
         $e = new DeliveranceException($e);
         $e->processAndContinue();
         if ($schedule) {
             $message_text = Deliverance::_('An error has occurred. The ' . 'newsletter has not been scheduled.');
         } else {
             $message_text = Deliverance::_('An error has occurred. The ' . 'newsletter has not been sent.');
         }
         $message = new SwatMessage($message_text, 'system-error');
     }
     if ($message !== null) {
         $this->app->messages->add($message);
     }
     return $relocate;
 }
Ejemplo n.º 2
0
 protected function getTableModel(SwatView $view)
 {
     $now = new SwatDate();
     $now->setTimezone($this->app->default_time_zone);
     $year = $this->start_date->getYear();
     $start_date = new SwatDate();
     $start_date->setTime(0, 0, 0);
     $start_date->setDate($year, 1, 1);
     $start_date->setTZ($this->app->default_time_zone);
     $end_date = clone $start_date;
     $end_date->addMonths(3);
     $display_end_date = clone $end_date;
     $display_end_date->subtractMonths(1);
     $store = new SwatTableStore();
     while ($end_date->before($now)) {
         for ($i = 1; $i <= 4; $i++) {
             // Only add the quarter to the table model if the start date
             // is within or prior to that quarter.
             if ($this->start_date->before($end_date)) {
                 $ds = new SwatDetailsStore();
                 $quarter = $start_date->formatLikeIntl('yyyy-qq');
                 $ds->date = clone $start_date;
                 $ds->year = $year;
                 $ds->quarter = $quarter;
                 $ds->quarter_title = sprintf(CME::_('Q%s - %s to %s'), $i, $start_date->formatLikeIntl('MMMM yyyy'), $display_end_date->formatLikeIntl('MMMM yyyy'));
                 foreach ($this->providers as $provider) {
                     $shortname = $provider->shortname;
                     $sensitive = isset($this->reports_by_quarter[$quarter][$shortname]);
                     $ds->{'is_' . $shortname . '_sensitive'} = $sensitive;
                 }
                 $store->add($ds);
             }
             $start_date->addMonths(3);
             $end_date->addMonths(3);
             $display_end_date->addMonths(3);
         }
         $year++;
     }
     return $store;
 }