<?php

/**
 * @package         MailChimp Auto-Subscribe
 * @version         5.0.3 Pro
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            http://www.tassos.gr
 * @copyright       Copyright © 2021 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

// No direct access to this file
defined('_JEXEC') or die;
JFormHelper::loadFieldClass('list');

class JFormFieldUserFields extends JFormFieldList
{
    protected function getInput()
    {
        $fieldsURL = JURI::base() . 'index.php?option=com_fields&context=com_users.user';

        return parent::getInput() . '
            <a class="btn btn-secondary" target="_blank" href="' . $fieldsURL . '">' . JText::_('MAS_USER_FIELDS') . '</a>
            <a style="margin-left:5px;" target="_blank" href="https://www.tassos.gr/joomla-extensions/mailchimp-auto-subscribe/docs/how-to-gain-user-consent-with-a-checkbox-field-gdpr">' . JText::_('JHELP') . '</a>
        ';
    }

    protected function getOptions()
    {
        $options = [];
        $fields  = $this->getCustomFields();

        $options[] = JHTML::_('select.option', '', '- ' . JText::_('MAS_SELECT_FIELD'). ' -');

        if (is_array($fields))
        {
            foreach ($fields as $key => $field)
            {
                $options[] = JHTML::_('select.option', $field->name, $field->title);
            }
        }

        return $options;
    }

    /**
     *  Get a list of all available User Fields
     */
    private function getCustomFields()
    {
        JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

        if (!class_exists('FieldsHelper'))
        {
            return;
        }

        return FieldsHelper::getFields('com_users.user');
    }
}