diff --git a/classes/Pommo_Groups.php b/classes/Pommo_Groups.php index 8740bd3..7df61ed 100644 --- a/classes/Pommo_Groups.php +++ b/classes/Pommo_Groups.php @@ -98,7 +98,7 @@ function members($p = array(), // make a group template // accepts a group template (assoc array) // return a group object (array) - function & make($in = array()) { + public static function make($in = array()) { $o = Pommo_Type::group(); return Pommo_Api::getParams($o, $in); } @@ -119,7 +119,7 @@ static function & makeDB(&$row) { // returns true if group ($in) is valid, false if not // TODO -> add validation of group array - function validate(&$in) { + public static function validate(&$in) { $logger =& Pommo::$_logger; $invalid = array(); @@ -181,7 +181,7 @@ static function & get($p = array()) { // accepts a filtering array --> // id (int || array) -> an array of field IDs // returns an array of group names. Array key(s) correlates to group ID. - function & getNames($id = null) { + public static function getNames($id = null) { $dbo =& Pommo::$_dbo; $o = array(); @@ -224,7 +224,7 @@ function & getMemberIDs($group, $status = 1, $filter = false) { // accepts filter by status (int) either 1 (active) (default), 0 (inactive), 2 (pending) // accepts a toggle (bool) to return IDs or Group Tally // returns a tally (int) - function tally($group, $status = 1) { + public static function tally($group, $status = 1) { $dbo =& Pommo::$_dbo; require_once(Pommo::$_baseDir. 'classes/Pommo_Sql.php'); @@ -239,7 +239,7 @@ function tally($group, $status = 1) { // adds a group to the database // accepts a group object (array) // returns the database ID of the added group or FALSE if failed - function add(&$in) { + public static function add($in) { $dbo =& Pommo::$_dbo; if (!Pommo_Groups::validate($in)) @@ -259,7 +259,7 @@ function add(&$in) { // removes a group from the database // accepts a single ID (int) or array of IDs // returns the # of deleted groups (int). 0 (false) if none. - function delete(&$id) { + public static function delete($id) { $dbo =& Pommo::$_dbo; $query = " @@ -286,7 +286,7 @@ function delete(&$id) { // Returns the # of rules affected by a group deletion // accepts a single ID (int) or array of IDs. // Returns a count (int) of affected rules. 0 if none. - function rulesAffected($id = array()) { + public static function rulesAffected($id = array()) { $dbo =& Pommo::$_dbo; $query = " @@ -302,7 +302,7 @@ function rulesAffected($id = array()) { // Checks if a group name exists // accepts a name (str) // returns (bool) true if exists, false if not - function nameExists($name = null) { + public static function nameExists($name = null) { $dbo =& Pommo::$_dbo; $query = " diff --git a/classes/Pommo_Rules.php b/classes/Pommo_Rules.php index efb5ee2..1b0c9fe 100644 --- a/classes/Pommo_Rules.php +++ b/classes/Pommo_Rules.php @@ -1,53 +1,53 @@ - * + * * This file is part of poMMo (http://www.pommo.org) - * - * poMMo is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published + * + * poMMo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or any later version. - * + * * poMMo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with program; see the file docs/LICENSE. If not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ class Pommo_Rules { - - // returns the legal(logical) group selections for new filters + + // returns the legal(logical) group selections for new filters // accepts a group object // accepts an array of all groups // returns array of group names. Array key correlates to group's ID - function & getLegalGroups(&$group, &$groups) { + public static function getLegalGroups(&$group, &$groups) { $o = array(); - + foreach($groups as $id => $g) { if($g['name'] != $group['name']) $o[$id] = $g['name']; } - + // remember; for is_in/not_in .. field ID should be NULL, value is ID of group to include/exclude foreach($group['rules'] as $r) { if ($r['logic'] == 'is_in' || $r['logic'] == 'not_in') unset($o[$r['value']]); } - + return $o; } - + // returns the legal(logical) selections for new filters based off current rules // accepts a group object (can be empty -- thus returning all legal field filters) // accepts a array of fields // returns an array of logics. Array key correlates to field_id. - function & getLegal(&$group, $fields) { + public static function getLegal(&$group, $fields) { $c = array(); - + $legalities = array( 'checkbox' => array('true','false'), 'multiple' => array('is','not'), @@ -56,22 +56,22 @@ function & getLegal(&$group, $fields) { 'number' => array('is','not','greater','less'), 'comment' => array() ); - + foreach ($fields as $field) $c[$field['id']] = $legalities[$field['type']]; - + if(empty($group['rules'])) return $c; - + // subtract illogical selections from $c - foreach ($group['rules'] as $rule) { - + foreach ($group['rules'] as $rule) { + if (!isset($c[$rule['field_id']])) continue; - - // create reference to this field's legalities + + // create reference to this field's legalities $l =& $c[$rule['field_id']]; - + switch($rule['logic']) { case 'true' : case 'false' : @@ -92,16 +92,16 @@ function & getLegal(&$group, $fields) { break; } } - + foreach($c as $key => $val) { if (empty($val)) unset($c[$key]); } - + return $c; } - - function getEnglish($str = null) { + + public static function getEnglish($str = null) { $english = array( 'is' => Pommo::_T('is'), 'not' => Pommo::_T('is not'), @@ -112,7 +112,7 @@ function getEnglish($str = null) { 'is_in' => Pommo::_T('or in group'), 'not_in' => Pommo::_T('and not in group') ); - + if(is_array($str)) { $out = array(); @@ -122,14 +122,14 @@ function getEnglish($str = null) { } return $out; } - - return (empty($str)) ? $english : $english[$str]; + + return (empty($str)) ? $english : $english[$str]; } - + function addBoolRule(&$group, &$match, &$logic) { global $pommo; $dbo =& Pommo::$_dbo; - + $query = " INSERT INTO " . $dbo->table['group_rules']." SET @@ -139,11 +139,11 @@ function addBoolRule(&$group, &$match, &$logic) { $query=$dbo->prepare($query,array($group,$match,$logic)); return $dbo->affected($query); } - + function addGroupRule(&$groupID, &$match, &$logic) { global $pommo; $dbo =& Pommo::$_dbo; - + $query = " INSERT INTO " . $dbo->table['group_rules']." SET @@ -153,16 +153,16 @@ function addGroupRule(&$groupID, &$match, &$logic) { $query=$dbo->prepare($query,array($groupID,$match,$logic)); return $dbo->affected($query); } - + function addFieldRule(&$group, &$field, &$logic, &$values, $type = 0) { global $pommo; $dbo =& Pommo::$_dbo; - + $type = ($type == 'or')? 1 : 0; - + // remove previous filters Pommo_Rules::deleteRule($group, $field, $logic); - + // get the field require_once(Pommo::$_baseDir.'classes/Pommo_Fields.php'); $field = current(Pommo_Fields::get(array('id' => $field))); @@ -174,42 +174,42 @@ function addFieldRule(&$group, &$field, &$logic, &$values, $type = 0) { $value = Pommo_Helper::timeFromStr($value); $v[] = $dbo->prepare("(%i,%i,'%s','%s',%i)",array($group, $field['id'], $logic, $value, $type)); } - + $query = " INSERT INTO " . $dbo->table['group_rules']." (group_id, field_id, logic, value, type) VALUES ".implode(',', $v); return $dbo->affected($query); } - - + + function deleteRule($gid, $fid, $logic) { global $pommo; $dbo =& Pommo::$_dbo; - - $where = ($logic == 'is_in' || $logic == 'not_in') ? + + $where = ($logic == 'is_in' || $logic == 'not_in') ? "AND field_id=0 AND rule_id=%i" : "AND field_id=%i"; - + $query = " DELETE FROM " . $dbo->table['group_rules']." WHERE group_id=%i AND logic='%s' ".$where; $query = $dbo->prepare($query,array($gid, $logic, $fid)); - return ($dbo->affected($query)); + return ($dbo->affected($query)); } - + function changeType($gid, $fid, $logic, $type) { global $pommo; $dbo =& Pommo::$_dbo; - + $type = ($type == 'or') ? 1 : 0; - + $query = " UPDATE " . $dbo->table['group_rules']." SET type=$type WHERE group_id=%i AND logic='%s' AND field_id=%i"; - $query = $dbo->prepare($query,array($gid, $logic, $fid)); - return ($dbo->affected($query)); + $query = $dbo->prepare($query,array($gid, $logic, $fid)); + return ($dbo->affected($query)); } } ?> diff --git a/classes/Pommo_Sql.php b/classes/Pommo_Sql.php index 3d41c36..38bbd46 100644 --- a/classes/Pommo_Sql.php +++ b/classes/Pommo_Sql.php @@ -1,18 +1,18 @@ - * + * * This file is part of poMMo (http://www.pommo.org) - * - * poMMo is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published + * + * poMMo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or any later version. - * + * * poMMo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with program; see the file docs/LICENSE. If not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. @@ -22,35 +22,35 @@ // REWRITE ALL... class Pommo_Sql { - - - + + + // returns where clauses as array // accepts a attribute filtering array. // array_key == column, value is filter table filter table (subscriber_pending, subscriber_data, subscribers) - // e.g. - // array('pending_code' => array("not: 'abc1234'", "is: 'def123'", "is: '2234'")); + // e.g. + // array('pending_code' => array("not: 'abc1234'", "is: 'def123'", "is: '2234'")); // array(12 => array("not: 'Milwaukee'")); (12 -- numeric -- is alias for field_id=12) // array('status' => array('equal: active')) // accepts a table prefix (e.g. WHERE prefix.column = 'value') // returns SQL WHERE + JOIN clauses (array) - + // DEPRECIATED.... function & fromFilter(&$in, $p = null) { global $pommo; $dbo =& Pommo::$_dbo; - + $where = $joins = array(); - + // parse column => logic => value from array $filters = array(); - foreach ($in as $col => $val) + foreach ($in as $col => $val) Pommo_Sql::getLogic($col,$val,$filters); - + // get where &/or joins - foreach($filters as $col => $l) { - if (is_numeric($col)) { // "likely" encountered a field_id in subscriber_data... + foreach($filters as $col => $l) { + if (is_numeric($col)) { // "likely" encountered a field_id in subscriber_data... foreach($l as $logic => $vals) { $i = count($joins); $join = "LEFT JOIN {$dbo->table['subscriber_data']} $p$i ON (s.subscriber_id = $p$i.subscriber_id AND $p$i.field_id=$col AND "; @@ -68,7 +68,7 @@ function & fromFilter(&$in, $p = null) { case "false": $joins[] = $join."$p$i.value != 'on')"; break; case "like" : - $joins[] = $dbo->prepare("[".$join."$p$i.value LIKE '%%S%']",array($vals[0])); break; + $joins[] = $dbo->prepare("[".$join."$p$i.value LIKE '%%S%']",array($vals[0])); break; } } } @@ -90,8 +90,8 @@ function & fromFilter(&$in, $p = null) { case "equal": $where[] = $dbo->prepare("[AND $p.$col = '%S']", array($vals[0])); break; case "like" : - $where[] = $dbo->prepare("[AND $p.$col LIKE '%%S%']",array($vals[0])); break; - + $where[] = $dbo->prepare("[AND $p.$col LIKE '%%S%']",array($vals[0])); break; + } } } @@ -100,10 +100,10 @@ function & fromFilter(&$in, $p = null) { $c = count($joins); for ($i=0; $i < $c; $i++) $where[] = "AND $p$i.subscriber_id IS NOT NULL"; // for an "or", this could be left out! - + return array('where' => $where, 'join' => $joins); } - + // get the column(s) logic + value(s) // DEPRECIATED.... function getLogic(&$col, &$val, &$filters) { @@ -112,9 +112,9 @@ function getLogic(&$col, &$val, &$filters) { Pommo_Sql::getLogic($col,$v,$filters); } else { - // extract logic ($matches[1]) + value ($matches[2]) + // extract logic ($matches[1]) + value ($matches[2]) preg_match('/^(?:(not|is|less|greater|true|false|equal|like):)?(.*)$/i',$val,$matches); - if (!empty($matches[1])) { + if (!empty($matches[1])) { if (empty($filters[$col])) $filters[$col] = array(); if (empty($filters[$col][$matches[1]])) @@ -123,9 +123,9 @@ function getLogic(&$col, &$val, &$filters) { } } } - - - + + + // A group "rules" array consists of the filtering rules which make up a group // it resembles: // $rules[rule_id] = array ( @@ -133,24 +133,24 @@ function getLogic(&$col, &$val, &$filters) { // 'logic' => $row['logic'], // 'value' => $row['value'], // ); - - + + // seperates and, or, and group inclusion/exclusion rules // accepts a group rules array // returns a seperated rules array - function & sortRules(&$rules) { + public static function sortRules(&$rules) { $o = array( 'and' => array(), 'or' => array(), 'include' => array(), 'exclude' => array() ); - + foreach($rules as $id => $r) { - + if($r['or']) $o['or'][$id] = $r; - else + else switch ($r['logic']) { case 'is_in': $o['include'][$id] = $r['value']; @@ -161,25 +161,25 @@ function & sortRules(&$rules) { default: $o['and'][$id] = $r; break; - } + } } return $o; } - - + + // LOGIC is either; "is, is not, less, greater, true, false, NOT IN, IN" - + // A "logic array" resembles: // $logic[field_id] = array( // [logic] => array(values) // is_in => array(1,2) // ); - + // accepts a group rules array // returns a logic array - function & sortLogic(&$rules) { + public static function sortLogic(&$rules) { $o = array(); - + foreach($rules as $r) { if(!isset($o[$r['field_id']])) $o[$r['field_id']] = array(); @@ -187,22 +187,22 @@ function & sortLogic(&$rules) { $o[$r['field_id']][$r['logic']] = array(); array_push($o[$r['field_id']][$r['logic']], $r['value']); } - + return $o; } - + // accepts a logic array // returns an array or SQL sub queries - function & getSubQueries(&$in) { + public static function getSubQueries($in) { global $pommo; $dbo =& Pommo::$_dbo; $o = array(); foreach($in as $fid => $a) { - + $sql = "subscriber_id IN (select subscriber_id from {$dbo->table['subscriber_data']} WHERE field_id=$fid "; - + foreach($a as $logic => $v) { switch ($logic) { case "is" : @@ -215,7 +215,7 @@ function & getSubQueries(&$in) { $sql.= $dbo->prepare("[ AND value > %I ]",array($v[0])); break; case "true": // WHERE field_id=$fid is already sufficient - break; + break; case "false": $sql = "subscriber_id NOT IN (select subscriber_ID from {$dbo->table['subscriber_data']} WHERE field_id=$fid"; break; @@ -227,27 +227,27 @@ function & getSubQueries(&$in) { return $o; } - + // generate the group SQL subselects // accepts a group object - function groupSQL(&$group, $tally = false, $status = 1, $filter = false) { + public static function groupSQL($group, $tally = false, $status = 1, $filter = false) { // used to prevent against group include/exclude recursion static $groups; - if (!isset ($groups[$group['id']])) + if (!isset ($groups[$group['id']])) $groups[$group['id']] = TRUE; - + global $pommo; $dbo =& Pommo::$_dbo; - + /* SELECT count(subscriber_id) - from subscribers - where - status ='1' + from subscribers + where + status ='1' AND ( // base group - subscriber_id in + subscriber_id in (select subscriber_id from subscriber_data where field_id =3 and value IN ('on')) - AND subscriber_id in + AND subscriber_id in (select subscriber_id from subscriber_data where field_id =4 and value NOT IN ('lemur')) OR subscriber_id in (select subscriber_id from subscriber_data where field_id =5 and value NOT IN ('on')) @@ -273,26 +273,26 @@ function groupSQL(&$group, $tally = false, $status = 1, $filter = false) { ) ) */ - + $rules = Pommo_Sql::sortRules($group['rules']); $ands = Pommo_Sql::getSubQueries(Pommo_Sql::sortLogic($rules['and'])); - $ors = (empty($rules['or'])) ? - array() : + $ors = (empty($rules['or'])) ? + array() : Pommo_Sql::getSubQueries(Pommo_Sql::sortLogic($rules['or'])); - + $sql = ($tally) ? 'SELECT count(subscriber_id) ' : 'SELECT subscriber_id '; - + $sql .= " FROM {$dbo->table['subscribers']} WHERE status=".intval($status); - + $q = FALSE; - + if(!empty($ands)) { $sql .= " AND (\n"; - + foreach($ands as $k => $s) { if($k != 0) $sql .= "\n AND "; @@ -300,12 +300,12 @@ function groupSQL(&$group, $tally = false, $status = 1, $filter = false) { } foreach($ors as $s) $sql .= "\n OR $s"; - + $sql .="\n)"; - + $q = TRUE; } - + foreach($rules['exclude'] as $gid) { if (!isset($groups[$gid])) { $sql .= "\nAND subscriber_id NOT IN (\n"; @@ -314,7 +314,7 @@ function groupSQL(&$group, $tally = false, $status = 1, $filter = false) { } $q = TRUE; } - + foreach($rules['include'] as $gid) { if (!isset($groups[$gid])) { $sql .= "\n".(($q) ? 'OR' : 'AND')." subscriber_id IN (\n"; @@ -323,19 +323,19 @@ function groupSQL(&$group, $tally = false, $status = 1, $filter = false) { } $q = TRUE; } - + // If a filter/search is requested, perform a match if(is_array($filter) && !empty($filter['field']) && !empty($filter['string'])) { - + // make MySQL LIKE() compliant $filter['string'] = mysql_real_escape_string(addcslashes($filter['string'],'%_')); - + $sql .= (is_numeric($filter['field'])) ? "\n AND subscriber_id in (select subscriber_id from {$dbo->table['subscriber_data']} WHERE field_id = ".(int)$filter['field']." AND value LIKE '%{$filter['string']}%')" : "\n AND ".mysql_real_escape_string($filter['field'])." LIKE '%{$filter['string']}%'"; } return $sql; } - + } ?> diff --git a/groups_edit.php b/groups_edit.php index 734e445..11a9fcd 100644 --- a/groups_edit.php +++ b/groups_edit.php @@ -2,7 +2,7 @@ /** * Original Code Copyright (C) 2005, 2006, 2007, 2008 Brice Burgess * released originally under GPLV2 - * + * * This file is part of poMMo. * * poMMo is free software: you can redistribute it and/or modify @@ -17,7 +17,7 @@ * * You should have received a copy of the GNU General Public License * along with Pommo. If not, see . - * + * * This fork is from https://github.com/soonick/poMMo * Please see docs/contribs for Contributors * @@ -47,16 +47,16 @@ $state =& Pommo_Api::stateInit('groups_edit',array( 'group' => 0), $_REQUEST); - -$groups = & Pommo_Groups::get(); -$fields = & Pommo_Fields::get(); + +$groups = Pommo_Groups::get(); +$fields = Pommo_Fields::get(); $group =& $groups[$state['group']]; if(empty($group)) Pommo::redirect('subscribers_groups.php'); - + $rules = Pommo_Sql::sortRules($group['rules']); $rules['and'] = Pommo_Sql::sortLogic($rules['and']); $rules['or'] = Pommo_Sql::sortLogic($rules['or']);