static function import($file, $offer)
 {
     global $CONFIG, $ST;
     $log_file = 'import/import_log.txt';
     set_time_limit(1000);
     $start_time = time();
     $result = "Не найден контрольный файл";
     $goods = array();
     if ($offer == 1) {
         $goods = CatImp::cat1($file);
     }
     if ($offer == 2) {
         $goods = CatImp::cat2($file);
     }
     if ($offer == 11) {
         $goods = CatImp::cat1c1('import');
     }
     $goods = $goods['goods'];
     $lnk = array();
     $rs = $ST->select("SELECT * FROM sc_shop_srv_extcat WHERE offer={$offer}");
     while ($rs->next()) {
         $lnk[$rs->get('id')] = $rs->getInt('lnk');
     }
     $cnt_imp = 0;
     $cnt_img = 0;
     $cnt_upd = 0;
     $cnt_ins = 0;
     if ($goods) {
         $lnk['00000000-0000-0000-0000-000000000000'] = 0;
         foreach ($goods as $gid => $g) {
             $cnt_imp++;
             if (isset($lnk[$g['ext_category']]) && isset($g['price'])) {
                 $data = array('price' => $g['price'], 'in_stock' => 1);
                 if ($g['ext_category'] == '00000000-0000-0000-0000-000000000000') {
                 } else {
                     $data['category'] = $lnk[$g['ext_category']];
                 }
                 if (isset($g['manufacturer'])) {
                     $data['manufacturer'] = @$g['manufacturer'];
                 }
                 if (true) {
                     $ST->update("sc_shop_item", array('in_stock' => 0), "offer={$offer}");
                 }
                 $rs = $ST->select("SELECT * FROM sc_shop_item WHERE ext_id='{$gid}' AND offer={$offer}");
                 if ($rs->next()) {
                     $id = $rs->getInt('id');
                     if ($rs->get('price') != $data['price'] && $rs->get('in_stock') != $data['in_stock']) {
                         //&& (isset($data['category']) && $rs->get('category')!=$data['category'])
                         $ST->update('sc_shop_item', $data, "id={$id}");
                         $cnt_upd++;
                     }
                 } else {
                     $data['name'] = $g['name'];
                     $data['description'] = $g['description'];
                     $data['ext_id'] = $gid;
                     $data['offer'] = $offer;
                     $id = $ST->insert('sc_shop_item', $data);
                     $cnt_ins++;
                 }
                 //					continue;
                 $filename = $CONFIG['CATALOG_PATH'] . '/goods/' . $id . '.jpg';
                 if (!empty($g['image_url']) && !file_exists(ROOT . $filename)) {
                     $img_content = @file_get_contents($g['image_url']);
                     if (!$img_content || strpos($img_content, 'rror')) {
                         continue;
                     }
                     file_put_contents(ROOT . $filename, $img_content);
                     $ST->update('sc_shop_item', array('img' => $filename), 'id=' . $id);
                     $cnt_img++;
                 }
             }
         }
         $stop_time = time();
         $t = $stop_time - $start_time;
         $result = date('Y-m-d H:i:s') . ' - Время загрузки=' . $t . '; Импортировано=' . $cnt_imp . '; Добавлено=' . $cnt_ins . '; Обновлено=' . $cnt_upd . '; Обновлено изображний=' . $cnt_img . "\r\n";
         file_put_contents($log_file, $result, FILE_APPEND);
         return array('t' => $t, 'imp' => $cnt_imp, 'ins' => $cnt_ins, 'upd' => $cnt_upd, 'msg' => $result);
     } else {
         $result = date('Y-m-d H:i:s') . " - Список пуст\r\n";
         file_put_contents($log_file, $result, FILE_APPEND);
         return array('msg' => $result);
     }
     return $result;
 }
