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