/**
  * Action to bridge a primitive call to the in session rigclient. If a
  * response parameter name is specified, its value is as a file to download.
  * If no response parameter is specifed, all the response paramters are
  * returned as a file in the format:
  *    name=value,name=value,...
  * <br />
  * The mandatory parameters are:
  * <ul>
  * 	<li>pc | primitiveController => The name of the primitive controller.</li>
  *  <lI>pa | primitiveAction => The name of the action to run on the specified
  *  controller.</li>
  * </ul>
  * The optional parameters are:
  * <ul>
  * 	<li>rp | responseParam => The name of the response pasrameter.</li>
  *  <li>mime => The mime type of returned file.</li>
  *  <li>fn | filename => The name of file (also forces file downlod)</li>
  *  <li>tf | transform => A transform to apply to the code. The transform
  *  options are:
  *    1) 'base64' - this base64 decodes the return value and should be used
  *    if the response is binary data.</li>
  * </ul>
  * Any other provided parameters are used as primitive request parameters.
  * <br />
  * If the called failed 'FAILED' is returned.
  */
 public function fileAction()
 {
     $this->_helper->viewRenderer->setNoRender();
     $this->_helper->layout()->disableLayout();
     $mime = $this->_config->primitive->file->mime;
     $response = Sahara_Soap::getSchedServerSessionClient()->getSessionInformation(array('userQName' => $this->_auth->getIdentity()));
     if (!$response->isInSession) {
         /* Not in session, so unable to determine the rig clients address. */
         echo 'FAILED';
         return;
     }
     /* Set up the correct object model. */
     list($junk, $allocUser) = explode(':', $this->_auth->getIdentity(), 2);
     $request = array('requestor' => $allocUser, 'param' => array());
     foreach ($this->_request->getParams() as $key => $val) {
         switch ($key) {
             case 'pc':
             case 'primitiveController':
                 $request['controller'] = $val;
                 break;
             case 'pa':
             case 'primitiveAction':
                 $request['action'] = $val;
                 break;
             case 'rp':
             case 'responseParam':
                 $responseParam = $val;
                 break;
                 /* MIME type of file. */
             /* MIME type of file. */
             case 'mime':
                 // Mime type
                 $mime = implode('/', explode('-', $val, 2));
                 break;
                 /* Filename of file (forces download). */
             /* Filename of file (forces download). */
             case 'fn':
             case 'downloadedname':
                 $filename = $val;
                 break;
                 /* Transform for string. */
             /* Transform for string. */
             case 'tf':
             case 'transform':
                 $transform = $val;
                 break;
                 /* These are Zend request parameters and irrelevant to the
                  * primitive call. */
             /* These are Zend request parameters and irrelevant to the
              * primitive call. */
             case 'controller':
             case 'action':
             case 'module':
                 break;
                 /* Parameters to provide to primitive call. */
             /* Parameters to provide to primitive call. */
             default:
                 $param = array('name' => $key, 'value' => $val);
                 array_push($request['param'], $param);
                 break;
         }
     }
     /* Set header about the response. */
     header("Content-Type: {$mime}");
     if (isset($filename)) {
         header("Content-disposition: attachment; filename={$filename}");
     }
     try {
         $rigClient = new Sahara_Soap($response->contactURL . '?wsdl');
         $response = $rigClient->performPrimitiveControl($request);
         if (!$response->success) {
             echo "FAILED " . $response->error->reason;
             return;
         }
         $response = $response->result;
         /* Return the specified response. */
         if ($responseParam) {
             if (isset($response->name) && $response->name == $responseParam) {
                 echo $this->_echoWithTransform($response->value, $transform);
                 return;
             } else {
                 foreach ($response as $r) {
                     if ($r->name == $responseParam) {
                         echo $this->_echoWithTransform($r->value, $transform);
                         return;
                     }
                 }
             }
             echo 'FAILED';
             return;
         }
         /** Return all the response parameters. */
         if (isset($response->name)) {
             echo $this->_echoWithTransform($response->value, $transform);
             return;
         }
         foreach ($response as $r) {
             $str .= $r->name . '=' . $r->value . ',';
         }
         if (isset($str)) {
             echo $this->_echoWithTransform(substr($str, 0, strlen($str) - 1), $transform);
             return;
         }
         echo 'FAILED';
     } catch (Exception $ex) {
         echo 'FAILED';
     }
 }
 /**
  * Action to bridge a primitive call to the in session rigclient. The
  * response is returned as a JSON string (either the response object or
  * a Zend fault).
  * <br />
  * The mandatory parameters are:
  * <ul>
  * 	<li>primitiveController => The name of the primitive controller.</li>
  *  <lI>primitiveAction => The name of the action to run on the specified controller.</li>
  * </ul>
  * Any other provided parameters are used as primitive request parameters.
  */
 public function primitivebridgeAction()
 {
     $this->_helper->viewRenderer->setNoRender();
     $this->_helper->layout()->disableLayout();
     $response = Sahara_Soap::getSchedServerSessionClient()->getSessionInformation(array('userQName' => $this->_auth->getIdentity()));
     if (!$response->isInSession) {
         /* Not in session, so unable to determine the rig clients address. */
         $error = array('success' => 'false', 'error' => array('code' => -1, 'operation' => 'Primitive bridge request', 'reason' => 'not in session'));
         echo $this->view->json($error);
         return;
     }
     /* Set up the correct object model. */
     list($junk, $allocUser) = explode(':', $this->_auth->getIdentity(), 2);
     $request = array('requestor' => $allocUser, 'param' => array());
     foreach ($this->_request->getParams() as $key => $val) {
         switch ($key) {
             case 'primitiveController':
                 $request['controller'] = $val;
                 break;
             case 'primitiveAction':
                 $request['action'] = $val;
                 break;
             case 'controller':
             case 'action':
             case 'module':
                 /* These are Zend request parameters and irrelevant to the
                  * primitive call. */
                 break;
             default:
                 $param = array('name' => $key, 'value' => $val);
                 array_push($request['param'], $param);
                 break;
         }
     }
     try {
         $rigClient = new Sahara_Soap($response->contactURL . '?wsdl');
         echo $this->view->json($rigClient->performPrimitiveControl($request));
     } catch (Exception $ex) {
         echo $this->view->json($ex);
     }
 }