<?php
function _get_cat_title($value,$name)
{
    if(!$_SYSTEM['STUDENT_FIELD_CATEGORIES'])
        $_SYSTEM['STUDENT_FIELD_CATEGORIES'] = db_request(db_execute("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES ORDER BY SORT_ORDER"),array(),array('ID'));
    return $_SYSTEM['STUDENT_FIELD_CATEGORIES'][$value][1]['TITLE'];
}

if($_REQUEST['tables'] && $_POST['tables'] && AllowEdit())
{
    foreach($_REQUEST['tables'] as $table_name=>$tables)
    {
        foreach($tables as $id=>$columns)
        {
            if($id!='new')
            {
                $sql = "UPDATE $table_name SET ";
                foreach($columns as $column=>$value)
                {
                    $sql .= $column."='".str_replace("\'","''",$value)."',";
                    $go = true;
                }
                $sql = substr($sql,0,-1)." WHERE ID='$id'";
                if($go)
                    db_execute($sql);
            }
            else
            {
                    $sql = "INSERT INTO $table_name ";
                    $fields .= "SCHOOL_YEAR,";
                    $values .= "'".get_school_year()."',";
                    $go = 0;
                    foreach($columns as $column=>$value)
                    {
                            if(isset($value))
                            {
                                    $fields .= $column.',';
                                    $values .= "'".str_replace("\'","''",$value)."',";
                                    $go = true;
                            }
                    }
                    $sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
                    if($go)
                        db_execute($sql);
                    if($table_name=='APP_EDU_CATEGORIES')
                    {
                        $id = db_request(db_execute("SELECT MAX(ID) AS ID FROM APP_EDU_CATEGORIES WHERE SCHOOL_YEAR = '".get_school_year()."'"));
                        $_REQUEST['edu_cat_id']=$id[1]['ID'];
                    }
            }
        }
    }
}
//inputAPP_ONLINE_USE
unset($_REQUEST['tables']);
//var_dump($_REQUEST["APP_ONLINE_USE"]);
$ex_online_field = db_request(db_execute("SELECT EDU_CAT_ID,CATEGORY_ID,FIELD_ID, APP_ONLINE_USE FROM ONLINE_FIELDS WHERE EDU_CAT_ID='".$_REQUEST['edu_cat_id']."' AND CATEGORY_ID='".$_REQUEST['field_cat_id']."'"),array(),array('FIELD_ID'));
if($_REQUEST["APP_ONLINE_USE"] && $_REQUEST["field_cat_id"]){
    if(count($_REQUEST["APP_ONLINE_USE"])){
        foreach ($_REQUEST["APP_ONLINE_USE"] as $field_id=>$Y)
        {
            $Y = ($Y=='Y')?'Y':'N';
            if($ex_online_field[$field_id][1]['APP_ONLINE_USE']=='Y' || $ex_online_field[$field_id][1]['APP_ONLINE_USE']=='N')
                db_execute("UPDATE ONLINE_FIELDS SET APP_ONLINE_USE = '".$Y."' WHERE category_id='".$_REQUEST['field_cat_id']."' AND EDU_CAT_ID='".$_REQUEST['edu_cat_id']."' AND FIELD_ID = '".$field_id."'");
            elseif($ex_online_field[$field_id][1]['APP_ONLINE_USE']=='')
                db_execute("INSERT INTO ONLINE_FIELDS (EDU_CAT_ID,CATEGORY_ID,FIELD_ID, APP_ONLINE_USE) VALUES ('".$_REQUEST['edu_cat_id']."','".$_REQUEST['field_cat_id']."','".$field_id."','".$Y."')");
        }
    }
}

if(AllowEdit() && $_REQUEST['edu_cat_id']!='new' && $_REQUEST['edu_field_id']!='new')
    $delete_button = "<INPUT type=button value="._('Delete')." onClick='javascript:window.location=\"modloader.php?servicemod=$_REQUEST[servicemod]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab]&edu_cat_id=$_REQUEST[edu_cat_id]&field_cat_id=$_REQUEST[field_cat_id]&servicefunction=delete\"'> &nbsp; &nbsp; ";
