PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Aug 04, 2020 9:31 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Progress Bar??
PostPosted: Mon Mar 30, 2009 5:39 am 
Offline
Forum Newbie

Joined: Mon Mar 30, 2009 5:34 am
Posts: 1
Is there any method to display any kind of progessbar???


Top
 Profile  
 
 Post subject: Re: Progress Bar??
PostPosted: Mon Mar 30, 2009 7:59 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
This is not possible to do server-side. You could probably piece something together using JavaScript/AJAX but it's not something that Swift Mailer supports out of the box.


Top
 Profile  
 
 Post subject: Re: Progress Bar??
PostPosted: Mon Apr 06, 2009 4:36 am 
Offline
Forum Commoner

Joined: Mon Mar 02, 2009 10:16 am
Posts: 43
Location: Veldhoven, Netherlands
I agree that it would be a nice option. I also agree that this can better be solved with Ajax. But in that case, SwiftMailer may have to supply the status information. Does SwiftMailer support a function that can be called to "poll" the current progress status?
As far as I understand, SwiftMailer does not use external timing (like Cron), so it may periodically put its status (e.g., the number of messages sent) into e.g. a small file (or a database record), where it can be read by Ajax for updating the progress indicator.


Top
 Profile  
 
 Post subject: Re: Progress Bar??
PostPosted: Mon Apr 06, 2009 5:43 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
The plugin API's SendEvent can be used to determine a percentage progress, though you may need to provide the total number upfront. You'd have to write a plugin that outputs this as it's sending. Just how you'd plan on providing the output in a usable format I'm not sure. Writing to a file would be my choice (since you can overwrite the file contents and have the AJAX poll for those changes).

Untested, proof of concept pseudo code.

Syntax: [ Download ] [ Hide ]
 
class ProgressTrackerPlugin implements Swift_Events_SendListener {
  private $_outputFile;
  private $_targetMessage;
  private $_targetAddresses;
  private $_processedAddresses;
 
  public function __construct($outputFile) {
    $this->_outputFile = $outputFile;
  }
 
  public function trackMessageProgress($message) {
    $this->_targetMessage = $message;
    $this->_targetAddresses = $message->getTo();
    $this->_processedAddresses = array();
  }
 
  public function beforeSendPeformed(Swift_Events_SendEvent $evt) {
    //Not used here
  }
 
  public function sendPerformed(Swift_Events_SendEvent $evt) {
    //Don't deal with any other message
    if ($evt->getMessage() !== $this->_targetMessage) {
      return;
    }
   
    $to = $message->getTo();
    foreach ($to as $address => $name) {
      if (array_key_exists($address, $this->_targetAddresses)) {
        //Quick sanity check to make sure we don't store the same address twice
        // Use address key as a unique constraint
        $this->_processedAddresses[$address] = $address;
      }
    }
   
    $results = array(
      'totalRequired' => count($this->_targetAddresses),
      'totalSent' => count($this->_processedAddresses)
    );
   
    //Write JSON to the output file
    file_put_contents($this->_outputFile, json_encode($results));
  }
}


So you'd use it like this:

Syntax: [ Download ] [ Hide ]
<?php
 
$mailer = new Swift_Mailer( ... );
 
$tracker = new ProgressTrackerPlugin('/some/output/file.json');
 
$mailer->registerPlugin($tracker);
 
$message = Swift_Message::newInstance( ... );
 
/*
 
Add recipients etc etc
 
*/

 
$tracker->trackMessageProgress($message);
 
$mailer->batchSend($message);


Your JS code just needs to keep making AJAX requests to the JSON file this script is writing to.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 06, 2009 11:33 am 
Offline
Forum Newbie

Joined: Thu Aug 06, 2009 11:32 am
Posts: 1
Hi,

your code outputs error "Fatal error: Class ProgressTrackerPlugin contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Swift_Events_SendListener::beforeSendPerformed) in C:\programavimas\EasyPHP 2.0b1\www\naujienlaiskis2\do\swift_lib\classes\Swift\ProgressTrackerPlugin.php on line 42"

maybe you could help me out and fix this? i'm not very good with OOP.

Thanks! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group