Backend\Modules\Profiles\Engine\Model::importCsv PHP Метод

importCsv() публичный статический Метод

Import CSV data
public static importCsv ( array $data, null $groupId = null, boolean $overwriteExisting = false ) : array
$data array The array from the .csv file
$groupId null $groupId Adding these profiles to a group
$overwriteExisting boolean $overwriteExisting
Результат array array('count' => array('exists' => 0, 'inserted' => 0));
    public static function importCsv($data, $groupId = null, $overwriteExisting = false)
    {
        // init statistics
        $statistics = array('count' => array('exists' => 0, 'inserted' => 0));
        // loop data
        foreach ($data as $item) {
            // field checking
            if (!isset($item['email']) || !isset($item['display_name']) || !isset($item['password'])) {
                throw new BackendException('The .csv file should have the following columns; "email", "password" and "display_name".');
            }
            // define exists
            $exists = self::existsByEmail($item['email']);
            // do not overwrite existing profiles
            if ($exists && !$overwriteExisting) {
                // adding to exists
                $statistics['count']['exists'] += 1;
                // skip this item
                continue;
            }
            // build item
            $values = array('email' => $item['email'], 'registered_on' => BackendModel::getUTCDate(), 'display_name' => $item['display_name'], 'url' => self::getUrl($item['display_name']));
            // does not exist
            if (!$exists) {
                // import
                $id = self::insert($values);
                // update counter
                $statistics['count']['inserted'] += 1;
                // already exists
            } else {
                // get profile
                $profile = self::getByEmail($item['email']);
                $id = $profile['id'];
                // exists
                $statistics['count']['exists'] += 1;
            }
            // new password filled in?
            if ($item['password']) {
                // get new salt
                $salt = self::getRandomString();
                // update salt
                self::setSetting($id, 'salt', $salt);
                // build password
                $values['password'] = self::getEncryptedString($item['password'], $salt);
            }
            // update values
            self::update($id, $values);
            // we have a group id
            if ($groupId) {
                // init values
                $values = array();
                // build item
                $values['profile_id'] = $id;
                $values['group_id'] = $groupId;
                $values['starts_on'] = BackendModel::getUTCDate();
                // insert values
                self::insertProfileGroup($values);
            }
        }
        return $statistics;
    }

Usage Example

Пример #1
0
 /**
  * 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);
         }
     }
 }