/*******************************************************************************<?php/*
 *
 * LEIDEN OPEN VARIATION DATABASE (LOVD)
 *
 * Created     : 2007-10-11
 * Modified    : 2009-04-09
 * Version     : 0.4
 * For LOVD    : 2.0-18
 *
 * Copyright   : 2004-2009 Leiden University Medical Center; http://www.LUMC.nl/
 * Programmer  : Ing. Ivo F.A.C. Fokkema <I.F.A.C.Fokkema@LUMC.nl>
 * Last edited : Ing. Ivo F.A.C. Fokkema <I.F.A.C.Fokkema@LUMC.nl>
 *
 *
 * This file is part of LOVD.
 *
 * LOVD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LOVD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LOVD; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *************/

class showmaxdbid {
    function getInfo ()
    {
        return
             array(
                    'name' => 'LOVD Show Maximum DB ID module',
                    'version' => '0.4',
                    'description' => 'LOVD module that shows the first free variant database ID on the form.',
                    'settings' => array(),
                  );
    }



    function enable ()
    {
        // Will be run when module is getting enabled.
        /*
        $aSett = $GLOBALS['_MODULES']->getSettings('showmaxdbid');
        $GLOBALS['_MODULES']->setSettings('showmaxdbid', $aSett);
        */
    }



    function processForm ($sForm, & $aForm)
    {
        // Process LOVD form data before it gets to the screen.

        if ($sForm == 'SubmitVariantData' || $sForm == 'SubmitVariantsEdit') {
            $aSett = $GLOBALS['_MODULES']->getSettings('showmaxdbid');
            //$_CURRDB = mysql_fetch_assoc(mysql_query('SELECT * FROM ' . TABLE_DBS . ' WHERE symbol = "' . $_POST['gene'] . '"'));

            $aReturn = array();
            foreach ($aForm as $nItem => $aItem) {
                // Line by line, go through the form.
                if ($aItem[2] == 'Variant/DBID') {
                    // Add information about the last DB ID.
                    // 2008-04-15; 2.0-06; We need to display the gene currently selected for submission, which is not always $_SESSION['currdb']!!!
                    if (lovd_getProjectFile() == '/submit.php') {
                        $sGene = $_POST['gene'];
                        $sSymb = substr($_POST['gene'], 0, strpos($_POST['gene'] . '_', '_'));
                    } else {
                        $sGene = $_SESSION['currdb'];
                        $sSymb = $_SESSION['currsymb'];
                    }
                    $lID = strlen($sSymb) + 6;
                    list($zMaxDBID) = mysql_fetch_row(mysql_query('SELECT MAX(LEFT(`Variant/DBID`, ' . $lID . ')) FROM `' . TABLEPREFIX . '_' . mysql_real_escape_string($sGene) . '_variants` WHERE LEFT(`Variant/DBID`, ' . $lID . ') REGEXP "^' . $sSymb . '_[0-9]{5}"'));
                    // 2008-10-16; 2.0-13; Some small modifications here to change "Max in use" to "First ID free".
                    if (!$zMaxDBID) {
                        $zMaxDBID = $sSymb . '_00001';
                    } else {
                        $zMaxDBID ++; // I know this looks weird, but it works.
                    }

                    // 2008-10-16; 2.0-13; Check submission for used DBID's.
                    if (lovd_getProjectFile() == '/submit.php' && count($_SESSION['submit_muts'][$_POST['gene']])) {
                        // This is a new ID, make sure it does not exist in this submission.
                        while (true) {
                            foreach ($_SESSION['submit_muts'][$_POST['gene']] as $aSession) {
                                if (substr($aSession['Variant/DBID'], 0, strlen($zMaxDBID)) == $zMaxDBID) {
                                    // Exists already and this is not the same variant!
                                    $zMaxDBID ++; // I know this looks weird, but it works.
                                    continue 2;
                                }
                            }
                            break;
                        }
                    }

                    // 2009-04-09; 2.0-18; Implemented #39 - Button to enter "First ID free"
                    $aItem[0] .= ' (First ID free: ' . $zMaxDBID . ')&nbsp;<SPAN id="copy_free_id">(<A href="#" onclick="javascript:document.forms[0][\'Variant/DBID\'].value=\'' . $zMaxDBID . '\'; return false;">copy to field</A>)</SPAN>';
                    $aReturn[] = $aItem;
                } else {
                    $aReturn[] = $aItem;
                }
            }
            $aForm = $aReturn;
            return true;
        }
        return false;
    }
}