예제 #1
0
        private void ReadRedirection(Model.Local.Article Article)
        {
            try
            {
                Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository();
                Article = ArticleRepository.ReadArticle(Article.Art_Id);
                Model.Prestashop.DataClassesPrestashop      DBPrestashop = new Model.Prestashop.DataClassesPrestashop(new MySqlConnection(Properties.Settings.Default.PRESTASHOPConnectionString));
                List <Model.Prestashop.Product_Redirection> list         = DBPrestashop.ExecuteQuery <Model.Prestashop.Product_Redirection>
                                                                               ("select redirect_type, " +
                                        #if (PRESTASHOP_VERSION_172)
                                                                               "id_type_redirected " +
                                        #else
                                                                               "id_product_redirected " +
                                        #endif
                                                                               " from ps_product_shop where id_product = " + Article.Pre_Id + " and id_shop = " + Core.Global.CurrentShop.IDShop + " ").ToList();

                if (list != null && list.Count == 1)
                {
                    Model.Prestashop.Product_Redirection values = list.FirstOrDefault();
                    Article.Art_RedirectType = values.redirect_type;
                                        #if (PRESTASHOP_VERSION_172)
                    Article.Art_RedirectProduct = (ArticleRepository.ExistPre_Id((int)values.id_type_redirected)) ? ArticleRepository.ReadPre_Id((int)values.id_type_redirected).Art_Id : 0;
                                        #else
                    Article.Art_RedirectProduct = (ArticleRepository.ExistPre_Id((int)values.id_product_redirected)) ? ArticleRepository.ReadPre_Id((int)values.id_product_redirected).Art_Id : 0;
                                        #endif
                    ArticleRepository.Save();
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }
        public void Exec(Int32 ImageSend)
        {
            try
            {
                Model.Local.ArticleImageRepository ArticleImageRepository = new Model.Local.ArticleImageRepository();
                if (ArticleImageRepository.ExistPre_Id(ImageSend) == false)
                {
                    Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository();
                    Model.Prestashop.PsImage           PsImage           = PsImageRepository.ReadImage(Convert.ToUInt32(ImageSend));
                    Model.Local.ArticleRepository      ArticleRepository = new Model.Local.ArticleRepository();
                    if (ArticleRepository.ExistPre_Id(Convert.ToInt32(PsImage.IDProduct)))
                    {
                        Model.Local.Article Article = ArticleRepository.ReadPre_Id(Convert.ToInt32(PsImage.IDProduct));

                        Model.Prestashop.PsImageLangRepository PsImageLangRepository = new Model.Prestashop.PsImageLangRepository();
                        if (PsImageLangRepository.ExistImageLang(Convert.ToUInt32(ImageSend), Core.Global.Lang))
                        {
                            string extension = Core.Img.jpgExtension;
                            String FTP       = Core.Global.GetConfig().ConfigFTPIP;
                            String User      = Core.Global.GetConfig().ConfigFTPUser;
                            String Password  = Core.Global.GetConfig().ConfigFTPPassword;

                            Model.Prestashop.PsImageLang PsImageLang  = PsImageLangRepository.ReadImageLang(Convert.ToUInt32(ImageSend), Core.Global.Lang);
                            Model.Local.ArticleImage     ArticleImage = new Model.Local.ArticleImage()
                            {
                                ImaArt_Name       = (!String.IsNullOrEmpty(PsImageLang.Legend)) ? PsImageLang.Legend : Article.Art_Ref,
                                ImaArt_Position   = PsImage.Position,
                                ImaArt_Default    = Convert.ToBoolean(PsImage.Cover),
                                Pre_Id            = Convert.ToInt32(PsImage.IDImage),
                                Art_Id            = Article.Art_Id,
                                ImaArt_Image      = string.Empty,
                                ImaArt_SourceFile = SearchFreeNameFile(Article.Art_Id, Article.Art_Ref, extension),
                                ImaArt_DateAdd    = DateTime.Now
                            };
                            ArticleImageRepository.Add(ArticleImage);

                            Boolean import_img = false;
                            try
                            {
                                // <JG> 10/04/2013 gestion système d'images
                                string ftpPath = "/img/p/";
                                switch (Core.Global.GetConfig().ConfigImageStorageMode)
                                {
                                case Core.Parametres.ImageStorageMode.old_system:
                                    #region old_system
                                    // no action on path
                                    break;
                                    #endregion

                                case Core.Parametres.ImageStorageMode.new_system:
                                default:
                                    #region new_system

                                    foreach (char directory in PsImage.IDImage.ToString())
                                    {
                                        ftpPath += directory + "/";
                                    }
                                    break;
                                    #endregion
                                }

                                // <JG> 21/05/2013 import image originale
                                Boolean import_img_tmp = false;
                                try
                                {
                                    string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system)
                                        ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + Core.Img.jpgExtension
                                        : FTP + ftpPath + PsImage.IDImage + Core.Img.jpgExtension;

                                    bool existfile = Core.Ftp.ExistFile(ftpfullpath, User, Password);
                                    if (existfile)
                                    {
                                        System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath);
                                        ftp.Credentials = new System.Net.NetworkCredential(User, Password);
                                        ftp.UseBinary   = true;
                                        ftp.UsePassive  = true;
                                        ftp.KeepAlive   = false;
                                        ftp.EnableSsl   = Core.Global.GetConfig().ConfigFTPSSL;

                                        System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse();
                                        Stream reader = response.GetResponseStream();

                                        MemoryStream memStream      = new MemoryStream();
                                        byte[]       buffer         = new byte[1024];
                                        byte[]       downloadedData = new byte[0];
                                        while (true)
                                        {
                                            int bytesRead = reader.Read(buffer, 0, buffer.Length);
                                            if (bytesRead != 0)
                                            {
                                                memStream.Write(buffer, 0, bytesRead);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                            downloadedData = memStream.ToArray();
                                        }

                                        string target_folder = (Core.Global.GetConfig().ConfigLocalStorageMode == Parametres.LocalStorageMode.simple_system)
                                            ? Global.GetConfig().Folders.TempArticle
                                            : ArticleImage.advanced_folder;

                                        if (downloadedData != null && downloadedData.Length != 0)
                                        {
                                            FileStream newFile = new FileStream(
                                                System.IO.Path.Combine(target_folder, String.Format("{0}" + extension, ArticleImage.ImaArt_Id)),
                                                FileMode.Create);
                                            newFile.Write(downloadedData, 0, downloadedData.Length);
                                            newFile.Close();
                                            newFile.Dispose();
                                            memStream.Dispose();
                                            downloadedData = new byte[0];
                                        }
                                        string local_file_tmp = System.IO.Path.Combine(target_folder, String.Format("{0}" + extension, ArticleImage.ImaArt_Id));

                                        // <JG> 30/09/2013 détection encodage PNG lors de l'import
                                        Boolean rename_to_png    = false;
                                        System.Drawing.Image img = System.Drawing.Image.FromFile(local_file_tmp);
                                        var imgguid = img.RawFormat.Guid;
                                        img.Dispose();
                                        System.Drawing.Imaging.ImageCodecInfo search;
                                        foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders())
                                        {
                                            if (codec.FormatID == imgguid)
                                            {
                                                search = codec;
                                                if (search.FormatDescription == "PNG")
                                                {
                                                    rename_to_png = true;
                                                }
                                                break;
                                            }
                                        }
                                        if (rename_to_png)
                                        {
                                            if (System.IO.File.Exists(local_file_tmp))
                                            {
                                                extension = Core.Img.pngExtension;
                                                System.IO.File.Move(local_file_tmp, System.IO.Path.Combine(target_folder, String.Format("{0}" + extension, ArticleImage.ImaArt_Id)));
                                            }
                                            ArticleImage.ImaArt_SourceFile = SearchFreeNameFile(Article.Art_Id, Article.Art_Ref, extension);
                                        }
                                        ArticleImage.ImaArt_Image = String.Format("{0}" + extension, ArticleImage.ImaArt_Id);
                                        ArticleImageRepository.Save();

                                        Core.Img.resizeImage(new System.Drawing.Bitmap(ArticleImage.TempFileName),
                                                             Core.Global.GetConfig().ConfigImageMiniatureWidth,
                                                             Core.Global.GetConfig().ConfigImageMiniatureHeight,
                                                             ArticleImage.SmallFileName);
                                        import_img_tmp = true;
                                    }
                                }
                                catch (Exception)
                                {
                                    // Not implemented
                                }
                                if (import_img_tmp)
                                {
                                    Model.Prestashop.PsImageTypeRepository PsImageTypeRepository = new Model.Prestashop.PsImageTypeRepository();
                                    List <Model.Prestashop.PsImageType>    ListPsImageType       = PsImageTypeRepository.ListProduct(1);
                                    foreach (Model.Prestashop.PsImageType PsImageType in ListPsImageType)
                                    {
                                        string ftpfullpath = (Core.Global.GetConfig().ConfigImageStorageMode == Core.Parametres.ImageStorageMode.old_system)
                                            ? FTP + ftpPath + PsImage.IDProduct + "-" + PsImage.IDImage + "-" + PsImageType.Name + Core.Img.jpgExtension
                                            : FTP + ftpPath + PsImage.IDImage + "-" + PsImageType.Name + Core.Img.jpgExtension;

                                        System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath);
                                        ftp.Credentials = new System.Net.NetworkCredential(User, Password);
                                        ftp.UseBinary   = true;
                                        ftp.UsePassive  = true;
                                        ftp.KeepAlive   = false;
                                        ftp.EnableSsl   = Core.Global.GetConfig().ConfigFTPSSL;

                                        System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse();
                                        Stream reader = response.GetResponseStream();

                                        MemoryStream memStream      = new MemoryStream();
                                        byte[]       buffer         = new byte[1024];
                                        byte[]       downloadedData = new byte[0];
                                        while (true)
                                        {
                                            int bytesRead = reader.Read(buffer, 0, buffer.Length);
                                            if (bytesRead != 0)
                                            {
                                                memStream.Write(buffer, 0, bytesRead);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                            downloadedData = memStream.ToArray();
                                        }

                                        if (downloadedData != null && downloadedData.Length != 0)
                                        {
                                            FileStream newFile = new FileStream(ArticleImage.FileName(PsImageType.Name), FileMode.Create);
                                            newFile.Write(downloadedData, 0, downloadedData.Length);
                                            newFile.Close();
                                        }
                                    }
                                    import_img = true;

                                    // gestion image par défaut
                                    if (ArticleImage.ImaArt_Default)
                                    {
                                        List <Model.Local.ArticleImage> ListArticleImage = ArticleImageRepository.ListArticle(ArticleImage.Art_Id.Value);
                                        if (ListArticleImage.Count(i => i.ImaArt_Default == true && i.ImaArt_Id != ArticleImage.ImaArt_Id) > 0)
                                        {
                                            foreach (Model.Local.ArticleImage ArticleImageDefault in ListArticleImage.Where(i => i.ImaArt_Default == true && i.ImaArt_Id != ArticleImage.ImaArt_Id))
                                            {
                                                ArticleImageDefault.ImaArt_Default = false;
                                                ArticleImageRepository.Save();
                                            }
                                        }
                                    }

                                    // liens images déclinaisons
                                    ExecAttributeImage(PsImage, ArticleImage);
                                }
                            }
                            catch (Exception ex)
                            {
                                Core.Error.SendMailError("[DOWNLOAD FTP IMAGE ARTICLE]<br />" + ex.ToString());
                                ArticleImageRepository.Delete(ArticleImage);
                            }
                            if (!import_img)
                            {
                                ArticleImageRepository.Delete(ArticleImage);
                            }
                        }
                    }
                }
                else if (ArticleImageRepository.ExistPrestaShop(ImageSend))
                {
                    // import des affectations aux déclinaisons
                    Model.Prestashop.PsImageRepository PsImageRepository = new Model.Prestashop.PsImageRepository();
                    Model.Prestashop.PsImage           PsImage           = PsImageRepository.ReadImage(Convert.ToUInt32(ImageSend));
                    Model.Local.ArticleImage           ArticleImage      = ArticleImageRepository.ReadPrestaShop(ImageSend);
                    ExecAttributeImage(PsImage, ArticleImage);
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }
        public void Exec(Model.Prestashop.PsProductAttachment PsProductAttachment)
        {
            try
            {
                //<YH> 21/08/2012
                string DirAttachment = Global.GetConfig().Folders.RootAttachment;

                Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository();
                if (ArticleRepository.ExistPre_Id(Convert.ToInt32(PsProductAttachment.IDProduct)))
                {
                    Model.Local.Article Article = ArticleRepository.ReadPre_Id(Convert.ToInt32(PsProductAttachment.IDProduct));
                    Model.Local.AttachmentRepository AttachmentRepository = new Model.Local.AttachmentRepository();
                    if (AttachmentRepository.ExistPre_IdArt_Id(Convert.ToInt32(PsProductAttachment.IDAttachment), Article.Art_Id) == false)
                    {
                        Model.Prestashop.PsAttachmentRepository     PsAttachmentRepository     = new Model.Prestashop.PsAttachmentRepository();
                        Model.Prestashop.PsAttachment               PsAttachment               = PsAttachmentRepository.ReadAttachment(PsProductAttachment.IDAttachment);
                        Model.Prestashop.PsAttachmentLangRepository PsAttachmentLangRepository = new Model.Prestashop.PsAttachmentLangRepository();
                        if (PsAttachmentLangRepository.ExistAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang))
                        {
                            Model.Prestashop.PsAttachmentLang PsAttachmentLang = PsAttachmentLangRepository.ReadAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang);
                            Model.Local.Attachment            Attachment       = new Model.Local.Attachment()
                            {
                                Att_FileName    = PsAttachment.FileName,
                                Att_Description = PsAttachmentLang.Description,
                                Att_Mime        = PsAttachment.Mime,
                                Att_Name        = PsAttachmentLang.Name,
                                Att_File        = this.ReadFile(DirAttachment, PsAttachment.File),
                                Pre_Id          = Convert.ToInt32(PsAttachment.IDAttachment),
                                Art_Id          = Article.Art_Id
                            };
                            AttachmentRepository.Add(Attachment);

                            String FTP      = Core.Global.GetConfig().ConfigFTPIP;
                            String User     = Core.Global.GetConfig().ConfigFTPUser;
                            String Password = Core.Global.GetConfig().ConfigFTPPassword;

                            // <JG> 21/05/2013 correction recherche fichier sur le ftp
                            string ftpfullpath           = FTP + "/download/" + PsAttachment.File;
                            System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath);
                            ftp.Credentials = new System.Net.NetworkCredential(User, Password);
                            ftp.UseBinary   = true;
                            ftp.UsePassive  = true;
                            ftp.KeepAlive   = false;
                            ftp.EnableSsl   = Core.Global.GetConfig().ConfigFTPSSL;

                            System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse();
                            Stream reader = response.GetResponseStream();

                            MemoryStream memStream      = new MemoryStream();
                            byte[]       buffer         = new byte[1024];
                            byte[]       downloadedData = new byte[0];
                            while (true)
                            {
                                int bytesRead = reader.Read(buffer, 0, buffer.Length);
                                if (bytesRead != 0)
                                {
                                    memStream.Write(buffer, 0, bytesRead);
                                }
                                else
                                {
                                    break;
                                }
                                downloadedData = memStream.ToArray();
                            }

                            if (downloadedData != null && downloadedData.Length != 0)
                            {
                                FileStream newFile = new FileStream(DirAttachment + Attachment.Att_File, FileMode.Create);
                                newFile.Write(downloadedData, 0, downloadedData.Length);
                                newFile.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }
예제 #4
0
        public void Exec(Int32 ProductSend)
        {
            try
            {
                Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository();
                if (ArticleRepository.ExistPre_Id(ProductSend) == false)
                {
                    Model.Prestashop.PsProductLangRepository PsProductLangRepository = new Model.Prestashop.PsProductLangRepository();
                    if (PsProductLangRepository.ExistProductLang(ProductSend, Global.Lang, Global.CurrentShop.IDShop))
                    {
                        Model.Prestashop.PsProductRepository PsProductRepository = new Model.Prestashop.PsProductRepository();
                        Model.Prestashop.PsProduct           PsProduct           = PsProductRepository.ReadId(Convert.ToUInt32(ProductSend));
                        Model.Prestashop.PsProductLang       PsProductLang       = PsProductLangRepository.ReadProductLang(ProductSend, Global.Lang, Global.CurrentShop.IDShop);

                        Model.Sage.F_ARTICLERepository F_ARTICLERepository = new Model.Sage.F_ARTICLERepository();
                        string product_reference = PsProduct.Reference;
                        product_reference = product_reference.Replace(" ", "_");
                        if (F_ARTICLERepository.ExistReference(product_reference))
                        {
                            Model.Sage.F_ARTICLE F_ARTICLE = F_ARTICLERepository.ReadReference(product_reference);
                            Model.Local.Article  Article   = new Model.Local.Article()
                            {
                                Art_Name              = PsProductLang.Name,
                                Art_Description       = PsProductLang.Description,
                                Art_Description_Short = PsProductLang.DescriptionShort,
                                Art_LinkRewrite       = PsProductLang.LinkRewrite,
                                Art_MetaTitle         = PsProductLang.MetaTitle,
                                Art_MetaKeyword       = PsProductLang.MetaKeywords,
                                Art_MetaDescription   = PsProductLang.MetaDescription,
                                Art_Ref             = PsProduct.Reference,
                                Art_Ean13           = PsProduct.EAn13,
                                Art_Pack            = PsProduct.CacheIsPack == 1,
                                Art_Solde           = Convert.ToBoolean(PsProduct.OnSale),
                                Art_Active          = Convert.ToBoolean(PsProduct.Active),
                                Art_Sync            = true,
                                Art_SyncPrice       = true,
                                Art_Date            = (PsProduct.DateUpd != null && PsProduct.DateUpd > new DateTime(1753, 1, 2)) ? PsProduct.DateUpd : DateTime.Now.Date,
                                Sag_Id              = F_ARTICLE.cbMarq,
                                Pre_Id              = Convert.ToInt32(PsProduct.IDProduct),
                                Cat_Id              = this.ReadCatalog(PsProduct.IDCategoryDefault),
                                Art_RedirectType    = new Model.Internal.RedirectType(Core.Parametres.RedirectType.NoRedirect404).Page,
                                Art_RedirectProduct = 0,
                                Art_Manufacturer    = (PsProduct.IDManufacturer != null) ? (int)PsProduct.IDManufacturer : 0,
                                Art_Supplier        = (PsProduct.IDSupplier != null) ? (int)PsProduct.IDSupplier : 0,
                            };

                            if (Article.Cat_Id == 0)
                            {
                                foreach (Model.Prestashop.PsCategoryProduct PsCategoryProduct in new Model.Prestashop.PsCategoryProductRepository().ListProduct(PsProduct.IDProduct))
                                {
                                    Article.Cat_Id = this.ReadCatalog(PsCategoryProduct.IDCategory);
                                    if (Article.Cat_Id != 0)
                                    {
                                        break;
                                    }
                                }
                            }

                            if (PsProduct.CacheIsPack == 1)
                            {
                                Article.Art_Pack = true;
                            }
                            if (Article.Cat_Id != 0)
                            {
                                ArticleRepository.Add(Article);
                                RecoveryChildData(Article, PsProduct, ArticleRepository, false);
                            }
                        }
                    }
                }
                else
                {
                    Model.Local.Article Article = ArticleRepository.ReadPre_Id(ProductSend);
                    Model.Prestashop.PsProductRepository PsProductRepository = new Model.Prestashop.PsProductRepository();
                    Model.Prestashop.PsProduct           PsProduct           = PsProductRepository.ReadId(Convert.ToUInt32(ProductSend));
                    RecoveryChildData(Article, PsProduct, ArticleRepository, true);
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }