$smarty->assign('returnStr', Pommo::_T('Groups Page'));
// Initialize page state with default values overriden by those held in $_REQUEST
$state =& PommoAPI::stateInit('groups_edit', array('group' => 0), $_REQUEST);
$groups =& PommoGroup::get();
$fields =& PommoField::get();
$group =& $groups[$state['group']];
if (empty($group)) {
    Pommo::redirect('subscribers_groups.php');
}
$rules = PommoSQL::sortRules($group['rules']);
$rules['and'] = PommoSQL::sortLogic($rules['and']);
$rules['or'] = PommoSQL::sortLogic($rules['or']);
foreach ($rules as $key => $a) {
    if ($key == 'include' || $key == 'exclude') {
        foreach ($a as $k => $gid) {
            $rules[$key][$k] = $groups[$gid]['name'];
        }
    }
}
$smarty->assign('fields', $fields);
$smarty->assign('legalFieldIDs', PommoRules::getLegal($group, $fields));
$smarty->assign('legalGroups', PommoRules::getLegalGroups($group, $groups));
$smarty->assign('group', $group);
$smarty->assign('logicNames', PommoRules::getEnglish());
$smarty->assign('rules', $rules);
$smarty->assign('tally', PommoGroup::tally($group));
$smarty->assign('ruleCount', count($rules['and']) + count($rules['or']) + count($rules['include']) + count($rules['exclude']));
$smarty->assign('getURL', $_SERVER['PHP_SELF'] . '?group_id=' . $group['id']);
$smarty->assign('t_include', Pommo::_T('INCLUDE'));
$smarty->display('admin/subscribers/groups_edit.tpl');
Pommo::kill();
 }
 if ($_REQUEST['ruleType'] == 'field') {
     $field = current(PommoField::get(array('id' => $_REQUEST['fieldID'])));
     $logic = isset($_REQUEST['logic']) && $_REQUEST['logic'] != "0" ? $_REQUEST['logic'] : false;
     $type = $_REQUEST['type'] == 'or' ? 'or' : 'and';
     $values = array();
     // check to see if we're editing [logic is passed *only* when edit button is clicked]
     if ($logic) {
         foreach ($group['rules'] as $rule) {
             if ($rule['logic'] == $logic && $rule['field_id'] == $_REQUEST['fieldID']) {
                 $values[] = $field['type'] == 'date' ? PommoHelper::timeFromStr($rule['value']) : $rule['value'];
             }
         }
     }
     $firstVal = empty($values) ? false : array_shift($values);
     $logic = $logic ? PommoRules::getEnglish(array($logic)) : PommoRules::getEnglish(end(PommoRules::getLegal($group, array($field))));
     $smarty->assign('type', $type);
     $smarty->assign('field', $field);
     $smarty->assign('logic', $logic);
     $smarty->assign('values', $values);
     $smarty->assign('firstVal', $firstVal);
     $smarty->display('admin/subscribers/ajax/rule.field.tpl');
     Pommo::kill();
 } elseif ($_REQUEST['ruleType'] == 'group') {
     $match = PommoGroup::getNames($_REQUEST['fieldID']);
     $key = key($match);
     $smarty->assign('match_name', $match[$key]);
     $smarty->assign('match_id', $key);
     $smarty->display('admin/subscribers/ajax/rule.group.tpl');
     Pommo::kill();
 }