use PSU\TeacherCert;
respond('/[i:sgs_id]/[*]', function ($request, $response, $app) {
    $sgs_id = $request->param('sgs_id');
    $app->populate(TeacherCert\Student\GateSystem::get($sgs_id));
    $app->populate('schools', new TeacherCert\Schools());
    $app->populate('school_model', new TeacherCert\Model\Student\School());
    $app->populate('teacher_model', new TeacherCert\Model\Student\CooperatingTeacher());
    $app->populate('cancel_url', $app->resolver($app->student_gate_system));
    $app->breadcrumbs->push(new PSU\Template\Breadcrumb($app->student_gate_system->gate_system()->name, $app->resolver($app->gate_system)));
    $app->breadcrumbs->push(new PSU\Template\Breadcrumb(sprintf('%s (%s)', $app->student_gate_system->student()->person()->formatName('f l'), $app->student_gate_system->student()->person()->id), $app->resolver($app->student_gate_system)));
    $app->populate_constituents = function () use($app) {
        $rc = TeacherCert\ActiveRecord::$rowcache = new TeacherCert\RowCache();
        $where = sprintf('id IN (SELECT constituent_id FROM psu_teacher_cert.constituent_schools WHERE school_id = %d)', $app->student_school->school_id);
        $rc->cache('PSU\\TeacherCert\\Constituent', 'id', $where);
        $constituent_schools = new TeacherCert\CooperatingTeachers($app->student_school->school_id);
        $schools_options = TeacherCert\Model::collection($constituent_schools, 'name=constituent_name');
        usort($schools_options, function ($a, $b) {
            return strnatcasecmp($a[1], $b[1]);
        });
        $app->teacher_model->constituent_school_id->options = $schools_options;
    };
});
respond('GET', '/[i:sgs_id]/[add|edit|delete:action]/[i:ss_id]?', function ($request, $response, $app) {
    $ss_id = $request->param('ss_id');
    if ('edit' === $request->param('action')) {
        $app->populate('student_school', TeacherCert\Student\School::get($ss_id));
        $app->populate_constituents();
        // populate form with existing school data
        $f = get_object_vars($app->student_school);
        if (isset($_SESSION['tcert-student-school-save'])) {
            $sf = $_SESSION['tcert-student-school-save'];