JobSearcbPageController.php

 avatar
unknown
php
2 days ago
5.6 kB
7
Indexable
<?php

use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\HTTP;
use SilverStripe\Control\Controller;

#use JobPage;

class JobSearchPageController extends PageController
{

    private static $allowed_actions = [
        'SearchJobsForm',
        'showJobsForm',
    ];


    public function SearchJobsForm(): Form
    {

	    $locations = [];
		$locations =  array('On-site only' => 'On-site only',
						'Remote option' => 'Remote option',
						'Temporary remote' => 'Temporary remote',
						'Hybrid' => 'Hybrid',
						'Remote-first' => 'Remote-first',
						'Any' => 'Any'
					);

		$types = [];
		$types =  array('Full-time' => 'Full-time',
					'Fresher' => 'Fresher',
					'Contract' => 'Contract',
					'Temporary' => 'Temporary',
					'Internship' => 'Internship',
					'Part-time' => 'Part-time',
					'Any' => 'Any'
				);

		$levels = [];
		$levels = array('Bachelor\'s degree' => 'Bachelor\'s degree',
					'Master\'s degree' => 'Master\'s degree',
					'Doctoral degree' => 'Doctoral degree',
					'Other' => 'Other',
					'Any' => 'Any'
				);

		$germanRequired = [];
		$germanRequired = array('Required' => 'Required','Not required' => 'Not required','Any' => 'Any');

		$fields = FieldList::create(
                    TextField::create('Title')
                        ->setAttribute('placeholder', 'e.g: Data scientist')
                        ->addExtraClass('form-control'),
                    TextField::create('StartDate','Earliest start date')
                        ->setAttribute('data-datepicker',true)
                        ->setAttribute('data-date-format','DD-MM-YYYY')
                        ->setAttribute('placeholder', 'e.g: 20.07.2025')
                        ->addExtraClass('form-control'),
                    DropdownField::create('Location','Job location type')
                        ->setSource($locations)
                        ->addExtraClass('form-control'),
                    DropdownField::create('Type','Job type')
                        ->setSource($types)
                        ->addExtraClass('form-control'),
                    DropdownField::create('Level','Education level required')
                        ->setSource($levels)
                        ->addExtraClass('form-control'),
                    TextField::create('Postal','Job Postal code')
                        ->setAttribute('placeholder', 'e.g: 10115')
                        ->addExtraClass('form-control'),
                    DropdownField::create('GermanRequired','German language required')
                        ->setSource($germanRequired)
                        ->addExtraClass('form-control')
        );

		$actions = FieldList::create(FormAction::create('showJobsForm'));

        $form = Form::create($this, 'SearchJobsForm', $fields, $actions);

        $form->setFormAction(
            Controller::join_links(
                Controller::curr()->Link(),
                'showJobsForm')
        );

        return $form;
    }


    public function showJobsForm(HTTPRequest $request)
    {
        $vars = $request->postVars();

        $title = $vars['Title'];
        $startDate = $vars['StartDate'];
        $location = $vars['Location'];
        $type = $vars['Type'];
        $level = $vars['Level'];
        $postal = $vars['Postal'];
        $germanRequired = $vars['GermanRequired'];

        $jobs = JobPage::get()->filter(['Active' => true,'Verified' => true])->sort('StartDate DESC');

        if (!empty($title)) {

			$jobs = $jobs->filter(array(
                'Title:PartialMatch' => $title
            ));
		}

		if (!empty($startDate)) {

            $startDateStamp = strtotime($startDate);
            $startDate2 = date('Y-m-d',$startDateStamp);
            $jobs = $jobs->filter([
                'StartDate:GreaterThanOrEqual' => $startDate2
            ]);
		}

		if (!empty($location)) {

			if ($location != 'Any') {
				$jobs = $jobs->filter('Location',$location);
			}
		}

		if (!empty($type)) {

			if ($type != 'Any') {
				$jobs = $jobs->filter('Type',$type);
			}
		}

		if (!empty($level)) {

			if ($level != 'Any') {
				$jobs = $jobs->filter('Level',$level);
			}
		}

		if (!empty($postal)) {
			$jobs = $jobs->filter(array('Postal:PartialMatch' => (int)$postal));
		}

		if (!empty($germanRequired)) {

			$gr = match($germanRequired){
					'Required' => true,
					'Not required' => false,
					'Any' => 'Any'
			};

			if ($gr != 'Any') {
				$jobs = $jobs->filter('GermanRequired',$gr);
            }
		}


		$paginatedJobs = PaginatedList::create($jobs,$request)
						->setPageLength(16)
						->setPaginationGetVar('s');

        #d($paginatedJobs);

        return [
            'Results' => $paginatedJobs,
        ];

        #$session = $request->getSession();

        ###########################################
    	$form = $this->SearchJobsForm();

		$form->loadDataFrom($request->postVars());

        return $this->redirect($this->Link());
        ###########################################

    }

}
Editor is loading...
Leave a Comment