File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Companies/CompanyService.php
<?php
namespace App\KommaApp\Companies;
use App\KommaApp\Base\Service;
use App\KommaApp\Companies\Models\Company;
use App\KommaApp\Companies\Models\CompanyTranslation;
use App\Mail\CompanyRegisteredMail;
use App\Mail\CompanyRegisteredThanksMail;
use Illuminate\Database\Eloquent\Collection;
class CompanyService extends Service
{
/**
* Get the companies with the right filled relations
*
* @return Company[]|\Illuminate\Database\Eloquent\Builder[]|Collection
*/
public function getAllCompanies()
{
$companies = Company::with('images', 'region', 'companyCategory', 'companyCategory.translation')
->has('region')
->has('companyCategory')
->where('active', 1)
->orderBy('name')
->get();
return $companies;
}
/**
* Filter the unique categories from the company collection
*
* @param Collection $companies
* @return \Illuminate\Support\Collection| array
*/
public function getUsedCategories(Collection $companies)
{
// $categories = collect();
//
// // Filter the unique companyCategories and append them
// $uniqueCategoriesCompanies = $companies->unique('companyCategory');
// foreach ($uniqueCategoriesCompanies as $company) $categories->push($company->companyCategory);
$categories = [];
$uniqueCategoriesCompanies = $companies->unique('companyCategory');
foreach ($uniqueCategoriesCompanies as $company) $categories[$company->companyCategory->translation->slug] = (object)['value' => $company->companyCategory->id , 'name' => $company->companyCategory->translation->name];
ksort($categories);
$categories = array_merge([(object)['value' => 0, 'name' => __('site/companies.allCompanies')]], $categories);
return $categories;
}
/**
* Filter the unique regions from the company collection
*
* @param Collection $companies
* @return \Illuminate\Support\Collection| array
*/
public function getUsedRegions(Collection $companies)
{
// $regions = collect();
//
// // Filter the unique companyCategories and append them
// $uniqueRegionsCategories = $companies->unique('region');
// foreach ($uniqueRegionsCategories as $company) $regions->push($company->companyCategory);
$regions = [];
$uniqueRegionsCategories = $companies->unique('region');
foreach ($uniqueRegionsCategories as $company) $regions[str_slug($company->region->name)] = (object)['value' => $company->region->id , 'name' => $company->region->name] ;
ksort($regions);
$regions = array_merge([(object)['value' => 0, 'name' => __('site/companies.allRegions')]], $regions);
return $regions;
}
public function createCompany(array $formValues)
{
// Check if the invoice information is filled else populate it
if(!isset($formValues['other_invoice_address'])) $this->populateInvoiceInformation($formValues);
// Edit the postal to the desired format
$formValues['postal'] = strtoupper( preg_replace('/\s+/','', $formValues['postal']) );
// If site doesn't start with http or https add 'http to it
if(!starts_with($formValues['site_url'], ['http://', 'https://'])) $formValues['site_url'] = 'http://' . $formValues['site_url'];
$company = new Company($formValues);
$company->site_id = $this->site->id;
$company->active = 1;
$company->slug = $this->createOrGetUniqueSlug($company, $company->name);
$company->save();
// Remove from search database because it needs to be verified first
$company->unsearchable();
// Just make the translation already, better safe then sorry
$company->translation()->create(['language_id' => \App::getLanguage()->id]);
return $company;
}
/**
* Populate the invoice information from the company information
*
* @param $formValues
*/
private function populateInvoiceInformation(&$formValues)
{
$fieldsToPopulate = ['email','phone','address','postal','city'];
foreach ($fieldsToPopulate as $field) $formValues['invoice_'.$field] = $formValues[$field];
}
/**
* Update the company upon the given form values
*
* @param Company $company
* @param array $formValues
*/
public function updateCompany(Company $company, array $formValues)
{
// Pull this value from the array because that is in the translation instead of the company model
$companyDescription = array_pull($formValues,'translationDescription');
// Create and slug value from the form value name
$formValues['slug'] = $this->createOrGetUniqueSlug($company, $formValues['name']);
// Edit the postal to the desired format
$formValues['postal'] = strtoupper( preg_replace('/\s+/','', $formValues['postal']) );
// If site doesn't start with http or https add 'http to it
if(!starts_with($formValues['site_url'], ['http://', 'https://'])) $formValues['site_url'] = 'http://' . $formValues['site_url'];
// Update the company and it's translation
$company->update($formValues);
if(isset($company->translation)){
$company->translation->description = $companyDescription;
$company->translation->save();
}
else{
$companyTranslation = new CompanyTranslation();
$companyTranslation->language_id = 104;
$companyTranslation->description = $companyDescription;
$company->translations()->save($companyTranslation);
}
}
/**
* Update the company upon the given form values
*
* @param Company $company
* @param array $formValues
*/
public function updateCompanyInvoice(Company $company, array $formValues)
{
// Update the company and it's translation
$company->update($formValues);
}
/**
* Bind a user to the company
*
* @param Company $company
* @param $userId
*/
public function linkToCompanyByUserId(Company $company, $userId)
{
$company->users()->attach($userId);
}
/**
* Send a confirm mail to the registered user
* and a notification mail to Erik
*
* @param Company $company
*/
public function sendRegisteredMails(Company $company)
{
$user = $company->users()->get()->first();
// Mail to Erik
\Mail::send( new CompanyRegisteredMail($company, $user) );
// Mail to (fresh registered) user
\Mail::send( new CompanyRegisteredThanksMail($company, $user) );
}
/**
* Generate an unique slug for a company
*
* @param Company $company
* @param string $name
* @param int $uniquifier
* @return string
*/
public function createOrGetUniqueSlug(Company $company, string $name, int $uniquifier = 0): string
{
if($uniquifier == 0) $slug = str_slug($name);
else $slug = str_slug($name) . '-' . $uniquifier;
$results = Company::where('slug', $slug);
if(isset($company->id)) $results = $results->where('id', '!=', $company->id);
$results = $results->get();
if($results->count() == 0) return $slug;
else{
$slug = $this->createOrGetUniqueSlug($company, $name, ($uniquifier + 1));
return $slug;
}
}
public function getCompaniesByEmailPart(array $emailParts)
{
$companyQuery = Company::where('active', 1)
->select('slug', 'email', 'site_url');
foreach ($emailParts as $key => $emailPart)
{
// Add query to check if ending is the mail list
if($key === 0) $companyQuery->where('email', 'LIKE', '%' . $emailPart);
else $companyQuery->orWhere('email', 'LIKE', '%' . $emailPart);
// And query if ending is in site_url
$companyQuery->orWhere('site_url', 'LIKE', '%' . $emailPart);
}
return $companyQuery->get();
}
}