Skip to content
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
32 changes: 32 additions & 0 deletions backend/Actions/AsgarosForum/AsgarosForumController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace BitApps\Integrations\Actions\AsgarosForum;

/**
* Provide functionality for Asgaros Forum integration.
*/
class AsgarosForumController
{
public static function asgarosForumAuthorize()
{
self::checkPluginExists();
wp_send_json_success(true);
}

public function execute($integrationData, $fieldValues)
{
$integrationDetails = $integrationData->flow_details;
$fieldMap = $integrationDetails->field_map ?? [];

$recordApiHelper = new RecordApiHelper($integrationDetails, $integrationData->id);

return $recordApiHelper->execute($fieldValues, $fieldMap);
}

private static function checkPluginExists()
{
if (!class_exists('AsgarosForum')) {
wp_send_json_error(__('Asgaros Forum is not activated or not installed', 'bit-integrations'), 400);
}
}
}
111 changes: 111 additions & 0 deletions backend/Actions/AsgarosForum/RecordApiHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace BitApps\Integrations\Actions\AsgarosForum;

use BitApps\Integrations\Config;
use BitApps\Integrations\Core\Util\Common;
use BitApps\Integrations\Core\Util\Hooks;
use BitApps\Integrations\Log\LogHandler;

/**
* Provide functionality for Asgaros Forum record operations.
*/
class RecordApiHelper
{
private $_integrationID;

private $_integrationDetails;

public function __construct($integrationDetails, $integId)
{
$this->_integrationDetails = $integrationDetails;
$this->_integrationID = $integId;
}

public function execute($fieldValues, $fieldMap)
{
if (!class_exists('AsgarosForum')) {
$response = [
'success' => false,
'message' => __('Asgaros Forum is not installed or activated', 'bit-integrations'),
];

LogHandler::save($this->_integrationID, ['type' => 'Asgaros Forum', 'type_name' => 'check'], 'error', $response);

return $response;
}

$mainAction = $this->_integrationDetails->mainAction ?? '';
$payload = $this->generateReqDataFromFieldMap($fieldMap, $fieldValues);

$defaultResponse = [
'success' => false,
// translators: %s is the plugin name.
'message' => wp_sprintf(__('%s plugin is not installed or activated', 'bit-integrations'), 'Bit Integrations Pro'),
];

switch ($mainAction) {
case 'create_topic':
$response = Hooks::apply(Config::withPrefix('asgaros_forum_create_topic'), $defaultResponse, $payload);
$actionType = 'create_topic';

break;

case 'create_forum':
$response = Hooks::apply(Config::withPrefix('asgaros_forum_create_forum'), $defaultResponse, $payload);
$actionType = 'create_forum';

break;

case 'post_reply_in_topic':
$response = Hooks::apply(Config::withPrefix('asgaros_forum_post_reply_in_topic'), $defaultResponse, $payload);
$actionType = 'post_reply_in_topic';

break;

case 'subscribe_user_in_forum':
$response = Hooks::apply(Config::withPrefix('asgaros_forum_subscribe_user_in_forum'), $defaultResponse, $payload);
$actionType = 'subscribe_user_in_forum';

break;

default:
$response = [
'success' => false,
'message' => __('Invalid action', 'bit-integrations'),
];
$actionType = 'unknown';

break;
}

$responseType = !empty($response['success']) ? 'success' : 'error';
LogHandler::save($this->_integrationID, ['type' => 'Asgaros Forum', 'type_name' => $actionType], $responseType, $response);

return $response;
}

private function generateReqDataFromFieldMap($fieldMap, $fieldValues)
{
$dataFinal = [];

if (!\is_array($fieldMap)) {
return $dataFinal;
}

foreach ($fieldMap as $item) {
$triggerValue = $item->formField ?? '';
$actionValue = $item->asgarosForumField ?? '';

if (empty($actionValue)) {
continue;
}

$dataFinal[$actionValue] = $triggerValue === 'custom' && isset($item->customValue)
? Common::replaceFieldWithValue($item->customValue, $fieldValues)
: ($fieldValues[$triggerValue] ?? '');
}

return $dataFinal;
}
}
6 changes: 6 additions & 0 deletions backend/Actions/AsgarosForum/Routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use BitApps\Integrations\Actions\AsgarosForum\AsgarosForumController;
use BitApps\Integrations\Core\Util\Route;

Route::post('asgaros_forum_authorize', [AsgarosForumController::class, 'asgarosForumAuthorize']);
106 changes: 106 additions & 0 deletions frontend/src/components/AllIntegrations/AsgarosForum/AsgarosForum.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { useState } from 'react'
import 'react-multiple-select-dropdown-lite/dist/index.css'
import { useNavigate, useParams } from 'react-router'
import BackIcn from '../../../Icons/BackIcn'
import { __ } from '../../../Utils/i18nwrap'
import SnackMsg from '../../Utilities/SnackMsg'
import { saveIntegConfig } from '../IntegrationHelpers/IntegrationHelpers'
import IntegrationStepThree from '../IntegrationHelpers/IntegrationStepThree'
import AsgarosForumAuthorization from './AsgarosForumAuthorization'
import { checkMappedFields } from './AsgarosForumCommonFunc'
import AsgarosForumIntegLayout from './AsgarosForumIntegLayout'

