/** * Validate the form */ private function validateForm() { if ($this->frm->isSubmitted()) { $this->frm->cleanupFields(); // get fields $ddmGroup = $this->frm->getField('group'); $fileFile = $this->frm->getField('file'); $csv = array(); // validate input $ddmGroup->isFilled(BL::getError('FieldIsRequired')); if ($fileFile->isFilled(BL::err('FieldIsRequired'))) { if ($fileFile->isAllowedExtension(array('csv'), sprintf(BL::getError('ExtensionNotAllowed'), 'csv'))) { $csv = Csv::fileToArray($fileFile->getTempFileName()); if ($csv === false) { $fileFile->addError(BL::getError('InvalidCSV')); } } } if ($this->frm->isCorrect()) { // import the profiles $overwrite = $this->frm->getField('overwrite_existing')->isChecked(); $statistics = BackendProfilesModel::importCsv($csv, $ddmGroup->getValue(), $overwrite); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_import', array('statistics' => $statistics)); // build redirect url with the right message $redirectUrl = BackendModel::createURLForAction('index') . '&report='; $redirectUrl .= $overwrite ? 'profiles-imported-and-updated' : 'profiles-imported'; $redirectUrl .= '&var[]=' . $statistics['count']['inserted']; $redirectUrl .= '&var[]=' . $statistics['count']['exists']; // everything is saved, so redirect to the overview $this->redirect($redirectUrl); } } }
/** * Validate the form */ private function validateForm() { if ($this->frm->isSubmitted()) { //--Clean form $this->frm->cleanupFields(); //--Get fields $fields = $this->frm->getFields(); //--Field required $fields['csv']->isFilled(BL::err('CSVIsRequired')); //--Check if form is correct if ($fields['csv']->isFilled()) { // convert the CSV file to an array $csv = BackendCSV::fileToArray($fields['csv']->getTempFileName(), array("email", "name"), null, ';'); //--check if the csv is correct if ($csv === false || empty($csv) || !isset($csv[0])) { $fields['csv']->addError(BL::err('InvalidCSV')); } //--Get all the groups $groups = $fields["groups"]->getValue(); $language = $fields["languages"]->getValue(); //--Process CSV $return = $this->processCsv($csv, $groups, $language); if (is_array($return)) { $this->tpl->assign('hideForm', true); $this->tpl->assign('return', $return); } } } }
public static function exportDemo() { // set the filename and path $filename = 'export_demo.csv'; $path = BACKEND_CACHE_PATH . '/mailengine/' . $filename; $users = array(array('*****@*****.**', "Example 1"), array('*****@*****.**', "Example 2"), array('*****@*****.**', "Example 3"), array('*****@*****.**', "Example 4")); BackendCSV::arrayToFile($path, $users, array(BL::lbl('Email'), BL::lbl('Name')), null, ';', '"', true); }
/** * Validate the form */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // shorten fields $fileCSV = $this->frm->getField('csv'); $chkGroups = $this->frm->getField('groups'); // validate fields $fileCSV->isFilled(BL::err('CSVIsRequired')); // convert the CSV file to an array $csv = $fileCSV->isFilled() ? BackendCSV::fileToArray($fileCSV->getTempFileName()) : null; // check if the csv is valid if ($csv === false || empty($csv) || !isset($csv[0])) { $fileCSV->addError(BL::err('InvalidCSV')); } // there was a csv file found if (!empty($csv)) { // fetch the columns of the first row $columns = array_keys($csv[0]); // loop the columns foreach ($csv as $row) { // fetch the row columns $rowColumns = array_keys($row); // check if the arrays match if ($rowColumns != $columns) { // add an error to the CSV files $fileCSV->addError(BL::err('InvalidCSV')); // exit loop break; } } } // get values $values = $this->frm->getValues(); // check if at least one recipient group is chosen if (empty($values['groups'])) { $chkGroups->addError(BL::err('ChooseAtLeastOneGroup')); } // no errors? if ($this->frm->isCorrect()) { // convert the CSV file to an array, and fetch the group's CM ID $csv = BackendCSV::fileToArray($fileCSV->getTempFileName()); // process our import, and get the failed subscribers $failedSubscribers = $this->processImport($csv, $values['groups']); // show a detailed report $this->tpl->assign('import', false); // no failed subscribers found if (empty($failedSubscribers)) { // trigger event BackendModel::triggerEvent($this->getModule(), 'after_import_address'); // redirect to success message $this->redirect(BackendModel::createURLForAction('Addresses') . '&report=imported-addresses&var[]=' . count($csv) . '&var[]=' . count($values['groups'])); } else { // write a CSV file to the cache $csvFile = 'import-report-' . CommonUri::getUrl(BackendModel::getUTCDate()) . '.csv'; BackendCSV::arrayToFile(BACKEND_CACHE_PATH . '/Mailmotor/' . $csvFile, $failedSubscribers, null, null, ';', '"'); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_import_address_with_failed_items', array('failed' => $failedSubscribers)); // redirect to failed message with an additional parameter to // display a download link to the report-csv form cache. $this->redirect(BackendModel::createURLForAction('Addresses') . '&error=imported-addresses&var[]=' . count($csv) . '&var[]=' . count($values['groups']) . '&var[]=' . count($failedSubscribers) . '&csv=' . $csvFile); } } } }
/** * Exports the statistics of all mailings for a given campaign ID in CSV format. This function * will send headers to download the CSV and exit your script after use. * * @param int $id The ID of the campaign. */ public static function exportStatisticsByCampaignID($id) { // set the filename and path $filename = 'statistics-' . \SpoonDate::getDate('YmdHi') . '.csv'; // fetch the addresses by group $records = array(); $records[] = BackendMailmotorCMHelper::getStatisticsByCampaignID($id); // unset some records unset($records[0]['opens'], $records[0]['clicks'], $records[0]['clicks_percentage'], $records[0]['recipients_total'], $records[0]['recipients_percentage']); // set columns $columns = array(); $columns[] = BL::msg('MailingCSVRecipients'); $columns[] = BL::msg('MailingCSVUniqueOpens'); $columns[] = BL::msg('MailingCSVUnsubscribes'); $columns[] = BL::msg('MailingCSVBounces'); $columns[] = BL::msg('MailingCSVUnopens'); $columns[] = BL::msg('MailingCSVBouncesPercentage'); $columns[] = BL::msg('MailingCSVUniqueOpensPercentage'); $columns[] = BL::msg('MailingCSVUnopensPercentage'); // set start of the CSV $csv = BackendCSV::arrayToString($records, $columns); // fetch all mailings in this campaign $mailings = BackendModel::getContainer()->get('database')->getRecords(self::QRY_DATAGRID_BROWSE_SENT_FOR_CAMPAIGN, array('sent', $id)); // mailings set if (!empty($mailings)) { // set mailings columns $mailingColumns = array(); $mailingColumns['name'] = BL::lbl('Name'); $mailingColumns['language'] = BL::lbl('Language'); // add the records to the csv string $csv .= PHP_EOL . 'Mailings:' . PHP_EOL . BackendCSV::arrayToString($mailings, $mailingColumns, array('id', 'campaign_id', 'campaign_name', 'send_on', 'status')); } // set headers for download header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '"'); echo $csv; exit; }
/** * Execute the action. */ public function execute() { $this->id = $this->getParameter('id', 'int'); // does the item exist if ($this->id !== null && BackendFormBuilderModel::exists($this->id)) { parent::execute(); $this->setFilter(); $this->setItems(); BackendCSV::outputCSV(date('Ymd_His') . '.csv', $this->rows, $this->columnHeaders); } else { // no item found, redirect to index, because somebody is f*****g with our url $this->redirect(BackendModel::createURLForAction('Index') . '&error=non-existing'); } }