Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions administrator/components/com_banners/src/Model/ClientModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Joomla\Component\Banners\Administrator\Model;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Versioning\VersionableModelTrait;
Expand Down Expand Up @@ -135,4 +136,54 @@ protected function prepareTable($table)
{
$table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
}

/**
* Override save to prevent duplicate client names when saving as copy.
*
* @param array $data The form data.
*
* @return boolean True on success, false on failure.
*
* @since __DEPLOY_VERSION__
*/
public function save($data)
{
$table = $this->getTable();

// If saving as copy or duplicate exists, generate a unique name
if (isset($data['name'])) {
$table->load(['name' => $data['name']]);

// Only modify name if it's a duplicate
if ($table->id) {
$data['name'] = $this->generateUniqueName($data['name']);
}
}

return parent::save($data);
}

/**
* Generate a unique client name if it already exists.
*
* @param string $name The original client name
*
* @return string Unique client name

* @since __DEPLOY_VERSION__
*/
protected function generateUniqueName($name)
{
$table = $this->getTable();
$baseName = $name;
$i = 2;

// Keep appending numbers until the name is unique
while ($table->load(['name' => $name])) {
$name = $baseName . ' (' . $i . ')';
$i++;
}

return $name;
}
}
1 change: 1 addition & 0 deletions administrator/language/en-GB/com_banners.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ COM_BANNERS_CLIENT_EDIT="Edit Client"
COM_BANNERS_CLIENT_EMPTYSTATE_BUTTON_ADD="Add your first client"
COM_BANNERS_CLIENT_EMPTYSTATE_CONTENT="A Banner Client contains contact details and further information such as tracking preferences."
COM_BANNERS_CLIENT_EMPTYSTATE_TITLE="No Clients have been created yet."
COM_BANNERS_CLIENT_ERROR_DUPLICATE_NAME="A client with this name already exists."
COM_BANNERS_CLIENT_NEW="New Client"
COM_BANNERS_CLIENT_SAVE_SUCCESS="Client saved."
COM_BANNERS_CLIENTS_FILTER_SEARCH_DESC="Search in client name. Prefix with ID: to search for a client ID."
Expand Down
Loading