Skip to content

Commit

Permalink
#4148 Build-in API support: forms as API, SEO routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Trofimov committed Dec 13, 2022
1 parent 2a6920e commit 17ec67c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
47 changes: 32 additions & 15 deletions inc/classes/BxDolPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,13 @@ static public function deleteSeoLinkByModule ($sModule)
}

/**
* Process SEO links. It takes request part from SEO link and process it
* to make it work as regular page link
* Get page object by SEO link. It takes request part from SEO link and returns page object
* @param $sRequest request URI with SEO link
* @return true - if page was found and processed correctly, false - if page wasn't found
* @return false - if page wasn't found,
* page object - on success,
* string URL - if permanent redirect is needed
*/
static public function processSeoLink ($sRequest)
static public function getPageBySeoLink ($sRequest)
{
if (!$sRequest || '/' === $sRequest || !getParam('permalinks_seo_links'))
return false;
Expand All @@ -357,8 +358,7 @@ static public function processSeoLink ($sRequest)
if ('/' === $sRequest[-1] || $sRequest != mb_strtolower($sRequest)) {
unset($_GET['_q']);
$sUrl = BX_DOL_URL_ROOT . bx_append_url_params(mb_strtolower(trim($sRequest, '/')), $_GET);
header('Location:' . $sUrl, true, 301);
exit;
return $sUrl;
}

// parse URL
Expand All @@ -372,8 +372,7 @@ static public function processSeoLink ($sRequest)
unset($_GET['_q']);
$a[0] = $aSeoUriRewrites[$a[0]];
$sUrl = BX_DOL_URL_ROOT . bx_append_url_params(mb_strtolower(implode('/', $a)), $_GET);
header('Location:' . $sUrl, true, 301);
exit;
return $sUrl;
}

// check page URI rewrite
Expand Down Expand Up @@ -403,18 +402,36 @@ static public function processSeoLink ($sRequest)
$sLink = substr($sLink, strlen(BX_DOL_URL_ROOT));

$s = BxDolPage::multisiteLinkCheck ('', $a[0], $aPage['module'], $_GET);
if (false !== $s) {
header('Location:' . $s . $sLink, true, 301);
exit;
}
if (false !== $s)
return $s . $sLink;
}

// display page
$_REQUEST['i'] = $_GET['i'] = $a[0];
$oPage = BxDolPage::getObjectInstanceByURI($a[0], false, true);
$oPage->displayPage();

return true;
return $oPage;
}

/**
* Process SEO links. It takes request part from SEO link and process it
* to make it work as regular page link
* @param $sRequest request URI with SEO link
* @return true - if page was found and processed correctly, false - if page wasn't found
*/
static public function processSeoLink ($sRequest)
{
$mixed = self::getPageBySeoLink($sRequest);
if (($sUrl = $mixed) && is_string($sUrl)) {
header('Location:' . $sUrl, true, 301);
exit;
}
elseif (($oPage = $mixed) && is_object($oPage)) {
$oPage->displayPage();
return true;
}
else {
return false;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/boonex/contact/classes/BxContactModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function serviceGetBlockForm()

if (defined('BX_API'))
return [
['id' => 1, 'type' => 'form', 'data' => $oForm->getCode(), 'request' => ['url' => '/api.php?r=bx_contact/get_block_form', 'immutable' => true]],
['id' => 1, 'type' => 'form', 'data' => $oForm->getCodeAPI(), 'request' => ['url' => '/api.php?r=bx_contact/get_block_form', 'immutable' => true]],
['id' => 2, 'type' => 'msg', 'data' => $sResult],
];

Expand Down
12 changes: 9 additions & 3 deletions template/scripts/BxBaseFormView.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,6 @@ function getCode($bDynamicMode = false)
'include' => &$sInclude
));

if (defined('BX_API'))
return ['inputs' => $this->aInputs, 'attrs' => $this->aFormAttrs, 'params' => $this->aParams];

if($this->sCode === false)
$this->sCode = $this->genForm();

Expand All @@ -341,6 +338,15 @@ function getCode($bDynamicMode = false)
return $sInclude . $sDynamicCssJs . $this->sCode;
}

function getCodeAPI()
{
$this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);

// TODO: process inputs to translate titles, alerts, etc

return ['inputs' => $this->aInputs, 'attrs' => $this->aFormAttrs, 'params' => $this->aParams];
}

public function getJsClassName()
{
return $this->_sJsClassName;
Expand Down
44 changes: 39 additions & 5 deletions template/scripts/BxBaseServicePages.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,54 @@ public function __construct()
* @page service Service Calls
* @section bx_system_pages System Services
* @subsection bx_system_pages-pages Pages
* @subsubsection bx_system_pages-get_page Get page
* @subsubsection bx_system_pages-get_page_by_request Get page by page URI
*
* @code bx_srv('system', 'get_page', ['about'], 'TemplServicePages'); @endcode
* @code {{~system:get_page:TemplServicePages['about']~}} @endcode
* @code bx_srv('system', 'get_page_by_request', ['about'], 'TemplServicePages'); @endcode
* @code {{~system:get_page_by_request:TemplServicePages['about']~}} @endcode
*
* Test method which page data
* @param $sURI page URI
*
* @see BxBaseServicePages::serviceGetPage
*/
/**
* @ref bx_system_general-get_page "get_page"
* @ref bx_system_general-get_page_by_request "get_page_by_request"
*/
public function serviceGetPage ($sURI)
public function serviceGetPageByRequest ($sRequest)
{
$mixed = BxDolPage::getPageBySeoLink($sRequest);

if (($sUrl = $mixed) && is_string($sUrl)) {
$aRes = ['redirect' => $sUrl];
}
elseif (($oPage = $mixed) && is_object($oPage)) {
return $oPage->getPage();
}
else {
$aRes = ['error' => _t("_sys_request_page_not_found_cpt")];
}

return $aRes;
}

/**
* @page service Service Calls
* @section bx_system_pages System Services
* @subsection bx_system_pages-pages Pages
* @subsubsection bx_system_pages-get_page_by_uri Get page by page URI
*
* @code bx_srv('system', 'get_page_by_uri', ['about'], 'TemplServicePages'); @endcode
* @code {{~system:get_page_by_uri:TemplServicePages['about']~}} @endcode
*
* Test method which page data
* @param $sURI page URI
*
* @see BxBaseServicePages::serviceGetPage
*/
/**
* @ref bx_system_general-get_page_by_uri "get_page_by_uri"
*/
public function serviceGetPageByUri ($sURI)
{
$oPage = BxDolPage::getObjectInstanceByURI($sURI, false, true);
if (!$oPage) {
Expand Down

0 comments on commit 17ec67c

Please sign in to comment.