Skip to content

Instantly share code, notes, and snippets.

@cygeorgel
Created February 12, 2020 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cygeorgel/7aa3dc8a0bdfb201349489992d2cc454 to your computer and use it in GitHub Desktop.
Save cygeorgel/7aa3dc8a0bdfb201349489992d2cc454 to your computer and use it in GitHub Desktop.
<?php
namespace App\BlueRock\Services;
class FileableIndexService extends FileableService
{
public function breakIndexes()
{
$fileable = $this->getFileable();
if ($fileable) {
if ($fileable->quickSearchIndex) { // otherwise loops
$fileable->quickSearchIndex = false;
$fileable->save();
$fileable->files()->update([
'quickSearchIndex' => false
]);
}
}
}
public function index()
{
$fileable = $this->getFileable();
$customerSuppliers = [];
$quickSearch = '';
$quickSearch .= implode(' ', numberVersions($fileable->phone)) . ' ';
$quickSearch .= implode(' ', numberVersions($fileable->mobilePhone)) . ' ';
$quickSearch .= $fileable->name . ' '
. $fileable->brand . ' '
. $fileable->mainContactLastName . ' '
. $fileable->customerAccount . ' '
. $fileable->emailAddress . ' '
. $fileable->mainAddressPostalCode . ' '
. $fileable->mainAddressCity;
$quickSearchLabel = $fileable->name . ' '
. $fileable->mainContactLastName . ' '
. $fileable->customerAccount . ' ##'
. $fileable->id;
foreach ($fileable->contacts as $contact) {
$quickSearch .= implode(' ', numberVersions($contact->mobilePhone));
$quickSearch .= implode(' ', numberVersions($contact->landlinePhone));
$quickSearch .= ' ' . $contact->lastName . ' ' . $contact->emailAddress . ' ';
}
foreach ($fileable->files as $file) {
$fileSuppliers = [];
if (empty($file->name)) {
$file->name = null;
$file->save();
}
if (empty ($file->site)) {
$file->site = null;
$file->save();
}
$quickSearchFile = '';
if ( ! empty ($file->name)) {
$quickSearch .= $file->name . ' ';
$quickSearchFile .= $file->name . ' ';
}
if ( ! empty ($file->site)) {
$quickSearch .= $file->site . ' ';
$quickSearchFile .= $file->site . ' ';
}
if ( ! empty ($file->postalCode)) {
$quickSearch .= $file->postalCode . ' ';
$quickSearchFile .= $file->postalCode . ' ';
}
if ( ! empty ($file->city)) {
$quickSearch .= $file->city . ' ';
$quickSearchFile .= $file->city . ' ';
}
if ( ! empty ($file->orderNumber)) {
$quickSearch .= $file->orderNumber . ' ';
$quickSearchFile .= $file->orderNumber . ' ';
}
if ( ! empty($file->parcelNumber)) {
$quickSearch .= $file->parcelNumber . ' ';
$quickSearchFile .= $file->parcelNumber . ' ';
}
if ( ! empty($file->phoneLine)) {
$quickSearch .= implode(' ', numberVersions($file->phoneLine)) . ' ';
$quickSearchFile .= implode(' ', numberVersions($file->phoneLine)) . ' ';
}
if ( ! empty($file->deviceIpAddress)) {
$quickSearch .= $file->deviceIpAddress . ' ';
$quickSearchFile .= $file->deviceIpAddress . ' ';
}
foreach ($file->telephonyLines as $line) {
$quickSearch .= implode(' ', numberVersions($line->phoneLine)) . ' ';
$quickSearchFile .= implode(' ', numberVersions($line->phoneLine)) . ' ';
}
foreach ($file->initial as $element) {
if ($element->item) {
if ($element->item->supplier) {
if ( ! in_array($element->item->supplier->name, $fileSuppliers)) {
array_push ($fileSuppliers, $element->item->supplier->name);
}
if ( ! in_array ($element->item->supplier->name, $customerSuppliers)) {
array_push($customerSuppliers, $element->item->supplier->name);
if ($this->getFileableType() == 'App\Customer') {
$count = \DB::table('customer_supplier')
->where('customer_id', $fileable->id)
->where('supplier_id', $element->item->supplier_id)
->count();
if ( ! $count ) {
$fileable->suppliers()->attach($element->item->supplier->id);
}
}
}
}
}
}
foreach ($file->licences as $element) {
if ($element->item->supplier) {
if ( ! in_array($element->item->supplier->name, $fileSuppliers)) {
array_push ($fileSuppliers, $element->item->supplier->name);
}
if ( ! in_array ($element->item->supplier->name, $customerSuppliers)) {
array_push($customerSuppliers, $element->item->supplier->name);
if ($this->getFileableType() == 'App\Customer') {
$count = \DB::table('customer_supplier')
->where('customer_id', $fileable->id)
->where('supplier_id', $element->item->supplier_id)
->count();
if ( ! $count ) {
$fileable->suppliers()->attach($element->item->supplier->id);
}
}
}
}
}
foreach ($file->recurring as $element) {
$indexes = ['ipV4', 'ipV6', 'login'];
foreach ($indexes as $index) {
if ($element->$index) {
$quickSearch .= ' ' . $element->$index . ' ';
$quickSearchFile .= ' ' . $element->$index . ' ';
}
}
if ($element->item) {
if ($element->item->supplier) {
if ( ! in_array($element->item->supplier->name, $fileSuppliers)) {
array_push ($fileSuppliers, $element->item->supplier->name);
}
if ( ! in_array ($element->item->supplier->name, $customerSuppliers)) {
array_push( $customerSuppliers, $element->item->supplier->name);
if ($this->getFileableType() == 'App\Customer') {
$count = \DB::table('customer_supplier')
->where('customer_id', $fileable->id)
->where('supplier_id', $element->item->supplier_id)
->count();
if ( ! $count ) {
$fileable->suppliers()->attach($element->item->supplier->id);
}
}
}
}
}
}
$fileSuppliers = json_encode($fileSuppliers);
$file->quickSearch = $quickSearchFile;
$file->suppliers = $fileSuppliers;
$file->quickSearchLabel = $file->site ?? $file->name ?? $file->id;
$file->quickSearchIndex = true;
$file->save();
}
if ($fileable->invoices) {
if (count ($fileable->invoices)) {
foreach ($fileable->invoices as $invoice) {
$quickSearch .= $invoice->number . ' ';
}
foreach ($fileable->invoices as $invoice) {
preg_match_all ('!\d+!', $invoice->number, $matches);
$quickSearch .= $matches[0][0] . ' ';
}
}
}
$customerSuppliers = json_encode($customerSuppliers);
$fileable->quickSearch = $quickSearch;
$fileable->suppliers = $customerSuppliers;
$fileable->quickSearchLabel = $quickSearchLabel;
$fileable->quickSearchIndex = true;
$fileable->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment