<?php

/**
 * Roles Wiaas DB updates
 *
 * Functions for updating wiaas roles in database
 *
 */



function wiaas_db_update_create_default_roles() {
    $roles = array(
        'customer', // this role will be created by woocommerce so we do not have to create it here
        'supplier',
        'commercial_lead'
    );

    // remove wordpress default roles
    remove_role('author');
    remove_role('contributor');
    remove_role('editor');
    remove_role('subscriber');

    // remove woocoommerce shop manager role
    remove_role('shop_manager');

    // Dummy gettext calls to get strings in the catalog.
    /* translators: user role */
    _x( 'Supplier', 'User role', 'wiaas' );
    /* translators: user role */
    _x( 'Commercial Lead', 'User role', 'wiaas' );

    // Add wiaas roles
    add_role(
        'commercial_lead',
        array(
            'read' => true,
        )
    );

    add_role(
        'supplier',
        array(
            'read' => true,
        )
    );

    add_role(
        'user',
        array(
            'read'  => true
        )
    );

    // set default wiaas role
    update_option('default_role', 'user');

    // capabilities

    $capabilities = array();

    $capability_types = array( 'wiaas_doc' );

    foreach ( $capability_types as $capability_type ) {

        $capabilities[ $capability_type ] = array(
            // Post type.
            "edit_{$capability_type}",
            "read_{$capability_type}",
            "delete_{$capability_type}",
            "edit_{$capability_type}s",
            "edit_others_{$capability_type}s",
            "publish_{$capability_type}s",
            "read_private_{$capability_type}s",
            "delete_{$capability_type}s",
            "delete_private_{$capability_type}s",
            "delete_published_{$capability_type}s",
            "delete_others_{$capability_type}s",
            "edit_private_{$capability_type}s",
            "edit_published_{$capability_type}s",

            // Terms.
            "manage_{$capability_type}_terms",
            "edit_{$capability_type}_terms",
            "delete_{$capability_type}_terms",
            "assign_{$capability_type}_terms",
        );
    }

    foreach ( $capabilities as $cap_group ) {
	    foreach ( $cap_group as $cap ) {
		    wp_roles()->add_cap( 'administrator', $cap );
		    wp_roles()->add_cap( 'commercial_lead', $cap );
	    }
    }
}


function wiaas_db_import_aam_role_settings() {
    // update role settings that are imported
    $content = file_get_contents( dirname( __FILE__ ) . '/data/aam-settings.json' );

    $importer = new AAM_Core_Importer($content);

    $importer->run();

    // update toolbar settins manually since it does not get imported
    update_option('aam_toolbar_default', array (
            'about' => '1',
            'toolbar-wp-logo' => '1',
            'wporg' => '1',
            'documentation' => '1',
            'support-forums' => '1',
            'feedback' => '1',
            'toolbar-site-name' => '0',
            'view-site' => '1',
            'view-store' => '1',
            'toolbar-updates' => '1',
            'toolbar-comments' => '1',
            'toolbar-new-content' => '1',
            'new-post' => '1',
            'new-media' => '1',
            'new-page' => '1',
            'new-product' => '1',
            'new-shop_order' => '1',
            'new-shop_coupon' => '1',
            'new-wiaas_doc' => '1',
            'new-user' => '1',
            'gravityforms-new-form' => '1',
        )
    );
}

function wiaas_db_update_update_commercial_lead_capabilities() {
	// add commercial lead specific roles

	wp_roles()->add_cap( 'commercial_lead', 'view_admin_dashboard' );
	wp_roles()->add_cap( 'commercial_lead', 'read' );
	wp_roles()->add_cap( 'commercial_lead', 'upload_files' );

	// enable commercial leads to see Products tab
	wp_roles()->add_cap( 'commercial_lead', 'edit_products' );
	wp_roles()->add_cap( 'commercial_lead', 'edit_others_products' );
	// enable commercial leads to set extra prices on products
	wp_roles()->add_cap( 'commercial_lead', 'manage_wiaas_cl_products' );

	// enable commercial leads to see Orders tab
	wp_roles()->add_cap( 'commercial_lead', 'edit_shop_orders' );

	// enable commercial leads to se Customers tab
	wp_roles()->add_cap( 'commercial_lead', 'manage_wiaas_cl_customers' );
}

function wiaas_db_update_update_supplier_capabilities() {
	// add supplier specific roles
	wp_roles()->add_cap( 'supplier', 'view_admin_dashboard' );
	wp_roles()->add_cap( 'supplier', 'read' );
}

function wiaas_db_update_update_admin_capabilities() {
	wp_roles()->add_cap( 'administrator', 'create_products' );
}


function wiaas_create_role_access_groups() {
	Groups_Group::create(array(
		'name' => 'admin',
	));

	Groups_Group::create(array(
		'name' => 'commercial_lead',
	));

	Groups_Group::create(array(
		'name' => 'supplier',
	));

	Groups_Group::create(array(
		'name' => 'customer',
	));
}