export default function AsgarosForum({ formFields, setFlow, flow, allIntegURL }) {
const navigate = useNavigate()
const { formID } = useParams()
const [isLoading, setIsLoading] = useState(false)
const [step, setStep] = useState(1)
const [snack, setSnackbar] = useState({ show: false })
const [asgarosForumConf, setAsgarosForumConf] = useState({
name: 'Asgaros Forum',
type: 'Asgaros Forum',
field_map: [],
asgarosForumFields: [],
actions: {},
mainAction: ''
})

const nextPage = val => {
setTimeout(() => {
const settingsWrapper = document.getElementById('btcd-settings-wrp')
if (settingsWrapper) settingsWrapper.scrollTop = 0
}, 300)

if (val === 3) {
if (!checkMappedFields(asgarosForumConf)) {
setSnackbar({
show: true,
msg: __('Please complete all required fields to continue.', 'bit-integrations')
})
return
}

if (asgarosForumConf.name !== '') {
setStep(val)
}
return
}

setStep(val)
}

return (
<div>
<SnackMsg snack={snack} setSnackbar={setSnackbar} />
<div className="txt-center mt-2" />

<AsgarosForumAuthorization
formID={formID}
asgarosForumConf={asgarosForumConf}
setAsgarosForumConf={setAsgarosForumConf}
step={step}
nextPage={nextPage}
isLoading={isLoading}
setIsLoading={setIsLoading}
setSnackbar={setSnackbar}
/>

<div
className="btcd-stp-page"
style={{
width: step === 2 && 900,
height: step === 2 && 'auto',
minHeight: step === 2 && `${500}px`
}}>
<AsgarosForumIntegLayout
formID={formID}
formFields={formFields}
asgarosForumConf={asgarosForumConf}
setAsgarosForumConf={setAsgarosForumConf}
setSnackbar={setSnackbar}
setIsLoading={setIsLoading}
isLoading={isLoading}
/>
<br />
<br />
<br />
<button
onClick={() => nextPage(3)}
disabled={!asgarosForumConf.mainAction || !checkMappedFields(asgarosForumConf)}
className="btn f-right btcd-btn-lg purple sh-sm flx"
type="button">
{__('Next', 'bit-integrations')}
<BackIcn className="ml-1 rev-icn" />
</button>
</div>

<IntegrationStepThree
step={step}
saveConfig={() =>
saveIntegConfig(flow, setFlow, allIntegURL, asgarosForumConf, navigate, '', '', setIsLoading)
}
isLoading={isLoading}
/>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useState } from 'react'
import { __ } from '../../../Utils/i18nwrap'
import LoaderSm from '../../Loaders/LoaderSm'
import Note from '../../Utilities/Note'
import { asgarosForumAuthentication } from './AsgarosForumCommonFunc'

export default function AsgarosForumAuthorization({
asgarosForumConf,
setAsgarosForumConf,
step,
nextPage,
isLoading,
setIsLoading,
isInfo
}) {
const [isAuthorized, setIsAuthorized] = useState(false)
const [error, setError] = useState({ name: '' })

const handleInput = e => {
const newConf = { ...asgarosForumConf }
newConf[e.target.name] = e.target.value
setAsgarosForumConf(newConf)
}

return (
<div
className="btcd-stp-page"
style={{ ...{ width: step === 1 && 900 }, ...{ height: step === 1 && 'auto' } }}>
<div className="mt-3">
<b>{__('Integration Name:', 'bit-integrations')}</b>
</div>
<input
className="btcd-paper-inp w-6 mt-1"
onChange={handleInput}
name="name"
value={asgarosForumConf.name}
type="text"
placeholder={__('Integration Name...', 'bit-integrations')}
disabled={isInfo}
/>
<div style={{ color: 'red', fontSize: '15px' }}>{error.name}</div>

<Note
note={__(
'To use Asgaros Forum integration, make sure the Asgaros Forum plugin is installed and active on your site.',
'bit-integrations'
)}
/>

{!isInfo && (
<>
<button
onClick={() =>
asgarosForumAuthentication(
asgarosForumConf,
setAsgarosForumConf,
setError,
setIsAuthorized,
setIsLoading
)
}
className="btn btcd-btn-lg purple sh-sm flx"
type="button"
disabled={isAuthorized || isLoading}>
{isAuthorized ? __('Authorized ✔', 'bit-integrations') : __('Authorize', 'bit-integrations')}
{isLoading && <LoaderSm size={20} clr="#022217" className="ml-2" />}
</button>
<br />
<button
onClick={() => nextPage(2)}
className="btn f-right btcd-btn-lg purple sh-sm flx"
type="button"
disabled={!isAuthorized}>
{__('Next', 'bit-integrations')}
<div className="btcd-icn icn-arrow_back rev-icn d-in-b" />
</button>
</>
)}
</div>
)
}
Loading
Loading