Object PHP Code


require_once "/sw/installed/apache20/htdocs/soap/lib/nusoap.php";
class XMPieRequest
{
    protected $xmpie_username = "un";
    protected $xmpie_password = "pw";
    protected $soap_version = 1;

    public $xmpie_api_server = "xmpie";

    protected $soap_class;
    protected $soap_action;
    protected $action_parameters = array();


    public $xmpie_class_app_name = "";
    public $xmpie_api_location = "";
    public $soap_request = "";
    public $soap_response = "";

    function __construct($soap_class, $soap_action, $action_parameters = array())
    {
        if (!empty($action_parameters))
        {
            $this->action_parameters = $action_parameters;
        }

        $this->soap_class = $soap_class;
        $this->soap_action = $soap_action;
        $this->xmpie_class_app_name = $this->soap_class . "_SSP.asmx";
        $this->xmpie_api_location = $this->xmpie_api_server . "/XMPieWSAPI/";
    }

    public function getSoapRequest()
    {
        if (empty($this->soap_request))
        {
            return null;
        }

        return $this->soap_request;
    }

    public function setupSoapRequest($action_parameters = array())
    {
        $this->action_parameters = empty($action_parameters) ?
                $this->action_parameters : $action_parameters;
        if (empty($this->action_parameters))
        {
            return null;
        }

        $this->action_parameters["inUsername"] = $this->xmpie_username;
        $this->action_parameters["inPassword"] = $this->xmpie_password;

        $soap_request = "";
        $soap_request .= $this->getXmpieSoapHeader();
        $soap_request .= $this->array_xml($this->action_parameters);
        $soap_request .= $this->getXmpieSoapFooter();

        $this->soap_request = $soap_request;
        return $soap_request;
    }

    public function doSoapRequest($want_response = false)
    {
        $req_target = "http://" . $this->xmpie_api_location .
                      $this->xmpie_class_app_name;
        $full_action_string = "XMPieWSAPI/" . $this->soap_action;

        $p_client =
            new SoapClient(null,
                           array("location" => "http://" . $req_target,
                                 "uri" => "http://schemas.xmlsoap.org/soap/envelope/"));

        $resp = $p_client->__doRequest($this->soap_request,
                                      $req_target,
                                      $full_action_string,
                                      $this->soap_version);

        $this->soap_response = $resp;

        if ($want_response)
        {
            return $this->soap_response;
        }
    }

    public function parseResponseIntoArray($soap_response_xml = "")
    {
        $this->soap_response = empty($soap_response_xml) ?
                               $this->soap_response : $soap_response_xml;

        $new_response = $this->drop_known_soap_tags();

        $resp_obj = simplexml_load_string($new_response);

        $response_string = $this->soap_action . "Response";
        $result_string = $this->soap_action . "Result";

        $xpath = "/$response_string/$result_string/diffgr:diffgram";
        $xpath_resp = $resp_obj->xpath($xpath);

        $ns_parser = new soap_parser($this->soap_response);

        return $ns_parser->get_response();
    }

    public function execute()
    {
        $this->setupSoapRequest();
        $this->doSoapRequest();
        return $this->parseResponseIntoArray();
    }

    protected function getXmpieSoapFooter()
    {
        $soap_request_footer = "soap_action>

";

        return $soap_request_footer;
    }
    protected function getXmpieSoapHeader()
    {  // Not Indented to get rid of as much unnecessary space as possible from XML.
$soap_request_header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>

    
        <$this->soap_action xmlns=\"XMPieWSAPI\">";

        return $soap_request_header;
    }

    public function getActionParameters()
    {
        return $this->action_parameters;
    }

    protected function array_xml($array, $num_prefix = "num_")
    {
        $return = "";
        if(!is_array($array)) // text
        {
            return $array;
        }
        else
        {
            foreach($array as $key=>$val) // subnode
            {
                $key = (is_numeric($key)? $num_prefix.$key : $key);
                $return.="<".$key.">".$this->array_xml($val, $num_prefix)."";
            }
        }

        return $return;
    }

    protected function drop_known_soap_tags()
    {
        $num_replacements = 0;
        $num_matches = preg_match_all("/<\/?soap:(Envelope|Body|Fault)(.*)?>/U", $this->soap_response, $tags_matched);


        $new_xml = preg_replace("/<\/?soap:(Envelope|Body|Fault)(.*)?>/U", "", $this->soap_response, -1, $num_replacements);
        $new_xml = preg_replace("/ xmlns=\".*\"/U", "", $new_xml);

        return $new_xml;
    }
}

/**
 * Should work with ONLY CampaignID and csv File Name.
 * All other parameters should be static / constant.
 */
class CSVDataSource extends XMPieRequest
{
    protected $xmpie_csv_file_type = "TXT";
    protected $xmpie_csv_connection_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\XMPIE\Xmpie_ftp\;Extended Properties=text;";
    protected $xmpie_csv_source_folder = "\\\\XMPIE\\XMPie_FTP\\";
    protected $xmpie_csv_del_source_after = "false";

