<?php
/**
 *    This file is part of Mobile Assistant Connector.
 *
 *   Mobile Assistant Connector is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Mobile Assistant Connector is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Mobile Assistant Connector.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @author    eMagicOne <contact@emagicone.com>
 *  @copyright 2014-2017 eMagicOne
 *  @license   http://www.gnu.org/licenses   GNU General Public License
 */

function deleteEmptyDevices()
{
    $sql = 'DELETE md FROM `'._DB_PREFIX_.'mobassistantconnector_devices` md
        LEFT JOIN `'._DB_PREFIX_
        .'mobassistantconnector_push_notifications` mpn ON mpn.`device_unique_id` = md.`device_unique_id`
        WHERE mpn.`device_unique_id` IS NULL';
    Db::getInstance()->execute($sql);
}

function deleteEmptyAccounts()
{
    $sql = 'DELETE ma FROM `'._DB_PREFIX_.'mobassistantconnector_accounts` ma
        LEFT JOIN `'._DB_PREFIX_
        .'mobassistantconnector_devices` md ON md.`account_id` = ma.`id`
        WHERE ma.`id` IS NULL';
    Db::getInstance()->execute($sql);
}

function getRestrictedActions()
{
    // Visible for users in back-end
    $restricted_actions = array(
        array(
            'group_name' => 'Push notification settings',
            'child'      => array(
                array(
                    'code' => 'push_notification_settings_new_order',
                    'name' => 'New order',
                ),
                array(
                    'code' => 'push_notification_settings_new_customer',
                    'name' => 'New customer',
                ),
                array(
                    'code' => 'push_notification_settings_order_statuses',
                    'name' => 'Order statuses',
                ),
            ),
        ),
        array(
            'group_name' => 'Store statistics',
            'child'      => array(
                array(
                    'code' => 'store_stats',
                    'name' => 'Store statistics',
                ),
            ),
        ),
        array(
            'group_name' => 'Products',
            'child'      => array(
                array(
                    'code' => 'products_list',
                    'name' => 'Product list',
                ),
                array(
                    'code' => 'product_details',
                    'name' => 'Product details',
                ),
            ),
        ),
        array(
            'group_name' => 'Customers',
            'child'      => array(
                array(
                    'code' => 'customers_list',
                    'name' => 'Customer list',
                ),
                array(
                    'code' => 'customer_details',
                    'name' => 'Customer details',
                ),
            ),
        ),
        array(
            'group_name' => 'Orders',
            'child'      => array(
                array(
                    'code' => 'orders_list',
                    'name' => 'Order list',
                ),
                array(
                    'code' => 'order_details',
                    'name' => 'Order details',
                ),
                array(
                    'code' => 'order_details_pdf',
                    'name' => 'Order details PDF',
                ),
                array(
                    'code' => 'order_details_products_list_pickup',
                    'name' => 'Order products list pickup',
                ),
                array(
                    'code' => 'update_order_status',
                    'name' => 'Order status updating',
                ),
                array(
                    'code' => 'update_order_tracking_number',
                    'name' => 'Order tracking number updating',
                ),
            ),
        ),
        array(
            'group_name' => 'Abandoned carts',
            'child'      => array(
                array(
                    'code' => 'abandoned_carts_list',
                    'name' => 'Abandoned cart list',
                ),
                array(
                    'code' => 'abandoned_cart_details',
                    'name' => 'Abandoned cart details',
                ),
            ),
        ),
    );

    return $restricted_actions;
}

function getRestrictedActionsToFunctions()
{
    $restricted_actions_to_functions = array(
        'push_notification_settings_new_order' => array('push_notification_settings', 'delete_push_config'),
        'push_notification_settings_new_customer' => array('push_notification_settings', 'delete_push_config'),
        'push_notification_settings_order_statuses' => array('push_notification_settings', 'delete_push_config'),
        'store_stats' => array('get_store_stats', 'get_data_graphs', 'get_status_stats'),
        'products_list' => array('search_products', 'search_products_ordered'),
        'product_details' => array('get_products_info', 'get_products_descr'),
        'customers_list' => array('get_customers'),
        'customer_details' => array('get_customers_info'),
        'orders_list' => array('get_orders'),
        'order_details' => array('get_orders_info'),
        'order_details_pdf' => array('get_order_pdf'),
        'order_details_products_list_pickup' => array('get_order_product_list_pickup'),
        'update_order_status' => array('set_order_action', 'change_status'),
        'update_order_tracking_number' => array( 'set_order_action', 'update_track_number'),
        'abandoned_carts_list' => array('get_abandoned_carts_list'),
        'abandoned_cart_details' => array('get_abandoned_cart_details')
    );

    return $restricted_actions_to_functions;
}

function getAlwaysAllowedFunctions()
{
    $allowed_functions_always = array(
        'run_self_test',
        'get_stores',
        'get_currencies',
        'get_store_title',
        'get_orders_statuses',
        'get_carriers',
    );

    return $allowed_functions_always;
}
