HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/form/form.class.php
<?php
/**
 * m_form.php
 * Created by Komma Mediadesign.
 * Author: mike
 * Date: 3/27/13
 */
class Form
{
    /*
     * Form attributes
     */
    private $_formAttr;

    /*
     * Add / edit form
     */
    private $_type;

    /*
     * Contains the fields
     */
    private $_fields = [];

    /*
     * Contains the fields
     */
    private $_buttons = [];

    /*
     * Contains a string with blockOutput
     */
    private $_blockOutput = [];

    /*
     * Construct
     */
    public function __construct($name)
    {
        $this->_formAttr['name'] = $name;
        $this->_id = $this->_formAttr['name'];
    }

    /*
     * Get form configuration
     */
    public function get()
    {
        return $this->_formAttr;
    }

    /*
     * Adds a field to the form
     * @param Field object
     */
    public function addField($Field)
    {
        $this->_fields[] = $Field;
    }

    /*
     * Adds a block to the form
     * @param string
     */
    public function addBlocks($string)
    {
        $this->_blockOutput = $string;
    }

    /*
     * Adds a button to the form
     * @param Button object
     */
    public function addButton($Button)
    {
        $this->_buttons[] = $Button;
    }

    /*
     * Set attributes
     */
    public function setAttr($attr, $value)
    {
        $this->_formAttr[$attr] = $value;
    }

    /*
     * Set form type to add or edit
     */
    public function setType($type)
    {
        $this->_type = $type;
    }

    /*
     * Render the form
     */
    public function render()
    {
        // Start form with specified attributes
        $form = '<form method="post"';
        foreach ($this->_formAttr as $attr => $value) {
            if (isset($value) && ! empty($value)) {
                $form .= ' '.$attr.'="'.$value.'"';
            }
        }
        $form .= '>';

        if (! empty($this->_blockOutput)) {
            $form .= $this->_blockOutput;
        }

        // Add some buttons to the form
        if (! empty($this->_buttons)) {
            foreach ($this->_buttons as $Button) {
                $Button->addClasses(['blue']);
                $form .= $Button->display(false);
            }
        }
        $form .= '<input type="hidden" name="form_type" value="'.$this->_type.'" />';
        if ($this->_type == 'edit') {
            // Id to edit
            defined('URL_SUB2') ? $id = URL_SUB2 : $id = 0;
            $form .= '<input type="hidden" name="edit_id" value="'.$id.'" />';
        }
        $form .= '</form>';

        return $form;
    }

    /*
     * Validate this form
     */
    public function validate()
    {
        if (Session::get($this->_formAttr['name'].'_errors')) {
            Session::destroy($this->_formAttr['name'].'_errors');
        }
        $valid = true;
        foreach ($this->_fields as $Field) {
            $data = $Field->get();
            if (! $Field->validate()) {
                Session::set([$this->_formAttr['name'].'_errors', $data['name']], $Field->getError());
                $valid = false;
            }
        }

        return $valid;
    }

    /*
     * Reset $this->_fields to an empty array
     */
    public function clearFields()
    {
        $this->_fields = [];
    }

    /*
     * Prepare values for the database
     */
    public function prepare()
    {
        // Data to store
        $data = [];

        // Get values
        foreach ($this->_fields as $Field) {
            $fieldData = $Field->get();
            $fieldName = $fieldData['name'];

            $key = [$fieldData['form_name'].'_data', $fieldData['name']];
            $value = Session::get($key);

            // Check if value is equal to label
            if (strip_tags($value) == $fieldData['label']) {
                $value = null;
            }

            // Check if to prepare values
            if (isset($fieldData['object'])) {
                $object = $fieldData['object'];

                $file = DOCUMENT_ROOT.'lib/form/types/'.$object.'.class.php';
                if (is_file($file)) {
                    if (! class_exists($object)) {
                        require $file;
                    }
                    $TypeObject = new $object;
                    if (method_exists($object, 'prepareToDb')) {
                        $value = $TypeObject->prepareToDb($value);
                    }
                }
            }

            /* extension CSB */
            if ($fieldData['form_name'] == 'projects' && $fieldName == 'services') {
                // do nothing..
            } else {
                // Add value to data array
                $data[$fieldName] = $value;
            }
        }

        // add a timestamp to the data
        if (! isset($data['timest']) && $this->_type == 'add') {
            $data['timest'] = time();
        }

        return $data;
    }

    /*
     * Prepare values for the database
     */
    public function fillStoredData($id)
    {
        $Dbh = new DatabaseHandler();
        $Dbh->setTableName(TABLE_PREFIX.$this->_formAttr['name'].'_items');
        $Dbh->addRule('id', $id);
        $result = $Dbh->select();

        // Get values for each field
        foreach ($this->_fields as $Field) {
            $fieldData = $Field->get();
            $fieldName = $fieldData['name'];
            if (isset($result[$fieldName])) {
                $value = $result[$fieldName];

                // prepare value from database?
                if (isset($fieldData['object'])) {
                    $object = $fieldData['object'];

                    $file = DOCUMENT_ROOT.'lib/form/types/'.$object.'.class.php';
                    if (is_file($file)) {
                        if (! class_exists($object)) {
                            require $file;
                        }
                        $TypeObject = new $object;

                        if (method_exists($object, 'prepareFromDb')) {
                            $value = $TypeObject->prepareFromDb($value);
                        }
                    }
                }
                $sessionKey = [$this->_formAttr['name'].'_data', $fieldName];
                Session::set($sessionKey, $value);
            }
            $Field->defaultValue();

            // Get Thumbnail
            if (isset($result['thumb'])) {
                $sessionKey = [$this->_formAttr['name'].'_data', 'thumb'];
                Session::set($sessionKey, $result['thumb']);
            }

            if (isset($fieldData['object'])) {
                if ($fieldData['object'] == 'Field_Type_Blog_Image' || $fieldData['object'] == 'Field_Type_Blog_Single_Image') {
                    // Fill images
                    $FileHandler = new File_Handler();
                    $FileHandler->set('_name', $this->_formAttr['name'].'_images');
                    $FileHandler->set('_tableName', TABLE_PREFIX.$this->_formAttr['name'].'_images');
                    $FileHandler->fillSession();
                }

                if ($fieldData['object'] == 'Field_Type_Blog_Video') {
                    // Fill videos
                    $VideoHandler = new Video_Handler();
                    $VideoHandler->set('_form_name', $this->_formAttr['name']);
                    $VideoHandler->fillSession();
                }
            }
        }

        /* CSB Extension */

        // Get URL SUB
        $LanguageHandler = new LanguageHandler();
        $LanguageHandler->set(URL_LANG);
        defined('URL_PAGE') ? $page = $LanguageHandler->convert(URL_PAGE) : $page = '';

        if ($page == 'projects' && defined('URL_SUB2')) {
            $sessionServices = [];

            $itemId = URL_SUB2;
            $Dbh = new DatabaseHandler();
            $Dbh->setTableName(TABLE_PREFIX.'projects_services');
            $Dbh->setData(['service_id'=>'']);
            $Dbh->addRule('project_id', $itemId);
            $itemsInDB = $Dbh->select();
            $itemsInDB = $Dbh->twoDimensional($itemsInDB);
            foreach ($itemsInDB as $item) {
                $sessionServices[] = $item['service_id'];
            }

            Session::set(['projects_data', 'services'], $sessionServices);
        }
    }
}