* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 7562 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_CAN_LOAD_FILES_'))
exit;
class Newsletter extends Module
{
private $_postErrors = array();
private $_html = '';
private $_postSucess;
public function __construct()
{
global $cookie;
$this->name = 'newsletter';
$this->tab = 'administration';
$this->version = 2.0;
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Newsletter');
$this->description = $this->l('Generates a .CSV file for mass mailings');
if ($this->id)
{
$this->_file = 'export_'.Configuration::get('PS_NEWSLETTER_RAND').'.csv';
$this->_postValid = array();
// Getting data...
$_countries = Country::getCountries((int)($cookie->id_lang));
// ...formatting array
$countries[0] = $this->l('All countries');
foreach ($_countries as $country)
$countries[$country['id_country']] = $country['name'];
// And filling fields to show !
$this->_fieldsExport = array(
'COUNTRY' => array(
'title' => $this->l('Customers\' country'),
'desc' => $this->l('Operate a filter on customers\' country.'),
'type' => 'select',
'value' => $countries,
'value_default' => 0
),
'SUSCRIBERS' => array(
'title' => $this->l('Newsletter subscribers'),
'desc' => $this->l('Filter newsletter subscribers.'),
'type' => 'select',
'value' => array(0 => $this->l('All customers'), 2 => $this->l('Subscribers'), 1 => $this->l('Non-subscribers')),
'value_default' => 2
),
'OPTIN' => array(
'title' => $this->l('Opted-in subscribers'),
'desc' => $this->l('Filter opted-in subscribers.'),
'type' => 'select',
'value' => array(0 => $this->l('All customers'), 2 => $this->l('Subscribers'), 1 => $this->l('Non-subscribers')),
'value_default' => 0
),
);
}
}
public function install()
{
return (parent::install() AND Configuration::updateValue('PS_NEWSLETTER_RAND', rand().rand()));
}
private function _postProcess()
{
if (isset($_POST['submitExport']) AND isset($_POST['action']))
{
if ($_POST['action'] == 'customers')
$result = $this->_getCustomers();
else
{
if (!Module::isInstalled('blocknewsletter'))
$this->_html .= $this->displayError('The module "blocknewsletter" is required for this feature');
else
$result = $this->_getBlockNewsletter();
}
if (!$nb = (int)(Db::getInstance(_PS_USE_SQL_SLAVE_)->NumRows()))
$this->_html .= $this->displayError($this->l('No customers found with these filters!'));
elseif ($fd = @fopen(dirname(__FILE__).'/'.strval(preg_replace('#\.{2,}#', '.', $_POST['action'])).'_'.$this->_file, 'w'))
{
foreach ($result AS $tab)
$this->_my_fputcsv($fd, $tab);
fclose($fd);
$this->_html .= $this->displayConfirmation(
$this->l('The .CSV file has been successfully exported.').
' ('.$nb.' '.$this->l('customers found').')
>
'.$this->l('Download the file').' '.$this->_file.'