function build_mass_mail_query($conditions)
{
global $db;
if(!is_array($conditions))
{
return '';
}
$search_sql = 'u.allownotices=1';
// List of valid LIKE search fields
$user_like_fields = array("username", "email");
foreach($user_like_fields as $search_field)
{
if($conditions[$search_field])
{
$search_sql .= " AND u.{$search_field} LIKE '%".$db->escape_string_like($conditions[$search_field])."%'";
}
}
// LESS THAN or GREATER THAN
$direction_fields = array("postnum");
foreach($direction_fields as $search_field)
{
$direction_field = $search_field."_dir";
if(!empty($conditions[$search_field]) && $conditions[$direction_field])
{
switch($conditions[$direction_field])
{
case "greater_than":
$direction = ">";
break;
case "less_than":
$direction = "<";
break;
default:
$direction = "=";
}
$search_sql .= " AND u.{$search_field}{$direction}'".(int)$conditions[$search_field]."'";
}
}
// Time-based search fields
$time_fields = array("regdate", "lastactive");
foreach($time_fields as $search_field)
{
$time_field = $search_field."_date";
$direction_field = $search_field."_dir";
if(!empty($conditions[$search_field]) && $conditions[$time_field] && $conditions[$direction_field])
{
switch($conditions[$time_field])
{
case "hours":
$date = $conditions[$search_field]*60*60;
break;
case "days":
$date = $conditions[$search_field]*60*60*24;
break;
case "weeks":
$date = $conditions[$search_field]*60*60*24*7;
break;
case "months":
$date = $conditions[$search_field]*60*60*24*30;
break;
case "years":
$date = $conditions[$search_field]*60*60*24*365;
break;
default:
$date = $conditions[$search_field]*60*60*24;
}
switch($conditions[$direction_field])
{
case "less_than":
$direction = ">";
break;
case "more_than":
$direction = "<";
break;
default:
$direction = "<";
}
$search_sql .= " AND u.{$search_field}{$direction}'".(TIME_NOW-$date)."'";
}
}
// Usergroup based searching
if($conditions['usergroup'])
{
if(!is_array($conditions['usergroup']))
{
$conditions['usergroup'] = array($conditions['usergroup']);
}
$conditions['usergroup'] = array_map('intval', $conditions['usergroup']);
foreach($conditions['usergroup'] as $usergroup)
{
switch($db->type)
{
case "pgsql":
case "sqlite":
$additional_sql .= " OR ','||additionalgroups||',' LIKE '%,{$usergroup},%'";
break;
default:
$additional_sql .= " OR CONCAT(',',additionalgroups,',') LIKE '%,{$usergroup},%'";
}
}
$search_sql .= " AND (u.usergroup IN (".implode(",", $conditions['usergroup']).") {$additional_sql})";
}
return $search_sql;
}