|
11 | 11 | namespace Joomla\Component\Finder\Administrator\Model; |
12 | 12 |
|
13 | 13 | use Joomla\CMS\Factory; |
| 14 | +use Joomla\CMS\Filter\OutputFilter; |
14 | 15 | use Joomla\CMS\Form\Form; |
15 | 16 | use Joomla\CMS\MVC\Model\AdminModel; |
16 | 17 | use Joomla\Component\Finder\Administrator\Table\FilterTable; |
| 18 | +use Joomla\String\StringHelper; |
17 | 19 |
|
18 | 20 | // phpcs:disable PSR1.Files.SideEffects |
19 | 21 | \defined('_JEXEC') or die; |
@@ -149,4 +151,137 @@ public function getTotal() |
149 | 151 |
|
150 | 152 | return $db->setQuery($query)->loadResult(); |
151 | 153 | } |
| 154 | + |
| 155 | + /** |
| 156 | + * Method to save the form data. |
| 157 | + * |
| 158 | + * Overrides the parent to correctly handle the 'save2copy' task for Finder filters. |
| 159 | + * |
| 160 | + * @param array $data The form data. |
| 161 | + * |
| 162 | + * @return boolean True on success, false on failure. |
| 163 | + * |
| 164 | + * @since __DEPLOY_VERSION__ |
| 165 | + */ |
| 166 | + public function save($data) |
| 167 | + { |
| 168 | + $app = Factory::getApplication(); |
| 169 | + $task = $app->getInput()->get('task', '', 'cmd'); |
| 170 | + |
| 171 | + if ($task === 'save2copy') { |
| 172 | + $data['filter_id'] = 0; |
| 173 | + |
| 174 | + $title = trim((string) ($data['title'] ?? '')); |
| 175 | + $alias = trim((string) ($data['alias'] ?? '')); |
| 176 | + |
| 177 | + if ($alias === '') { |
| 178 | + $alias = OutputFilter::stringURLSafe($title); |
| 179 | + } |
| 180 | + |
| 181 | + // Generate unique title + alias |
| 182 | + list($title, $alias) = $this->generateNewTitleAndAlias($title, $alias); |
| 183 | + |
| 184 | + $data['title'] = $title; |
| 185 | + $data['alias'] = $alias; |
| 186 | + } |
| 187 | + |
| 188 | + return parent::save($data); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Generate a new unique title and alias for a copied filter. |
| 193 | + * Follows the same logic as Joomla's core content models. |
| 194 | + * |
| 195 | + * @param string $title The original title. |
| 196 | + * @param string $alias The original alias. |
| 197 | + * |
| 198 | + * @return array Array with [newTitle, newAlias]. |
| 199 | + * |
| 200 | + * @since __DEPLOY_VERSION__ |
| 201 | + */ |
| 202 | + protected function generateNewTitleAndAlias(string $title, string $alias): array |
| 203 | + { |
| 204 | + $db = $this->getDatabase(); |
| 205 | + |
| 206 | + if (preg_match('/^(.*?)(?:\s\((\d+)\))?$/', $title, $matches)) { |
| 207 | + $baseTitle = trim($matches[1]); |
| 208 | + } else { |
| 209 | + $baseTitle = trim($title); |
| 210 | + } |
| 211 | + |
| 212 | + $baseAlias = trim($alias ?: OutputFilter::stringURLSafe($title)); |
| 213 | + |
| 214 | + $likeTitle = $db->quote($db->escape($baseTitle, true) . '%', false); |
| 215 | + |
| 216 | + $query = $db->getQuery(true) |
| 217 | + ->select($db->quoteName('title')) |
| 218 | + ->from($db->quoteName('#__finder_filters')) |
| 219 | + ->where($db->quoteName('title') . ' LIKE ' . $likeTitle); |
| 220 | + |
| 221 | + $existingTitles = $db->setQuery($query)->loadColumn(); |
| 222 | + |
| 223 | + $maxNum = 0; |
| 224 | + foreach ($existingTitles as $existing) { |
| 225 | + if (preg_match('/^\Q' . $baseTitle . '\E(?:\s\((\d+)\))?$/', $existing, $matches)) { |
| 226 | + $num = isset($matches[1]) ? (int) $matches[1] : 1; |
| 227 | + if ($num > $maxNum) { |
| 228 | + $maxNum = $num; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + $nextNum = $maxNum + 1; |
| 234 | + |
| 235 | + $newTitle = $baseTitle; |
| 236 | + if ($nextNum > 1) { |
| 237 | + $newTitle .= ' (' . $nextNum . ')'; |
| 238 | + } |
| 239 | + |
| 240 | + // Build a unique alias |
| 241 | + $newAlias = $this->getUniqueAlias($baseAlias); |
| 242 | + |
| 243 | + return [$newTitle, $newAlias]; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Ensure a unique alias in the table by incrementing with dash style. |
| 248 | + * |
| 249 | + * |
| 250 | + * @param string $base The starting alias (already URL-safe). |
| 251 | + * |
| 252 | + * @return string A unique alias. |
| 253 | + * |
| 254 | + * @since __DEPLOY_VERSION__ |
| 255 | + */ |
| 256 | + protected function getUniqueAlias(string $base): string |
| 257 | + { |
| 258 | + $alias = $base !== '' ? $base : OutputFilter::stringURLSafe(uniqid('filter-', true)); |
| 259 | + |
| 260 | + while ($this->aliasExists($alias)) { |
| 261 | + $alias = StringHelper::increment($alias, 'dash'); |
| 262 | + } |
| 263 | + |
| 264 | + return $alias; |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Check whether an alias exists in the table. |
| 269 | + * |
| 270 | + * @param string $alias The alias to test. |
| 271 | + * |
| 272 | + * @return boolean True if it exists, false otherwise. |
| 273 | + * |
| 274 | + * @since __DEPLOY_VERSION__ |
| 275 | + */ |
| 276 | + protected function aliasExists(string $alias): bool |
| 277 | + { |
| 278 | + $db = $this->getDatabase(); |
| 279 | + $query = $db->getQuery(true) |
| 280 | + ->select('COUNT(*)') |
| 281 | + ->from($db->quoteName('#__finder_filters')) |
| 282 | + ->where($db->quoteName('alias') . ' = :alias') |
| 283 | + ->bind(':alias', $alias); |
| 284 | + |
| 285 | + return (int) $db->setQuery($query)->loadResult() > 0; |
| 286 | + } |
152 | 287 | } |
0 commit comments