<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.html.html');
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
/**
 * JFormFieldSeller class
 * 
 * @category Component
 * @package  Joomla
 * @author   WebKul software private limited <support@webkul.com>
 * @license  http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link     Technical Support:  webkul.uvdesk.com
 */
class JFormFieldSeller extends JFormFieldList
{
    protected $type = 'seller';
    /**
     * GetOptions function
     *
     * @return void
     */
    public function getOptions()
    {
        $options = array();
        $db     = JFactory::getDbo();
        $query  = $db->getQuery(true);
        $query->select(
            'u.id AS value, u.username AS text,s.screenname as screenname'
        );
        $query->from('`#__users` as u');
        $query->join(
            'INNER',
            '`#__marketplace_sellerprofile` as s ON s.seller_id=u.id'
        );
        $query->order('username');
        // Get the options.
        $db->setQuery($query);

        try
        {
            $options = $db->loadObjectList();
        }
        catch (RuntimeException $e)
        {
            JError::raiseWarning(500, $e->getMessage());
        }

        $i=0;
        foreach ($options as $opt) {
            $user = JFactory::getUser($opt->value);
            $groups = $user->groups;
            if (in_array(8, $groups)) {
                unset($options[$i]);
            }
            $i++;
        }
        $options=array_values($options);
        $options_f=array();
        foreach ($options as $key => $value) {
            if (strlen($value->screenname)) {
                if (strlen($value->screenname)) {
                    $options[$key]->text=$value->text." (".$value->screenname.")";
                } else {
                    $options[$key]->text=$value->text;
                }
            }
        }
        return array_merge(parent::getOptions(), $options);
    }
}
