/// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            base.Page_Load(sender, e);
            try
            {
                if (!Page.IsPostBack && !Page.IsCallback)
                {
                    atiRadDatePicker.SelectedDate = DateTime.Today;
                    ServiceReference service = new ServiceReference("~/DesktopModules/ATI_Base/resources/services/StreamService.asmx");
                    service.InlineScript = true;
                    ScriptManager.GetCurrent(Page).Services.Add(service);

                    atiProfileImg.Settings = GroupSettings != null ? (Affine.Data.UserSettings)GroupSettings : UserSettings;
                    Affine.WebService.StreamService streamService = new Affine.WebService.StreamService();
                    aqufitEntities entities = new aqufitEntities();
                    WODType[]      wodTypes = entities.WODTypes.ToArray();
                    ddlWODType.Items.AddRange(wodTypes.Select(t => new ListItem()
                    {
                        Text = t.Name, Value = "" + t.Id
                    }).ToArray());

                    // CA - new ...
                    ddlWODType2.Items.AddRange(wodTypes.Select(t => new ListItem()
                    {
                        Text = t.Name, Value = "" + t.Id
                    }).ToArray());
                    RadListBoxExcerciseSource.DataValueField = "Value";
                    RadListBoxExcerciseSource.DataTextField  = "Text";
                    RadListBoxExcerciseSource.DataSource     = entities.Exercises.OrderBy(ex => ex.Name).Select(ex => new { Text = ex.Name, Value = ex.Id }).ToList();
                    RadListBoxExcerciseSource.DataBind();

                    string js = string.Empty;
                    if (UserSettings.MainGroupKey.HasValue && GroupSettings == null)   // Remember that "g" is when a group admin is scheduling a wod..
                    {
                        long[] groupIds = entities.UserFriends.Where(f => f.SrcUserSettingKey == UserSettings.Id && f.Relationship >= (int)Affine.Utils.ConstsUtil.Relationships.GROUP_OWNER).Select(f => f.DestUserSettingKey).ToArray();
                        lbGroups.DataTextField  = "Text";
                        lbGroups.DataValueField = "Value";
                        lbGroups.DataSource     = entities.UserSettings.OfType <Group>().Where(Affine.Utils.Linq.LinqUtils.BuildContainsExpression <UserSettings, long>(us => us.Id, groupIds)).Select(g => new { Text = g.UserFirstName, Value = g.Id }).ToList();
                        lbGroups.DataBind();
                        ListItem li = lbGroups.Items.FindByValue("" + UserSettings.MainGroupKey);
                        if (li != null)
                        {
                            li.Selected = true;
                        }
                    }
                    else
                    {   // we skip the first 2 steps of the workout creator.. since they have no group
                        js += "Aqufit.Page.Actions.HaveNoGroup(); ";
                    }

                    ScriptManager.RegisterStartupScript(this, Page.GetType(), "ExerciseList",
                                                        "$(function(){ " +
                                                        js +
                                                        //   "Aqufit.Page.atiExerciseListScript.generateStreamDom('" + streamService.GetAllExercises() + "'); " +
                                                        "Aqufit.Page.Events.OnLoad();" +
                                                        "});", true);

                    string baseUrl = ResolveUrl("~");
                    aReturn.HRef     = baseUrl + UserSettings.UserName;
                    aMyWorkouts.HRef = baseUrl + "Profile/MyWorkouts";
                    //Profile/MyWorkouts


                    atiTemp.Visible = false;
                    atiNew.Visible  = true;
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
Пример #2
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            base.Page_Load(sender, e);

            try
            {
                ServiceReference service = new ServiceReference("~/DesktopModules/ATI_Base/resources/services/StreamService.asmx");
                service.InlineScript = true;
                ScriptManager.GetCurrent(Page).Services.Add(service);
                imgAd.Src         = ResolveUrl("/portals/0/images/adTastyPaleo.jpg");
                imgCheck.ImageUrl = ResolveUrl("~/DesktopModules/ATI_Base/resources/images/iCheck.png");

                if (!Page.IsPostBack && !Page.IsCallback)
                {
                    baseUrl = ResolveUrl("~/");
                    long wId = 0;
                    if (HttpContext.Current.Items["w"] != null)
                    {
                        wId = Convert.ToInt64(HttpContext.Current.Items["w"]);
                    }
                    else if (Request["w"] != null)
                    {
                        wId = Convert.ToInt64(Request["w"]);
                    }

                    // Are we viewing a specific workout ?
                    if (wId > 0)
                    {
                        divMainLinks.Visible = true;

                        atiProfile.Visible     = false;
                        hiddenWorkoutKey.Value = "" + wId;
                        aqufitEntities entities = new aqufitEntities();
                        WOD            wod      = entities.WODs.Include("WODType").Include("WODSets").Include("WODSets.WODExercises").Include("WODSets.WODExercises.Exercise").FirstOrDefault(w => w.Id == wId);
                        if (base.UserSettings != null && wod.Standard == 0)
                        {
                            User2WODFav fav = entities.User2WODFav.FirstOrDefault(mr => mr.WOD.Id == wod.Id && mr.UserSetting.Id == UserSettings.Id);
                            bAddWorkout.Visible    = fav == null;
                            bRemoveWorkout.Visible = fav != null;
                        }
                        lWorkoutTitle.Text = wod.Name;

                        //  lWorkoutDescription.Text = wod.Description;

                        // constructWorkoutInfo(wod);
                        lWorkoutInfo.Text = wod.Description;
                        if (wod.Standard > 0)
                        {
                            imgCrossFit.Visible   = true;
                            imgCrossFit.Src       = ResolveUrl("~/DesktopModules/ATI_Base/resources/images/xfit.png");
                            atiProfileImg.Visible = false;
                        }
                        else
                        {
                            atiProfileImg.Settings = entities.UserSettings.FirstOrDefault(us => us.Id == wod.UserSettingsKey);
                        }
                        atiWorkoutPanel.Visible  = false;
                        atiWorkoutViewer.Visible = true;

                        // Get the leader board
                        IQueryable <Workout> stream = entities.UserStreamSet.OfType <Workout>().Where(w => w.WOD.Id == wId);
                        long typeId = entities.WODs.Where(w => w.Id == wId).Select(w => w.WODType.Id).FirstOrDefault();
                        switch (typeId)
                        {
                        case (long)Affine.Utils.WorkoutUtil.WodType.AMRAP:
                        case (long)Affine.Utils.WorkoutUtil.WodType.SCORE:
                            atiScoreRangePanel.Visible = true;
                            stream = stream.OrderByDescending(w => w.Score);
                            break;

                        case (long)Affine.Utils.WorkoutUtil.WodType.MAX_WEIGHT:
                            atiMaxRangePanel.Visible = true;
                            atiMaxWeightUnitsFirst.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_LBS);
                            atiMaxWeightUnitsFirst.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_KG);
                            atiMaxWeightUnitsFirst.Selected = WeightUnits;
                            atiMaxWeightUnitsLast.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_LBS);
                            atiMaxWeightUnitsLast.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_KG);
                            atiMaxWeightUnitsLast.Selected = WeightUnits;
                            stream = stream.OrderByDescending(w => w.Max);
                            break;

                        case (long)Affine.Utils.WorkoutUtil.WodType.TIMED:
                            atiTimeSpanePanel.Visible = true;
                            stream = stream.OrderBy(w => w.Duration);
                            break;

                        default:
                            stream = stream.OrderByDescending(w => w.TimeStamp);
                            break;
                        }
                        string js = string.Empty;
                        atiShareLink.ShareLink  = "http://" + Request.Url.Host + "/workout/" + wod.Id;
                        atiShareLink.ShareTitle = "FlexFWD.com crossfit WOD " + wod.Name;

                        workoutTabTitle.Text = "&nbsp;" + (string.IsNullOrWhiteSpace(wod.Name) ? "Untitled" : wod.Name) + "&nbsp;";
                        Affine.WebService.StreamService ss = new WebService.StreamService();
                        string jsonEveryone = ss.getStreamDataForWOD(wod.Id, -1, 0, 15, true, true, -1, -1, -1);

                        string jsonYou = string.Empty;

                        js += " Aqufit.Page." + atiWorkoutHighChart.ID + ".fromStreamData('" + jsonEveryone + "'); ";
                        if (base.UserSettings != null)
                        {
                            hlLogWorkout.HRef = baseUrl + UserSettings.UserName + "?w=" + wId;
                            hlWorkouts.HRef   = baseUrl + UserSettings.UserName + "/workout-history";
                            jsonYou           = ss.getStreamDataForWOD(wod.Id, base.UserSettings.Id, 0, 10, true, true, -1, -1, -1);
                            js += "Aqufit.Page.atiYouStreamScript.generateStreamDom('" + jsonYou + "');";
                            js += " Aqufit.Page." + atiWorkoutHighChart.ID + ".fromYourStreamData('" + jsonYou + "'); ";
                            // TODO: this could be improved on...
                            Workout thisWod = entities.UserStreamSet.OfType <Workout>().FirstOrDefault(w => w.UserSetting.Id == UserSettings.Id && w.WOD.Id == wId);
                            if (thisWod != null)
                            {   // graphs of this wod
                                hlGraph.HRef = ResolveUrl("~/") + UserSettings.UserName + "/workout/" + thisWod.Id;
                            }
                            else
                            {   // just grab any workout then..
                                Workout any = entities.UserStreamSet.OfType <Workout>().OrderByDescending(w => w.Id).FirstOrDefault(w => w.UserSetting.Id == UserSettings.Id);
                                if (any != null)
                                {
                                    hlGraph.HRef = ResolveUrl("~/") + UserSettings.UserName + "/workout/" + any.Id;
                                }
                                else
                                {   // no workouts ??? say what.. slack man :) hide graph
                                    hlGraph.Visible = false;
                                }
                            }
                        }
                        else
                        {
                            hlGraph.HRef                 = ResolveUrl("~/Login");
                            hlLogWorkout.HRef            = hlGraph.HRef;
                            hlWorkouts.HRef              = hlGraph.HRef;
                            atiPanelYourProgress.Visible = false;
                        }
                        hlCreateWOD.HRef  = baseUrl + "Profile/WorkoutBuilder";
                        hlMyWorkouts.HRef = baseUrl + "Profile/MyWorkouts";
                        js += " Aqufit.Page." + atiWorkoutHighChart.ID + ".drawChart(); ";



                        ScriptManager.RegisterStartupScript(this, Page.GetType(), "SimilarRouteList", "$(function(){ " + js + " Aqufit.Page.atiEveryoneStreamScript.generateStreamDom('" + jsonEveryone + "'); });", true);



                        YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
                        //order results by the number of views (most viewed first)
                        query.OrderBy          = "viewCount";
                        query.NumberToRetrieve = 3;
                        query.SafeSearch       = YouTubeQuery.SafeSearchValues.Moderate;

                        YouTubeRequestSettings settings = new YouTubeRequestSettings(ConfigurationManager.AppSettings["youtubeApp"], ConfigurationManager.AppSettings["youtubeKey"]);
                        YouTubeRequest         request  = new YouTubeRequest(settings);
                        const int     NUM_ENTRIES       = 50;
                        IList <Video> videos            = new List <Video>();
                        IList <Video> groupVideo        = new List <Video>();
                        // first try to find videos with regard to users group
                        Feed <Video> videoFeed = null;

                        if (this.UserSettings != null)
                        {
                            long[] groupIds = entities.UserFriends.Where(f => (f.SrcUserSettingKey == UserSettings.Id || f.DestUserSettingKey == UserSettings.Id) && f.Relationship >= (int)Affine.Utils.ConstsUtil.Relationships.GROUP_OWNER).Select(f => f.SrcUserSettingKey == UserSettings.Id ? f.DestUserSettingKey : f.SrcUserSettingKey).ToArray();
                            Group  business = entities.UserSettings.OfType <Group>().Where(Utils.Linq.LinqUtils.BuildContainsExpression <Group, long>(us => us.Id, groupIds)).FirstOrDefault(g => g.GroupType.Id == 1);
                            if (business != null)
                            {   // TODO: need the business name...
                                query.Query = business.UserName;
                            }
                            else
                            {
                                UserSettings creator = entities.UserSettings.FirstOrDefault(u => u.Id == wod.UserSettingsKey);
                                if (creator is Group)
                                {   // TODO: need the business name...
                                    query.Query = creator.UserFirstName + " " + creator.UserLastName;
                                }
                                else
                                {
                                    query.Query = UserSettings.UserFirstName + " " + UserSettings.UserLastName;
                                }
                            }
                            videoFeed = request.Get <Video>(query);
                            foreach (Video v in videoFeed.Entries)
                            {
                                groupVideo.Add(v);
                            }
                        }
                        if (videos.Count < NUM_ENTRIES)
                        {   // now try the crossfit WOD name
                            query.NumberToRetrieve = NUM_ENTRIES - videos.Count;
                            query.Query            = "crossfit wod " + wod.Name;
                            videoFeed = request.Get <Video>(query);
                            foreach (Video v in videoFeed.Entries)
                            {
                                videos.Add(v);
                            }
                            if (videos.Count < NUM_ENTRIES)
                            {   // this is last resort .. just get videos about crossfit...
                                query.NumberToRetrieve = NUM_ENTRIES - videos.Count;
                                query.Query            = "crossfit wod";
                                videoFeed = request.Get <Video>(query);
                                foreach (Video v in videoFeed.Entries)
                                {
                                    videos.Add(v);
                                }
                            }
                        }

                        const int TAKE = 3;
                        if (videos.Count > TAKE)
                        {
                            Random random = new Random((int)DateTime.Now.Ticks);
                            int    rand   = random.Next(videos.Count - TAKE);
                            videos = videos.Skip(rand).Take(TAKE).ToList();
                            if (groupVideo.Count > 0)
                            {
                                // always replace one of the main videos with a gorup one.. (if possible)
                                rand      = random.Next(groupVideo.Count - 1);
                                videos[0] = groupVideo[rand];
                            }
                            atiYoutubeThumbList.VideoFeed = videos;
                        }
                        else
                        {
                            atiVideoPanel.Visible = false;
                        }
                    }
                    else
                    {
                        atiVideoPanel.Visible     = false;
                        atiPanelQuickView.Visible = false;
                        hlGraph.Visible           = false;
                        aqufitEntities entities      = new aqufitEntities();
                        var            exerciseArray = entities.Exercises.OrderBy(x => x.Name).Select(x => new{ Text = x.Name, Value = x.Id }).ToArray();
                        RadListBoxExcerciseSource.DataSource = exerciseArray;
                        RadListBoxExcerciseSource.DataBind();

                        string order = orderDate.Checked ? "date" : "popular";
                        if (Settings["Configure"] != null && Convert.ToString(Settings["Configure"]).Equals("ConfigureMyWorkouts"))
                        {
                            this.IsMyWorkouts          = true;
                            atiProfile.ProfileSettings = base.UserSettings;
                            atiProfile.IsOwner         = true;
                            atiProfile.IsSmall         = true;
                            atiWorkoutPanel.Visible    = true;
                            workoutTabTitle.Text       = "My Workouts";
                            liMyWorkouts.Visible       = false;
                            liFindWorkout.Visible      = true;
                            WebService.StreamService streamService = new WebService.StreamService();
                            string json = streamService.GetWorkouts(base.UserSettings.Id, 0, 30, order, null);
                            ScriptManager.RegisterStartupScript(this, Page.GetType(), "WorkoutList", "$(function(){ Aqufit.Page.atiWorkoutListScript.isMyRoutes = true; Aqufit.Page.atiWorkoutListScript.generateStreamDom('" + json + "'); });", true);
                        }
                        else
                        {
                            this.IsMyWorkouts        = false;
                            atiProfile.Visible       = false;
                            workoutTabTitle.Text     = "Workouts";
                            atiWorkoutPanel.Visible  = true;
                            atiWorkoutViewer.Visible = false;
                            WebService.StreamService streamService = new WebService.StreamService();
                            string json = streamService.GetWorkouts(-1, 0, 30, order, null);
                            ScriptManager.RegisterStartupScript(this, Page.GetType(), "WorkoutList2", "$(function(){ Aqufit.Page.atiWorkoutListScript.generateStreamDom('" + json + "'); });", true);
                        }
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }