Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

36784 Plugin can not set supervisor in address record #594

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion plugin/Form/FormAddressCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ public function __construct(
/**
* @param FormData $pFormData
* @param bool $mergeExisting
* @param string $contactType
* @param int|null $estateId
* @return int the new (or updated) address ID
* @throws ApiClientException
* @throws UnknownFieldException
* @throws DependencyException
* @throws NotFoundException
*/
public function createOrCompleteAddress(
FormData $pFormData, bool $mergeExisting = false, string $contactType = ''): int
FormData $pFormData, bool $mergeExisting = false, string $contactType = '', int $estateId = null): int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add PHPDoc for these arguments: $contactType and $estateId for complete documentation? :)

{
$requestParams = $this->getAddressDataForApiCall($pFormData);
$requestParams['checkDuplicate'] = $mergeExisting;
Expand All @@ -82,6 +84,13 @@ public function createOrCompleteAddress(
if ( key_exists( 'newsletter', $requestParams ) ) {
unset( $requestParams['newsletter'] );
}
if (!empty($estateId)) {
$userName = $this->getSupervisorUsernameByEstateId($estateId);
if (!empty($userName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past we got minor bugs with using empty() function because "0" and 0 is also empty true. It is unlikely but what happens if the ID or username is 0?

Do you think using isset could be better or could causes other errors?

Edit:
Oh I see getSupervisorUsernameByEstateId returns maybe an empty string. So I guess we need empty() here.

Copy link
Contributor Author

@dai-eastgate dai-eastgate Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check and let you know asap

$requestParams['Benutzer'] = $userName;
}
}

$pApiClientAction = new APIClientActionGeneric
($this->_pSDKWrapper, onOfficeSDK::ACTION_ID_CREATE, 'address');
$pApiClientAction->setParameters($requestParams);
Expand Down Expand Up @@ -170,4 +179,61 @@ public function getAddressDataForEmail(FormData $pFormData): array
}
return $addressData;
}

/**
* @param int $estateId
* @return string
* @throws ApiClientException
* @throws DependencyException
* @throws NotFoundException
*/
private function getSupervisorUsernameByEstateId(int $estateId): string
{
$requestParams = [
'filter' => ['Id' => [['op' => '=', 'val' => $estateId]]],
'data' => ['benutzer'],
];

$pApiClientAction = new APIClientActionGeneric
($this->_pSDKWrapper, onOfficeSDK::ACTION_ID_READ, 'estate');

$pApiClientAction->setParameters($requestParams);
$pApiClientAction->addRequestToQueue();
$this->_pSDKWrapper->sendRequests();
$result = $pApiClientAction->getResultRecords();

if (!empty($result) && isset($result[0]["elements"]["benutzer"])) {
$userId = $result[0]["elements"]["benutzer"];
return $this->getUserNameById($userId);
} else {
return '';
}
}

/**
* @param string $userId
* @return string
* @throws ApiClientException
* @throws DependencyException
* @throws NotFoundException
*/
private function getUserNameById(string $userId): string
{
$pApiClientAction = new APIClientActionGeneric
($this->_pSDKWrapper, onOfficeSDK::ACTION_ID_GET, 'users');

$pApiClientAction->addRequestToQueue();
$this->_pSDKWrapper->sendRequests();
$result = $pApiClientAction->getResultRecords();

$userResult = array_values(array_filter($result, function($item) use ($userId) {
return $item['id'] == $userId && isset($item["elements"]["username"]);
}));

if (!empty($userResult)) {
return $userResult[0]["elements"]["username"];
} else {
return '';
}
}
}
9 changes: 9 additions & 0 deletions plugin/Form/FormPostInterestConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use onOffice\WPlugin\Field\SearchcriteriaFields;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\WP\WPQueryWrapper;

/**
*
Expand Down Expand Up @@ -66,4 +67,12 @@ public function getSearchcriteriaFields(): SearchcriteriaFields;
*/

public function getNewsletterAccepted(): bool;

/**
*
* @return WPQueryWrapper
*
*/

public function getWPQueryWrapper(): WPQueryWrapper;
}
20 changes: 19 additions & 1 deletion plugin/Form/FormPostInterestConfigurationDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use onOffice\WPlugin\Field\SearchcriteriaFields;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\WP\WPQueryWrapper;


/**
Expand All @@ -43,23 +44,28 @@ class FormPostInterestConfigurationDefault
/** @var SearchcriteriaFields */
private $_pSearchcriteriaFields;

/** @var WPQueryWrapper */
private $_pWPQueryWrapper = null;

/**
*
* @param SDKWrapper $pSDKWrapper
* @param FormAddressCreator $pFormAddressCreator
* @param SearchcriteriaFields $pSearchcriteriaFields
* @param WPQueryWrapper $pWPQueryWrapper
*
*/

public function __construct(
SDKWrapper $pSDKWrapper,
FormAddressCreator $pFormAddressCreator,
SearchcriteriaFields $pSearchcriteriaFields)
SearchcriteriaFields $pSearchcriteriaFields,
WPQueryWrapper $pWPQueryWrapper)
{
$this->_pSDKWrapper = $pSDKWrapper;
$this->_pFormAddressCreator = $pFormAddressCreator;
$this->_pSearchcriteriaFields = $pSearchcriteriaFields;
$this->_pWPQueryWrapper = $pWPQueryWrapper;
}


Expand Down Expand Up @@ -106,4 +112,16 @@ public function getNewsletterAccepted(): bool
{
return filter_var( $_POST['newsletter'] ?? null, FILTER_SANITIZE_STRING ) === 'y';
}


/**
*
* @return WPQueryWrapper
*
*/

public function getWPQueryWrapper(): WPQueryWrapper
{
return $this->_pWPQueryWrapper;
}
}
20 changes: 19 additions & 1 deletion plugin/Form/FormPostInterestConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use onOffice\WPlugin\Field\SearchcriteriaFields;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\WP\WPQueryWrapper;


/**
Expand All @@ -46,22 +47,28 @@ class FormPostInterestConfigurationTest
/** @var bool */
private $_newsletterAccepted = false;

/** @var WPQueryWrapper */
private $_pWPQueryWrapper;

/**
*
* @param SDKWrapper $pSDKWrapper
* @param FormAddressCreator $pFormAddressCreator
* @param SearchcriteriaFields $pSearchcriteriaFields
* @param WPQueryWrapper $pWPQueryWrapper
*
*/

public function __construct(
SDKWrapper $pSDKWrapper,
FormAddressCreator $pFormAddressCreator,
SearchcriteriaFields $pSearchcriteriaFields)
SearchcriteriaFields $pSearchcriteriaFields,
WPQueryWrapper $pWPQueryWrapper)
{
$this->_pSDKWrapper = $pSDKWrapper;
$this->_pFormAddressCreator = $pFormAddressCreator;
$this->_pSearchcriteriaFields = $pSearchcriteriaFields;
$this->_pWPQueryWrapper = $pWPQueryWrapper;
}


Expand Down Expand Up @@ -121,4 +128,15 @@ public function setNewsletterAccepted( bool $newsletterAccepted )
{
$this->_newsletterAccepted = $newsletterAccepted;
}

/**
*
* @return WPQueryWrapper
*
*/

public function getWPQueryWrapper(): WPQueryWrapper
{
return $this->_pWPQueryWrapper;
}
}
10 changes: 10 additions & 0 deletions plugin/Form/FormPostOwnerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use onOffice\WPlugin\Controller\InputVariableReader;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\WP\WPQueryWrapper;

/**
*
Expand Down Expand Up @@ -73,4 +74,13 @@ public function getFormAddressCreator(): FormAddressCreator;
*
*/
public function getNewsletterAccepted(): bool;


/**
*
* @return WPQueryWrapper
*
*/

public function getWPQueryWrapper(): WPQueryWrapper;
}
19 changes: 18 additions & 1 deletion plugin/Form/FormPostOwnerConfigurationDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use onOffice\SDK\onOfficeSDK;
use onOffice\WPlugin\Controller\InputVariableReader;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\WP\WPQueryWrapper;

/**
*
Expand All @@ -40,18 +41,22 @@ class FormPostOwnerConfigurationDefault
/** @var FormAddressCreator */
private $_pFormAddressCreator;

/** @var WPQueryWrapper */
private $_pWPQueryWrapper = null;

/**
*
* @param SDKWrapper $pSDKWrapper
* @param FormAddressCreator $pFormAddressCreator
* @param WPQueryWrapper $pWPQueryWrapper
*
*/

public function __construct(SDKWrapper $pSDKWrapper, FormAddressCreator $pFormAddressCreator)
public function __construct(SDKWrapper $pSDKWrapper, FormAddressCreator $pFormAddressCreator, WPQueryWrapper $pWPQueryWrapper)
{
$this->_pSDKWrapper = $pSDKWrapper;
$this->_pFormAddressCreator = $pFormAddressCreator;
$this->_pWPQueryWrapper = $pWPQueryWrapper;
}


Expand Down Expand Up @@ -110,4 +115,16 @@ public function getNewsletterAccepted(): bool
{
return filter_var( $_POST['newsletter'] ?? null, FILTER_SANITIZE_STRING ) === 'y';
}


/**
*
* @return WPQueryWrapper
*
*/

public function getWPQueryWrapper(): WPQueryWrapper
{
return $this->_pWPQueryWrapper;
}
}
21 changes: 20 additions & 1 deletion plugin/Form/FormPostOwnerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use onOffice\WPlugin\Controller\InputVariableReader;
use onOffice\WPlugin\Controller\InputVariableReaderConfigTest;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\WP\WPQueryWrapper;

/**
*
Expand All @@ -47,20 +48,26 @@ class FormPostOwnerConfigurationTest
/** @var bool */
private $_newsletterAccepted = false;

/** @var WPQueryWrapper */
private $_pWPQueryWrapper;

/**
* @param SDKWrapper $pSDKWrapper
* @param FormAddressCreator $pFormAddressCreator
* @param InputVariableReaderConfigTest $pInputVariableReaderConfigTest
* @param WPQueryWrapper $pWPQueryWrapper
*/

public function __construct(
SDKWrapper $pSDKWrapper,
FormAddressCreator $pFormAddressCreator,
InputVariableReaderConfigTest $pInputVariableReaderConfigTest)
InputVariableReaderConfigTest $pInputVariableReaderConfigTest,
WPQueryWrapper $pWPQueryWrapper)
{
$this->_pInputVariableReaderConfigTest = $pInputVariableReaderConfigTest;
$this->_pFormAddressCreator = $pFormAddressCreator;
$this->_pSDKWrapper = $pSDKWrapper;
$this->_pWPQueryWrapper = $pWPQueryWrapper;
}


Expand Down Expand Up @@ -133,4 +140,16 @@ public function setNewsletterAccepted( bool $newsletterAccepted )
{
$this->_newsletterAccepted = $newsletterAccepted;
}


/**
*
* @return WPQueryWrapper
*
*/

public function getWPQueryWrapper(): WPQueryWrapper
{
return $this->_pWPQueryWrapper;
}
}
4 changes: 3 additions & 1 deletion plugin/FormPostContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ private function createAddress(FormData $pFormData)
$pFormConfig = $pFormData->getDataFormConfiguration();
$checkDuplicate = $pFormConfig->getCheckDuplicateOnCreateAddress();
$contactType = $pFormConfig->getContactType();
$pWPQuery = $this->_pFormPostContactConfiguration->getWPQueryWrapper()->getWPQuery();
$estateId = $pWPQuery->get('estate_id', null);
$addressId = $this->_pFormPostContactConfiguration->getFormAddressCreator()
->createOrCompleteAddress($pFormData, $checkDuplicate, $contactType);
->createOrCompleteAddress($pFormData, $checkDuplicate, $contactType, $estateId);

if (!$this->_pFormPostContactConfiguration->getNewsletterAccepted()) {
// No subscription for newsletter, which is ok
Expand Down
5 changes: 3 additions & 2 deletions plugin/FormPostInterest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ protected function analyseFormContentByPrefix(FormData $pFormData)
if ( $pFormConfiguration->getCreateInterest() ) {
$checkduplicate = $pFormConfiguration->getCheckDuplicateOnCreateAddress();
$contactType = $pFormConfiguration->getContactType();
$pWPQuery = $this->_pFormPostInterestConfiguration->getWPQueryWrapper()->getWPQuery();
$estateId = $pWPQuery->get('estate_id', null);
$addressId = $this->_pFormPostInterestConfiguration->getFormAddressCreator()
->createOrCompleteAddress( $pFormData,
$checkduplicate, $contactType);
->createOrCompleteAddress( $pFormData, $checkduplicate, $contactType, $estateId);
$this->createSearchcriteria( $pFormData, $addressId );
$this->setNewsletter( $addressId );
}
Expand Down
6 changes: 4 additions & 2 deletions plugin/FormPostOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ protected function analyseFormContentByPrefix(FormData $pFormData)
if ( $pDataFormConfiguration->getCreateOwner() ) {
$checkduplicate = $pDataFormConfiguration->getCheckDuplicateOnCreateAddress();
$contactType = $pDataFormConfiguration->getContactType();
$pWPQuery = $this->_pFormPostOwnerConfiguration->getWPQueryWrapper()->getWPQuery();
$estateId = $pWPQuery->get('estate_id', null);
$addressId = $this->_pFormPostOwnerConfiguration->getFormAddressCreator()
->createOrCompleteAddress( $pFormData,
$checkduplicate, $contactType);
->createOrCompleteAddress($pFormData, $checkduplicate, $contactType, $estateId);
$estateData = $this->getEstateData();
$estateId = $this->createEstate( $estateData );
$this->createOwnerRelation( $estateId, $addressId );
$this->setNewsletter( $addressId );
Expand Down
Loading
Loading