<?php
defined( 'ABSPATH' ) or exit;   // show a blank page if try to access this file directly

require_once( EWZ_PLUGIN_DIR . "/classes/ewz-permission.php");
require_once( EWZ_PLUGIN_DIR . "/includes/ewz-common.php");
/** ***************************************************
 * static functions that run during activation/deactivation
 * uninstall is done in uninstall.php
 * ************************************************** */

class Ewz_Setup
{
    /***************************************************************************************/
    /* Some original installations defaulted to latin1 charset                             */
    /* WP now defaults to utf8                                                             */
    /* Storing utf8 characters in serialized data in such columns breaks the serializaton. */
    /***************************************************************************************/
    /**
     * @return void
     */
    public static function update_to_utf8(){
        global $wpdb;

        if( 'UTF-8' == get_option('blog_charset') ){
            $ewz_tables = array( EWZ_LAYOUT_TABLE, EWZ_FIELD_TABLE, EWZ_WEBFORM_TABLE, EWZ_ITEM_TABLE );
            foreach( $ewz_tables as $table ){
                $results = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table`" ); // no tainted data is ordered
                if ( ! $results ) {
                    return;
                }

                if ( $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) {  // no tainted data
                   $result = $wpdb->query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" ); // no tainted data

                }
            }
        }
    }

    /**
     * @return void
     */
    public static function activate_or_install_ewz()
    {
        global $wpdb;

        $reqvers = ewz_required_versions_warning();
        if( $reqvers ){
            deactivate_plugins( EWZ_PLUGIN_DIR . '/entrywizard.php' );
            die( "EntryWizard requires at least $reqvers" );
        }

        // Make sure all the global constants get set
        if( !defined( 'EWZ_LAYOUT_TABLE' ) ){
            ewz_init_globals();
        }

        $layout_table = EWZ_LAYOUT_TABLE;
        $field_table = EWZ_FIELD_TABLE;
        $webform_table = EWZ_WEBFORM_TABLE;
        $item_table = EWZ_ITEM_TABLE;

        // will just update if they already exist
        self::create_db_tables();

        // update the db version option
        update_option( 'ewz_data_version', EWZ_CURRENT_VERSION );

        $rowcount = $wpdb->get_var( "SELECT count(*) FROM $layout_table" );   // no tainted data

        if( !file_exists(  EWZ_IMG_UPLOAD_DIR ) ){
            mkdir( EWZ_IMG_UPLOAD_DIR );
            chmod( EWZ_IMG_UPLOAD_DIR, 0755 );
        }

        // return if there is data already in the tables
        if( $rowcount > 0 ){
            return;
        }

        // there is no existing data, create the sample stuff
        // set the ids so they aren't all the same, for easier debugging
        $wpdb->query("ALTER TABLE $layout_table AUTO_INCREMENT=33;");  // no tainted data
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }

        $wpdb->query("ALTER TABLE $field_table AUTO_INCREMENT=152;");  // no tainted data
        if( $wpdb->last_error ){  throw new EWZ_Exception("DB Error " . $wpdb->last_error ); } /** @phpstan-ignore-line */

        $wpdb->query("ALTER TABLE $webform_table AUTO_INCREMENT=287;");  // no tainted data
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $wpdb->query("ALTER TABLE $item_table AUTO_INCREMENT=413;");  // no tainted data
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */


        $maxnumitems1 = 4;
        $override1 = 0;
        $maxnumitems2 = 3;
        $override2 = 1;

        $ids1 = self::create_base_layout1($maxnumitems1, $override1);
        $ids2 = self::create_base_layout2($maxnumitems2, $override2);

        $webform_id1 = self::create_sample_webform( "Example Competition Form", "example", $ids1['layout_id'],
                                                    $ids1['max_num_items'] );
        $webform_id2 = self::create_sample_webform( "Image Pair Form", "pair", $ids2['layout_id'],
                                                    $ids2['max_num_items'] );

        self::create_sample_data1( EWZ_IMG_UPLOAD_DIR, $webform_id1, "example", $ids1 );
        self::create_sample_data2( EWZ_IMG_UPLOAD_DIR, $webform_id2, "pair", $ids2 );
        self::set_initial_permissions();
    }

    /** ***************************************************
     * Create the database tables, and insert two rows in the layout table
     * ************************************************** */
    /**
     * @return void
     */
    public static function create_db_tables()
    {
        global $wpdb;

        error_log( "EWZ: creating/updating tables " );
        $layout_table = EWZ_LAYOUT_TABLE;
        $field_table = EWZ_FIELD_TABLE;
        $webform_table = EWZ_WEBFORM_TABLE;
        $item_table = EWZ_ITEM_TABLE;

        //You must put each field on its own line in your SQL statement.
        //You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
        //You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
        //You must not use any apostrophes or backticks around field names.

        //NB: wordpress does not support table constraints (!)

        $create_layout_sql = "CREATE TABLE $layout_table (
                                           layout_id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
                                           layout_name char(60) NOT NULL UNIQUE,
                                           max_num_items smallint(5) UNSIGNED NOT NULL,
                                           layout_order smallint(5) UNSIGNED NOT NULL,
                                           override tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                           restrictions text NULL,
                                           extra_cols text NULL,
                                           notes text NULL,
                                         PRIMARY KEY  (layout_id)
                              ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";

        //NB: ss_column may be -1
        $create_field_sql = "CREATE TABLE $field_table (
                                              field_id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
                                              layout_id mediumint(8) UNSIGNED NOT NULL,
                                              field_type char(3) NOT NULL,
                                              field_header char(50) NOT NULL,
                                              field_ident char(15) NOT NULL,
                                              required tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                              pg_column smallint(5) UNSIGNED NOT NULL,
                                              ss_column smallint(6) NOT NULL,
                                              append tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                              all_items tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                              fdata text NOT NULL,
                                            PRIMARY KEY  (field_id),
                                            KEY ind_layout (layout_id)
                             ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";


        $create_webform_sql = "CREATE TABLE $webform_table (
                                                webform_id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
                                                layout_id mediumint(8) UNSIGNED NOT NULL,
                                                num_items smallint(5) UNSIGNED NOT NULL,
                                                webform_title varchar(100) NOT NULL UNIQUE,
                                                webform_ident char(15) NOT NULL UNIQUE,
                                                prefix char(75) NULL,
                                                apply_prefix tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                                gen_fname tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                                webform_order smallint(5) UNSIGNED NOT NULL,
                                                upload_open tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                                hidden tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
                                                open_for varchar(1000) NOT NULL,
                                                attach_prefs varchar(1000) NULL,
                                              PRIMARY KEY  (webform_id)
                               ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";


        $create_item_sql = "CREATE TABLE $item_table (
                                              item_id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                                              user_id bigint(20) UNSIGNED NOT NULL,
                                              webform_id mediumint(8) UNSIGNED NOT NULL,
                                              last_change datetime NOT NULL,
                                              upload_date datetime NOT NULL,
                                              item_files text NOT NULL,
                                              item_data text NOT NULL,
                                            PRIMARY KEY  (item_id),
                                            KEY ind_wform (webform_id),
                                            KEY ind_usr (user_id)
                               ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";


        // item_data and item_files are indexed by field_id for convenient access
        // item_data  == ( field_id => ('field_id'=> field_id, 'value' => value),
        //                   field_id => ('field_id'=> field_id, 'value' => value),
        //                   ....  )
        // item_files == ( field_id => ('field_id'=> field_id, 'fname'=> filename, 'thumb_url'=>thumburl, 'type'=>type),
        //                 field_id => ('field_id'=> field_id, 'fname'=> filename, 'url'=>thumburl, 'type'=>type),
        //                    .... )

        // The dbDelta function examines the current table structure, compares it to the desired table structure,
        // and either adds or modifies the table as necessary, so it can be very handy for updates
        // (see wp-admin/upgrade-schema.php for more examples of how to use dbDelta).
        // Note that the dbDelta function is rather picky, however. For instance:
        // You have to put each field on its own line in your SQL statement.
        // You have to have two spaces between the words PRIMARY KEY and the definition of your primary key.
        // You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.

        // need this for the dbDelta function, not automatically loaded
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        // create the tables if they don't already exist

            $db_res = dbDelta( $create_layout_sql );
            if( $err = $wpdb->last_error ){
                error_log('EWZ: DB Layout error: '. print_r($err, true));
            }
            if( $db_res ){
                error_log('EWZ: '. print_r($db_res, true));
            }

            $db_res = dbDelta( $create_field_sql );
            if( $err = $wpdb->last_error ){
                error_log('EWZ: DB error: '. print_r($err, true));
            }
            if( $db_res ){
               error_log('EWZ: '. print_r($db_res, true));
            }

            $db_res = dbDelta( $create_webform_sql );
            if( $err = $wpdb->last_error ){
                error_log('EWZ: DB error: '. print_r($err, true));
            }
            if( $db_res ){
               error_log('EWZ: '. print_r($db_res, true));
            }

            $db_res = dbDelta( $create_item_sql );
            if( $err = $wpdb->last_error ){
                error_log('EWZ: DB error: '. print_r($err, true));
            }
            if( $db_res ){
               error_log('EWZ: '. print_r($db_res, true));
            }
    }


    /* ***************************************************
     * Drop our database tables and remove any entries we created in WP tables
     * ************************************************** */


    /*   * **************************************************
     * Create two sample layouts to act as the starting point
     * ************************************************** */
    /**
     * @param  int $maxnumitems
     * @param  int $override
     * @return array<string,string|int>
     */
    public static function create_base_layout1($maxnumitems, $override)
    {

        global $wpdb;
        $layout_table = EWZ_LAYOUT_TABLE;
        $field_table = EWZ_FIELD_TABLE;

        $type_field1 = array(
                             'field_type' => 'opt',
                             'field_header' => 'Type',
                             'field_ident' => 'Tp',
                             'required' => 1,
                             'pg_column' => 0,
                             'ss_column' => 3,
                             'append' => 0,
                             'fdata' => array(
                                              'options' => array(
                                                                 array( 'value' => 'P',
                                                                        'label' => 'Print',
                                                                        'maxnum' => 0,
                                                                        ),
                                                                 array('value' => 'D',
                                                                       'label' => 'Digital',
                                                                       'maxnum' => 0,
                                                                       ),
                                                                 array('value' => 'S',
                                                                       'label' => 'Slide',
                                                                       'maxnum' => 0,
                                                                       ),
                                                                 ),
                                              ),
                             );

        $cat_field1 = array(
                            'field_type' => 'opt',
                            'field_header' => 'Category',
                            'field_ident' => 'Cat',
                            'required' => 0,
                            'pg_column' => 1,
                            'ss_column' => 4,
                            'append' => 1,
                            'fdata' => array('options' => array(array('value' => 'A',
                                                                      'label' => 'Assigned Topic',
                                                                      'maxnum' => 2,
                                                                      ),
                                                                array('value' => 'P',
                                                                      'label' => 'Pictorial',
                                                                      'maxnum' => 2,
                                                                      ),
                                                                ),
                                             ),
                            );

        $title_field1 = array(
                              'field_type' => 'str',
                              'field_header' => 'Title',
                              'field_ident' => 'Title',
                              'required' => 1,
                              'pg_column' => 2,
                              'ss_column' => 5,
                              'append' => 0,
                              'fdata' => array(
                                               'fieldwidth' => 20,
                                               'maxstringchars' => 50,
                                               'ss_col_fmt' => -1,
                                               ),
                              );

        $img_field1 = array(
                            'field_type' => 'img',
                            'field_header' => 'Image File',
                            'field_ident' => 'Filename',
                            'required' => 0,
                            'pg_column' => 3,
                            'ss_column' => 0,
                            'append' => 0,
                            'fdata' => array(
                                             'max_img_w' => 1280,
                                             'ss_col_w'  => 14,
                                             'max_img_h' => 1024,
                                             'ss_col_h'  => 15,
                                             'canrotate' => true,
                                             'mustfill' => false,
                                             'ss_col_o'  => 16,
                                             'max_img_size' => 1,
                                             'min_longest_dim' => 800,
                                             'allowed_image_types' => array('image/jpeg',
                                                                            'image/png',
                                                                            'image/gif',
                                                                            ),
                                             ),
                            );



        $rows_affected = $wpdb->insert( $layout_table,                                         // no esc
                                        array( 'layout_name' => "Sample Competition Layout",
                                               'max_num_items' => $maxnumitems,
                                               'override' => $override,
                                               'restrictions' => '',
                                               'extra_cols' => '',
                                               'layout_order' => 0,
                                               ),
                                        array('%s', '%s', '%d', '%s')
                                        );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }
        // assert($rows_affected === 1);
        $layout_id1 = $wpdb->insert_id;

        $rows_affected1 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id1,
                                              'field_type' => $type_field1['field_type'],
                                              'field_header' => $type_field1['field_header'],
                                              'field_ident' => $type_field1['field_ident'],
                                              'required' => $type_field1['required'],
                                              'pg_column' => $type_field1['pg_column'],
                                              'ss_column' => $type_field1['ss_column'],
                                              'append' => $type_field1['append'],
                                              'fdata' => serialize( $type_field1['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id1 = $wpdb->insert_id;
        // assert($rows_affected1 === 1);

        $rows_affected2 = $wpdb->insert( $field_table,                                         // no esc
                                         array('layout_id' => $layout_id1,
                                               'field_type' => $cat_field1['field_type'],
                                               'field_header' => $cat_field1['field_header'],
                                               'field_ident' => $cat_field1['field_ident'],
                                               'required' => $cat_field1['required'],
                                               'pg_column' => $cat_field1['pg_column'],
                                               'ss_column' => $cat_field1['ss_column'],
                                               'append' => $cat_field1['append'],
                                               'fdata' => serialize( $cat_field1['fdata'] )
                                               ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id2 = $wpdb->insert_id;
        // assert($rows_affected2 === 1);

        $rows_affected3 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id1,
                                              'field_type' => $title_field1['field_type'],
                                              'field_header' => $title_field1['field_header'],
                                              'field_ident' => $title_field1['field_ident'],
                                              'required' => $title_field1['required'],
                                              'pg_column' => $title_field1['pg_column'],
                                              'ss_column' => $title_field1['ss_column'],
                                              'append' => $title_field1['append'],
                                              'fdata' => serialize( $title_field1['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id3 = $wpdb->insert_id;
        // assert($rows_affected3 === 1);


        $rows_affected4 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id1,
                                              'field_type' => $img_field1['field_type'],
                                              'field_header' => $img_field1['field_header'],
                                              'field_ident' => $img_field1['field_ident'],
                                              'required' => $img_field1['required'],
                                              'pg_column' => $img_field1['pg_column'],
                                              'ss_column' => $img_field1['ss_column'],
                                              'append' => $img_field1['append'],
                                              'fdata' => serialize( $img_field1['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id4 = $wpdb->insert_id;
        // assert($rows_affected4 === 1);


        $restr_data = array(
                            array( $field_id1 => 'D',
                                   $field_id2 => '~*~',
                                   $field_id3 => '~*~',
                                   $field_id4 => '~-~',
                                   'msg' => 'A Digital type must have an Image File'
                                   ));
        $xtra_data = array( "mnm" => 1,
                            "mem" => 2,
                            "dtu" => 10,
                            "iid" => 9,
                            "mid" => -1,
                            "mli" => 7,
                            );
        $wpdb->update( $layout_table,                                         // no esc
                       array('restrictions' => serialize( $restr_data ),
                             'extra_cols' => serialize( $xtra_data )
                             ),
                       array('layout_id' => $layout_id1), array('%s')
                       );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */


        return array( 'field_id1'=>$field_id1,
                      'field_id2'=>$field_id2,
                      'field_id3'=>$field_id3,
                      'field_id4'=>$field_id4,
                      'layout_id'=>$layout_id1,
                      'max_num_items'=>$maxnumitems,
                      'override'=>$override,
                      );
    }

    /**
     * @param  int $maxnumitems
     * @param  int $override
     * @return array<string,string|int>
     */
    public static function create_base_layout2($maxnumitems, $override)
    {
        global $wpdb;
        // assert( is_int($maxnumitems) );
        // assert( is_int($override) );
        $layout_table = EWZ_LAYOUT_TABLE;
        $field_table = EWZ_FIELD_TABLE;

        // Set up the fields

        $div_field2 = array(
                            'field_type'   => 'opt',
                            'field_header' => 'Division',
                            'field_ident'  => 'Division',
                            'required'     => 1,
                            'pg_column'    => 0,
                            'ss_column'    => 5,
                            'append'       => 0,
                            'fdata'        => array( 'options' => array( array('value' => 'N',
                                                                               'label' => 'Nature',
                                                                               'maxnum' => 2,
                                                                               ),
                                                                         array('value' => 'P',
                                                                               'label' => 'Portrait',
                                                                               'maxnum' => 2,
                                                                               ),
                                                                         array('value' => 'S',
                                                                               'label' => 'Still Life',
                                                                               'maxnum' => 2,
                                                                               ),
                                                                         ),
                                                     ),
                            );
        $cb_field2 = array(
                            'field_type'   => 'chk',
                            'field_header' => 'Comments',
                            'field_ident'  => 'comments',
                            'required'     => 0,
                            'pg_column'    => 1,
                            'ss_column'    => 4,
                            'append'       => 0,
                            'fdata'        => array( 'chkmax' => 2 ),
                           );

        $title_field2 = array(
                              'field_type' => 'str',
                              'field_header' => 'Caption',
                              'field_ident' => 'caption',
                              'required' => 0,
                              'pg_column' => 2,
                              'ss_column' => 3,
                              'append'     => 0,
                              'fdata' => array(
                                               'fieldwidth' => 20,
                                               'maxstringchars' => 50,
                                               'ss_col_fmt' => -1,
                                               ),
                              );

        $rad_field2 = array(
                            'field_type'   => 'rad',
                            'field_header' => 'Monochrome',
                            'field_ident'  => 'mono',
                            'required'     => 0,
                            'pg_column'    => 3,
                            'ss_column'    => 2,
                            'append'       => 0,
                            'fdata'        => array( ),
                           );
        $orig_field2 = array(
                             'field_type' => 'img',
                             'field_header' => 'Original Image',
                             'field_ident' => 'original',
                             'required' => 0,
                             'pg_column' => 4,
                             'ss_column' => 1,
                             'append'       => 0,
                             'fdata' => array(
                                              'max_img_w' => 1280,
                                              'ss_col_w'  => -1,
                                              'max_img_h' => 1024,
                                              'ss_col_h'  => -1,
                                              'canrotate' => false,
                                              'mustfill' => false,
                                              'ss_col_o'  => -1,
                                              'max_img_size' => 1,
                                              'min_longest_dim' => 800,
                                              'allowed_image_types' => array('image/jpeg',
                                                                             ),
                                              ),
                             );
        $final_field2 = array(
                              'field_type' => 'img',
                              'field_header' => 'Final Image',
                              'field_ident' => 'final',
                              'required' => 1,
                              'pg_column' => 5,
                              'ss_column' => 0,
                              'append'       => 0,
                              'fdata' => array(
                                               'max_img_w' => 1280,
                                               'ss_col_w'  => 14,
                                               'max_img_h' => 1024,
                                               'ss_col_h'  => 15,
                                               'canrotate' => false,
                                               'mustfill' => false,
                                               'ss_col_o'  => 16,
                                               'max_img_size' => 1,
                                               'min_longest_dim' => 800,
                                               'allowed_image_types' => array('image/jpeg',
                                                                              ),
                                               ),
                              );

        // create the layout
        $rows_affected = $wpdb->insert( $layout_table,                                         // no esc
                                        array( 'layout_name' => "Image Pair Layout",
                                               'max_num_items' => $maxnumitems,
                                               'layout_order' => 1,
                                               )
                                        );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }

        $layout_id2 = $wpdb->insert_id;
        // assert( $rows_affected === 1 );


        // insert the fields
        $rows_affected0 = $wpdb->insert( $field_table,                                         // no esc
                                        array( 'layout_id' => $layout_id2,
                                               'field_type' => $div_field2['field_type'],
                                               'field_header' => $div_field2['field_header'],
                                               'field_ident' => $div_field2['field_ident'],
                                               'required' => $div_field2['required'],
                                               'pg_column' => $div_field2['pg_column'],
                                               'ss_column' => $div_field2['ss_column'],
                                               'append' => $div_field2['append'],
                                               'fdata' => serialize( $div_field2['fdata'] )
                                               ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id0 = $wpdb->insert_id;
        // assert( $rows_affected0 === 1 );

        $rows_affected1 = $wpdb->insert( $field_table,                                         // no esc
                                        array( 'layout_id' => $layout_id2,
                                               'field_type' => $cb_field2['field_type'],
                                               'field_header' => $cb_field2['field_header'],
                                               'field_ident' => $cb_field2['field_ident'],
                                               'required' => $cb_field2['required'],
                                               'pg_column' => $cb_field2['pg_column'],
                                               'ss_column' => $cb_field2['ss_column'],
                                               'append' => $cb_field2['append'],
                                               'fdata' => serialize( $cb_field2['fdata'] )
                                               ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id1 = $wpdb->insert_id;
        // assert( $rows_affected1 === 1 );


        $rows_affected2 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id2,
                                              'field_type' => $title_field2['field_type'],
                                              'field_header' => $title_field2['field_header'],
                                              'field_ident' => $title_field2['field_ident'],
                                              'required' => $title_field2['required'],
                                              'pg_column' => $title_field2['pg_column'],
                                              'ss_column' => $title_field2['ss_column'],
                                               'append' => $title_field2['append'],
                                              'fdata' => serialize( $title_field2['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id2 = $wpdb->insert_id;
        // assert( $rows_affected2 === 1 );

        $rows_affected3 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id2,
                                              'field_type' => $rad_field2['field_type'],
                                              'field_header' => $rad_field2['field_header'],
                                              'field_ident' => $rad_field2['field_ident'],
                                              'required' => $rad_field2['required'],
                                              'pg_column' => $rad_field2['pg_column'],
                                              'ss_column' => $rad_field2['ss_column'],
                                               'append' => $rad_field2['append'],
                                              'fdata' => serialize( $rad_field2['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */

        $field_id3 = $wpdb->insert_id;
        // assert( $rows_affected3 === 1 );


        $rows_affected4 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id2,
                                              'field_type' => $orig_field2['field_type'],
                                              'field_header' => $orig_field2['field_header'],
                                              'field_ident' => $orig_field2['field_ident'],
                                              'required' => $orig_field2['required'],
                                              'pg_column' => $orig_field2['pg_column'],
                                              'ss_column' => $orig_field2['ss_column'],
                                               'append' => $orig_field2['append'],
                                              'fdata' => serialize( $orig_field2['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */


        $field_id4 = $wpdb->insert_id;
        // assert( $rows_affected4 === 1 );

        $rows_affected5 = $wpdb->insert( $field_table,                                         // no esc
                                        array('layout_id' => $layout_id2,
                                              'field_type' => $final_field2['field_type'],
                                              'field_header' => $final_field2['field_header'],
                                              'field_ident' => $final_field2['field_ident'],
                                              'required' => $final_field2['required'],
                                              'pg_column' => $final_field2['pg_column'],
                                              'ss_column' => $final_field2['ss_column'],
                                               'append' => $final_field2['append'],
                                              'fdata' => serialize( $final_field2['fdata'] )
                                              ) );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */


        $field_id5 = $wpdb->insert_id;
        // assert( $rows_affected5 === 1 );

        $restr_data = array();

        $extra_data = array(
                            "dtu" => 11,   //~~
                            "iid" => 8, //~~
                            "mem" => 12, //~~
                            "mid" => -1,//~~
                            "mli" => 13, //~~
                            "mnm" => 10, //~~
                            "wfm" => -1,//~~
                            "wft" => 9,//~~
                            "wid" => -1,//~~
                            "custom1" => 6,//~~
                            );

        $wpdb->update( $layout_table,                                         // no esc
                       array('restrictions' => serialize( $restr_data ),
                             'extra_cols' => serialize( $extra_data )
                             ),
                       array('layout_id' => $layout_id2), array('%s')
                       );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }/** @phpstan-ignore-line */


        return array('field_id0'=>$field_id0,
                     'field_id1'=>$field_id1,
                      'field_id2'=>$field_id2,
                      'field_id3'=>$field_id3,
                      'field_id4'=>$field_id4,
                      'field_id5'=>$field_id5,
                      'layout_id'=>$layout_id2,
                      'max_num_items'=>$maxnumitems,
                      'override'=>$override,
                      );
    }

    /**
     * @param  string $webform_title
     * @param  string $webform_ident
     * @param  int $layout_id
     * @param  int $num_items
     * @return int
     */
    public static function create_sample_webform( $webform_title, $webform_ident, $layout_id, $num_items )
    {
        global $wpdb;
        $webform_table = EWZ_WEBFORM_TABLE;
        $rows_affected = $wpdb->insert( $webform_table,                                         // no esc
                                        array('layout_id' => $layout_id,
                                              'num_items' => $num_items,
                                              'webform_title' => $webform_title,
                                              'webform_ident' => $webform_ident,
                                              'webform_order' => 0,
                                              'upload_open' => 1,
                                              'apply_prefix' => 1,
                                              'gen_fname' => 0,
                                              'open_for' => "a:0:{}",
                                              )
                                        );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }

        $webform_id = $wpdb->insert_id;
        // assert( $rows_affected === 1 );

        return $webform_id;
    }

    /**
     * @param  string $dir
     * @param  int    $webform_id
     * @param  string $webform_ident
     * @param  array<string,string|int>  $ids
     * @return void
     */
    public static function create_sample_data1(  $dir, $webform_id, $webform_ident, $ids ){
        global $wpdb;

        $item_table = EWZ_ITEM_TABLE;
        $path =  $webform_ident;
        $url =   $webform_ident;


        $files = array( $ids['field_id4'] => array( "field_id"  => $ids['field_id4'],
                                      "thumb_url" => "{$url}/sample2-thumb.jpg",
                                      "fname"     => "{$path}/sample2.jpg",
                                      "type"      => "image/jpg",
                                      "width"     => 1280,
                                      "height"    => 960,
                                      "orient"    => "L",
                                      ),
                        );
        $data = array(
                       $ids['field_id1'] => array( "field_id" => $ids['field_id1'],
                                     "value"    => "P"
                                     ),
                       $ids['field_id2'] => array( "field_id" => $ids['field_id2'],
                                     "value"    => "A"
                                      ),
                       $ids['field_id3'] => array( "field_id" => $ids['field_id3'],
                                     "value"    => "my beautiful image"
                                     ),
                       $ids['field_id4'] => array( "field_id" => $ids['field_id4'],
                                     "value"    => "ewz_img_upload"
                                     ),
                      );
        $user_id = get_current_user_id();

        $rows_affected = $wpdb->insert( $item_table,                                         // no esc
                                         array( 'user_id'     => $user_id,
                                                'webform_id'  => $webform_id,
                                                'last_change' => current_time( 'mysql' ),
                                                'upload_date' => current_time( 'mysql' ),
                                                'item_files'  => serialize( $files ),
                                                'item_data'   => serialize( $data )
                                                          )
                                        );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }

        // assert( $rows_affected === 1 );

        $dir =  EWZ_IMG_UPLOAD_DIR . '/' . $webform_ident;
        if( !file_exists( $dir ) ){
            mkdir( $dir );
            chmod( $dir, 0755 );
        }
        copy( dirname( __FILE__ ) . '/../images/sample2-thumb.jpg', "{$dir}/sample2-thumb.jpg" );
        copy( dirname( __FILE__ ). '/../images/sample2.jpg', "{$dir}/sample2.jpg" );
   }

    /**
     * @param  string $dir
     * @param  int    $webform_id
     * @param  string $webform_ident
     * @param  array<string,string|int> $ids
     * @return void
     */
    public static function create_sample_data2(  $dir, $webform_id, $webform_ident, $ids ){
        global $wpdb;

        $item_table = EWZ_ITEM_TABLE;
        $path =  $webform_ident;
        $url =   $webform_ident;


        $files = array( $ids['field_id4'] => array( "field_id"  => $ids['field_id4'],
                                      "thumb_url" => "{$url}/sample1-thumb.jpg",
                                      "fname"     => "{$path}/sample1.jpg",
                                      "type"      => "image/jpg",
                                      "width"     => 677,
                                      "height"    => 1024,
                                      "orient"    => "P",
                                      ),
                        $ids['field_id5'] => array( "field_id"  => $ids['field_id5'],
                                      "thumb_url" => "{$url}/sample3-thumb.jpg",
                                      "fname"     => "{$path}/sample3.jpg",
                                      "type"      => "image/jpg",
                                      "width"     => 677,
                                      "height"    => 1024,
                                      "orient"    => "P",
                                      ),

                        );
        $data = array(
                       $ids['field_id0'] => array( "field_id" => $ids['field_id0'],
                                     "value"    => "N"
                                     ),
                       $ids['field_id1'] => array( "field_id" => $ids['field_id1'],
                                     "value"    => false
                                     ),
                       $ids['field_id2'] => array( "field_id" => $ids['field_id2'],
                                     "value"    => "My Image"
                                      ),
                       $ids['field_id3'] => array( "field_id" => $ids['field_id3'],
                                     "value"    => true
                                     ),
                       $ids['field_id4'] => array( "field_id" => $ids['field_id4'],
                                     "value"    => "ewz_img_upload"
                                     ),
                       $ids['field_id5'] => array( "field_id" => $ids['field_id5'],
                                     "value"    => "ewz_img_upload"
                                     ),
                      );
        $user_id = get_current_user_id();


        $rows_affected = $wpdb->insert( $item_table,                                         // no esc
                                         array( 'user_id'     => $user_id,
                                                'webform_id'  => $webform_id,
                                                'last_change' => current_time( 'mysql' ),
                                                'upload_date' => current_time( 'mysql' ),
                                                'item_files'  => serialize( $files ),
                                                'item_data'   => serialize( $data )
                                                          )
                                        );
        if( $wpdb->last_error ){ throw new EWZ_Exception("DB Error " . $wpdb->last_error ); }

        // assert( $rows_affected === 1 );

        $dir =  EWZ_IMG_UPLOAD_DIR . '/' . $webform_ident;
        if( !file_exists( $dir ) ){
            mkdir( $dir );
            chmod( $dir, 0755 );
        }

        copy( dirname( __FILE__ ) . '/../images/sample1-thumb.jpg', "{$dir}/sample1-thumb.jpg" );
        copy( dirname( __FILE__ ) . '/../images/sample1.jpg', "{$dir}/sample1.jpg" );
        copy( dirname( __FILE__ ) . '/../images/sample3-thumb.jpg', "{$dir}/sample3-thumb.jpg" );
        copy( dirname( __FILE__ ). '/../images/sample3.jpg', "{$dir}/sample3.jpg" );


    }

    /**
     * @return void
     */
    public static function protect_uploads()
    {
        // If it does not already exist, create the ewz_img_upload folder,
        // and create .htaccess and index.php files in it
        if( !file_exists( EWZ_IMG_UPLOAD_DIR ) ){
            mkdir( EWZ_IMG_UPLOAD_DIR );
            chmod ( EWZ_IMG_UPLOAD_DIR, 0755 );
        }
        $f2 =  EWZ_IMG_UPLOAD_DIR . '/.htaccess' ;
        if( !file_exists( $f2 ) ){
            $outp = fopen( $f2, 'w' );
            fwrite( $outp, "Options -Indexes" );
            fclose( $outp );
            chmod ( $f2, 0644 );
        }
        // belt-and-braces
        if( $dh = opendir( EWZ_IMG_UPLOAD_DIR ) ){
            while (($file = readdir($dh)) !== false) {
                if( is_dir( EWZ_IMG_UPLOAD_DIR . "/$file" ) ){
                    $f1 =  EWZ_IMG_UPLOAD_DIR . "/{$file}/index.php" ;
                    if( !file_exists( $f1 ) ){
                        $outp = fopen( $f1, 'w' );
                        fwrite( $outp, "<?php\n   //No listing\n?>\n" );
                        fclose( $outp );
                        chmod( $f1, 0644 );
                    }
                }
            }
            closedir($dh);
        }
    }

    /**
     * @return void
     */
    private static function set_initial_permissions()
    {
        $user = wp_get_current_user();

        Ewz_Permission::add_perm( $user->ID, 'ewz_can_edit_layout', array( "-1" ) );
        Ewz_Permission::add_perm( $user->ID, 'ewz_can_assign_layout', array( "-1" ) );
        Ewz_Permission::add_perm( $user->ID, 'ewz_can_edit_webform', array( "-1" ) );
        Ewz_Permission::add_perm( $user->ID, 'ewz_can_download_webform', array( "-1" ) );

        self::protect_uploads();
    }

    /**
     * @param  string $dir
     * @return void
     */
    private static function rrmdir( $dir ) {
        // assert( is_string( $dir ) );

      error_log( "EWZ: removing directory $dir" );
      if ( is_dir( $dir ) ) {
         $objects = scandir( $dir );
         foreach ( $objects as $object ) {
            if ( $object != "." && $object != ".." ) {
               if ( filetype( $dir . "/" . $object ) == "dir" ) {
                  self::rrmdir( $dir . "/" . $object );
               } else {
                  unlink( $dir . "/" . $object );
               }
            }
         }
         reset( $objects );
         rmdir( $dir );
      }
   }
}
