From 137c89cd9d63d04db0283b96532595f14fa3176e Mon Sep 17 00:00:00 2001 From: Thekindbull Date: Tue, 17 Dec 2024 23:12:42 +0800 Subject: [PATCH] company moderation fixed --- .../Company/CreateCompanyController.php | 20 ++++-- .../Controllers/Company/DetailsController.php | 8 +-- app/Models/Bitrix/BitrixSender.php | 34 +++++---- app/Models/Bitrix/SendClient.php | 72 +++++++------------ app/Models/Bitrix/SendCompany.php | 15 ++-- resources/views/company/create.blade.php | 23 ++++-- 6 files changed, 83 insertions(+), 89 deletions(-) diff --git a/app/Http/Controllers/Company/CreateCompanyController.php b/app/Http/Controllers/Company/CreateCompanyController.php index 0aff52b..1baca20 100644 --- a/app/Http/Controllers/Company/CreateCompanyController.php +++ b/app/Http/Controllers/Company/CreateCompanyController.php @@ -20,16 +20,22 @@ public function __invoke(Request $request) 'status' => 'new', 'type' => 'AGENCY' ]); - $company = Company::create($request->only('name', 'email', 'inn', 'legal_address', 'secret', 'status', 'type')); - if ($company) + $data = $request->only('name', 'email', 'inn', 'legal_address', 'secret', 'status', 'type', 'phone'); + $company = Company::create($data); + if (!$company) { - $companyConfirmByBitrix = new SendCompany($company); - $companyConfirmByBitrix->send(); - return view('company.created'); + return back()->with('error', __('Company creation error'))->withInput(); } - else + $data['callback_url'] = route('company.status.update', [ + 'company' => $company->id, + 'secret' => $company->secret + ]); + $companyConfirmByBitrix = new SendCompany($company->id, $data); + if (!$sender = $companyConfirmByBitrix->send()) { - return back()->withInputs(); + $company->delete(); + return back()->with('error', __('Company creation error'))->withInput(); } + return view('company.created'); } } diff --git a/app/Http/Controllers/Company/DetailsController.php b/app/Http/Controllers/Company/DetailsController.php index 7212ac9..7dcf580 100644 --- a/app/Http/Controllers/Company/DetailsController.php +++ b/app/Http/Controllers/Company/DetailsController.php @@ -49,10 +49,10 @@ public function index() public function store(Request $request, Company $company) { $userId = auth()->user()->id; - $agent = Agent::where('user_id', $userId)->get(); - if ($agent->count() == 1) + $admin = CompanyAdmin::where('user_id', $userId)->get(); + if ($admin->count() == 1) { - $agent = $agent->first(); + $agent = $admin->first(); if ($agent->company_id != $company->id) { return; @@ -62,8 +62,6 @@ public function store(Request $request, Company $company) { return back(); } - ; - $company->details = $request->all(); $company->save(); return to_route('company.details', [ diff --git a/app/Models/Bitrix/BitrixSender.php b/app/Models/Bitrix/BitrixSender.php index 9af23e5..1ea0bab 100644 --- a/app/Models/Bitrix/BitrixSender.php +++ b/app/Models/Bitrix/BitrixSender.php @@ -4,14 +4,16 @@ use Illuminate\Support\Facades\Http; class BitrixSender -{ + { public $url; public $data; - public function __construct($url, $data) { + public function __construct(string $url, array $data) + { $this->url = $url; $this->data = $data; - } - public function send() { + } + public function send() + { $postdata = http_build_query( $this->data ); @@ -22,25 +24,31 @@ public function send() { ), 'http' => array( 'method' => 'POST', - 'header' => - 'Content-type: application/x-www-form-urlencoded'."\r\n". + 'header' => + 'Content-type: application/x-www-form-urlencoded' . "\r\n" . '', 'content' => $postdata ) ); - - try { + + try + { $context = stream_context_create($opts); $result = file_get_contents($this->url, false, $context); $result = json_decode($result, $associative = true); - if (array_key_exists('result', $result)) { + if (array_key_exists('result', $result)) + { return $result['result']; - } else { + } + else + { return false; + } } - } catch (\Exception $e) { - dd($e); + catch (\Exception $e) + { return false; + } } } -} + diff --git a/app/Models/Bitrix/SendClient.php b/app/Models/Bitrix/SendClient.php index 74ff4fa..92af2e3 100644 --- a/app/Models/Bitrix/SendClient.php +++ b/app/Models/Bitrix/SendClient.php @@ -5,14 +5,15 @@ use Illuminate\Support\Facades\Http; class SendClient -{ + { private $IBLOCK_TYPE_ID = 'lists'; //CONFIG для Альфы - + private $URL = 'https://b24alfa.pro/rest/3165/v90a792nderzu0dj/lists.element.add.json'; private $IBLOCK_ID = 27; private $ID; + const NAME = "NAME"; const CLIENT_SECOND_NAME = "PROPERTY_94"; const CLIENT_FIRST_NAME = "PROPERTY_95"; const CLIENT_PHONE = "PROPERTY_96"; @@ -23,63 +24,38 @@ class SendClient const CALLBACK_URL = "PROPERTY_105"; private $data = []; - public function __construct($id, $data) { + public function __construct($id, $data) + { $this->ID = $id; $data = array_change_key_case($data, CASE_UPPER); $finalData = []; $refl = new \ReflectionClass(__CLASS__); $constants = $refl->getConstants(); - foreach ( $constants as $constName => $constValue ) - { - foreach ($data as $key => $value) + foreach ($constants as $constName => $constValue) { - if ( $constName == $key ) { + foreach ($data as $key => $value) + { + if ($constName == $key) + { $finalData[$constValue] = $value; break; + } } } - } $finalData['NAME'] = $finalData[self::CLIENT_FIRST_NAME] . ' ' . $finalData[self::CLIENT_SECOND_NAME]; $this->data = $finalData; return; - } - - public function send() { - $postdata = http_build_query( - [ - 'IBLOCK_TYPE_ID' => $this->IBLOCK_TYPE_ID, - 'IBLOCK_ID' => $this->IBLOCK_ID, - 'ELEMENT_CODE' => $this->ID, - 'FIELDS' => $this->data - ] - ); - - $opts = array( - 'ssl' => array( - 'verify_peer' => false, - 'verify_peername' => false - ), - 'http' => array( - 'method' => 'POST', - 'header' => - 'Content-type: application/x-www-form-urlencoded'."\r\n". - '', - 'content' => $postdata - ) - ); - return true; - try { - $context = stream_context_create($opts); - $result = file_get_contents($this->URL, false, $context); - $result = json_decode($result, $associative = true); - if (array_key_exists('result', $result)) { - return $result['result']; - } else { - return false; - } - } catch (\Exception $e) { - dd($e); - return false; } - } -} + + public function send() + { + $data = [ + 'IBLOCK_TYPE_ID' => $this->IBLOCK_TYPE_ID, + 'IBLOCK_ID' => $this->IBLOCK_ID, + 'ELEMENT_CODE' => $this->ID, + 'FIELDS' => $this->data + ]; + $sender = new BitrixSender($this->URL, $data); + return $sender->send(); + } + } \ No newline at end of file diff --git a/app/Models/Bitrix/SendCompany.php b/app/Models/Bitrix/SendCompany.php index e82d873..11f24c0 100644 --- a/app/Models/Bitrix/SendCompany.php +++ b/app/Models/Bitrix/SendCompany.php @@ -23,15 +23,11 @@ class SendCompany const CALLBACK_URL = "PROPERTY_122"; private $data = []; - public function __construct(Company $company) + public function __construct($id, array $data) { - $this->ID = $company->id; - $data = array_change_key_case($company->attributesToArray(), CASE_UPPER); + $this->ID = $id; + $data = array_change_key_case($data, CASE_UPPER); $finalData = $this->castConstants($data); - $finalData[self::CALLBACK_URL] = route('company.status.update', [ - 'company' => $this->ID, - 'secret' => $company->secret - ]); $this->data = $finalData; return; } @@ -60,10 +56,9 @@ public function send() 'IBLOCK_TYPE_ID' => $this->IBLOCK_TYPE_ID, 'IBLOCK_ID' => $this->IBLOCK_ID, 'ELEMENT_CODE' => $this->ID, - 'FIELDS' => $this->data, - 'PROPERTY_VALUES' => $this->data + 'FIELDS' => $this->data ]; $sender = new BitrixSender($this->URL, $data); - $sender->send(); + return $sender->send(); } } diff --git a/resources/views/company/create.blade.php b/resources/views/company/create.blade.php index 754664a..52e09ec 100644 --- a/resources/views/company/create.blade.php +++ b/resources/views/company/create.blade.php @@ -2,31 +2,42 @@ @section('content')
+ @isset($error) + E: {{ $error }} + @endisset +
{{ __('Create company form') }}
-
@csrf
- +
- +
-
- + +
+
+ +
- +