src/Controller/JavascriptController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Document\ClassSchedule;
  4. use App\Exception\NotFoundHttpException;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Response;
  7. /**
  8.  * Javascript Controller
  9.  *
  10.  * Dynamically create javascript
  11.  *
  12.  * @Route("/dynamic-assets")
  13.  * @author "Cornell University, Student Services IT"
  14.  */
  15. class JavascriptController extends AbstractController
  16. {
  17.     /**
  18.      * Creates global RosterSharedConfig variables
  19.      * Used for configuration values across Class Roster
  20.      *
  21.      * Used for roster.config Angular Module enables .RosterConfig constant
  22.      *
  23.      * Single cached response for each roster
  24.      *
  25.      * @Route("/{rosterSlug}/RosterSharedConfig.js", name="app_js_roster_shared_config", methods={"GET"})
  26.      * @return \Symfony\Component\HttpFoundation\Response
  27.      */
  28.     public function rosterSharedConfigAction($rosterSlug)
  29.     {
  30.         $roster $this->getSystemSettings()->getRosterBySlug($rosterSlug);
  31.         if (!$roster) {
  32.             throw new NotFoundHttpException('Roster Not Found');
  33.         }
  34.         $response = new Response();
  35.         if ($this->isGatewayCacheEnabled()) {
  36.             $response->setPublic();
  37.             $response->setMaxAge($this->getGatewayCacheLifetime());
  38.             $response->setSharedMaxAge($this->getGatewayCacheLifetime());
  39.         }
  40.         $response->headers->set('Content-Type''text/javascript');
  41.         // color palettes
  42.         $colorChoices = [];
  43.         $colorPaletteData $this->getConnection()->fetchAll('SELECT DISTINCT palette FROM colorchoice ORDER BY palette');
  44.         $colorPalettes = [];
  45.         foreach ($colorPaletteData AS $colorPalette) {
  46.             $colors $this->getConnection()->fetchAll('SELECT * FROM colorchoice WHERE palette=? ORDER BY title', [$colorPalette['palette']]);
  47.             $colorPaletteChoices = [];
  48.             foreach ($colors AS $color) {
  49.                 $colorPaletteChoices[] = [
  50.                     'code' => $color['code_primary'],
  51.                     'meta' => ['secondary' => $color['code_secondary'], 'title' => $color['title']]
  52.                 ];
  53.             }
  54.             $colorChoices[$colorPalette['palette']][] = $colorPaletteChoices;
  55.             $colorPalettes[] = $colorPalette['palette'];
  56.         }
  57.         // random events
  58.         $randomEvents $this->getConnection()->fetchAll('SELECT title, location FROM randomevent');
  59.         // do not re-poll service if within this increment
  60.         $rosterSchedulerConfig $this->getParameter('roster_scheduler');
  61.         $ttlMinFav $rosterSchedulerConfig['ttl_min_fav'];
  62.         // wait at least # seconds before saving to throttle repeated changes
  63.         $ttlMinSave $rosterSchedulerConfig['ttl_min_save'];
  64.         // min Ttl before considering course-detail data stale, should be at least as long
  65.         // as CapLoader refresh sequence
  66.         $ttlMinCourseDetail $rosterSchedulerConfig['ttl_min_course_detail'];
  67.         $availRosters = [];
  68.         $allAvailableRosters $this->getSystemSettings()->getAvailableRosters();
  69.         foreach ($allAvailableRosters as $availableRoster) {
  70.             $availRosters[$availableRoster->getStrm()] = $availableRoster;
  71.         }
  72.         $termSetup $this->getSystemSettings()->getTermSetup($roster);
  73.         $navRosters array_values($this->getSystemSettings()->getDropdownRosters()->toArray());
  74.         // terms - used by termTranslate filter in Syllabi for Enrollment display
  75.         $minStrm 2363;
  76.         $academicTerms $this->getDocumentManager()->createQueryBuilder('AppBundle:Term')->field('strm')->gte($minStrm)->getQuery()->execute();
  77.         return $this->render('Javascript/rosterSharedConfig.js.twig', ['colorPalettes' => json_encode($colorPalettes),
  78.             'colorChoices' => json_encode($colorChoices),
  79.             'randomEvents' => json_encode($randomEvents),
  80.             'ttlMinFav' => $ttlMinFav,
  81.             'ttlMinSave' => $ttlMinSave,
  82.             'ttlMinCourseDetail' => $ttlMinCourseDetail,
  83.             'availRosters' => json_encode($availRosters),
  84.             'navRosters' => json_encode($navRosters),
  85.             'termSetup' => json_encode($termSetup),
  86.             'academicTerms' => $academicTerms,
  87.             'roster' => $roster,
  88.         ], $response);
  89.     }
  90.     /**
  91.      * Support Scheduler's course search type-ahead. All searches previously server side.
  92.      *
  93.      * @Route("/{rosterSlug}/CourseTypeahead.js", name="app_js_roster_course_typeahead", methods={"GET"})
  94.      * @return \Symfony\Component\HttpFoundation\Response
  95.      */
  96.     public function courseTypeaheadAction($rosterSlug)
  97.     {
  98.         $roster $this->getSystemSettings()->getRosterBySlug($rosterSlug);
  99.         if (!$roster) {
  100.             throw new NotFoundHttpException('Roster Not Found');
  101.         }
  102.         $response = new Response();
  103.         if ($this->isGatewayCacheEnabled()) {
  104.             $response->setPublic();
  105.             $response->setMaxAge($this->getGatewayCacheLifetime());
  106.             $response->setSharedMaxAge($this->getGatewayCacheLifetime());
  107.         }
  108.         $response->headers->set('Content-Type''text/javascript');
  109.         $courses = [];
  110.         $qb $this->getDocumentManager()->createQueryBuilder('App\Document\ClassSchedule');
  111.         // assigning results back to $qb allows us to modify results before returning
  112.         $qb $qb
  113.             ->select('crseId''crseOfferNbr''subject''catalogNbr''titleShort''titleLong')
  114.             ->field('versionId')->equals($roster->getVersion()->getId())
  115.             ->sort('subject''asc')
  116.             ->sort('catalogNbr''asc')
  117.             ->getQuery();
  118.         $cursor $qb->execute();
  119.         /** @var ClassSchedule $classSchedule */
  120.         foreach ($cursor as $classSchedule) {
  121.             $courses[] = ['i' => $classSchedule->getCrseId(),
  122.                 'o' => $classSchedule->getCrseOfferNbr(),
  123.                 's' => $classSchedule->getSubject(),
  124.                 'n' => $classSchedule->getCatalogNbr(),
  125.                 'ts' => $classSchedule->getTitleShort()?$classSchedule->getTitleShort():'',
  126.                 'tl' => $classSchedule->getTitleLong()?$classSchedule->getTitleLong():'',
  127.             ];
  128.         }
  129.         return $this->render('Javascript/courseTypeahead.js.twig', ['courses' => json_encode($courses),
  130.             'roster' => $roster
  131.         ], $response);
  132.     }
  133. }