/* ==================================================================== * * (c) Rafael Hernandez rhm@stanford.edu 2001 * * ==================================================================== */ /* Include files */ #include #include #include #include #include /* SQL Communications Area */ #include "cgi.h" #include "orautils.h" #define PAGE15 15 #define MaX(a,b) ((a) > (b) ? (a) : (b)) /* Function prototypes */ int BrowseCatalog(char *namedob, int A, int Na, int L, int P, int B, int pg); int QueryPagePhotos(char *stmt, int page, int oldpage); char *Cart(char *av, short iav, char *aux, long cat, short icat, char *image, short iimage, long price, short iprice); /* * NAME * main * DESCRIPTION * This will output a table will all the technical info that we have * for photographs * We display the corresponding page given by "page" * and create our query depending on the user selection. */ int main() { char * uid = "/"; int result; BindingSet *bs; char *namedob = NULL, *selection, *page; int A = 0, Na = 0, L = 0, P = 0, B = 0, pg; SetEnvs(); Initialize(); EXEC SQL WHENEVER SQLERROR DO PrintErr("Error: "); EXEC SQL CONNECT :uid; /* HTML header */ printf("content-type: text/html\n\n"); printf("\n"); /* Load and print to stdin a javascript file */ /* This will include the content of this javascript file in our html document. We need this to do the cookie and cart manipulations */ LoadJS("cart.js"); printf(" \n"); /* Get all our parameters */ bs = GetValuesFor("scroller"); if (bs) namedob = GetNextValue(bs); /* printf("namedob = [%s]\n", namedob); */ if (namedob && !strcmp(namedob, "")) namedob = NULL; bs = GetValuesFor("Landscape"); if (bs) { selection = GetNextValue(bs); /* printf("L selection = %s\n", selection); */ if (selection && !strcmp(selection, "on")) L = 1; } bs = GetValuesFor("Portrait"); if (bs) { selection = GetNextValue(bs); /* printf("P selection = %s\n", selection); */ if (selection && !strcmp(selection, "on")) P = 1; } bs = GetValuesFor("Abstract"); if (bs) { selection = GetNextValue(bs); /* printf("B selection = %s\n", selection); */ if (selection && !strcmp(selection, "on")) B = 1; } bs = GetValuesFor("Available"); if (bs) { selection = GetNextValue(bs); /* printf("A selection = %s\n", selection); */ if (selection && !strcmp(selection, "on")) A = 1; } bs = GetValuesFor("NonAvailable"); if (bs) { selection = GetNextValue(bs); /* printf("Na selection = %s\n", selection); */ if (selection && !strcmp(selection, "on")) Na = 1; } /* What page to show? */ bs = GetValuesFor("page"); if (bs) { page = GetNextValue(bs); if (!page) page = "1"; } else page = "1"; pg = atoi(page); /* Do the query */ result = BrowseCatalog(namedob, A, Na, L, P, B, pg); if (result) { /* output all the info using hidden fields in order to be able to do the paging */ printf("
", homepagecgi); printf("", namedob == NULL ? "" : namedob); if (L) printf(""); if (P) printf(""); if (B) printf(""); if (A) printf(""); if (Na) printf(""); printf("", pg + 1); printf(""); printf(""); printf("
"); printf("
", homepagecgi); printf(""); printf("
"); } else { /* No more data ! */ printf("
", homepagecgi); printf("

*** NO MORE DATA ***"); printf("

"); printf("

