1. Go to this page and download the library: Download tourze/user-tag-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use UserTagBundle\Entity\Tag;
use UserTagBundle\Entity\Category;
use UserTagBundle\Enum\TagType;
// Create a category
$category = new Category();
$category->setName('Customer Status');
$entityManager->persist($category);
// Create a static tag
$tag = new Tag();
$tag->setName('VIP Customer')
->setType(TagType::StaticTag)
->setCategory($category)
->setDescription('High-value customers');
$entityManager->persist($tag);
// Create a smart tag with JSON rules
$smartTag = new Tag();
$smartTag->setName('Active User')
->setType(TagType::SmartTag)
->setCategory($category);
$smartRule = new SmartRule();
$smartRule->setTag($smartTag)
->setCronStatement('0 0 * * *') // Daily at midnight
->setJsonStatement([
'conditions' => [
['field' => 'last_login', 'operator' => '>=', 'value' => '7 days ago']
]
]);
$entityManager->persist($smartTag);
$entityManager->persist($smartRule);
$entityManager->flush();
// Create a SQL-based tag
$sqlTag = new Tag();
$sqlTag->setName('Recent Buyers')
->setType(TagType::SqlTag)
->setCategory($category);
$sqlRule = new SqlRule();
$sqlRule->setTag($sqlTag)
->setSqlStatement("
SELECT user_id
FROM orders
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY user_id
HAVING COUNT(*) >= 3
");
$entityManager->persist($sqlTag);
$entityManager->persist($sqlRule);
$entityManager->flush();
// Get user tags
$procedure = new GetUserTagList();
$result = $procedure->execute();
// Assign tag to user
$assignProcedure = new AssignTagToBizUser();
$assignProcedure->setParameters([
'tagId' => $tag->getId(),
'bizUserId' => $userId
]);
$assignProcedure->execute();
use UserTagBundle\Enum\TagType;
// Create a custom tag type
class CustomTagType extends TagType
{
public const CustomType = 'custom';
}
use UserTagBundle\Event\BeforeAddTagEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TagEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
BeforeAddTagEvent::class => 'onBeforeAddTag',
];
}
public function onBeforeAddTag(BeforeAddTagEvent $event): void
{
// Custom logic before tag assignment
$user = $event->getUser();
$tag = $event->getTag();
// Example: Validate business rules
if (!$this->validateTagAssignment($user, $tag)) {
throw new \InvalidArgumentException('Tag assignment not allowed');
}
}
}