<?php
$cms_root_path = "../../../";
require_once($cms_root_path."includes/compatibility_includer.php");
use Mtc\Cms\Models\PageListItemData as PageListItemData;
use Mtc\Cms\Models\PageList as PageList;

// check if regex pattern is valid
if ( !empty($_POST['settings']['regex']) ) {
    $match = @preg_match( $_POST['settings']['regex'] , "Hello World" );
    if ( $match === false ) {
       die("false");
    }
}

// update
if (!empty($_POST) && isset($_POST['page_list_id']) && is_numeric($_POST['page_list_id'])
    && !empty($_POST['page_list_item_data_name'])) {

    $page_list = PageList::find($_POST['page_list_id'], ["id", "allow_more"]);
    if (isset($_POST['settings']['allow_more'])) {
        $page_list->allow_more = $_POST['settings']['allow_more'];
        $page_list->save();
    }

    $all_page_list_item_data_list = PageListItemData::where("page_list_id", $_POST['page_list_id'])
        ->where("name", $_POST['page_list_item_data_name'])
        ->get();

    if ( empty($all_page_list_item_data_list) ) {
        die("false");
    }

    foreach ($all_page_list_item_data_list as $plid) {
        if ( isset($_POST['settings']['required']) ) {
            $plid->required = $_POST['settings']['required'];
        }
        if ( isset($_POST['settings']['regex']) ) {
            $plid->regex = $_POST['settings']['regex'];
        }
        if ( isset($_POST['settings']['mime']) ) {
            $plid->mime = $_POST['settings']['mime'];
        }
        if ( isset($_POST['settings']['data1']) ) {
            $plid->data1 = $_POST['settings']['data1'];
        }
        if ( isset($_POST['settings']['info']) ) {
            $plid->info = $_POST['settings']['info'];
        }
        if ( isset($_POST['settings']['settings']) ) {
            $plid->settings = $_POST['settings']['settings'];
        }
        if ( isset($_POST['settings']['data2']) ) {
            $plid->data2 = $_POST['settings']['data2'];
        }
        if ( isset($_POST['settings']['data3']) ) {
            $plid->data3 = $_POST['settings']['data3'];
        }
        if (isset($_POST['settings']['css_class'])) {
            $plid->css_class = $_POST['settings']['css_class'];
        }
        if (isset($_POST['settings']['options'])) {
            $plid->options = $_POST['settings']['options'];
        }
        if (isset($_POST['settings']['language'])) {
            $plid->language = $_POST['settings']['language'];
        }
        if (isset($_POST['settings']['setup'])) {
            $plid->setup = $_POST['settings']['setup'];
        }
        $plid->Save();
    }
    die("true");
}
die("false");
