<?php 
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."init.php");
include (PATH_TO_ROOT."webcati/init.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");

//recursive function to get all XML elements!

class DesignerPanelCssManipulator extends GridDataManipulator{
	
	public function templateDefaultsAction()
	{
		$templateId = $_POST['templateId'];
		$selectQpos = $_POST['selectQpos'];
		$selTypes = $_POST['selectType'];
		$idSpecial = $_POST['ansType'];
		
		$typeDevice = $_POST['deviceDesktop'];
		
		if(is_array($selTypes))
		{
			foreach ($selTypes as $type) {
				$values[] = "($templateId,0,$type,-999,0,0,$idSpecial,$typeDevice)";
				if($_POST['deviceDesktop'] && $_POST['deviceMobile'])
					$values[] = "($templateId,0,$type,-999,0,0,$idSpecial,2)";
			}
			
		}
		if(is_array($selectTypeSp))
		{
			foreach ($selectTypeSp as $type) {
				$values[] = "($templateId,0,$type,-999,0,0,0,$typeDevice)";
			}
			
		}
		if(is_array($selectQpos))
		{
			foreach ($selectQpos as $type) {
				$values[] = "($templateId,0,0,$type,0,0,0,$typeDevice)";
				if($_POST['deviceDesktop'] && $_POST['deviceMobile'])
					$values[] = "($templateId,0,0,$type,0,0,0,2)";
			}
			
		}
		if($_POST['selectDefault'])
		{
			$values[] = "($templateId,0,0,-999,0,0,0,$typeDevice)";
			if($_POST['deviceDesktop'] && $_POST['deviceMobile'])
					$values[] = "($templateId,0,0,-999,0,0,0,2)";
		}
		if(!$values)
		{
			return "{success:true,error:'No select!'}";
		}
			
		$sql = "INSERT INTO _DesignerManager(id_template,id_question,id_type,id_qpos,id_project,id_user,id_special,type_device) VALUES ".implode(',',$values);
		$sql .= " ON DUPLICATE KEY UPDATE id_template = $templateId"; 
		$this->db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
		if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
		return "{success:true}";
	}

    public function saveBodyPropertiesAction()
    {
        $id = explode('_', $_POST['id']);
        $id = $id[1];
        
        $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . "._DesignerTemplates SET body_properties = '" . mysql_real_escape_string($_POST['body_properties']) . "', id = " . $id; 
        $sql .= " ON DUPLICATE KEY UPDATE body_properties = '" . mysql_real_escape_string($_POST['body_properties'] ) . "'";
        
        
        if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
        return "{success:true}";
    }
    
    public function loadBodyPropertiesAction()
    {
        $id = explode('_', $_POST['id']);
        $id = $id[1];
        
        $sql = "SELECT body_properties FROM " . DB_WARPIT_WEBCATI_BASE . "._DesignerTemplates WHERE id = " . $id; 
        
        
        //if() return $this->db->JsonError();
        $res = $this->db->SQLexecute($sql);
        $dat = $this->db->fetchAssoc($res);
        $data['success'] = TRUE;
        $data['data'] = $dat;
        return json_encode($data);
    }
    
    public function saveControlsAction()
    {
        $id = explode('_', $_POST['id']);
        $id = $id[1];
        
        $checkbox_images = "";
        $custom_checkbox_enable = 0;
        
        if($_POST['custom_checkbox_enable'] == 'on')
        {
            $checkbox_images = $_POST['checkbox_checked_image'] . ";";
            $checkbox_images .= $_POST['checkbox_unchecked_image'] . ";";
            $checkbox_images .= $_POST['checkbox_disabled_image'];
            
            $custom_checkbox_enable = 1;
        }
        
        $radio_images = "";
        $custom_radio_enable = 0;
        
        if($_POST['custom_radio_enable'] == 'on')
        {
            $radio_images = $_POST['radio_checked_image'] . ";";
            $radio_images .= $_POST['radio_unchecked_image'] . ";";
            $radio_images .= $_POST['radio_disabled_image'];
            
            $custom_radio_enable = 1;
          
        }
        
        
        $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . "._DesignerTemplates 
                SET custom_checkbox_enable = '" . $custom_checkbox_enable . "', checkbox_images = '" . mysql_real_escape_string($checkbox_images) . "',
                custom_radio_enable = '" . $custom_radio_enable . "', radio_images = '" . mysql_real_escape_string($radio_images) . "',
                id = " . $id; 
        $sql .= " ON DUPLICATE KEY 
                 UPDATE custom_checkbox_enable = '" . $custom_checkbox_enable . "', checkbox_images = '" . mysql_real_escape_string($checkbox_images) . "',
                 custom_radio_enable = '" . $custom_radio_enable . "', radio_images = '" . mysql_real_escape_string($radio_images) . "'";
       // print $sql;
        
        if(!$this->db->SQLexecute($sql)) return $this->db->JsonError();
        return "{success:true}";
        
        
    }
    
    public function loadControlsAction()
    {
        $id = explode('_', $_POST['id']);
        $id = $id[1];
        
        $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "._DesignerTemplates WHERE id = " . $id; 
        $res = $this->db->SQLexecute($sql);
        $data = $this->db->fetchAssoc($res);
        $dat = array();
        if($data['custom_checkbox_enable'] == "1")
        {
            $imagesArray = explode(';',$data['checkbox_images']);
            if(count($imagesArray) == 3)
            {
                $dat['checkbox_checked_image'] = $imagesArray[0];
                $dat['checkbox_unchecked_image'] = $imagesArray[1];
                $dat['checkbox_disabled_image'] = $imagesArray[2];
                $dat['custom_checkbox_enable'] = $data['custom_checkbox_enable'];
            }
        }
        if($data['custom_radio_enable'] == "1")
        {
            $imagesArray = explode(';',$data['radio_images']);
            if(count($imagesArray) == 3)
            {
                $dat['radio_checked_image'] = $imagesArray[0];
                $dat['radio_unchecked_image'] = $imagesArray[1];
                $dat['radio_disabled_image'] = $imagesArray[2];
                $dat['custom_radio_enable'] = $data['custom_radio_enable'];
            }
        }
        
        $data['success'] = TRUE;
        $data['data'] = $dat;
        return json_encode($data);
    }
    
	
}
	
	
	$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
	$manipulator = new DesignerPanelCssManipulator($db,'nimaveze');
	$comm = $_POST['action'].'Action';
	if(method_exists($manipulator,$comm))
		echo $manipulator->$comm();
	else 
	{
		echo '{success:false,error:"Unknown action!"}';	
	}

?>