Skip to content

Commit

Permalink
feat:138-prestashop-integration (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
OdapX authored Sep 6, 2023
1 parent cc6270e commit 0e0320d
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build:widget:chat-bubble": "NEXT_PUBLIC_DASHBOARD_URL=https://app.chaindesk.ai npx parcel build widgets/chat-bubble.tsx",
"build:widget:chat-bubble:dev": "NEXT_PUBLIC_DASHBOARD_URL=http://localhost:3000 npx parcel build widgets/chat-bubble.tsx",
"publish:package:chat-bubble": "npm run build:widget:chat-bubble && cp dist/chat-bubble* packages/chat-bubble/ && cd packages/chat-bubble && npm version patch && npm publish --access public",
"build:widget:prestashop": "cd widgets/prestashop && bash build.sh",
"prisma:generate": "prisma generate",
"prisma:migrate:dev": "dotenv -e .env.local -- prisma migrate dev",
"prisma:migrate:prod": "dotenv -e .env.local -- prisma migrate deploy",
Expand Down
2 changes: 1 addition & 1 deletion pages/api/integrations/[name]/config-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const updateIntegrationConfig = async (
) => {
const session = req.session;
const data = req.body as z.infer<typeof schema>;
const name = req.query.name as 'wordpress' | 'shopify';
const name = req.query.name as 'wordpress' | 'shopify' | 'prestashop';

const agent = await prisma.agent.findUnique({
where: {
Expand Down
1 change: 1 addition & 0 deletions widgets/prestashop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chaindesk.zip
6 changes: 6 additions & 0 deletions widgets/prestashop/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
rm -rf chaindesk chaindesk.zip && \
mkdir -p chaindesk && \
cp -r chaindesk.php controllers docker-compose.yml entrypoint.sh index.php logo.png views chaindesk && \
zip -r chaindesk.zip chaindesk && \
rm -rf chaindesk
103 changes: 103 additions & 0 deletions widgets/prestashop/chaindesk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* chaindesk.php
*
* This file is the main entry point for the ChainDesk module in PrestaShop.
* It initializes and configures the module.
*
* @author chaindesk
* @copyright chaindesk
* @license MIT License
*/

if (!defined('_PS_VERSION_')) {
exit;
}

class Chaindesk extends Module
{
public function __construct()
{
$this->name = 'chaindesk';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'chaindesk';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => '8.99.99',
];
$this->bootstrap = true;

parent::__construct();

$this->displayName = $this->l('chaindesk');
$this->description = $this->l('chaindesk.');

$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

if (!Configuration::get('MYMODULE_NAME')) {
$this->warning = $this->l('No name provided');
}
}

public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}

return parent::install() &&
$this->registerHooks() &&
$this->installTab() &&
Configuration::updateValue('MYMODULE_NAME', 'chaindesk');
}

private function registerHooks()
{
return $this->registerHook('displayHeader') && $this->registerHook('hookHeader');
}

public function installTab()
{
$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'AdminChainDesk';

// Add a blank line before the following line to adhere to the rule
$tab->name = [];

foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'Chaindesk';
}

$tab->id_parent = (int) Tab::getIdFromClassName('AdminAdmin');
$tab->module = $this->name;
return $tab->add();
}

public function uninstall()
{
return parent::uninstall();
}

public function hookHeader()
{
// Sanitize and validate the agent ID.
$agentId = Configuration::get('CHAINDESK_AGENT_ID');
$agentId = Tools::safeOutput($agentId);

if (!empty($agentId) && !Tools::isSubmit('chaindesk_script_enqueued')) {
$output = '<script
data-cfasync="false"
data-name="databerry-chat-bubble"
id="' . $agentId . '"
src="https://cdn.jsdelivr.net/npm/@databerry/chat-bubble@latest"
></script>';

$output .= '<script>var chaindesk_script_enqueued = true;</script>';

return $output;
}
}
}
63 changes: 63 additions & 0 deletions widgets/prestashop/controllers/admin/AdminChainDeskController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* AdminChainDeskController.php
*
* This file contains the AdminChainDeskController class, which is responsible for handling
* administrative functions related to the ChainDesk module in PrestaShop.
*
* @author chaindesk
* @copyright chaindesk
* @license MIT License
*/

if (!defined('_PS_VERSION_')) {
exit;
}

class AdminChainDeskController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
}

