Moved many field related get methods to its own classes

This commit is contained in:
2022-09-10 10:12:13 +02:00
parent 4a32d3d50e
commit c5a85f167e
34 changed files with 2406 additions and 32 deletions

View File

@@ -0,0 +1,232 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Dispenser;
/**
* Compiler Field Customcode
*
* @since 3.2.0
*/
class Customcode
{
/**
* Tracking the update of fields per/view
*
* @var array
* @since 3.2.0
*/
protected array $views;
/**
* Compiler Customcode Dispenser
*
* @var Dispenser
* @since 3.2.0
*/
protected Dispenser $dispenser;
/**
* Constructor
*
* @param Dispenser|null $dispenser The compiler customcode dispenser object.
*
* @since 3.2.0
*/
public function __construct(?Dispenser $dispenser = null)
{
$this->dispenser = $dispenser ?: Compiler::_('Customcode.Dispenser');
}
/**
* Update field customcode
*
* @param int $id The field id
* @param object $field The field object
* @param string|null $singleViewName The view edit or single name
* @param string|null $listViewName The view list name
*
* @return void
* @since 3.2.0
*/
public function update(int $id, object &$field, $singleViewName = null, $listViewName = null)
{
// check if we should load scripts for single view
if ($singleViewName && StringHelper::check($singleViewName)
&& !isset($this->views[$singleViewName][$id]))
{
// add_javascript_view_footer
if ($field->add_javascript_view_footer == 1
&& StringHelper::check(
$field->javascript_view_footer
))
{
$convert__ = true;
if (isset($field->javascript_view_footer_decoded)
&& $field->javascript_view_footer_decoded)
{
$convert__ = false;
}
$this->dispenser->set(
$field->javascript_view_footer,
'view_footer',
$singleViewName,
null,
array(
'table' => 'field',
'id' => (int) $id,
'field' => 'javascript_view_footer',
'type' => 'js',
'prefix' => PHP_EOL),
$convert__,
$convert__,
true
);
if (!isset($field->javascript_view_footer_decoded))
{
$field->javascript_view_footer_decoded
= true;
}
if (strpos($field->javascript_view_footer, "token") !== false
|| strpos($field->javascript_view_footer, "task=ajax") !== false)
{
if (!isset($this->dispenser->hub['token']))
{
$this->dispenser->hub['token'] = [];
}
if (!isset($this->dispenser->hub['token'][$singleViewName])
|| !$this->dispenser->hub['token'][$singleViewName])
{
$this->dispenser->hub['token'][$singleViewName]
= true;
}
}
}
// add_css_view
if ($field->add_css_view == 1)
{
$convert__ = true;
if (isset($field->css_view_decoded)
&& $field->css_view_decoded)
{
$convert__ = false;
}
$this->dispenser->set(
$field->css_view,
'css_view',
$singleViewName,
null,
array('prefix' => PHP_EOL),
$convert__,
$convert__,
true
);
if (!isset($field->css_view_decoded))
{
$field->css_view_decoded = true;
}
}
// add this only once to single view.
$this->views[$singleViewName][$id] = true;
}
// check if we should load scripts for list views
if ($listViewName && StringHelper::check($listViewName)
&& !isset($this->views[$listViewName][$id]))
{
// add_javascript_views_footer
if ($field->add_javascript_views_footer == 1
&& StringHelper::check(
$field->javascript_views_footer
))
{
$convert__ = true;
if (isset($field->javascript_views_footer_decoded)
&& $field->javascript_views_footer_decoded)
{
$convert__ = false;
}
$this->dispenser->set(
$field->javascript_views_footer,
'views_footer',
$singleViewName,
null,
array(
'table' => 'field',
'id' => (int) $id,
'field' => 'javascript_views_footer',
'type' => 'js',
'prefix' => PHP_EOL),
$convert__,
$convert__,
true
);
if (!isset($field->javascript_views_footer_decoded))
{
$field->javascript_views_footer_decoded = true;
}
if (strpos($field->javascript_views_footer, "token") !== false
|| strpos($field->javascript_views_footer, "task=ajax") !== false)
{
if (!isset($this->dispenser->hub['token']))
{
$this->dispenser->hub['token'] = [];
}
if (!isset($this->dispenser->hub['token'][$listViewName])
|| !$this->dispenser->hub['token'][$listViewName])
{
$this->dispenser->hub['token'][$listViewName]
= true;
}
}
}
// add_css_views
if ($field->add_css_views == 1)
{
$convert__ = true;
if (isset($field->css_views_decoded)
&& $field->css_views_decoded)
{
$convert__ = false;
}
$this->dispenser->set(
$field->css_views,
'css_views',
$singleViewName,
null,
array('prefix' => PHP_EOL),
$convert__,
$convert__,
true
);
if (!isset($field->css_views_decoded))
{
$field->css_views_decoded = true;
}
}
// add this only once to list view.
$this->views[$listViewName][$id] = true;
}
}
}

