<?php

/*  Alternate WebKit
(C) 2013 by Alternate Tools
-----------------------------------------------------

    filename: class_form.php
*/


if (!defined ("CLASS_FORM"))
{
    define ("CLASS_FORM", "defined");
    define ("FORMTYPE_INPUT", "0");
    define ("FORMTYPE_TEXTINPUT", "1");
    define ("FORMTYPE_CHECKBOX", "2");
    define ("FORMTYPE_RADIO", "3");
    define ("FORMTYPE_COMBOBOX", "4");

    include "class_storagefile.php";

    class cForm
    {
        var $m_frmId;
        var $m_frmDate;
        var $m_frmDescriptions;
        var $m_frmTypes;
        var $m_frmValues;
        var $m_frmSizes;
        var $m_frmLenghts;
        var $m_frmMandatorys;
        var $m_frmMaxFields;
        var $m_frmAdditionalEMail;
        var $m_frmLocked;
        var $m_frmFolder;

        function cForm()  // PHP 4
        {
            $this->Init();
        }

        function __construct()
        {
            $this->Init();
        }

        function Init()
        {
            $this->m_frmId = "0";
            $this->m_frmDate = "";
            unset($this->m_frmDescriptions);
            $this->m_frmDescriptions = array();
            unset($this->m_frmTypes);
            $this->m_frmTypes = array();
            unset($this->m_frmValues);
            $this->m_frmValues = array();
            unset($this->m_frmSizes);
            $this->m_frmSizes = array();
            unset($this->m_frmLenghts);
            $this->m_frmLenghts = array();
            unset($this->m_frmMandatorys);
            $this->m_frmMandatorys = array();
            $this->m_frmMaxFields = 1;
            $this->m_frmAdditionalEMail = "";
            $this->BuildArray();
            $this->m_frmLocked = "0";

            $this->m_frmFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_frmId;
        }
        
        function GetDate()
        {
            return $this->m_frmDate;
        }
        
        function DeleteField($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmDescriptions[$index] = "";
            $this->m_frmTypes[$index] = "";
            $this->m_frmValues[$index] = "";
            $this->m_frmSizes[$index] = "";
            $this->m_frmLenghts[$index] = "";
            $this->m_frmMandatorys[$index] = "";
        }
        
        function SetDescription($index, $description)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmDescriptions[$index] = $description;
        }
        function GetDescription($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            return $this->m_frmDescriptions[$index];
        }

        function SetType($index, $type)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmTypes[$index] = $type;
        }
        function GetType($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            return $this->m_frmTypes[$index];
        }

        function SetValue($index, $value)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmValues[$index] = $value;
        }
        function GetValue($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            return $this->m_frmValues[$index];
        }

        function SetSize($index, $size)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmSizes[$index] = $size;
        }
        function GetSize($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            return $this->m_frmSizes[$index];
        }

        function SetLength($index, $length)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmLenghts[$index] = $length;
        }
        function GetLength($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            return $this->m_frmLenghts[$index];
        }

        function SetMandatory($index, $mandatory)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            $this->m_frmMandatorys[$index] = $mandatory;
        }
        function GetMandatory($index)
        {
            if ($index >= $this->m_frmMaxFields)
                return;
            return $this->m_frmMandatorys[$index];
        }

        function SetAdditionalEMail($email)
        {
            $this->m_frmAdditionalEMail = $email;
        }
        function GetAdditionalEMail()
        {
            return $this->m_frmAdditionalEMail;
        }

        function SetDescriptions($descriptions)
        {
            $this->m_frmDescriptions = $descriptions;
        }
        function GetDescriptions()
        {
            return $this->m_frmDescriptions;
        }
        
        function SetTypes($types)
        {
            $this->m_frmTypes = $types;
        }
        function GetTypes()
        {
            return $this->m_frmTypes;
        }

        function SetValues($values)
        {
            $this->m_frmValues = $values;
        }
        function GetValues()
        {
            return $this->m_frmValues;
        }

        function SetSizes($sizes)
        {
            $this->m_frmSizes = $sizes;
        }
        function GetSizes()
        {
            return $this->m_frmSizes;
        }

        function SetLengths($lengths)
        {
            $this->m_frmLenghts = $lengths;
        }
        function GetLengths()
        {
            return $this->m_frmLenghts;
        }

        function SetMandatorys($mandatorys)
        {
            $this->m_frmMandatorys = $mandatorys;
        }
        function GetMandatorys()
        {
            return $this->m_frmMandatorys;
        }

        function SetMaxFields($count)
        {
            if ($count <= 0)
                $count = 1;
            unset($this->m_frmDescriptions);
            unset($this->m_frmTypes);
            unset($this->m_frmValues);
            unset($this->m_frmSizes);
            unset($this->m_frmLenghts);
            unset($this->m_frmMandatorys);
            $this->m_frmMaxFields = $count;
            $this->BuildArray();
        }
        function ResizeMaxFields($count)
        {
            if ($count <= 0)
                $count = 1;
            if ($count == $this->m_frmMaxFields)
                return;
            if ($count > $this->m_frmMaxFields)  // Enlarge
            {
                $this->m_frmMaxFields = $count;
                $this->BuildArray();
            }
            else  // Truncate
            {
                $newDescriptions = array();
                $newTypes = array();
                $newValues = array();
                $newSizes = array();
                $newLengths = array();
                $newMandatorys = array();
                for ($i = 0; $i < $count ; $i ++)
                {
                    $newDescriptions[] = $this->m_frmDescriptions[$i];
                    $newTypes[] = $this->m_frmTypes[$i];
                    $newValues[] = $this->m_frmValues[$i];
                    $newSizes[] = $this->m_frmSizes[$i];
                    $newLengths[] = $this->m_frmLenghts[$i];
                    $newMandatorys[] = $this->m_frmMandatorys[$i];
                }
                $this->m_frmDescriptions = $newDescriptions;
                $this->m_frmTypes = $newTypes;
                $this->m_frmValues = $newValues;
                $this->m_frmSizes = $newSizes;
                $this->m_frmLenghts = $newLengths;
                $this->m_frmMandatorys = $newMandatorys;
                $this->m_frmMaxFields = $count;
            }

        }
        function BuildArray()
        {
            for ($i = 0; $i < $this->m_frmMaxFields; $i ++)
            {
                if (isset($this->m_frmDescriptions[$i]) == false)
                    $this->m_frmDescriptions[$i] = "";
                if (isset($this->m_frmTypes[$i]) == false)
                    $this->m_frmTypes[$i] = "";
                if (isset($this->m_frmValues[$i]) == false)
                    $this->m_frmValues[$i] = "";
                if (isset($this->m_frmSizes[$i]) == false)
                    $this->m_frmSizes[$i] = "";
                if (isset($this->m_frmLenghts[$i]) == false)
                    $this->m_frmLenghts[$i] = "";
                if (isset($this->m_frmMandatorys[$i]) == false)
                    $this->m_frmMandatorys[$i] = "";
            }
        }
        function GetMaxFields()
        {
            return $this->m_frmMaxFields;
        }

        function IsLocked()
        {
            return ($this->m_frmLocked == "1");
        }

        function SetLocked($bLock)
        {
            $this->m_frmLocked = "0";
            if ($bLock == true)
                $this->m_frmLocked = "1";
            return $this->SaveForm();
        }

        function SetFolder($folder)
        {
            $this->m_frmFolder = $folder;
        }
        
        // Main methods
        function CreateForm()
        {
            $this->Init();
            $this->m_frmId = uniqid("frm", FALSE);
            $this->m_frmDate = date("Y-m-d H:i:s");
        }
        function CreateFormById($frmId)
        {
            $this->Init();
            $this->m_frmId = $frmId;
            $this->m_frmDate = date("Y-m-d H:i:s");
        }

        function DeleteFormFinal()
        {
            return unlink($this->m_frmFolder . $this->m_frmId . ".frm");
        }

        function DeleteFormFinalFromId($frmId)
        {
            $this->m_frmId = $frmId;
            return DeleteFormFinal();
        }


        function LoadFormFromId($frmId)
        {
            $this->m_frmId = $frmId;
            return $this->LoadForm();
        }

        function LoadForm()
        {
            $fileName = $this->m_frmFolder . $this->m_frmId  . ".frm";
            // echo  $fileName . '<br>';

            if ($this->m_frmId == "0" || $this->m_frmFolder == "")
                return false;


            $cStorage = new cStorageFile;
            if ($cStorage->LoadStorageFromFile($fileName) == false)
                return false;

            if ($cStorage->GetData("Id") == "")
                return false;

            $this->m_frmId = $cStorage->GetData("Id");
            $this->m_frmDate = $cStorage->GetData("Date");
            $this->m_frmDescriptions = explode("-|-", $cStorage->GetData("Descriptions"));
            $this->m_frmTypes = explode("-|-", $cStorage->GetData("Types"));
            $this->m_frmValues = explode("-|-", $cStorage->GetData("Values"));
            $this->m_frmSizes = explode("-|-", $cStorage->GetData("Sizes"));
            $this->m_frmLenghts = explode("-|-", $cStorage->GetData("Lengths"));
            $this->m_frmMandatorys = explode("-|-", $cStorage->GetData("Mandatorys"));
            $this->m_frmMaxFields = $cStorage->GetData("MaxFields");
            $this->m_frmAdditionalEMail = $cStorage->GetData("AdditionalEMail");
            $this->BuildArray();  // For old galleries, or updates
            $this->m_frmLocked = $cStorage->GetData("Locked");

            return true;

        }

        function SaveForm()
        {
            $newRow = "\r\n";
            $fileName = $this->m_frmFolder . $this->m_frmId  . ".frm";
            // echo $fileName;

            if ($this->m_frmId == "0" || $this->m_frmFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("form");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_frmId);
            $cStorage->AddData("Date", $this->m_frmDate);
            $cStorage->AddData("Descriptions", implode('-|-', $this->m_frmDescriptions));
            $cStorage->AddData("Types", implode('-|-', $this->m_frmTypes));
            $cStorage->AddData("Values", implode('-|-', $this->m_frmValues));
            $cStorage->AddData("Sizes", implode('-|-', $this->m_frmSizes));
            $cStorage->AddData("Lengths", implode('-|-', $this->m_frmLenghts));
            $cStorage->AddData("Mandatorys", implode('-|-', $this->m_frmMandatorys));
            $cStorage->AddData("MaxFields", $this->m_frmMaxFields);
            $cStorage->AddData("AdditionalEMail", $this->m_frmAdditionalEMail);
            $cStorage->AddData("Locked", $this->m_frmLocked, false);

            return $cStorage->SaveStorageToFile($fileName);
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}