File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Debtors/DebtorService.php
<?php
namespace KommaApp\Debtors;
use Illuminate\Validation\Validator;
use KommaApp\Debtors\Models\Debtor;
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
class DebtorService
{
/**
* This will validate the search form
*
* @param $input
* @return bool
*/
public function validateSearchForm($input)
{
$rules = ['search_field' => 'required|min:4'];
//Only add the checkRobotResponse rule if robotShow is true
if (\Session::get('robot.show')) {
$rules['g-recaptcha-response'] = 'checkRobotResponse';
}
$validation = \Validator::make($input, $rules);
if ($validation->fails()) return $validation->messages();
return false;
}
/**
* Validate the searchTerm on a list of excluded terms
* @param $searchTerm
* @return bool
*/
public function validateSearchTerm($searchTerm)
{
$excludedTerms = \Config::get('equichecker.excludedTerms');
if (in_array(strtolower(strtoupper(trim($searchTerm))), $excludedTerms)) return false;
return true;
}
public function search($input)
{
$this->showHideRobotNextSearch();
//Save the input to the session
\Session::set('search.term', $input);
//Remove spaces between numbers
$input = preg_replace('/(?<=\d) +(?=\d)/', '', $input);
//split the input in parts on space
$parts = explode(' ', $input);
//Start of the query
$debtors = Debtor::select('debtors.*');
//loop trough the parts
foreach ($parts as $part) {
//Per part we are going to create an AND group, so all the parts are needed
$debtors->where(function ($query) use ($part) {
//Search the text fields
$this->searchTextFields($query, $part);
//search the phone fields
$this->searchPhoneFields($query, $part);
});
}
//get the result
return $debtors->get();
}
/**
* Search in the text fields
*
*
* @param $debtors
* @param $text
* @return mixed
*/
private function searchTextFields(&$debtors, $text)
{
//Clean the text from special characters
$text = $this->cleanText($text);
//If it is an empty string stop and return the debtors
if ($text == '') return $debtors;
//Add % for wider search
$text = '%' . $text . '%';
//Search in the text based fields
$debtors = $debtors->orWhere('company_name_search', 'LIKE', $text);
$debtors = $debtors->orWhere('last_name_search', 'LIKE', $text);
$debtors = $debtors->orWhere('private_street_search', 'LIKE', $text);
$debtors = $debtors->orWhere('office_street_search', 'LIKE', $text);
$debtors = $debtors->orWhere('post_street_search', 'LIKE', $text);
}
/**
* Search in the phone number fields
*
* @param $debtors
* @param $text
* @return mixed
*/
private function searchPhoneFields(&$debtors, $text)
{
//Prepare the phone number
$number = $this->prepareTelephoneNumber($text);
//If it is an empty string stop and return the debtors
if ($number == '') return $debtors;
//Search in the phone_search fields
$debtors = $debtors->orWhere('office_phone_search', '=', $number);
$debtors = $debtors->orWhere('company_phone_search', '=', $number);
$debtors = $debtors->orWhere('private_phone_search', '=', $number);
$debtors = $debtors->orWhere('mobile_search', '=', $number);
}
/**
* This method prepares the telephone number
* It will clip the international part of
* and remove any non numeral characters
*
* @param $number
* @return mixed
*/
public function prepareTelephoneNumber($number)
{
//Clip international part of the number
$number = preg_replace('/(^(\+|[0]{2})[0-9]{2})/', '0', $number);
//Strip all non numeral characters
$number = preg_replace('/[^0-9.]+/', '', $number);
return $number;
}
/**
* This method cleans the text to a clean string
* et. It will remove the accents
*
* @param $string
* @return mixed
*/
public function cleanText($string)
{
//Manually change æ to ae
$string = preg_replace("/æ/i", "ae", $string);
//Manually change œ to oe
$string = preg_replace("/œ/i", "oe", $string);
//Manually change ß to ss
$string = preg_replace("/ß/i", "ss", $string);
//Manually change ü to ue
$string = preg_replace("/ü/i", "ue", $string);
//Convert the special charachters to theire htmlemtities, amd then strip all but the first letter
$string = preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($string));
return $string;
}
/**
* Get a Debtor by the hashedDebtorCode
* this is a wrapper for getDebtorBy
*
* @param $hash
* @return Debtor $debtor
*/
public function getDebtorByHash($hash)
{
if (!$debtor = $this->getDebtorBy('hashed_debtors_code', $hash)) return false;
return $debtor;
}
/**
* Get a Debtor by the DebtorCode
* this is a wrapper for getDebtorBy
*
* @param $code
* @return null
*/
public function getDebtorByCode($code)
{
if (!$debtor = $this->getDebtorBy('debtors_code', $code)) return false;
return $debtor;
}
/**
* Get a debtor by a given field, value
* This is a wrapper for getDebtorsBy
*
* @param $field
* @param $value
* @return bool
*/
public function getDebtorBy($field, $value)
{
if (!$debtors = $this->getDebtorsBy($field, $value, 1)) return false;
return $debtors->first();
}
public function getDebtorById($id){
return Debtor::withTrashed()->where('id', $id)->first();
}
/**
* Get a debtor by a given field, value
*
* @param $field
* @param $value
* @param bool $limit
* @return bool
*/
public function getDebtorsBy($field, $value, $limit = false)
{
$debtors = Debtor::where($field, '=', $value);
if ($limit) $debtors->limit($limit);
$debtors = $debtors->get();
if ($debtors->count() == 0) return false;
return $debtors;
}
/**
* This makes the given value unique,
* It will check if the field/value exist
* with excluding the current id
* If it exist, it will add -1 and test again
*
* @param $field
* @param $value
* @param null $exclude_id
* @return mixed
*/
public function makeUnique($field, $value, $exclude_id = null)
{
$debtors = Debtor::where($field, '=', $value);
if ($exclude_id != null) $debtors->where('id', '!=', $exclude_id);
$debtors = $debtors->get();
if ($debtors->count() == 0) return $value;
return $this->checkUnique($field, $value . '-1', $exclude_id);
}
/**
* This method will show/hide the robot for the next search
*
*/
private function showHideRobotNextSearch()
{
//Get the searchCuont or set this to 0
$searchCount = \Session::get('robot.searchCount', 0);
//Add 1 to the count
$searchCount++;
//If the count is divisable by the robotLessSearches
if ($searchCount % \Config::get('equichecker.robotLessSearches') == 0) {
//We will show the robot again
\Session::put('robot.show', true);
} else {
//Else hide the robot
\Session::forget('robot.show', true);
}
//Enlarge the searchCount
\Session::put('robot.searchCount', $searchCount);
}
/**
* Enable the robot for the very first time
*/
public function showRobotOnFirstVisit()
{
if (\Session::get('robot.searchCount', 0) == 0) {
\Session::put('robot.show', true);
}
}
}