Page 1 of 1

Progress Bar??

Posted: Mon Mar 30, 2009 5:39 am
by a.verdeja
Is there any method to display any kind of progessbar???

Re: Progress Bar??

Posted: Mon Mar 30, 2009 7:59 am
by Chris Corbyn
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.

Re: Progress Bar??

Posted: Mon Apr 06, 2009 4:36 am
by HHahn
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.

Re: Progress Bar??

Posted: Mon Apr 06, 2009 5:43 am
by Chris Corbyn
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.

Code: Select all

 
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:

Code: Select all

<?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.

Posted: Thu Aug 06, 2009 11:33 am
by s4crificed
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! :)