getContext(); $module = $context->getModuleName(); $document = $context->getActionName(); $actionInstance = $context->getActionStack()->getLastEntry()->getActionInstance(); $actionInstance->setLayout(false); // Getting information from app.yml $tempDir = sfConfig::get('app_sfOpenOfficePlugin_temp_dir', '/tmp'); $locale = sfConfig::get('app_sfOpenOfficePlugin_locale'); // Grab template vars if ($templateVars === null) { $templateVars = $actionInstance->getVarHolder()->getAll(); } $extension = $this->getFileExtension(); $documentPath = sfConfig::get('sf_app_module_dir') . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . sfConfig::get('sf_app_module_document_dir_name') . DIRECTORY_SEPARATOR . $document; if ( SF_ENVIRONMENT == 'dev' ) { if ( filemtime($documentPath) < filemtime($documentPath . '.' . $extension) ) { try { $this->php_cli = @sfToolkit::getPhpCli(); } catch (Exception $e) { throw new Exception("PHP is not in your basedir"); } if ( is_dir(sfConfig::get('sf_app_module_dir') . DIRECTORY_SEPARATOR . $module) ) { $app = sfConfig::get('sf_app'); } else { $pluginDirs = glob(sfConfig::get('sf_root_dir').'/plugins/*/modules/' . $module); if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $pluginDirs[0], $match)) { $app = $match[1]; } } $command = 'ooffice-create-document ' . $app . ' ' . $module . ' ' . $document . '.' . $extension; $command = sprintf('%s %s %s', $this->php_cli, SF_ROOT_DIR.'/symfony', $command); ob_start(); passthru($command, $return); $content = ob_get_clean(); } } $tempName = $document . time(); try { mkdir($tempDir . DIRECTORY_SEPARATOR . $tempName, 0700, true); } catch (Exception $e) { throw new Exception("Error creating $tempDir"); } $this->attributeHolder->add($this->getGlobalVars()); $this->attributeHolder->add($templateVars); // auto vars to be used in the templates // this helps to save dynamic resources in the right place // see sfOpenOfficeHelper $this->attributeHolder->add(array( 'sf_ooffice_out_dir' => $tempDir . DIRECTORY_SEPARATOR . $tempName . DIRECTORY_SEPARATOR )); // For each skeleton file process it as if it has PHP code // save it in a tempDir if ( is_dir($documentPath) ) { foreach($this->scandir_recursive($documentPath) as $input_filepath) { $output_filepath = $tempDir . DIRECTORY_SEPARATOR . $tempName . DIRECTORY_SEPARATOR . substr($input_filepath, strlen($documentPath)); if (is_file($input_filepath)) { if(substr($input_filepath, -3) == 'xml') { $output = $this->renderFile($input_filepath); @file_put_contents($output_filepath, $output); } else { //$context->getLogger()->info("{sfOpenOfficeView} copying $input_filepath to $output_filepath"); @copy($input_filepath, $output_filepath); } } else { @mkdir($output_filepath); } } } else { throw new Exception("You have to run 'symfony ooffice-create-document' task"); } // Lets zip the document chdir($tempDir . DIRECTORY_SEPARATOR . $tempName); exec('zip -r ../'. "$tempName.$extension" .' * > /dev/null 2> /dev/null'); $generatedPath = $tempDir . DIRECTORY_SEPARATOR . $tempName . '.' . $extension; if ( !file_exists($generatedPath) ) { throw new Exception("Error creating $extension file"); } // We have generated the document for openoffice, lets apply the macro // TODO error handling $outputExtension = $this->getOutputExtension(); $outputPath = $tempDir . DIRECTORY_SEPARATOR . $tempName . '.' .$outputExtension; $filters = sfConfig::get('app_sfOpenOfficePlugin_filters'); $headers = sfConfig::get('app_sfOpenOfficePlugin_headers'); $filter = $filters[$extension][$outputExtension]['name']; $header = array_merge($headers['all'], $headers[$outputExtension]); $ret = sfConfig::get('sf_plugin_ooffice_script').' '.sfConfig::get('sf_plugin_ooffice_config_dir') . " release \"$generatedPath\" \"$outputPath\" \"$filter\"" . ': ' . shell_exec(sfConfig::get('sf_plugin_ooffice_script') . ' ' . sfConfig::get('sf_plugin_ooffice_config_dir') . " release \"$generatedPath\" \"$outputPath\" \"$filter\""); if ( file_exists($outputPath) ) { foreach($header as $name => $value) { header("$name: $value"); } header('Content-Length: ' . filesize($outputPath)); readfile($outputPath); // we are done! so byebye // TODO Change this. It should let symfony continue the filters chain exit(0); } else { throw new Exception("There was an error generating $outputPath ($ret)"); } } private function scandir_recursive($path) { $list_ignore = array ('.','..','.svn','Thumbs.db','.DS_STORE'); if (!is_dir($path)) return 0; $list=array(); $directory = @opendir("$path"); // @-no error display while ($file= @readdir($directory)) { if (!in_array($file,$list_ignore)) { $f=$path."/".$file; $f=preg_replace('/(\/){2,}/','/',$f); //replace double slashes $list[]=$f; if(is_dir($f)) { $list = array_merge($list ,$this->scandir_recursive($f)); //RECURSIVE CALL } } } @closedir($directory); return $list ; } }