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 " :
@@ -328,22 +287,6 @@ function renderSvg($svg, $id) {
328287
329288 break ;
330289
331- ////////////////////////////////////////////////////////////////////////
332- // GET TIME DROPDOWNS
333- case "getTimeField " :
334- // Verify that we have the field name and the default time
335- if (empty ($ _POST ['name ' ])) {
336- die (json_encode (array ("error " =>"argument " , "msg " =>"A field name must be provided " , "arg " =>"name " )));
337- }
338- if (empty ($ _POST ['default ' ])) {
339- die (json_encode (array ("error " =>"argument " , "msg " =>"A default time must be provided " , "arg " =>"default " )));
340- }
341-
342- // Return the code
343- echo json_encode (array ("code " => getTimeField ($ _POST ['name ' ], $ _POST ['default ' ])));
344-
345- break ;
346-
347290 ////////////////////////////////////////////////////////////////////////
348291 // GET MATCHING SCHEDULES
349292 case "getMatchingSchedules " :
@@ -434,76 +377,7 @@ function renderSvg($svg, $id) {
434377
435378 break ;
436379
437- ////////////////////////////////////////////////////////////////////////
438- // STORE A SCHEDULE
439- case "saveSchedule " :
440- // There has to be a json object given
441- if (empty ($ _POST ['data ' ])) {
442- die (json_encode (array ("error " => "argument " , "msg " => "No schedule was provided " , "arg " => "schedule " )));
443- }
444- $ _POST ['data ' ] = html_entity_decode ($ _POST ['data ' ], ENT_QUOTES );
445- $ json = stripslashes ($ _POST ['data ' ]);
446-
447- // Make sure the object was successfully decoded
448- $ json = json_decode ($ json , true );
449- if ($ json == null ) {
450- die (json_encode (array ("error " => "argument " , "msg " => "The schedule could not be decoded " , "arg " => "schedule " )));
451- }
452- if (!isset ($ json ['starttime ' ]) || !isset ($ json ['endtime ' ]) || !isset ($ json ['building ' ]) || !isset ($ json ['startday ' ]) || !isset ($ json ['endday ' ])) {
453- die (json_encode (array ("error " => "argument " , "msg " => "A required schedule parameter was not provided " )));
454- }
455-
456- // Start the storing process with storing the data about the schedule
457- $ query = "INSERT INTO schedules (oldid, startday, endday, starttime, endtime, building, quarter) " .
458- " VALUES('', ' {$ json ['startday ' ]}', ' {$ json ['endday ' ]}', ' {$ json ['starttime ' ]}', ' {$ json ['endtime ' ]}', ' {$ json ['building ' ]}', " .
459- " ' {$ json ['term ' ]}') " ;
460- $ result = mysql_query ($ query );
461- if (!$ result ) {
462- die (json_encode (array ("error " => "mysql " , "msg " => "Failed to store the schedule: " . mysql_error ($ dbConn ))));
463- }
464-
465- // Grab the latest id for the schedule
466- $ schedId = mysql_insert_id ();
467-
468- // Optionally process the svg for the schedule
469- $ image = false ;
470- if (!empty ($ _POST ['svg ' ]) && renderSvg (html_entity_decode ($ _POST ['svg ' ]), $ schedId )) {
471- $ query = "UPDATE schedules SET image = ((1)) WHERE id = ' {$ schedId }' " ;
472- mysql_query ($ query ); // We don't particularly care if this fails
473- }
474380
475- // Now iterate through the schedule
476- foreach ($ json ['schedule ' ] as $ item ) {
477- // Process it into schedulenoncourses if the item is a non-course item
478- if ($ item ['courseNum ' ] == "non " ) {
479- // Process each time as a seperate item
480- foreach ($ item ['times ' ] as $ time ) {
481- $ query = "INSERT INTO schedulenoncourses (title, day, start, end, schedule) " .
482- " VALUES(' {$ item ['title ' ]}', ' {$ time ['day ' ]}', ' {$ time ['start ' ]}', ' {$ time ['end ' ]}', ' {$ schedId }') " ;
483- $ result = mysql_query ($ query );
484- if (!$ result ) {
485- die (json_encode (array ("error " => "mysql " , "msg " => "Storing non-course item ' {$ item ['title ' ]}' failed: " . mysql_error ($ dbConn ))));
486- }
487- }
488- } else {
489- // Process each course. It's crazy simple now.
490- $ query = "INSERT INTO schedulecourses (schedule, section) " .
491- " VALUES(' {$ schedId }', ' {$ item ['id ' ]}') " ;
492- $ result = mysql_query ($ query );
493- if (!$ result ) {
494- die (json_encode (array ("error " => "mysql " , "msg " => "Storing a course ' {$ item ['courseNum ' ]}' failed: " . mysql_error ($ dbConn ))));
495- }
496- }
497- }
498-
499- // Everything was successful, return a nice, simple URL to the schedule
500- // To make it cool, let's make it a hex id
501- $ hexId = dechex ($ schedId );
502- $ url = "{$ HTTPROOTADDRESS }schedule/ {$ hexId }" ;
503-
504- echo json_encode (array ("url " => $ url , "id " => $ hexId ));
505-
506- break ;
507381
508382 ////////////////////////////////////////////////////////////////////////
509383 // DEFAULT ACTION
0 commit comments