شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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