2022-09-14 15:40:02 +00:00
|
|
|
<?php
|
2022-09-20 11:06:03 +00:00
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
|
|
|
* @created 3rd September, 2022
|
|
|
|
* @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
|
|
|
|
*/
|
2022-09-14 15:40:02 +00:00
|
|
|
|
|
|
|
namespace VDM\Joomla\Componentbuilder\Search;
|
|
|
|
|
|
|
|
|
2022-10-30 22:34:54 +00:00
|
|
|
use Joomla\CMS\Language\Text;
|
2022-09-14 15:40:02 +00:00
|
|
|
use VDM\Joomla\Componentbuilder\Search\Factory;
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Config;
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Database\Get;
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Database\Set;
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Agent\Find;
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Agent\Replace;
|
|
|
|
use VDM\Joomla\Componentbuilder\Search\Agent\Search;
|
2022-10-20 14:40:18 +00:00
|
|
|
use VDM\Joomla\Componentbuilder\Search\Agent\Update;
|
2022-10-30 22:34:54 +00:00
|
|
|
use VDM\Joomla\Componentbuilder\Search\Table;
|
2022-09-14 15:40:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search Agent
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
class Agent
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Search Config
|
|
|
|
*
|
|
|
|
* @var Config
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Config $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search Get Database
|
|
|
|
*
|
|
|
|
* @var Get
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Get $get;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search Set Database
|
|
|
|
*
|
|
|
|
* @var Set
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Set $set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search Find
|
|
|
|
*
|
|
|
|
* @var Find
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Find $find;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search Replace
|
|
|
|
*
|
|
|
|
* @var Replace
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Replace $replace;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search
|
|
|
|
*
|
|
|
|
* @var Search
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Search $search;
|
|
|
|
|
2022-10-20 14:40:18 +00:00
|
|
|
/**
|
|
|
|
* Update
|
|
|
|
*
|
|
|
|
* @var Update
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Update $update;
|
|
|
|
|
2022-10-30 22:34:54 +00:00
|
|
|
/**
|
|
|
|
* Table
|
|
|
|
*
|
|
|
|
* @var Table
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected Table $table;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value to search view
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected string $return;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marker start and end values
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected array $marker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marker start and end html values
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected array $markerHtml;
|
|
|
|
|
2022-09-14 15:40:02 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2022-10-30 22:34:54 +00:00
|
|
|
* @param Config|null $config The search config object.
|
|
|
|
* @param Get|null $get The search get database object.
|
|
|
|
* @param Set|null $set The search get database object.
|
|
|
|
* @param Find|null $find The search find object.
|
|
|
|
* @param Replace|null $replace The search replace object.
|
|
|
|
* @param Search|null $search The search object.
|
|
|
|
* @param Update|null $update The update object.
|
|
|
|
* @param Table|null $table The table object.
|
2022-09-14 15:40:02 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function __construct(?Config $config = null, ?Get $get = null,
|
|
|
|
?Set$set = null, ?Find $find = null, ?Replace $replace = null,
|
2022-10-30 22:34:54 +00:00
|
|
|
?Search $search = null, ?Update $update = null, ?Table $table = null)
|
2022-09-14 15:40:02 +00:00
|
|
|
{
|
|
|
|
$this->config = $config ?: Factory::_('Config');
|
|
|
|
$this->get = $get ?: Factory::_('Get.Database');
|
|
|
|
$this->set = $set ?: Factory::_('Set.Database');
|
|
|
|
$this->find = $find ?: Factory::_('Agent.Find');
|
|
|
|
$this->replace = $replace ?: Factory::_('Agent.Replace');
|
|
|
|
$this->search = $search ?: Factory::_('Agent.Search');
|
2022-10-20 14:40:18 +00:00
|
|
|
$this->update = $update ?: Factory::_('Agent.Update');
|
2022-10-30 22:34:54 +00:00
|
|
|
$this->table = $table ?: Factory::_('Table');
|
2022-10-20 14:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of a field in a row and table
|
|
|
|
*
|
|
|
|
* @param int $id The item ID
|
|
|
|
* @param string $field The field key
|
|
|
|
* @param mixed $line The field line
|
|
|
|
* @param string|null $table The table
|
|
|
|
* @param bool $update The switch to triger an update (default is false)
|
|
|
|
*
|
2022-10-30 22:34:54 +00:00
|
|
|
* @return string
|
2022-10-20 14:40:18 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function getValue(int $id, string $field, $line = null,
|
2022-10-30 22:34:54 +00:00
|
|
|
?string $table = null, bool $update = false): string
|
2022-10-20 14:40:18 +00:00
|
|
|
{
|
|
|
|
// set the table name
|
|
|
|
if (empty($table))
|
|
|
|
{
|
|
|
|
$table = $this->config->table_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($value = $this->get->value($id, $field, $table)) !== null)
|
|
|
|
{
|
2022-10-30 22:34:54 +00:00
|
|
|
// we only return strings that can load in an editor
|
|
|
|
if (is_string($value))
|
2022-10-20 14:40:18 +00:00
|
|
|
{
|
2022-10-30 22:34:54 +00:00
|
|
|
// try to update the value if required
|
|
|
|
if ($update && ($updated_value = $this->update->value($value, $line)) !== null)
|
|
|
|
{
|
|
|
|
return $updated_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2022-10-20 14:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 22:34:54 +00:00
|
|
|
return '// VALUE CAN NOT BE LOADED (AT THIS TIME) SINCE ITS NOT A STRING';
|
2022-10-20 14:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a field in a row and table
|
|
|
|
*
|
|
|
|
* @param mixed $value The field value
|
|
|
|
* @param int $id The item ID
|
|
|
|
* @param string $field The field key
|
|
|
|
* @param string|null $table The table
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function setValue($value, int $id, string $field, ?string $table = null): bool
|
|
|
|
{
|
|
|
|
// set the table name
|
|
|
|
if (empty($table))
|
|
|
|
{
|
|
|
|
$table = $this->config->table_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->set->value($value, $id, $field, $table);
|
2022-09-14 15:40:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 22:34:54 +00:00
|
|
|
/**
|
|
|
|
* Return Table Ready Search Results
|
|
|
|
*
|
|
|
|
* @param string|null $table The table being searched
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function table(?string $table = null): ?array
|
|
|
|
{
|
|
|
|
// set the table name
|
|
|
|
if (empty($table))
|
|
|
|
{
|
|
|
|
$table = $this->config->table_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(($values = $this->find($table)) !== null)
|
|
|
|
{
|
2022-11-02 19:48:20 +00:00
|
|
|
// build return value
|
|
|
|
$this->setReturnValue();
|
2022-10-30 22:34:54 +00:00
|
|
|
|
|
|
|
// set the markers
|
2022-11-02 19:48:20 +00:00
|
|
|
$this->setMarkers();
|
|
|
|
|
|
|
|
// start table bucket
|
|
|
|
$table_rows = [];
|
2022-10-30 22:34:54 +00:00
|
|
|
|
|
|
|
foreach ($values as $id => $fields)
|
|
|
|
{
|
|
|
|
foreach ($fields as $field => $lines)
|
|
|
|
{
|
|
|
|
foreach ($lines as $line => $code)
|
|
|
|
{
|
|
|
|
$table_rows[] = $this->getRow($code, $table, $field, $id, $line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $table_rows;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-14 15:40:02 +00:00
|
|
|
/**
|
|
|
|
* Search the posted table for the search value and return all
|
|
|
|
*
|
|
|
|
* @param string|null $table The table being searched
|
|
|
|
*
|
|
|
|
* @return array|null
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function find(?string $table = null): ?array
|
|
|
|
{
|
|
|
|
// set the table name
|
|
|
|
if (empty($table))
|
|
|
|
{
|
|
|
|
$table = $this->config->table_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = 1;
|
|
|
|
|
|
|
|
// continue loading items until all are searched
|
|
|
|
while(($items = $this->get->items($table, $set)) !== null)
|
|
|
|
{
|
|
|
|
$this->find->items($items, $table);
|
|
|
|
$set++;
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:41:41 +00:00
|
|
|
return $this->search->get($table);
|
2022-09-14 15:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search the posted table for the search value, and replace all
|
|
|
|
*
|
|
|
|
* @param string|null $table The table being searched
|
|
|
|
*
|
2022-11-02 19:48:20 +00:00
|
|
|
* @return int
|
2022-09-14 15:40:02 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2022-11-02 19:48:20 +00:00
|
|
|
public function replace(?string $table = null): int
|
2022-09-14 15:40:02 +00:00
|
|
|
{
|
|
|
|
// set the table name
|
|
|
|
if (empty($table))
|
|
|
|
{
|
|
|
|
$table = $this->config->table_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = 1;
|
2022-11-02 19:48:20 +00:00
|
|
|
$replaced = 0;
|
2022-09-14 15:40:02 +00:00
|
|
|
|
|
|
|
// continue loading items until all was loaded
|
|
|
|
while(($items = $this->get->items($table, $set)) !== null)
|
|
|
|
{
|
|
|
|
// search for items
|
|
|
|
$this->find->items($items, $table);
|
|
|
|
|
|
|
|
// update those found
|
|
|
|
$this->replace->items($this->find->get($table), $table);
|
|
|
|
|
|
|
|
// update the database
|
2022-11-02 19:48:20 +00:00
|
|
|
if ($this->set->items($this->replace->get($table), $table))
|
|
|
|
{
|
|
|
|
$replaced++;
|
|
|
|
}
|
2022-09-14 15:40:02 +00:00
|
|
|
|
|
|
|
// reset found items
|
|
|
|
$this->find->reset($table);
|
|
|
|
$this->replace->reset($table);
|
|
|
|
|
|
|
|
$set++;
|
|
|
|
}
|
2022-11-02 19:48:20 +00:00
|
|
|
|
|
|
|
// we return the number of times we replaced
|
|
|
|
return $replaced;
|
2022-09-14 15:40:02 +00:00
|
|
|
}
|
2022-10-30 22:34:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return prepared code string for table
|
|
|
|
*
|
|
|
|
* @param string $code The code value fro the table
|
|
|
|
* @param string|null $table The table
|
|
|
|
* @param string $field The field key
|
|
|
|
* @param int $id The the row id
|
|
|
|
* @param mixed $line The code line where found
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected function getRow(string $code, string $table, string $field, int $id, $line): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'edit' => $this->getRowEditButton($table, $field, $id, $line),
|
|
|
|
'code' => $this->getRowCode($code),
|
|
|
|
'table' => $table,
|
|
|
|
'field' => $field,
|
|
|
|
'id' => $id,
|
|
|
|
'line' => $line
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return prepared code string for table
|
|
|
|
*
|
|
|
|
* @param string $code The code value fro the table
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected function getRowCode(string $code): string
|
|
|
|
{
|
|
|
|
return str_replace($this->marker, $this->markerHtml, htmlentities($code));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Item button to edit an item
|
|
|
|
*
|
|
|
|
* @param string|null $view The single view
|
|
|
|
* @param string $field The field key
|
|
|
|
* @param int $id The the row id
|
|
|
|
* @param mixed $line The code line where found
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected function getRowEditButton(string $view, string $field, int $id, $line): string
|
|
|
|
{
|
|
|
|
// get list view
|
|
|
|
$views = $this->table->get($view, $field, 'list');
|
2022-11-02 19:48:20 +00:00
|
|
|
$tab = $this->table->get($view, $field, 'tab_name');
|
2022-10-30 22:34:54 +00:00
|
|
|
|
|
|
|
// return edit link
|
2022-11-02 19:48:20 +00:00
|
|
|
return '<a class="hasTooltip btn btn-mini" href="index.php?option=com_componentbuilder' .
|
|
|
|
'&view=' . $views .
|
|
|
|
'&task=' . $view . '.edit' .
|
|
|
|
'&id=' . $id .
|
|
|
|
'&open_tab=' . $tab .
|
|
|
|
'&open_field=' . $field .
|
|
|
|
'&return=' . $this->return . '" title="' .
|
|
|
|
Text::sprintf('COM_COMPONENTBUILDER_EDIT_S_S_DIRECTLY', $view, $field) . '." ><span class="icon-edit"></span></a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the return value for this search
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected function setReturnValue()
|
|
|
|
{
|
|
|
|
// set the return value so the search auto load on return
|
|
|
|
$this->return = urlencode(base64_encode('index.php?option=com_componentbuilder&view=search' .
|
|
|
|
'&type_search=' . (int) $this->config->type_search .
|
|
|
|
'&match_case=' . (int) $this->config->match_case .
|
|
|
|
'&whole_word=' . (int) $this->config->whole_word .
|
|
|
|
'®ex_search=' . (int) $this->config->regex_search .
|
|
|
|
'&search_value=' . (string) urlencode($this->config->search_value) .
|
|
|
|
'&replace_value=' . (string) urlencode($this->config->replace_value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the markers of the found code
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected function setMarkers()
|
|
|
|
{
|
|
|
|
// set the markers
|
|
|
|
$this->marker = [$this->config->marker_start, $this->config->marker_end];
|
|
|
|
$this->markerHtml = ['<span class="found_code">','</span>'];
|
2022-10-30 22:34:54 +00:00
|
|
|
}
|
2022-09-14 15:40:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|