"); printf("
", homepagecgi); printf(""); printf("
"); } PrintHTMLEnd(); EXEC SQL ROLLBACK RELEASE; return(0); } /* * NAME * BrowseCatalog * DESCRIPTION * Browse the catalog of images. We can browse depending * on several options, the author, availability, type of * photo. */ int BrowseCatalog(char *namedob, int A, int Na, int L, int P, int B, int pg) { char name[256]; char dob[256]; char where[512]; char stmt[1024]; int andflag = 0, orflag = 0, page, oldpage; page = pg * PAGE15; oldpage = page - PAGE15; stmt[0] = 0; strcpy(stmt, "SELECT catalogn, film, color, fstop, speed, resolution, available, image, price FROM (SELECT ROWNUM AS rnum, catalogn, film, color, fstop, speed, resolution, available, image, price FROM (SELECT P.catalogn, P.film, P.color, P.fstop, P.speed, P.resolution, P.available, P.image, P.price FROM Photo P"); where[0] = 0; /* Have we selected anything at all? */ if (namedob || ( A + Na + L + P + B) > 0) { strcpy(where, " WHERE ("); } /* Do we want all the Availables or Not availables? */ if (A + Na == 1) { if (A) { strcat(where, " P.available='Y' )"); } if (Na) { strcat(where, " P.available='N' )"); } andflag = 1; } /* What types? Landscape, Portrait or/and Abstract */ if (L + P + B > 0 && L + P + B < 3) { if (L) { if (andflag) strcat(where, " AND ("); strcat(where, " EXISTS (SELECT catalogn FROM Landscape WHERE catalogn = P.catalogn) "); orflag = 1; } if (P) { if (orflag) strcat(where, " OR "); else if (andflag) strcat(where, " AND ("); strcat(where, " EXISTS (SELECT catalogn FROM Portrait WHERE catalogn = P.catalogn) "); orflag = 1; } if (B) { if (orflag) strcat(where, " OR "); else if (andflag) strcat(where, " AND ("); strcat(where, " EXISTS (SELECT catalogn FROM Abstract WHERE catalogn = P.catalogn) "); } strcat(where, ") "); andflag = 1; } /* What author?, if any */ if (namedob) { name[0] = 0; dob[0] = 0; strcpy(name, strtok(namedob, "|")); strcpy(dob, strtok(NULL, "\0")); strcat(stmt, ", Takes T "); if (andflag) strcat(where, " AND ("); strcat(where, " P.catalogn=T.catalogn AND T.name='"); strcat(where, name); strcat(where, "' AND T.dob='"); strcat(where, dob); strcat(where, "' )"); } strcat(stmt, where); strcat(stmt, ") WHERE ROWNUM <= :page) WHERE rnum > :oldpage"); /* printf("

%s\n", stmt); */ /* Do the query */ return(QueryPagePhotos(stmt, page, oldpage)); } /* * NAME * QueryPagePhotos * DESCRIPTION * Do all the selection and paging while browsing the Photo Catalog */ int QueryPagePhotos(char *stmt, int page, int oldpage) { int done = 0, i, j = 0, numinpage; long int acat[PAGE15]; char afilm[PAGE15][256], acolor[PAGE15][3]; float afstop[PAGE15]; char aspeed[PAGE15][256]; long int aresolution[PAGE15]; char aavailable[PAGE15][3]; char aimage[PAGE15][256]; long int aprice[PAGE15]; short int iacat[PAGE15], iafilm[PAGE15], iacolor[PAGE15], iafstop[PAGE15], iaspeed[PAGE15], iaresolution[PAGE15], iaavailable[PAGE15], iaprice[PAGE15], iaimage[PAGE15]; char aux[1024]; EXEC SQL VAR afilm IS STRING(256); EXEC SQL VAR aspeed IS STRING(256); EXEC SQL VAR aimage IS STRING(256); EXEC SQL VAR acolor IS STRING(3); EXEC SQL VAR aavailable IS STRING(3); EXEC SQL PREPARE S1 FROM :stmt; EXEC SQL DECLARE c1 CURSOR FOR S1; EXEC SQL OPEN c1 USING :page, :oldpage; EXEC SQL WHENEVER NOT FOUND CONTINUE; EXEC SQL FETCH c1 INTO :acat:iacat, :afilm:iafilm, :acolor:iacolor, :afstop:iafstop, :aspeed:iaspeed, :aresolution:iaresolution, :aavailable:iaavailable, :aimage:iaimage, :aprice:iaprice; /* Display the photographers. Note, technically we only execute the loop once but I wanted to reuse my code from pda5 so I did just minimal changes */ while (!done) { printf("\n"); printf("" "" "\n"); numinpage = sqlca.sqlerrd[2] - j * PAGE15; for (i = 0; i < numinpage; ++i) { printf("\n",i % 2 == 0 ? "Aqua" : "Gray", I(acat[i], iacat[i]), S(afilm[i], iafilm[i]), S(acolor[i], iacolor[i]), F(afstop[i], iafstop[i]), S(aspeed[i], iaspeed[i]), I(aresolution[i], iaresolution[i]), Cart(aavailable[i], iaavailable[i], aux, acat[i], iacat[i], aimage[i], iaimage[i], aprice[i], iaprice[i]), I(aprice[i], iaprice[i]), homepage, S(aimage[i], iaimage[i]), S(aimage[i], iaimage[i])); } printf("
NUMBERFILMCOLORF-STOPSPEEDRESOLUTIONAVAILABLEPRICEIMAGE
%ld%s%s%.1f%s%ld%s%ld%s
\n"); if (numinpage == PAGE15) { done = 1; } else break; } EXEC SQL CLOSE c1; return done; } /* * NAME * Cart * DESCRIPTION * Create a button Add for the available photos. We will use * the JavaScript method add (defined in cart.js) */ char *Cart(char *av, short iav, char *aux, long cat, short icat, char *image, short iimage, long price, short iprice) { if (iav == -1) return ""; if (!strcmp(av, "Y")) { aux[0] = 0; sprintf(aux, "

", homepagecgi, I(cat, icat), S(image, iimage), I(price, iprice)); return aux; } if (!strcmp(av, "N")) return "N"; return ""; }