<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * ccsb class
 *
 * soap library for Code Igniter.
 *
 * @author		ccsb
 * @version		1.0.0
 * @based on	CL Auth by adi tio priambodo (tioadji@yahoo.com)
  */
 
class ccsb
{ 
	var $xml;
	var $rows;
	var $sts;
	var $des;
	var	$detail;
	var $orher_nodes; 
	var $group_search_index;
	function ccsb($sp_name) 
	{
		$this->ci =& get_instance(); 
 		$this->ci->load->library('Session');
 	    $update_activity = array('last_activity' => time());
        $this->ci->session->set_userdata($update_activity); 
		// Initialize
	 	$XMLDATA_info = '<param>'
							.'<xuser>'
								.'<xid>1</xid>'
								.'<xpass>2</xpass>'
							.'</xuser>'
						.'<command>'
						.'<spt>'.$sp_name.'</spt>'
						.'</command>'
						.'<web_session>'
							.'<last_activity>'._get_time().'</last_activity>'
							.'<data>'.$storeprocedurename.'</data>' 
						.'</web_session>'
						.'<search>'
						.'</search>'
						.'<search2>'
						.'</search2>' 
						.'<field>'
						.'</field>'
						.'</param>' ;
		$this->$_xml =DOMDocument::loadXML($XMLDATA_info);
		$group_search_index=-1; 
		$detail=      array(  'sort'=> 0,
        					  'desc'=> 0,
        					  'rowspage'=> 0,
        					  'rows'=> 0,
        					  'pages'=> 0,
        					  'page'=> 1
        					);
	}
	
	 
 
	function addGroupSearch2($modeGroup) {	 
		$node = $this->$_xml->getElementsByTagName('param')->item(0);
		$search = $node->getElementsByTagName('search2')->item(0);
		$listGroupSearch = $search->getElementsByTagName('fg');
		$ke=0;
		if ($listGroupSearch->item(0) != null){
			$ke =$listGroupSearch->length;
		}
		$ke++;
		$element = $this->$_xml->createElement('fg', '');
		$element->setAttribute('gID',$ke);
		$element->setAttribute('mg',$modeGroup);
		$search->appendChild($element);
		return $element;
	}

	function addSearchText2( 	$groupSearch	// target group 
								,$name			// field code ex: rID, r1
								,$modesearch	//0:AND	,1:OR
								,$modepersamaan //1:equal , 2:< , 3:> , 4:between
								,$FindString)
	{
	
		$list_find =$this->$_xml->getElementsByTagName('listf');
		$ke=0;
		if ($list_find->item(0)!= null)  
		$ke = $list_find->length;
		
		$ke++;
		
		$element = $this->$_xml->createElement('listf', '');
		$element->setAttribute('fid',$ke);
		$element->setAttribute('fn',$name);
		$element->setAttribute('mg',$modesearch);
		$element->setAttribute('mp',$modepersamaan);
		$element->setAttribute('mf','1');//--= 1= STRING ,2=ANGKA , 3=DATETIME 
		$element->setAttribute('ft1',$FindString);
		$element->setAttribute('ft2','');//di pakai untuk between
		$groupSearch->appendChild($element);
	}

	function addSearchNum2(	 $groupSearch	// target group 
						,$name			// field code ex: rID, r1
						,$modesearch	//0:AND	,1:OR
						,$modepersamaan //1:equal , 2:< , 3:> , 4:between
						,$num1
						,$num2
						){ 
		$list_find =$this->$_xml->getElementsByTagName('listf');
		$ke=0;
		if ($list_find->item(0)!= null)  
		$ke = $list_find->length;
		
		$ke++;
		
		$element = $this->$_xml->createElement('listf', '');
		$element->setAttribute('fid',$ke);
		$element->setAttribute('fn',$name);
		$element->setAttribute('mg',$modesearch);
		$element->setAttribute('mp',$modepersamaan);
		$element->setAttribute('mf','2');//--= 1= STRING ,2=ANGKA , 3=DATETIME 
		$element->setAttribute('ft1',$num1);
		$element->setAttribute('ft2',$num);//di pakai untuk between
		$groupSearch->appendChild($element);
	}
 

