Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\BlueRock\Services;
  4.  
  5. class FileableIndexService
  6. {
  7. public function __construct($fileable_type, $fileable_id)
  8. {
  9. $this->fileable_type = $fileable_type;
  10.  
  11. $this->fileable_id = $fileable_id;
  12. }
  13.  
  14. private function getFileable()
  15. {
  16. return $this->fileable_type::find($this->fileable_id);
  17. }
  18.  
  19.  
  20. public function breakIndexes()
  21. {
  22. $fileable = $this->getFileable();
  23.  
  24. $fileable->quickSearchIndex = false;
  25.  
  26. $fileable->files()->update(['quickSearchIndex' => false]);
  27.  
  28. }
  29.  
  30. public function index()
  31. {
  32. $fileable = $this->getFileable();
  33.  
  34. $customerSuppliers = [];
  35.  
  36. $quickSearch = '';
  37.  
  38. $quickSearch .= implode(' ', numberVersions($fileable->phone)) . ' ';
  39.  
  40. $quickSearch .= implode(' ', numberVersions($fileable->mobilePhone)) . ' ';
  41.  
  42. $quickSearch .= $fileable->name . ' '
  43. . $fileable->brand . ' '
  44. . $fileable->mainContactLastName . ' '
  45. . $fileable->customerAccount . ' '
  46. . $fileable->emailAddress . ' '
  47. . $fileable->mainAddressPostalCode . ' '
  48. . $fileable->mainAddressCity;
  49.  
  50. $quickSearchLabel = $fileable->name . ' '
  51. . $fileable->mainContactLastName . ' '
  52. . $fileable->customerAccount . ' ##'
  53. . $fileable->id;
  54.  
  55. foreach ($fileable->contacts as $contact) {
  56.  
  57. $quickSearch .= implode(' ', numberVersions($contact->mobilePhone));
  58.  
  59. $quickSearch .= implode(' ', numberVersions($contact->landlinePhone));
  60.  
  61. $quickSearch .= ' ' . $contact->lastName . ' ' . $contact->emailAddress . ' ';
  62.  
  63. }
  64.  
  65. foreach ($fileable->files as $file) {
  66.  
  67. $fileSuppliers = [];
  68.  
  69. if (empty($file->name)) {
  70.  
  71. $file->name = null;
  72. $file->save();
  73.  
  74. }
  75.  
  76. if (empty ($file->site)) {
  77.  
  78. $file->site = null;
  79. $file->save();
  80. }
  81.  
  82. $quickSearchFile = '';
  83.  
  84. if ( ! empty($file->name)) {
  85.  
  86. $quickSearch .= $file->name . ' ';
  87.  
  88. $quickSearchFile .= $file->name . ' ';
  89.  
  90. }
  91.  
  92. if ( ! empty($file->site)) {
  93.  
  94. $quickSearch .= $file->site . ' ';
  95.  
  96. $quickSearchFile .= $file->site . ' ';
  97.  
  98. }
  99.  
  100. if ( ! empty($file->postalCode)) {
  101.  
  102. $quickSearch .= $file->postalCode . ' ';
  103.  
  104. $quickSearchFile .= $file->postalCode . ' ';
  105. }
  106.  
  107. if ( ! empty($file->city)) {
  108.  
  109. $quickSearch .= $file->city . ' ';
  110.  
  111. $quickSearchFile .= $file->city . ' ';
  112.  
  113. }
  114.  
  115. if ( ! empty($file->orderNumber)) {
  116.  
  117. $quickSearch .= $file->orderNumber . ' ';
  118.  
  119. $quickSearchFile .= $file->orderNumber . ' ';
  120. }
  121.  
  122. if ( ! empty($file->parcelNumber)) {
  123.  
  124. $quickSearch .= $file->parcelNumber . ' ';
  125.  
  126. $quickSearchFile .= $file->parcelNumber . ' ';
  127. }
  128.  
  129. if ( ! empty($file->phoneLine)) {
  130.  
  131. $quickSearch .= implode(' ', numberVersions($file->phoneLine)) . ' ';
  132.  
  133. $quickSearchFile .= implode(' ', numberVersions($file->phoneLine)) . ' ';
  134. }
  135.  
  136. if ( ! empty($file->deviceIpAddress)) {
  137.  
  138. $quickSearch .= $file->deviceIpAddress . ' ';
  139.  
  140. $quickSearchFile .= $file->deviceIpAddress . ' ';
  141. }
  142.  
  143. foreach ($file->telephonyLines as $line) {
  144.  
  145. $quickSearch .= implode(' ', numberVersions($line->phoneLine)) . ' ';
  146.  
  147. $quickSearchFile .= implode(' ', numberVersions($line->phoneLine)) . ' ';
  148.  
  149. }
  150.  
  151. foreach ($file->recurring as $element) {
  152.  
  153. if ($element->item->supplier) {
  154.  
  155. if ( ! in_array($element->item->supplier->name, $fileSuppliers)) {
  156.  
  157. array_push ($fileSuppliers, $element->item->supplier->name);
  158.  
  159. }
  160.  
  161. if ( ! in_array ($element->item->supplier->name, $customerSuppliers)) {
  162.  
  163. array_push($customerSuppliers, $element->item->supplier->name);
  164.  
  165. }
  166. }
  167.  
  168. }
  169.  
  170. $fileSuppliers = json_encode($fileSuppliers);
  171.  
  172. $file->quickSearch = $quickSearchFile;
  173. $file->suppliers = $fileSuppliers;
  174. $file->quickSearchLabel = $file->site ?? $file->name ?? $file->id;
  175. $file->quickSearchIndex = true;
  176. $file->save();
  177.  
  178. }
  179.  
  180. if ($fileable->invoices) {
  181.  
  182. if (count($fileable->invoices)) {
  183.  
  184. foreach ($fileable->invoices as $invoice) {
  185.  
  186. $quickSearch .= $invoice->number . ' ';
  187.  
  188. }
  189.  
  190. foreach ($fileable->invoices as $invoice) {
  191.  
  192. preg_match_all('!\d+!', $invoice->number, $matches);
  193.  
  194. $quickSearch .= $matches[0][0] . ' ';
  195. }
  196. }
  197. }
  198.  
  199. $customerSuppliers = json_encode($customerSuppliers);
  200.  
  201. $fileable->quickSearch = $quickSearch;
  202. $fileable->suppliers = $customerSuppliers;
  203. $fileable->quickSearchLabel = $quickSearchLabel;
  204. $fileable->quickSearchIndex = true;
  205. $fileable->save();
  206.  
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement