Skip to content

Commit

Permalink
Basic working module created.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjnewbs committed Jun 15, 2018
0 parents commit 122ce24
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Block/FacebookChat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Newbury\FacebookChat\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Config\ScopeConfigInterface;;
use Magento\Framework\Locale\Resolver;

class FacebookChat extends Template
{
const CONFIG_PATH_PAGE_ID = 'newbury/facebookchat/page_id';

/**
* @var ScopeConfigInterface
*/
protected $config;

/**
* @var Resolver
*/
protected $localeResolver;

/**
* FacebookChat constructor.
* @param Template\Context $context
* @param ScopeConfigInterface $config
* @param Resolver $resolver
* @param array $data
*/
public function __construct(
Template\Context $context,
ScopeConfigInterface $config,
Resolver $resolver,
array $data = []
) {
parent::__construct($context, $data);
$this->config = $config;
$this->localeResolver = $resolver;
}

/**
* @return mixed
*/
public function getPageId()
{
return $this->config->getValue(self::CONFIG_PATH_PAGE_ID);
}

/**
* @return string
*/
public function getLocale()
{
return $this->localeResolver->getLocale();
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Craig Newbury

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "newbury/module-facebook-chat",
"description": "Magento 2 module allowing the easy installation of the Facebook customer chat plugin.",
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"MIT"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Newbury\\FacebookChat\\": ""
}
},
"authors": [
{
"name": "Craig Newbury",
"email": "[email protected]",
"role": "Developer"
}
],
"support": {
"email": "[email protected]"
}
}
18 changes: 18 additions & 0 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="Newbury_Modules::group" title="Magento modules by Craig Newbury">
<resource id="Newbury_FacebookChat::config" title="Facebook Chat Configuration"/>
</resource>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
26 changes: 26 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="newbury" sortOrder="500" translate="label">
<label>Newbury Modules</label>
</tab>
<section id="newbury_facebookchat" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>Facebook Chat Configuration</label>
<tab>newbury</tab>
<resource>Newbury_FacebookChat::config</resource>
<group id="config" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>Facebook Chat Settings</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>newbury/facebookchat/enabled</config_path>
</field>

<field id="page_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Page ID</label>
<config_path>newbury/facebookchat/page_id</config_path>
</field>
</group>
</section>
</system>
</config>
10 changes: 10 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<newbury>
<facebookchat>
<enabled>0</enabled>
</facebookchat>
</newbury>
</default>
</config>
4 changes: 4 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Newbury_FacebookChat" setup_version="1.0.0"/>
</config>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Newbury_FacebookChat',
__DIR__
);
12 changes: 12 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block class="Newbury\FacebookChat\Block\FacebookChat"
template="Newbury_FacebookChat::FacebookChat.phtml"
name="newbury_facebookchat_chatcode"
after="-"
ifconfig="newbury/facebookchat/enabled" />
</referenceContainer>
</body>
</page>
14 changes: 14 additions & 0 deletions view/frontend/templates/FacebookChat.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php /** @var $block \Newbury\FacebookChat\Block\FacebookChat */ ?>
<?php if(empty($block->getPageId())) return; ?>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = 'https://connect.facebook.net/<?= $block->getLocale() ?>/sdk/xfbml.customerchat.js#xfbml=1&version=v2.12&autoLogAppEvents=1';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-customerchat"
page_id="<?= $block->getPageId() ?>">
</div>

0 comments on commit 122ce24

Please sign in to comment.