summaryrefslogtreecommitdiff
path: root/cgi-bin/card_table.pl
blob: a4d26f6d8f8243f647d072dc52d70a32e75497b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/perl

$|=1;

srand;

use strict;

use Apache::Request;
use Apache::Constants qw(REDIRECT);
use Benchmark::Timer;
use HTML::Template;
use Bit::Vector;
use Image::Magick;

use Compose::local_lib;
use Compose::db_connection;

my $r = Apache::Request->new(Apache->request);
$r->send_http_header('text/javascript');

my $tt = new Benchmark::Timer;
$tt->start('all');

my $local_lib = new Compose::local_lib();

my $dbh = new Compose::db_connection('localhost','aes','apache','webconnect');

my $form;
foreach my $key (sort $r->param) {
        $form->{$key} = $local_lib->fix_spaces($r->param($key));
}

# table=home

##################################################
#
#
if ($r->method() eq "GET") {

	if ($form->{'table'} eq "") {

		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() );
		my %tables = $dbh->queryDB($qry,'select_0');

		print qq(
[
);
		my @list=();
		foreach my $table (sort { $a cmp $b } keys %tables) {
			push @list,"\t\"$table\"";
		}

		print join(",\n",@list) . qq(
]
);



	} elsif ($form->{'table'} ne "") {

		my $qry = "";

		my $ckb = "checkbox_1";
		if ($form->{'table'} eq "intro") {
			$ckb = "checkbox_0";
		}

		my $t = new Benchmark::Timer;

		$form->{'num_cards'} = 15; # if ($form->{'num_cards'} eq "");
		$form->{'w'} = 900 if ($form->{'w'} eq "" || $form->{'w'} < 900);
		$form->{'h'} = 400 if ($form->{'h'} eq "" || $form->{'h'} < 400);
		$form->{'max'} = 20 if ($form->{'max'} eq "");

		# get cards
		if ($form->{'table'} eq 'home') {
			$qry = qq( 
				SELECT
					content_id,
					enc_content_id,
					textfield_0                                                                                            AS title,
					select_0                                                                                               AS category,
					checkbox_4                                                                                             AS protected, 
					image_1_width                                                                                          AS rotated_width, 
					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
					checkbox_0              = "Yes"                  AND
					$ckb                    = "Yes"                  AND
					checkbox_2             != "Yes"                  AND
					!(textfield_5 is NULL or textfield_5 = "")
				ORDER BY
					last_modified_date
				LIMIT
					$form->{'num_cards'}
			);
		} else {
			#$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'}) ;

			$qry = qq( 
				SELECT 
					content_id,
					enc_content_id,
					textfield_0                                                                                            AS title, 
					select_0                                                                                               AS category, 
					checkbox_4                                                                                             AS protected, 
					image_1_width                                                                                          AS rotated_width, 
					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
					checkbox_2             != "Yes"                  AND
					!(textfield_5 IS NULL OR textfield_5 = "")
				ORDER BY 
					last_modified_date 
				LIMIT
					$form->{'num_cards'}
			);

		}

		my %res = $dbh->queryRawDB($qry);

		my @master_v = new Bit::Vector($form->{'w'},$form->{'h'});
		my ($hei,$wid);
		my @rowM=();

		my $back_image;
		if ($form->{'gen'} ne "") {
			$back_image = Image::Magick->new(size=>"$form->{'w'} x $form->{'h'}");
			$back_image->ReadImage('xc:white');
		}

		my ($idx,$x,$y);

		print qq( [ \n);
		my $iter = 0;
		foreach $idx (sort {$a <=> $b} keys %res) {
		
			my $protected = ($res{$idx}{'protected'} eq 'Yes') ? 'true' : 'false';
		
			($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'});

			print qq(\t{
		"cid"       : "$res{$idx}{'enc_content_id'}",
		"title"     : "$res{$idx}{'title'}",
		"category"  : "$res{$idx}{'category'}",
		"locked"    : $protected,
		"x"         : "$x",
		"y"         : "$y",
		"chip"      : "$res{$idx}{'rotated_image'}"
	});
	print ",\n" if (++$iter != keys(%res));
	}
		print qq( \n ]);
	
		if ($form->{'gen'} ne "") {
			$back_image->Write("/usr/web/designer/docroot/test.png");	
		}

	}

}

#$tt->stop('all');
#print $tt->report() ." <br>";
#print qq(<img src="/test.png">) if ($form->{'gen'});

###########################################
# 

