<?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    points
 */

/**
 * Hook class.
 */
class Hook_commandr_command_send_points
{
    /**
     * Run function for Commandr hooks.
     *
     * @param  array $options The options with which the command was called
     * @param  array $parameters The parameters with which the command was called
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return array Array of stdcommand, stdhtml, stdout, and stderr responses
     */
    public function run(array $options, array $parameters, object &$commandr_fs) : array
    {
        if (!addon_installed('points')) {
            return ['', '', '', do_lang('INTERNAL_ERROR')];
        }

        require_lang('points');

        if ((array_key_exists('h', $options)) || (array_key_exists('help', $options))) {
            return ['', do_command_help('send_points', ['h', 'a', 'nr'], [true, true, true, true]), '', ''];
        }

        if (!array_key_exists(0, $parameters)) {
            return ['', '', '', do_lang('MISSING_PARAM', '1', 'send_points')];
        }
        if (!array_key_exists(1, $parameters)) {
            return ['', '', '', do_lang('MISSING_PARAM', '2', 'send_points')];
        }
        if (!array_key_exists(2, $parameters)) {
            return ['', '', '', do_lang('MISSING_PARAM', '3', 'send_points')];
        }

        require_code('points2');

        $member_id = $GLOBALS['FORUM_DRIVER']->get_member_from_username($parameters[0]);
        if (($member_id === null) || (is_guest($member_id))) {
            return ['', '', '', do_lang('MEMBER_NO_EXIST')];
        }

        $amount_gift_points = (((array_key_exists(3, $parameters)) && ($parameters[3] != 'null')) ? intval($parameters[3]) : null);
        $anonymous = ((array_key_exists('a', $options)) || (array_key_exists('anonymous', $options))) ? 1 : 0;
        $is_ranked = (array_key_exists('nr', $options));

        // If parameter 4 is not provided, this is a system credit
        if (!array_key_exists(4, $parameters)) {
            // Include name of member who awarded the points if not anonymous
            if (!(array_key_exists('a', $options)) && !(array_key_exists('anonymous', $options))) {
                $parameters[1] .= ' (' . do_lang('SENT_BY', $GLOBALS['FORUM_DRIVER']->get_username(get_member())) . ')';
            }
            points_credit_member(intval($parameters[0]), $parameters[1], intval($parameters[2]), $anonymous, true, 0, 'points', 'credit', '', null, $is_ranked);
            return ['', '', do_lang('SUCCESS'), ''];
        }

        $member_id_sender = $GLOBALS['FORUM_DRIVER']->get_member_from_username($parameters[4]);
        points_transact($member_id_sender, $member_id, $parameters[1], intval($parameters[2]), $amount_gift_points, $anonymous, true, 0, '', '', '', null, false, $is_ranked);

        return ['', '', do_lang('SUCCESS'), ''];
    }
}
