/**
  * Uploads a batch file to the rig client. The user must already been to be
  * assigned to a rig.
  * <br />
  * Unlike the other functions in this controller, the response is not JSON but
  * a text string with the format:
  * <ul>
  *  <li>true - Succeeding uploading and invoking batch control.</li>
  *  <li>false; &lt;error reason;&gt; - Failed uploading or invoking batch
  *  control with a provided reason.</li>
  * </ul>
  */
 public function torigclientAction()
 {
     $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' => 'Batch control request', 'reason' => 'not in session'));
         echo "error; Not in session.";
         return;
     }
     $adapter = new Zend_File_Transfer_Adapter_Http();
     $dest = $this->_config->upload->dir;
     if (!$dest) {
         $this->_logger->error("Batch download directory not configured, 'upload.dir' property.");
         throw new Exception("Batch download directory not configured.");
     }
     $adapter->setDestination($dest)->addValidator('Count', false, 1);
     /* Size file validator. */
     if ($size = $this->_config->upload->size) {
         $adapter->addValidator('FilesSize', false, $size);
     }
     /* Extension file validator. */
     if ($ext = $this->_config->upload->extension) {
         $adapter->addValidator('Extension', false, $ext);
     }
     if (!$adapter->receive()) {
         $error = 'File validation has failed.';
         foreach ($adapter->getMessages() as $k => $v) {
             switch ($k) {
                 case 'fileExtensionFalse':
                     $error .= ' The file extension was incorrect.';
                     break;
                 case 'fileUploadErrorIniSize':
                 case 'fileUploadErrorFormSize':
                     $error .= ' The file size was too large.';
                     break;
                 default:
                     $error .= ' ' . $v;
                     break;
             }
         }
         echo "error; {$error}";
         return;
     }
     $file = $adapter->getFileName();
     list($ns, $name) = explode(':', $this->_auth->getIdentity());
     $request = array('requestor' => $name, 'fileName' => basename($file), 'batchFile' => file_get_contents($file));
     if (!$request['batchFile']) {
         $this->_logger->warn("Failed to read batch file {$file}.");
         echo 'false; Upload to read batch file.';
         return;
     }
     unlink($file);
     try {
         $rigClient = new Sahara_Soap($response->contactURL . '?wsdl');
         header('Content-Type', 'text/plain');
         $response = $rigClient->performBatchControl($request);
         echo $response->success ? 'true' : 'false; ' . $response->error->reason;
     } catch (Exception $ex) {
         $this->_logger->error("Soap error calling batch 'performPrimitiveControl'. Message: " . $ex->getMessage() . ', code: ' . $ex->getCode() . '.');
         echo 'false; ' . $ex->getMessage();
     }
 }