Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.67 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: GRAPHICBOX
  5.  * Date: 20.06.2017
  6.  * Time: 10:45
  7.  */
  8. $servername = "127.0.0.1";
  9. $username = "root";
  10. $password = "";
  11.  
  12. // Create connection
  13. $conn = new mysqli($servername, $username, $password,"polampy");
  14. if ($conn->connect_error) {
  15.     die("Connection failed: " . $conn->connect_error);
  16. }
  17. echo "Connected successfully";
  18. $conn->set_charset("utf8");
  19.  
  20. $productsSql = "SELECT * FROM products ";
  21.  
  22. $products = $conn->query($productsSql);
  23.  
  24.  
  25. $attributesSql = "SELECT * FROM attributes";
  26. $attributesResult = $conn->query($attributesSql);
  27.  
  28. $attributes = [];
  29. while($attributeRow = $attributesResult->fetch_assoc())
  30. {
  31.     $attributes[] = $attributeRow;
  32. }
  33.  
  34. $outArr = [];
  35. while($product = $products->fetch_assoc())
  36. {
  37.     $prod = new Product(count($attributes));
  38.     $productId = $product['product_id'];
  39.  
  40.     $stockSql = "SELECT * FROM products_stock WHERE product_id=".$productId;
  41.     $stock = $conn->query($stockSql)->fetch_assoc();
  42.  
  43.  
  44.     $prod->code = $stock['code'];
  45.  
  46.  
  47.     $prod->productId = $productId;
  48.  
  49.     $translationSql = "SELECT * FROM products_translations WHERE product_id = ".$productId;
  50.     $translation = $conn->query($translationSql)->fetch_assoc();
  51.  
  52.     $prod->name = n($translation['name']);
  53.  
  54.     $prod->description = n(strip_tags($translation['description']));
  55.  
  56.     $categorySql = "SELECT * FROM products_categories WHERE product_id = ".$productId;
  57.  
  58.     $category = $conn->query($categorySql)->fetch_assoc();
  59.  
  60.  
  61.     $categoryId = $category['category_id'];
  62.  
  63.     fillWithCategories($categoryId,$prod,$conn);
  64.     fillWithAttributes($prod,$conn,$attributes);
  65.     fillWithPhotos($prod,$conn);
  66. //    $categoryMap = $conn->query($categoryMapSql)->fetch_assoc();
  67.  
  68. $outArr[] = $prod;
  69.     print_r($prod);
  70. //    die();
  71. //    die();
  72.  
  73. }
  74.  
  75.  
  76. $file = __DIR__."/polamp.csv";
  77. //file_put_contents($file,"");
  78.  
  79. $firstRow = ["Kod","Nazwa","Opis","Kategoria główna", "Podkategoria 1","Podkategoria 2","Podkategoria 3"];
  80. foreach($attributes as $attr)
  81. {
  82.     $firstRow[] = $attr['name'];
  83. }
  84. $firstRow[] = "Zdjęcia";
  85.  
  86. file_put_contents($file,implode(";",$firstRow)."\n");
  87. /** @var Product $item */
  88. foreach($outArr as $item)
  89. {
  90.     $outRowArr = [];
  91.     $outRowArr[] = n($item->code);
  92.     $outRowArr[] = n($item->name);
  93.     $outRowArr[] = str_replace(["Do pobrania:Karta katalogowaCertyfikat CE","Do pobrania:Karta katalogowa Certyfikat CE","Do pobrania:Certyfikat CE","Do pobraniaKarta katalogowa Certyfikat CE","Do pobrania:  Karta katalogowa Certyfikat CE","Do pobrania:Karta katalogowaCertyfikat CE "],["","","","","",""],n($item->description));
  94. //    $descr = mb_ereg_replace('/\s\s+/', ' ',$descr);
  95. //    $outRowArr[] = mb_ereg_replace('/\t+/', ' ',$descr);
  96.     $outRowArr[] = n(implode(";",$item->categories));
  97.     $outRowArr[] = n(implode(";",$item->attributes));
  98.     $outRowArr[] = n(implode(", ",$item->photos));
  99.  
  100.     file_put_contents($file,implode(";",$outRowArr)."\n",FILE_APPEND);
  101. }
  102.  
  103.  
  104. function fillWithCategories($mainCategoryId,Product $prod, mysqli $conn)
  105. {
  106.     $categoryMapSql = "SELECT * FROM categories_map WHERE child_id=".$mainCategoryId." ORDER BY level ASC";
  107.  
  108.     $map = $conn->query($categoryMapSql);
  109.  
  110. //    $categoriesCount = $map->num_rows;
  111.  
  112.     $i = 0;
  113.     while($mapRow = $map->fetch_assoc())
  114.     {
  115.         $categoryId = $mapRow['category_id'];
  116.         $categorySql = "SELECT * FROM categories_translations WHERE category_id=".$categoryId;
  117.         $category = $conn->query($categorySql)->fetch_assoc();
  118. //        $index = $categoriesCount - $mapRow['level'] - 1;
  119.         $prod->categories[$i] = $category['name'];
  120.         $i++;
  121.     }
  122.  
  123. }
  124.  
  125. function fillWithPhotos(Product $prod, mysqli $conn)
  126. {
  127.     $photosSql = "SELECT * FROM products_gfx WHERE product_id=".$prod->productId." ORDER BY main DESC";
  128.     $photos = $conn->query($photosSql);
  129.     $i = 1;
  130.     while($photo = $photos->fetch_assoc())
  131.     {
  132.  
  133.         $name = $prod->code."_zdjecie".$i;
  134.         $photoLink = "http://polampy.pl/export_images/images/".$name.".jpg";
  135.         $prod->photos[] = $photoLink;
  136. //        downloadImage($photo['unic_name'],$prod->code."_zdjecie".$i);
  137.         $i++;
  138.     }
  139. }
  140. function downloadImage($name,$outName)
  141. {
  142.     $photoLink = "http://polampy.pl/environment/cache/images/0_0_productGfx_".$name.".jpg";
  143.     $content = file_get_contents($photoLink);
  144.     file_put_contents(__DIR__."/images/".$outName.".jpg",$content);
  145.     echo "\n image:".$name;
  146. }
  147.  
  148. function fillWithAttributes(Product $prod, mysqli $conn, $attributes)
  149. {
  150.     $attributesSql = "SELECT * FROM attributes_values WHERE product_id=".$prod->productId;
  151.     $attrs = $conn->query($attributesSql);
  152.     while($attr = $attrs->fetch_assoc())
  153.     {
  154. //        $attributeInfoSql = "SELECT * FROM attributes WHERE attribute_id=".$attr['attribute_id'];
  155. //        $attrInfo = $conn->query($attributeInfoSql)->fetch_assoc();
  156. //        $prod->attributes[] = $attrInfo['name'].":".$attr['value'];
  157.  
  158.         for($i = 0; $i < count($attributes); $i++)
  159.         {
  160.             $attribute = $attributes[$i];
  161.             if($attribute['attribute_id'] == $attr['attribute_id'])
  162.             {
  163.                 $prod->attributes[$i] = $attr['value'];
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. function n($string)
  174. {
  175.     return str_replace(["\n","\r"],[" "," "],$string);
  176. }
  177.  
  178. class Product
  179. {
  180.  
  181.     public function __construct($attrCount)
  182.     {
  183.         $this->attributes = array_fill(0,$attrCount,"");
  184.         $this->categories = array_fill(0,4,"");
  185.     }
  186.  
  187.     public $productId;
  188.  
  189.     public $code;
  190.  
  191.     public $name;
  192.  
  193.     public $description;
  194.  
  195.     public $categories;
  196.  
  197.     public $attributes;
  198.  
  199.     public $photos = [];
  200.  
  201.     public $catalog;
  202.  
  203.     public $ce;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement