2021-03-05 03:08:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
|
|
|
* @created 30th April, 2015
|
2024-03-02 20:10:30 +00:00
|
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
2021-03-05 03:08:47 +00:00
|
|
|
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
?>
|
|
|
|
###BOM###
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
2023-02-27 12:27:41 +00:00
|
|
|
###IMPORT_CUSTOM_MODEL_HEADER###
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-25 08:30:55 +00:00
|
|
|
* ###Component### ###View### Base Database Model
|
2021-03-05 03:08:47 +00:00
|
|
|
*/
|
2022-05-25 08:30:55 +00:00
|
|
|
class ###Component###Model###View### extends BaseDatabaseModel
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
|
|
|
// set uploading values
|
|
|
|
protected $use_streams = false;
|
|
|
|
protected $allow_unsafe = false;
|
2024-03-02 20:10:30 +00:00
|
|
|
protected $safeFileOptions = [];
|
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* @var object JTable object
|
|
|
|
*/
|
|
|
|
protected $_table = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var object JTable object
|
|
|
|
*/
|
|
|
|
protected $_url = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model context string.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_context = 'com_###component###.###view###';
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Import Settings
|
|
|
|
*/
|
|
|
|
protected $getType = NULL;
|
|
|
|
protected $dataType = NULL;
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +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-03-02 20:10:30 +00:00
|
|
|
$app = Factory::getApplication('administrator');
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
$this->setState('message', $app->getUserState('com_###component###.message'));
|
|
|
|
$app->setUserState('com_###component###.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();
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
###IMPORT_METHOD_CUSTOM###
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Works out an importation spreadsheet from a HTTP upload
|
|
|
|
*
|
|
|
|
* @return spreadsheet definition or false on failure
|
|
|
|
*/
|
|
|
|
protected function _getPackageFromUpload()
|
2024-03-02 20:10:30 +00:00
|
|
|
{
|
2021-03-05 03:08:47 +00:00
|
|
|
// Get the uploaded file information
|
2024-03-02 20:10:30 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-03-05 03:08:47 +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-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// Make sure that file uploads are enabled in php
|
|
|
|
if (!(bool) ini_get('file_uploads'))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_WARNIMPORTFILE'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no uploaded file, we have a problem...
|
|
|
|
if (!is_array($userfile))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_NO_FILE_SELECTED'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there was a problem uploading the file.
|
|
|
|
if ($userfile['error'] || $userfile['size'] < 1)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_WARNIMPORTUPLOADERROR'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the appropriate paths
|
2024-03-02 20:10:30 +00:00
|
|
|
$config = Factory::getConfig();
|
2021-03-05 03:08:47 +00:00
|
|
|
$tmp_dest = $config->get('tmp_path') . '/' . $userfile['name'];
|
|
|
|
$tmp_src = $userfile['tmp_name'];
|
|
|
|
|
|
|
|
// Move uploaded file
|
2021-03-08 22:36:30 +00:00
|
|
|
$p_file = File::upload($tmp_src, $tmp_dest, $this->use_streams, $this->allow_unsafe, $this->safeFileOptions);
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
// Was the package downloaded?
|
|
|
|
if (!$p_file)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$session = Factory::getSession();
|
2021-03-05 03:08:47 +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-03-02 20:10:30 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-03-05 03:08:47 +00:00
|
|
|
$input = $app->input;
|
|
|
|
|
|
|
|
// Get the path to the package to import
|
|
|
|
$p_dir = $input->getString('import_directory');
|
2024-03-02 20:10:30 +00:00
|
|
|
$p_dir = Path::clean($p_dir);
|
2021-03-05 03:08:47 +00:00
|
|
|
// Did you give us a valid path?
|
|
|
|
if (!file_exists($p_dir))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_PLEASE_ENTER_A_PACKAGE_DIRECTORY'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect the package type
|
|
|
|
$type = $this->getType;
|
|
|
|
|
|
|
|
// Did you give us a valid package?
|
|
|
|
if (!$type)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_PATH_DOES_NOT_HAVE_A_VALID_PACKAGE'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// check the extention
|
|
|
|
if(!$this->checkExtension($p_dir))
|
|
|
|
{
|
|
|
|
// set error message
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_DOES_NOT_HAVE_A_VALID_FILE_TYPE'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +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-03-02 20:10:30 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-03-05 03:08:47 +00:00
|
|
|
$input = $app->input;
|
|
|
|
|
|
|
|
// Get the URL of the package to import
|
|
|
|
$url = $input->getString('import_url');
|
|
|
|
|
|
|
|
// Did you give us a URL?
|
|
|
|
if (!$url)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_ENTER_A_URL'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download the package at the URL given
|
2024-03-02 20:10:30 +00:00
|
|
|
$p_file = InstallerHelper::downloadPackage($url);
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
// Was the package downloaded?
|
|
|
|
if (!$p_file)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_INVALID_URL'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that this is a valid spreadsheet
|
|
|
|
$package = $this->check($p_file);
|
|
|
|
|
|
|
|
return $package;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +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-03-02 20:10:30 +00:00
|
|
|
$app = Factory::getApplication();
|
2021-03-05 03:08:47 +00:00
|
|
|
// Clean the name
|
2024-03-02 20:10:30 +00:00
|
|
|
$archivename = Path::clean($archivename);
|
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// check the extention
|
|
|
|
if(!$this->checkExtension($archivename))
|
|
|
|
{
|
|
|
|
// Cleanup the import files
|
|
|
|
$this->remove($archivename);
|
2024-03-02 20:10:30 +00:00
|
|
|
$app->enqueueMessage(Text::_('COM_###COMPONENT###_IMPORT_MSG_DOES_NOT_HAVE_A_VALID_FILE_TYPE'), 'warning');
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
|
|
|
$config = Factory::getConfig();
|
2021-03-05 03:08:47 +00:00
|
|
|
// set Package Name
|
|
|
|
$check['packagename'] = $archivename;
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// set directory
|
|
|
|
$check['dir'] = $config->get('tmp_path'). '/' .$archivename;
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// set type
|
|
|
|
$check['type'] = $this->getType;
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
return $check;
|
|
|
|
}
|
|
|
|
###IMPORT_EXT_METHOD###
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up temporary uploaded spreadsheet
|
|
|
|
*
|
|
|
|
* @param string $package Name of the uploaded spreadsheet file
|
|
|
|
*
|
|
|
|
* @return boolean True on success
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function remove($package)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$config = Factory::getConfig();
|
2021-03-05 03:08:47 +00:00
|
|
|
$package = $config->get('tmp_path'). '/' .$package;
|
|
|
|
|
|
|
|
// Is the package file a valid file?
|
|
|
|
if (is_file($package))
|
|
|
|
{
|
2021-03-08 22:36:30 +00:00
|
|
|
File::delete($package);
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
elseif (is_file(Path::clean($package)))
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
|
|
|
// It might also be just a base filename
|
2024-03-02 20:10:30 +00:00
|
|
|
File::delete(Path::clean($package));
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
###IMPORT_SETDATA_METHOD###
|
|
|
|
###IMPORT_SAVE_METHOD###
|
|
|
|
|
|
|
|
protected function getAlias($name,$type = false)
|
|
|
|
{
|
|
|
|
// sanitize the name to an alias
|
2024-03-02 20:10:30 +00:00
|
|
|
if (Factory::getConfig()->get('unicodeslugs') == 1)
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$alias = OutputFilter::stringURLUnicodeSlug($name);
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$alias = OutputFilter::stringURLSafe($name);
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
|
|
|
// must be a uniqe alias
|
|
|
|
if ($type)
|
|
|
|
{
|
|
|
|
return $this->getUniqe($alias,'alias',$type);
|
|
|
|
}
|
|
|
|
return $alias;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +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-03-02 20:10:30 +00:00
|
|
|
$value = StringHelper::increment($value, 'dash');
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
|
|
|
$this->uniqeValueArray[$type][$field][$value] = $value;
|
|
|
|
return $value;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
protected function getAliasesUsed($table)
|
|
|
|
{
|
|
|
|
// Get a db connection.
|
2024-03-02 20:10:30 +00:00
|
|
|
$db = Factory::getDbo();
|
2021-03-05 03:08:47 +00:00
|
|
|
// first we check if there is a alias column
|
|
|
|
$columns = $db->getTableColumns('#__###component###_'.$table);
|
|
|
|
if(isset($columns['alias'])){
|
|
|
|
// Create a new query object.
|
|
|
|
$query = $db->getQuery(true);
|
|
|
|
$query->select($db->quoteName(array('alias')));
|
|
|
|
$query->from($db->quoteName('#__###component###_'.$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;
|
|
|
|
}
|
|
|
|
}
|