    function __construct($action_parameters)
    {
        parent::__construct("DataSource", "CreateNew", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inType"] = $this->xmpie_csv_file_type;
        $this->action_parameters["inConnectionString"] = $this->xmpie_csv_connection_string;
        $this->action_parameters["inSourceFolder"] = $this->xmpie_csv_source_folder;
        $this->action_parameters["inDeleteSource"] = $this->xmpie_csv_del_source_after;
        $this->action_parameters["inName"] = $this->action_parameters["file_name"];
        $this->action_parameters["inCampaignID"] = $this->action_parameters["campaign_id"];
        $this->action_parameters["inAdditionalInfo"] = $this->action_parameters["file_name"] . "@,@,";

        parent::setupSoapRequest();
    }
}

class JobTicket extends XMPieRequest
{
    function __construct($action_parameters)
    {
        parent::__construct("JobTicket", "CreateNewTicketForDocument", $action_parameters);
    }

    function setupSoapRequest()
    {
        $csv_table_name = preg_replace("/\.csv/", "#csv", $this->action_parameters["file_name"]);

        $this->action_parameters["inDocumentID"] = $this->action_parameters["document_id"];
        $this->action_parameters["inRITableName"] = $csv_table_name;

        parent::setupSoapRequest();
    }
}

class SetOutputParameters extends XMPieRequest
{
    function __construct($action_parameters)
    {
        parent::__construct("JobTicket", "SetOutputParameters", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inTicketID"] = $this->action_parameters["job_ticket_id"];

        parent::setupSoapRequest();
    }
}

class SetJobType extends XMPieRequest
{
    protected $job_type = "PRINT";

    function __construct($action_parameters)
    {
        parent::__construct("JobTicket", "SetJobType", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inJobType"] = $this->job_type;
        $this->action_parameters["inTicketID"] = $this->action_parameters["job_ticket_id"];

        parent::setupSoapRequest();
    }
}

class SetOutputInfo extends XMPieRequest
{
    protected $output_type = "PDFO";
    protected $output_media = 1;
    protcted $output_folder = "";
    protected $output_filename = "";

    function __construct($action_parameters)
    {
        $this->output_type = empty($action_parameters["output_type"]) ?
                             $this->output_type : $action_parameters["output_type"];
        $this->output_filename = empty($action_parameters["output_filename"]) ?
                                 "koh_xmpie_job_" . $action_parameters["job_ticket_id"] :
                                 $action_parameters["output_filename"];

        parent::__construct("JobTicket", "SetOutputInfo", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inType"] = $this->output_type;
        $this->action_parameters["inMedia"] = $this->output_media;
        $this->action_parameters["inFolder"] = $this->output_folder;
        $this->action_parameters["inTicketID"] = $this->action_parameters["job_ticket_id"];
        $this->action_parameters["inFileName"] = $this->output_filename;

        parent::setupSoapRequest();
    }
}

class SetDestination extends XMPieRequest
{
    protected $delete_after = "false";
    protected $destination_parameters = "";

    function __construct($action_parameters)
    {
        $this->destination_parameters = empty($action_parameters["destination_parameters"]) ?
                                        $this->destination_parameters :
                                        $action_parameters["destination_parameters"];
        $this->delete_after = empty($action_parameters["delete_after"]) ?
                              $this->delete_after :
                              $action_parameters["delete_after"];

        parent::__construct("JobTicket", "AddDestinationByID", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inTicketID"] = $this->action_parameters["job_ticket_id"];
        $this->action_parameters["inDestinationID"] = $this->action_parameters["destination_id"];
        $this->action_parameters["inDestinationParam"] = $this->destination_parameters;
        $this->action_parameters["inDeleteSource"] = $this->delete_after;

        parent::setupSoapRequest();
    }
}

class SetDataRange extends XMPieRequest
{
    protected $range_start_point = -1;
    protected $range_end_point = -1;

    function __construct($action_parameters)
    {
        $this->range_start_point = empty($action_parameters["range_start_point"]) ?
                                   $this->range_start_point :
                                   $action_parameters["range_start_point"];
        $this->range_end_point = empty($action_parameters["range_end_point"]) ?
                                 $this->range_end_point :
                                 $action_parameters["range_end_point"];

        parent::__construct("JobTicket", "SetRIRange", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inRangeFrom"] = $this->range_start_point;
        $this->action_parameters["inRangeTo"] = $this->range_end_point;
        $this->action_parameters["inTicketID"] = $this->action_parameters["job_ticket_id"];

        parent::setupSoapRequest();
    }
}

class SubmitJob extends XMPieRequest
{
    protected $job_priority = 0;

    function __construct($action_parameters)
    {
        $this->job_priority = empty($action_parameters["job_priority"]) ?
                              $this->job_priority :
                              $action_parameters["job_priority"];

        parent::__construct("Production", "SubmitJob", $action_parameters);
    }

    function setupSoapRequest()
    {
        $this->action_parameters["inJobTicket"] = $this->action_parameters["job_ticket_id"];
        $this->action_parameters["inPriority"] = $this->job_priority;

        parent::setupSoapRequest();
    }
}