<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    ecommerce
 */

/**
 * Hook class.
 */
class Hook_actionlog_ecommerce extends Hook_actionlog
{
    /**
     * Get details of action log entry types handled by this hook.
     *
     * @return array Map of handler data in standard format
     */
    public function get_handlers() : array
    {
        if (!addon_installed('ecommerce')) {
            return [];
        }

        require_lang('ecommerce');

        $ret = [
            'CREATE_INVOICE' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => 0,
                'written_context_index' => null,
                'followup_page_links' => [
                    'VIEW_PROFILE' => ['FORUM_DRIVER__PROFILE_URL', '{MEMBER_ID}'],
                    'CREATE_INVOICE' => '_SEARCH:admin_invoices:add',
                ],
            ],
            'DELETE_INVOICE' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => 0,
                'written_context_index' => null,
                'followup_page_links' => [
                    'CREATE_INVOICE' => '_SEARCH:admin_invoices:add',
                ],
            ],
            'ECOM_PRODUCTS_ADD_MAIL_FORWARDER' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => null,
                'written_context_index' => 0,
                'followup_page_links' => [
                    'ECOM_PRODUCTS_MANAGE_INVENTORY' => '_SEARCH:admin_ecommerce:prices',
                ],
            ],
            'ECOM_PRODUCTS_ADD_MAIL_POP3' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => null,
                'written_context_index' => 0,
                'followup_page_links' => [
                    'ECOM_PRODUCTS_MANAGE_INVENTORY' => '_SEARCH:admin_ecommerce:prices',
                ],
            ],
            'ECOM_PRODUCTS_AMEND_CUSTOM_PERMISSIONS' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => null,
                'written_context_index' => null,
                'followup_page_links' => [
                    'ECOM_PRODUCTS_MANAGE_INVENTORY' => '_SEARCH:admin_ecommerce:prices',
                ],
            ],
            'ECOM_PRODUCTS_AMEND_CUSTOM_PRODUCTS' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => null,
                'written_context_index' => null,
                'followup_page_links' => [
                    'ECOM_PRODUCTS_MANAGE_INVENTORY' => '_SEARCH:admin_ecommerce:prices',
                ],
            ],
            'ECOM_PRODUCT_CHANGED_PRICES' => [
                'flags' => ACTIONLOG_FLAGS_NONE,
                'cma_hook' => null,
                'identifier_index' => null,
                'written_context_index' => null,
                'followup_page_links' => [
                    'ECOM_PRODUCTS_MANAGE_INVENTORY' => '_SEARCH:admin_ecommerce:prices',
                ],
            ],
        ];

        if (get_forum_type() == 'cns') {
            $ret += [
                'ADD_USERGROUP_SUBSCRIPTION' => [
                    'flags' => ACTIONLOG_FLAGS_NONE,
                    'cma_hook' => 'usergroup_subscription',
                    'identifier_index' => 0,
                    'written_context_index' => 1,
                    'followup_page_links' => [
                        'EDIT_THIS_USERGROUP_SUBSCRIPTION' => '_SEARCH:admin_ecommerce:_edit:{ID}',
                        'ADD_USERGROUP_SUBSCRIPTION' => '_SEARCH:admin_ecommerce:add',
                    ],
                ],
                'EDIT_USERGROUP_SUBSCRIPTION' => [
                    'flags' => ACTIONLOG_FLAGS_NONE,
                    'cma_hook' => 'usergroup_subscription',
                    'identifier_index' => 0,
                    'written_context_index' => 1,
                    'followup_page_links' => [
                        'EDIT_THIS_USERGROUP_SUBSCRIPTION' => '_SEARCH:admin_ecommerce:_edit:{ID}',
                        'ADD_USERGROUP_SUBSCRIPTION' => '_SEARCH:admin_ecommerce:add',
                    ],
                ],
                'DELETE_USERGROUP_SUBSCRIPTION' => [
                    'flags' => ACTIONLOG_FLAGS_NONE,
                    'cma_hook' => 'usergroup_subscription',
                    'identifier_index' => 0,
                    'written_context_index' => 1,
                    'followup_page_links' => [
                        'ADD_USERGROUP_SUBSCRIPTION' => '_SEARCH:admin_ecommerce:add',
                    ],
                ],
            ];
        }

        return $ret;
    }

    /**
     * Get written context for an action log entry handled by this hook.
     *
     * @param  array $actionlog_row Action log row
     * @param  array $handler_data Handler data
     * @param  ?string $identifier Identifier (null: none)
     * @return ?string Written context (null: none)
     */
    protected function get_written_context(array $actionlog_row, array $handler_data, ?string $identifier) : ?string
    {
        switch ($actionlog_row['the_type']) {
            case 'CREATE_INVOICE':
                $member_id = $GLOBALS['SITE_DB']->query_select_value_if_there('ecom_invoices', 'i_member_id', ['id' => intval($actionlog_row['param_a'])]);
                if ($member_id !== null) {
                    $username = $GLOBALS['FORUM_DRIVER']->get_username($member_id);
                    if ($username === null) {
                        $username = '#' . strval($member_id);
                    }
                } else {
                    $username = do_lang('UNKNOWN');
                }

                $written_context = do_lang('SOMETHING_FROM', $actionlog_row['param_b'], $username);
                return $written_context;
        }

        return parent::get_written_context($actionlog_row, $handler_data, $identifier);
    }

    /**
     * Get details of action log entry types handled by this hook.
     *
     * @param  array $actionlog_row Action log row
     * @param  ?string $identifier The identifier associated with this action log entry (null: unknown / none)
     * @param  ?string $written_context The written context associated with this action log entry (null: unknown / none)
     * @param  array $bindings Default bindings
     */
    protected function get_extended_actionlog_bindings(array $actionlog_row, ?string $identifier, ?string $written_context, array &$bindings)
    {
        switch ($actionlog_row['the_type']) {
            case 'CREATE_INVOICE':
                if ($identifier !== null) {
                    $member_id = $GLOBALS['SITE_DB']->query_select_value_if_there('ecom_invoices', 'i_member_id', ['id' => intval($identifier)]);
                    if ($member_id !== null) {
                        $bindings += [
                            'MEMBER_ID' => strval($member_id),
                        ];
                    }
                }
                break;
        }
    }
}
