public function getFilterForMailJob($job, $respondentId = null, $organizationId = null)
    {
        // Set up filter
        $filter = array(
        	'can_email'           => 1,
            'gtr_active'          => 1,
            'gsu_active'          => 1,
            'grc_success'         => 1,
        	'gto_completion_time' => NULL,
        	'gto_valid_from <= CURRENT_TIMESTAMP',
            '(gto_valid_until IS NULL OR gto_valid_until >= CURRENT_TIMESTAMP)'
        );

        switch ($job['gcj_filter_mode']) {
            case 'E':   // Reminder before expiry
                $filter[] = 'gto_mail_sent_date < CURRENT_DATE() AND CURRENT_DATE() = DATE(DATE_SUB(gto_valid_until, INTERVAL ' . $job['gcj_filter_days_between'] . ' DAY))';
                break;

            case 'R':   // Reminder after first email
                $filter[] = 'gto_mail_sent_date <= DATE_SUB(CURRENT_DATE, INTERVAL ' . $job['gcj_filter_days_between'] . ' DAY)';
                $filter[] = 'gto_mail_sent_num <= ' . $job['gcj_filter_max_reminders'];
                break;

            case 'N':   // First email
            default:
                $filter['gto_mail_sent_date'] = NULL;
                break;
        }
        if ($job['gcj_id_organization']) {
            if ($organizationId && ($organizationId !== $job['gcj_id_organization'])) {
                // Should never return any data
                $filter[] = '1=0';
                return $filter;
            }
            $filter['gto_id_organization'] = $job['gcj_id_organization'];
        }
        if ($job['gcj_id_track']) {
            $filter['gto_id_track'] = $job['gcj_id_track'];
        }
        if ($job['gcj_round_description']) {
            if ($job['gcj_id_track']) {
                $roundIds = $this->db->fetchCol('
                    SELECT gro_id_round FROM gems__rounds WHERE gro_active = 1 AND gro_id_track = ? AND gro_round_description = ?', array(
                    $job['gcj_id_track'],
                    $job['gcj_round_description'])
                );
            } else {
                $roundIds = $this->db->fetchCol('
                    SELECT gro_id_round FROM gems__rounds WHERE gro_active = 1 AND gro_round_description = ?', array(
                    $job['gcj_round_description'])
                );
            }
            // Add round 0 for inserted rounds, and check if the description matches
            $filter[] = sprintf('(gto_id_round IN (%s)) OR (gto_id_round = 0 AND gto_round_description = %s)', $this->db->quote($roundIds), $this->db->quote($job['gcj_round_description']));
        }
        if ($job['gcj_id_survey']) {
            $filter['gto_id_survey'] = $job['gcj_id_survey'];
        }
        if ($respondentId) {
            $filter['gto_id_respondent'] = $respondentId;
        }

        if ($job['gcj_target'] == 1) {
            // Only relations
            $filter[] = 'gto_id_relation <> 0';
        } elseif ($job['gcj_target'] == 2) {
            // Only respondents
            $filter[] = '(gto_id_relation = 0 OR gto_id_relation IS NULL)';
        }

        return $filter;
    }