Skip to content

Commit

Permalink
Fixing admin: model and view
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiljevski committed Dec 30, 2021
1 parent 722c083 commit e0a821c
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 68 deletions.
2 changes: 1 addition & 1 deletion com_admirorgallery/admin/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function display($cachable = false, $urlparams = false)
}

AdmirorGalleryHelper::addSubmenu(
$this->input->get('view', 'control_panel'), $this->input->get('AG_resourceType', '')
$this->input->get('view', 'control_panel'), $this->input->get('resourceType', '')
);

$doc = JFactory::getDocument();
Expand Down
2 changes: 1 addition & 1 deletion com_admirorgallery/admin/controllers/admirorgallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function agApply(): void
$model = $this->getModel('admirorgallery');

// UPDATE
$model->_update();
$model->update();

parent::display();
}
Expand Down
13 changes: 9 additions & 4 deletions com_admirorgallery/admin/controllers/resourcemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ function ag_install()

if (isset($file) && !empty($file['name']))
{
$this->model->_install($file);
$resourceType = $this->model->input->getVar('resourceType');

// Trim trailing directory separator
$resourceType = substr($resourceType, 0, strlen($resourceType) - 1);

$this->model->installResource($file, $resourceType, "zip", JFactory::getConfig()->get('tmp_path'));
}
else
{
Expand All @@ -48,11 +53,11 @@ function ag_install()

function ag_uninstall()
{
$ag_cidArray = $this->input->getVar('cid');
$idsToRemove = $this->input->getVar('cid');

if (!empty($ag_cidArray))
if (!empty($idsToRemove))
{
$this->model->_uninstall($ag_cidArray);
$this->model->uninstallResource($idsToRemove, $this->model->input->getVar('resourceType'));
}

parent::display();
Expand Down
18 changes: 10 additions & 8 deletions com_admirorgallery/admin/helpers/admirorgallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ abstract class AdmirorGalleryHelper
/**
* Configure the Linkbar
*
* @param string $submenu
* @param string $type
* @param string $submenu Submenu
* @param string $type Type
*
* @return void
*
* @since 5.5.0
*/
public static function addSubmenu(string $submenu, string $type)
public static function addSubmenu(string $submenu, string $type): void
{
JHtmlSidebar::addEntry(JText::_('COM_ADMIRORGALLERY_CONTROL_PANEL'),
'index.php?option=com_admirorgallery&controller=admirorgallery', $submenu == 'control_panel'
);
JHtmlSidebar::addEntry(JText::_('COM_ADMIRORGALLERY_TEMPLATES'),
'index.php?option=com_admirorgallery&view=resourcemanager&AG_resourceType=templates',
'index.php?option=com_admirorgallery&view=resourcemanager&resourceType=templates',
$type == 'templates'
);
JHtmlSidebar::addEntry(JText::_('COM_ADMIRORGALLERY_POPUPS'),
'index.php?option=com_admirorgallery&view=resourcemanager&AG_resourceType=popups',
'index.php?option=com_admirorgallery&view=resourcemanager&resourceType=popups',
$type == 'popups'
);
JHtmlSidebar::addEntry(JText::_('COM_ADMIRORGALLERY_IMAGE_MANAGER'),
Expand All @@ -53,14 +55,14 @@ public static function addSubmenu(string $submenu, string $type)
/**
* Read JInput values
*
* @param string $name
* @param $default
* @param string $name Name of the fieald we need walue from
* @param mixed $default Default value in case none is found
*
* @return string|null
*
* @since 5.5.0
*/
public static function getCmd(string $name, $default): ?string
public static function getCmd(string $name, mixed $default): ?string
{
try
{
Expand Down
22 changes: 17 additions & 5 deletions com_admirorgallery/admin/models/admirorgallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,33 @@
use Joomla\CMS\Language\Text as JText;
use Joomla\CMS\MVC\Model\BaseDatabaseModel as JModelLegacy;

/**
* AdmirorgalleryModelAdmirorgallery
*
* @since 1.0.0
*/
class AdmirorgalleryModelAdmirorgallery extends JModelLegacy
{
function _update()
/**
* update
*
* @return void
*
* @since 1.0.0
*/
public function update(): void
{
$AG_DB_input = '{';
$dbInput = '{';

foreach ($_POST['params'] as $key => $value)
{
$AG_DB_input .= '"' . $key . '":"' . $value . '",';
$dbInput .= '"' . $key . '":"' . $value . '",';
}

$AG_DB_input = substr_replace($AG_DB_input, '}', -1, 1);
$dbInput = substr_replace($dbInput, '}', -1, 1);

$db = JFactory::getDBO();
$query = "UPDATE #__extensions SET params='" . $AG_DB_input . "' WHERE (element = 'admirorgallery') AND (type = 'plugin')";
$query = "UPDATE #__extensions SET params='" . $dbInput . "' WHERE (element = 'admirorgallery') AND (type = 'plugin')";
$db->setQuery($query);

if ($db->execute())
Expand Down
5 changes: 5 additions & 0 deletions com_admirorgallery/admin/models/button.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

use Joomla\CMS\MVC\Model\BaseDatabaseModel as JModelLegacy;

/**
* AdmirorgalleryModelButton
*
* @since 1.0.0
*/
class AdmirorgalleryModelButton extends JModelLegacy
{
}
23 changes: 14 additions & 9 deletions com_admirorgallery/admin/models/imagemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

JLoader::register('SecureImage', dirname(__FILE__, 2) . DIRECTORY_SEPARATOR . "scripts" . DIRECTORY_SEPARATOR . "secureimage.php");

/**
* AdmirorgalleryModelImagemanager
*
* @since 1.0.0
*/
class AdmirorgalleryModelImagemanager extends JModelLegacy
{

Expand Down Expand Up @@ -213,38 +218,38 @@ function _set_visible($cbSelectItem, $ag_folderName, $AG_visible)
function _fileUpload($itemURL, $file)
{
$config = JFactory::getConfig();
$tmp_dest = $config->get('tmp_path');
$tempDestination = $config->get('tmp_path');
$ag_ext_valid = array("jpg", "jpeg", "gif", "png", "zip");

//Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
$ag_file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

$src = $file['tmp_name'];
$dest = $tmp_dest . DIRECTORY_SEPARATOR . $filename;
$dest = $tempDestination . DIRECTORY_SEPARATOR . $filename;

// FILTER EXTENSION
$ag_ext_check = array_search($ag_file_ext, $ag_ext_valid);
if (is_numeric($ag_ext_check)) {
if (JFile::upload($src, $dest)) {
if ($ag_file_ext == "zip") {
//
if (JArchive::extract($tmp_dest . DIRECTORY_SEPARATOR . $filename, $tmp_dest . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename))) {
$files = JFolder::files($tmp_dest . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename), '.', true, true);
if (JArchive::extract($tempDestination . DIRECTORY_SEPARATOR . $filename, $tempDestination . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename))) {
$files = JFolder::files($tempDestination . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename), '.', true, true);
foreach ($files as $file_path) {
$image = new SecureImage($file_path);
if (!$image->CheckIt()) {
JFile::delete($file_path);
}
}
JFolder::copy($tmp_dest . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename), JPATH_SITE . $itemURL . JFile::stripExt($filename), '', true);
JFile::delete($tmp_dest . DIRECTORY_SEPARATOR . $filename);
JFolder::delete($tmp_dest . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename));
JFolder::copy($tempDestination . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename), JPATH_SITE . $itemURL . JFile::stripExt($filename), '', true);
JFile::delete($tempDestination . DIRECTORY_SEPARATOR . $filename);
JFolder::delete($tempDestination . DIRECTORY_SEPARATOR . 'AdmirorImageUpload' . DIRECTORY_SEPARATOR . JFile::stripExt($filename));
JFactory::getApplication()->enqueueMessage(JText::_('AG_ZIP_PACKAGE_IS_UPLOADED_AND_EXTRACTED') . " " . $filename, 'message');
}
} else {
if (JFile::copy($tmp_dest . DIRECTORY_SEPARATOR . $filename, JPATH_SITE . $itemURL . $filename)) {
JFile::delete($tmp_dest . DIRECTORY_SEPARATOR . $filename);
if (JFile::copy($tempDestination . DIRECTORY_SEPARATOR . $filename, JPATH_SITE . $itemURL . $filename)) {
JFile::delete($tempDestination . DIRECTORY_SEPARATOR . $filename);
JFactory::getApplication()->enqueueMessage(JText::_('AG_IMAGE_IS_UPLOADED') . " " . $filename, 'message');
}
}
Expand Down
78 changes: 47 additions & 31 deletions com_admirorgallery/admin/models/resourcemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,74 @@
use Joomla\CMS\Filesystem\Folder as JFolder;
use Joomla\Archive\Archive as JArchive;

/**
* AdmirorgalleryModelResourcemanager
*
* @since 1.0.0
*/
class AdmirorgalleryModelResourcemanager extends JModelLegacy
{

function _install($file)
/**
* installResource
*
* @param string $file Manifest file of the resource
* @param string $resourceType Current resource type (used to locate folder) popup|template
* @param string $fileType Supported file type : zip
* @param string $tempDestination Temp folder location
*
* @return void
*
* @since 1.0.0
*/
public function installResource(string $file, string $resourceType, string $fileType, string $tempDestination): void
{

$AG_resourceType = $this->input->getVar('AG_resourceType'); // Current resource type
$config = JFactory::getConfig();
$tmp_dest = $config->get('tmp_path');
$resourceType = substr($AG_resourceType, 0, strlen($AG_resourceType) - 1);

$file_type = "zip";

if (isset($file) && !empty($file['name']))
{
// Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

$src = $file['tmp_name'];
$dest = $tmp_dest . DIRECTORY_SEPARATOR . $filename;
$dest = $tempDestination . DIRECTORY_SEPARATOR . $filename;

// First check if the file has the right extension
if ($ext == $file_type)
if ($ext == $fileType)
{
if (JFile::upload($src, $dest))
{
if (JArchive::extract($tmp_dest . DIRECTORY_SEPARATOR . $filename, $tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType))
if (JArchive::extract($tempDestination . DIRECTORY_SEPARATOR . $filename, $tempDestination . DIRECTORY_SEPARATOR . $resourceType))
{
JFile::delete($tmp_dest . DIRECTORY_SEPARATOR . $filename);
JFile::delete($tempDestination . DIRECTORY_SEPARATOR . $filename);
}

// TEMPLATE DETAILS PARSING
if (JFile::exists($tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename) . DIRECTORY_SEPARATOR . 'details.xml'))
if (JFile::exists($tempDestination . DIRECTORY_SEPARATOR . $resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename) . DIRECTORY_SEPARATOR . 'details.xml'))
{
$ag_resourceManager_xml = simplexml_load_file($tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename) . DIRECTORY_SEPARATOR . 'details.xml');
$resourceManagerXmlObject = simplexml_load_file($tempDestination . DIRECTORY_SEPARATOR . $resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename) . DIRECTORY_SEPARATOR . 'details.xml');

if (isset($ag_resourceManager_xml->type))
if (isset($resourceManagerXmlObject->type))
{
$ag_resourceManager_type = $ag_resourceManager_xml->type;
$resourceManagerType = $resourceManagerXmlObject->type;
}
else
{
JFolder::delete($tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType);
JFolder::delete($tempDestination . DIRECTORY_SEPARATOR . $resourceType);
JFactory::getApplication()->enqueueMessage(JText::_('AG_ZIP_PACKAGE_IS_NOT_VALID_RESOURCE_TYPE') . " " . $filename, 'error');

return;
}
}
else
{
JFolder::delete($tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType);
JFolder::delete($tempDestination . DIRECTORY_SEPARATOR . $resourceType);
JFactory::getApplication()->enqueueMessage(JText::_('AG_ZIP_PACKAGE_IS_NOT_VALID_RESOURCE_TYPE') . " " . $filename, 'error');

return;
}

if (($ag_resourceManager_type) && ($ag_resourceManager_type == $resourceType))
if (($resourceManagerType) && ($resourceManagerType == $resourceType))
{
$result = JFolder::move($tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename), JPATH_SITE . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . $AG_resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename));
$result = JFolder::move($tempDestination . DIRECTORY_SEPARATOR . $resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename), JPATH_SITE . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . $resourceType . DIRECTORY_SEPARATOR . JFile::stripExt($filename));

if ($result)
{
Expand All @@ -90,7 +98,7 @@ function _install($file)
}
else
{
JFolder::delete($tmp_dest . DIRECTORY_SEPARATOR . $AG_resourceType);
JFolder::delete($tempDestination . DIRECTORY_SEPARATOR . $resourceType);
JFactory::getApplication()->enqueueMessage(JText::_('AG_ZIP_PACKAGE_IS_NOT_VALID_RESOURCE_TYPE') . " " . $filename, 'error');
}
}
Expand All @@ -106,21 +114,29 @@ function _install($file)
}
}

function _uninstall($ag_cidArray)
/**
* uninstallResource
*
* @param mixed $idsToRemove Array of resource id's to be uninstalled
* @param mixed $resourceType Current resource type (used to locate folder) popup|template
*
* @return void
*
* @since 1.0.0
*/
public function uninstallResource(array $idsToRemove, string $resourceType): void
{
$AG_resourceType = $this->input->getVar('AG_resourceType'); // Current resource type

foreach ($ag_cidArray as $ag_cidArrayKey => $ag_cidArrayValue)
foreach ($idsToRemove as $idValue)
{
if (!empty($ag_cidArrayValue))
if (!empty($idValue))
{
if (JFolder::delete(JPATH_SITE . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . $AG_resourceType . DIRECTORY_SEPARATOR . $ag_cidArrayValue))
if (JFolder::delete(JPATH_SITE . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . 'admirorgallery' . DIRECTORY_SEPARATOR . $resourceType . DIRECTORY_SEPARATOR . $idValue))
{
JFactory::getApplication()->enqueueMessage(JText::_('AG_PACKAGE_REMOVED') . " " . $ag_cidArrayValue, 'message');
JFactory::getApplication()->enqueueMessage(JText::_('AG_PACKAGE_REMOVED') . " " . $idValue, 'message');
}
else
{
JFactory::getApplication()->enqueueMessage(JText::_('AG_PACKAGE_CANNOT_BE_REMOVED') . " " . $ag_cidArrayValue, 'error');
JFactory::getApplication()->enqueueMessage(JText::_('AG_PACKAGE_CANNOT_BE_REMOVED') . " " . $idValue, 'error');
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions com_admirorgallery/admin/views/resourcemanager/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<input type="hidden" name="boxchecked" value="0"/>
<input type="hidden" name="view" value="resourcemanager"/>
<input type="hidden" name="controller" value="resourcemanager"/>
<input type="hidden" name="AG_resourceType" value="<?php echo $this->ag_resource_type; ?>"/>
<input type="hidden" name="resourceType" value="<?php echo $this->ag_resource_type; ?>"/>
<?php echo JHTML::_('form.token'); ?>

<script type="text/javascript">
Expand Down Expand Up @@ -115,12 +115,12 @@
$ag_resourceManager_description = JText::_("AG_NO_DESCRIPTION");

if (JFIle::exists(JPATH_SITE . '/plugins/content/admirorgallery/admirorgallery/' . $this->ag_resource_type . '/' . $ag_resourceManager_id . '/details.xml')) {// N U
$ag_resourceManager_xml = simplexml_load_file(JPATH_SITE . '/plugins/content/admirorgallery/admirorgallery/' . $this->ag_resource_type . '/' . $ag_resourceManager_id . '/details.xml');
$ag_resourceManager_name = $ag_resourceManager_xml->name;
$ag_resourceManager_creationDate = $ag_resourceManager_xml->creationDate;
$ag_resourceManager_author = $ag_resourceManager_xml->author;
$ag_resourceManager_version = $ag_resourceManager_xml->version;
$ag_resourceManager_description = $ag_resourceManager_xml->description;
$resourceManagerXmlObject = simplexml_load_file(JPATH_SITE . '/plugins/content/admirorgallery/admirorgallery/' . $this->ag_resource_type . '/' . $ag_resourceManager_id . '/details.xml');
$ag_resourceManager_name = $resourceManagerXmlObject->name;
$ag_resourceManager_creationDate = $resourceManagerXmlObject->creationDate;
$ag_resourceManager_author = $resourceManagerXmlObject->author;
$ag_resourceManager_version = $resourceManagerXmlObject->version;
$ag_resourceManager_description = $resourceManagerXmlObject->description;
}
?>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions com_admirorgallery/admin/views/resourcemanager/view.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ function display($tpl = null)
$app = JFactory::getApplication();
$jinput = $app->input;
$option = $jinput->getCmd('option');
$this->ag_resource_type = $jinput->getVar('AG_resourceType'); // Current resource type
$this->ag_resource_type = $jinput->getVar('resourceType'); // Current resource type

JToolBarHelper::title(JText::_('COM_ADMIRORGALLERY_' . strtoupper($this->ag_resource_type)), $this->ag_resource_type);

// Loading JPagination vars
$this->limitstart = $app->getUserStateFromRequest($option . '.limitstart', 'limitstart', 0, 'int');
$this->limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'), 'int');

// Read folder depending on $AG_resourceType
// Read folder depending on $resourceType
$this->ag_resourceManager_installed = JFolder::folders(JPATH_SITE . '/plugins/content/admirorgallery/admirorgallery/' . $this->ag_resource_type); // N U
sort($this->ag_resourceManager_installed);

Expand Down

0 comments on commit e0a821c

Please sign in to comment.