Skip to content

Commit

Permalink
Add search field on the main page #62
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickakk committed Apr 19, 2023
1 parent 7ee9694 commit 461f91a
Show file tree
Hide file tree
Showing 8 changed files with 2,635 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ upgrade
*.cache

webroot/js/idp_select/DiscoFeed\.json
webroot/search_list.json
4 changes: 1 addition & 3 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
## Version 2023-04

### New feature
- x
- Search for a course by name or institution directly (searchbar / typahead) #62

### Small changes
- Removed redundant social media links and usage of external file resource content.js #68
- Introduce generic email address notation and avoid direct mailto links #67

### Bug fixes
- z

## Version 2023-03

Expand Down
53 changes: 53 additions & 0 deletions src/Command/GenSearchListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\I18n\FrozenTime;

/* This file should be run on build to create the JSON file for the seach bar
As well it should be run automatically on regular basis */

class GenSearchListCommand extends Command
{
public function execute(Arguments $args, ConsoleIo $io)
{
$this->loadModel('DhcrCore.Courses');
$this->loadModel('Logentries');

$io->out('~~~ Started: Generate Search List ~~~');

$courses = $this->Courses->find('all', [
'contain' => ['Institutions'],
'order' => ['Courses.name' => 'asc']
])->where(
[
'active' => 1,
'deleted' => 0,
'Courses.updated >' => new FrozenTime('-489 Days'),
'approved' => 1
]
);
$searchList = [];
foreach ($courses as $course) {
$searchList[] = trim($course->name) . ' - ' . trim($course->institution->name);
}

$file = fopen('webroot/search_list.json', 'w');
fwrite($file, json_encode($searchList));
fclose($file);

$this->Logentries->createLogEntry(
'10',
'586',
basename(__FILE__, '.php'),
'Generated Search List.',
'Total Courses: ' . sizeof($searchList)
);

$io->out('Total courses: ' . sizeof($searchList));
$io->out('~~~ Finished: Generate Search List ~~~');
}
}
10 changes: 10 additions & 0 deletions src/Command/gen_search_list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

### ENV ###

CAKE_PATH='/app'
export PATH="/app/.heroku/php/bin:${PATH}"

### CAKE COMMANDS ###

bin/cake gen_search_list 2>&1
30 changes: 28 additions & 2 deletions src/Controller/CoursesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ class CoursesController extends AppController
public $Courses = null;
public const SKIP_AUTHORIZATION = [
'index',
'view'
'view',
'search'
];

public function initialize(): void
{
parent::initialize();
$this->Authentication->allowUnauthenticated(['index', 'view']);
$this->Authentication->allowUnauthenticated(['index', 'view', 'search']);
if (in_array($this->request->getParam('action'), self::SKIP_AUTHORIZATION)) {
$this->Authorization->skipAuthorization();
}
Expand Down Expand Up @@ -110,6 +111,31 @@ public function view($id = null)
$this->render('index');
}

public function search($courseName, $institutionName)
{
$this->loadModel('DhcrCore.Courses');
$institutionName = urldecode(($institutionName));
$institutionId = $this->Courses->Institutions->find()->where(['name' => $institutionName])->first()->id;
$courseName = urldecode($courseName);
$courses = $this->Courses->find()->where([
'name' => $courseName,
'institution_id' => $institutionId
]);
if ($courses->count() < 1) {
// set flash message
// redirect to index
echo ('No results found. Please select a course from the list.');
die();
} elseif ($courses->count() > 1) {
// set flash message
// redirect to index
echo ('Too much results found. Please report this as a bug.');
die();
}
$courseId = $courses->first()->id;
return $this->redirect(['controller' => 'Courses', 'action' => 'view', $courseId]);
}

public function add()
{
$this->loadModel('DhcrCore.Courses');
Expand Down
66 changes: 65 additions & 1 deletion templates/layout/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,72 @@
<div id="container">
<?= $this->fetch('content') ?>
</div>
<?= $this->Flash->render('flash') ?>
<?= $this->Html->script('jquery-3.4.1.min.js') ?>
<?= $this->Html->script('typeahead.bundle.js') ?>
<style>
.bs-example {
font-family: sans-serif;
position: relative;
margin: 100px;
}

.typeahead,
.tt-query,
.tt-hint {
border: 2px solid #CCCCCC;
border-radius: 8px;
font-size: 14px;
/* Set input font size */
height: 30px;
line-height: 30px;
outline: medium none;
padding: 8px 12px;
width: 422px;
}

.typeahead {
background-color: #FFFFFF;
}

.typeahead:focus {
border: 2px solid #0097CF;
}

.tt-query {
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
}

.tt-hint {
color: #999999;
}

.tt-menu {
background-color: #FFFFFF;
border: 1px solid rgba(0, 0, 0, 0.2);
border-radius: 8px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
margin-top: 12px;
padding: 8px 0;
width: 422px;
}

.tt-suggestion {
font-size: 14px;
/* Set suggestion dropdown font size */
padding: 3px 20px;
}

.tt-suggestion:hover {
cursor: pointer;
background-color: #0097CF;
color: #FFFFFF;
}

.tt-suggestion p {
margin: 0;
}
</style>
<?= $this->Flash->render('flash') ?>
<?= $this->Html->script('/leaflet/leaflet') ?>
<?= $this->Html->script('/leaflet/leaflet.markercluster') ?>
<?= $this->Html->script([
Expand Down
Loading

0 comments on commit 461f91a

Please sign in to comment.