private function featuresAutocomplete($q)
    {
        $result = array();
        $model = new shopFeatureModel();
        $value = $model->escape($q, 'like');
        $table = $model->getTableName();
        $options = (array) waRequest::get('options', array());
        $where = array('1');
        if (!empty($options['single'])) {
            $where[] = '`multiple`=0';
        }
        $where = ' AND ((' . implode(') AND (', $where) . '))';
        $sql = <<<SQL
SELECT `id`,`name`,`code`,`type`,`count` FROM {$table}
WHERE
  (`parent_id` IS NULL
  AND
  (
  (`name` LIKE '%{$value}%')
  OR
  (`code` LIKE '%{$value}%')
  )){$where}
ORDER BY `count` DESC
LIMIT 20
SQL;
        foreach ($model->query($sql)->fetchAll('code', true) as $code => $f) {
            $f['type'] = shopFeatureModel::getTypeName($f);
            $f['count'] = _w('%d value', '%d values', $f['count']);
            $id = $f['id'];
            unset($f['id']);
            $result[] = array('id' => $id, 'value' => $code, 'name' => $f['name'], 'label' => implode('; ', array_filter($f)));
        }
        return $result;
    }
 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);
         }
     }
 }
Beispiel #3
0
<?php

$model = new shopFeatureModel();
try {
    $model->query('SELECT `count` FROM `shop_feature` WHERE 0');
} catch (waDbException $ex) {
    $model->query('ALTER TABLE  `shop_feature`  ADD  `count` INT UNSIGNED NOT NULL');
}
$model->recount();