I'm developing an app that will look up an email inbox and save specific emails as part of it's functionality. What happens is an imap connection is made and then all emails that are set as UNSEEN are retrieved. Each email is checked against pre-defined conditions and then saved to a DB if they are met. These conditions can be set by the user and can be the following:
Subject
- Contains string
- Does not contain string
Body
- Contains string
- Does not contain string
From
- Specific address
Conditions can be "chained" for instance:
FILTER WHERE
Subject CONTAINS "Order Confirmation"
AND
Email FROM "billyjones26@gmail.com" OR "billyjones26@googlemail.com"
I'm having trouble thinking of how to format and store these conditions. I need to have a GUI where the user can create these conditions.
My questions is how should I store these conditions? I.e some sort of DB table structure, or maybe converted into a string format and stored in a single table. There needs to be a unlimited number of conditions for an unlimited number of users, and I need to know what operators there are, etc.
Hope that's enough information!
* EDIT FOR MICHAEL *
So I can create the conditions and save them. Now I'm retrieving them and trying to match emails. I created a filter with one condition: Subject contains 'TEST'. Only one email should match this but somehow all emails are being added to the matched array.
My controller that is getting the emails/conditions:
public function check_email(){
$filters = $this->filters_model->get_filters($owner_id=1);
foreach($filters->result() as $filter){
$emails = $this->gmail->get_emails($mailbox_id = $filter->mailbox_id, $limit = 10);
$matched = array();
$conditions = unserialize($filter->conditions);
foreach($emails as $email){
if($conditions->isMet($email) == TRUE){
$matched[] = $email;
}
}
echo count($matched);
echo '<pre>'.$filter->title.'<br /.';
print_r($conditions);
echo '</pre><br />-----';
exit;
}
}
Keyvalueprerequisite.php
This seems to work fine as a var_dump of stripos($subject, $this->value) !== FALSE;
shows only TRUE for 1 email.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keyvalueprerequisite {
private $key;
private $comparator;
private $value;
public function __construct($key, $comparator, $value){
$this->key = $key;
$this->comparator = $comparator;
$this->value = $value;
}
public function isMet(&$context)
{
switch ($this->key) {
case "subject":
$subject = $context["subject"];
if ($this->comparator === "in"){
return stripos($subject, $this->value) !== FALSE;
} else if ($this->comparator === "!in") {
return stripos($subject, $this->value) === FALSE;
}
return FALSE;
break;
case "body":
$body = $context["body"];
if ($this->comparator === "in") {
return stripos($body, $this->value) !== FALSE;
} else if ($this->comparator === "!in") {
return stripos($body, $this->value) === FALSE;
}
return FALSE;
break;
case "from_address":
$from = $context["from"];
if ($this->comparator === "=") {
return $this->value === $from;
} else if ($this->comparator === "!=") {
return $this->value !== $from;
} else{
return false;
}
break;
default:
}
return FALSE;
}
}
Prerequisistegroup.php
Something might not be quite write here. a var_dump of $result = $result && $is_met
returns true for each of the 10 emails.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Prerequisitegroup {
private $type;
private $prerequisites;
public function __construct($type = 'AND'){
$this->type = $type;
}
public function add(){
foreach(func_get_args() as $prerequisite){
$this->prerequisites[] = $prerequisite;
}
}
public function isMet(&$context)
{
if (empty($this->prerequisites) === FALSE) {
$result = TRUE;
foreach ($this->prerequisites as $prerequisite) {
$is_met = $prerequisite->isMet($context);
if ($this->type === 'AND') {
$result = $result && $is_met;
if ($result === FALSE) {
return FALSE;
}
} else {
$result = $result || $is_met;
if ($result === TRUE) {
return TRUE;
}
}
}
return $result;
}
return TRUE;
}
}
These are my conditions that are being checked:
string(3) "AND"
["prerequisites":"Prerequisitegroup":private]=>
array(1) {
[0]=>
object(Prerequisitegroup)#23 (2) {
["type":"Prerequisitegroup":private]=>
string(2) "OR"
["prerequisites":"Prerequisitegroup":private]=>
array(1) {
[0]=>
object(Keyvalueprerequisite)#24 (3) {
["key":"Keyvalueprerequisite":private]=>
string(7) "subject"
["comparator":"Keyvalueprerequisite":private]=>
string(2) "in"
["value":"Keyvalueprerequisite":private]=>
string(4) "TEST"
}
}
}
}
}