<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/

require_once(JPATH_SITE . "/components/com_os_cck/functions.php");

//JHTML::_('behavior.modal', 'a.modal-button');
$doc = JFactory::getDocument();
includeJavaScriptCCK('components/com_os_cck/assets/js/functions.js');
includeStylesCCK('components/com_os_cck/assets/css/admin_style.css');
 
class JFormFieldPaymentsystems extends JFormField{
  protected function getInput(){
    $os_cck_configuration = JComponentHelper::getParams('com_os_cck');  
//    var_dump($os_cck_configuration->get('payment_systems')); exit;
    $system_array = array();
    $system_array[] = '2checkout';
    $system_array[] = 'paypal';
    $system_array[] = 'paypal2';
    $system_array[] = 'stripe';
    
    
    $db = JFactory::getDBO();
    $query = "SELECT name,enabled,element FROM #__extensions WHERE folder='payment'";
    $db->setQuery($query);
    $plugins = $db->loadObjectList();
//    var_dump($plugins); exit;
    $instaled_plugins = array();
//    $enabled_plugins = array();
    
    foreach($plugins as $plugin){
//        if($plugin->enabled == 1){
//            $enabled_plugins[] = $plugin->element;
//        }
        $instaled_plugins[] = $plugin->element;
    }
    
    $merge_plugins = array_merge($system_array, $instaled_plugins);
    $merge_plugins = array_unique($merge_plugins);
    sort($merge_plugins);
    
    $plugins_opt = array();
    foreach($merge_plugins as $plugin){
        $install_plugin = false;
        $enable_plugin = false;
        $plugin_name = '';
        foreach($plugins as $plugin2){
            if($plugin2->element == $plugin){
                $install_plugin = true;
                $plugin_name = str_replace('Payment - ', '', $plugin2->name);
                if($plugin2->enabled == 1){
                    $enable_plugin = true;
                }
            }
        }
        if($plugin_name == ''){
            switch ($plugin){
                case '2checkout':
                    $plugin_name = '2Checkout';
                    break;
                case 'paypal':
                    $plugin_name = 'Paypal';
                    break;
                case 'paypal2':
                    $plugin_name = 'Paypal 2';
                    break;
                case 'stripe':
                    $plugin_name = 'Stripe';
                    break;
            }
        }
        
        $disabled = false;
        if(!$install_plugin){
            $plugin_name .= '(Do not install)';
            $disabled = true;
        }
        if($install_plugin && !$enable_plugin){
            $plugin_name .= '(Do not enable)';
            $disabled = true;
        }
        $plugins_opt[] = JHTML::_('select.option',$plugin,$plugin_name, "value", "text", $disabled);
        
    }
    
    $plugins_list = JHTML::_('select.genericlist',$plugins_opt, $this->name.'[]',
                                    'size="6" class="inputbox" multiple="multiple" ', 'value', 'text', $os_cck_configuration->get('payment_systems', array()));
    
    return $plugins_list;
    
  }
}