Esempio n. 1
0
/**
 * import from an uploaded file
 *
 * @param array $vars array of parameters
 *
 * @return status
 */
function Products_importFile($vars = false)
{
    // { set up variables
    if ($vars === false) {
        return false;
    }
    if (!@$vars->productsImportDeleteAfter['varvalue']) {
        $vars->productsImportDeleteAfter = array('varvalue' => false);
    }
    if (!@$vars->productsImportDelimiter['varvalue']) {
        $vars->productsImportDelimiter = array('varvalue' => ',');
    }
    if (!@$vars->productsImportFileUrl['varvalue']) {
        $vars->productsImportFileUrl = array('varvalue' => 'ww.cache/products/import.csv');
    }
    if (!@$vars->productsImportImagesDir['varvalue']) {
        $vars->productsImportImagesDir = array('varvalue' => 'ww.cache/products/images');
    }
    $fname = USERBASE . '/' . $vars->productsImportFileUrl['varvalue'];
    // }
    if (strpos($fname, '..') !== false) {
        return array('message' => __('Invalid file URL'));
    }
    if (!file_exists($fname)) {
        return array('message' => __('File not uploaded'));
    }
    if (function_exists('mb_detect_encoding')) {
        $charset = mb_detect_encoding(file_get_contents($fname), 'UTF-8', true);
    } else {
        $charset = 'UTF-8';
    }
    $handle = fopen($fname, 'r');
    if ($charset != 'UTF-8') {
        stream_filter_register("utf8encode", "Utf8encode_Filter") or die(__('Failed to register filter'));
        stream_filter_prepend($handle, "utf8encode");
    }
    $row = fgetcsv($handle, 1000, $vars->productsImportDelimiter['varvalue']);
    // { check the headers
    $headers = array();
    foreach ($row as $k => $v) {
        if ($v) {
            $headers[$v] = $k;
        }
    }
    if (!isset($headers['_name']) || !isset($headers['_ean']) || !isset($headers['_stocknumber']) || !isset($headers['_type']) || !isset($headers['_categories'])) {
        $req = '_name, _ean, _stocknumber, _type, _categories';
        return array('message' => __('Missing required headers (%1)', array($req), 'core') . '. ' . __('Please use the Download link to get a sample import file.'), 'headers-found' => $headers);
    }
    // }
    $product_types = array();
    $imported = 0;
    $categoriesByName = array();
    $preUpload = (int) @$vars->productsImportSetExisting['varvalue'];
    $postUpload = (int) @$vars->productsImportSetImported['varvalue'];
    if ($preUpload) {
        dbQuery('update products set enabled=' . ($preUpload - 1) . ', date_edited=now()');
    }
    // { do the import
    while (($data = fgetcsv($handle, 1000, $vars->productsImportDelimiter['varvalue'])) !== false) {
        $id = 0;
        $stocknumber = $data[$headers['_stocknumber']];
        // { stockcontrol_total (how many are in stock)
        $stockcontrol_total = '';
        if (isset($headers['_stockcontrol_total']) && isset($data[$headers['_stockcontrol_total']])) {
            $stockcontrol_total = ',stockcontrol_total=' . (int) $data[$headers['_stockcontrol_total']];
        }
        // }
        $type = $data[$headers['_type']];
        if (!$type) {
            $type = 'default';
        }
        if (isset($product_types[$type]) && $product_types[$type]) {
            $type_id = $product_types[$type];
        } else {
            $type_id = (int) dbOne('select id from products_types where name="' . addslashes($type) . '"', 'id');
            if (!$type_id) {
                $type_id = (int) dbOne('select id from products_types limit 1', 'id');
            }
            $product_types[$type] = $type_id;
        }
        $name = $data[$headers['_name']];
        $ean = $data[$headers['_ean']];
        if ($stocknumber) {
            $id = (int) dbOne('select id from products where stock_number="' . addslashes($stocknumber) . '"', 'id');
            if ($id) {
                dbQuery('update products set ean="' . addslashes($ean) . '"' . ',product_type_id=' . $type_id . ',name="' . addslashes($name) . '",date_edited=now()' . $stockcontrol_total . ' where id=' . $id);
            }
        }
        if (!$id) {
            $sql = 'insert into products set ' . 'stock_number="' . addslashes($stocknumber) . '"' . $stockcontrol_total . ',product_type_id=' . $type_id . ',name="' . addslashes($name) . '"' . ',ean="' . addslashes($ean) . '"' . ',date_created=now()' . ',date_edited=now()' . ',activates_on=now()' . ',expires_on="2100-01-01"' . ',enabled=1' . ',data_fields="{}"' . ',online_store_fields="{}"';
            dbQuery($sql);
            $id = dbLastInsertId();
        }
        // { get data from Products table
        $row = dbRow('select data_fields,online_store_fields,activates_on,expires_on' . ' from products where id=' . $id);
        // }
        $data_fields = json_decode($row['data_fields'], true);
        $os_fields = json_decode($row['online_store_fields'], true);
        foreach ($headers as $k => $v) {
            if (preg_match('/^_/', $k)) {
                continue;
            }
            foreach ($data_fields as $k2 => $v2) {
                if ($v2['n'] == $k) {
                    unset($data_fields[$k2]);
                }
            }
            $data_fields[] = array('n' => $k, 'v' => $data[$v]);
        }
        if (@$data[$headers['_price']]) {
            $os_fields['_price'] = Products_importParseNumber(@$data[$headers['_price']]);
            $os_fields['_saleprice'] = Products_importParseNumber(@$data[$headers['_saleprice']]);
            $os_fields['_bulkprice'] = Products_importParseNumber(@$data[$headers['_bulkprice']]);
            $os_fields['_bulkamount'] = (int) @$data[$headers['_bulkamount']];
        } else {
            $os_fields = array();
        }
        $dates = '';
        $now = date('Y-m-d');
        if ($postUpload && ($row['activates_on'] > $now || $row['expires_on'] < $now)) {
            $dates = ',activates_on="' . $now . '",expires_on="2100-01-01"';
        }
        if (!$postUpload && ($row['activates_on'] < $now && $row['expires_on'] > $now)) {
            $dates = ',activates_on="' . $now . '",expires_on="' . $now . '"';
        }
        // { update the product row
        dbQuery('update products set ' . 'data_fields="' . addslashes(json_encode($data_fields)) . '"' . ',online_store_fields="' . addslashes(json_encode($os_fields)) . '"' . ',date_edited=now()' . $dates . ',enabled=' . $postUpload . ' where id=' . $id);
        // }
        $cid = (int) @$vars->productsImportCategory['varvalue'];
        switch ($cid) {
            case '-1':
                // { from file
                ProductsCategoriesProducts::deleteByProductId($id);
                dbQuery('update products set num_of_categories=0 where id=' . $id);
                Core_cacheClear('products');
                if (@$data[$headers['_categories']]) {
                    $catnames = explode('|', $data[$headers['_categories']]);
                    foreach ($catnames as $catname) {
                        $cat = ProductCategory::getInstanceByName($catname);
                        if (!$cat) {
                            continue;
                        }
                        ProductsCategoriesProducts::insert($cat->vals['id'], $id);
                        Products_categoriesRecount(array($id));
                    }
                }
                break;
                // }
            // }
            case '0':
                break;
            default:
                // {
                ProductsCategoriesProducts::deleteByProductId($id);
                ProductsCategoriesProducts::insert($cid, $id);
                break;
                // }
        }
        $imported++;
    }
    // }
    Core_cacheClear('products');
    if ($imported) {
        return array('message' => __('Imported %1 products', array($imported), 'core'));
    }
    return array('message' => __('No products imported'));
}
Esempio n. 2
0
/**
 * add a product to a category
 *
 * @return null
 */
