Subversion Repositories portal2

Rev

Rev 723 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*  emg - Energy Monitoring & Graphing
*  Copyright (C) 2006-2012 Romain Lievin
*
*  This program 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 3 of the License, or
*  (at your option) any later version.
*
*  This program 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 this program; if not, write to the Free Software Foundation,
*  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

?>

<div id="page_form_data_add_submit">
        <h1>
                <?php echo _('Add Data'); ?>
        </h1>
</div>

<?php

$ok = false;
$error = new Error();

/*** first check that both the username, password and form token have been sent ***/
if(!isset($_POST['form_token_2'], $_POST['form_data'],
                $_POST['form_datum_name'], $_POST['form_datum_type'],
                $_POST['form_datum_unit'], $_POST['form_datum_a'],
                $_POST['form_datum_b']
        ))
{
        $error->push(_('One or more fields are missing.'));
}
else
if( $_POST['form_token_2'] != $_SESSION['form_token_2'])
{
        $error->push(_('Internal error: invalid token.'));
}
else
if(isset($_POST['form_cancel']))
{
        $error->push(_('Operation aborted.'));
        unset($_SESSION['groups']);
}
elseif(strlen( $_POST['form_datum_name']) > 64 || strlen($_POST['form_datum_name']) < 4)
{
        $error->push(_('Name must have [4-64] characters.'));
}
else
if(filter_var($_POST['form_datum_type'], FILTER_VALIDATE_INT) === false)
{
        $error->push(_('Incorrect numeric value.'));
}
else
if(filter_var($_POST['form_datum_b'], FILTER_VALIDATE_INT) === false)
{
        $error->push(_('Incorrect numeric value.'));
}
else
if(filter_var($_POST['form_datum_a'], FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_SCIENTIFIC | FILTER_FLAG_ALLOW_FRACTION) === false)
{
        $error->push(_('Incorrect numeric value.'));
}

else
if(!isset($_SESSION['groups']))
{
        $error->push(_('You must add items to the group before submitting them.'));
}
else
if($_SESSION['groups']->getCount() < 2)
{
        $error->push(_('A group requires 2 items at least.'));
}
else
{
        $form_datum_name = filter_var($_POST['form_datum_name'], FILTER_SANITIZE_STRING);
        $form_datum_type = filter_var($_POST['form_datum_type'], FILTER_SANITIZE_NUMBER_INT);
        $form_datum_unit = filter_var($_POST['form_datum_unit'], FILTER_SANITIZE_STRING);
        $form_datum_a    = filter_var($_POST['form_datum_a'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_SCIENTIFIC | FILTER_FLAG_ALLOW_FRACTION);
        $form_datum_b    = filter_var($_POST['form_datum_b'], FILTER_SANITIZE_NUMBER_INT);

        // check that group is homogen
        {
                $groups          = $_SESSION['groups'];
                foreach($groups as $key => $group)
                {
                }
        }

        $datum = DatumFactory::createDatumObject($form_datum_name, $form_datum_type);
        $datum->setGroups($_SESSION['groups']);
        $datum->setType($form_datum_type);
        $datum->setUnit($form_datum_unit);
        $datum->setSlope($form_datum_a);
        $datum->setIntercept($form_datum_b);
        if($form_datum_type !== DatumFactory::SINGLE)
        {
                $group_keys = $groups->getKeys();
                $group = $groups->getItem($group_keys[0]);
                $datum->setPath($_SESSION['park']->getDeviceKeyFromPath($group->getPath()));
        }      

        $data  = $_SESSION['data'];
        if($data->isItemExists($form_datum_name))
        {
                $error->push(_('Data already exists. Choose another name.'));
                $ok = false;
        }
        else
        {
                $datum_key_ = str_replace(' ', '_', $datum->getName());
                $data->addItem($datum_key_, $datum);

                $error->push(_('Data added.'));
                $ok         = true;

                unset($_SESSION['groups']);
        }

        /*** save configuration contents ***/
        //config_update();
}

echo $error->display();

/* Redirection */
if(isset($_POST['form_cancel']) || ($ok === true))
{
        $id = link_get_id_from_filename("form_data_list.php");
        $refresh = $_SESSION['refresh_redirection'];
echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}
else if($ok == false)
{
        $id = link_get_id_from_filename("form_data_add.php");
        $refresh = $_SESSION['refresh_redirection'];
echo "<meta http-equiv=\"refresh\" content=\"$refresh; URL=index.php?id_page=$id\">";
}

?>