if($_REQUEST['field_cat_id']=='new' && $_REQUEST['edu_cat_id']<>'')
{
    $field_categories_array = db_request(db_execute("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES  WHERE ID NOT IN (SELECT FIELD_CATEGORY_ID FROM STUDENT_FIELD_CATEGORIES_IN_EDU WHERE EDU_CATEGORY_ID='$_REQUEST[edu_cat_id]' AND SCHOOL_YEAR='".get_school_year()."') ORDER BY SORT_ORDER"),array(),array('ID'));
    $options = array();
    if(count($field_categories_array))
    {
            foreach($field_categories_array as $category_id=>$category)
            {
                    $options[$category_id] = $category[1]['TITLE'];
            }
    }
    echo "<INPUT TYPE=\"hidden\" name=\"tables[STUDENT_FIELD_CATEGORIES_IN_EDU][$_REQUEST[field_cat_id]][EDU_CATEGORY_ID]\" VALUE=\"$_REQUEST[edu_cat_id]\">";
    echo $HTMLElem->printText(_('Select a Field Category: ')).SelectInput('','tables[STUDENT_FIELD_CATEGORIES_IN_EDU]['.$_REQUEST[field_cat_id].'][FIELD_CATEGORY_ID]','',$options);
}
elseif($_REQUEST['edu_cat_id'] && $_REQUEST['field_cat_id']=='')
{
    if($_REQUEST['edu_cat_id']!='new')
        {
                $sql = "SELECT ID AS EDU_CATEGORY_ID,SCHOOL_ID,TITLE,SHORT_NAME FROM APP_EDU_CATEGORIES WHERE ID='$_REQUEST[edu_cat_id]'";
                $QI = db_execute($sql);
                $array = db_request($QI);
                $array = $array[1];
                $title = $array['TITLE'];
                $div = 'force';
                $new = false;
        }
        else
        {
                $title = _('New Competitive Group');
                //unset($delete_button);
                unset($array);
                $div = false;
                $new = true;
        }

    $applicant_school_array = db_request(db_execute("SELECT ID,TITLE FROM SCHOOLS  WHERE ID IN (".implode(',',get_apply_school_array()).")"),array(),array('ID'));
    $options = array();
    if(count($applicant_school_array))
    {
            foreach($applicant_school_array as $app_school_id=>$info)
            {
                    $options[$app_school_id] = $info[1]['TITLE'];
            }
    }
    DisplayHeader($title,'<table><tr><td>'.$delete_button.$submitButton->createSButton(_('Save')).'</td></tr></table>','','','','','','','no');
    $title = '<b>'.$title.'</b>';
    $header .= '<TABLE  cellpadding="0" width=600px cellspacing="0">';
    $header .= '<TR>';
    $header .= '<TD width=200>'.TextInput($array['TITLE'], 'tables[APP_EDU_CATEGORIES]['.$_REQUEST['edu_cat_id'].'][TITLE]', _('Title')).'</TD>';
    $header .= '<TD width=200>'.TextInput($array['SHORT_NAME'], 'tables[APP_EDU_CATEGORIES]['.$_REQUEST['edu_cat_id'].'][SHORT_NAME]', _('Short Name')).'</TD>';
    $header .= '<TD width=200>'.$HTMLElem->printText(_('Select a School(Admission mode): ')).SelectInput($array['SCHOOL_ID'],'tables[APP_EDU_CATEGORIES]['.$_REQUEST[edu_cat_id].'][SCHOOL_ID]','',$options).'</TD>';

    $header .= '</TR>';
    $header .= '</TABLE>';
    DisplayHeader($header,'','','','','','','',true);

}
elseif($_REQUEST['edu_cat_id'] && $_REQUEST['field_cat_id'])
{
    $sql = db_execute("SELECT TITLE,ALTERNATIVE_NAME,SORT_ORDER FROM STUDENT_FIELD_CATEGORIES WHERE ID='$_REQUEST[field_cat_id]' ORDER BY SORT_ORDER");
    $array = db_request($sql);
    $array = $array[1];
    $title = $array['TITLE'];
    $title = '<b>'.$title.'</b>';
    DisplayHeader($title,'<table><tr><td>'.$delete_button.$submitButton->createSButton(_('Save')).'</td></tr></table>','','','','','','',true);
    $header .= '<TABLE  cellpadding="0" width=600px cellspacing="0">';
    $header .= '<TR>';
    $header .= '<TD width=200>'.TextInput($array['ALTERNATIVE_NAME'], 'tables[STUDENT_FIELD_CATEGORIES]['.$_REQUEST['field_cat_id'].'][ALTERNATIVE_NAME]', _('Alternate Title')).'</TD>';
    $header .= '<TD width=200>'.TextInput($array['SORT_ORDER'], 'tables[STUDENT_FIELD_CATEGORIES]['.$_REQUEST['field_cat_id'].'][SORT_ORDER]', _('Sort Order')).'</TD>';
    $header .= '</TR>';
    $header .= '</TABLE>';
    DisplayHeader($header,'','','','','','','',true);
}
$Gridoutput_options = array('save'=>false,'search'=>false,'xscroll'=>false);
$sql = "SELECT ID AS EDU_CATEGORY_ID,TITLE FROM APP_EDU_CATEGORIES WHERE SCHOOL_YEAR='".get_school_year()."' ORDER BY TITLE";
$QI = db_execute($sql);
$EDU_CAT_array = db_request($QI);
echo '<TABLE width=100% cellspacing=0><TR>';
if(count($EDU_CAT_array))
{
        if($_REQUEST['edu_cat_id'])
        {
                foreach($EDU_CAT_array as $key=>$value)
                {
                        if($value['EDU_CATEGORY_ID']==$_REQUEST['edu_cat_id'])
                                $EDU_CAT_array[$key]['row_color'] = Preferences('HIGHLIGHT');
                }
        }
}
echo '<TD valign=top width='.$cell_width.'>';
$link = array();
$columns = array('TITLE'=>_('Competitive Categories'));
$link['TITLE']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab]&useontop=".$_REQUEST['useontop'];
$link['TITLE']['variables'] = array('edu_cat_id'=>'EDU_CATEGORY_ID');
$link['add']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab]&edu_cat_id=new";
unset($orig_css);
Gridoutput($EDU_CAT_array,$columns,_('Competitive Categories'),_('Amount of Competitive Categories'),$link,array(),$Gridoutput_options);
echo '</TD>';
if($_REQUEST['edu_cat_id'] && $_REQUEST['edu_cat_id']<>'new')
{
    $sql = "SELECT EDU_CATEGORY_ID,FIELD_CATEGORY_ID,FIELD_CATEGORY_ID AS CAT_TITLE FROM student_field_categories_in_edu s WHERE EDU_CATEGORY_ID='$_REQUEST[edu_cat_id]' AND SCHOOL_YEAR='".get_school_year()."' ORDER BY FIELD_CATEGORY_ID";
    $QI = db_execute($sql);
    $field_cat_array = db_request($QI,array('CAT_TITLE'=>'_get_cat_title'),array());
    if(count($field_cat_array))
    {
            if($_REQUEST['field_cat_id'])
            {
                    foreach($field_cat_array as $key=>$value)
                    {
                            if($value['FIELD_CATEGORY_ID']==$_REQUEST['field_cat_id'])
                                    $field_cat_array[$key]['row_color'] = Preferences('HIGHLIGHT');
                    }
            }
    }
    echo '<TD valign=top width='.$cell_width.'>';
    $columns = array('CAT_TITLE'=>_('Field Category'));
    $link = array();
    $link['CAT_TITLE']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab]&useontop=".$_REQUEST['useontop']."&edu_cat_id=$_REQUEST[edu_cat_id]";
    $link['CAT_TITLE']['variables'] = array('field_cat_id'=>'FIELD_CATEGORY_ID');
    $link['add']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab]&edu_cat_id=$_REQUEST[edu_cat_id]&field_cat_id=new";
    Gridoutput($field_cat_array,$columns,_('Field Categories'),_('Amount of Field Categories'),$link,array(),$Gridoutput_options);
    echo '</TD>';
    if($_REQUEST['field_cat_id'] && $_REQUEST['field_cat_id']<>'new')
    {
        echo '<TD>';
        //.$sql = "SELECT sf.ID,sf.TITLE,sf.APP_ONLINE_USE,sf.REQUIRED from student_fields sf,STUDENT_FIELDS_IN_CATS sfc WHERE category_id='$_REQUEST[field_cat_id]' AND (FOR_APPLICANTS='Y' OR USE_APPLICANTS='Y' OR REQUIRED='Y') ORDER BY id";
        //$sql = "SELECT ID,TITLE,APP_ONLINE_USE,REQUIRED,FOR_APPLICANTS,USE_APPLICANTS FROM student_fields WHERE CATEGORY_ID='$_REQUEST[field_cat_id]' AND (FOR_APPLICANTS='Y' OR USE_APPLICANTS='Y' OR REQUIRED='Y') OR ID IN (SELECT sfc.FIELD_ID,sf.ID,sf.FOR_APPLICANTS,sf.USE_APPLICANTS,sf.REQUIRED FROM  STUDENT_FIELDS_IN_CATS sfc ,STUDENT_FIELDS sf WHERE sfc.FIELD_ID=sf.ID AND (sf.FOR_APPLICANTS='Y' OR sf.USE_APPLICANTS='Y' OR sf.REQUIRED='Y') AND CATEGORY_ID='".$_REQUEST['field_cat_id']."') ORDER BY id";
        $online_field = db_request(db_execute("SELECT EDU_CAT_ID,CATEGORY_ID,FIELD_ID, APP_ONLINE_USE FROM ONLINE_FIELDS"),array(),array('EDU_CAT_ID','CATEGORY_ID','FIELD_ID'));
        $sql = "SELECT ssf.ID,ssf.TITLE,ssf.ID AS APP_ONLINE_USE
                FROM student_fields ssf
                WHERE ssf.CATEGORY_ID='".$_REQUEST['field_cat_id']."' AND ssf.TYPE<>'holder' AND
                (ssf.FOR_APPLICANTS='Y' OR ssf.USE_APPLICANTS='Y' OR ssf.REQUIRED='Y') OR
                ID IN (
                    SELECT sfc.FIELD_ID
                    FROM  STUDENT_FIELDS_IN_CATS sfc ,STUDENT_FIELDS sf
                    WHERE sfc.field_id=sf.id AND sf.TYPE<>'holder' AND
                    (sf.for_applicants='Y' OR sf.use_applicants='Y' OR sf.required='Y')
                    AND sfc.category_id='".$_REQUEST['field_cat_id']."'
                )
                ORDER BY id
            ";
        $QI = db_execute($sql);
        $field_array = db_request($QI,array('APP_ONLINE_USE'=>'_makeCheckbox'),array());
        
        //,array('APP_ONLINE_USE'=>'_makeCheckbox'),array()
        //var_dump($field_array);
        echo '<TD valign=top width='.$cell_width.'>';
        $columns = array('TITLE'=>_('Field Title'),'APP_ONLINE_USE'=>_('Use'));
        $link = array();
        Gridoutput($field_array,$columns,_('Student Fields'),_('Amount of Fields'),$link,array(),$Gridoutput_options);
        echo '</TD>';
    }

}
echo '</tr>';
echo '</table>';
function _makeCheckbox($value,$title)
{
    global $THIS_array,$_SYSTEM,$online_field,$_REQUEST;
    if($THIS_array['REQUIRED']=='Y')
        return _('Yes');
    else
        return CheckBoxInput($online_field[$_REQUEST['edu_cat_id']][$_REQUEST['field_cat_id']][$value][1]['APP_ONLINE_USE'],'APP_ONLINE_USE['.$THIS_array['ID'].']','','',true);
    
}
?>
