if (!isset($_SESSION["userselection"]) || !is_array($_SESSION["userselection"]) || $_GET["reset"]) {
$_SESSION["userselection"] = array();
}
$tables = array(
"attribute" => "attribute",
"user_attribute" => "user_attribute",
);
$GLOBALS["table_prefix"] = "phplist_";
if ($_GET["deleterule"]) {
unset($_SESSION["userselection"]["criterion".$_GET["deleterule"]]);
Redirect($_GET["page"]."&id=$id&tab=".$_GET["tab"]);
}
$baseurl = PageURL2($_GET["page"].'&id='.$_GET["id"]);
# should move this to common library area
function parseDate($strdate,$format = 'Y-m-d') {
# parse a string date into a date
$strdate = trim($strdate);
if (strlen($strdate) < 6) {
$newvalue = 0;
} elseif (preg_match("#(\d{2,2}).(\d{2,2}).(\d{4,4})#",$strdate,$regs)) {
$newvalue = mktime(0,0,0,$regs[2],$regs[1],$regs[3]);
} elseif (preg_match("#(\d{4,4}).(\d{2,2}).(\d{2,2})#",$value,$regs)) {
$newvalue = mktime(0,0,0,$regs[3],$regs[1],$regs[1]);
} elseif (preg_match("#(\d{2,2}).(\w{3,3}).(\d{2,4})#",$value,$regs)) {
$newvalue = strtotime($value);
} elseif (preg_match("#(\d{2,4}).(\w{3,3}).(\d{2,2})#",$value,$regs)) {
$newvalue = strtotime($value);
} else {
$newvalue = strtotime($value);
if ($newvalue < 0) {
$newvalue = 0;
}
}
if ($newvalue) {
return date($format,$newvalue);
} else {
return "";
}
}
# new criteria system, add one by one:
if ($_POST["criteria_attribute"]) {
$operator = $_POST["criteria_operator"];
if (is_array($_POST["criteria_values"])) {
$values = join(", ",$_POST["criteria_values"]);
} else {
$values = $_POST["criteria_values"];
}
foreach ($_POST["attribute_names"] as $key => $val) {
$att_names[$key] = $val;
}
$newcriterion = array(
"attribute" => sprintf('%d',$_POST["criteria_attribute"]),
"attribute_name" => $att_names[$_POST["criteria_attribute"]],
"operator" => $operator,
"values" => $values,
);
# find out what number we are
$num = $_SESSION["userselection"]["num"];
if (!$num) $num = 1;
# store this one
# print $att_names[$_POST["criteria_attribute"]];
# print $_POST["attribute_names[".$_POST["criteria_attribute"]."]"];
print "
Adding ".$newcriterion["attribute_name"]." ".$newcriterion["operator"]." ".$newcriterion["values"]."
";
$_SESSION["userselection"]["criterion$num"] = delimited($newcriterion);
# increase number
$_SESSION["userselection"]["num"]++;
}
if (isset($_POST["criteria_match"])) {
$_SESSION["criteria_overall_operator"] = $_POST["criteria_match"];
}
$num = sprintf('%d',$_SESSION["userselection"]["num"]);
#print '
'.$num . " criteria already defined";
$ls = new WebblerListing("Existing criteria");
$used_attributes = array();
$delete_base = sprintf('%s&id=%d&tab=%s',$_GET["page"],$_GET["id"],$_GET["tab"]);
$tc = 0; # table counter
$mainoperator = $_SESSION["criteria_overall_operator"] == "all"? ' and ':' or ';
for ($i = 1; $i<=$num;$i++) {
$crit_data = parseDelimitedData($_SESSION["userselection"]["criterion$i"]);
if ($crit_data["attribute"]) {
array_push($used_attributes,$crit_data["attribute"]);
$ls->addElement(''.$crit_data["attribute_name"]);
$ls->addColumn(''.$crit_data["attribute_name"],"operator",$crit_data["operator"]);
$ls->addColumn(''.$crit_data["attribute_name"],"values",$crit_data["values"]);
$ls->addColumn(''.$crit_data["attribute_name"],"remove",PageLink2($delete_base."&deleterule=".$i,"Remove"));
$attribute = $_POST["criteria"][$i];
# hmm, rather get is some other way, this is a bit unnecessary
$type = Sql_Fetch_Row_Query("select type from {$tables["attribute"]} where id = ".$crit_data["attribute"]);
$operator = "";
switch($type[0]) {
case "checkboxgroup":
if ($tc) {
$where_clause .= " $mainoperator ";
$select_clause .= " left join $tables[user_attribute] as table$tc on table0.userid = table$tc.userid ";
} else {
$select_clause = "table$tc.userid from $tables[user_attribute] as table$tc ";
}
$where_clause .= " ( table$tc.attributeid = ".$crit_data["attribute"]." and (";
if ($crit_data["operator"] == "is") {
$operator = ' or ';
$compare = ' > ';
} else {
$operator = ' and ';
$compare = ' < ';
}
foreach (explode(",",$crit_data["values"]) as $val) {
if (isset($or_clause)) {
$or_clause .= " $operator ";
}
$or_clause .= "find_in_set('$val',table$tc.value) $compare 0";
}
$where_clause .= $or_clause . ") ) ";
break;
case "checkbox":
$value = $crit_data["values"][0];
if ($tc) {
$where_clause .= " $mainoperator ";
$select_clause .= " left join $tables[user_attribute] as table$tc on table0.userid = table$tc.userid ";
} else {
$select_clause = "table$tc.userid from $tables[user_attribute] as table$tc";
}
$where_clause .= " ( table$tc.attributeid = ".$crit_data["attribute"]." and ";
if ($crit_data["operator"] == "isnot") {
$where_clause .= ' not ';
}
if ($value) {
$where_clause .= "( length(table$tc.value) and table$tc.value != \"off\" and table$tc.value != \"0\") ";
} else {
$where_clause .= "( table$tc.value = \"\" or table$tc.value = \"0\" or table$tc.value = \"off\") ";
}
$where_clause .= ' ) ';
break;
case "date":
$date_value = parseDate($crit_data["values"]);
if (!$date_value) {
break;
}
if (isset($where_clause)) {
$where_clause .= " $mainoperator ";
$select_clause .= " left join $tables[user_attribute] as table$tc on table0.userid = table$tc.userid ";
} else {
$select_clause = " table$tc.userid from $tables[user_attribute] as table$tc ";
}
$where_clause .= ' ( table'.$tc.'.attributeid = '.$crit_data["attribute"].' and table'.$tc.'.value != "" and table'.$tc.'.value ';
switch ($crit_data["operator"]) {
case "is":
$where_clause .= ' = "'.$date_value . '" )';break;
case "isnot":
$where_clause .= ' != "'.$date_value . '" )';break;
case "isbefore":
$where_clause .= ' <= "'.$date_value . '" )';break;
case "isafter":
$where_clause .= ' >= "'.$date_value . '" )';break;
}
# $where_clause .= " )";
break;
default:
if (isset($where_clause)) {
$where_clause .= " $mainoperator ";
$select_clause .= " left join $tables[user_attribute] as table$tc on table0.userid = table$tc.userid ";
} else {
$select_clause = " table$tc.userid from $tables[user_attribute] as table$tc ";
}
$where_clause .= " ( table$tc.attributeid = ".$crit_data["attribute"]." and table$tc.value ";
if ($crit_data["operator"] == "isnot") {
$where_clause .= ' not in (';
} else {
$where_clause .= ' in (';
}
$where_clause .= $crit_data["values"] . ") )";
}
$tc++;
}
}
if ($num) {
$userselection_query = "select $select_clause where $where_clause";
# $count_query = addslashes($count_query);
if ($_GET["calculate"]) {
ob_end_flush();
print "$userselection_query
";
print "Calculating ...";
flush();
$req = Sql_Query($userselection_query);
print '.. '.Sql_Num_Rows($req). " users apply
";
}
$ls->addButton("Calculate",$baseurl.'&tab='.$_GET["tab"].'&calculate=1');
$ls->addButton("Reset",$baseurl.'&tab='.$_GET["tab"].'&reset=1');
$existing_criteria = $ls->display();
}
$att_js = '
';
$att_drop = ''.$att_names;
$operator_drop = '
';
$values_drop = '
';
$values_drop .= '';
$values_drop .= '';
# $values_drop .= '';
# $values_drop .= '';
$values_drop .= '';
$values_drop .= '';
$existing_overall_operator = $_SESSION["criteria_overall_operator"] == "any" ? "any":"all";
$criteria_overall_operator =
sprintf('Match all of these rules
Match any of these rules ',
$existing_overall_operator == "all"? "checked":"",$existing_overall_operator == "any"? "checked":"");
$criteria_styles = '
';
$criteria_content = $criteria_overall_operator.$existing_criteria.$criteria_styles.$att_js.
''.
''.$att_drop.''.
''.$operator_drop.''.
''.$values_drop.''.
'';
'
';
print '';