Esempio n. 1
0
        /// <summary>
        /// Creates a new gallery for the logged in user.
        /// </summary>
        /// <param name="core">Core token</param>
        /// <param name="parent">Parent gallery</param>
        /// <param name="title">Gallery title</param>
        /// <param name="slug">Gallery slug</param>
        /// <param name="description">Gallery description</param>
        /// <param name="permissions">Gallery permission mask</param>
        /// <returns>An instance of the newly created gallery</returns>
        public static Gallery Create(Core core, Primitive owner, Gallery parent, string title, ref string slug, string description)
        {
            if (core == null)
            {
                throw new NullCoreException();
            }

            GallerySettings settings = null;
            try
            {
                settings = new GallerySettings(core, owner);
            }
            catch (InvalidGallerySettingsException)
            {
                settings = GallerySettings.Create(core, owner);
            }

            string parents = "";
            // ensure we have generated a valid slug
            slug = Gallery.GetSlugFromTitle(title, slug);

            if (!Gallery.CheckGallerySlugValid(slug))
            {
                throw new GallerySlugNotValidException();
            }

            if (!Gallery.CheckGallerySlugUnique(core.Db, parent.owner, parent.FullPath, slug))
            {
                throw new GallerySlugNotUniqueException();
            }

            if (parent != null)
            {
                ParentTree parentTree = new ParentTree();

                if (parent.Parents != null)
                {
                    foreach (ParentTreeNode ptn in parent.Parents.Nodes)
                    {
                        parentTree.Nodes.Add(new ParentTreeNode(ptn.ParentTitle, ptn.ParentSlug, ptn.ParentId));
                    }
                }

                if (parent.Id > 0)
                {
                    parentTree.Nodes.Add(new ParentTreeNode(parent.GalleryTitle, parent.Path, parent.Id));
                }

                XmlSerializer xs = new XmlSerializer(typeof(ParentTree));
                StringBuilder sb = new StringBuilder();
                StringWriter stw = new StringWriter(sb);

                xs.Serialize(stw, parentTree);
                stw.Flush();
                stw.Close();

                parents = sb.ToString();
            }

            InsertQuery iQuery = new InsertQuery("user_galleries");
            iQuery.AddField("gallery_title", title);
            iQuery.AddField("gallery_abstract", description);
            iQuery.AddField("gallery_path", slug);
            iQuery.AddField("gallery_parent_path", parent.FullPath);
            iQuery.AddField("user_id", core.LoggedInMemberId);
            iQuery.AddField("settings_id", settings.Id);
            iQuery.AddField("gallery_item_id", owner.Id);
            iQuery.AddField("gallery_item_type_id", owner.TypeId);
            iQuery.AddField("gallery_parent_id", parent.GalleryId);
            iQuery.AddField("gallery_bytes", 0);
            iQuery.AddField("gallery_items", 0);
            iQuery.AddField("gallery_item_comments", 0);
            iQuery.AddField("gallery_visits", 0);
            iQuery.AddField("gallery_hierarchy", parents);

            long galleryId = core.Db.Query(iQuery);

            Gallery gallery = new Gallery(core, owner, galleryId);

            /* LOAD THE DEFAULT ITEM PERMISSIONS */
            Access.CreateAllGrantsForOwner(core, gallery);
            Access.CreateGrantForPrimitive(core, gallery, User.GetEveryoneGroupKey(core), "VIEW");
            Access.CreateGrantForPrimitive(core, gallery, Friend.GetFriendsGroupKey(core), "COMMENT");
            Access.CreateGrantForPrimitive(core, gallery, User.GetEveryoneGroupKey(core), "VIEW_ITEMS");
            Access.CreateGrantForPrimitive(core, gallery, Friend.GetFriendsGroupKey(core), "COMMENT_ITEMS");
            Access.CreateGrantForPrimitive(core, gallery, Friend.GetFriendsGroupKey(core), "RATE_ITEMS");

            return gallery;
        }
