Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\BlueRock\Services;
- class FileableIndexService
- {
- public function __construct($fileable_type, $fileable_id)
- {
- $this->fileable_type = $fileable_type;
- $this->fileable_id = $fileable_id;
- }
- private function getFileable()
- {
- return $this->fileable_type::find($this->fileable_id);
- }
- public function breakIndexes()
- {
- $fileable = $this->getFileable();
- $fileable->quickSearchIndex = false;
- $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->recurring 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);
- }
- }
- }
- $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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement