Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Newspaper

Public general use code classes and xml files that we've compiled and used over the years:

Newspaper and publication display format support class.

   1:  using System;
   2:  using System.Data;
   3:  using System.Collections;
   4:  using System.Collections.Generic;
   5:  using System.Linq;
   6:  using System.Web;
   7:   
   8:  namespace Ia.Cl.Model
   9:  {
  10:      /// <summary publish="true">
  11:      /// Newspaper and publication display format support class.
  12:      /// </summary>
  13:      /// <remarks> 
  14:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  15:      ///
  16:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  17:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  18:      ///
  19:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  20:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  21:      /// 
  22:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  23:      /// 
  24:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  25:      /// </remarks>
  26:      public class Newspaper
  27:      {
  28:          public Newspaper() { }
  29:   
  30:          ////////////////////////////////////////////////////////////////////////////
  31:   
  32:          /// <summary>
  33:          ///
  34:          /// </summary>
  35:          public static DataTable CountryMenu(DataTable countryDataTable, DataTable advertisementDataTable)
  36:          {
  37:              // This will format the country menu and add a count columns to indicate the number
  38:              // of advs under the country
  39:   
  40:              // This returns a DataTable that has the result of the filter operation on the table
  41:   
  42:              //string sort = "id ASC",filter="";
  43:              DataTable dt = new DataTable();
  44:              DataRow dr;
  45:   
  46:              dt = countryDataTable.Clone();
  47:              dt.Columns.Add(new DataColumn("count", System.Type.GetType("System.Int32")));
  48:   
  49:              foreach (DataRow row in countryDataTable.Rows) //,sort))
  50:              {
  51:                  dr = dt.NewRow();
  52:   
  53:                  if (row["id"].ToString() == "0") advertisementDataTable.DefaultView.RowFilter = "show = true AND country_id <> -1";
  54:                  else advertisementDataTable.DefaultView.RowFilter = "show = true AND country_id = " + row["id"];
  55:   
  56:                  dr["count"] = advertisementDataTable.DefaultView.Count;
  57:                  dr["id"] = row["id"];
  58:                  dr["name"] = row["name"].ToString() + " (" + dr["count"].ToString() + ")";
  59:                  dr["name_e"] = row["name_e"].ToString() + " (" + dr["count"].ToString() + ")";
  60:                  dt.Rows.Add(dr);
  61:              }
  62:   
  63:              return dt;
  64:          }
  65:   
  66:          ////////////////////////////////////////////////////////////////////////////
  67:   
  68:          /// <summary>
  69:          ///
  70:          /// </summary>
  71:          public static string ClassifiedGrid(DataView dataView)
  72:          {
  73:              // the screenResolution will define the resolution of the user's computer screen. The output will be formated to best fit 
  74:              // this screen.
  75:   
  76:              // This function will generate a string format of a table that contains ads generated in a tabular form
  77:              bool added, show;
  78:              string str, id, width, height, html, style;
  79:              int duration;
  80:              DateTime modified;
  81:              int xpos, ypos, count, last_line_count, columns;
  82:   
  83:              columns = 7;
  84:   
  85:              Grid grid = new Grid(columns, 30);
  86:              Point point;
  87:   
  88:              // loop through classified ads
  89:   
  90:              foreach (DataRowView adv in dataView)
  91:              {
  92:                  id = adv["id"].ToString();
  93:                  width = adv["width"].ToString();
  94:                  height = adv["height"].ToString();
  95:                  html = global::Ia.Cl.Model.Html.HtmlDecode(adv["html"].ToString());
  96:                  style = adv["style"].ToString();
  97:                  show = (bool)adv["show"];
  98:                  modified = (DateTime)(adv["modified"]);
  99:                  duration = int.Parse(adv["duration"].ToString());
 100:   
 101:                  added = false;
 102:   
 103:                  xpos = 0; ypos = 0;
 104:   
 105:                  if (Math.Abs(modified.DayOfYear - DateTime.UtcNow.AddHours(3).DayOfYear) <= duration)
 106:                  {
 107:                      // this checks if the adv was modified within x days from this day.
 108:   
 109:                      if (show)
 110:                      {
 111:                          while (!added)
 112:                          {
 113:                              if (grid.IsAvailable(xpos, ypos, int.Parse(width), int.Parse(height)))
 114:                              {
 115:                                  grid.Add(int.Parse(id), html, xpos, ypos, int.Parse(width), int.Parse(height), style);
 116:                                  added = true;
 117:                              }
 118:   
 119:                              // increment position
 120:                              if (xpos == grid.Width) { xpos = 0; ypos++; }
 121:                              else xpos++;
 122:                          }
 123:                      }
 124:                  }
 125:   
 126:              }
 127:   
 128:              // output the values stored in the grid:
 129:   
 130:              count = last_line_count = 0;
 131:              str = "<table class=grid cellspacing=3>";
 132:   
 133:              for (int y = 0; y <= grid.Height; y++)
 134:              {
 135:                  str += "<tr>";
 136:   
 137:                  for (int x = 0; x <= grid.Width; x++)
 138:                  {
 139:                      point = grid.Read(x, y);
 140:   
 141:                      if (point != null)
 142:                      {
 143:                          //str += point.id+":"+point.x+":"+point.y+":"+point.html;
 144:                          str += "<td width=" + (100 * point.width) + " colspan=" + point.width + " height=" + (100 * point.height) + " rowspan=" + point.height + " class=\"" + point.style + "\">";
 145:                          str += point.html;
 146:                          str += "</td>";
 147:                          count++;
 148:                      }
 149:                      /*
 150:                                else if(count >= dv.Count && grid.IsAvailable(x,y,1,1))
 151:                                {
 152:                                  str += "<td width=100 colspan=1 height=100 rowspan=1>&nbsp;</td>";
 153:                                  last_line_count++;
 154:                                }
 155:                      */
 156:                      else if (grid.IsAvailable(x, y, 1, 1))
 157:                      {
 158:                          str += "<td width=100 colspan=1 height=100 rowspan=1>&nbsp;</td>";
 159:                          last_line_count++;
 160:                      }
 161:                  }
 162:                  str += "</tr>";
 163:   
 164:                  if (last_line_count == (grid.Width + 1)) break; // exit the height loop
 165:                  else last_line_count = 0;
 166:              }
 167:              str += "</table>";
 168:   
 169:              return str;
 170:          }
 171:   
 172:          ////////////////////////////////////////////////////////////////////////////
 173:   
 174:          /// <summary>
 175:          ///
 176:          /// </summary>
 177:          public class Grid
 178:          {
 179:              /// <summary/>
 180:              public bool occupied;
 181:   
 182:              /// <summary/>
 183:              public int x, y, grid_width, grid_height;
 184:   
 185:              private ArrayList point = new ArrayList();
 186:   
 187:              ////////////////////////////////////////////////////////////////////////////
 188:   
 189:              /// <summary>
 190:              ///
 191:              /// </summary>
 192:              public Grid(int grid_width, int grid_height)
 193:              {
 194:                  this.grid_width = grid_width;
 195:                  this.grid_height = grid_height;
 196:              }
 197:   
 198:              ////////////////////////////////////////////////////////////////////////////
 199:   
 200:              /// <summary>
 201:              ///
 202:              /// </summary>
 203:              public void Add(int id, string html, int x, int y, int width, int height, string style)
 204:              {
 205:                  point.Add(new Point(id, html, x, y, width, height, style));
 206:              }
 207:   
 208:              ////////////////////////////////////////////////////////////////////////////
 209:   
 210:              /// <summary>
 211:              ///
 212:              /// </summary>
 213:              public Point Read(int x, int y)
 214:              {
 215:                  foreach (Point p in point)
 216:                  {
 217:                      if (p.x == x && p.y == y) return p;
 218:                  }
 219:   
 220:                  return null;
 221:              }
 222:   
 223:              ////////////////////////////////////////////////////////////////////////////
 224:   
 225:              /// <summary>
 226:              ///
 227:              /// </summary>
 228:              public bool IsAvailable(int x, int y, int w, int h)
 229:              {
 230:                  // this checks if new adv could be added without conflect
 231:   
 232:                  // checks that the position is within the grid
 233:                  if (x > grid_width || y > grid_height) return false;
 234:   
 235:                  // checks that the adv is within the grid
 236:                  if ((x + w - 1) > grid_width || (y + h - 1) > grid_height) return false;
 237:   
 238:                  // loop through all points in grid
 239:                  foreach (Point p in point)
 240:                  {
 241:                      // if this position is occupied return false
 242:                      if (p.x == x && p.y == y) return false;
 243:                      // if this position falls within the width *and* height of a point return false:
 244:                      else if ((x >= p.x) && (x < (p.x + p.width)) && (y >= p.y) && (y < (p.y + p.height))) return false;
 245:   
 246:                      else
 247:                      {
 248:                          // this checks that the adv area does note fall within the area of another ad
 249:   
 250:                          int x_max, x_min, y_max, y_min;
 251:                          int px_max, px_min, py_max, py_min;
 252:   
 253:                          x_max = x + w;
 254:                          x_min = x;
 255:                          y_max = y + h;
 256:                          y_min = y;
 257:   
 258:                          px_max = p.x + p.width;
 259:                          px_min = p.x;
 260:                          py_max = p.y + p.height;
 261:                          py_min = p.y;
 262:   
 263:                          if (!(((x_min >= px_max) || (x_max <= px_min)) || ((y_min >= py_max) || (y_max <= py_min)))) return false;
 264:                      }
 265:                  }
 266:   
 267:                  return true;
 268:              }
 269:   
 270:              /// <summary/>
 271:              public int Width { get { return grid_width; } }
 272:   
 273:              /// <summary/>
 274:              public int Height { get { return grid_height; } }
 275:          }
 276:   
 277:          ////////////////////////////////////////////////////////////////////////////
 278:   
 279:          /// <summary>
 280:          ///
 281:          /// </summary>
 282:          public class Point
 283:          {
 284:              /// <summary/>
 285:              public int id;
 286:   
 287:              /// <summary/>
 288:              public bool occupied;
 289:   
 290:              /// <summary/>
 291:              public int x, y, width, height;
 292:   
 293:              /// <summary/>
 294:              public string html, style;
 295:   
 296:              ////////////////////////////////////////////////////////////////////////////
 297:   
 298:              /// <summary>
 299:              ///
 300:              /// </summary>
 301:              public Point(int id, string html, int x, int y, int width, int height, string style)
 302:              {
 303:                  this.id = id;
 304:                  this.x = x;
 305:                  this.y = y;
 306:                  this.width = width;
 307:                  this.height = height;
 308:                  this.html = html;
 309:                  this.style = style;
 310:              }
 311:   
 312:              /// <summary/>
 313:              public int X { get { return x; } }
 314:   
 315:              /// <summary/>
 316:              public int Y { get { return y; } }
 317:   
 318:              /// <summary/>
 319:              public int Width { get { return width; } }
 320:   
 321:              /// <summary/>
 322:              public int Height { get { return height; } }
 323:   
 324:              /// <summary/>
 325:              public int Id { get { return id; } }
 326:   
 327:              /// <summary/>
 328:              public string Html { get { return html; } }
 329:   
 330:              /// <summary/>
 331:              public string Style { get { return style; } }
 332:          }
 333:   
 334:          ////////////////////////////////////////////////////////////////////////////
 335:          ////////////////////////////////////////////////////////////////////////////
 336:      }
 337:  }