Very simple notification/alert system using only Fabrik and PHP

AlexTCGPro

Member
Hey guys!
You all have been a great help, always, so I want to give something back to you, hopefully someone finds it useful or expands from it.

This is to have a very simple notification system in your website, you know, a simple counter of how many messages or articles you have unread for instance. What we will be doing is basically add a list that watches the values of another list and updates itself accordingly.

I made it from scratch with simple trial and error, just to show how dynamic Fabrik is.
For this example I'll have a menu item displaying all the values from a list called 'Products', its content can be whatever, I want that each user gets a simple counter somewhere in the site displaying how many of these products they haven't seen using Fabrik's details view.
  • First, we need to create a new list (or database) that will handle the notifications, in this case I'll only use two elements, but you can easily add to it. I'll be naming it 'notifications_products'
    • Relevant User ID ('user_id').
    • Relevant value ID (the ID of the value from the other List you will be watching, in this case the list would be 'products' and this element 'product_id').
    • (You can add a date element if you care for it)
  • We are gonna need a counter, very simple PHP code that will allow you to count how many unread notifications the user has, in this case:
Code:
<?php
/* GET USER DATA */
$user = JFactory::getUser();
/* COUNT ALERTS */
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('count(*)')->from('products')->where('id NOT IN (SELECT product_id FROM notifications_products WHERE user_id = ' . $user->id . ')');
$db->setQuery($query);
echo $db->loadResult();
?>
If you understand that code you will see exactly how this notification system works.
It basically counts all the ids from 'products' except the ones that are listed in 'notifications_products' and have the current user id.
You put that code whenever you want the counter to be displayed, I personally use sourcerer and have that code together with some CSS code in a module I have in the whole site.
  • Now we just need a way to register whenever the user has seen or not the value in the list, for this I use the plugin advance module manager by regular labs to have a module that loads only when a value's details from the list 'products' is being watched (simply setting it to be displayed when the url has '/products/details' on it should suffice).
Code:
<?php
/* GET ROWID FROM DETAILS VIEW */
$app = JFactory::getApplication();
$product_id = $app->input->get('rowid', '');
/* GET USER DATA */
$user = JFactory::getUser();
/* CHECK IF VALUE EXIST */
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('count(*)')->from('notification_products')->where('product_id = ' . $product_id . ' AND user_id = ' . $user->id);
$db->setQuery($query);
$already_view = $db->loadResult();
/* ADD VALUE */
if (is_null($already_view)) {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $columns = array('product_id', 'user_id');
    $values = array($db->quote($product_id), $db->quote($user->id));
    $query->insert($db->quoteName('notification_products'))->columns($db->quoteName($columns))->values(implode(',', $values));
    $db->setQuery($query);
    $db->execute();
};
?>
That will add a value to 'notifications_products' with both current product id and user id if one doesn't exist already.

And that's it, simple notification system that will track whenever an user has view or not a certain value from another list. You can easily expand from here, add time and date when the person saw it for instance, useful for a chat or to notify them again in case the value was updated since.
I recommend you play around with the code to have unseen values with a different color or highlighted somehow so the user knows exactly what they haven't seen.
In this example I set up the list to only work with the products lists, but you can easily add another element to check what list the notification should keep track of and have just one list handling the notifications of multiple lists.

Note:
You do not need sourcerer nor advance module manager, I used them because it's easier for me.

I haven't tested this exact code, if you find any syntax or code mistake comment about it and I'll fix it.
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top