diff --git a/api/index.php b/api/index.php index b558600..dd328a6 100644 --- a/api/index.php +++ b/api/index.php @@ -119,11 +119,17 @@ function checkRecaptchaRequest($token, $ipAddress) return $response_decoded['success']; } -function getCompaniesForUser($user_id) +function getCompaniesForUser($user_id, $except_company_id = null) { if(CModule::IncludeModule('iblock')) { - $existed_client_as_user_res = CIBlockElement::GetList([ 'id' => 'desc' ], [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'PROPERTY_USERS' => $user_id ], false, []); + $filter = [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'PROPERTY_USERS' => $user_id ]; + if($except_company_id !== null) + { + $filter['!ID'] = $except_company_id; + } + + $existed_client_as_user_res = CIBlockElement::GetList([ 'id' => 'desc' ], $filter, false, []); $companies = []; @@ -215,6 +221,80 @@ function getUsersForCompany($code) return []; } +function setCompanyForUser($ID, $REQ, $replace = false) +{ + $user_properties = [ + 'COMPANY' => (string)$REQ['org_title'], + 'INN' => (string)$REQ['inn'], + 'KPP' => (string)$REQ['kpp'], + 'OGRN' => (string)$REQ['ogrn'], + 'ADMINS' => [ $ID ], + 'USERS' => [ $ID ], + ]; + + $ar_new_client = [ + 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, + 'NAME' => $REQ['org_title'], + 'CODE' => $REQ['crm_id'], + 'PROPERTY_VALUES' => $user_properties, + 'ACTIVE' => 'Y', // активен + ]; + + $admins_to_remove = []; + + $new_client = new CIBlockElement; + if($new_client_id = $new_client->Add($ar_new_client)) + { + } + else + { + $existed_client_res = CIBlockElement::GetList([ 'id' => 'desc' ], [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'CODE' => $REQ['crm_id'] ], false, []); + while ($existed_client_element = $existed_client_res->GetNextElement()) + { + $existed_client_record = $existed_client_element->GetFields(); + $existed_client_record['PROPERTIES'] = $existed_client_element->GetProperties(); + + if($replace) + { + $admins = []; + $users = []; + + $admins_to_remove = $existed_client_record['PROPERTIES']['ADMINS']['VALUE']; + } + else + { + $admins = $existed_client_record['PROPERTIES']['ADMINS']['VALUE']; + $users = $existed_client_record['PROPERTIES']['USERS']['VALUE']; + } + + array_push($admins, $ID); + array_push($users, $ID); + + $admins = array_unique($admins); + $users = array_unique($users); + + $updated_fields = [ + 'PROPERTY_VALUES' => [ + 'ADMINS' => $admins, + 'USERS' => $users, + ] + ]; + + CIBlockElement::SetPropertyValuesEx($existed_client_record['ID'], IBLOCK_ID_CLIENTS, [ 'ADMINS' => $admins, 'USERS' => $users, ]); + + foreach($admins_to_remove AS $admin_to_remove_id) + { + $another_user_companies = getCompaniesForUser($admin_to_remove_id, $existed_client_record['ID']); + + if(count($another_user_companies) === 0) + { + \CUser::Delete($admin_to_remove_id); + } + } + } + } +} + if($_SERVER['REMOTE_USER'] && strpos($_SERVER['REMOTE_USER'], "Bearer") > -1) { $token = str_replace("Bearer ", "", $_SERVER['REMOTE_USER']); @@ -292,119 +372,130 @@ switch($PARAM_1) { if($auth['username'] !== 'crm') { header('HTTP/1.0 401 Unauthorized'); print json_encode(["status" => "error", "error" => "unauthorized", "message" => "Unauthorized"]); die(); } - $user_registered = false; + $profile = [ + "XML_ID" => $REQ['crm_id'], + "LOGIN" => $REQ['email'], + "NAME" => $REQ['firstname'], + "SECOND_NAME" => $REQ['secondname'], + "LAST_NAME" => $REQ['lastname'], + "EMAIL" => $REQ['email'], + "UF_ORG_TITLE" => (string)$REQ['org_title'], + "UF_INN" => (string)$REQ['inn'], + "UF_KPP" => (string)$REQ['kpp'], + "UF_OGRN" => (string)$REQ['ogrn'], + "UF_PHONE_NUMBER" => (int)$REQ['phone'], + ]; - $existed_users_res = \CUser::GetList(["ID" => "ASC"], false, [ "XML_ID" => $REQ['crm_id'] ], []); + $user_registered_id = null; + $company_registered_id = null; + + $existed_users_res = \CUser::GetList(["ID" => "ASC"], false, [ "LOGIN" => $REQ['email'] ], []); while($existed_user = $existed_users_res->Fetch()) { - $user_registered = true; + $user_registered_id = $existed_user["ID"]; } - if($user_registered) + $existed_client_as_admin_res = CIBlockElement::GetList([ 'ID' => 'ASC' ], [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'CODE' => $REQ['crm_id'] ], false, []); + while ($existed_client_as_admin_element = $existed_client_as_admin_res->GetNextElement()) { - print json_encode([ - "status" => "error", - "error" => "user_already_registered", - "message" => "User already registered", - ]); + $existed_client_as_admin_record = $existed_client_as_admin_element->GetFields(); + $company_registered_id = $existed_client_as_admin_record['ID']; } - else - { - $user = new \CUser; - $profile = [ - "XML_ID" => $REQ['crm_id'], - "LOGIN" => $REQ['email'], - "NAME" => $REQ['firstname'], - "SECOND_NAME" => $REQ['secondname'], - "LAST_NAME" => $REQ['lastname'], - "PASSWORD" => $REQ['password'], - "CONFIRM_PASSWORD" => $REQ['password'], - "EMAIL" => $REQ['email'], - "UF_ORG_TITLE" => (string)$REQ['org_title'], - "UF_INN" => (string)$REQ['inn'], - "UF_KPP" => (string)$REQ['kpp'], - "UF_OGRN" => (string)$REQ['ogrn'], - "UF_PHONE_NUMBER" => (int)$REQ['phone'], - ]; - $ID = $user->Add($profile); - if (intval($ID) > 0) + if($user_registered_id !== null) + { + //user exists + + if($company_registered_id === null) { + //new company for user + setCompanyForUser($user_registered_id, $REQ); + + $company_message = "Вам предоставлен доступ к Личному кабинету следующей организации:

\n\n"; + $company_message .= $REQ['org_title'].", ИНН: ".$REQ['inn']."
\n"; + \Bitrix\Main\Mail\Event::send([ - "EVENT_NAME" => "USER_INFO", + "EVENT_NAME" => "CLIENT_USER_INVITE", "LID" => "s1", "C_FIELDS" => Array( "EMAIL" => $REQ['email'], - "ORG_NAME" => $REQ['org_title'], - "LOGIN" => $REQ['email'], - "PASS" => $REQ['password'], + "COMPANIES" => $company_message, ) ]); - $user_properties = [ - 'COMPANY' => $REQ['org_title'], - 'INN' => (string)$REQ['inn'], - 'KPP' => (string)$REQ['kpp'], - 'OGRN' => (string)$REQ['ogrn'], - 'ADMINS' => [ $ID ], - 'USERS' => [ $ID ], - ]; - - $ar_new_client = [ - 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, - 'NAME' => $REQ['org_title'], - 'CODE' => $REQ['crm_id'], - 'PROPERTY_VALUES' => $user_properties, - 'ACTIVE' => 'Y', // активен - ]; - - $new_client = new CIBlockElement; - if($new_client_id = $new_client->Add($ar_new_client)) - { - } - else - { - $existed_client_res = CIBlockElement::GetList([ 'id' => 'desc' ], [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'CODE' => $REQ['crm_id'] ], false, []); - while ($existed_client_element = $existed_client_res->GetNextElement()) - { - $existed_client_record = $existed_client_element->GetFields(); - $existed_client_record['PROPERTIES'] = $existed_client_element->GetProperties(); - - $admins = $existed_client_record['PROPERTIES']['ADMINS']['VALUE']; - $users = $existed_client_record['PROPERTIES']['USERS']['VALUE']; - - array_push($admins, $ID); - array_push($users, $ID); - - $admins = array_unique($admins); - $users = array_unique($users); - - $updated_fields = [ - 'PROPERTY_VALUES' => [ - 'ADMINS' => $admins, - 'USERS' => $users, - ] - ]; - - CIBlockElement::SetPropertyValuesEx($existed_client_record['ID'], IBLOCK_ID_CLIENTS, [ 'ADMINS' => $admins, 'USERS' => $users, ]); - } - } - print json_encode([ "status" => "success" ]); + + die(); } else { - $error = $user->LAST_ERROR; - //echo "
".print_r($arFields, true)."
"; - //echo $error; - print json_encode([ "status" => "error", - "error" => "wrong_payload", - "message" => $error, + "error" => "company_already_registered", + "message" => "Company already registered", ]); + + die(); + } + } + else + { + if($company_registered_id === null) + { + $user = new \CUser; + + $profile["PASSWORD"] = $REQ['password']; + $profile["CONFIRM_PASSWORD"] = $REQ['password']; + + $ID = $user->Add($profile); + if (intval($ID) > 0) + { + \Bitrix\Main\Mail\Event::send([ + "EVENT_NAME" => "USER_INFO", + "LID" => "s1", + "C_FIELDS" => Array( + "EMAIL" => $REQ['email'], + "ORG_NAME" => $REQ['org_title'], + "LOGIN" => $REQ['email'], + "PASS" => $REQ['password'], + ) + ]); + + //new user & new company + setCompanyForUser($ID, $REQ); + + print json_encode([ + "status" => "success" + ]); + + die(); + } + else + { + $error = $user->LAST_ERROR; + //echo "
".print_r($arFields, true)."
"; + //echo $error; + + print json_encode([ + "status" => "error", + "error" => "wrong_payload", + "message" => $error, + ]); + + die(); + } + } + else + { + print json_encode([ + "status" => "error", + "error" => "company_already_registered", + "message" => "Company already registered", + ]); + + die(); } } @@ -416,42 +507,51 @@ switch($PARAM_1) { if($auth['username'] !== 'crm') { header('HTTP/1.0 401 Unauthorized'); print json_encode(["status" => "error", "error" => "unauthorized", "message" => "Unauthorized"]); die(); } - $user_registered = false; + $profile = [ + "XML_ID" => $REQ['crm_id'], + "LOGIN" => $REQ['email'], + "NAME" => $REQ['firstname'], + "SECOND_NAME" => $REQ['secondname'], + "LAST_NAME" => $REQ['lastname'], + "EMAIL" => $REQ['email'], + "UF_ORG_TITLE" => (string)$REQ['org_title'], + "UF_INN" => (string)$REQ['inn'], + "UF_KPP" => (string)$REQ['kpp'], + "UF_OGRN" => (string)$REQ['ogrn'], + "UF_PHONE_NUMBER" => (int)$REQ['phone'], + ]; - $existed_users_res = \CUser::GetList(["ID" => "ASC"], false, [ "XML_ID" => $REQ['crm_id'] ], []); + $user_registered = null; + $company_registered_id = null; + $company_registered = null; + + $existed_users_res = \CUser::GetList(["ID" => "ASC"], false, [ "LOGIN" => $REQ['email'] ], []); while($existed_user = $existed_users_res->Fetch()) { - $user_registered = true; + $user_registered = $existed_user['ID']; + } - $user = new \CUser; - $profile = [ - "XML_ID" => $REQ['crm_id'], - "LOGIN" => $REQ['email'], - "NAME" => $REQ['firstname'], - "SECOND_NAME" => $REQ['secondname'], - "LAST_NAME" => $REQ['lastname'], - "PASSWORD" => $REQ['password'], - "CONFIRM_PASSWORD" => $REQ['password'], - "EMAIL" => $REQ['email'], - "UF_ORG_TITLE" => (string)$REQ['org_title'], - "UF_INN" => (string)$REQ['inn'], - "UF_KPP" => (string)$REQ['kpp'], - "UF_OGRN" => (string)$REQ['ogrn'], - "UF_PHONE_NUMBER" => (int)$REQ['phone'], - ]; + $existed_client_as_admin_res = CIBlockElement::GetList([ 'ID' => 'ASC' ], [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'CODE' => $REQ['crm_id'] ], false, []); + while ($existed_client_as_admin_element = $existed_client_as_admin_res->GetNextElement()) + { + $existed_client_as_admin_record = $existed_client_as_admin_element->GetFields(); + $company_registered_id = $existed_client_as_admin_record['ID']; + } - $user->Update($existed_user['ID'], $profile); + if($user_registered !== null) + { + // user exists - if($user->LAST_ERROR) - { - print json_encode([ - "status" => "error", - "error" => "wrong_payload", - "message" => $user->LAST_ERROR, - ]); - } - else + if($company_registered_id !== null) { + //existed company + + $profile["PASSWORD"] = $REQ['password']; + $profile["CONFIRM_PASSWORD"] = $REQ['password']; + + $user = new \CUser; + $user->Update($user_registered, $profile); + \Bitrix\Main\Mail\Event::send([ "EVENT_NAME" => "USER_INFO", "LID" => "s1", @@ -463,22 +563,127 @@ switch($PARAM_1) ) ]); + //"PASSWORD" => $REQ['password'], + //"CONFIRM_PASSWORD" => $REQ['password'], + + //print json_encode([ + // "status" => "error", + // "error" => "company_already_registered", + // "message" => "Company already registered", + //]); + print json_encode([ "status" => "success" ]); + + die(); + } + else + { + //new company + //setCompanyForUser($user_registered_id, $REQ); + + print json_encode([ + "status" => "error", + "error" => "no_company_for_update", + "message" => "No company for update", + ]); + + die(); + } + } + else + { + // new user + + if($company_registered_id !== null) + { + //existed company + //THIS! + + $profile["PASSWORD"] = $REQ['password']; + $profile["CONFIRM_PASSWORD"] = $REQ['password']; + + $user = new \CUser; + $ID = $user->Add($profile); + + if (intval($ID) > 0) + { + //new user & new company + setCompanyForUser($ID, $REQ, true); + + \Bitrix\Main\Mail\Event::send([ + "EVENT_NAME" => "USER_INFO", + "LID" => "s1", + "C_FIELDS" => Array( + "EMAIL" => $REQ['email'], + "ORG_NAME" => $REQ['org_title'], + "LOGIN" => $REQ['email'], + "PASS" => $REQ['password'], + ) + ]); + + print json_encode([ + "status" => "success" + ]); + + die(); + } + else + { + print json_encode([ + "status" => "error", + "error" => "wrong_payload", + "message" => $user->LAST_ERROR, + ]); + + die(); + } + } + else + { + //no company + + print json_encode([ + "status" => "error", + "error" => "no_company_for_update", + "message" => "No company for update", + ]); + + die(); } } - if(!$user_registered) +/* ******************************* + \Bitrix\Main\Mail\Event::send([ + "EVENT_NAME" => "USER_INFO", + "LID" => "s1", + "C_FIELDS" => Array( + "EMAIL" => $REQ['email'], + "ORG_NAME" => (string)$REQ['org_title'], + "LOGIN" => $REQ['email'], + "PASS" => $REQ['password'], + ) + ]); + + if($user->LAST_ERROR) { print json_encode([ "status" => "error", - "error" => "unknown_user", - "message" => "Unknown user", + "error" => "wrong_payload", + "message" => $user->LAST_ERROR, + ]); + } + else + { + print json_encode([ + "status" => "success" ]); } die(); +******************************* */ + } break; @@ -821,8 +1026,10 @@ switch($PARAM_1) if(count($user_companies) === 0) { - $user = new CUser; - $user->Update($removed_user_id, [ "BLOCKED" => "Y" ]); + \CUser::Delete($removed_user_id); + + //$user = new CUser; + //$user->Update($removed_user_id, [ "BLOCKED" => "Y" ]); } } @@ -1505,7 +1712,7 @@ switch($PARAM_1) { $user = new \CUser; $login_result = $user->Login($REQ['email'], $REQ['password'], "N"); - + if($login_result == 1) { $existed_user_res = \CUser::GetByLogin($REQ['email']); @@ -2031,27 +2238,27 @@ switch($PARAM_1) break; case "vizitka": - { - //define("LOG_FILENAME", $_SERVER["DOCUMENT_ROOT"]."/vizitka/tmp/log.txt"); - //AddMessage2Log("_REQUEST['guid'] => ".$_REQUEST['guid'], "my_module_id"); - - $c = curl_init(); - curl_setopt($c, CURLOPT_URL, API_HOST."/site/GetUserBusinessCard/?guid=".$_REQUEST['guid']); - curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30); - curl_setopt($c, CURLOPT_TIMEOUT, 30); - curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); - curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0); - curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); + { + //define("LOG_FILENAME", $_SERVER["DOCUMENT_ROOT"]."/vizitka/tmp/log.txt"); + //AddMessage2Log("_REQUEST['guid'] => ".$_REQUEST['guid'], "my_module_id"); - $response = curl_exec($c) or die(curl_error($c)); - - curl_close($c); - print $response; + $c = curl_init(); + curl_setopt($c, CURLOPT_URL, API_HOST."/site/GetUserBusinessCard/?guid=".$_REQUEST['guid']); + curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30); + curl_setopt($c, CURLOPT_TIMEOUT, 30); + curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); - die(); - } - break; + $response = curl_exec($c) or die(curl_error($c)); + + curl_close($c); + print $response; + + die(); + } + break; default: { diff --git a/local/components/evolution/about.roadmap/templates/.default/template.php b/local/components/evolution/about.roadmap/templates/.default/template.php index ac30698..73649d6 100644 --- a/local/components/evolution/about.roadmap/templates/.default/template.php +++ b/local/components/evolution/about.roadmap/templates/.default/template.php @@ -21,7 +21,7 @@ $this->setFrameMode(true); путь развития

- Скачать презентацию + Скачать презентацию
@@ -31,7 +31,7 @@ $this->setFrameMode(true);

-

+

", "

"], ["", "
"], $period['PREVIEW_TEXT']); ?>