示例#1
0
 /**
  * Execute the action.
  */
 public function execute()
 {
     // define path
     $path = BACKEND_CACHE_PATH . '/Profiles/import_template.csv';
     // define required fields
     $fields = array('email', 'display_name', 'password');
     // define file
     $file = new \SpoonFileCSV();
     // download the file
     $file->arrayToFile($path, array(), $fields, null, ',', '"', true);
 }
 /**
  * Validate the form
  *
  * @return	void
  */
 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() ? SpoonFileCSV::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 = SpoonFileCSV::fileToArray($fileCSV->getTempFileName(), null, null, ';', '"');
             // 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-' . SpoonFilter::urlise(BackendModel::getUTCDate()) . '.csv';
                 SpoonFileCSV::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);
             }
         }
     }
 }
示例#3
0
 /**
  * Exports a series of e-mail address records by group ID in CSV format. This function will send headers to download the CSV and exit your script after use.
  *
  * @return	void
  * @param	int $id		The id of the group to export.
  */
 public static function exportAddressesByGroupID($id)
 {
     // set the filename and path
     $filename = 'addresses-' . SpoonDate::getDate('YmdHi') . '.csv';
     $path = BACKEND_CACHE_PATH . '/mailmotor/' . $filename;
     // fetch the addresses by group
     $records = self::getAddressesByGroupID($id);
     // fetch the group fields
     $groupFields = array_flip(self::getCustomFields($id));
     // group custom fields found
     if (!empty($groupFields)) {
         // loop the group fields and empty every value
         foreach ($groupFields as &$field) {
             $field = '';
         }
     }
     // records found
     if (!empty($records)) {
         // loop records
         foreach ($records as $key => $record) {
             // reformat the date
             $records[$key]['created_on'] = SpoonDate::getDate('j F Y', $record['created_on'], BL::getWorkingLanguage());
             // fetch custom fields for this e-mail
             $customFields = self::getCustomFieldsByAddress($record['email']);
             $customFields = !empty($customFields[$id]) ? $customFields[$id] : $groupFields;
             // loop custom fields
             foreach ($customFields as $column => $value) {
                 // add the fields to this record
                 $records[$key][$column] = $value;
             }
         }
     }
     // generate the CSV and download the file
     SpoonFileCSV::arrayToFile($path, $records, array(BL::lbl('Email'), BL::lbl('Created')), null, ';', '"', true);
 }