Subversion Repositories portal2

Rev

Rev 722 | 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_trackers_add_submit">
        <h1>
                <?php echo _('Adding tracker'); ?>
        </h1>
</div>

<?php
$ok = false;
$error = new Error();

if(!isset($_POST['form_token'],
                $_POST['form_device_key'], $_POST['form_sensor_key'], $_POST['form_variable_key'],
                $_POST['form_tracker_name'], $_POST['form_tracker_index'], $_POST['form_tracker_unit'],
                $_POST['form_tracker_offset'], $_POST['form_tracker_step']))
{
        $error->push(_('One or more fields are missing.'));
}
else
if( $_POST['form_token'] != $_SESSION['form_token'])
{
        $error->push(_('Internal error: invalid token.'));
}
else
if(filter_var($_POST['form_tracker_offset'], FILTER_VALIDATE_INT) === false)
{
        $error->push(_('Incorrect numeric value.'));
}
else
if(isset($_POST['form_cancel']))
{
        $form_device_key         = filter_var($_POST['form_device_key'], FILTER_SANITIZE_STRING);
        $error->push(_('Operation aborted.'));
}
elseif(strlen( $_POST['form_tracker_name']) > 64 || strlen($_POST['form_tracker_name']) < 4)
{
        $error->push(_('Name must have [4-64] characters.'));
}
else
{
        $form_device_key     = filter_var($_POST['form_device_key'], FILTER_SANITIZE_STRING);
        $form_sensor_key     = filter_var($_POST['form_sensor_key'], FILTER_SANITIZE_STRING);
        $form_variable_key   = filter_var($_POST['form_variable_key'], FILTER_SANITIZE_STRING);

        $form_tracker_name   = filter_var($_POST['form_tracker_name'], FILTER_SANITIZE_STRING);
        $form_tracker_unit   = filter_var($_POST['form_tracker_unit'], FILTER_SANITIZE_STRING);
        $form_tracker_offset = filter_var($_POST['form_tracker_offset'], FILTER_SANITIZE_NUMBER_INT);
        $form_tracker_step   = filter_var($_POST['form_tracker_step'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_SCIENTIFIC | FILTER_FLAG_ALLOW_FRACTION);

        $park                = $_SESSION['park'];
        if($park)
        {
                $device        = $park->getDevice($form_device_key);

                $path          = $park->buildPathFromKeys($form_device_key, $form_sensor_key, $form_variable_key);
                $tracker       = TrackerFactory::createTracker($form_tracker_name);

                $sensor        = $park->getSensorFromPath($path);
                $sensor_name   = $sensor->getName();

                $variable      = $park->getVariableFromPath($path);
                $variable_name = $variable->getName();

                $tracker->setIndex($device->countTrackers());
                $tracker->setName($form_tracker_name);
                $tracker->setOffset($form_tracker_offset);
                $tracker->setStep($form_tracker_step);
                $tracker->setSensorName($sensor_name);
                $tracker->setVariableName($variable_name);
                $tracker->setUnit($form_tracker_unit);

                $device->addTracker($path, $tracker);
               
                try
                {
                        $device->saveTrackers();
                }
                catch (MyException $e)
                {
                        $error->push($e->getErrorReport());            
                }      

                $error->push(_('Tracker added.'));

                $ok            = true;
        }

        /*** save configuration contents ***/
        merge_trackers_and_data();
        config_save();
}

echo $error->display();

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

?>