| @copyright 2007-2026 (c) AVE.cms
| @link https://ave-cms.ru
| @version 3.3
*/
namespace App\Frontend\QuickEdit;
defined('BASEPATH') || die('Direct access to this location is not allowed.');
use App\Common\Auth;
use App\Common\AdminLocation;
/** Resolves cache-safe edit placeholders immediately before the public response. */
class PublicQuickEdit
{
public static function inject($html)
{
$html = (string) $html;
$pattern = self::placeholderPattern();
if (!Auth::systemUserCan('manage_documents')) {
return preg_replace($pattern, '', $html);
}
return self::replacePlaceholders($html, $pattern, Auth::systemUserCan('manage_products'));
}
protected static function replacePlaceholders($html, $pattern = null, $canManageProducts = true)
{
$pattern = $pattern ?: self::placeholderPattern();
return preg_replace_callback($pattern, function ($match) use ($canManageProducts) {
$attributes = isset($match[1]) ? (string) $match[1] : '';
if (!preg_match('/\bdata-document-id\s*=\s*(["\'])(\d+)\1/i', $attributes, $idMatch)) {
return '';
}
$documentId = (int) $idMatch[2];
$class = '';
if (preg_match('/\bdata-edit-class\s*=\s*(["\'])(.*?)\1/i', $attributes, $classMatch)) {
$class = preg_replace('/[^A-Za-z0-9 _-]/', '', html_entity_decode($classMatch[2], ENT_QUOTES, 'UTF-8'));
}
$target = 'document';
if (preg_match('/\bdata-edit-target\s*=\s*(["\'])(document|product)\1/i', $attributes, $targetMatch)) {
$target = strtolower((string) $targetMatch[2]);
}
if ($target === 'product' && !$canManageProducts) {
return '';
}
$base = defined('ABS_PATH') ? rtrim((string) ABS_PATH, '/') : '';
$path = $target === 'product'
? 'catalog/products/' . $documentId . '/edit'
: 'documents/' . $documentId . '/edit';
$label = $target === 'product' ? 'Редактировать товар' : 'Редактировать документ';
$url = $base . AdminLocation::url($path) . '?quick_edit=1&pop=1';
return 'ID ' . $documentId . ''
. 'Редактировать';
}, $html);
}
protected static function placeholderPattern()
{
return '/]*\bdata-adminx-edit-placeholder\b)([^>]*)>\s*<\/span>/i';
}
}