View File

@@ -0,0 +1,329 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use Joomla\CMS\Factory;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Utilities\JsonHelper;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Componentbuilder\Compiler\Config;
use VDM\Joomla\Componentbuilder\Compiler\Interfaces\EventInterface;
use VDM\Joomla\Componentbuilder\Compiler\Interfaces\HistoryInterface;
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
use VDM\Joomla\Componentbuilder\Compiler\Customcode;
use VDM\Joomla\Componentbuilder\Compiler\Field\Customcode as FieldCustomcode;
use VDM\Joomla\Componentbuilder\Compiler\Field\Validation;
/**
* Compiler Field Data
*
* @since 3.2.0
*/
class Data
{
/**
* Compiler Fields
*
* @var array
* @since 3.2.0
*/
protected array $fields;
/**
* Compiler Config
*
* @var Config
* @since 3.2.0
*/
protected Config $config;
/**
* Compiler Event
*
* @var EventInterface
* @since 3.2.0
*/
protected EventInterface $event;
/**
* Compiler History
*
* @var HistoryInterface
* @since 3.2.0
*/
protected HistoryInterface $history;
/**
* Compiler Placeholder
*
* @var Placeholder
* @since 3.2.0
*/
protected Placeholder $placeholder;
/**
* Compiler Customcode
*
* @var Customcode
* @since 3.2.0
*/
protected Customcode $customcode;
/**
* Compiler Field Customcode
*
* @var FieldCustomcode
* @since 3.2.0
*/
protected FieldCustomcode $fieldCustomcode;
/**
* Compiler Field Validation
*
* @var Validation
* @since 3.2.0
*/
protected Validation $validation;
/**
* Database object to query local DB
*
* @var \JDatabaseDriver
* @since 3.2.0
**/
protected \JDatabaseDriver $db;
/**
* Constructor
*
* @param Config|null $config The compiler config object.
* @param EventInterface|null $event The compiler event api object.
* @param HistoryInterface|null $history The compiler history object.
* @param Placeholder|null $placeholder The compiler placeholder object.
* @param Customcode|null $customcode The compiler customcode object.
* @param FieldCustomcode|null $fieldCustomcode The field customcode object.
* @param Validation|null $validation The field validation rule object.
* @param \JDatabaseDriver|null $db The database object.
*
* @since 3.2.0
*/
public function __construct(?Config $config = null, ?EventInterface $event = null, ?HistoryInterface $history = null,
?Placeholder $placeholder = null, ?Customcode $customcode = null, ?FieldCustomcode $fieldCustomcode = null,
?Validation $validation = null, ?\JDatabaseDriver $db = null)
{
$this->config = $config ?: Compiler::_('Config');
$this->event = $event ?: Compiler::_('Event');
$this->history = $history ?: Compiler::_('History');
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
$this->customcode = $customcode ?: Compiler::_('Customcode');
$this->fieldCustomcode = $fieldCustomcode ?: Compiler::_('Field.Customcode');
$this->validation = $validation ?: Compiler::_('Field.Validation');
$this->db = $db ?: Factory::getDbo();
}
/**
* Get all Field Data
*
* @param int $id The field ID
* @param string|null $singleViewName The view edit or single name
* @param string|null $listViewName The view list name
*
* @return object|null The field data
* @since 3.2.0
*/
public function get(int $id, ?string $singleViewName = null, ?string $listViewName = null): ?object
{
if ($id > 0 && !isset($this->fields[$id]))
{
// Create a new query object.
$query = $this->db->getQuery(true);
// Select all the values in the field
$query->select('a.*');
$query->select(
$this->db->quoteName(
array('c.name', 'c.properties'),
array('type_name', 'properties')
)
);
$query->from('#__componentbuilder_field AS a');
$query->join(
'LEFT',
$this->db->quoteName('#__componentbuilder_fieldtype', 'c')
. ' ON (' . $this->db->quoteName('a.fieldtype') . ' = '
. $this->db->quoteName('c.id') . ')'
);
$query->where(
$this->db->quoteName('a.id') . ' = ' . $this->db->quote($id)
);
// TODO we need to update the event signatures
$context = $this->config->component_context;
// Trigger Event: jcb_ce_onBeforeQueryFieldData
$this->event->trigger(
'jcb_ce_onBeforeQueryFieldData',
array(&$context, &$id, &$query, &$this->db)
);
// Reset the query using our newly populated query object.
$this->db->setQuery($query);
$this->db->execute();
if ($this->db->getNumRows())
{
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$field = $this->db->loadObject();
// Trigger Event: jcb_ce_onBeforeModelFieldData
$this->event->trigger(
'jcb_ce_onBeforeModelFieldData',
array(&$context, &$field)
);
// adding a fix for the changed name of type to fieldtype
$field->type = $field->fieldtype;
// load the values form params
$field->xml = $this->customcode->update(json_decode($field->xml));
// check if we have validate (validation rule and set it if found)
$this->validation->set($id, $field->xml);
// load the type values form type params
$field->properties = (isset($field->properties)
&& JsonHelper::check($field->properties))
? json_decode($field->properties, true) : null;
if (ArrayHelper::check($field->properties))
{
$field->properties = array_values($field->properties);
}
// check if we have WHMCS encryption
if (4 == $field->store
&& !$this->config->whmcs_encryption)
{
$this->config->whmcs_encryption = true;
}
// check if we have basic encryption
elseif (3 == $field->store
&& !$this->config->basic_encryption)
{
$this->config->basic_encryption = true;
}
// check if we have better encryption
elseif (5 == $field->store
&& $this->config->medium_encryption)
{
$this->config->medium_encryption = true;
}
// check if we have better encryption
elseif (6 == $field->store
&& StringHelper::check(
$field->on_get_model_field
)
&& StringHelper::check(
$field->on_save_model_field
))
{
// add only if string lenght found
if (StringHelper::check(
$field->initiator_on_save_model
))
{
$field->initiator_save_key = md5(
$field->initiator_on_save_model
);
$field->initiator_save = explode(
PHP_EOL, $this->placeholder->update(
$this->customcode->update(
base64_decode(
$field->initiator_on_save_model
)
), $this->placeholder->active
)
);
}
if (StringHelper::check(
$field->initiator_on_save_model
))
{
$field->initiator_get_key = md5(
$field->initiator_on_get_model
);
$field->initiator_get = explode(
PHP_EOL, $this->placeholder->update(
$this->customcode->update(
base64_decode(
$field->initiator_on_get_model
)
), $this->placeholder->active
)
);
}
// set the field modeling
$field->model_field['save'] = explode(
PHP_EOL, $this->placeholder->update(
$this->customcode->update(
base64_decode($field->on_save_model_field)
), $this->placeholder->active
)
);
$field->model_field['get'] = explode(
PHP_EOL, $this->placeholder->update(
$this->customcode->update(
base64_decode($field->on_get_model_field)
), $this->placeholder->active
)
);
// remove the original values
unset(
$field->on_save_model_field,
$field->on_get_model_field,
$field->initiator_on_save_model,
$field->initiator_on_get_model
);
}
// get the last used version
$field->history = $this->history->get('field', $id);
// Trigger Event: jcb_ce_onAfterModelFieldData
$this->event->trigger(
'jcb_ce_onAfterModelFieldData',
array(&$context, &$field)
);
$this->fields[$id] = $field;
}
else
{
return null;
}
}
if ($id > 0 && isset($this->fields[$id]))
{
// update the customcode of the field
$this->fieldCustomcode->update($id, $this->fields[$id], $singleViewName, $listViewName);
// return the field
return $this->fields[$id];
}
return null;
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Componentbuilder\Compiler\Registry;
/**
* Compiler Field Database Name
*
* @since 3.2.0
*/
class DatabaseName
{
/**
* The compiler registry
*
* @var Registry
* @since 3.2.0
*/
protected Registry $registry;
/**
* Constructor
*
* @param Registry|null $config The compiler registry object.
*
* @since 3.2.0
*/
public function __construct(?Registry $registry = null)
{
$this->registry = $registry ?: Compiler::_('Registry');
}
/**
* get the field database name and AS prefix
*
* @param string $nameListCode The list view name
* @param int $fieldId The field ID
* @param string $targetArea The area being targeted
*
* @return string|null
* @since 3.2.0
*/
public function get(string $nameListCode, int $fieldId, string $targetArea = 'builder.list'): ?string
{
if (($fields = $this->registry->get("${targetArea}.${nameListCode}")) !== null)
{
if ($fieldId < 0)
{
switch ($fieldId)
{
case -1:
return 'a.id';
case -2:
return 'a.ordering';
case -3:
return 'a.published';
}
}
foreach ($fields as $field)
{
if ($field['id'] == $fieldId)
{
// now check if this is a category
if ($field['type'] === 'category')
{
return 'c.title';
}
// set the custom code
elseif (ArrayHelper::check(
$field['custom']
))
{
return $field['custom']['db'] . "."
. $field['custom']['text'];
}
else
{
return 'a.' . $field['code'];
}
}
}
}
return null;
}
}

View File

@@ -0,0 +1,114 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field\JoomlaThree;
use Joomla\CMS\Filesystem\Folder;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Componentbuilder\Compiler\Interfaces\Field\CoreValidationInterface;
/**
* Core Joomla Field Validation Rules
*
* @since 3.2.0
*/
class CoreValidation implements CoreValidationInterface
{
/**
* Local Core Joomla Rules
*
* @var array|null
* @since 3.2.0
**/
protected ?array $rules = null;
/**
* Local Core Joomla Rules Path
*
* @var string
* @since 3.2.0
**/
protected string $path;
/**
* Constructor
*
* @since 3.2.0
*/
public function __construct()
{
// set the path to the form validation rules
$this->path = JPATH_LIBRARIES . '/src/Form/Rule';
}
/**
* Get the Array of Existing Validation Rule Names
*
* @param bool $lowercase Switch to set rules lowercase
*
* @return array
* @since 3.2.0
*/
public function get(bool $lowercase = false): array
{
if (!$this->rules)
{
// check if the path exist
if (!Folder::exists($this->path))
{
return [];
}
// we must first store the current working directory
$joomla = getcwd();
// go to that folder
chdir($this->path);
// load all the files in this path
$rules = Folder::files('.', '\.php', true, true);
// change back to Joomla working directory
chdir($joomla);
// make sure we have an array
if (!ArrayHelper::check($rules))
{
return false;
}
// remove the Rule.php from the name
$this->rules = array_map( function ($name) {
return str_replace(array('./','Rule.php'), '', $name);
}, $rules);
}
// return rules if found
if (is_array($this->rules))
{
// check if the names should be all lowercase
if ($lowercase)
{
return array_map( function($item) {
return strtolower($item);
}, $this->rules);
}
return $this->rules;
}
// return empty array
return [];
}
}

View File

@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>

View File

@@ -0,0 +1,234 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\String\TypeHelper;
use VDM\Joomla\Utilities\String\FieldHelper;
use VDM\Joomla\Utilities\GetHelper;
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
use VDM\Joomla\Componentbuilder\Compiler\Field\UniqueName;
use VDM\Joomla\Componentbuilder\Compiler\Registry;
/**
* Compiler Field Name
*
* @since 3.2.0
*/
class Name
{
/**
* The compiler registry
*
* @var Registry
* @since 3.2.0
*/
protected Registry $registry;
/**
* Unique Field Names
*
* @var array
* @since 3.2.0
*/
protected array $unique;
/**
* Compiler Placeholder
*
* @var Placeholder
* @since 3.2.0
*/
protected Placeholder $placeholder;
/**
* Compiler Field Unique Name
*
* @var UniqueName
* @since 3.2.0
*/
protected UniqueName $uniqueName;
/**
* Constructor
*
* @param Placeholder|null $placeholder The compiler component placeholder object.
* @param UniqueName|null $uniqueName The compiler field unique name object.
* @param Registry|null $registry The compiler registry object.
*
* @since 3.2.0
*/
public function __construct(?Placeholder $placeholder = null, ?UniqueName $uniqueName = null, ?Registry $registry = null)
{
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
$this->uniqueName = $uniqueName ?: Compiler::_('Field.Unique.Name');
$this->registry = $registry ?: Compiler::_('Registry');
}
/**
* Get the field's actual name
*
* @param array $field The field array
* @param string|null $listViewName The list view name
* @param string $amicably The peaceful resolve (for fields in subforms in same view :)
*
* @return string Success returns field name
* @since 3.2.0
*/
public function get(array &$field, ?string $listViewName = null, string $amicably = ''): string
{
// return the unique name if already set
if ($listViewName && StringHelper::check($listViewName)
&& isset($field['hash'])
&& isset(
$this->unique[$listViewName . $amicably . $field['hash']]
))
{
return $this->unique[$listViewName . $amicably . $field['hash']];
}
// always make sure we have a field name and type
if (!isset($field['settings']) || !isset($field['settings']->type_name)
|| !isset($field['settings']->name))
{
return 'error';
}
// set the type name
$type_name = TypeHelper::safe(
$field['settings']->type_name
);
// set the name of the field
$name = FieldHelper::safe($field['settings']->name);
// check that we have the properties
if (ArrayHelper::check($field['settings']->properties))
{
foreach ($field['settings']->properties as $property)
{
if ($property['name'] === 'name')
{
// if category then name must be catid (only one per view)
if ($type_name === 'category')
{
// quick check if this is a category linked to view page
$requeSt_id = GetHelper::between(
$field['settings']->xml, 'name="', '"'
);
if (strpos($requeSt_id, '_request_id') !== false
|| strpos($requeSt_id, '_request_catid') !== false)
{
// keep it then, don't change
$name = $this->placeholder->update(
$requeSt_id, $this->placeholder->active
);
}
else
{
$name = 'catid';
}
// if list view name is set
if (StringHelper::check($listViewName))
{
// check if we should use another Text Name as this views name
$otherName = $this->placeholder->update(
GetHelper::between(
$field['settings']->xml, 'othername="', '"'
), $this->placeholder->active
);
$otherViews = $this->placeholder->update(
GetHelper::between(
$field['settings']->xml, 'views="', '"'
), $this->placeholder->active
);
$otherView = $this->placeholder->update(
GetHelper::between(
$field['settings']->xml, 'view="', '"'
), $this->placeholder->active
);
// This is to link other view category
if (StringHelper::check($otherName)
&& StringHelper::check(
$otherViews
) && StringHelper::check(
$otherView
))
{
// set other category details
$this->registry->set("category.other.name.${listViewName}", [
'name' => FieldHelper::safe(
$otherName
),
'views' => StringHelper::safe(
$otherViews
),
'view' => StringHelper::safe(
$otherView
)
]);
}
}
}
// if tag is set then enable all tag options for this view (only one per view)
elseif ($type_name === 'tag')
{
$name = 'tags';
}
// if the field is set as alias it must be called alias
elseif (isset($field['alias']) && $field['alias'])
{
$name = 'alias';
}
else
{
// get value from xml
$xml = FieldHelper::safe(
$this->placeholder->update(
GetHelper::between(
$field['settings']->xml, 'name="', '"'
), $this->placeholder->active
)
);
// check if a value was found
if (StringHelper::check($xml))
{
$name = $xml;
}
}
// exit foreach loop
break;
}
}
}
// return the value unique
if (StringHelper::check($listViewName) && isset($field['hash']))
{
$this->unique[$listViewName . $amicably . $field['hash']]
= $this->uniqueName->get($name, $listViewName . $amicably);
// now return the unique name
return $this->unique[$listViewName . $amicably . $field['hash']];
}
// fall back to global
return $name;
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\ObjectHelper;
use VDM\Joomla\Utilities\GetHelper;
use VDM\Joomla\Utilities\String\TypeHelper;
/**
* Compiler Field Type Name
*
* @since 3.2.0
*/
class TypeName
{
/**
* Get the field's actual type
*
* @param object $field The field object
*
* @return string Success returns field type
* @since 3.2.0
*/
public function get(&$field)
{
// check if we have done this already
if (isset($field['type_name']))
{
return $field['type_name'];
}
// check that we have the properties
if (isset($field['settings'])
&& ObjectHelper::check(
$field['settings']
)
&& isset($field['settings']->properties)
&& ArrayHelper::check(
$field['settings']->properties
))
{
// search for own custom fields
if (strpos($field['settings']->type_name, '@') !== false)
{
// set own custom field
$field['settings']->own_custom = $field['settings']->type_name;
$field['settings']->type_name = 'Custom';
}
// set the type name
$type_name = TypeHelper::safe(
$field['settings']->type_name
);
// if custom (we must use the xml value)
if (strtolower($type_name) === 'custom'
|| strtolower($type_name) === 'customuser')
{
$type = TypeHelper::safe(
GetHelper::between(
$field['settings']->xml, 'type="', '"'
)
);
}
else
{
// loop over properties looking for the type value
foreach ($field['settings']->properties as $property)
{
if ($property['name']
=== 'type') // type field is never adjustable (unless custom)
{
// force the default value
if (isset($property['example'])
&& StringHelper::check(
$property['example']
))
{
$type = TypeHelper::safe(
$property['example']
);
}
// fall back on the xml settings (not ideal)
else
{
$type = TypeHelper::safe(
GetHelper::between(
$field['settings']->xml, 'type="', '"'
)
);
}
// exit foreach loop
break;
}
}
}
// check if the value is set
if (isset($type) && StringHelper::check($type))
{
return $type;
}
// fallback on type name set in name field (not ideal)
else
{
return $type_name;
}
}
// fall back to text
return 'text';
}
}

View File

@@ -0,0 +1,112 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Utilities\String\FieldHelper;
use VDM\Joomla\Componentbuilder\Compiler\Registry;
/**
* Compiler Field Unique Name
*
* @since 3.2.0
*/
class UniqueName
{
/**
* The compiler registry
*
* @var Registry
* @since 3.2.0
*/
protected Registry $registry;
/**
* Constructor
*
* @param Registry|null $registry The compiler registry object.
*
* @since 3.2.0
*/
public function __construct(?Registry $registry = null)
{
$this->registry = $registry ?: Compiler::_('Registry');
}
/**
* Count how many times the same field is used per view
*
* @param string $name The name of the field
* @param string $view The name of the view
*
* @return void
* @since 3.2.0
*/
public function set(string $name, string $view)
{
if (($number = $this->registry->get("unique.names.${view}.counter.${name}")) === null)
{
$this->registry->set("unique.names.${view}.counter.${name}", 1);
return;
}
// count how many times the field is used
$this->registry->set("unique.names.${view}.counter.${name}", ++$number);
return;
}
/**
* Naming each field with an unique name
*
* @param string $name The name of the field
* @param string $view The name of the view
*
* @return string the name
* @since 3.2.0
*/
public function get(string $name, string $view): string
{
// only increment if the field name is used multiple times
if ($this->registry->get("unique.names.${view}.counter.${name}") > 1)
{
$counter = 1;
// set the unique name
$unique_name = FieldHelper::safe(
$name . '_' . $counter
);
while ($this->registry->get("unique.names.${view}.names.${unique_name}") !== null)
{
// increment the number
$counter++;
// try again
$unique_name = FieldHelper::safe(
$name . '_' . $counter
);
}
// set the new name number
$this->registry->set("unique.names.${view}.names.${unique_name}", $counter);
// return the unique name
return $unique_name;
}
return $name;
}
}

View File

@@ -0,0 +1,168 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Componentbuilder\Compiler\Field;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\GetHelper;
use VDM\Joomla\Componentbuilder\Compiler\Registry;
use VDM\Joomla\Componentbuilder\Compiler\Customcode;
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Gui;
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
use VDM\Joomla\Componentbuilder\Compiler\Interfaces\Field\CoreValidationInterface;
/**
* Compiler Field Validation Rules
*
* @since 3.2.0
*/
class Validation
{
/**
* Compiler registry
*
* @var Registry
* @since 3.2.0
*/
protected Registry $registry;
/**
* Compiler Gui
*
* @var Gui
* @since 3.2.0
*/
protected Gui $gui;
/**
* Compiler Placeholder
*
* @var Placeholder
* @since 3.2.0
*/
protected Placeholder $placeholder;
/**
* Compiler Customcode
*
* @var Customcode
* @since 3.2.0
*/
protected Customcode $customcode;
/**
* Compiler Field Core Validation
*
* @var CoreValidationInterface
* @since 3.2.0
*/
protected CoreValidationInterface $validation;
/**
* Constructor
*
* @param Registry|null $registry The compiler registry object.
* @param Gui|null $gui The compiler customcode gui object.
* @param Placeholder|null $placeholder The compiler placeholder object.
* @param Customcode|null $customcode The compiler customcode object.
* @param CoreValidationInterface|null $validation The core validation rule object.
*
* @since 3.2.0
*/
public function __construct(?Registry $registry = null, ?Gui $gui = null, ?Placeholder $placeholder = null,
?Customcode $customcode = null, ?CoreValidationInterface $validation = null)
{
$this->registry = $registry ?: Compiler::_('Registry');
$this->gui = $gui ?: Compiler::_('Customcode.Gui');
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
$this->customcode = $customcode ?: Compiler::_('Customcode');
$this->validation = $validation ?: Compiler::_('Field.Core.Validation');
}
/**
* Set the validation rule
*
* @param int $id The field id
* @param string $field The field string
*
* @return void
* @since 3.2.0
*/
public function set(int $id, string $field)
{
// check if we have validate (validation rule set)
$validation_rule = GetHelper::between(
$field, 'validate="', '"'
);
if (StringHelper::check($validation_rule))
{
// make sure it is lowercase
$validation_rule = StringHelper::safe(
$validation_rule
);
// link this field to this validation (don't move this down)
$this->registry->set("validation.linked.${id}", $validation_rule);
// make sure it is not already set
if ($this->registry->get("validation.rules.${validation_rule}") === null)
{
// get joomla core validation names and make sure this rule is not a core validation rule
if (!in_array($validation_rule, (array) $this->validation->get(true)))
{
// get the class methods for this rule if it exists
if (($php_code = GetHelper::var(
'validation_rule', $validation_rule, 'name', 'php'
)) !== false)
{
// open and set the validation rule
$this->registry->set("validation.rules.${validation_rule}",
$this->gui->set(
$this->placeholder->update(
$this->customcode->update(
base64_decode(
$php_code
)
), $this->placeholder->active
),
array(
'table' => 'validation_rule',
'field' => 'php',
'id' => GetHelper::var(
'validation_rule',
$validation_rule, 'name', 'id'
),
'type' => 'php'
)
)
);
}
else
{
// TODO set the notice that this validation rule is custom and was not found
$this->registry->remove("validation.linked.${id}");
}
}
else
{
// remove link (we only want custom validations linked)
$this->registry->remove("validation.linked.${id}");
}
}
}
}
}

View File

@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>