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());
            }
        }
Ejemplo n.º 2
0
        private void RecoveryDataProductAttribute(Model.Prestashop.PsProduct Product)
        {
            Model.Prestashop.PsProductAttributeRepository      PsProductAttributeRepository      = new Model.Prestashop.PsProductAttributeRepository();
            Model.Prestashop.PsProductAttributeShopRepository  PsProductAttributeShopRepository  = new Model.Prestashop.PsProductAttributeShopRepository();
            Model.Prestashop.PsProductAttributeImageRepository PsProductAttributeImageRepository = new Model.Prestashop.PsProductAttributeImageRepository();

            Model.Local.AttributeArticleRepository    AttributeArticleRepository    = new Model.Local.AttributeArticleRepository();
            Model.Local.ConditioningArticleRepository ConditioningArticleRepository = new Model.Local.ConditioningArticleRepository();
            Model.Local.CompositionArticleRepository  CompositionArticleRepository  = new Model.Local.CompositionArticleRepository();

            Model.Local.AttributeArticleImageRepository   AttributeArticleImageRepository   = new Model.Local.AttributeArticleImageRepository();
            Model.Local.CompositionArticleImageRepository CompositionArticleImageRepository = new Model.Local.CompositionArticleImageRepository();
            Model.Local.ArticleImageRepository            ArticleImageRepository            = new Model.Local.ArticleImageRepository();

            Model.Local.AttributeArticle    AttributeArticle;
            Model.Local.ConditioningArticle ConditioningArticle;
            Model.Local.CompositionArticle  CompositionArticle;
            Model.Local.ArticleImage        ArticleImage;

            List <Model.Prestashop.PsProductAttribute> ListPsProductAttribute = PsProductAttributeRepository.List(Product.IDProduct);

            //parcours déclinaisons PrestaShop
            foreach (Model.Prestashop.PsProductAttribute PsProductAttribute in ListPsProductAttribute)
            {
                // test si déclinaison en gamme dans PrestaConnect
                if (AttributeArticleRepository.ExistPrestashop((int)PsProductAttribute.IDProductAttribute))
                {
                    AttributeArticle = AttributeArticleRepository.ReadPrestashop((int)PsProductAttribute.IDProductAttribute);
                    Model.Prestashop.PsProductAttributeShop PsProductAttributeShop = (PsProductAttributeShopRepository.ExistPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop))
                        ? PsProductAttributeShopRepository.ReadPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop)
                        : null;
                    bool defaut = (PsProductAttributeShop != null) ? PsProductAttributeShop.DefaultOn == 1 : PsProductAttribute.DefaultOn == 1;
                    if (AttributeArticle.AttArt_Default != defaut)
                    {
                        AttributeArticle.AttArt_Default = defaut;
                        AttributeArticleRepository.Save();
                        if (defaut)
                        {
                            List <Model.Local.AttributeArticle> ListAttributeArticle = AttributeArticleRepository.ListArticle(AttributeArticle.Art_Id);
                            if (ListAttributeArticle.Count(i => i.AttArt_Default == true && i.AttArt_Id != AttributeArticle.AttArt_Id) > 0)
                            {
                                foreach (Model.Local.AttributeArticle AttributeArticleDefault in ListAttributeArticle.Where(i => i.AttArt_Default == true && i.AttArt_Id != AttributeArticle.Art_Id))
                                {
                                    AttributeArticleDefault.AttArt_Default = false;
                                    AttributeArticleRepository.Save();
                                }
                            }
                        }
                    }

                    // attribution images gammes
                    if (PsProductAttributeImageRepository.ExistProductAttribute(PsProductAttribute.IDProductAttribute))
                    {
                        List <Model.Prestashop.PsProductAttributeImage> ListPsProductAttributeImage = PsProductAttributeImageRepository.ListProductAttribute(PsProductAttribute.IDProductAttribute);
                        // étape 1 attachement dans PrestaConnect des images affectées a la déclinaison PrestaShop
                        foreach (Model.Prestashop.PsProductAttributeImage PsProductAttributeImage in ListPsProductAttributeImage)
                        {
                            // si l'image existe dans PrestaConnect
                            if (ArticleImageRepository.ExistPre_Id((int)PsProductAttributeImage.IDImage))
                            {
                                ArticleImage = ArticleImageRepository.ReadPrestaShop((int)PsProductAttributeImage.IDImage);
                                if (!AttributeArticleImageRepository.ExistAttributeArticleImage(AttributeArticle.AttArt_Id, ArticleImage.ImaArt_Id))
                                {
                                    AttributeArticleImageRepository.Add(new Model.Local.AttributeArticleImage()
                                    {
                                        AttArt_Id = AttributeArticle.AttArt_Id,
                                        ImaArt_Id = ArticleImage.ImaArt_Id,
                                    });
                                }
                            }
                        }
                        // étape 2 détachement
                        List <uint> list_prestashop = ListPsProductAttributeImage.Select(pai => pai.IDImage).ToList();
                        foreach (Model.Local.AttributeArticleImage AttributeArticleImage in AttributeArticleImageRepository.ListAttributeArticle(AttributeArticle.AttArt_Id))
                        {
                            if (AttributeArticleImage.ArticleImage.Pre_Id != null &&
                                !list_prestashop.Contains((uint)AttributeArticleImage.ArticleImage.Pre_Id.Value))
                            {
                                AttributeArticleImageRepository.Delete(AttributeArticleImage);
                            }
                        }
                    }
                    else if (AttributeArticleImageRepository.ExistAttributeArticle(AttributeArticle.AttArt_Id))
                    {
                        // absence de lien gamme déclinaison dans Prestashop donc suppression des liens dans PrestaConnect
                        AttributeArticleImageRepository.DeleteAll(AttributeArticleImageRepository.ListAttributeArticle(AttributeArticle.AttArt_Id));
                    }
                }
                // test si déclinaison en conditionnement dans PrestaConnect
                else if (ConditioningArticleRepository.ExistPrestashop((int)PsProductAttribute.IDProductAttribute))
                {
                    ConditioningArticle = ConditioningArticleRepository.ReadPrestashop((int)PsProductAttribute.IDProductAttribute);
                    Model.Prestashop.PsProductAttributeShop PsProductAttributeShop = (PsProductAttributeShopRepository.ExistPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop))
                        ? PsProductAttributeShopRepository.ReadPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop)
                        : null;
                    bool defaut = (PsProductAttributeShop != null) ? PsProductAttributeShop.DefaultOn == 1 : PsProductAttribute.DefaultOn == 1;
                    if (ConditioningArticle.ConArt_Default != defaut)
                    {
                        ConditioningArticle.ConArt_Default = defaut;
                        ConditioningArticleRepository.Save();
                        if (defaut)
                        {
                            List <Model.Local.ConditioningArticle> ListConditioningArticle = ConditioningArticleRepository.ListArticle(ConditioningArticle.Art_Id);
                            if (ListConditioningArticle.Count(i => i.ConArt_Default == true && i.ConArt_Id != ConditioningArticle.ConArt_Id) > 0)
                            {
                                foreach (Model.Local.ConditioningArticle ConditioningArticleDefault in ListConditioningArticle.Where(i => i.ConArt_Default == true && i.ConArt_Id != ConditioningArticle.Art_Id))
                                {
                                    ConditioningArticleDefault.ConArt_Default = false;
                                    ConditioningArticleRepository.Save();
                                }
                            }
                        }
                    }
                }
                // test si déclinaison composition dans PrestaConnect
                else if (CompositionArticleRepository.ExistPrestaShop((int)PsProductAttribute.IDProductAttribute))
                {
                    CompositionArticle = CompositionArticleRepository.ReadPrestaShop((int)PsProductAttribute.IDProductAttribute);
                    Model.Prestashop.PsProductAttributeShop PsProductAttributeShop = (PsProductAttributeShopRepository.ExistPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop))
                        ? PsProductAttributeShopRepository.ReadPsProductAttributeShop(PsProductAttribute.IDProductAttribute, Core.Global.CurrentShop.IDShop)
                        : null;
                    bool defaut = (PsProductAttributeShop != null) ? PsProductAttributeShop.DefaultOn == 1 : PsProductAttribute.DefaultOn == 1;
                    if (CompositionArticle.ComArt_Default != defaut)
                    {
                        CompositionArticle.ComArt_Default = defaut;
                        CompositionArticleRepository.Save();
                        if (defaut)
                        {
                            List <Model.Local.CompositionArticle> ListCompositionArticle = CompositionArticleRepository.ListArticle(CompositionArticle.ComArt_ArtId);
                            if (ListCompositionArticle.Count(i => i.ComArt_Default == true && i.ComArt_Id != CompositionArticle.ComArt_Id) > 0)
                            {
                                foreach (Model.Local.CompositionArticle CompositionArticleDefault in ListCompositionArticle.Where(i => i.ComArt_Default == true && i.ComArt_Id != CompositionArticle.ComArt_ArtId))
                                {
                                    CompositionArticleDefault.ComArt_Default = false;
                                    ConditioningArticleRepository.Save();
                                }
                            }
                        }
                    }

                    // attribution images gammes
                    if (PsProductAttributeImageRepository.ExistProductAttribute(PsProductAttribute.IDProductAttribute))
                    {
                        List <Model.Prestashop.PsProductAttributeImage> ListPsProductAttributeImage = PsProductAttributeImageRepository.ListProductAttribute(PsProductAttribute.IDProductAttribute);
                        // étape 1 attachement dans PrestaConnect des images affectées a la déclinaison PrestaShop
                        foreach (Model.Prestashop.PsProductAttributeImage PsProductAttributeImage in ListPsProductAttributeImage)
                        {
                            // si l'image existe dans PrestaConnect
                            if (ArticleImageRepository.ExistPre_Id((int)PsProductAttributeImage.IDImage))
                            {
                                ArticleImage = ArticleImageRepository.ReadPrestaShop((int)PsProductAttributeImage.IDImage);
                                if (!CompositionArticleImageRepository.ExistCompositionArticleImage(CompositionArticle.ComArt_Id, ArticleImage.ImaArt_Id))
                                {
                                    CompositionArticleImageRepository.Add(new Model.Local.CompositionArticleImage()
                                    {
                                        ComArt_Id = CompositionArticle.ComArt_Id,
                                        ImaArt_Id = ArticleImage.ImaArt_Id,
                                    });
                                }
                            }
                        }
                        // étape 2 détachement
                        List <uint> list_prestashop = ListPsProductAttributeImage.Select(pai => pai.IDImage).ToList();
                        foreach (Model.Local.CompositionArticleImage CompositionArticleImage in CompositionArticleImageRepository.ListCompositionArticle(CompositionArticle.ComArt_Id))
                        {
                            if (CompositionArticleImage.ArticleImage.Pre_Id != null &&
                                !list_prestashop.Contains((uint)CompositionArticleImage.ArticleImage.Pre_Id.Value))
                            {
                                CompositionArticleImageRepository.Delete(CompositionArticleImage);
                            }
                        }
                    }
                    else if (CompositionArticleImageRepository.ExistCompositionArticle(CompositionArticle.ComArt_Id))
                    {
                        // absence de lien gamme déclinaison dans Prestashop donc suppression des liens dans PrestaConnect
                        CompositionArticleImageRepository.DeleteAll(CompositionArticleImageRepository.ListCompositionArticle(CompositionArticle.ComArt_Id));
                    }
                }
            }
        }