	function addSearchDate2( $groupSearch	// target group 
							,$name			// field code ex: rID, r1
							,$modesearch	//0:AND	,1:OR
							,$modepersamaan //1:equal , 2:< , 3:> , 4:between
							,$FindDate
							,$FindDate2
							){

	$list_find =$this->$_xml->getElementsByTagName('listf');
	$ke=0;
	if ($list_find->item(0)!= null)  
	$ke = $list_find->length;

	$ke++;

	$element = $this->$_xml->createElement('listf', '');
	$element->setAttribute('fid',$ke);
	$element->setAttribute('fn',$name);
	$element->setAttribute('mg',$modesearch);
	$element->setAttribute('mp',$modepersamaan);
	$element->setAttribute('mf','3');//--= 1= STRING ,2=ANGKA , 3=DATETIME 
	$element->setAttribute('ft1',$FindDate);
	$element->setAttribute('ft2',$FindDate2);//di pakai untuk between
	$groupSearch->appendChild($element);
	}

	function add_car($car){
		$node = $this->$_xml->getElementsByTagName('param')->item(0);
		$field = $node->getElementsByTagName('field')->item(0);
		if ($field->getElementsByTagName('car')->item(0)!=null){
			$listfile = $field->getElementsByTagName('car')->item(0);
		}
		else {
			$listfile = $this->$_xml->createElement('car', '');
			$field->appendChild($listfile);
		}
		$l = $this->$_xml->createElement('l', '');
		$listfile->appendChild($l);
		
		$element = $this->$_xml->createElement('id', $car);
		$l->appendChild($element);
	}

	function AddField($name,$value){
		$node =  $this->$_xml->getElementsByTagName('param')->item(0);
		$search = $node->getElementsByTagName('field')->item(0);
		$element =  $this->$_xml->createElement($name, $value);
		$search->appendChild($element);
	}
	 
	
 	function getXMLValue($xmlnode,$tagName,$default=''){ 
		$hasil='';
		if ($xmlnode !=null) {
			if ($xmlnode->getElementsByTagName($tagName)->item(0)!=null){
					$hasil = $xmlnode->getElementsByTagName($tagName)->item(0)->nodeValue;
				} 
		}else{
		 $hasil=$default;
		}
		return $hasil;
	}
	
	function paramView(&$sts,&$des,&$rows,&$detail){
		
	
	function call_ws($other_nodes_tag=''){
		$ky = "PalapaTcoTidCyb3rC3ycl3V2012XVIP"; // 32 * 8 = 256 bit key
		  
		try {
	  	
			 $clientX_info = new SoapClient( APPWSPATH , array('trace' => 1,
													"exceptions" => true,                                                  
													'features' => SOAP_SINGLE_ELEMENT_ARRAYS + SOAP_USE_XSI_ARRAY_TYPE ,
													'cache_wsdl'=> WSDL_CACHE_NONE,
													'style'    => SOAP_DOCUMENT,
													'use'      => SOAP_LITERAL
												)); 
         
        	$strXMLData_info = $clientX_info->OlahDataOutStringWithEncrypt(array('Param'=>encryptRJ256($ky,$this->$_xml->saveXML())))->OlahDataOutStringWithEncryptResult; 
          	$xml_report = DOMDocument::loadXML(decryptRJ256($ky,$strXMLData_info));
       	 	$xpath_report = new DOMXpath($xml_report);
			
		 	$this->$_sts =(int)substr($xpath_report->query('//result/sts/no')->item(0)->nodeValue) ;
          	$this->$_des = $xpath_report->query('//result/sts/des')->item(0)->nodeValue;
			
        	if ($this->$_$sts==0){ 
        		$this->$_rows = $xpath_report->query('//result/xrow');
				$this->$orher_nodes=array(); 
		 		if (is_array($other_nodes_tag)){ 
					foreach ($other_nodes_tag as &$value) {
						$orher_nodes[$value] = $xpath_report->query('//result/'.$value); 
					}
				}else{
					if ($other_nodes_tag != '')
					$this->$orher_nodes = $xpath_report->query('//result/'.$other_nodes_tag);  
				}
		  	} 
        	if ($xpath_report->query('//result/detail/sort')->item(0) !=null){
        		foreach ($xpath_report->query('//result/detail')->item(0)->childNodes as $field) {
					$detail[$field->nodeName]=$field->nodeValue;
				}
		 		
        	}else{
        		$detail=  array(  'sort'=> 0,
        					  'desc'=> 0,
        					  'rowspage'=> 0,
        					  'rows'=> 0,
        					  'pages'=> 0,
        					  'page'=> 1
        					);
        	}
	 		//$des =$des .$timexec;
		
		} catch (SoapFault $fault) {
				$this->$_sts =9001;
				$this->$_des= $fault->faultcode ."\n". $fault->faultstring;
			// trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
		} catch(Exception $e) {
				$this->$_sts =9002;
				$this->$_des=$e->getMessage(); 
		} finally {
		  	unset($strXMLData_info);
			unset($xml_report);
			unset($xpath_report); 
		}
	}     
		
 

	 
}

?>