Overview

Namespaces

  • Avisota
    • Event
    • Message
    • Queue
    • Recipient
    • RecipientSource
    • Renderer
    • Templating
    • Test
      • Database
      • Imap
      • Message
      • Queue
      • Renderer
      • Transport
    • Transport

Classes

  • CSVFile
  • Dummy

Interfaces

  • RecipientSourceInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: /**
  4:  * Avisota newsletter and mailing system
  5:  *
  6:  * PHP Version 5.3
  7:  *
  8:  * @copyright  bit3 UG 2013
  9:  * @author     Tristan Lins <tristan.lins@bit3.de>
 10:  * @package    avisota-core
 11:  * @license    LGPL-3.0+
 12:  * @link       http://avisota.org
 13:  */
 14: 
 15: namespace Avisota\RecipientSource;
 16: 
 17: use Avisota\Recipient\MutableRecipient;
 18: 
 19: /**
 20:  * A recipient source that read the recipients from a csv file.
 21:  *
 22:  * @package avisota-core
 23:  */
 24: class CSVFile implements RecipientSourceInterface
 25: {
 26:     private $file;
 27: 
 28:     private $columnAssignment;
 29: 
 30:     private $delimiter;
 31: 
 32:     private $enclosure;
 33: 
 34:     private $escape;
 35: 
 36:     /**
 37:      * @param string $fileData
 38:      */
 39:     public function __construct($file, array $columnAssignment, $delimiter = ',', $enclosure = '"', $escape = '\\')
 40:     {
 41:         $this->file             = (string) $file;
 42:         $this->columnAssignment = $columnAssignment;
 43:         $this->delimiter        = $delimiter;
 44:         $this->enclosure        = $enclosure;
 45:         $this->escape           = $escape;
 46:     }
 47: 
 48:     /**
 49:      * Count the recipients.
 50:      *
 51:      * @return int
 52:      */
 53:     public function countRecipients()
 54:     {
 55:         $in = fopen($this->file, 'r');
 56: 
 57:         if (!$in) {
 58:             return 0;
 59:         }
 60: 
 61:         $recipients = 0;
 62: 
 63:         $index = array_search('email', $this->columnAssignment);
 64: 
 65:         while ($row = fgetcsv($in, 0, $this->delimiter, $this->enclosure, $this->escape)) {
 66:             if (!empty($row[$index])) {
 67:                 $recipients ++;
 68:             }
 69:         }
 70: 
 71:         fclose($in);
 72: 
 73:         return $recipients;
 74:     }
 75: 
 76:     /**
 77:      * {@inheritdoc}
 78:      */
 79:     public function getRecipients($limit = null, $offset = null)
 80:     {
 81:         $in = fopen($this->file, 'r');
 82: 
 83:         if (!$in) {
 84:             return null;
 85:         }
 86: 
 87:         $recipients = array();
 88: 
 89:         while ($row = fgetcsv($in, 0, $this->delimiter, $this->enclosure, $this->escape)) {
 90:             $details = array();
 91: 
 92:             foreach ($this->columnAssignment as $index => $field) {
 93:                 if (isset($row[$index])) {
 94:                     $details[$field] = $row[$index];
 95:                 }
 96:             }
 97: 
 98:             if (empty($details['email'])) {
 99:                 continue;
100:             }
101: 
102:             $recipients[] = new MutableRecipient($details['email'], $details);
103:         }
104: 
105:         fclose($in);
106: 
107:         return $recipients;
108:     }
109: }
110: 
avisota/core API documentation generated by ApiGen 2.8.0