protected override DriverResult Editor(FacebookPostPart part, IUpdateModel updater, dynamic shapeHelper) { FacebookPostPartSettingVM setting = part.Settings.GetModel <FacebookPostPartSettingVM>(); var tokens = new Dictionary <string, object> { { "Content", part.ContentItem } }; FacebookPostVM vm = new FacebookPostVM(); updater.TryUpdateModel(vm, Prefix, null, null); var mapperConfiguration = new MapperConfiguration(cfg => { cfg.CreateMap <FacebookPostVM, FacebookPostPart>(); }); IMapper _mapper = mapperConfiguration.CreateMapper(); _mapper.Map(vm, part); if (_orchardServices.WorkContext.HttpContext.Request.Form["FacebookType"] != null && _orchardServices.WorkContext.HttpContext.Request.Form["FacebookType"] == "1") { part.FacebookType = FacebookType.Post; } else { part.FacebookType = FacebookType.ShareLink; } if (vm.SelectedList != null && vm.SelectedList.Count() > 0) { part.AccountList = vm.SelectedList.Select(x => Int32.Parse(x)).ToArray(); } return(Editor(part, shapeHelper)); }
public FacebookPostHandler(ITokenizer tokenizer, IRepository <FacebookPostPartRecord> repository, IFacebookService facebookService, IOrchardServices orchardServices, INotifier notifier) { _facebookService = facebookService; _orchardServices = orchardServices; _tokenizer = tokenizer; _notifier = notifier; T = NullLocalizer.Instance; Filters.Add(StorageFilter.For(repository)); OnUpdated <FacebookPostPart>((context, part) => { #region [calcola i token e li sovrascrive messo qui e non nell'edit perchè non si sa in che ordine viene eseguita la parte e di conseguenza se i valori dei token riferiti ad altre parti sono aggiornati] var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext); FacebookPostPartSettingVM setting = part.Settings.GetModel <FacebookPostPartSettingVM>(); var tokens = new Dictionary <string, object> { { "Content", part.ContentItem } }; if (!string.IsNullOrEmpty(setting.FacebookCaption)) { part.FacebookCaption = _tokenizer.Replace(setting.FacebookCaption, tokens); } if (!string.IsNullOrEmpty(setting.FacebookDescription)) { part.FacebookDescription = _tokenizer.Replace(setting.FacebookDescription, tokens); } if (!string.IsNullOrEmpty(setting.FacebookLink)) { part.FacebookLink = _tokenizer.Replace(setting.FacebookLink, tokens); } if (!string.IsNullOrEmpty(setting.FacebookMessage)) { part.FacebookMessage = _tokenizer.Replace(setting.FacebookMessage, tokens); } if (!string.IsNullOrEmpty(setting.FacebookName)) { part.FacebookName = _tokenizer.Replace(setting.FacebookName, tokens); } if (!string.IsNullOrEmpty(setting.FacebookPicture)) { string idimg = _tokenizer.Replace(setting.FacebookPicture, tokens); Int32 idimage = 0; Int32.TryParse(idimg.Replace("{", "").Replace("}", "").Split(',')[0], out idimage);; if (idimage > 0) { var ContentImage = _orchardServices.ContentManager.Get(idimage, VersionOptions.Published); part.FacebookIdPicture = idimage.ToString(); part.FacebookPicture = urlHelper.MakeAbsolute(ContentImage.As <MediaPart>().MediaUrl); } else { part.FacebookPicture = ""; part.FacebookIdPicture = ""; } } #endregion ; }); OnPublished <FacebookPostPart>((context, facebookpart) => { try { bool publishEnabled = true; // string linktosend = ""; if (facebookpart.ContentItem.ContentType == "CommunicationAdvertising") { ICommunicationService _communicationService; bool tryed = _orchardServices.WorkContext.TryResolve <ICommunicationService>(out _communicationService); publishEnabled = _communicationService.AdvertisingIsAvailable(facebookpart.Id); if (!publishEnabled) { _notifier.Add(NotifyType.Error, T("Advertising can't be published, see campaign validation date")); } } if (publishEnabled) { if (facebookpart.SendOnNextPublish && !facebookpart.FacebookMessageSent) { List <FacebookAccountPart> FacebookAccountSettings = _facebookService.Facebook_GetAccessToken(facebookpart); if (FacebookAccountSettings.Count > 0) { ResponseAction rsp = _facebookService.PostFacebook(facebookpart); if (rsp.Success) { facebookpart.FacebookMessageSent = true; } else { _notifier.Add(NotifyType.Error, T("Facebook error:" + rsp.Message)); } } } } } catch (Exception ex) { _notifier.Add(NotifyType.Error, T("Facebook error:" + ex.Message)); } } ); }
protected override DriverResult Editor(FacebookPostPart part, dynamic shapeHelper) { var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext); FacebookPostVM vm = new FacebookPostVM(); var mapperConfiguration = new MapperConfiguration(cfg => { cfg.CreateMap <FacebookPostPart, FacebookPostVM>(); }); IMapper _mapper = mapperConfiguration.CreateMapper(); _mapper.Map(part, vm); if (string.IsNullOrEmpty(vm.FacebookType.ToString())) { vm.FacebookType = FacebookType.Post; } FacebookPostPartSettingVM setting = part.Settings.GetModel <FacebookPostPartSettingVM>(); var tokens = new Dictionary <string, object> { { "Content", part.ContentItem } }; if (!string.IsNullOrEmpty(setting.FacebookCaption)) { vm.ShowFacebookCaption = false; } if (!string.IsNullOrEmpty(setting.FacebookDescription)) { vm.ShowFacebookDescription = false; } if (!string.IsNullOrEmpty(setting.FacebookLink)) { vm.ShowFacebookLink = false; } if (!string.IsNullOrEmpty(setting.FacebookMessage)) { vm.ShowFacebookMessage = false; } if (!string.IsNullOrEmpty(setting.FacebookName)) { vm.ShowFacebookName = false; } if (!string.IsNullOrEmpty(setting.FacebookPicture)) { vm.ShowFacebookPicture = false; } else { vm.FacebookPicture = part.FacebookPicture; } List <FacebookAccountPart> listaccount = _facebookService.GetValidFacebookAccount(); // List<SelectListItem> lSelectList = new List<SelectListItem>(); List <OptionList> optionList = new List <OptionList>(); foreach (FacebookAccountPart fa in listaccount) { // lSelectList.Insert(0, new SelectListItem() { Value = fa.Id.ToString(), Text = fa.AccountType + " - " + fa.DisplayAs }); OptionList ol = new OptionList { Value = fa.Id.ToString(), Text = fa.AccountType + " - " + fa.DisplayAs, ImageUrl = urlHelper.Content("~/Media/" + _shellSettings.Name + "/facebook_" + fa.UserIdFacebook + ".jpg"), Selected = part.AccountList.Contains(fa.Id) ? "selected=\"selected\"" : "" }; optionList.Add(ol); } vm.ListOption = optionList; return(ContentShape("Parts_FacebookPost", () => shapeHelper.EditorTemplate(TemplateName: "Parts/FacebookPost", Model: vm, Prefix: Prefix))); }