sub place_data {

	my $master_v = shift;
	my $page_width = shift;
	my $page_height = shift;
	my $image_data_zdegree_image = shift;
	my $image_data_rotated_image = shift;
	my $image_data_rotated_width = shift;
	my $image_data_rotated_height = shift;
	my $image_data_publish_loc = shift;
	my $back_image = shift;
	my $gen_image = shift;
	my $max_collide = shift;

	my $done = 0;
	my $attempts = 0;
	
        my $t = new Benchmark::Timer;

	my ($max_x,$max_y,$xpos,$ypos,$vector,$sdone,$start,$attempts,$sattempts,$min,$max,$collisions,$xrand,$yrand );

	while ((!$done) && $attempts < 500) {

		$xrand = int(rand($page_width-20-$image_data_rotated_width)+10);
		$yrand = int(rand($page_height-20-$image_data_rotated_height)+10);

		my $max_collide = int($image_data_rotated_width*$image_data_rotated_height*($max_collide/100));

		$max_x = $xrand+$image_data_rotated_width;
		$max_y = $yrand+$image_data_rotated_height;

        	$t->start('3.1');

        	$collisions = 0;

        	for ($ypos = $yrand;$ypos <= $max_y; $ypos++) {
                	$vector = @{$master_v}->[$ypos];

                	$sdone=0;
                	$start = $xrand;
                	$sattempts = 0;

                	while (!$sdone && $sattempts++ < 20) {
                        	($min,$max) = $vector->Interval_Scan_inc($start);

                        	if ($max+1 >= $page_width) {
                                	$sdone=1;
                        	}

                        	if ($min < $max_x && $max) {
                                	if ($max > $max_x || $max+1 >= $page_width) {
                                        	$collisions += $max_x - $min;
	                                        $sdone = 1;
					} else {
                                        	$collisions += $max - $min;
                                        	$start = $max+1;
                               		}

                        	} elsif ($min > $max_x || !$max || !$min) {
                                	$sdone=1;
                        	}
                	}
        	}

	        #$t->stop('3.1');
        	#print $t->report() . "<br>";


		if ($collisions < $max_collide) {

			# place image in master array

			my ($xpos,$ypos);
			my $max_x = $xrand+$image_data_rotated_width;
			my $max_y = $yrand+$image_data_rotated_height;	

			#$t->start('5.1');

			my $vector;	
			for ($ypos = $yrand;$ypos <= $max_y; $ypos++) {
				$vector = @{$master_v}->[$ypos];
				$vector->Interval_Fill($xrand,$max_x);
			}

			#$t->stop('5.1');
                        #print $t->report() . "<br>";

			$done = 1;
	
			if ($gen_image) {
	                	my $card_image = Image::Magick->new();
				$card_image->Read($image_data_publish_loc);
				$back_image->Composite(image=>$card_image, x=>$xrand,y=>$yrand);
			}

		} else {

			$attempts++;
		}
	}

	#print "ATTEMPTS: $attempts <br>";

	if ($attempts >= 499) {
		#print "UNABLE TO PLACE IMAGE <br>";
	}


	return ($xrand,$yrand,$back_image);
}

##################################################
#

sub check_collisions {

	my $master_v = shift;
	my $width = shift;
	my $height = shift;
	my $xrand = shift;
	my $yrand = shift;
	my $max_collide = shift;
	my $page_width = shift;


	my $t = new Benchmark::Timer;

	my $max_x = $xrand+$width;
	my $max_y = $yrand+$height;

	#$t->start('3.1');

	my $btot = 0;

	my ($xpos,$ypos,$vector,$sdone,$start,$attempts,$min,$max);

        for ($ypos = $yrand;$ypos <= $max_y; $ypos++) {
		$vector = @{$master_v}->[$ypos];

		$sdone=0;
		$start = $xrand;
		$attempts = 0;

		while (!$sdone && $attempts++ < 20) {	
			($min,$max) = $vector->Interval_Scan_inc($start);

			if ($max+1 >= $page_width) {
				$sdone=1;
			}

			if ($min < $max_x && $max) {
				if ($max > $max_x || $max+1 >= $page_width) {
					$btot += $max_x - $min;
					$sdone = 1;
				} else { 
					$btot += $max - $min;
					$start = $max+1;
				}

			} elsif ($min > $max_x || !$max || !$min) {
				$sdone=1;
			}
		}
        }

        #$t->stop('3.1');
        #print $t->report() . "<br>";

	return $btot;

}