예제 #1
0
    /**
     * Show_Send_Step_4
     * Step 4 handles two pieces of functionality:
     * - if cron support is enabled, it "approves" the job for sending and then redirects the user to the main splittest page
     *
     * If cron is not enabled, it processes and sends the emails out in popup mode.
     * It looks at the queues table for people to send to, and sends one email per window refresh.
     * It prints out a report of what's going on:
     * - how many have been sent
     * - how many left
     * - approx how long it has taken so far
     * - approx how long to go
     * - optional extra - pause after displaying that info and sending the email (based on user restrictions)
     *
     * @uses Jobs_API
     * @uses Jobs_API::ApproveJob
     * @uses Jobs_API::QueueSize
     * @uses CheckCronEnabled
     * @uses Splittest_Send_API::StartJob
     */
    public function Show_Send_Step_4()
    {
        $send_details = IEM::sessionGet('SplitTestSendDetails');
        if (!$send_details || !isset($send_details['splitid']) || (int) $send_details['splitid'] <= 0) {
            FlashMessage(GetLang('Addon_splittest_Send_InvalidSplitTest'), SS_FLASH_MSG_ERROR, $this->admin_url);
            return;
        }
        $jobid = $send_details['Job'];
        require_once SENDSTUDIO_API_DIRECTORY . '/jobs.php';
        $jobApi = new Jobs_API();
        if (isset($_GET['Start']) || self::CheckCronEnabled()) {
            /**
             * Remove the "cleanup" variables so we don't kill the send off when we either
             * - successfully schedule a send
             * - or start a send going.
             */
            IEM::sessionRemove('SplitTestSend_Cleanup');
            $user = GetUser();
            $jobApi->ApproveJob($jobid, $user->Get('userid'), $user->Get('userid'));
        }
        /**
         * If we get here and cron is enabled, we're finishing off a scheduled send setup.
         * Show a message and return the user to the manage screen.
         */
        if (self::CheckCronEnabled()) {
            FlashMessage(GetLang('Addon_splittest_Send_JobScheduled'), SS_FLASH_MSG_SUCCESS, $this->admin_url);
            return;
        }
        $this->template_system->Assign('AdminUrl', $this->admin_url, false);
        $send_api = $this->GetApi('Splittest_Send');
        if (isset($_GET['Start'])) {
            $send_api->StartJob($jobid, $send_details['splitid']);
        }
        $sendqueue = $jobApi->GetJobQueue($jobid);
        $job = $jobApi->LoadJob($jobid);
        $send_api->Set('statids', $send_details['Stats']);
        $send_api->Set('jobdetails', $job['jobdetails']);
        $send_api->Set('jobowner', $job['ownerid']);
        $queuesize = $jobApi->QueueSize($sendqueue, 'splittest');
        $send_details['SendQueue'] = $sendqueue;
        $timenow = $send_api->GetServerTime();
        $timediff = $timenow - $send_details['SendStartTime'];
        $time_so_far = $this->TimeDifference($timediff);
        $num_left_to_send = $send_details['SendSize'] - $queuesize;
        if ($num_left_to_send > 0) {
            $timeunits = $timediff / $num_left_to_send;
            $timediff = $timeunits * $queuesize;
        } else {
            $timediff = 0;
        }
        $timewaiting = $this->TimeDifference($timediff);
        $this->template_system->Assign('SendTimeSoFar', sprintf(GetLang('Addon_splittest_Send_Step4_TimeSoFar'), $time_so_far));
        $this->template_system->Assign('SendTimeLeft', sprintf(GetLang('Addon_splittest_Send_Step4_TimeLeft'), $timewaiting));
        if ($num_left_to_send == 1) {
            $this->template_system->Assign('Send_NumberAlreadySent', GetLang('Addon_splittest_Send_Step4_NumberSent_One'));
        } else {
            $this->template_system->Assign('Send_NumberAlreadySent', sprintf(GetLang('Addon_splittest_Send_Step4_NumberSent_Many'), $this->PrintNumber($num_left_to_send)));
        }
        if ($queuesize <= 0) {
            require_once SENDSTUDIO_API_DIRECTORY . '/ss_email.php';
            $email = new SS_Email_API();
            if (SENDSTUDIO_SAFE_MODE) {
                $email->Set('imagedir', TEMP_DIRECTORY . '/send');
            } else {
                $email->Set('imagedir', TEMP_DIRECTORY . '/send.' . $jobid . '.' . $sendqueue);
            }
            $email->CleanupImages();
            $send_details['SendEndTime'] = $send_api->GetServerTime();
            IEM::sessionSet('SplitTestSendDetails', $send_details);
            $this->template_system->Assign('Send_NumberLeft', GetLang('Addon_splittest_Send_Step4_SendFinished'));
            $this->template_system->ParseTemplate('send_step4');
            ?>
				<script>
					window.opener.focus();
					window.opener.document.location = '<?php 
            echo $this->admin_url . '&Action=Send&Step=5';
            ?>
';
					window.close();
				</script>
			<?php 
            return;
        }
        if ($queuesize == 1) {
            $this->template_system->Assign('Send_NumberLeft', GetLang('Addon_splittest_Send_Step4_NumberLeft_One'));
        } else {
            $this->template_system->Assign('Send_NumberLeft', sprintf(GetLang('Addon_splittest_Send_Step4_NumberLeft_Many'), $this->PrintNumber($queuesize)));
        }
        $send_api->SetupJob($jobid, $sendqueue);
        $send_api->SetupNewsletter();
        $recipients = $send_api->FetchFromQueue($sendqueue, 'splittest', 1, 1);
        $send_api->SetupDynamicContentFields($recipients);
        $send_api->SetupCustomFields($recipients);
        $sent_ok = false;
        foreach ($recipients as $p => $recipientid) {
            $send_results = $send_api->SendToRecipient($recipientid, $sendqueue);
            // save the info in the session, then see if we need to pause between each email.
            if ($send_results['success'] > 0) {
                $sent_ok = true;
                $send_details['EmailResults']['success']++;
            } else {
                $send_details['EmailResults']['failure']++;
            }
            $send_details['EmailResults']['total']++;
            IEM::sessionSet('SplitTestSendDetails', $send_details);
        }
        session_write_close();
        $this->template_system->ParseTemplate('send_step4');
        // we should only need to pause if we successfully sent.
        if ($sent_ok) {
            $send_api->Pause();
        }
    }