function Products_adminCategoryProductAdd()
{
    $pids = explode(',', $_REQUEST['pid']);
    $cid = (int) $_REQUEST['cid'];
    $arr = array();
    foreach ($pids as $pid) {
        $pid = (int) $pid;
        $arr[] = (int) $pid;
        ProductsCategoriesProducts::delete($cid, $pid);
    }
    foreach ($pids as $pid) {
        $pid = (int) $pid;
        ProductsCategoriesProducts::insert($cid, $pid);
    }
    Products_categoriesRecount($pids);
    dbQuery('update products set date_edited=now() where id in (' . join(', ', $arr) . ')');
    Core_cacheClear('products');
    return array('ok' => 1);
}
Esempio n. 3
0
 if ($id) {
     $sql = "update products {$sql} where id={$id}";
     dbQuery($sql);
 } else {
     dbQuery("insert into products {$sql},date_created=now()");
     $id = dbLastInsertId();
 }
 // }
 // { save categories
 ProductsCategoriesProducts::deleteByProductId($id);
 if (!isset($_REQUEST['product_categories'])) {
     $type = ProductType::getInstance((int) $_REQUEST['product_type_id']);
     $_REQUEST['product_categories'] = array((string) $type->default_category => 'on');
 }
 foreach ($_REQUEST['product_categories'] as $key => $val) {
     ProductsCategoriesProducts::insert($key, $id);
 }
 // }
 // { save product relations
 $rls = array();
 foreach ($relations as $r) {
     $rls[$r['id']] = $r;
     if ($r['one_way']) {
         dbQuery('delete from products_relations where from_id=' . $id . ' and relation_id=' . $r['id']);
     } else {
         dbQuery('delete from products_relations where (from_id=' . $id . ' or to_id=' . $id . ') and relation_id=' . $r['id']);
     }
 }
 if (isset($_REQUEST['product-relations-type'])) {
     foreach ($_REQUEST['product-relations-type'] as $k => $v) {
         if ($v && $_REQUEST['products-relations-product'][$k]) {