<?php
/**
 * 2012 - 2017 Mitrocops
 *
 * MODULE reviewsadv
 *
 * @author    Mitrocops <developersaddons@gmail.com>
 * @copyright Copyright (c) permanent, Mitrocops
 * @license   Addons PrestaShop license limitation
 * @version   1.4.6
 * @link      https://addons.prestashop.com/en/2_community-developer?contributor=189784
 *
 * NOTICE OF LICENSE
 *
 * Don't use this module on several shops. The license provided by PrestaShop Addons
 * for all its modules is valid only once for a single shop.
 */

class postshelp  extends Module {

    private $_name = 'reviewsadv';
    private $_is_vk = 0;

	public function postToAPI($data){
		
		$customer_name = $data['customer_name'];
		$product_name = $data['product_name'];
		$product_link = $data['product_link'];
		$rating = (int)$data['rating'];
		$picture = $data['image'];
		$id_lang = $data['id_lang'];
		
		$stars = '';
		for($i=1;$i<=5;$i++){
			$active_star = '★ ';
			$no_active_star = '☆ ';
			
			if($i<=$rating){
				$stars .= $active_star;
			} else {
				$stars .= $no_active_star;
			}
		}
		
		
		
		$name = $data['name'];
		
		$data_requrements = $this->checkrequirements();
    	$is_pstwitterpost = $data_requrements['pstwitterpost'];
    	$is_psvkpost = $data_requrements['psvkpost'];
    	
		// post to Twitter //
		if(Configuration::get($name.'twpost_on') && $is_pstwitterpost){
			require_once(dirname(__FILE__).'/../../../modules/pstwitterpost/pstwitterpost.php');
			$obj_pstwitterpost = new pstwitterpost();
				
			$status = $customer_name.' '.Configuration::get($name.'twdesc'.'_'.$id_lang).': '.$stars.' - '.$product_name.' - '.$product_link;
			$obj_pstwitterpost->postWithAPI(array('status'=>$status));
		}
		// post to Twitter //
		
		if($this->_is_vk) {
            // post to Vkontakte //
            if (Configuration::get($name . 'vkpost_on') && $is_psvkpost) {
                require_once(dirname(__FILE__) . '/../../../modules/psvkpost/psvkpost.php');
                $obj_psvkpost = new psvkpost();

                $status = $customer_name . ' ' . Configuration::get($name . 'vkdesc' . '_' . $id_lang) . ': ' . $stars . ' - ' . $product_name . ' - ' . $product_link;
                $obj_psvkpost->postWithAPI(array('status' => $status, 'image' => $picture, 'product_url' => $product_link));
            }
            // post to Vkontakte //
        }
		
	}
	
	public function postsSettings($data){
    	$_html = '';
    	$title = $data['translate']['title']; 
    	$hint1 = $data['translate']['hint1']; 
    	$hint2 = $data['translate']['hint2']; 

    	$title_pstwitterpost = $data['translate']['title_pstwitterpost'];
    	$title_psvkpost = $data['translate']['title_psvkpost'];
    	$buy_module_psvkpost = $data['translate']['buy_module_psvkpost'];
    	$buy_module_pstwitterpost = $data['translate']['buy_module_pstwitterpost'];
    	

    	$data_requrements = $this->checkrequirements();
    	$is_pstwitterpost = $data_requrements['pstwitterpost'];
    	$is_psvkpost = $data_requrements['psvkpost'];


            $_html .= '<div class="panel">
				       <div class="panel-heading"><i class="fa fa-facebook fa-lg"></i>&nbsp;'.$title.'</div>';

    	
    	$_html .= '<div style="padding: 10px; border: 1px solid rgb(129, 207, 230); font-size: 13px;">
    			  '.$hint1. 
    			  $hint2.'
    			  </div>';
    	
    	
    	
    	$_html .= '<br/><div style="clear:both"></div><br/>';
    	
    	

    	if($is_pstwitterpost){
                $_html .= $this->_pstwitterform16($data);

    	
    	} else {
            $_html .= '<h3 class="title-block-content">'.$title_pstwitterpost.'</h3>';
            $_html .= '<div style="padding: 10px; border: 1px solid red; font-size: 13px;">
    			  '.$buy_module_pstwitterpost.'
    			  </div>';
    	}

        $_html .= '<br/><div style="clear:both"></div><br/>';


        if($this->_is_vk) {
            if ($is_psvkpost) {
                    $_html .= $this->_psvkform16($data);


            } else {
                $_html .= '<h3 class="title-block-content">' . $title_psvkpost . '</h3>';
                $_html .= '<div style="padding: 10px; border: 1px solid red; font-size: 13px;">
    			  ' . $buy_module_psvkpost . '
    			  </div>';
            }
        }

            $_html .= '</div>';


        return $_html;
    }

    private function _psvkform16($data){
        require_once(_PS_MODULE_DIR_.$this->_name .'/reviewsadv.php');
        $obj_reviewsadv = new reviewsadv();
        return $obj_reviewsadv->psvkform16($data);

    }



    private function _pstwitterform16($data){
        require_once(_PS_MODULE_DIR_.$this->_name .'/reviewsadv.php');
        $obj_reviewsadv = new reviewsadv();
        return $obj_reviewsadv->pstwitterform16($data);

    }




	public function checkrequirements(){
    		// Vkontakte Wall Post
    		$is_on_psvkpost = 0;
    		
    		if (file_exists(dirname(__FILE__).'/../../../modules/psvkpost/psvkpost.php')) 
			{
                if(version_compare(_PS_VERSION_, '1.5', '>')) {
                    $_is_psvkpost_active = Module::isEnabled('psvkpost');
                    if ($_is_psvkpost_active)
                        $is_on_psvkpost = 1;
                } else {
                    $is_on_psvkpost = 1;
                }
			}
			
			// Twitter Wall Post
    		$is_on_pstwitterpost = 0;
    		if (file_exists(dirname(__FILE__).'/../../../modules/pstwitterpost/pstwitterpost.php')) 
			{
                if(version_compare(_PS_VERSION_, '1.5', '>')) {
				$_is_psvkpost_active = Module::isEnabled('pstwitterpost');
				if($_is_psvkpost_active)
					$is_on_pstwitterpost = 1;
                } else {
                    $is_on_pstwitterpost = 1;
                }
			}
			
			return array('psvkpost'=>$is_on_psvkpost,'pstwitterpost'=>$is_on_pstwitterpost);
    }
}