<commit_before><?php
/**
 * PHPFrame/Document/RPC.php
 * 
 * PHP version 5
 * 
 * @category   MVC_Framework
 * @package    PHPFrame
 * @subpackage Document
 * @author     <NAME> <<EMAIL>>
 * @copyright  2009 E-noise.com Limited
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    SVN: $Id$
 * @link       http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 */

/**
 * RPC Document Class
 * 
 * @category   MVC_Framework
 * @package    PHPFrame
 * @subpackage Document
 * @author     <NAME> <<EMAIL>>
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link       http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @see        PHPFrame_Document
 * @since      1.0
 */
class PHPFrame_Document_RPC extends PHPFrame_Document_XML
{
    /**
     * Constructor
     * 
     * @access public
     * @return void
     * @uses   DOMImplementation, PHPFrame_Utils_URI, PHPFrame_Application_Pathway
     * @since  1.0 
     */
    public function __construct($mime="text/xml", $charset=null) 
    {
        // Call parent's constructor to set mime type
        parent::__construct($mime, $charset);
        
        // Add body
        $this->addNode($this->dom, "methodResponse");       
    }
       
    /**
     * Render view and store in document's body
     * 
     * This method is invoked by the views and renders the ouput data in the
     * document specific format.
     * 
     * @param PHPFrame_MVC_View $view        The view object to process.
     * @param bool              $apply_theme Boolean to insicate whether we want to apply 
     *                                       the overall theme or not.
     * 
     * @access public
     * @return void
     * @since  1.0
     * @todo It is very important to check path used for require_once call for security.
     */
    public function render(PHPFrame_MVC_View $view, $apply_theme=null) 
    {
    	//adding test data
    	PHPFrame::Session()->getSysevents()->setSummary('FAKE INFO','info');
    	
    	$event_summaries = PHPFrame::Session()->getSysevents()->asArray();
		if (!empty($event_summaries['summary']))
		{
			$error = false;
			foreach($event_summaries as $summary)
			{
				if ($summary[0] == 'error')
				{
					$error = true;
					break;
				}
			}
			if ($error) 
			{
				$this->_makeFaultPayload(5,$summary[1]);
			}
			else
			{
				$view->addData('messages',$event_summaries);
				$this->_makeParamPayload($view->getData());
			}
			unset($summary);
		}
		else
		{
			$this->_makeParamPayload($view->getData());
		}
    }
    
	/**
     * Make Fault Payload
     * 
     * This method is used to build an XML-RPC Falult Response with given faultCode and description
     * 
     * @param int $fault_code The fault code.
     * @param string $fault_string The fault description.
     * 
     * @access private
     * @since  1.0
     * @return string An XML-RPC methodResponse structure with Fault node.
     */
    private function _makeFaultPayload($fault_code, $fault_string)
    {
    	$parent_node = $this->dom->getElementsByTagName("methodResponse")->item(0);
		$parent_node = $this->addNode($parent_node,'fault');
		$parent_node = $this->addNode($parent_node,'value');
		$fault_array = array('faultCode'=>$fault_code,'faultString'=>$fault_string);
		$parent_node = $this->_addStruct($parent_node,$fault_array);
    }
    
	/**
     * Make Param Payload
     * 
     * This method is used to build an XML-RPC Param Response from an array
     * <commit_msg>RPC Document now deals with system events correctly<commit_after><?php
/**
 * PHPFrame/Document/RPC.php
 * 
 * PHP version 5
 * 
 * @category   MVC_Framework
 * @package    PHPFrame
 * @subpackage Document
 * @author     <NAME> <<EMAIL>>
 * @copyright  2009 E-noise.com Limited
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    SVN: $Id$
 * @link       http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 */

/**
 * RPC Document Class
 * 
 * @category   MVC_Framework
 * @package    PHPFrame
 * @subpackage Document
 * @author     <NAME> <<EMAIL>>
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link       http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @see        PHPFrame_Document
 * @since      1.0
 */
class PHPFrame_Document_RPC extends PHPFrame_Document_XML
{
    /**
     * Constructor
     * 
     * @access public
     * @return void
     * @uses   DOMImplementation, PHPFrame_Utils_URI, PHPFrame_Application_Pathway
     * @since  1.0 
     */
    public function __construct($mime="text/xml", $charset=null) 
    {
        // Call parent's constructor to set mime type
        parent::__construct($mime, $charset);
        
        // Add body
        $this->addNode($this->dom, "methodResponse");       
    }
       
    /**
     * Render view and store in document's body
     * 
     * This method is invoked by the views and renders the ouput data in the
     * document specific format.
     * 
     * @param PHPFrame_MVC_View $view        The view object to process.
     * @param bool              $apply_theme Boolean to insicate whether we want to apply 
     *                                       the overall theme or not.
     * 
     * @access public
     * @return void
     * @since  1.0
     * @todo It is very important to check path used for require_once call for security.
     */
    public function render(PHPFrame_MVC_View $view, $apply_theme=null) 
    {
    	$events = PHPFrame::Session()->getSysevents()->asArray();
    	
    	if (
    		is_array($events)
    		&& isset($events['summary'])
    		&& count($events['summary'])>1
    	){
    		$summary = $events['summary'];
    		if ($summary[0] == 'error')
    		{
    			$this->_makeFaultPayload(5,$summary[1]);
    		}
    		else
    		{
    			$view->addData('sysevents',$summary);
				$this->_makeParamPayload($view->getData());	
    		}
    	}
    	else
    	{
    		$this->_makeParamPayload($view->getData());
    	} 
    }
    
	/**
     * Make Fault Payload
     * 
     * This method is used to build an XML-RPC Falult Response with given faultCode and description
     * 
     * @param int $fault_code The fault code.
     * @param string $fault_string The fault description.
     * 
     * @access private
     * @since  1.0
     * @return string An XML-RPC methodResponse structure with Fault node.
     */
    private function _makeFaultPayload($fault_code, $fault_string)
    {
    	$parent_node = $this->dom->getElementsByTagName("methodResponse")->item(0);
		$parent_node = $this->addNode($parent_node,'fault');
		$parent_node = $this->addNode($parent_node,'value');
		$fault_array = array('faultCode'=>$fault_code,'faultString'=>$fault_string);
		$parent_node = $this->_addStruct($parent_node,$fault_array);
    }
    
	/**
     * Make Param Payload
     * 
     * This method is used to build an XML-RPC Param Response from an array
     * 
{'fraction_non_alphanumeric': 0.12507138777841234, 'fraction_numerical': 0.004997144488863506, 'mean_word_length': 2.44903988183161, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4306937', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2170, 'n_words': 511}