Beispiel #1
0
        protected override DriverResult Editor(TwitterPostPart part, dynamic shapeHelper)
        {
            TwitterPostVM vm = new TwitterPostVM();

            var mapperConfiguration = new MapperConfiguration(cfg => {
                cfg.CreateMap <TwitterPostPart, TwitterPostVM>();
            });
            IMapper _mapper = mapperConfiguration.CreateMapper();

            _mapper.Map <TwitterPostPart, TwitterPostVM>(part, vm);
            TwitterPostPartSettingVM setting = part.Settings.GetModel <TwitterPostPartSettingVM>();

            if (!string.IsNullOrEmpty(setting.Title))
            {
                vm.ShowTitle = false;
            }
            if (!string.IsNullOrEmpty(setting.Description))
            {
                vm.ShowDescription = false;
            }
            if (!string.IsNullOrEmpty(setting.Image))
            {
                vm.ShowPicture = false;
            }
            if (setting.ShowTwitterCurrentLink)
            {
                vm.ShowTwitterCurrentLink = true;
            }
            else
            {
                vm.ShowTwitterCurrentLink = false;
            }
            List <TwitterAccountPart> listaccount = _TwitterService.GetValidTwitterAccount();
            List <OptionList>         optionList  = new List <OptionList>();
            var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext);

            foreach (TwitterAccountPart fa in listaccount)
            {
                OptionList ol = new OptionList {
                    Value    = fa.Id.ToString(),
                    Text     = fa.AccountType + " - " + fa.DisplayAs,
                    ImageUrl = urlHelper.Content("~/Media/" + _shellSettings.Name + "/twitter_" + fa.DisplayAs + ".jpg"),
                    Selected = part.AccountList.Contains(fa.Id)?"selected=\"selected\"":""
                };
                optionList.Add(ol);
            }
            vm.ListOption   = optionList;
            vm.SelectedList = part.AccountList.Select(x => x.ToString()).ToArray();
            return(ContentShape("Parts_TwitterPost",
                                () => shapeHelper.EditorTemplate(TemplateName: "Parts/TwitterPost",
                                                                 Model: vm,
                                                                 Prefix: Prefix)));
        }