public function initContent()
{
$agentId = $this->getAgentIdFromUrl();

$this->assignTemplateData($agentId);

$this->content .= $this->context->smarty->fetch(
$this->getTemplatePath() . 'chaindesk.tpl'
);

parent::initContent();
}

private function getAgentIdFromUrl()
{
if (Tools::getIsset('agentId')) {
$sanitizedAgentId = pSQL(Tools::getValue('agentId'));
Configuration::updateValue('CHAINDESK_AGENT_ID', $sanitizedAgentId);
return $sanitizedAgentId;
}

return Configuration::get('CHAINDESK_AGENT_ID');
}

private function assignTemplateData($agentId)
{
$this->context->smarty->assign([
'add_to_chaindesk_link' => $this->generateChainDeskLink(),
'agentId' => $agentId,
]);
}

private function generateChainDeskLink()
{
$agentId = Configuration::get('CHAINDESK_AGENT_ID');
$http_callback = Tools::getShopDomainSsl(true, true) . $_SERVER['REQUEST_URI'];
$base_url = 'https://app.chaindesk.ai'; // Use single quotes for simple strings
return $base_url . '/integrations/prestashop/config?callback=' . urlencode($http_callback) . '&siteurl=' . urlencode(Tools::getShopDomainSsl(true, true)) . '&agentId=' . $agentId;
}
}
1 change: 1 addition & 0 deletions widgets/prestashop/controllers/admin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
?>
1 change: 1 addition & 0 deletions widgets/prestashop/controllers/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
?>
36 changes: 36 additions & 0 deletions widgets/prestashop/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.1"
services:
mysql:
container_name: chaindesk_prestashop_db
image: mysql:5.7
volumes:
- db:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: prestashop
networks:
- prestashop_network
prestashop:
container_name: prestashop
image: prestashop/prestashop
restart: unless-stopped
depends_on:
- mysql
ports:
- 8080:80
environment:
DB_SERVER: chaindesk_prestashop_db
DB_NAME: prestashop
DB_USER: root
DB_PASSWD: admin
volumes:
- prestashop:/var/www/html
- .:/var/www/html/modules/chaindesk
networks:
- prestashop_network
volumes:
db:
prestashop:
networks:
prestashop_network:
1 change: 1 addition & 0 deletions widgets/prestashop/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
?>
Binary file added widgets/prestashop/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
25 changes: 25 additions & 0 deletions widgets/prestashop/views/templates/admin/chaindesk.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{*
* chaindesk.tpl
*
* This template file is used for displaying Chaindesk-related content in PrestaShop's admin area.
*
* @author chaindesk
* @copyright chaindesk
* @license MIT License
*}

{if $agentId}
<div class="bootstrap container">
<div class="alert alert-success text-center mt-2">
<h2>{l s='Connected with Chaindesk.' d='chaindesk'}</h2>
<p>{l s='You can now use Chaindesk from your homepage.' d='chaindesk'}</p>
<a href="{$add_to_chaindesk_link}" class="btn btn-primary pointer ">{l s='Reconfigure' d='chaindesk'}</a>
</div>
{else}
<div class="alert alert-info">
<h2>{l s='Connect with Chaindesk.ai' d='chaindesk'}</h2>
<p>{l s='This link will redirect you to Chaindesk and configure your PrestaShop.' d='chaindesk'}</p>
<a href="{$add_to_chaindesk_link}" class="btn btn-primary">{l s='Connect with Chaindesk' d='chaindesk'}</a>
</div>
</div>
{/if}
Empty file.
Empty file.

1 comment on commit 0e0320d

@vercel
Copy link

@vercel vercel bot commented on 0e0320d Sep 6, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.