Release of v5.0.3-alpha4

Fix database default fields to allow NULL. #1169. Fix the power list field to allow search. #1167. Expanded the Demo component in JCB v4 to include more advance features.
This commit is contained in:
2024-09-18 04:40:33 +02:00
parent fc8fabe298
commit 0603c39cc1
129 changed files with 7128 additions and 1449 deletions

View File

@@ -14,9 +14,11 @@ namespace VDM\Joomla\Utilities\Component;
use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\Input\Input;
use Joomla\Registry\Registry;
use VDM\Joomla\Utilities\String\NamespaceHelper;
use VDM\Joomla\Utilities\StringHelper;
/**
@@ -50,6 +52,57 @@ abstract class Helper
*/
protected static array $params = [];
/**
* Sets a parameter value for the given target in the specified option's params.
* If no option is provided, it falls back to the default option.
*
* This method updates the parameters for a given extension in the database,
* only if the new value differs from the existing one.
*
* @param string $target The parameter name to be updated.
* @param mixed $value The value to set for the parameter.
* @param string|null $option The optional extension element name. Defaults to null, which will use the default option.
*
* @return mixed The previous value of the parameter before it was updated.
* @since 5.0.3
*/
public static function setParams(string $target, $value, ?string $option = null)
{
// Ensure that an option is specified, defaulting to the system's option if not provided.
if (empty($option))
{
$option = static::getOption();
}
// Retrieve current parameters for the specified option.
$params = static::getParams($option);
// Get the current value of the target parameter.
$was = $params->get($target, null);
// Only proceed if the new value differs from the current value.
if ($was !== $value)
{
// Update the parameter value.
$params->set($target, $value);
// Obtain a database connection instance.
$db = Factory::getDBO();
$query = $db->getQuery(true);
// Build and execute the query to update the parameters in the database.
$query->update('#__extensions AS a')
->set('a.params = ' . $db->quote((string) $params))
->where('a.element = ' . $db->quote((string) $option));
$db->setQuery($query);
$db->execute();
}
// Return the previous value of the parameter.
return $was;
}
/**
* Gets the parameter object for the component
*
@@ -64,16 +117,16 @@ abstract class Helper
// check that we have an option
if (empty($option))
{
$option = self::getOption();
$option = static::getOption();
}
// get global value
if (!isset(self::$params[$option]) || !self::$params[$option] instanceof Registry)
if (!isset(static::$params[$option]) || !static::$params[$option] instanceof Registry)
{
self::$params[$option] = ComponentHelper::getParams($option);
static::$params[$option] = ComponentHelper::getParams($option);
}
return self::$params[$option];
return static::$params[$option];
}
/**
@@ -86,7 +139,7 @@ abstract class Helper
*/
public static function setOption(?string $option): void
{
self::$option = $option;
static::$option = $option;
}
/**
@@ -99,13 +152,13 @@ abstract class Helper
*/
public static function getOption(?string $default = 'empty'): ?string
{
if (empty(self::$option))
if (empty(static::$option))
{
// get the option from the url input
self::$option = (new Input)->getString('option', null);
static::$option = (new Input)->getString('option', null);
}
if (empty(self::$option))
if (empty(static::$option))
{
$app = Factory::getApplication();
@@ -113,16 +166,16 @@ abstract class Helper
if (method_exists($app, 'getInput'))
{
// get the option from the application
self::$option = $app->getInput()->getCmd('option', $default);
static::$option = $app->getInput()->getCmd('option', $default);
}
else
{
// Use the default value if getInput method does not exist
self::$option = $default;
static::$option = $default;
}
}
return self::$option;
return static::$option;
}
/**
@@ -139,7 +192,7 @@ abstract class Helper
// check that we have an option
if (empty($option))
{
$option = self::getOption();
$option = static::getOption();
}
// option with com_
if (is_string($option) && strpos($option, 'com_') === 0)
@@ -164,7 +217,7 @@ abstract class Helper
{
// check that we have an option
// and get the code name from it
if (($code_name = self::getCode($option, null)) !== null)
if (($code_name = static::getCode($option, null)) !== null)
{
// we build the helper class name
$helper_name = '\\' . \ucfirst($code_name) . 'Helper';
@@ -176,7 +229,7 @@ abstract class Helper
}
// try loading namespace
if (($namespace = self::getNamespace($option)) !== null)
if (($namespace = static::getNamespace($option)) !== null)
{
$name = \ucfirst($code_name) . 'Helper';
$namespace_helper = '\\' . $namespace . '\Administrator\Helper\\' . NamespaceHelper::safeSegment($name); // TODO target site or admin locations not just admin...
@@ -202,7 +255,7 @@ abstract class Helper
*/
public static function getNamespace(?string $option = null): ?string
{
$manifest = self::getManifest($option);
$manifest = static::getManifest($option);
return $manifest->namespace ?? null;
}
@@ -220,13 +273,13 @@ abstract class Helper
public static function getManifest(?string $option = null): ?object
{
if ($option === null
&& ($option = self::getOption($option)) === null)
&& ($option = static::getOption($option)) === null)
{
return null;
}
// get global manifest_cache values
if (!isset(self::$manifest[$option]))
if (!isset(static::$manifest[$option]))
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
@@ -240,14 +293,14 @@ abstract class Helper
try {
$manifest = $db->loadResult();
self::$manifest[$option] = json_decode($manifest);
static::$manifest[$option] = json_decode($manifest);
} catch (\Exception $e) {
// Handle the database error appropriately.
self::$manifest[$option] = null;
static::$manifest[$option] = null;
}
}
return self::$manifest[$option];
return static::$manifest[$option];
}
/**
@@ -263,7 +316,7 @@ abstract class Helper
public static function methodExists(string $method, ?string $option = null): bool
{
// get the helper class
return ($helper = self::get($option, null)) !== null &&
return ($helper = static::get($option, null)) !== null &&
method_exists($helper, $method);
}
@@ -280,7 +333,7 @@ abstract class Helper
public static function _(string $method, array $arguments = [], ?string $option = null)
{
// get the helper class
if (($helper = self::get($option, null)) !== null &&
if (($helper = static::get($option, null)) !== null &&
method_exists($helper, $method))
{
// we know this is not ideal...
@@ -291,6 +344,77 @@ abstract class Helper
return null;
}
/**
* Returns a Model object based on the specified type, prefix, and configuration.
*
* @param string $type The model type to instantiate. Must not be empty.
* @param string $prefix Prefix for the model class name. Optional, defaults to 'Administrator'.
* @param string|null $option The component option. Optional, defaults to the component's option.
* @param array $config Configuration array for the model. Optional, defaults to an empty array.
*
* @return BaseDatabaseModel The instantiated model object.
*
* @throws \InvalidArgumentException If the $type parameter is empty.
* @throws \Exception For other errors that may occur during model creation.
*
* @since 5.0.3
*/
public static function getModel(string $type, string $prefix = 'Administrator',
?string $option = null, array $config = []): BaseDatabaseModel
{
// Ensure the $type parameter is not empty
if (empty($type))
{
throw new \InvalidArgumentException('The $type parameter cannot be empty when calling Component Helper getModel method.');
}
// Ensure the $option parameter is set, defaulting to the component's option if not provided
if (empty($option))
{
$option = static::getOption();
}
// Normalize the model type name if the first character is not uppercase
if (!ctype_upper($type[0]))
{
$type = StringHelper::safe($type, 'F');
}
// Normalize the prefix if it's not 'Site' or 'Administrator'
if ($prefix !== 'Site' && $prefix !== 'Administrator')
{
$prefix = static::getPrefixFromModelPath($prefix);
}
// Instantiate and return the model using the MVCFactory
return Factory::getApplication()
->bootComponent($option)
->getMVCFactory()
->createModel($type, $prefix, $config);
}
/**
* Get the prefix from the model path
*
* @param string $path The model path
*
* @return string The prefix value
* @since 5.0.3
*/
private static function getPrefixFromModelPath(string $path): string
{
// Check if $path starts with JPATH_ADMINISTRATOR path
if (str_starts_with($path, JPATH_ADMINISTRATOR . '/components/'))
{
return 'Administrator';
}
// Check if $path starts with JPATH_SITE path
elseif (str_starts_with($path, JPATH_SITE . '/components/'))
{
return 'Site';
}
return 'Administrator';
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,327 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 3rd September, 2020
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Utilities;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\Path;
use VDM\Joomla\Utilities\Component\Helper;
use VDM\Joomla\Utilities\MimeHelper;
use VDM\Joomla\Utilities\StringHelper;
/**
* Upload Helper
*
* @since 3.0.11
*/
abstract class UploadHelper
{
/**
* True to use streams
*
* @var bool
*
* @since 3.0.11
*/
public static bool $useStreams = false;
/**
* Allow the upload of unsafe files
*
* @var bool
*
* @since 3.0.11
*/
public static bool $allowUnsafe = false;
/**
* Options to InputFilter::isSafeFile
*
* @var array
*
* @since 3.0.11
*/
public static array $safeFileOptions = [];
/**
* Set the error behavior
*
* @var bool
*
* @since 3.0.11
*/
public static bool $enqueueError = true;
/**
* Legal Formats
*
* @var array
*
* @since 5.0.3
*/
public static array $legalFormats = [];
/**
* Errors
*
* @var array
*
* @since 3.0.11
*/
protected static array $errors = [];
/**
* Get file/files from a HTTP upload.
*
* @param string $field The input field name
* @param string $type The file type
* @param string|null $filter The filter to use when uploading the file
* @param string|null $path The path to the directory where the file must be placed
*
* @return array|null File details or false on failure.
* @since 3.0.11
*/
public static function get(string $field, string $type, string $filter = null, string $path = null): ?array
{
// Get the uploaded file information.
$input = Factory::getApplication()->input;
// set the default filter
if (empty($filter))
{
$filter = 'array';
}
// if raw then also unsafe
// see: https://github.com/joomla/joomla-cms/blob/4.1-dev/administrator/components/com_installer/src/Model/InstallModel.php#L259
elseif ($filter === 'raw')
{
static::$allowUnsafe = true;
}
// check if we have a file destination name in the field name
$name = null;
if (strpos($field, ':') !== false)
{
list($field, $name) = explode(':', $field);
}
// See JInputFiles::get.
$userfile = $input->files->get($field, null, $filter);
// Make sure that file uploads are enabled in php.
if (!(bool) ini_get('file_uploads'))
{
static::setError(Text::_('COM_COMPONENTBUILDER_WARNING_UPLOAD_ERROR'));
return null;
}
// If there is no uploaded file, we have a problem...
if (!is_array($userfile))
{
static::setError(Text::_('COM_COMPONENTBUILDER_NO_UPLOAD_SELECTED'));
return null;
}
// Is the PHP tmp directory missing?
if ($userfile['error'] && ($userfile['error'] == UPLOAD_ERR_NO_TMP_DIR))
{
static::setError(Text::_('COM_COMPONENTBUILDER_THERE_WAS_AN_ERROR_UPLOADING_TO_THE_SERVER') . '<br>' . Text::_('COM_COMPONENTBUILDER_THE_PHP_TEMPORARY_FOLDER_IS_NOT_SET'));
return null;
}
// Is the max upload size too small in php.ini?
if ($userfile['error'] && ($userfile['error'] == UPLOAD_ERR_INI_SIZE))
{
static::setError(Text::_('COM_COMPONENTBUILDER_THERE_WAS_AN_ERROR_UPLOADING_TO_THE_SERVER') . '<br>' . Text::_('COM_COMPONENTBUILDER_YOUR_FILE_WAS_IS_LARGER_THAN_THE_ALLOWED_SIZE'));
return null;
}
// Check if there was a different problem uploading the file.
if ($userfile['error'] || $userfile['size'] < 1)
{
static::setError(Text::_('COM_COMPONENTBUILDER_THERE_WAS_AN_ERROR_UPLOADING_TO_THE_SERVER'));
return null;
}
// check if a path was passed and exist
if (is_string($path) && Folder::create($path))
{
// set the path
$userfile['path'] = $path;
}
else
{
// get the Joomla config class
$config = Factory::getConfig();
// set the path
$userfile['path'] = $config->get('tmp_path');
}
// set the random part of the name
$userfile['random'] = StringHelper::random(12);
// set the file name
if (empty($name))
{
// set the file name
$userfile['file_name'] = $userfile['random'] . $userfile['name'];
}
else
{
// check that his name has file format
if (is_string($name) && strpos($name, '.') === false)
{
$name = $name . '.' . MimeHelper::extension($userfile['name']);
}
$userfile['file_name'] = $name;
}
// set full path
$userfile['full_path'] = Path::clean($userfile['path'] . '/' . $userfile['file_name']);
// Upload the file.
if (File::upload($userfile['tmp_name'], $userfile['full_path'], static::$useStreams, static::$allowUnsafe))
{
// Check that this is a valid file
return static::check($userfile, $type);
}
return null;
}
/**
* Get the errors
*
* @param bool $toString The option to return errors as a string
*
* @return array|string
* @since 3.0.11
*/
public static function getError($toString = false)
{
if ($toString)
{
return implode(' ' . PHP_EOL, static::$errors);
}
return static::$errors;
}
/**
* Check a file and verifies it as a allowed file format file
*
* @param array $upload The uploaded details array
* @param string $type The file type
*
* @return array|null of elements
*
*/
protected static function check(array $upload, string $type): ?array
{
// Default extensions/formats
$extensions = MimeHelper::getFileExtensions($type);
// Clean the path
$upload_path = Path::clean($upload['full_path']);
// Get file extension/format
$extension = MimeHelper::extension($upload_path);
$mime = $upload['type'];
unset($upload['type']);
// set to check
$checking_mime = MimeHelper::mimeType($upload_path);
// Legal file formats
$legal = [];
// check if the file format is even in the list
if (in_array($extension, $extensions))
{
// get allowed formats
$legal_formats = (array) Helper::getParams()->get($type . '_formats', []);
$legal_extensions = array_values(array_unique(array_merge($legal_formats, static::$legalFormats)));
}
// check the extension
if (!in_array($extension, $legal_extensions))
{
// Cleanup the import file
static::remove($upload['full_path']);
static::setError(Text::_('COM_COMPONENTBUILDER_UPLOAD_IS_NOT_A_VALID_TYPE'));
return null;
}
if ($checking_mime === $mime)
{
$upload['mime'] = $mime; // TODO we should keep and eye on this.
}
$upload['extension'] = $extension;
return $upload;
}
/**
* Clean up temporary uploaded file
*
* @param string $fullPath The full path of the uploaded file
*
* @return boolean True on success
*
*/
protected static function remove($fullPath)
{
// Is the package file a valid file?
if (is_file($fullPath))
{
File::delete($fullPath);
}
elseif (is_file(Path::clean($fullPath)))
{
// It might also be just a base filename
File::delete(Path::clean($fullPath));
}
}
/**
* Set the errors
*
* @param string $message The error message
*
* @return void
* @since 3.0.11
*/
protected static function setError($message)
{
if (static::$enqueueError)
{
Factory::getApplication()->enqueueMessage($message, 'error');
}
else
{
static::$errors[] = $message;
}
}
}