private function initExport() { $hash = shopImportexportHelper::getCollectionHash(); $this->data['export_category'] = !in_array($hash['type'], array('id', 'set', 'type')); $this->data['timestamp'] = time(); $this->data['hash'] = $hash['hash']; $encoding = waRequest::post('encoding', 'utf-8'); $options = array(); $config = array('encoding' => $encoding, 'delimiter' => waRequest::post('delimiter', ';'), 'features' => !!waRequest::post('features'), 'images' => !!waRequest::post('images'), 'extra' => !!waRequest::post('extra'), 'domain' => waRequest::post('domain'), 'hash' => $hash['hash']); $map = shopCsvProductuploadController::getMapFields(true, $config['extra']); $this->data['composite_features'] = array(); $features_model = new shopFeatureModel(); if (!empty($config['features'])) { if (preg_match('@^id/(.+)$@', $this->data['hash'], $matches)) { $product_ids = array_unique(array_map('intval', explode(',', $matches[1]))); $features = $features_model->getByProduct($product_ids); $feature_selectable_model = new shopProductFeaturesSelectableModel(); $feature_ids = $feature_selectable_model->getFeatures($product_ids); if ($feature_ids = array_diff($feature_ids, array_keys($features))) { $features += $features_model->getById($feature_ids); } $parents = array(); foreach ($features as $feature) { if (!empty($feature['parent_id'])) { if (!isset($parents[$feature['parent_id']])) { $parents[$feature['parent_id']] = $feature['parent_id']; } } } if ($parents) { $features += $features_model->getById($parents); } } else { $features = $features_model->getAll(); } if ($features) { $options['features'] = true; foreach ($features as $feature) { if (!preg_match('/\\.\\d$/', $feature['code']) && $feature['type'] != shopFeatureModel::TYPE_DIVIDER) { $map[sprintf('features:%s', $feature['code'])] = $feature['name']; if ($encoding != 'UTF-8') { $this->data['composite_features'][$feature['code']] = true; } } } } } $tax_model = new shopTaxModel(); if ($taxes = $tax_model->getAll()) { $this->data['taxes'] = array(); foreach ($taxes as $tax) { $this->data['taxes'][$tax['id']] = $tax['name']; } } if (!empty($config['images'])) { $sql = 'SELECT COUNT(1) AS `cnt` FROM `shop_product_images` GROUP BY `product_id` ORDER BY `cnt` DESC LIMIT 1'; if ($cnt = $features_model->query($sql)->fetchField('cnt')) { $options['images'] = true; for ($n = 0; $n < $cnt; $n++) { $field = sprintf('images:%d', $n); $map[$field] = _w('Product images'); } } if (isset($map['images'])) { unset($map['images']); } } else { if (isset($map['images'])) { unset($map['images']); } foreach (array_keys($map) as $field) { if (preg_match('@^images:\\d+$@', $field)) { unset($map[$field]); } } } $profile_helper = new shopImportexportHelper('csv:product:export'); $profile = $profile_helper->setConfig($config); $profile_raw = waRequest::request('profile', array(), waRequest::TYPE_ARRAY); $profile_name = substr(waLocale::transliterate(ifempty($profile_raw['name'], $profile)), 0, 32); $name = sprintf('products(%s)_%s_%s.csv', $profile_name, date('Y-m-d'), strtolower($encoding)); $name = preg_replace('@[^A-Za-z0-9\\-\\(\\),_\\.]+@', '', $name); $file = wa()->getTempPath('csv/download/' . $profile . '/' . $name); $this->writer = new shopCsvWriter($file, $config['delimiter'], $encoding); $this->writer->setMap($map); $this->data['file'] = serialize($this->writer); $this->data['map'][self::STAGE_CATEGORY] = null; $this->data['map'][self::STAGE_PRODUCT] = 0; $this->data['config'] = $config; $this->data['options'] = $options; $this->initRouting(); $this->data['count'] = array(self::STAGE_PRODUCT => $this->getCollection()->count(), self::STAGE_CATEGORY => 0, self::STAGE_SKU => null, self::STAGE_IMAGE => null); if ($this->data['export_category']) { $model = new shopCategoryModel(); if (preg_match('@^category/(\\d+)$@', $this->data['hash'], $matches)) { $this->data['count'][self::STAGE_CATEGORY] = count($model->getPath($matches[1])) + 1; //TODO add subcategories for nested //$model->getTree($matches[1]); } else { $this->data['count'][self::STAGE_CATEGORY] = $model->countByField('type', shopCategoryModel::TYPE_STATIC); } } }