public static function printMembershipStatusChooser($personid, $value)
 {
     Person_Group::printMembershipStatusChooser('membership_status[' . (int) $personid . ']', $value);
 }
    function printForm()
    {
        $GLOBALS['system']->includeDBClass('person_group');
        $params = $this->getValue('params');
        ?>
		<h3>Find me people...</h3>

		<h4>whose person/family record matches these rules:</h4>
		<table class="table table-border table-auto-width indent-left table-condensed">
		<?php 
        foreach ($this->_query_fields as $i) {
            $v = $this->_field_details[$i];
            if (in_array($v['type'], array('select', 'reference', 'datetime', 'text')) && !in_array($i, array('p.first_name', 'p.last_name', 'f.family_name', 'p.remarks', 'p.email'))) {
                ?>
				<tr>
					<td>
						<label class="checkbox">
							<input autofocus="1" type="checkbox" name="enable_rule[]" value="<?php 
                echo $i;
                ?>
" id="enable_rule_<?php 
                echo $i;
                ?>
" class="select-rule-toggle" <?php 
                if (isset($params['rules'][$i])) {
                    echo 'checked="checked" ';
                }
                ?>
/>
							<strong><?php 
                echo $v['label'];
                ?>
</strong>
							<?php 
                if ($v['type'] == 'datetime') {
                    echo 'is between...';
                } else {
                    echo 'is...';
                }
                ?>
						</label>
					</td>
					<td>
						<div class="select-rule-options" <?php 
                if (!isset($params['rules'][$i])) {
                    echo 'style="display: none" ';
                }
                ?>
>
							<?php 
                $key = str_replace('.', '_', $i);
                if ($v['type'] == 'datetime') {
                    $value = array_get($params['rules'], $i, array('from' => '2000-01-01', 'to' => date('Y-m-d')));
                    print_widget('params_' . $key . '_from', array('type' => 'date'), $value['from']);
                    echo ' and ';
                    print_widget('params_' . $i . '_to', array('type' => 'date'), $value['to']);
                } else {
                    $v['allow_multiple'] = TRUE;
                    print_widget('params_' . $key, $v, array_get($params['rules'], $i, $v['type'] == 'select' ? array() : ''));
                }
                ?>
						</div>
					</td>
				</tr>
				<?php 
            }
        }
        ?>
		</table>
		
		<?php 
        // DATE FIELD RULES
        if ($GLOBALS['system']->featureEnabled('DATES')) {
            if (empty($params['dates']) && !empty($params['rules']['date'])) {
                $params['dates'][] = $params['rules']['date'] + array('criteria' => 'between');
                unset($params['rules']['date']);
            }
            ?>
			<h4>who have
				<?php 
            $dlParams = array('type' => 'select', 'options' => array('OR' => 'any', 'AND' => 'all'));
            print_widget('params_date_logic', $dlParams, array_get($params, 'date_logic', 'AND'));
            ?>
				 the following date fields...</h4>
			<table class="table expandable indent-left" id="datefield-rules">
			<?php 
            $values = array_get($params, 'dates', array());
            if (empty($values)) {
                $values[] = array('typeid' => NULL, 'criteria' => 'any', 'anniversary' => TRUE, 'from' => '2000-01-01', 'to' => date('Y-m-d'));
            }
            foreach ($values as $i => $value) {
                ?>
				<tr>
					<td class="narrow">
						<?php 
                print_widget('params_date_' . $i . '_typeid', array('type' => 'select', 'options' => array(0 => '--Choose--') + Person::getDateTypes()), (string) $value['typeid']);
                ?>
					</td>
					<td>
						<?php 
                // backwards compatibility:
                if (array_get($value, 'criteria') == 'between') {
                    $value['criteria'] = $value['anniversary'] ? 'anniversary' : 'exact';
                }
                $cparams = array('type' => 'select', 'options' => array('any' => 'filled with any value', 'empty' => 'not filled in', 'exact' => 'with exact value within...', 'anniversary' => 'with exact value or anniversary within...'), 'class' => 'datefield-rule-criteria');
                print_widget('params_date_' . $i . '_criteria', $cparams, $value['criteria']);
                $pts = array('fixed' => '', 'relative' => '');
                $pts[array_get($value, 'periodtype', 'fixed')] = 'checked="checked"';
                ?>
						<div class="datefield-rule-period">
							<label class="checkbox">
								<input type="radio" name="params_date_<?php 
                echo $i;
                ?>
_periodtype" value="fixed" <?php 
                echo $pts['fixed'];
                ?>
 />
								the period from
								<?php 
                print_widget('params_date_' . $i . '_from', array('type' => 'date'), $value['from']);
                ?>
								to
								<?php 
                print_widget('params_date_' . $i . '_to', array('type' => 'date'), $value['to']);
                ?>
							</label>
							<label class="checkbox">
								<input type="radio" name="params_date_<?php 
                echo $i;
                ?>
_periodtype" value="relative"<?php 
                echo $pts['relative'];
                ?>
 />
								the
								<?php 
                print_widget('params_date_' . $i . '_periodlength', array('type' => 'int'), array_get($value, 'periodlength', 14));
                ?>
								day period
								<?php 
                print_widget('params_date_' . $i . '_periodanchor', array('type' => 'select', 'options' => array('before' => 'before', 'ending' => 'ending on', 'starting' => 'starting on', 'after' => 'after')), array_get($value, 'periodanchor', 'ending'));
                ?>
								the day the report is executed
							</label>

						</div>
					</td>
				</tr>
				<?php 
            }
            ?>
			</table>
			<?php 
        }
        ?>

		<h4>who <strong>are</strong> in one or more of these groups:</h4>
		<div class="indent-left">

			<?php 
        Person_Group::printMultiChooser('include_groupids', array_get($params, 'include_groups', array()), array(), TRUE);
        ?>

			<label class="checkbox">
				<input type="checkbox" name="enable_group_membership_status" data-toggle="enable" data-target="#group-membership-status *" value="1"	<?php 
        if (!empty($params['group_membership_status'])) {
            echo 'checked="checked"';
        }
        ?>
 />
				with membership status of
			</label>
			<span id="group-membership-status"><?php 
        Person_Group::printMembershipStatusChooser('group_membership_status', array_get($params, 'group_membership_status'), true);
        ?>
</span>

			<label class="checkbox">
				<input type="checkbox" name="enable_group_join_date" data-toggle="enable" data-target="#group-join-dates *" value="1"	<?php 
        if (!empty($params['group_join_date_from'])) {
            echo 'checked="checked"';
        }
        ?>
 />
				and joined the group between
			</label> 
			<span id="group-join-dates">
			<?php 
        print_widget('group_join_date_from', array('type' => 'date'), array_get($params, 'group_join_date_from'));
        ?>
			and <?php 
        print_widget('group_join_date_to', array('type' => 'date'), array_get($params, 'group_join_date_to'));
        ?>
			</span>
		</div>


		<h4>who are <strong>not</strong> in any of these groups:</h4>
		<div class="indent-left">
			<?php 
        Person_Group::printMultiChooser('exclude_groupids', array_get($params, 'exclude_groups', array()), array(), TRUE);
        ?>
		</div>
	
	<?php 
        if ($GLOBALS['user_system']->havePerm(PERM_VIEWNOTE)) {
            ?>
		<h4>who have a person note containing the phrase:</h4>
		<div class="indent-left">
			<input type="text" name="note_phrase" size="60" value="<?php 
            echo isset($params["note_phrase"]) ? $params["note_phrase"] : "";
            ?>
">
		</div>
		<?php 
        }
        if ($GLOBALS['user_system']->havePerm(PERM_VIEWATTENDANCE)) {
            ?>
		<h4>whose attendance...</h4>
		<div class="indent-left">
			at 
			<?php 
            $groupid_params = array('type' => 'select', 'options' => array(null => '(Nothing)', '__cong__' => 'their congregation'), 'attrs' => array('data-toggle' => 'enable', 'data-target' => '.attendance-input'));
            $groups = $GLOBALS['system']->getDBObjectData('person_group', array('!attendance_recording_days' => 0, 'is_archived' => 0), 'AND');
            foreach ($groups as $id => $groupdata) {
                $groupid_params['options'][$id] = $groupdata['name'];
            }
            print_widget('attendance_groupid', $groupid_params, array_get($params, 'attendance_groupid', 0));
            ?>
			<br />

			has been 

			<?php 
            $operator_params = array('type' => 'select', 'options' => array('<' => 'less than', '>' => 'more than'), 'class' => 'attendance-input');
            print_widget('attendance_operator', $operator_params, array_get($params, 'attendance_operator', '<'));
            ?>
			<input name="attendance_percent" type="text" size="2" class="int-box attendance-input" value="<?php 
            echo (int) array_get($params, 'attendance_percent', 50);
            ?>
" />%

			<br />over the last <input name="attendance_weeks" type="text" size="2" class="int-box attendance-input" value="<?php 
            echo (int) array_get($params, 'attendance_weeks', 2);
            ?>
" /> weeks
		</div>
		<?php 
        }
        ?>

		<h3>For each person found, show me...</h3>
		<?php 
        $show_fields = array_get($params, 'show_fields', array());
        ?>

		<table class="table-condensed expandable indent-left">
		<?php 
        foreach ($show_fields as $chosen_field) {
            ?>
			<tr>
				<td>
					<img src="<?php 
            echo BASE_URL;
            ?>
/resources/img/expand_up_down_green_small.png" class="icon insert-row-below" style="position: relative; top: 2ex" title="Create a blank entry here" />
				</td>
				<td>
					<?php 
            $options = array('' => '');
            foreach ($this->_show_fields as $i => $opt) {
                if (is_null($opt)) {
                    $options['--' . $i] = '-----';
                } else {
                    $options[$opt] = $this->_field_details[$opt]['label'];
                }
            }
            if ($GLOBALS['user_system']->havePerm(PERM_VIEWATTENDANCE)) {
                $options['--Z'] = '-----';
                $options['attendance_percent'] = 'Attendance rate during specified period';
                $options['attendance_numabsences'] = 'Number of absences since last marked present';
            }
            if ($GLOBALS['user_system']->havePerm(PERM_VIEWNOTE)) {
                $options['--Y'] = '-----';
                $options['notes.subjects'] = 'Notes matching the phrase above';
                $options['actionnotes.subjects'] = 'Notes requiring action';
            }
            if ($GLOBALS['system']->featureEnabled('DATES')) {
                $options['--A'] = '-----';
                foreach (Person::getDateTypes() as $typeid => $name) {
                    $options['date---' . $typeid] = ucfirst($name) . ' Date';
                }
            }
            $options['--B'] = '-----';
            if ($GLOBALS['system']->featureEnabled('PHOTOS')) {
                $options['photo'] = 'Photo';
            }
            $options['groups'] = 'Which of the selected groups they are in';
            $options['membershipstatus'] = 'Group membership status';
            $options['all_members'] = 'Names of all their family members';
            $options['adult_members'] = 'Names of their adult family members';
            if ($GLOBALS['system']->featureEnabled('PHOTOS')) {
                $options['photo'] = 'Photo';
            }
            $options['--D'] = '-----';
            $options['view_link'] = 'A link to view their person record';
            $options['edit_link'] = 'A link to edit their person record';
            $options['checkbox'] = 'A checkbox for bulk actions';
            print_widget('show_fields[]', array('options' => $options, 'type' => 'select', 'disabled_prefix' => '--'), $chosen_field);
            ?>
				</td>
				<td class="nowrap">
					<img src="<?php 
            echo BASE_URL;
            ?>
/resources/img/arrow_up_thin_black.png" class="icon move-row-up" title="Move this item up" />
					<img src="<?php 
            echo BASE_URL;
            ?>
/resources/img/arrow_down_thin_black.png" class="icon move-row-down" title="Move this item down" />
					<img src="<?php 
            echo BASE_URL;
            ?>
/resources/img/cross_red.png" class="icon delete-row" title="Delete this item" />
				</td>
			</tr>
			<?php 
        }
        ?>
		</table>


		<h3>Group the results...</h3>
		<?php 
        $gb = array_get($params, 'group_by', '');
        ?>
		<div class="indent-left">
			<select name="group_by">
				<option value=""<?php 
        if ($gb == '') {
            echo ' selected="selected"';
        }
        ?>
>all together</option>
				<option value="groupid"<?php 
        if ($gb == 'groupid') {
            echo ' selected="selected"';
        }
        ?>
>by group membership</option>
			<?php 
        foreach ($this->_query_fields as $i) {
            $v = $this->_field_details[$i];
            if (!in_array($v['type'], array('select', 'reference'))) {
                continue;
            }
            ?>
				<option value="<?php 
            echo $i;
            ?>
"<?php 
            if ($gb == $i) {
                echo ' selected="selected"';
            }
            ?>
>by <?php 
            echo $v['label'];
            ?>
</option>
				<?php 
        }
        ?>
			</select>
			<p class="smallprint">Note: Result groups that do not contain any persons will not be shown</p>
		</div>

		<h3>Sort the results by...</h3>

		<select name="sort_by" class="indent-left">
		<?php 
        $sb = array_get($params, 'sort_by');
        foreach ($this->_show_fields as $name) {
            if (is_null($name)) {
                ?>
				<option disabled="disabled">------</option>
				<?php 
            } else {
                ?>
				<option value="<?php 
                echo $name;
                ?>
"<?php 
                if ($sb == $name) {
                    echo ' selected="selected"';
                }
                ?>
><?php 
                echo ents($this->_field_details[$name]['label']);
                ?>
</option>
				<?php 
            }
        }
        ?>
		<option disabled="disabled">------</option>
		<option value="attendance_percent"<?php 
        if ($sb == "attendance_percent") {
            echo ' selected="selected"';
        }
        ?>
>Attendance rate during the specified period</option>
		<option value="attendance_numabsences""<?php 
        if ($sb == "attendance_numabsences") {
            echo ' selected="selected"';
        }
        ?>
>Number of absences since last marked present</option>
		<?php 
        if ($GLOBALS['system']->featureEnabled('DATES')) {
            ?>
			<option disabled="disabled">------</option>
			<?php 
            foreach (Person::getDateTypes() as $typeid => $name) {
                ?>
				<option value="date---<?php 
                echo $typeid;
                ?>
"<?php 
                if ($sb == 'date---' . $typeid) {
                    echo ' selected="selected"';
                }
                ?>
><?php 
                echo ents($name);
                ?>
 date</option>
				<?php 
            }
        }
        ?>
		</select>

		<?php 
        if ($GLOBALS['user_system']->havePerm(PERM_MANAGEREPORTS)) {
            ?>
			<h3>I want to save this report...</h3>
			<div class="indent-left">
				<label type="radio">
					<input type="radio" name="save_option" value="new" id="save_option_new" <?php 
            if (empty($this->id)) {
                echo 'checked="checked"';
            }
            ?>
 />
					as a new report called
					<input type="text" name="new_query_name" />
				</label>
		
				<label type="radio">
					<input type="radio" name="save_option" value="replace" id="save_option_replace" <?php 
            if ($this->id && $this->id != 'TEMP') {
                echo 'checked="checked"';
            }
            ?>
 />
					in place of an existing report
					<?php 
            print_widget('replace_query_id', array('type' => 'reference', 'references' => 'person_query'), $this->id);
            ?>
				</label>

				<label type="radio">
					<input type="radio" name="save_option" value="temp" id="save_option_temp"<?php 
            if (empty($this->id) || $this->id == 'TEMP') {
                echo ' checked="checked"';
            }
            ?>
 />
					only temporarily as an ad-hoc report
				</label>
			</div>
			<?php 
        }
    }
" />
			<input type="hidden" name="action" value="add_member" />
			<input type="hidden" name="overwrite_membership" value="1" />
			<input type="hidden" name="back_to" value="persons" />

			<div class="modal-header">
				<h4>Add <?php 
    $person->printFieldValue('name');
    ?>
 to a group</h4>
			</div>
			<div class="modal-body">
				<?php 
    $GLOBALS['system']->includeDBClass('person_group');
    echo 'Add as a ';
    Person_Group::printMembershipStatusChooser('membership_status');
    echo ' of ';
    $can_add_group = Person_Group::printChooser('groupid', 0, array_keys($groups));
    ?>
			</div>
			<div class="modal-footer">
				<input type="submit" class="btn" value="Go" accesskey="s" onclick="if (!$('[name=groupid]').val()) { alert('Choose a group first'); return false; }" />
				<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
			</div>
		</form>
	</div>
	<?php 
}
// -------- CHECK PERMISSIONS AND ASSEMBLE DATA ------------- //
$accordion = SizeDetector::getWidth() && SizeDetector::isNarrow();
$tabs = array('basic' => 'Basic Details');