11<?php
22////////////////////////////////////////////////////////////////////////////
3- // SCHEDULE AJAX CALLS
3+ // GENERATION AJAX CALLS
44//
55// @author Ben Russell ([email protected] ) 66//
7- // @file js/scheduleAjax .php
7+ // @file api/generate .php
88// @descrip Provides standalone JSON object retreival for schedule designing
99// form and display
1010////////////////////////////////////////////////////////////////////////////
@@ -184,42 +184,6 @@ function timeStringToMinutes($str) {
184184 return $ hour * 60 + $ minute ;
185185}
186186
187- /**
188- * Generates a render of schedule's SVG. The PNG render of the image will be
189- * stored in /img/schedules/ with a filename equal to the id of the schedule.
190- * @param $svg string The SVG code for the image
191- * @param $id string The ID of the schedule, for file name generation
192- * @return bool True on success, False otherwise.
193- */
194- function renderSvg ($ svg , $ id ) {
195- try {
196-
197- // Prepend parsing info
198- $ svg = preg_replace ('/(.*<svg[^>]* width=")(100\%)(.*)/ ' , '${1}1000px${3} ' , $ svg );
199- $ svg = '<?xml version="1.1" encoding="UTF-8" standalone="no"?> ' . $ svg ;
200- // Load the image into an ImageMagick object
201- $ im = new Imagick ();
202- $ im ->readimageblob ($ svg );
203-
204- // Convert it to png
205- $ im ->setImageFormat ("png24 " );
206-
207- $ im ->scaleimage (1000 , 600 , true );
208-
209-
210- // Write it to the filesystem
211- $ im ->writeimage ("../img/schedules/ {$ id }.png " );
212- $ im ->clear ();
213- $ im ->destroy ();
214-
215- // Success!
216- return true ;
217-
218- } catch (Exception $ e ) {
219- return false ;
220- }
221- }
222-
223187////////////////////////////////////////////////////////////////////////////
224188// MAIN EXECUTION
225189
@@ -229,12 +193,7 @@ function renderSvg($svg, $id) {
229193// Escape the post data
230194$ _POST = sanitize ($ _POST );
231195
232- // What action are we performing today?
233- if (empty ($ _POST ['action ' ])) {
234- $ _POST ['action ' ] = null ;
235- }
236-
237- switch ($ _POST ['action ' ]) {
196+ switch (getAction ()) {
238197 ////////////////////////////////////////////////////////////////////////
239198 // GET COURSE OPTIONS
240199 case "getCourseOpts " :
@@ -325,22 +284,6 @@ function renderSvg($svg, $id) {
325284
326285 break ;
327286
328- ////////////////////////////////////////////////////////////////////////
329- // GET TIME DROPDOWNS
330- case "getTimeField " :
331- // Verify that we have the field name and the default time
332- if (empty ($ _POST ['name ' ])) {
333- die (json_encode (array ("error " =>"argument " , "msg " =>"A field name must be provided " , "arg " =>"name " )));
334- }
335- if (empty ($ _POST ['default ' ])) {
336- die (json_encode (array ("error " =>"argument " , "msg " =>"A default time must be provided " , "arg " =>"default " )));
337- }
338-
339- // Return the code
340- echo json_encode (array ("code " => getTimeField ($ _POST ['name ' ], $ _POST ['default ' ])));
341-
342- break ;
343-
344287 ////////////////////////////////////////////////////////////////////////
345288 // GET MATCHING SCHEDULES
346289 case "getMatchingSchedules " :
@@ -431,76 +374,7 @@ function renderSvg($svg, $id) {
431374
432375 break ;
433376
434- ////////////////////////////////////////////////////////////////////////
435- // STORE A SCHEDULE
436- case "saveSchedule " :
437- // There has to be a json object given
438- if (empty ($ _POST ['data ' ])) {
439- die (json_encode (array ("error " => "argument " , "msg " => "No schedule was provided " , "arg " => "schedule " )));
440- }
441- $ _POST ['data ' ] = html_entity_decode ($ _POST ['data ' ], ENT_QUOTES );
442- $ json = stripslashes ($ _POST ['data ' ]);
443-
444- // Make sure the object was successfully decoded
445- $ json = json_decode ($ json , true );
446- if ($ json == null ) {
447- die (json_encode (array ("error " => "argument " , "msg " => "The schedule could not be decoded " , "arg " => "schedule " )));
448- }
449- if (!isset ($ json ['starttime ' ]) || !isset ($ json ['endtime ' ]) || !isset ($ json ['building ' ]) || !isset ($ json ['startday ' ]) || !isset ($ json ['endday ' ])) {
450- die (json_encode (array ("error " => "argument " , "msg " => "A required schedule parameter was not provided " )));
451- }
452-
453- // Start the storing process with storing the data about the schedule
454- $ query = "INSERT INTO schedules (oldid, startday, endday, starttime, endtime, building, quarter) " .
455- " VALUES('', ' {$ json ['startday ' ]}', ' {$ json ['endday ' ]}', ' {$ json ['starttime ' ]}', ' {$ json ['endtime ' ]}', ' {$ json ['building ' ]}', " .
456- " ' {$ json ['term ' ]}') " ;
457- $ result = mysql_query ($ query );
458- if (!$ result ) {
459- die (json_encode (array ("error " => "mysql " , "msg " => "Failed to store the schedule: " . mysql_error ($ dbConn ))));
460- }
461-
462- // Grab the latest id for the schedule
463- $ schedId = mysql_insert_id ();
464-
465- // Optionally process the svg for the schedule
466- $ image = false ;
467- if (!empty ($ _POST ['svg ' ]) && renderSvg (html_entity_decode ($ _POST ['svg ' ]), $ schedId )) {
468- $ query = "UPDATE schedules SET image = ((1)) WHERE id = ' {$ schedId }' " ;
469- mysql_query ($ query ); // We don't particularly care if this fails
470- }
471377
472- // Now iterate through the schedule
473- foreach ($ json ['schedule ' ] as $ item ) {
474- // Process it into schedulenoncourses if the item is a non-course item
475- if ($ item ['courseNum ' ] == "non " ) {
476- // Process each time as a seperate item
477- foreach ($ item ['times ' ] as $ time ) {
478- $ query = "INSERT INTO schedulenoncourses (title, day, start, end, schedule) " .
479- " VALUES(' {$ item ['title ' ]}', ' {$ time ['day ' ]}', ' {$ time ['start ' ]}', ' {$ time ['end ' ]}', ' {$ schedId }') " ;
480- $ result = mysql_query ($ query );
481- if (!$ result ) {
482- die (json_encode (array ("error " => "mysql " , "msg " => "Storing non-course item ' {$ item ['title ' ]}' failed: " . mysql_error ($ dbConn ))));
483- }
484- }
485- } else {
486- // Process each course. It's crazy simple now.
487- $ query = "INSERT INTO schedulecourses (schedule, section) " .
488- " VALUES(' {$ schedId }', ' {$ item ['id ' ]}') " ;
489- $ result = mysql_query ($ query );
490- if (!$ result ) {
491- die (json_encode (array ("error " => "mysql " , "msg " => "Storing a course ' {$ item ['courseNum ' ]}' failed: " . mysql_error ($ dbConn ))));
492- }
493- }
494- }
495-
496- // Everything was successful, return a nice, simple URL to the schedule
497- // To make it cool, let's make it a hex id
498- $ hexId = dechex ($ schedId );
499- $ url = "{$ HTTPROOTADDRESS }schedule/ {$ hexId }" ;
500-
501- echo json_encode (array ("url " => $ url , "id " => $ hexId ));
502-
503- break ;
504378
505379 ////////////////////////////////////////////////////////////////////////
506380 // DEFAULT ACTION
0 commit comments