Beispiel #2
0
        protected override DriverResult Display(TwitterPostPart part, string displayType, dynamic shapeHelper)
        {
            //Determine if we're on an admin page
            bool isAdmin = AdminFilter.IsApplied(_orchardServices.WorkContext.HttpContext.Request.RequestContext);

            if (isAdmin)
            {
                if ((displayType == "Detail") || (displayType == "Summary"))
                {
                    var         pcr = _providerConfigurationService.Get("Twitter");
                    TwitterOgVM vm  = new TwitterOgVM();
                    if (pcr != null)
                    {
                        vm.Site = pcr.UserIdentifier;
                    }
                    TwitterPostPartSettingVM setting = part.Settings.GetModel <TwitterPostPartSettingVM>();
                    var tokens = new Dictionary <string, object> {
                        { "Content", part.ContentItem }
                    };
                    if (!string.IsNullOrEmpty(setting.Description))
                    {
                        vm.Description = _tokenizer.Replace(setting.Description, tokens);
                    }
                    else
                    {
                        vm.Description = part.TwitterDescription;
                    }
                    if (!string.IsNullOrEmpty(setting.Image))
                    {
                        string ids = _tokenizer.Replace(setting.Image, tokens);

                        int idimage;
                        Int32.TryParse(ids.Replace("{", "").Replace("}", "").Split(',')[0], out idimage);;
                        if (idimage > 0)
                        {
                            // _orchardServices.ContentManager.Get(id);
                            // vm.Image = Url.ItemDisplayUrl(_orchardServices.ContentManager.Get(id));
                            var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext);
                            //       vm.Image = urlHelper.ItemDisplayUrl(_orchardServices.ContentManager.Get(id));// get current display link
                            //   Fvm.Link = urlHelper.MakeAbsolute(urlHelper.ItemDisplayUrl(Twitterpart));// get current display link
                            var ContentImage = _orchardServices.ContentManager.Get(idimage, VersionOptions.Published);
                            //   var pathdocument = Path.Combine(ContentImage.As<MediaPart>().FolderPath, ContentImage.As<MediaPart>().FileName);
                            //  part.TwitterPicture = pathdocument;//
                            vm.Image = urlHelper.MakeAbsolute(ContentImage.As <MediaPart>().MediaUrl);
                            //   .ResizeMediaUrl(Width: previewWidth, Height: previewHeight, Mode: "crop", Alignment: "middlecenter", Path: Model.MediaData.MediaUrl)');
                        }
                        else
                        {
                            vm.Image = "";
                        }
                    }
                    else
                    {
                        vm.Image = part.TwitterPicture;
                    }
                    if (!string.IsNullOrEmpty(setting.Title))
                    {
                        vm.Title = _tokenizer.Replace(setting.Title, tokens);
                    }

                    else
                    {
                        vm.Title = part.TwitterTitle;
                    }
                    return(ContentShape("Parts_TwitterPost_Detail",
                                        () => shapeHelper.Parts_TwitterPost_Detail(Twitter: vm, SendOnNextPublish: part.SendOnNextPublish, TwitterMessageSent: part.TwitterMessageSent)));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        public TwitterPostHandler(ITokenizer tokenizer, IRepository <TwitterPostPartRecord> repository, ITwitterService TwitterService, IOrchardServices orchardServices, INotifier notifier)
        {
            _TwitterService  = TwitterService;
            _orchardServices = orchardServices;
            _tokenizer       = tokenizer;
            _notifier        = notifier;
            T = NullLocalizer.Instance;
            Filters.Add(StorageFilter.For(repository));
            //    Filters.Add(new ActivatingFilter<TwitterPostPart>("CommunicationAdvertising"));
            OnUpdated <TwitterPostPart>((context, part) => {
                TwitterPostPartSettingVM setting = part.Settings.GetModel <TwitterPostPartSettingVM>();
                var tokens = new Dictionary <string, object> {
                    { "Content", part.ContentItem }
                };
                if (!string.IsNullOrEmpty(setting.Description))
                {
                    part.TwitterDescription = _tokenizer.Replace(setting.Description, tokens);
                }
                var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext);
                if (string.IsNullOrEmpty(setting.Image))
                {
                    part.TwitterPicture = "";
                }
                else
                {
                    string listid = _tokenizer.Replace(setting.Image, tokens);
                    listid        = listid.Replace("{", "").Replace("}", "");
                    Int32 idimage = 0;
                    Int32.TryParse(listid.Split(',')[0], out idimage);
                    if (idimage > 0)
                    {
                        var ContentImage    = _orchardServices.ContentManager.Get(idimage, VersionOptions.Published);
                        var pathdocument    = Path.Combine(ContentImage.As <MediaPart>().FolderPath, ContentImage.As <MediaPart>().FileName);
                        part.TwitterPicture = pathdocument;
                    }
                    else
                    {
                        part.TwitterPicture = "";
                    }
                }
                if (!string.IsNullOrEmpty(setting.Title))
                {
                    part.TwitterTitle = _tokenizer.Replace(setting.Title, tokens);
                }
            });
            OnPublished <TwitterPostPart>((context, Twitterpart) => {
                try {
                    PostToTwitterViewModel Fvm = new PostToTwitterViewModel();
                    Fvm.Message = Twitterpart.TwitterMessage;
                    if (Twitterpart.ContentItem.ContentType == "CommunicationAdvertising")
                    {
                        ICommunicationService _communicationService;
                        bool tryed = _orchardServices.WorkContext.TryResolve <ICommunicationService>(out _communicationService);
                        if (tryed)
                        {
                            Fvm.Link = _communicationService.GetCampaignLink("Twitter", Twitterpart);
                        }
                        else
                        {
                            Fvm.Link = "";
                        }
                    }
                    else
                    if (Twitterpart.TwitterCurrentLink)
                    {
                        var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext);
                        Fvm.Link      = urlHelper.MakeAbsolute(urlHelper.ItemDisplayUrl(Twitterpart));// get current display link
                    }


                    Fvm.Picture = Twitterpart.TwitterPicture;

                    Fvm.AccountList = Twitterpart.AccountList;
                    if (Twitterpart.SendOnNextPublish && !Twitterpart.TwitterMessageSent)
                    {
                        if (Twitterpart.AccountList.Length == 0)
                        {
                            _notifier.Add(NotifyType.Warning, T("No Twitter account specified."));
                        }
                        else
                        {
                            ResponseAction rsp = _TwitterService.PostTwitter(Fvm);
                            if (rsp.Success)
                            {
                                Twitterpart.TwitterMessageSent = true;
                            }
                        }
                    }
                }
                catch (Exception ex) {
                    _notifier.Add(NotifyType.Error, T("Twitter error:" + ex.Message));
                }
            });
        }