Esempio n. 2
0
        /// <summary>
        /// Updates the parent path for children galleries and photos
        /// </summary>
        /// <param name="oldPath">Old parent path</param>
        /// <param name="newPath">New parent path</param>
        private void updateParentPathChildren(string oldPath, string newPath)
        {
            if (owner is User)
            {
                List<Gallery> galleries = ((Gallery)this).GetGalleries();

                foreach (Gallery gallery in galleries)
                {
                    ParentTree parentTree = new ParentTree();

                    if (this.Parents != null)
                    {
                        foreach (ParentTreeNode ptn in this.Parents.Nodes)
                        {
                            parentTree.Nodes.Add(new ParentTreeNode(ptn.ParentTitle, ptn.ParentSlug, ptn.ParentId));
                        }
                    }

                    if (this.Id > 0)
                    {
                        parentTree.Nodes.Add(new ParentTreeNode(this.GalleryTitle, this.Path, this.Id));
                    }

                    XmlSerializer xs = new XmlSerializer(typeof(ParentTree));
                    StringBuilder sb = new StringBuilder();
                    StringWriter stw = new StringWriter(sb);

                    xs.Serialize(stw, parentTree);
                    stw.Flush();
                    stw.Close();

                    db.BeginTransaction();
                    UpdateQuery uQuery = new UpdateQuery("user_galleries");
                    uQuery.AddField("gallery_hierarchy", sb.ToString());
                    uQuery.AddField("gallery_parent_path", newPath);
                    uQuery.AddCondition("gallery_id", gallery.Id);

                    db.Query(uQuery);

                    gallery.updateParentPathChildren(gallery.FullPath, newPath + "/" + gallery.Path);
                }

                {
                    UpdateQuery uQuery = new UpdateQuery("gallery_items");
                    uQuery.AddField("gallery_item_parent_path", newPath);
                    uQuery.AddCondition("gallery_id", Id);
                    uQuery.AddCondition("user_id", owner.Id);

                    db.Query(uQuery);
                }
            }
            else
            {
                throw new GalleryCannotUpdateChildrenException();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="core"></param>
        /// <param name="parent"></param>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <param name="rules"></param>
        /// <param name="permissions"></param>
        /// <param name="isCategory"></param>
        /// <returns></returns>
        /// <exception cref="NullCoreException"></exception>
        /// <exception cref="InvalidForumException"></exception>
        /// <exception cref="UnauthorisedToCreateItemException"></exception>
        public static Forum Create(Core core, Forum parent, string title, string description, string rules, ushort permissions, bool isCategory)
        {
            string parents;
            int order = 0;
            int level = 0;

            //core.db.BeginTransaction();

            if (core == null)
            {
                throw new NullCoreException();
            }

            if (parent == null)
            {
                throw new InvalidForumException();
            }

            level = parent.Level + 1;

            if (parent.Owner is UserGroup)
            {
                if (!((UserGroup)parent.Owner).IsGroupOperator(core.Session.LoggedInMember.ItemKey))
                {
                    // todo: throw new exception
                    throw new UnauthorisedToCreateItemException();
                }
            }

            SelectQuery query = new SelectQuery(GetTable(typeof(Forum)));
            query.AddFields("forum_order");
            query.AddCondition("forum_order", ConditionEquality.GreaterThan, parent.Order);
            query.AddCondition("forum_item_id", parent.Owner.Id);
            query.AddCondition("forum_item_type_id", parent.Owner.TypeId);
            query.AddCondition("forum_parent_id", parent.Id);
            query.AddSort(SortOrder.Descending, "forum_order");
            query.LimitCount = 1;

            DataTable orderTable = core.Db.Query(query);

            if (orderTable.Rows.Count == 1)
            {
                order = (int)orderTable.Rows[0]["forum_order"] + 1;
            }
            else
            {
                order = parent.Order + 1;
            }

            // increment all items below in the order
            UpdateQuery uQuery = new UpdateQuery(GetTable(typeof(Forum)));
            uQuery.AddField("forum_order", new QueryOperation("forum_order", QueryOperations.Addition, 1));
            uQuery.AddCondition("forum_order", ConditionEquality.GreaterThanEqual, order);
            uQuery.AddCondition("forum_item_id", parent.Owner.Id);
            uQuery.AddCondition("forum_item_type_id", parent.Owner.TypeId);

            core.Db.Query(uQuery);

            ParentTree parentTree = new ParentTree();

            if (parent.Parents != null)
            {
                foreach (ParentTreeNode ptn in parent.Parents.Nodes)
                {
                    parentTree.Nodes.Add(new ParentTreeNode(ptn.ParentTitle, ptn.ParentId));
                }
            }

            if (parent.Id > 0)
            {
                parentTree.Nodes.Add(new ParentTreeNode(parent.Title, parent.Id));
            }

            XmlSerializer xs = new XmlSerializer(typeof(ParentTree));
            StringBuilder sb = new StringBuilder();
            StringWriter stw = new StringWriter(sb);

            xs.Serialize(stw, parentTree);
            stw.Flush();
            stw.Close();

            parents = sb.ToString();

            InsertQuery iquery = new InsertQuery(GetTable(typeof(Forum)));
            iquery.AddField("forum_parent_id", parent.Id);
            iquery.AddField("forum_title", title);
            iquery.AddField("forum_description", description);
            iquery.AddField("forum_rules", rules);
            iquery.AddField("forum_order", order);
            iquery.AddField("forum_level", level);
            iquery.AddField("forum_category", isCategory);
            iquery.AddField("forum_locked", false);
            iquery.AddField("forum_topics", 0);
            iquery.AddField("forum_posts", 0);
            iquery.AddField("forum_item_id", parent.Owner.Id);
            iquery.AddField("forum_item_type_id", parent.Owner.TypeId);
            iquery.AddField("forum_parents", parents);
            iquery.AddField("forum_last_post_id", 0);
            iquery.AddField("forum_last_post_time_ut", UnixTime.UnixTimeStamp());

            long forumId = core.Db.Query(iquery);

            Forum forum = new Forum(core, forumId);

            /* LOAD THE DEFAULT ITEM PERMISSIONS */
            //Access.CreateAllGrantsForOwner(core, forum);

            if (parent.Owner is UserGroup)
            {
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupMembersGroupKey(core), "VIEW");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupMembersGroupKey(core), "VIEW_TOPICS");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupMembersGroupKey(core), "LIST_TOPICS");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupMembersGroupKey(core), "REPLY_TOPICS");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupMembersGroupKey(core), "CREATE_TOPICS");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupMembersGroupKey(core), "REPORT_POSTS");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupOperatorsGroupKey(core), "CREATE_STICKY");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupOfficersGroupKey(core), "CREATE_STICKY");
                Access.CreateGrantForPrimitive(core, forum, UserGroup.GetGroupOperatorsGroupKey(core), "CREATE_ANNOUNCEMENT");
            }

            return forum;
        }