Ejemplo n.º 2
0
 static function import($file, $offer)
 {
     global $CONFIG, $ST;
     $log_file = 'import/import_log.txt';
     set_time_limit(1000);
     $start_time = time();
     $result = "Не найден контрольный файл";
     $goods = array();
     if ($offer == 1) {
         $goods = CatImp::cat1($file);
     }
     if ($offer == 2) {
         $goods = CatImp::cat2($file);
     }
     if ($offer == 4) {
         $goods = CatImp::cat4($file);
     }
     if ($offer == 11) {
         $goods = CatImp::cat1c1('import');
     }
     $only_changes = null;
     if (isset($goods['only_changes'])) {
         $only_changes = $goods['only_changes'];
     }
     $goods = $goods['goods'];
     $lnk = array();
     $rs = $ST->select("SELECT * FROM sc_shop_srv_extcat WHERE offer={$offer}");
     while ($rs->next()) {
         $lnk[$rs->get('id')] = $rs->getInt('lnk');
     }
     $cnt_imp = 0;
     $cnt_img = 0;
     $cnt_upd = 0;
     $cnt_ins = 0;
     if ($goods) {
         if ($only_changes === false) {
             $gids = array_keys($goods);
             if (@$CONFIG['SHOP_IMP_FULL_REMOVE'] == 'true') {
                 //удалять товары при полной выгрузке
                 $ST->update("sc_shop_item", array('in_stock' => -1), "offer={$offer} AND ext_id NOT IN(" . implode(',', $gids) . ")");
             } else {
                 //просто обнулять
                 $ST->update("sc_shop_item", array('in_stock' => 0), "offer={$offer} AND in_stock>-1 AND ext_id NOT IN(" . implode(',', $gids) . ")");
             }
         }
         $lnk['00000000-0000-0000-0000-000000000000'] = 0;
         foreach ($goods as $gid => $g) {
             $cnt_imp++;
             $data = array('in_stock' => $g['in_stock']);
             if ($g['price']) {
                 $data['price'] = $g['price'];
             }
             //				if(isset($lnk[$g['ext_category']]) && isset($g['price']) ){
             if ($g['ext_category'] == '00000000-0000-0000-0000-000000000000') {
             } elseif (isset($lnk[$g['ext_category']])) {
                 $data['category'] = $lnk[$g['ext_category']];
             }
             if (isset($g['manufacturer'])) {
                 $data['manufacturer'] = @$g['manufacturer'];
             }
             if (!empty($g['vendor'])) {
                 $rs = $ST->select("SELECT * FROM sc_manufacturer WHERE name='" . SQL::slashes($g['vendor']) . "'");
                 if ($rs->next()) {
                     $data['manufacturer_id'] = $rs->getInt('id');
                 } else {
                     $data['manufacturer_id'] = $ST->insert('sc_manufacturer', array('name' => $g['vendor']));
                 }
             }
             if (!empty($g['name'])) {
                 $data['name'] = $g['name'];
             }
             if (!empty($g['description'])) {
                 $data['description'] = $g['description'];
             }
             if (!empty($g['html2'])) {
                 $data['html2'] = $g['html2'];
             }
             if (!empty($g['weight_flg'])) {
                 $data['weight_flg'] = $g['weight_flg'] == 'true' ? 1 : 0;
             }
             $rs = $ST->select("SELECT * FROM sc_shop_item WHERE ext_id='{$gid}' AND offer={$offer}");
             if ($rs->next()) {
                 $id = $rs->getInt('id');
                 $add_data = array();
                 foreach (array('category', 'manufacturer_id', 'weight_flg', 'name', 'price', 'in_stock', 'description', 'html2') as $k) {
                     if (isset($data[$k]) && $rs->get($k) != $data[$k]) {
                         $add_data[$k] = $data[$k];
                     }
                 }
                 if ($add_data) {
                     foreach ($add_data as $k => $v) {
                         if (!in_array($k, array('price', 'in_stock'))) {
                             $add_data['sort3'] = 1;
                             break;
                         }
                     }
                     //							if(isset($add_data['price']) && $rs->getFloat('price')!=$add_data['price']){
                     //								$add_data['old_price']=$rs->getFloat('price');
                     //							}
                     $add_data['update_time'] = date('Y-m-d H:i:s');
                     $ST->update('sc_shop_item', $add_data, "id={$id}");
                     $cnt_upd++;
                     $ST->insert('sc_shop_log', array('type' => 'goodsimport', 'time' => date('Y-m-d H:i:s'), 'data' => serialize(array('id' => $id) + $add_data)));
                 }
             } else {
                 if (empty($data['category'])) {
                     continue;
                 }
                 $data['ext_id'] = $gid;
                 $data['offer'] = $offer;
                 $data['sort3'] = 1;
                 $data['insert_time'] = date('Y-m-d H:i:s');
                 $id = $ST->insert('sc_shop_item', $data);
                 $cnt_ins++;
             }
             //Свойства
             if (!empty($g['param'])) {
                 foreach ($g['param'] as $p => $v) {
                     $rs = $ST->select("SELECT id FROM sc_shop_prop WHERE name='" . SQL::slashes($p) . "'");
                     if ($rs->next()) {
                         $pid = $rs->getInt('id');
                     } else {
                         $pid = $ST->insert('sc_shop_prop', array('name' => $p));
                     }
                     $rs = $ST->select("SELECT * FROM sc_shop_prop_val WHERE item_id={$id} AND prop_id={$pid}");
                     if ($rs->next()) {
                         if ($v != $rs->get('value')) {
                             $ST->update('sc_shop_prop_val', array('value' => $v), "id={$rs->get('id')}");
                         }
                     } else {
                         $ST->insert('sc_shop_prop_val', array('value' => $v, 'item_id' => $id, 'prop_id' => $pid));
                     }
                 }
             }
             //					continue;
             $filename = $CONFIG['CATALOG_PATH'] . '/goods/' . $id . '.jpg';
             if (!empty($g['image_url']) && !file_exists(ROOT . $filename)) {
                 $img_content = @file_get_contents($g['image_url']);
                 if (!$img_content || strpos($img_content, 'rror')) {
                     continue;
                 }
                 file_put_contents(ROOT . $filename, $img_content);
                 $ST->update('sc_shop_item', array('img' => $filename), 'id=' . $id);
                 $cnt_img++;
             }
             //				}
         }
         $stop_time = time();
         $t = $stop_time - $start_time;
         $result = date('Y-m-d H:i:s') . ' - Время загрузки=' . $t . '; Импортировано=' . $cnt_imp . '; Добавлено=' . $cnt_ins . '; Обновлено=' . $cnt_upd . '; Обновлено изображний=' . $cnt_img . "\r\n";
         file_put_contents($log_file, $result, FILE_APPEND);
         return array('t' => $t, 'imp' => $cnt_imp, 'ins' => $cnt_ins, 'upd' => $cnt_upd, 'msg' => $result);
     } else {
         $result = date('Y-m-d H:i:s') . " - Список пуст\r\n";
         file_put_contents($log_file, $result, FILE_APPEND);
         return array('msg' => $result);
     }
     return $result;
 }
