php alert system?

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I already have a fairly successful site running a free CRM system; however, many users are asking that it alerts them of pre-set events.

I have a start time and duration (epoch values). Do I have to create a cron job running every few minutes or is there another way? I have no idea how to approach this from an AJAX point of view as my javascript experience is zero.

Any guidance would be appreciated.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
1. How do you want them notified? Email? Alert/message/popup while they are viewing the site?
2. Where are the 'events' stored? In a db for each user?
 

xadrieth

New Member
Messages
62
Reaction score
1
Points
0
Hmmmm..

you could could create a PHP file/class that checks for something, and have the MySQL database store the info for the events.

It could work like this:
PHP:
$dbc = new mysqli('localhost', 'root', 'pw', 'freecrm');

$dbc->query('SELECT * FROM crm_events WHERE event_over = \'0\';');

while ($events = $query->fetch_array(MYSQLI_ASSOC)) {
    if ((time() >= $events['time']) && ($events['even_over'] == 0)) {
        // run event
        $dbc->query('UPDATE crm_events SET event_over = \'1\' WHERE event_id = \'' .  $envents['event_id'] . '\';';
    }
}

I dunno, something like that could work.

Just be sure to put the function/class at the bottom/top of each page that way whenever the page is accessed, it executes the event early as possible.
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
If your website generates enough hits, you can simply have a function called at the end of each or next-to-each view of the website and that goes through the event list and takes the required steps. It can also be called at the beginning of the pages.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
1. How do you want them notified? Email? Alert/message/popup while they are viewing the site?
2. Where are the 'events' stored? In a db for each user?

In an ideal world, e-mail by default and pop-up if signed in.

The events are stored in a table related to the contact/user parent table.

Hmmmm..

you could could create a PHP file/class that checks for something, and have the MySQL database store the info for the events.

It could work like this:
PHP:
$dbc = new mysqli('localhost', 'root', 'pw', 'freecrm');

$dbc->query('SELECT * FROM crm_events WHERE event_over = \'0\';');

while ($events = $query->fetch_array(MYSQLI_ASSOC)) {
    if ((time() >= $events['time']) && ($events['even_over'] == 0)) {
        // run event
        $dbc->query('UPDATE crm_events SET event_over = \'1\' WHERE event_id = \'' .  $envents['event_id'] . '\';';
    }
}
I dunno, something like that could work.

Just be sure to put the function/class at the bottom/top of each page that way whenever the page is accessed, it executes the event early as possible.

I can sort of see where you're going with this, but do I need to add the additional "over" field - plus, doesn't this just update the status of an event - I can't see an action if even_overt ="1".

Good start though.

If your website generates enough hits, you can simply have a function called at the end of each or next-to-each view of the website and that goes through the event list and takes the required steps. It can also be called at the beginning of the pages.

Hmm - not sure - I get quite a few but it varies considerably week-day to weekends. If I call a function with every page load, loading data for every user.. is that going to present a security risk???? (*head hurts*) Maybe not, I just need to get my head around that and the load on the server..

An addition complication is that my system adjusts times according to timezone (set in user prefs) even though the epoch value remains constant. This is going to make life tricky.

Thanks for the help so far.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
for the popup, just do a simple javascript alert("Hello world"); or a confirm if you want to have them redirected to a more in-depth detailed message.

As you have said, you can create a cron to go off every 30mins or so, depending on how urgent it is to be close to the exact time.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
Simple solution, write a small class that can be called from page hits and then have a javascript loop reqest data using ajax from another standalone script using the same class :p

If that didn't make sense :

notification.class.php
PHP:
<?php

...

public function pageHitNotifiaction($userID) {
 ... // call the checkNewNotifications($userID) using vars passed to the function
 return $new_notification_array;
}

public function pageConfirmNotification($userID) {
 ... // when the member/user has read/confirmed the notification
 ... // call the flagNotifiactionRead()
}

public function ajaxHitNotifiaction() {
 $userID = $_GET['uerID'];
 ... // call the checkNewNotifications($userID) using $_GET vars
 echo $new_notifications;
}

public function ajaxConfirmNotification() {
 $userID = $_GET['uerID'];
 ... // when the member/user has read/confirmed the notification
 ... // call the flagNotifiactionRead()
}

public function cronHitNotifiaction() {
 ... // use this to shoot off emails
}


private function getNewNotifications($userID) {
 ... // check and return any notification ids relevant to that user or what ever
}

private function flagNotifiactionRead() {
 ... // just add a boolean column to the event table that can be used to flag events as passed/read/notified
}

private function sendAllEmailNotifications() {
 ... // call this from a cron
}

?>

I would recomend some kind of que(major ball ache) if you have allot of these notifications to be sent out.
I wrote a system a while back that ran off different actions from a que members inserted into. The actions had a execution time of anything from 5 secs to 120secs as they where curling another system, it got really complex, shiffting up php max execution time and predicting how long it was going to take to execute a action, then working out how many to run in each cron run. It also had notifications via email, x10forums private message and sms to a certain network :)
 
Top