<?php

if($_POST['values'] && AllowEdit())
{
    foreach($_POST['values'] as $id => $columns)
    {

        if($id != 'new'){
            $sql = "UPDATE APP_IDGEN SET ";

            foreach($columns as $column => $value){
                $sql .= $column."='".str_replace("\'", "''", $value)."',";
            }
            $sql = substr($sql, 0, -1)." WHERE ID='$id'";
            db_execute($sql);
//echo $sql;
        }
        else
        {
            $sql = "INSERT INTO APP_IDGEN ";

            $fields = 'ID,SCHOOL_ID,';
            $values = db_seq_nextval('APP_IDGEN_SEQ').",'".get_school_id()."',";

            $go = 0;
            foreach($columns as $column => $value)
            {
                if($value)
                {
                    $fields .= $column.',';
                    $values .= "'".str_replace("\'", "''", $value)."',";
                    $go = true;
                }
            }
            $sql .= '('.substr($fields, 0, -1).') values('.substr($values, 0, -1).')';
//echo $sql;
            if($go)
                db_execute($sql);
        }
    }
}

if($_REQUEST['servicefunction'] == 'remove_id' && AllowEdit())
{
        if(DeletePrompt(_('data element')))
        {
        	db_execute("DELETE FROM APP_IDGEN WHERE ID='$_REQUEST[id]'");
                unset($_REQUEST['servicefunction']);
                $remove_id = false;
	}
}
                        
if($_REQUEST['servicefunction'] != 'remove_id')
{

    $columns = array('ELEMENT_ID' => _('Data Element'),
            'SORT_ORDER' => _('Sort Order'),
            'IDMASK' => _('ID Mask'));
    $aids_array = db_request(db_execute("SELECT * FROM APP_IDGEN WHERE SCHOOL_ID='".get_school_id()."' ORDER BY SORT_ORDER"),array('ELEMENT_ID'=>'_getElem','IDMASK'=>'_makeTextInput','SORT_ORDER'=>'_makeTextInput'));
    $link['add']['html'] = array('ELEMENT_ID'=>_makeElemInput('', 'ELEMENT_ID'), 'SORT_ORDER' => _makeTextInput('', 'SORT_ORDER'), 'IDMASK' => _makeTextInput('', 'IDMASK'),);
    $link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&system_tab=$_REQUEST[system_tab]&tab=$_REQUEST[tab]&servicefunction=remove_id";
    $link['remove']['variables'] = array('id' => 'ID');
    $GO_options = array('save'=>false);
    Gridoutput($aids_array, $columns, _('Data'), _('Amount of Data Elements'), $link, array(),$GO_options);

}

function _makeTextInput($value, $name)
{
    global $THIS_array;

    if($THIS_array['ID'])
    {
        $id = $THIS_array['ID'];
        $div = 'force';
    }else{
        $id = 'new';
        $div = false;
    }
    if($name != 'TITLE')
        $addition = 'size=5 maxlength=10';

    return TextInput($value, 'values['.$id.']['.$name.']', '', $addition, $div);
}

function _getElem($value)
{
    $options = array('1'=>_('School Name'),
		    '2'=>_('Faculty'),
		    '3'=>_('Major'),
		    '4'=>_('Special Subject'),
		    '5'=>_('Form of Education'),
		    '6'=>_('Competitive Category'));
    return $options[$value];
}
function _makeElemInput($value, $name)
{
    global $THIS_array;
    $options = array('1'=>_('School Name'),
		    '2'=>_('Faculty'),
		    '3'=>_('Major'),
		    '4'=>_('Special Subject'),
		    '5'=>_('Form of Education'),
		    '6'=>_('Competitive Category'));
    if($THIS_array['ELEMENT_ID'])
    {
        $id = $THIS_array['ELEMENT_ID'];
        $div = 'force';
    }
    else
    {
        $id = 'new';
        $div = false;
    }

    return SelectInput($value, 'values['.$id.']['.$name.']','',$options, _('N/A'),'',$div,'',false);
}

?>