Ejemplo n.º 1
0
	protected void Page_Load(object sender, EventArgs e)
	{
		Response.Buffer = true;

		Parameters["IBlockTypeId"] = Parameters.Get("IBlockTypeId", 0).ToString();
		Parameters["IBlockId"] = Parameters.Get("IBlockId", 0).ToString();
		Parameters["IBlockSectionId"] = Parameters.Get("IBlockSectionId", 0).ToString();
		Parameters["ElementsCount"] = Parameters.Get("ElementsCount", 20).ToString();
		Parameters["DaysCount"] = Parameters.Get("DaysCount", 0).ToString();
		Parameters["RssTTL"] = Parameters.Get("RssTTL", 60).ToString();

		if (!IsCached((Page.User as BXPrincipal).GetAllRoles(true)))
		{
			List<BXTemplateIBlockElement> items = new List<BXTemplateIBlockElement>();
			Results["Items"] = items;
			if (IBlockId > 0)
			{
				BXFormFilter iblockFilter = new BXFormFilter(
						new BXFormFilterItem("ID", IBlockId, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Active", "Y", BXSqlFilterOperators.Equal)
					);

				if (IBlockTypeId>0)
					iblockFilter.Add(new BXFormFilterItem("typeid", IBlockTypeId, BXSqlFilterOperators.Equal));

				BXInfoBlockCollectionOld iblockCollection = BXInfoBlockManagerOld.GetList(iblockFilter,	null);

				foreach (BXInfoBlockOld iblock in iblockCollection)
				{
					IBlock = iblock;

					Results["IBlockName"] = BXStringUtility.XmlEncode(IBlock.NameRaw);
					Results["IBlockDescription"] = BXStringUtility.XmlEncode(IBlock.DescriptionRaw);

					BXFormFilter elementFilter = new BXFormFilter(
						new BXFormFilterItem("IBlockId", iblock.IBlockId, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Active", "Y", BXSqlFilterOperators.Equal),
						new BXFormFilterItem("ActiveDate", "Y", BXSqlFilterOperators.Equal),
						new BXFormFilterItem("CheckPermissions", "Y", BXSqlFilterOperators.Equal)
					);

					if (Parameters.Get<int>("DaysCount") > 0)
					{
						elementFilter.Add(new BXFormFilterItem("ActiveFromDate", DateTime.Now.AddDays(-Parameters.Get<int>("DaysCount")), BXSqlFilterOperators.Greater));
					}

					if (IBlockSectionId > 0)
					{
						elementFilter.Add(new BXFormFilterItem("Section.Id", IBlockSectionId, BXSqlFilterOperators.Equal));

						BXFormFilter sectionFilter = new BXFormFilter(
							new BXFormFilterItem("IBlockId", iblock.IBlockId, BXSqlFilterOperators.Equal),
							new BXFormFilterItem("Active", "Y", BXSqlFilterOperators.Equal),
							new BXFormFilterItem("ActiveGlobal", "Y", BXSqlFilterOperators.Equal),
							new BXFormFilterItem("ID", IBlockSectionId, BXSqlFilterOperators.Equal)
						);

						BXInfoBlockSectionCollectionOld sectionCollection = BXInfoBlockSectionManagerOld.GetList(sectionFilter, null);
						if (sectionCollection.Count > 0)
						{
							IBlockSection = sectionCollection[0];
							Results["IBlockSectionName"] = BXStringUtility.XmlEncode(IBlockSection.NameRaw);
							Results["IBlockSectionDescription"] = BXStringUtility.XmlEncode(IBlockSection.DescriptionRaw);
						}
					}

					// TODO: передавать сортировку из параметров
					BXOrderBy_old elementOrderBy = new BXOrderBy_old("ActiveFromDate", "DESC");
					elementOrderBy.Add("Sort", "Asc");
					elementOrderBy.Add("ID", "Desc");

					BXInfoBlockElementCollectionOld elementCollection = BXInfoBlockElementManagerOld.GetList(
						elementFilter,
						elementOrderBy,
						null,
						new BXQueryParams(new BXPagingOptions(0, Parameters.Get<int>("ElementsCount")))
					);
					foreach (BXInfoBlockElementOld element in elementCollection)
					{
						BXTemplateIBlockElement item = new BXTemplateIBlockElement();
						item.ElementId = element.ElementId;
						item.Code = BXStringUtility.XmlEncode(element.CodeRaw);
						item.IBlockId = element.IBlockId;
						item.Name = BXStringUtility.XmlEncode(element.NameRaw);
						item.Image = element.PreviewImage;
						item.Description = BXStringUtility.XmlEncode(element.PreviewTextRaw);

						string displayDate = "";
						if (element.ActiveFromDate != DateTime.MinValue)
							displayDate = element.ActiveFromDate.ToString("r");
						else
							displayDate = "";
						item.DisplayDate = displayDate;

						string detailUrl = null;
						if (Parameters.ContainsKey("DetailUrl") && !String.IsNullOrEmpty(Parameters["DetailUrl"]))
						{
							string str = Parameters["DetailUrl"].Replace("#SITE_DIR#", BXSite.Current.Directory);
							str = str.Replace("#IBLOCK_ID#", iblock.IBlockId.ToString());
							str = str.Replace("#SECTION_ID#", (element.Sections.Count > 0) ? element.Sections[0].ToString() : "");
							str = str.Replace("#ELEMENT_ID#", element.ElementId.ToString());
							Results["IBlockId"] = iblock.IBlockId;
							Results["SectionId"] = (element.Sections.Count > 0) ? element.Sections[0].ToString() : "";
							Results["ElementId"] = element.ElementId;
							str = BXUri.ToRelativeUri(BXComponentManager.MakeLink(str, Results)).Replace("//", "/");
							detailUrl = BXStringUtility.XmlEncode(str);
						}
						item.DetailUrl = detailUrl;
						items.Add(item);
					}
				}
			}

			IncludeComponentTemplate();
		}
	}
	private void InitPage()
	{
		iblockId = base.GetRequestInt("iblock_id");
		if (iblockId > 0)
			hfIBlockId.Value = iblockId.ToString();
		Int32.TryParse(hfIBlockId.Value, out iblockId);
		if (iblockId > 0)
		{
			iblock = BXInfoBlockManagerOld.GetById(iblockId);
			if (iblock == null)
			{
				iblockId = 0;
				hfIBlockId.Value = iblockId.ToString();
			}
		}

		if (iblockId > 0)
		{
			typeId = iblock.TypeId;
			hfTypeId.Value = typeId.ToString();
			type = BXInfoBlockTypeManagerOld.GetById(typeId);
		}
		else
		{
			typeId = base.GetRequestInt("type_id");
			if (typeId > 0)
				hfTypeId.Value = typeId.ToString();
			Int32.TryParse(hfTypeId.Value, out typeId);
		}

		if (!this.BXUser.IsCanOperate("IBlockView", "iblock", iblockId))
			BXAuthentication.AuthenticationRequired();

		filterSectionId.Values.Add(new ListItem(GetMessage("Any"), ""));
		filterSectionId.Values.Add(new ListItem(GetMessage("TopLevel"), "0"));

		BXIBlockSectionCollection sections = BXIBlockSection.GetList(
			((iblockId > 0)? new BXFilter(new BXFilterItem(BXIBlockSection.Fields.IBlock.ID, BXSqlFilterOperators.Equal, iblockId)) : null),
			new BXOrderBy(new BXOrderByPair(BXIBlockSection.Fields.LeftMargin, BXOrderByDirection.Asc)),
			null,
			null,
			BXTextEncoder.EmptyTextEncoder
		);

		foreach (BXIBlockSection section in sections)
		{
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < section.DepthLevel; i++)
				sb.Append(" . ");
			sb.Append(section.Name);
			filterSectionId.Values.Add(new ListItem(sb.ToString(), section.Id.ToString()));
		}

		filterModifiedBy.Values.Add(new ListItem(GetMessage("Any"), ""));
		filterCreatedBy.Values.Add(new ListItem(GetMessage("Any"), ""));
		foreach (BXUser user in BXUserManager.GetList(null, new BXOrderBy_old("LastName", "Asc", "FirstName", "Asc", "UserName", "Asc")))
		{
			filterModifiedBy.Values.Add(new ListItem("(" + user.UserName + ") " + user.FirstName + " " + user.LastName, user.UserId.ToString()));
			filterCreatedBy.Values.Add(new ListItem("(" + user.UserName + ") " + user.FirstName + " " + user.LastName, user.UserId.ToString()));
		}
	}