| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php
- namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Providers;
- use ThurData\Servers\KerioEmail\App\Enums\Kerio;
- use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
- use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response;
- use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateAccount;
- use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
- use function ThurData\Servers\KerioEmail\Core\Helper\di;
- use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
- use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
- use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
- use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
- /**
- *
- * Created by PhpStorm.
- * User: ThurData
- * Date: 18.09.19
- * Time: 09:35
- * Class EditAccountDataProvider
- */
- class EditAccountDataProvider extends BaseDataProvider
- {
- /**
- *
- */
- public function read()
- {
- /**
- * hosting id
- */
- $hid = $this->getRequestValue('id');
- /**
- * load hosting
- */
- $hosting = Hosting::where('id', $hid)->first();
- /**
- * load api
- */
- $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
- $fields =array(
- 'id',
- 'companyContactId',
- 'loginName',
- 'fullName',
- 'description',
- 'isEnabled',
- 'emailAddresses',
- );
- $cond = array(
- array(
- "fieldName" => "id",
- "comparator" => "Eq",
- "value" => $this->actionElementId
- )
- );
- try {
- $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $domainID = $api->getDomainId($this->getWhmcsParamByKey('customfields')['maildomain']);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $account = $api->getUsers($fields,$domainID,$cond);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $address = $api->getAddress($this->actionElementId);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- $api->logout();
- $this->data['id'] = $account[0]['id'];
- $this->data['username'] = $account[0]['loginName'];
- $this->data['domain'] = $this->getWhmcsParamByKey('customfields')['maildomain'];
- $this->data['firstname'] = $address['contacts'][0]['firstName'];
- $this->data['lastname'] = $address['contacts'][0]['surName'];
- $this->data['display_name'] = $address['contacts'][0]['commonName'];
- $this->data['status'] = $account['isEnabled'];
- $this->data['title'] = $address['contacts'][0]['titleBefore'];
- $this->data['profession'] = $address['contacts'][0]['profession'];
- $this->data['work_phone'] = $address['contacts'][0]['phoneNumberWorkVoice'];
- $this->data['mobile_phone'] = $address['contacts'][0]['phoneNumberMobile'];
- $this->data['department'] = $address['contacts'][0]['departmentName'];
- $this->data['office'] = $address['contacts'][0]['postalAddressWork']['extendedAddress'];
- $lang = di('lang');
- $this->availableValues['status'] = [
- Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
- Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed')
- ];
- $this->availableValues['unit'] = [
- 'MegaBytes' => 'MB',
- 'GigaBytes' => 'GB',
- ];
- }
- /**
- * @return HtmlDataJsonResponse
- */
- public function update()
- {
- /**
- * hosting id
- */
- $hid = $this->request->get('id');
- $fieldToProtection = ['firstname', 'lastname', 'display_name', 'office', 'title', 'department', 'profession'];
- foreach ($this->formData as $field => &$value)
- {
- $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
- }
- /**
- * product manager allow to check product settings
- */
- $productManager = new ProductManager();
- $productManager->loadByHostingId($hid);
- $account['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
- if ($this->formData['quota'] > 0) {
- $account['diskSizeLimit']['isActive'] = TRUE;
- $account['diskSizeLimit']['limit']['value'] = $this->formData['quota'];
- $account['diskSizeLimit']['limit']['unit'] = $this->formData['unit'];
- } else {
- $account['diskSizeLimit']['isActive'] = FALSE;
- }
- $fields['firstName'] = $this->formData['firstname'];
- $fields['surName'] = $this->formData['lastname'];
- $fields['commonName'] = $this->formData['display_name'];
- $fields['postalAddressWork']['extendedAddress'] = $this->formData['office'];
- $fields['titleBefore'] = $this->formData['title'];
- $fields['phoneNumberWorkVoice'] = $this->formData['work_phone'];
- $fields['phoneNumberMobile'] = $this->formData['mobile_phone'];
- $fields['departmentName'] = $this->formData['department'];
- $fields['profession'] = $this->formData['profession'];
- $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
- try {
- $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $result = $api->modifyUser($this->formData['id'], $account);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $result = $api->setAddress($this->formData['id'], $fields);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- $api->logout();
- return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountHasBeenUpdated')->setStatusSuccess();
- }
- /**
- * @return HtmlDataJsonResponse
- */
- public function updateStatus()
- {
- /**
- * hosting id
- */
- $hid = $this->request->get('id');
- /**
- * product manager allow to check product settings
- */
- $productManager = new ProductManager();
- $productManager->loadByHostingId($hid);
- $status['isEnabled'] = $this->formData['status'] === 'active' ? TRUE : FALSE;
- $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
- try {
- $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $result = $api->modifyUser($this->formData['id'], $status);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- $api->logout();
- /**
- * return success
- */
- return (new HtmlDataJsonResponse())->setMessageAndTranslate('emailAccountStatusHasBeenUpdated')->setStatusSuccess();
- }
- /**
- * @return HtmlDataJsonResponse
- */
- public function changePassword()
- {
- /**
- * hosting id
- */
- $hid = $this->request->get('id');
- /**
- * product manager allow to check product settings
- */
- $productManager = new ProductManager();
- $productManager->loadByHostingId($hid);
- $fields['password'] = $this->formData['password'];
- $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
- try {
- $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- try {
- $result = $api->modifyUser($this->formData['id'], $fields);
- } catch (KerioApiException $error) {
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $error,
- 'Debug Error',
- $error->getMessage()
- );
- return ['error' => $error->getMessage()];
- }
- $api->logout();
- return (new HtmlDataJsonResponse())->setMessageAndTranslate('passwordChangedSuccessfully')->setStatusSuccess();
- }
- /**
- *
- */
- public function readCosParams()
- {
- $hid = $this->getRequestValue('id');
- /**
- * product manager allow to check product settings
- */
- $productManager = new ProductManager();
- $productManager->loadByHostingId($hid);
- if($productManager->get('cos_name') === ClassOfServices::CLASS_OF_SERVICE_QUOTA)
- {
- /**
- *
- * get soap create domain service
- */
- $api =(new KerioManager())
- ->getApiByHosting($hid)
- ->soap;
- /**
- *
- * get cos from API
- */
- $classOfServices = $api->repository()->cos->all();
- /**
- *
- * load configurable options coses
- */
- $supportedCos = $productManager->getSettingCos();
- /**
- *
- * add COS to array
- */
- $configoptions = $this->getFilteredCosConfigurableOptions();
- foreach($classOfServices as $cos)
- {
- /**
- *
- *
- * skip COS which is not used in configurable options
- */
- if(!($supportedCos && array_key_exists($cos->getId(), $supportedCos)))
- {
- continue;
- }
- /**
- *
- * skip if class of services doesnt exists in config option list
- */
- if($configoptions && !array_key_exists('cosQuota_'.$cos->getId(), $configoptions))
- {
- continue;
- }
- /**
- * skip not purchased as CO
- */
- if ($configoptions && $configoptions['cosQuota_'.$cos->getId()] == 0)
- {
- continue;
- }
- /**
- * 1. check if config opts are not available
- * 2. skip if quantity === 0
- */
- if(!$configoptions && $supportedCos[$cos->getId()] == 0)
- {
- continue;
- }
- /* @var $cos ClassOfService*/
- $this->availableValues['cosId'][$cos->getId()] = $cos->getMbMailQuote().' MB';
- }
- }
- }
- /**
- * @return bool|mixed
- */
- protected function getFilteredCosConfigurableOptions()
- {
- $configoptions = $this->getWhmcsParamByKey('configoptions');
- foreach($configoptions as $key => $value)
- {
- if(strpos($key, ClassOfServicesOptions::COS_CONFIG_OPT_PREFIX) === false)
- {
- unset($configoptions[$key]);
- }
- }
- return $configoptions;
- }
- }
|