private function initImport()
 {
     $name = basename(waRequest::post('file'));
     if (empty($name)) {
         throw new waException('Empty import filename');
     }
     $config = wa('shop')->getConfig();
     /**
      * @var shopConfig $config ;
      */
     //TODO detect emulate & type of control
     $file = wa()->getTempPath('csv/upload/' . $name);
     $this->data['emulate'] = waRequest::post('emulate') ? array() : null;
     $this->data['rights'] = $this->getUser()->getRights('shop', 'settings');
     $this->data['new_features'] = array();
     $this->data['currencies'] = $config->getCurrencies();
     $this->data['type_id'] = waRequest::post('type_id', null, waRequest::TYPE_INT);
     if ($this->data['type_id'] && !in_array($this->data['type_id'], $this->data['types'])) {
         $this->data['type_id'] = reset($this->data['types']);
     }
     $map = waRequest::post('csv_map');
     if ($this->emulate()) {
         $this->reader = shopCsvReader::snapshot($file);
         if (!$this->reader) {
             throw new waException('CSV file not found');
         }
         $this->reader->rewind();
     } else {
         /*, waRequest::post('encoding', 'utf-8')*/
         //after upload encoding converted into utf-8
         $this->reader = new shopCsvReader($file, waRequest::post('delimiter', ';'));
         $header = $this->reader->header();
         foreach ($map as $id => &$target) {
             if (preg_match('@^f\\+:(.+)$@', $target, $matches)) {
                 if ($this->data['rights']) {
                     $id = preg_replace('@\\D.*$@', '', $id);
                     $feature = array('name' => ifset($header[$id], 'csv feature'), 'type' => shopFeatureModel::TYPE_VARCHAR, 'multiple' => 0, 'selectable' => 0);
                     list($feature['type'], $feature['multiple'], $feature['selectable']) = explode(':', $matches[1]);
                     $feature['type'] = preg_replace('@([^\\.]+\\.)\\1@', '$1', $feature['type']);
                     if (empty($feature_model)) {
                         $feature_model = new shopFeatureModel();
                     }
                     if (empty($type_features_model)) {
                         $type_features_model = new shopTypeFeaturesModel();
                     }
                     $feature['id'] = $feature_model->save($feature);
                     if ($this->data['type_id']) {
                         $type_features_model->updateByFeature($feature['id'], array($this->data['type_id']), false);
                     }
                     $target = 'features:' . $feature['code'];
                     $this->data['new_features'][$feature['code']] = array('id' => $feature['id'], 'types' => (array) $this->data['type_id']);
                 } else {
                     unset($map[$id]);
                 }
             }
         }
         unset($target);
     }
     $map = array_flip($map);
     $this->reader->setMap($map);
     $this->data['file'] = serialize($this->reader);
     $this->data['primary'] = waRequest::post('primary', 'name');
     $this->data['secondary'] = waRequest::post('secondary', 'skus:-1:sku');
     $this->data['extra_secondary'] = false;
     switch ($this->data['secondary']) {
         case 'skus:-1:sku':
             if (isset($map['skus:-1:name']) && intval($map['skus:-1:name']) >= 0) {
                 $this->data['extra_secondary'] = 'skus:-1:name';
             }
             break;
         case 'skus:-1:name':
             if (isset($map['skus:-1:sku']) && intval($map['skus:-1:sku']) >= 0) {
                 $this->data['extra_secondary'] = 'skus:-1:sku';
             }
             break;
     }
     $upload_app = waRequest::post('upload_app', 'shop', waRequest::TYPE_STRING_TRIM);
     if ($upload_app != 'site') {
         $upload_app = 'shop';
     }
     $this->data['upload_path'] = preg_replace('@[\\\\/]+$@', '/', waRequest::post('upload_path', 'upload/images/') . '/');
     $this->data['upload_path'] = preg_replace('@(^|/)(\\.\\.)/@', '$1/', $this->data['upload_path']);
     if (waSystem::getSetting('csv.upload_path') != $this->data['upload_path']) {
         $app_settings = new waAppSettingsModel();
         $app_settings->set('shop', 'csv.upload_path', $this->data['upload_path']);
     }
     $this->data['virtual_sku_stock'] = waRequest::post('virtual_sku_stock', '', waRequest::TYPE_STRING_TRIM);
     if ($upload_app == 'site') {
         $this->data['upload_path'] = wa()->getDataPath($this->data['upload_path'], true, 'site');
     } else {
         $this->data['upload_path'] = wa()->getDataPath($this->data['upload_path'], false, 'shop');
     }
     if (waSystem::getSetting('csv.upload_app') != $upload_app) {
         if (empty($app_settings)) {
             $app_settings = new waAppSettingsModel();
         }
         $app_settings->set('shop', 'csv.upload_app', $upload_app);
     }
     $this->data['ignore_category'] = !!waRequest::post('ignore_category', 0, waRequest::TYPE_INT);
     if (!in_array($this->data['primary'], array('name', 'url', 'null'))) {
         throw new waException(_w('Invalid primary field'));
     }
     if ($this->data['primary'] == 'null') {
         $this->data['primary'] = null;
     }
     if (!in_array($this->data['secondary'], array('skus:-1:sku', 'skus:-1:name'))) {
         throw new waException(_w('Invalid secondary field'));
     }
     $current = $this->reader->current();
     if (!empty($this->data['primary']) && self::getData($current, $this->data['primary']) === null) {
         throw new waException(_w('Empty primary CSV column'));
     }
     if (empty($this->data['primary']) && self::getData($current, $this->data['secondary']) === null) {
         throw new waException(_w('Empty secondary CSV column'));
     }
     $this->data['count'] = array(self::STAGE_FILE => $this->reader ? $this->reader->size() : null, self::STAGE_CATEGORY => null, self::STAGE_PRODUCT => null, self::STAGE_SKU => null, self::STAGE_IMAGE => null);
 }
 protected function save(waRequestFile $file)
 {
     $path = wa()->getTempPath('csv/upload/');
     waFiles::create($path);
     $original_name = $file->name;
     if ($name = tempnam($path, 'csv')) {
         unlink($name);
         if (($ext = pathinfo($original_name, PATHINFO_EXTENSION)) && preg_match('/^\\w+$/', $ext)) {
             $name .= '.' . $ext;
         }
         $file->moveTo($name);
     } else {
         throw new waException(_w('Error file upload'));
     }
     $encoding = waRequest::post('encoding', 'UTF-8');
     $delimiter = waRequest::post('delimiter');
     try {
         $this->reader = new shopCsvReader($name, $delimiter, $encoding);
         $delimiters = array(';', ',', 'tab');
         $used_delimiters = array($delimiter);
         while (count($this->reader->header()) < 2 && ($delimiter = array_diff($delimiters, $used_delimiters))) {
             $delimiter = reset($delimiter);
             $used_delimiters[] = $delimiter;
             $this->reader->delete();
             $this->reader = new shopCsvReader($name, $delimiter, $encoding);
         }
         if (count($this->reader->header()) < 2) {
             $this->reader->delete();
             $delimiter = waRequest::post('delimiter');
             $this->reader = new shopCsvReader($name, $delimiter, $encoding);
         }
         $encodings = array('UTF-8', 'Windows-1251', 'ISO-8859-1');
         $used_encodings = array($encoding);
         while (in_array(false, (array) $this->reader->header(), true) && ($encoding = array_diff($encodings, $used_encodings))) {
             $encoding = reset($encoding);
             $used_encodings[] = $encoding;
             $this->reader->delete();
             $this->reader = new shopCsvReader($name, $delimiter, $encoding);
         }
         if (in_array(false, (array) $this->reader->header(), true) || count($this->reader->header()) < 2) {
             throw new waException($this->reader->header() ? _w('No data columns were located in the uploaded file. Make sure right separator and encoding were chosen for this upload.') : _w('Unsupported CSV file structure'));
         }
         $profile_helper = new shopImportexportHelper('csv:product:import');
         $profile = $profile_helper->getConfig();
         $profile['config'] += array('encoding' => $encoding, 'delimiter' => ';', 'map' => array());
         $params = array();
         $params['id'] = 'csvproducts';
         $params['title_wrapper'] = '%s';
         $params['description_wrapper'] = '<br><span class="hint">%s</span>';
         $params['control_wrapper'] = '<div class="field"><div class="name">%s</div><div class="value">%s %s</div></div>';
         $params['options'] = $this->options();
         $control = true ? shopCsvReader::TABLE_CONTROL : shopCsvReader::MAP_CONTROL;
         switch ($control) {
             case shopCsvReader::TABLE_CONTROL:
                 $params['preview'] = 50;
                 $params['columns'] = array(array('shopCsvProductviewController', 'tableRowHandler'), '&nbsp;');
                 $params['control_wrapper'] = '<div class="field"><div class="value" style="overflow-x:auto;margin-left:0;">%s %s</div></div>';
                 $params['title_wrapper'] = false;
                 $params['row_handler'] = 'csv_product/rows/';
                 $params['row_handler_string'] = true;
                 $params['autocomplete_handler'] = 'csv_product/autocomplete/reset/';
                 break;
             case shopCsvReader::MAP_CONTROL:
             default:
                 $control = shopCsvReader::MAP_CONTROL;
                 break;
         }
         return array('name' => htmlentities(basename($this->reader->file()), ENT_QUOTES, 'utf-8'), 'original_name' => htmlentities(basename($original_name), ENT_QUOTES, 'utf-8'), 'size' => waFiles::formatSize($this->reader->size()), 'original_size' => waFiles::formatSize($file->size), 'controls' => waHtmlControl::getControl($control, 'csv_map', $params), 'control' => $control, 'header' => $this->reader->header(), 'columns_offset' => count(ifset($params['columns'], array())), 'delimiter' => $delimiter, 'encoding' => $encoding);
     } catch (waException $ex) {
         if ($this->reader) {
             $this->reader->delete(true);
         }
         throw $ex;
     }
 }