Skip to content

Commit

Permalink
2.1.0
Browse files Browse the repository at this point in the history
Plugin becomes a service provider
  • Loading branch information
conseilgouz committed Mar 10, 2024
1 parent 5293def commit 24e826a
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 17 deletions.
12 changes: 6 additions & 6 deletions cglike.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<extension version="4.0" type="plugin" group="ajax" method="upgrade">
<name>Ajax - CG Like plugin</name>
<version>2.0.4</version>
<creationDate>2023-10-13</creationDate>
<version>2.1.0</version>
<creationDate>2024-03-10</creationDate>
<author>ConseilGouz</author>
<authorEmail>pascal.leconte@conseilgouz.com</authorEmail>
<authorUrl>https://www.conseilgouz.com</authorUrl>
<license>GNU General Public License version 3 or later</license>
<copyright>Copyright (C) 2023 ConseilGouz. All rights reserved.</copyright>
<copyright>Copyright (C) 2024 ConseilGouz. All rights reserved.</copyright>
<description>Joomla Ajax Like Plugin</description>

<namespace path="src">ConseilGouz\Plugin\Ajax\Cglike</namespace>
<files>
<filename plugin="cglike">cglike.php</filename>
<filename >cglike.xml</filename>
<folder plugin="cglike">services</folder>
<folder>src</folder>
<filename>index.html</filename>
</files>
<languages>
Expand Down
12 changes: 12 additions & 0 deletions plg_cg_like_ajax_changelog.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<changelogs>
<changelog>
<element>cg_like</element>
<type>plugin</type>
<group>ajax</group>
<version>2.1.0</version>
<note>
<item>10/03/2023</item>
</note>
<change>
<item>Plugin becomes a service provider</item>
</change>
</changelog>
<changelog>
<element>cg_like</element>
<type>plugin</type>
Expand Down
4 changes: 4 additions & 0 deletions services/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<body>
</body>
</html>
47 changes: 47 additions & 0 deletions services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* File cg_like_ajax.php for Joomla 4.x/5.x
* Author ConseilGouz
* Support https://www.conseilgouz.com
* Copyright Copyright (C) 2024 ConseilGouz. All Rights Reserved.
* License GNU GPL v3 or later
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Database\DatabaseInterface;
use ConseilGouz\Plugin\Ajax\Cglike\Extension\Cglike;

return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since 4.2.0
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container) {
$displatcher = $container->get(DispatcherInterface::class);
$plugin = new Cglike(
$displatcher,
(array) PluginHelper::getPlugin('ajax', 'cglike')
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));
return $plugin;
}
);
}
};
33 changes: 22 additions & 11 deletions cglike.php → src/Extension/Cglike.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
<?php
defined('_JEXEC') or die;
/**
* File cg_like_ajax.php for Joomla 4.x/5.x
* Author ConseilGouz
* Support https://www.conseilgouz.com
* Copyright Copyright (C) 2023 ConseilGouz. All Rights Reserved.
* Copyright Copyright (C) 2024 ConseilGouz. All Rights Reserved.
* License GNU GPL v3 or later
*/
namespace ConseilGouz\Plugin\Ajax\Cglike\Extension;

defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;
use Joomla\Event\SubscriberInterface;
use Joomla\Database\DatabaseAwareTrait;

class plgAjaxCGLike extends CMSPlugin
class Cglike extends CMSPlugin implements SubscriberInterface
{
private $min_php_version = '7.4';
use DatabaseAwareTrait;

protected $autoloadLanguage = true;

function onAjaxCglike() {

public static function getSubscribedEvents(): array
{
return [
'onAjaxCglike' => 'goAjax',
];
}
function goAjax($event) {
$input = Factory::getApplication()->input;
$id = $input->get('id', '', 'integer');
$out = "";
if (!self::cookie($id)) {// cookie exist => exit
$out .='{"ret":"9","msg":"'.Text::_("CG_AJAX_ALREADY").'"}';
return $out;
return $event->addResult($out);
}
$plugin = PluginHelper::getPlugin('content', 'cglike');
$params = new Registry($plugin->params);
self::setcookie($id,$params);
if (!self::addOne($id)) {
$out .= '{"ret":"9","msg":"'.Text::_("CG_AJAX_SQL_ERROR").'"}';
return $out;
return $event->addResult($out);
}
$count = self::countId($id);
$out .='{"ret":"0","msg":"'.Text::_("CG_AJAX_THANKS").'","cnt":"'.$count.'"}';
return $out;
return $event->addResult($out);
}
function cookie($id) {
$jinput = Factory::getApplication()->input;
Expand Down Expand Up @@ -73,7 +84,7 @@ function setcookie($id,$params) {
}
}
function addOne($id) {
$db = Factory::getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->insert('#__cg_like');
$query->set('cid = '.$db->quote($id));
Expand All @@ -85,7 +96,7 @@ function addOne($id) {
return true;
}
function countId($id) {
$db = Factory::getDbo();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query ->select( 'COUNT(id)')
->from($db->quoteName('#__cg_like'))
Expand Down
4 changes: 4 additions & 0 deletions src/Extension/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<body>
</body>
</html>
75 changes: 75 additions & 0 deletions src/Field/VersionField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* File cg_like_ajax.php for Joomla 4.x/5.x
* Author ConseilGouz
* Support https://www.conseilgouz.com
* Copyright Copyright (C) 2024 ConseilGouz. All Rights Reserved.
* License GNU GPL v3 or later
*/
namespace ConseilGouz\Plugin\Ajax\Cglike\Field;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Language\Text;
use Joomla\String\StringHelper;

// Prevent direct access
defined('_JEXEC') || die;

class VersionField extends FormField
{
/**
* Element name
*
* @var string
*/
protected $_name = 'Version';
protected $def;

function getInput()
{
$return = '';
// Load language
$extension = $this->def('extension');

$version = '';

$jinput = Factory::getApplication()->input;
$db = Factory::getDBO();
$query = $db->getQuery(true);
$query
->select($db->quoteName('manifest_cache'))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('element') . '=' . $db->Quote($extension));
$db->setQuery($query, 0, 1);
$row = $db->loadAssoc();
$tmp = json_decode($row['manifest_cache']);
$version = $tmp->version;

$document = Factory::getDocument();
$css = '';
$css .= ".version {display:block;text-align:right;color:brown;font-size:10px;}";
$css .= ".readonly.plg-desc {font-weight:normal;}";
$css .= "fieldset.radio label {width:auto;}";
$document->addStyleDeclaration($css);
$margintop = $this->def('margintop');
if (StringHelper::strlen($margintop)) {
$js = "document.addEventListener('DOMContentLoaded', function() {
vers = document.querySelector('.version');
parent = vers.parentElement.parentElement;
parent.style.marginTop = '".$margintop."';
})";
$document->addScriptDeclaration($js);
}
$return .= '<span class="version">' . Text::_('JVERSION') . ' ' . $version . "</span>";

return $return;

}
public function def($val, $default = '')
{
return ( isset( $this->element[$val] ) && (string) $this->element[$val] != '' ) ? (string) $this->element[$val] : $default;
}

}
1 change: 1 addition & 0 deletions src/Field/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><body></body></html>
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<body>
</body>
</html>

0 comments on commit 24e826a

Please sign in to comment.