diff options
author | Mike Crute <mcrute@gmail.com> | 2010-01-17 12:06:15 -0500 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2010-01-17 12:06:15 -0500 |
commit | 55974d21a824378b287e563bce4c32597060cfca (patch) | |
tree | ff8426efe10ff98189fee26583f061fb08f19a50 /cgi-bin/card_table.pl | |
download | santoprene_designer_site-master.tar.bz2 santoprene_designer_site-master.tar.xz santoprene_designer_site-master.zip |
Diffstat (limited to 'cgi-bin/card_table.pl')
-rwxr-xr-x | cgi-bin/card_table.pl | 371 |
1 files changed, 371 insertions, 0 deletions
diff --git a/cgi-bin/card_table.pl b/cgi-bin/card_table.pl new file mode 100755 index 0000000..a4d26f6 --- /dev/null +++ b/cgi-bin/card_table.pl | |||
@@ -0,0 +1,371 @@ | |||
1 | #!/usr/bin/perl | ||
2 | |||
3 | $|=1; | ||
4 | |||
5 | srand; | ||
6 | |||
7 | use strict; | ||
8 | |||
9 | use Apache::Request; | ||
10 | use Apache::Constants qw(REDIRECT); | ||
11 | use Benchmark::Timer; | ||
12 | use HTML::Template; | ||
13 | use Bit::Vector; | ||
14 | use Image::Magick; | ||
15 | |||
16 | use Compose::local_lib; | ||
17 | use Compose::db_connection; | ||
18 | |||
19 | my $r = Apache::Request->new(Apache->request); | ||
20 | $r->send_http_header('text/javascript'); | ||
21 | |||
22 | my $tt = new Benchmark::Timer; | ||
23 | $tt->start('all'); | ||
24 | |||
25 | my $local_lib = new Compose::local_lib(); | ||
26 | |||
27 | my $dbh = new Compose::db_connection('localhost','aes','apache','webconnect'); | ||
28 | |||
29 | my $form; | ||
30 | foreach my $key (sort $r->param) { | ||
31 | $form->{$key} = $local_lib->fix_spaces($r->param($key)); | ||
32 | } | ||
33 | |||
34 | # table=home | ||
35 | |||
36 | ################################################## | ||
37 | # | ||
38 | # | ||
39 | if ($r->method() eq "GET") { | ||
40 | |||
41 | if ($form->{'table'} eq "") { | ||
42 | |||
43 | my $qry = qq(select distinct select_0 from content_data where site_id=1 and form_id=48 and moderation_status=3 and date_0 <= NOW() ); | ||
44 | my %tables = $dbh->queryDB($qry,'select_0'); | ||
45 | |||
46 | print qq( | ||
47 | [ | ||
48 | ); | ||
49 | my @list=(); | ||
50 | foreach my $table (sort { $a cmp $b } keys %tables) { | ||
51 | push @list,"\t\"$table\""; | ||
52 | } | ||
53 | |||
54 | print join(",\n",@list) . qq( | ||
55 | ] | ||
56 | ); | ||
57 | |||
58 | |||
59 | |||
60 | } elsif ($form->{'table'} ne "") { | ||
61 | |||
62 | my $qry = ""; | ||
63 | |||
64 | my $ckb = "checkbox_1"; | ||
65 | if ($form->{'table'} eq "intro") { | ||
66 | $ckb = "checkbox_0"; | ||
67 | } | ||
68 | |||
69 | my $t = new Benchmark::Timer; | ||
70 | |||
71 | $form->{'num_cards'} = 15; # if ($form->{'num_cards'} eq ""); | ||
72 | $form->{'w'} = 900 if ($form->{'w'} eq "" || $form->{'w'} < 900); | ||
73 | $form->{'h'} = 400 if ($form->{'h'} eq "" || $form->{'h'} < 400); | ||
74 | $form->{'max'} = 20 if ($form->{'max'} eq ""); | ||
75 | |||
76 | # get cards | ||
77 | if ($form->{'table'} eq 'home') { | ||
78 | $qry = qq( | ||
79 | SELECT | ||
80 | content_id, | ||
81 | enc_content_id, | ||
82 | textfield_0 AS title, | ||
83 | select_0 AS category, | ||
84 | checkbox_4 AS protected, | ||
85 | image_1_width AS rotated_width, | ||
86 | image_1_height AS rotated_height, | ||
87 | image_2_width AS zdegree_width, | ||
88 | image_2_height AS zdegree_height, | ||
89 | CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/index.html") AS page_loc, | ||
90 | CONCAT(clients.website.server_docroot, clients.website.publish_docroot,"/",human_dir,"/orig/",image_1) AS publish_loc, | ||
91 | CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/orig/",image_1) AS rotated_image, | ||
92 | CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/orig/",image_2) AS zdegree_image | ||
93 | FROM | ||
94 | content_data, | ||
95 | clients.website | ||
96 | WHERE | ||
97 | clients.website.site_id = content_data.site_id AND | ||
98 | content_data.site_id = 1 AND | ||
99 | form_id = 48 AND | ||
100 | moderation_status >= 3 AND | ||
101 | date_0 <= NOW() AND | ||
102 | checkbox_0 = "Yes" AND | ||
103 | $ckb = "Yes" AND | ||
104 | checkbox_2 != "Yes" AND | ||
105 | !(textfield_5 is NULL or textfield_5 = "") | ||
106 | ORDER BY | ||
107 | last_modified_date | ||
108 | LIMIT | ||
109 | $form->{'num_cards'} | ||
110 | ); | ||
111 | } else { | ||
112 | #$qry = qq( select content_id,enc_content_id,textfield_0 as title, select_0 as category, image_1_height as rotated_height, image_2_width as zdegree_width, image_2_height as zdegree_height, CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/index.html") as page_loc , CONCAT(clients.website.server_docroot, clients.website.publish_docroot,"/",human_dir,"/orig/",image_1) as publish_loc, CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/orig/",image_1) as rotated_image, CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/orig/",image_2) as zdegree_image from content_data,clients.website where clients.website.site_id=content_data.site_id and content_data.site_id=1 and form_id=48 and moderation_status >=3 and date_0 <= NOW() and select_0="$form->{'table'}" and $ckb="Yes" and !(textfield_5 is NULL or textfield_5 = "") and checkbox_2 != "Yes" order by last_modified_date limit $form->{'num_cards'}) ; | ||
113 | |||
114 | $qry = qq( | ||
115 | SELECT | ||
116 | content_id, | ||
117 | enc_content_id, | ||
118 | textfield_0 AS title, | ||
119 | select_0 AS category, | ||
120 | checkbox_4 AS protected, | ||
121 | image_1_width AS rotated_width, | ||
122 | image_1_height AS rotated_height, | ||
123 | image_2_width AS zdegree_width, | ||
124 | image_2_height AS zdegree_height, | ||
125 | CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/index.html") AS page_loc, | ||
126 | CONCAT(clients.website.server_docroot, clients.website.publish_docroot,"/",human_dir,"/orig/",image_1) AS publish_loc, | ||
127 | CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/orig/",image_1) AS rotated_image, | ||
128 | CONCAT(clients.website.site_url,clients.website.publish_docroot,"/",human_dir,"/orig/",image_2) AS zdegree_image | ||
129 | FROM | ||
130 | content_data, | ||
131 | clients.website | ||
132 | WHERE | ||
133 | clients.website.site_id = content_data.site_id AND | ||
134 | content_data.site_id = 1 AND | ||
135 | form_id = 48 AND | ||
136 | moderation_status >= 3 AND | ||
137 | date_0 <= NOW() AND | ||
138 | select_0 = "$form->{'table'}" AND | ||
139 | $ckb = "Yes" AND | ||
140 | checkbox_2 != "Yes" AND | ||
141 | !(textfield_5 IS NULL OR textfield_5 = "") | ||
142 | ORDER BY | ||
143 | last_modified_date | ||
144 | LIMIT | ||
145 | $form->{'num_cards'} | ||
146 | ); | ||
147 | |||
148 | } | ||
149 | |||
150 | my %res = $dbh->queryRawDB($qry); | ||
151 | |||
152 | my @master_v = new Bit::Vector($form->{'w'},$form->{'h'}); | ||
153 | my ($hei,$wid); | ||
154 | my @rowM=(); | ||
155 | |||
156 | my $back_image; | ||
157 | if ($form->{'gen'} ne "") { | ||
158 | $back_image = Image::Magick->new(size=>"$form->{'w'} x $form->{'h'}"); | ||
159 | $back_image->ReadImage('xc:white'); | ||
160 | } | ||
161 | |||
162 | my ($idx,$x,$y); | ||
163 | |||
164 | print qq( [ \n); | ||
165 | my $iter = 0; | ||
166 | foreach $idx (sort {$a <=> $b} keys %res) { | ||
167 | |||
168 | my $protected = ($res{$idx}{'protected'} eq 'Yes') ? 'true' : 'false'; | ||
169 | |||
170 | ($x,$y,$back_image) = &place_data(\@master_v,$form->{'w'},$form->{'h'},$res{$idx}{'zdegree_image'},$res{$idx}{'rotated_image'},$res{$idx}{'rotated_width'},$res{$idx}{'rotated_height'},$res{$idx}{'publish_loc'},$back_image,$form->{'gen'},$form->{'max'}); | ||
171 | |||
172 | print qq(\t{ | ||
173 | "cid" : "$res{$idx}{'enc_content_id'}", | ||
174 | "title" : "$res{$idx}{'title'}", | ||
175 | "category" : "$res{$idx}{'category'}", | ||
176 | "locked" : $protected, | ||
177 | "x" : "$x", | ||
178 | "y" : "$y", | ||
179 | "chip" : "$res{$idx}{'rotated_image'}" | ||
180 | }); | ||
181 | print ",\n" if (++$iter != keys(%res)); | ||
182 | } | ||
183 | print qq( \n ]); | ||
184 | |||
185 | if ($form->{'gen'} ne "") { | ||
186 | $back_image->Write("/usr/web/designer/docroot/test.png"); | ||
187 | } | ||
188 | |||
189 | } | ||
190 | |||
191 | } | ||
192 | |||
193 | #$tt->stop('all'); | ||
194 | #print $tt->report() ." <br>"; | ||
195 | #print qq(<img src="/test.png">) if ($form->{'gen'}); | ||
196 | |||
197 | ########################################### | ||
198 | # | ||
199 | |||
200 | sub place_data { | ||
201 | |||
202 | my $master_v = shift; | ||
203 | my $page_width = shift; | ||
204 | my $page_height = shift; | ||
205 | my $image_data_zdegree_image = shift; | ||
206 | my $image_data_rotated_image = shift; | ||
207 | my $image_data_rotated_width = shift; | ||
208 | my $image_data_rotated_height = shift; | ||
209 | my $image_data_publish_loc = shift; | ||
210 | my $back_image = shift; | ||
211 | my $gen_image = shift; | ||
212 | my $max_collide = shift; | ||
213 | |||
214 | my $done = 0; | ||
215 | my $attempts = 0; | ||
216 | |||
217 | my $t = new Benchmark::Timer; | ||
218 | |||
219 | my ($max_x,$max_y,$xpos,$ypos,$vector,$sdone,$start,$attempts,$sattempts,$min,$max,$collisions,$xrand,$yrand ); | ||
220 | |||
221 | while ((!$done) && $attempts < 500) { | ||
222 | |||
223 | $xrand = int(rand($page_width-20-$image_data_rotated_width)+10); | ||
224 | $yrand = int(rand($page_height-20-$image_data_rotated_height)+10); | ||
225 | |||
226 | my $max_collide = int($image_data_rotated_width*$image_data_rotated_height*($max_collide/100)); | ||
227 | |||
228 | $max_x = $xrand+$image_data_rotated_width; | ||
229 | $max_y = $yrand+$image_data_rotated_height; | ||
230 | |||
231 | $t->start('3.1'); | ||
232 | |||
233 | $collisions = 0; | ||
234 | |||
235 | for ($ypos = $yrand;$ypos <= $max_y; $ypos++) { | ||
236 | $vector = @{$master_v}->[$ypos]; | ||
237 | |||
238 | $sdone=0; | ||
239 | $start = $xrand; | ||
240 | $sattempts = 0; | ||
241 | |||
242 | while (!$sdone && $sattempts++ < 20) { | ||
243 | ($min,$max) = $vector->Interval_Scan_inc($start); | ||
244 | |||
245 | if ($max+1 >= $page_width) { | ||
246 | $sdone=1; | ||
247 | } | ||
248 | |||
249 | if ($min < $max_x && $max) { | ||
250 | if ($max > $max_x || $max+1 >= $page_width) { | ||
251 | $collisions += $max_x - $min; | ||
252 | $sdone = 1; | ||
253 | } else { | ||
254 | $collisions += $max - $min; | ||
255 | $start = $max+1; | ||
256 | } | ||
257 | |||
258 | } elsif ($min > $max_x || !$max || !$min) { | ||
259 | $sdone=1; | ||
260 | } | ||
261 | } | ||
262 | } | ||
263 | |||
264 | #$t->stop('3.1'); | ||
265 | #print $t->report() . "<br>"; | ||
266 | |||
267 | |||
268 | if ($collisions < $max_collide) { | ||
269 | |||
270 | # place image in master array | ||
271 | |||
272 | my ($xpos,$ypos); | ||
273 | my $max_x = $xrand+$image_data_rotated_width; | ||
274 | my $max_y = $yrand+$image_data_rotated_height; | ||
275 | |||
276 | #$t->start('5.1'); | ||
277 | |||
278 | my $vector; | ||
279 | for ($ypos = $yrand;$ypos <= $max_y; $ypos++) { | ||
280 | $vector = @{$master_v}->[$ypos]; | ||
281 | $vector->Interval_Fill($xrand,$max_x); | ||
282 | } | ||
283 | |||
284 | #$t->stop('5.1'); | ||
285 | #print $t->report() . "<br>"; | ||
286 | |||
287 | $done = 1; | ||
288 | |||
289 | if ($gen_image) { | ||
290 | my $card_image = Image::Magick->new(); | ||
291 | $card_image->Read($image_data_publish_loc); | ||
292 | $back_image->Composite(image=>$card_image, x=>$xrand,y=>$yrand); | ||
293 | } | ||
294 | |||
295 | } else { | ||
296 | |||
297 | $attempts++; | ||
298 | } | ||
299 | } | ||
300 | |||
301 | #print "ATTEMPTS: $attempts <br>"; | ||
302 | |||
303 | if ($attempts >= 499) { | ||
304 | #print "UNABLE TO PLACE IMAGE <br>"; | ||
305 | } | ||
306 | |||
307 | |||
308 | return ($xrand,$yrand,$back_image); | ||
309 | } | ||
310 | |||
311 | ################################################## | ||
312 | # | ||
313 | |||
314 | sub check_collisions { | ||
315 | |||
316 | my $master_v = shift; | ||
317 | my $width = shift; | ||
318 | my $height = shift; | ||
319 | my $xrand = shift; | ||
320 | my $yrand = shift; | ||
321 | my $max_collide = shift; | ||
322 | my $page_width = shift; | ||
323 | |||
324 | |||
325 | my $t = new Benchmark::Timer; | ||
326 | |||
327 | my $max_x = $xrand+$width; | ||
328 | my $max_y = $yrand+$height; | ||
329 | |||
330 | #$t->start('3.1'); | ||
331 | |||
332 | my $btot = 0; | ||
333 | |||
334 | my ($xpos,$ypos,$vector,$sdone,$start,$attempts,$min,$max); | ||
335 | |||
336 | for ($ypos = $yrand;$ypos <= $max_y; $ypos++) { | ||
337 | $vector = @{$master_v}->[$ypos]; | ||
338 | |||
339 | $sdone=0; | ||
340 | $start = $xrand; | ||
341 | $attempts = 0; | ||
342 | |||
343 | while (!$sdone && $attempts++ < 20) { | ||
344 | ($min,$max) = $vector->Interval_Scan_inc($start); | ||
345 | |||
346 | if ($max+1 >= $page_width) { | ||
347 | $sdone=1; | ||
348 | } | ||
349 | |||
350 | if ($min < $max_x && $max) { | ||
351 | if ($max > $max_x || $max+1 >= $page_width) { | ||
352 | $btot += $max_x - $min; | ||
353 | $sdone = 1; | ||
354 | } else { | ||
355 | $btot += $max - $min; | ||
356 | $start = $max+1; | ||
357 | } | ||
358 | |||
359 | } elsif ($min > $max_x || !$max || !$min) { | ||
360 | $sdone=1; | ||
361 | } | ||
362 | } | ||
363 | } | ||
364 | |||
365 | #$t->stop('3.1'); | ||
366 | #print $t->report() . "<br>"; | ||
367 | |||
368 | return $btot; | ||
369 | |||
370 | } | ||
371 | |||