2015-11-30 21:30:54 +00:00
|
|
|
<?php
|
2021-08-16 17:11:22 +00:00
|
|
|
/*-------------------------------------------------------------------------------------------------------------| www.vdm.io |------/
|
|
|
|
____ ____ __ __ __
|
|
|
|
/\ _`\ /\ _`\ __ /\ \__ __/\ \ /\ \__
|
|
|
|
\ \,\L\_\ __ _ __ ___ ___ ___ ___ \ \ \/\ \/\_\ ____\ \ ,_\ _ __ /\_\ \ \____ __ __\ \ ,_\ ___ _ __
|
|
|
|
\/_\__ \ /'__`\/\`'__\/' __` __`\ / __`\ /' _ `\ \ \ \ \ \/\ \ /',__\\ \ \/ /\`'__\/\ \ \ '__`\/\ \/\ \\ \ \/ / __`\/\`'__\
|
|
|
|
/\ \L\ \/\ __/\ \ \/ /\ \/\ \/\ \/\ \L\ \/\ \/\ \ \ \ \_\ \ \ \/\__, `\\ \ \_\ \ \/ \ \ \ \ \L\ \ \ \_\ \\ \ \_/\ \L\ \ \ \/
|
|
|
|
\ `\____\ \____\\ \_\ \ \_\ \_\ \_\ \____/\ \_\ \_\ \ \____/\ \_\/\____/ \ \__\\ \_\ \ \_\ \_,__/\ \____/ \ \__\ \____/\ \_\
|
|
|
|
\/_____/\/____/ \/_/ \/_/\/_/\/_/\/___/ \/_/\/_/ \/___/ \/_/\/___/ \/__/ \/_/ \/_/\/___/ \/___/ \/__/\/___/ \/_/
|
|
|
|
|
|
|
|
/------------------------------------------------------------------------------------------------------------------------------------/
|
|
|
|
|
2024-02-10 09:13:31 +00:00
|
|
|
@version 3.0.x
|
2021-08-16 17:11:22 +00:00
|
|
|
@created 22nd October, 2015
|
|
|
|
@package Sermon Distributor
|
|
|
|
@subpackage import.php
|
|
|
|
@author Llewellyn van der Merwe <https://www.vdm.io/>
|
|
|
|
@copyright Copyright (C) 2015. All Rights Reserved
|
|
|
|
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
|
|
|
|
A sermon distributor that links to Dropbox.
|
|
|
|
|
|
|
|
/----------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
2024-02-10 09:13:31 +00:00
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
use Joomla\CMS\Language\Text;
|
2021-08-16 17:11:22 +00:00
|
|
|
use Joomla\CMS\Filesystem\File;
|
|
|
|
use Joomla\CMS\Filesystem\Folder;
|
2024-02-10 09:13:31 +00:00
|
|
|
use Joomla\CMS\Filesystem\Path;
|
|
|
|
use Joomla\CMS\Filter\OutputFilter;
|
|
|
|
use Joomla\CMS\Installer\InstallerHelper;
|
|
|
|
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
|
|
|
|
use Joomla\String\StringHelper;
|
2021-08-16 17:11:22 +00:00
|
|
|
use Joomla\Utilities\ArrayHelper;
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
2024-02-10 09:13:31 +00:00
|
|
|
use VDM\Joomla\Utilities\ArrayHelper as UtilitiesArrayHelper;
|
2021-08-16 17:11:22 +00:00
|
|
|
|
2023-05-29 12:21:45 +00:00
|
|
|
/***
|
2022-05-27 05:37:27 +00:00
|
|
|
* Sermondistributor Import Base Database Model
|
2021-08-16 17:11:22 +00:00
|
|
|
*/
|
2022-05-27 05:37:27 +00:00
|
|
|
class SermondistributorModelImport extends BaseDatabaseModel
|
2021-08-16 17:11:22 +00:00
|
|
|
{
|
|
|
|
// set uploading values
|
|
|
|
protected $use_streams = false;
|
|
|
|
protected $allow_unsafe = false;
|
2024-02-10 09:13:31 +00:00
|
|
|
protected $safeFileOptions = [];
|
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
/**
|
|
|
|
* @var object JTable object
|
|
|
|
*/
|
|
|
|
protected $_table = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var object JTable object
|
|
|
|
*/
|
|
|
|
protected $_url = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model context string.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_context = 'com_sermondistributor.import';
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
/**
|
|
|
|
* Import Settings
|
|
|
|
*/
|
2024-02-10 09:13:31 +00:00
|
|
|
protected $getType = NULL;
|
|
|
|
protected $dataType = NULL;
|
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
/**
|
|
|
|
* Method to auto-populate the model state.
|
|
|
|
*
|
|
|
|
* Note. Calling getState in this method will result in recursion.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function populateState()
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app = Factory::getApplication('administrator');
|
2021-08-16 17:11:22 +00:00
|
|
|
|
|
|
|
$this->setState('message', $app->getUserState('com_sermondistributor.message'));
|
|
|
|
$app->setUserState('com_sermondistributor.message', '');
|
|
|
|
|
|
|
|
// Recall the 'Import from Directory' path.
|
|
|
|
$path = $app->getUserStateFromRequest($this->_context . '.import_directory', 'import_directory', $app->get('tmp_path'));
|
|
|
|
$this->setState('import.directory', $path);
|
|
|
|
parent::populateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import an spreadsheet from either folder, url or upload.
|
|
|
|
*
|
|
|
|
* @return boolean result of import
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function import()
|
|
|
|
{
|
|
|
|
$this->setState('action', 'import');
|
2024-02-10 09:13:31 +00:00
|
|
|
$app = Factory::getApplication();
|
|
|
|
$session = Factory::getSession();
|
2021-08-16 17:11:22 +00:00
|
|
|
$package = null;
|
|
|
|
$continue = false;
|
|
|
|
// get import type
|
|
|
|
$this->getType = $app->input->getString('gettype', NULL);
|
|
|
|
// get import type
|
2024-02-10 09:13:31 +00:00
|
|
|
$this->dataType = $session->get('dataType_VDM_IMPORTINTO', NULL);
|
2021-08-16 17:11:22 +00:00
|
|
|
|
|
|
|
if ($package === null)
|
|
|
|
{
|
|
|
|
switch ($this->getType)
|
|
|
|
{
|
|
|
|
case 'folder':
|
|
|
|
// Remember the 'Import from Directory' path.
|
|
|
|
$app->getUserStateFromRequest($this->_context . '.import_directory', 'import_directory');
|
|
|
|
$package = $this->_getPackageFromFolder();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'upload':
|
|
|
|
$package = $this->_getPackageFromUpload();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'url':
|
|
|
|
$package = $this->_getPackageFromUrl();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'continue':
|
2024-02-10 09:13:31 +00:00
|
|
|
$continue = true;
|
|
|
|
$package = $session->get('package', null);
|
|
|
|
$package = json_decode($package, true);
|
2021-08-16 17:11:22 +00:00
|
|
|
// clear session
|
|
|
|
$session->clear('package');
|
|
|
|
$session->clear('dataType');
|
|
|
|
$session->clear('hasPackage');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->setUserState('com_sermondistributor.message', Text::_('COM_SERMONDISTRIBUTOR_IMPORT_NO_IMPORT_TYPE_FOUND'));
|
2021-08-16 17:11:22 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Was the package valid?
|
|
|
|
if (!$package || !$package['type'])
|
|
|
|
{
|
|
|
|
if (in_array($this->getType, array('upload', 'url')))
|
|
|
|
{
|
|
|
|
$this->remove($package['packagename']);
|
|
|
|
}
|
|
|
|
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->setUserState('com_sermondistributor.message', Text::_('COM_SERMONDISTRIBUTOR_IMPORT_UNABLE_TO_FIND_IMPORT_PACKAGE'));
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// first link data to table headers
|
|
|
|
if(!$continue){
|
|
|
|
$package = json_encode($package);
|
|
|
|
$session->set('package', $package);
|
|
|
|
$session->set('dataType', $this->dataType);
|
|
|
|
$session->set('hasPackage', true);
|
|
|
|
return true;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// set the data
|
|
|
|
$headerList = json_decode($session->get($this->dataType.'_VDM_IMPORTHEADERS', false), true);
|
|
|
|
if (!$this->setData($package,$this->dataType,$headerList))
|
|
|
|
{
|
|
|
|
// There was an error importing the package
|
2024-02-10 09:13:31 +00:00
|
|
|
$msg = Text::_('COM_SERMONDISTRIBUTOR_IMPORT_ERROR');
|
2021-08-16 17:11:22 +00:00
|
|
|
$back = $session->get('backto_VDM_IMPORT', NULL);
|
|
|
|
if ($back)
|
|
|
|
{
|
|
|
|
$app->setUserState('com_sermondistributor.redirect_url', 'index.php?option=com_sermondistributor&view='.$back);
|
|
|
|
$session->clear('backto_VDM_IMPORT');
|
|
|
|
}
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Package imported sucessfully
|
2024-02-10 09:13:31 +00:00
|
|
|
$msg = Text::sprintf('COM_SERMONDISTRIBUTOR_IMPORT_SUCCESS', $package['packagename']);
|
2021-08-16 17:11:22 +00:00
|
|
|
$back = $session->get('backto_VDM_IMPORT', NULL);
|
|
|
|
if ($back)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->setUserState('com_sermondistributor.redirect_url', 'index.php?option=com_sermondistributor&view='.$back);
|
|
|
|
$session->clear('backto_VDM_IMPORT');
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some model state values
|
|
|
|
$app->enqueueMessage($msg);
|
|
|
|
|
|
|
|
// remove file after import
|
|
|
|
$this->remove($package['packagename']);
|
|
|
|
$session->clear($this->getType.'_VDM_IMPORTHEADERS');
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Works out an importation spreadsheet from a HTTP upload
|
|
|
|
*
|
|
|
|
* @return spreadsheet definition or false on failure
|
|
|
|
*/
|
|
|
|
protected function _getPackageFromUpload()
|
2024-02-10 09:13:31 +00:00
|
|
|
{
|
2021-08-16 17:11:22 +00:00
|
|
|
// Get the uploaded file information
|
2024-02-10 09:13:31 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-08-16 17:11:22 +00:00
|
|
|
$input = $app->input;
|
|
|
|
|
|
|
|
// Do not change the filter type 'raw'. We need this to let files containing PHP code to upload. See JInputFiles::get.
|
|
|
|
$userfile = $input->files->get('import_package', null, 'raw');
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// Make sure that file uploads are enabled in php
|
|
|
|
if (!(bool) ini_get('file_uploads'))
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_WARNIMPORTFILE'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no uploaded file, we have a problem...
|
|
|
|
if (!is_array($userfile))
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_NO_FILE_SELECTED'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there was a problem uploading the file.
|
|
|
|
if ($userfile['error'] || $userfile['size'] < 1)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_WARNIMPORTUPLOADERROR'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the appropriate paths
|
2024-02-10 09:13:31 +00:00
|
|
|
$config = Factory::getConfig();
|
2021-08-16 17:11:22 +00:00
|
|
|
$tmp_dest = $config->get('tmp_path') . '/' . $userfile['name'];
|
|
|
|
$tmp_src = $userfile['tmp_name'];
|
|
|
|
|
|
|
|
// Move uploaded file
|
|
|
|
$p_file = File::upload($tmp_src, $tmp_dest, $this->use_streams, $this->allow_unsafe, $this->safeFileOptions);
|
|
|
|
|
|
|
|
// Was the package downloaded?
|
|
|
|
if (!$p_file)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$session = Factory::getSession();
|
2021-08-16 17:11:22 +00:00
|
|
|
$session->clear('package');
|
|
|
|
$session->clear('dataType');
|
|
|
|
$session->clear('hasPackage');
|
|
|
|
// was not uploaded
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that this is a valid spreadsheet
|
|
|
|
$package = $this->check($userfile['name']);
|
|
|
|
|
|
|
|
return $package;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import an spreadsheet from a directory
|
|
|
|
*
|
|
|
|
* @return array Spreadsheet details or false on failure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function _getPackageFromFolder()
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-08-16 17:11:22 +00:00
|
|
|
$input = $app->input;
|
|
|
|
|
|
|
|
// Get the path to the package to import
|
|
|
|
$p_dir = $input->getString('import_directory');
|
2024-02-10 09:13:31 +00:00
|
|
|
$p_dir = Path::clean($p_dir);
|
2021-08-16 17:11:22 +00:00
|
|
|
// Did you give us a valid path?
|
|
|
|
if (!file_exists($p_dir))
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_PLEASE_ENTER_A_PACKAGE_DIRECTORY'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect the package type
|
|
|
|
$type = $this->getType;
|
|
|
|
|
|
|
|
// Did you give us a valid package?
|
|
|
|
if (!$type)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_PATH_DOES_NOT_HAVE_A_VALID_PACKAGE'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// check the extention
|
|
|
|
if(!$this->checkExtension($p_dir))
|
|
|
|
{
|
|
|
|
// set error message
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_DOES_NOT_HAVE_A_VALID_FILE_TYPE'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
$package['packagename'] = null;
|
|
|
|
$package['dir'] = $p_dir;
|
|
|
|
$package['type'] = $type;
|
|
|
|
|
|
|
|
return $package;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import an spreadsheet from a URL
|
|
|
|
*
|
|
|
|
* @return Package details or false on failure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function _getPackageFromUrl()
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-08-16 17:11:22 +00:00
|
|
|
$input = $app->input;
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// Get the URL of the package to import
|
|
|
|
$url = $input->getString('import_url');
|
|
|
|
|
|
|
|
// Did you give us a URL?
|
|
|
|
if (!$url)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_ENTER_A_URL'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download the package at the URL given
|
2024-02-10 09:13:31 +00:00
|
|
|
$p_file = InstallerHelper::downloadPackage($url);
|
2021-08-16 17:11:22 +00:00
|
|
|
|
|
|
|
// Was the package downloaded?
|
|
|
|
if (!$p_file)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_INVALID_URL'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that this is a valid spreadsheet
|
|
|
|
$package = $this->check($p_file);
|
|
|
|
|
|
|
|
return $package;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
/**
|
|
|
|
* Check a file and verifies it as a spreadsheet file
|
|
|
|
* Supports .csv .xlsx .xls and .ods
|
|
|
|
*
|
|
|
|
* @param string $p_filename The uploaded package filename or import directory
|
|
|
|
*
|
|
|
|
* @return array of elements
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function check($archivename)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-08-16 17:11:22 +00:00
|
|
|
// Clean the name
|
2024-02-10 09:13:31 +00:00
|
|
|
$archivename = Path::clean($archivename);
|
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// check the extention
|
|
|
|
if(!$this->checkExtension($archivename))
|
|
|
|
{
|
|
|
|
// Cleanup the import files
|
|
|
|
$this->remove($archivename);
|
2024-02-10 09:13:31 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_SERMONDISTRIBUTOR_IMPORT_MSG_DOES_NOT_HAVE_A_VALID_FILE_TYPE'), 'warning');
|
2021-08-16 17:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
|
|
|
$config = Factory::getConfig();
|
2021-08-16 17:11:22 +00:00
|
|
|
// set Package Name
|
|
|
|
$check['packagename'] = $archivename;
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// set directory
|
|
|
|
$check['dir'] = $config->get('tmp_path'). '/' .$archivename;
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
// set type
|
|
|
|
$check['type'] = $this->getType;
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
return $check;
|
2020-05-30 21:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the extension
|
|
|
|
*
|
|
|
|
* @param string $file Name of the uploaded file
|
|
|
|
*
|
|
|
|
* @return boolean True on success
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function checkExtension($file)
|
|
|
|
{
|
|
|
|
// check the extention
|
|
|
|
switch(strtolower(pathinfo($file, PATHINFO_EXTENSION)))
|
|
|
|
{
|
|
|
|
case 'xls':
|
|
|
|
case 'ods':
|
|
|
|
case 'csv':
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up temporary uploaded spreadsheet
|
|
|
|
*
|
|
|
|
* @param string $package Name of the uploaded spreadsheet file
|
|
|
|
*
|
|
|
|
* @return boolean True on success
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function remove($package)
|
|
|
|
{
|
|
|
|
jimport('joomla.filesystem.file');
|
2024-02-10 09:13:31 +00:00
|
|
|
|
|
|
|
$config = Factory::getConfig();
|
2021-08-16 17:11:22 +00:00
|
|
|
$package = $config->get('tmp_path'). '/' .$package;
|
|
|
|
|
|
|
|
// Is the package file a valid file?
|
|
|
|
if (is_file($package))
|
|
|
|
{
|
|
|
|
File::delete($package);
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
elseif (is_file(Path::clean($package)))
|
2021-08-16 17:11:22 +00:00
|
|
|
{
|
|
|
|
// It might also be just a base filename
|
2024-02-10 09:13:31 +00:00
|
|
|
File::delete(Path::clean($package));
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
2020-05-30 21:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the data from the spreadsheet to the database
|
|
|
|
*
|
|
|
|
* @param string $package Paths to the uploaded package file
|
|
|
|
*
|
|
|
|
* @return boolean false on failure
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
protected function setData($package,$table,$target_headers)
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
if (UtilitiesArrayHelper::check($target_headers))
|
2020-05-30 21:39:43 +00:00
|
|
|
{
|
|
|
|
// make sure the file is loaded
|
|
|
|
SermondistributorHelper::composerAutoload('phpspreadsheet');
|
2024-02-10 09:13:31 +00:00
|
|
|
$jinput = Factory::getApplication()->input;
|
2020-05-30 21:39:43 +00:00
|
|
|
foreach($target_headers as $header)
|
|
|
|
{
|
2020-11-30 16:57:29 +00:00
|
|
|
if (($column = $jinput->getString($header, false)) !== false ||
|
|
|
|
($column = $jinput->getString(strtolower($header), false)) !== false)
|
|
|
|
{
|
|
|
|
$data['target_headers'][$header] = $column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$data['target_headers'][$header] = null;
|
|
|
|
}
|
2020-05-30 21:39:43 +00:00
|
|
|
}
|
|
|
|
// set the data
|
|
|
|
if(isset($package['dir']))
|
|
|
|
{
|
|
|
|
$inputFileType = IOFactory::identify($package['dir']);
|
|
|
|
$excelReader = IOFactory::createReader($inputFileType);
|
|
|
|
$excelReader->setReadDataOnly(true);
|
|
|
|
$excelObj = $excelReader->load($package['dir']);
|
|
|
|
$data['array'] = $excelObj->getActiveSheet()->toArray(null, true,true,true);
|
|
|
|
$excelObj->disconnectWorksheets();
|
|
|
|
unset($excelObj);
|
|
|
|
return $this->save($data, $table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the data from the file to the database
|
|
|
|
*
|
|
|
|
* @param string $package Paths to the uploaded package file
|
|
|
|
*
|
|
|
|
* @return boolean false on failure
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
protected function save($data,$table)
|
|
|
|
{
|
|
|
|
// import the data if there is any
|
2024-02-10 09:13:31 +00:00
|
|
|
if(UtilitiesArrayHelper::check($data['array']))
|
2020-05-30 21:39:43 +00:00
|
|
|
{
|
|
|
|
// get user object
|
2024-02-10 09:13:31 +00:00
|
|
|
$user = Factory::getUser();
|
2020-05-30 21:39:43 +00:00
|
|
|
// remove header if it has headers
|
|
|
|
$id_key = $data['target_headers']['id'];
|
|
|
|
$published_key = $data['target_headers']['published'];
|
|
|
|
$ordering_key = $data['target_headers']['ordering'];
|
|
|
|
// get the first array set
|
|
|
|
$firstSet = reset($data['array']);
|
|
|
|
|
|
|
|
// check if first array is a header array and remove if true
|
|
|
|
if($firstSet[$id_key] == 'id' || $firstSet[$published_key] == 'published' || $firstSet[$ordering_key] == 'ordering')
|
|
|
|
{
|
|
|
|
array_shift($data['array']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure there is still values in array and that it was not only headers
|
2024-02-10 09:13:31 +00:00
|
|
|
if(UtilitiesArrayHelper::check($data['array']) && $user->authorise($table.'.import', 'com_sermondistributor') && $user->authorise('core.import', 'com_sermondistributor'))
|
2020-05-30 21:39:43 +00:00
|
|
|
{
|
|
|
|
// set target.
|
|
|
|
$target = array_flip($data['target_headers']);
|
|
|
|
// Get a db connection.
|
2024-02-10 09:13:31 +00:00
|
|
|
$db = Factory::getDbo();
|
2020-05-30 21:39:43 +00:00
|
|
|
// set some defaults
|
2024-02-10 09:13:31 +00:00
|
|
|
$todayDate = Factory::getDate()->toSql();
|
2020-05-30 21:39:43 +00:00
|
|
|
// get global action permissions
|
|
|
|
$canDo = SermondistributorHelper::getActions($table);
|
|
|
|
$canEdit = $canDo->get('core.edit');
|
|
|
|
$canState = $canDo->get('core.edit.state');
|
|
|
|
$canCreate = $canDo->get('core.create');
|
|
|
|
$hasAlias = $this->getAliasesUsed($table);
|
|
|
|
// prosses the data
|
|
|
|
foreach($data['array'] as $row)
|
|
|
|
{
|
|
|
|
$found = false;
|
|
|
|
if (isset($row[$id_key]) && is_numeric($row[$id_key]) && $row[$id_key] > 0)
|
|
|
|
{
|
|
|
|
// raw items import & update!
|
|
|
|
$query = $db->getQuery(true);
|
|
|
|
$query
|
|
|
|
->select('version')
|
|
|
|
->from($db->quoteName('#__sermondistributor_'.$table))
|
|
|
|
->where($db->quoteName('id') . ' = '. $db->quote($row[$id_key]));
|
|
|
|
// Reset the query using our newly populated query object.
|
|
|
|
$db->setQuery($query);
|
|
|
|
$db->execute();
|
|
|
|
$found = $db->getNumRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($found && $canEdit)
|
|
|
|
{
|
|
|
|
// update item
|
|
|
|
$id = $row[$id_key];
|
|
|
|
$version = $db->loadResult();
|
|
|
|
// reset all buckets
|
|
|
|
$query = $db->getQuery(true);
|
|
|
|
$fields = array();
|
|
|
|
// Fields to update.
|
|
|
|
foreach($row as $key => $cell)
|
|
|
|
{
|
|
|
|
// ignore column
|
|
|
|
if ('IGNORE' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// update modified
|
|
|
|
if ('modified_by' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// update modified
|
|
|
|
if ('modified' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// update version
|
|
|
|
if ('version' == $target[$key])
|
|
|
|
{
|
|
|
|
$cell = (int) $version + 1;
|
|
|
|
}
|
|
|
|
// verify publish authority
|
|
|
|
if ('published' == $target[$key] && !$canState)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// set to update array
|
|
|
|
if(in_array($key, $data['target_headers']) && is_numeric($cell))
|
|
|
|
{
|
|
|
|
$fields[] = $db->quoteName($target[$key]) . ' = ' . $cell;
|
|
|
|
}
|
|
|
|
elseif(in_array($key, $data['target_headers']) && is_string($cell))
|
|
|
|
{
|
|
|
|
$fields[] = $db->quoteName($target[$key]) . ' = ' . $db->quote($cell);
|
|
|
|
}
|
|
|
|
elseif(in_array($key, $data['target_headers']) && is_null($cell))
|
|
|
|
{
|
|
|
|
// if import data is null then set empty
|
|
|
|
$fields[] = $db->quoteName($target[$key]) . " = ''";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// load the defaults
|
|
|
|
$fields[] = $db->quoteName('modified_by') . ' = ' . $db->quote($user->id);
|
|
|
|
$fields[] = $db->quoteName('modified') . ' = ' . $db->quote($todayDate);
|
|
|
|
// Conditions for which records should be updated.
|
|
|
|
$conditions = array(
|
|
|
|
$db->quoteName('id') . ' = ' . $id
|
|
|
|
);
|
|
|
|
|
|
|
|
$query->update($db->quoteName('#__sermondistributor_'.$table))->set($fields)->where($conditions);
|
|
|
|
$db->setQuery($query);
|
|
|
|
$db->execute();
|
|
|
|
}
|
|
|
|
elseif ($canCreate)
|
|
|
|
{
|
|
|
|
// insert item
|
|
|
|
$query = $db->getQuery(true);
|
|
|
|
// reset all buckets
|
|
|
|
$columns = array();
|
|
|
|
$values = array();
|
|
|
|
$version = false;
|
|
|
|
// Insert columns. Insert values.
|
|
|
|
foreach($row as $key => $cell)
|
|
|
|
{
|
|
|
|
// ignore column
|
|
|
|
if ('IGNORE' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// remove id
|
|
|
|
if ('id' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// update created
|
|
|
|
if ('created_by' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// update created
|
|
|
|
if ('created' == $target[$key])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Make sure the alias is incremented
|
|
|
|
if ('alias' == $target[$key])
|
|
|
|
{
|
|
|
|
$cell = $this->getAlias($cell,$table);
|
|
|
|
}
|
|
|
|
// update version
|
|
|
|
if ('version' == $target[$key])
|
|
|
|
{
|
|
|
|
$cell = 1;
|
|
|
|
$version = true;
|
|
|
|
}
|
|
|
|
// set to insert array
|
|
|
|
if(in_array($key, $data['target_headers']) && is_numeric($cell))
|
|
|
|
{
|
|
|
|
$columns[] = $target[$key];
|
|
|
|
$values[] = $cell;
|
|
|
|
}
|
|
|
|
elseif(in_array($key, $data['target_headers']) && is_string($cell))
|
|
|
|
{
|
|
|
|
$columns[] = $target[$key];
|
|
|
|
$values[] = $db->quote($cell);
|
|
|
|
}
|
|
|
|
elseif(in_array($key, $data['target_headers']) && is_null($cell))
|
|
|
|
{
|
|
|
|
// if import data is null then set empty
|
|
|
|
$columns[] = $target[$key];
|
|
|
|
$values[] = "''";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// load the defaults
|
|
|
|
$columns[] = 'created_by';
|
|
|
|
$values[] = $db->quote($user->id);
|
|
|
|
$columns[] = 'created';
|
|
|
|
$values[] = $db->quote($todayDate);
|
|
|
|
if (!$version)
|
|
|
|
{
|
|
|
|
$columns[] = 'version';
|
|
|
|
$values[] = 1;
|
|
|
|
}
|
|
|
|
// Prepare the insert query.
|
|
|
|
$query
|
|
|
|
->insert($db->quoteName('#__sermondistributor_'.$table))
|
|
|
|
->columns($db->quoteName($columns))
|
|
|
|
->values(implode(',', $values));
|
|
|
|
// Set the query using our newly populated query object and execute it.
|
|
|
|
$db->setQuery($query);
|
|
|
|
$done = $db->execute();
|
|
|
|
if ($done)
|
|
|
|
{
|
|
|
|
$aId = $db->insertid();
|
|
|
|
// make sure the access of asset is set
|
|
|
|
SermondistributorHelper::setAsset($aId,$table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAlias($name,$type = false)
|
|
|
|
{
|
|
|
|
// sanitize the name to an alias
|
2024-02-10 09:13:31 +00:00
|
|
|
if (Factory::getConfig()->get('unicodeslugs') == 1)
|
2021-08-16 17:11:22 +00:00
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$alias = OutputFilter::stringURLUnicodeSlug($name);
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$alias = OutputFilter::stringURLSafe($name);
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
|
|
|
// must be a uniqe alias
|
|
|
|
if ($type)
|
|
|
|
{
|
|
|
|
return $this->getUniqe($alias,'alias',$type);
|
|
|
|
}
|
|
|
|
return $alias;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
/**
|
|
|
|
* Method to generate a uniqe value.
|
|
|
|
*
|
|
|
|
* @param string $field name.
|
|
|
|
* @param string $value data.
|
|
|
|
* @param string $type table.
|
|
|
|
*
|
|
|
|
* @return string New value.
|
|
|
|
*/
|
|
|
|
protected function getUniqe($value,$field,$type)
|
|
|
|
{
|
|
|
|
// insure the filed is always uniqe
|
|
|
|
while (isset($this->uniqeValueArray[$type][$field][$value]))
|
|
|
|
{
|
2024-02-10 09:13:31 +00:00
|
|
|
$value = StringHelper::increment($value, 'dash');
|
2021-08-16 17:11:22 +00:00
|
|
|
}
|
|
|
|
$this->uniqeValueArray[$type][$field][$value] = $value;
|
|
|
|
return $value;
|
|
|
|
}
|
2024-02-10 09:13:31 +00:00
|
|
|
|
2021-08-16 17:11:22 +00:00
|
|
|
protected function getAliasesUsed($table)
|
|
|
|
{
|
|
|
|
// Get a db connection.
|
2024-02-10 09:13:31 +00:00
|
|
|
$db = Factory::getDbo();
|
2021-08-16 17:11:22 +00:00
|
|
|
// first we check if there is a alias column
|
|
|
|
$columns = $db->getTableColumns('#__sermondistributor_'.$table);
|
|
|
|
if(isset($columns['alias'])){
|
|
|
|
// Create a new query object.
|
|
|
|
$query = $db->getQuery(true);
|
|
|
|
$query->select($db->quoteName(array('alias')));
|
|
|
|
$query->from($db->quoteName('#__sermondistributor_'.$table));
|
|
|
|
$db->setQuery($query);
|
|
|
|
$db->execute();
|
|
|
|
if ($db->getNumRows())
|
|
|
|
{
|
|
|
|
$aliases = $db->loadColumn();
|
|
|
|
foreach($aliases as $alias)
|
|
|
|
{
|
|
|
|
$this->uniqeValueArray[$table]['alias'][$alias] = $alias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|