Skip to content
Snippets Groups Projects
Commit ec8e6194 authored by Brian Weaver's avatar Brian Weaver
Browse files

courses: forgot to commit search form...

parent a5a16644
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\asc_courses\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\TempStore\PrivateTempStore;
// // Other classes I thought I might use...
// use Drupal\Core\Session\AccountInterface;
// use Drupal\Core\Session\SessionManagerInterface;
// use Drupal\user\PrivateTempStoreFactory;
// use \Drupal\asc_courses\AscCoursesApi;
// use \Drupal\asc_courses\AscCoursesImporter;
/**
* Form for searching and displaying course records from `asc_courses_processed` table
*/
class CourseSearchForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'asc_courses_search';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Submit buttons
$form['actions'] = [
"#type" => "actions",
'submit' => [
'#type' => 'submit',
'#value' => "Search",
'#name' => "search_button",
'#button_type' => "primary",
],
'clear' => [
'#type' => 'submit',
'#value' => "Clear",
'#name' => 'clear_button',
]
];
// Load saved form values, if any
$tempstore = \Drupal::service('tempstore.private');
$store = $tempstore->get('asc_courses_collection');
$subject = $store->get('search_subject');
$catalog_nbr = $store->get('search_catalog_nbr');
$title = $store->get('search_title');
$form['subject'] = array(
'#type' => 'textfield',
'#title' => $this->t('Subject Abbreviation'),
'#default_value' => $subject,
// '#description' => $this->t('desc'),
'#weight' => 1,
);
$form['catalog_nbr'] = array(
'#type' => 'textfield',
'#title' => $this->t('Catalog Number'),
'#default_value' => $catalog_nbr,
// '#description' => $this->t('desc'),
'#weight' => 20,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#default_value' => $title,
// '#description' => $this->t('desc'),
'#weight' => 30,
);
// Table of search results, if search terms were provided
if(!empty($subject) || !empty($catalog_nbr) || !empty($title)) {
// table header
$header_table = array(
'crse_id' => t('Course ID'),
'subject' => t('Subject Abbreviation'),
'catalog_nbr' => t('Catalog Number'),
'course_title_long' => t('Course Title'),
// 'opt' => t('operations'),
// 'opt1' => t('operations'),
);
// select records from db
$query = \Drupal::database()->select('asc_courses_processed', 'acp');
$query->fields('acp', ['crse_id','subject','catalog_nbr','course_title_long']);
if(!empty($subject)) {
$query->condition('subject', '%'.$subject.'%', 'LIKE');
}
if(!empty($catalog_nbr)) {
$query->condition('catalog_nbr', '%'.$catalog_nbr.'%', 'LIKE');
}
if(!empty($title)) {
$query->condition('course_title_long', '%'.$title.'%', 'LIKE');
}
$query->orderBy('acp.subject');
$query->orderBy('acp.catalog_nbr');
$results = $query->execute()->fetchAll();
// build table rows with results
$rows=array();
foreach($results as $data) {
// $delete = Url::fromUserInput('/admin/config/content/asc-courses/alacarte/delete/'.$data->id);
// $edit = Url::fromUserInput('/admin/config/content/asc-courses/alacarte?num='.$data->id);
$rows[] = array(
'crse_id' =>$data->crse_id,
'subject' => $data->subject,
'catalog_nbr' => $data->catalog_nbr,
'course_title_long' => $data->course_title_long,
// \Drupal::l('Delete', $delete),
// \Drupal::l('Edit', $edit),
);
}
// add table to form
$form['table'] = [
'#type' => 'table',
'#header' => $header_table,
'#rows' => $rows,
'#weight' => 100,
'#empty' => t('No courses found'),
];
}
else { // List of subject abbreviations
$table_header = array(
'subject_abbrev' => t('Subject Abbreviations'),
);
$query = \Drupal::database()->select('asc_courses_processed', 'acp');
$query->fields('acp', ['dept_org','subject']);
$query->orderBy('acp.subject');
$query->distinct();
$subject_results = $query->execute()->fetchAll();
foreach ($subject_results as $subject_result) {
$subject_table_rows[] = [
"subject_abbrev" => $subject_result->subject,
];
}
// add table to form
$form['table'] = [
'#type' => 'table',
'#header' => $table_header,
'#rows' => $subject_table_rows,
'#weight' => 100,
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// dsm('Course search form was validated');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$tempstore = \Drupal::service('tempstore.private');
$store = $tempstore->get('asc_courses_collection');
$trigger = $form_state->getTriggeringElement();
if($trigger['#name'] == 'clear_button') {
// Clear the search fields
$store->delete('search_subject');
$store->delete('search_catalog_nbr');
$store->delete('search_title');
}
else {
// Save form inputs, for reuse when rebuilding the form
$store->set('search_subject', $form_state->getValue('subject'));
$store->set('search_catalog_nbr', $form_state->getValue('catalog_nbr'));
$store->set('search_title', $form_state->getValue('title'));
}
// return parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'asc_courses.settings',
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment