示例#1
0
    protected void CreateAdButton_Click(object sender, EventArgs e)
    {
        ErrorMessagePanel.Visible = false;
        SuccMessagePanel.Visible  = false;

        if (Page.IsValid)
        {
            try
            {
                AppSettings.DemoCheck();
                Member User      = null;
                int    oldPackId = 0;

                if (Member.IsLogged)
                {
                    User = Member.Logged(Context);
                }

                if (URL.Enabled)
                {
                    throw new MsgException(U4200.CHECKURL);
                }

                PtcAdvert Ad;
                if (PageRequest == RequestType.Edit)
                {
                    int adId = Convert.ToInt32(ViewState["editid"]);
                    Ad        = new PtcAdvert(adId);
                    oldPackId = Ad.Pack.Id;
                }
                else
                {
                    Ad = new PtcAdvert();
                }

                Money         AdCost = Money.Zero;
                PtcAdvertPack Pack   = new PtcAdvertPack();

                GetAdWithPrice(ref Ad, ref AdCost, ref Pack);

                //Add the ad to the db (for approval)
                Ad.Price = AdCost;
                if (Pack != null)
                {
                    Ad.Pack = Pack;
                }

                if (Member.IsLogged)
                {
                    //Take money and save the user
                    int adViews = 0;
                    if (AppSettings.PtcAdverts.PTCCreditsEnabled && UseExtraViewsCheckBox.Checked)
                    {
                        adViews = Convert.ToInt32(PTCCreditsTextBox.Text);
                        if (adViews < 0)
                        {
                            throw new MsgException("Input cannot be negative.");
                        }

                        decimal adCreditsSpent       = 0;
                        var     creditsPerDayOrClick = 0m;
                        if (Pack == null)
                        {
                            if (TitanFeatures.isSatvetErturkmen)
                            {
                                PtcAdvertPack advertPack = TableHelper.GetListFromRawQuery <PtcAdvertPack>("SELECT TOP 1 * FROM PtcAdvertPacks")[0];
                                Ad.Pack = advertPack;
                                Ad.Ends = End.FromClicks(adViews);
                                creditsPerDayOrClick = advertPack.PTCCreditsPerDayOrClick;
                            }
                        }
                        else
                        {
                            Ad.Ends = Ad.Ends.AddValue(adViews);
                            creditsPerDayOrClick = Pack.PTCCreditsPerDayOrClick;
                        }

                        if (PageRequest == RequestType.Edit && Ad.ExtraViews != -1)
                        {
                            adCreditsSpent = (adViews - Ad.ExtraViews) * creditsPerDayOrClick;
                            if (adCreditsSpent < 0)
                            {
                                adCreditsSpent = 0;
                            }
                        }
                        else
                        {
                            adCreditsSpent = adViews * creditsPerDayOrClick;
                        }

                        Ad.ExtraViews = adViews;

                        if (adCreditsSpent > Member.Current.PTCCredits)
                        {
                            throw new MsgException(U5006.NOTENOUGHADCREDITS);
                        }
                        else
                        {
                            User.SubstractFromPTCCredits(adCreditsSpent, "PTC advert");
                        }
                    }
                    else
                    {
                        Ad.ExtraViews = -1;
                    }

                    if (PageRequest == RequestType.Edit)
                    {
                        Ad.IsEdited = true;
                    }

                    Ad.TargetBalance = TargetBalanceRadioButtonList.TargetBalance;
                    PurchaseOption.ChargeBalance(User, AdCost, TargetBalanceRadioButtonList.Feature, Ad.TargetBalance, PageRequest == RequestType.Edit ? "PTC update" : "PTC advert");

                    Ad.Advertiser       = Advertiser.AsMember(User.Name);
                    Ad.AdvertiserUserId = User.Id;
                    Ad.Status           = AdvertStatusExtensions.GetStartingStatus();

                    if (AppSettings.PtcAdverts.AdvertPTCPackCashbackEnabled && Pack.PTCPackCashBackPercent != 0)
                    {
                        Money CashBack = new Money(Double.Parse(AdCost.ToClearString()) * ((Double)Pack.PTCPackCashBackPercent / 100.0));
                        User.AddToTrafficBalance(CashBack, U6010.PTCCASHBACK);
                        User.Save();
                    }
                }
                else
                {
                    Ad.Advertiser    = Advertiser.AsStranger(OutEmail.Text);
                    Ad.TargetBalance = PurchaseBalances.PaymentProcessor;
                    Ad.Status        = AdvertStatus.Null;
                }

                if (oldPackId != 0 && oldPackId != Ad.Pack.Id)
                {
                    Ad.ResetClicks();
                }

                Ad.Save();

                if (Member.IsLogged)
                {
                    //Achievements trial
                    int  UserCurrentCampaigns = TableHelper.CountOf <PtcAdvert>(TableHelper.MakeDictionary("CreatorUsername", User.Name));
                    bool ShouldBeSaved        = User.TryToAddAchievements(
                        Prem.PTC.Achievements.Achievement.GetProperAchievements(
                            Prem.PTC.Achievements.AchievementType.AfterAdvertisingPtcCampaigns, UserCurrentCampaigns));

                    if (ShouldBeSaved)
                    {
                        User.Save();
                    }

                    if (PageRequest == RequestType.Edit)
                    {
                        History.AddEdit(User.Name, AdCost, "Advert campaign");
                    }
                    else
                    {
                        History.AddPurchase(User.Name, AdCost, "Advert campaign");

                        MatrixBase.TryAddMemberAndCredit(User, AdCost, AdvertType.PTC);
                    }

                    if (Ad.Status != AdvertStatus.WaitingForAcceptance)
                    {
                        PtcCrediter.TryToCreditReferrerAfterPurchase(User, AdCost, PageRequest == RequestType.Edit ? "PTC update" : "PTC advert");
                    }

                    Title.Text                      = "";
                    URL.Text                        = "";
                    chbDescription.Checked          = false;
                    chbGeolocation.Checked          = false;
                    chbBold.Checked                 = false;
                    BackgroundColorCheckBox.Checked = false;
                    StarredAdCheckBox.Checked       = false;

                    SuccMessagePanel.Visible = true;

                    if (PageRequest == RequestType.Edit)
                    {
                        SuccMessage.Text = Ad.Status == AdvertStatus.WaitingForAcceptance ? U6012.EDITEDADAWAITSAPPROVAL : U6012.ADUPDATED;
                    }
                    else
                    {
                        SuccMessage.Text = Ad.Status == AdvertStatus.WaitingForAcceptance ? U4200.ADAWAITSAPPROVAL : U3501.ADCREATED;
                    }
                }
                else
                {
                    //Show buttons
                    PaymentButtons.Visible = true;
                    CreateAdButton.Visible = false;

                    PaymentButtons.Text = GenerateHTMLButtons.GetPaymentButtons(new BuyAdvertButtonGenerator <IPtcAdvertPack>(Ad));
                }

                ViewState["editid"] = null;
            }
            catch (MsgException ex)
            {
                ErrorMessagePanel.Visible = true;
                ErrorMessage.Text         = ex.Message;
            }
            catch (Exception ex)
            {
                ErrorLogger.Log(ex);
            }
        }
    }