Ejemplo n.º 3
0
 function actExtCat()
 {
     global $ST, $get;
     $lnk = array();
     $cond = '';
     $type = 4;
     if ($t = $get->get('type')) {
         $type = $t;
     }
     $cond .= " AND offer={$type}";
     $rs = $ST->select("SELECT * FROM sc_shop_srv_extcat WHERE 1=1 {$cond}");
     while ($rs->next()) {
         $lnk[$rs->get('id')] = $rs->getInt('lnk');
     }
     $dir = CATALOG_DIR;
     $catalog_file = '';
     //		$catalog_file=CATALOG_FILE_NAME;
     if ($f = $get->get('file')) {
         $catalog_file = $f;
     }
     if ($type == 1) {
         $data = CatImp::cat1($dir . '/' . $catalog_file);
     }
     if ($type == 2) {
         $data = CatImp::cat2($dir . '/' . $catalog_file);
     }
     if ($type == 4) {
         $data = CatImp::cat4($dir . '/' . $catalog_file);
     }
     if ($type == 11) {
         $data = CatImp::cat1c1($dir);
     }
     $this->setTitle('Настройка импорта каталога тип ' . $get->get('type'));
     $this->explorer[] = array('name' => 'Настройка импорта каталога тип ' . $get->get('type'));
     /*пока тип файла привяжем к прайсу*/
     $this->display(array('offer' => $type, 'lnk' => $lnk, 'catalog' => $data['catalog'], 'count' => $data['count'], 'file' => $dir . '/' . $catalog_file), dirname(__FILE__) . '/admin_catsrv_extcat.tpl.php');
 }