Advertisement
vishnu3006

zipname

Nov 29th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.43 KB | None | 0 0
  1. <?php
  2.  
  3. function formModifications_menu() {
  4.  
  5.   $items['translate/bulk'] = array(
  6.     'title' => t('Send Multiple Contents'),
  7.     'description' => t('Translate multiple nodes'),
  8.     'page callback' => 'drupal_get_form',
  9.     'page arguments' => array('formModifications_translate_bulk'),
  10.     'access arguments' => array('access translator'),
  11.   );
  12.    
  13.    return $items;
  14. }
  15.  
  16.  
  17. function formModifications_translate_bulk  (){
  18.   // Get the nodes.
  19.   $nodes_ser = trim($_GET['nodes']);
  20.   $nodes = explode("-",$nodes_ser);
  21.  
  22.  // print_r($nodes);
  23.     $form = array();
  24.  
  25. }
  26.  
  27.  
  28.  
  29. function formModifications_form_alter(&$form,&$form_state,$form_id){
  30.  
  31.                 if ($form_id == 'node_admin_content') {
  32.                     $form['admin']['options']['operation']['#options']['send_for_translation'] = t('Send For Translation');
  33.                     $form['admin']['options']['submit']['#submit'][] = 'custom_send_for_translation_submit_admin';
  34.                 }
  35. }
  36.  
  37. function custom_send_for_translation_submit_admin($form, &$form_state) {
  38. $zipName = "Content";  
  39. $nodes = array();                                                                                                                                            
  40.               foreach ($form_state['values']['nodes'] as $key => $value)
  41.               {
  42.                      if ($value != 0) {
  43.                               $selected[] = node_load($key);
  44.                        }
  45.               }
  46.              
  47.  
  48.                  
  49.        foreach ($selected as $key => $value ){
  50.              $structure = './sites/all/tempfolder/';
  51.                                                                        
  52.                                                                                    
  53.               if(!is_dir($structure))
  54.               {
  55.                  mkdir($structure, 0, true);
  56.             }
  57.                                                                                                                                                                      
  58.           $fileName = $value->title;                                          
  59.           $sPattern = '/\s*/m';
  60.           $sReplace = '';
  61.           $refinedFileName = preg_replace( $sPattern, $sReplace, $fileName ).".xml";
  62.           $finalRefinedFileName = $structure.$refinedFileName;                                                                                    
  63.           $ourFileHandle = fopen($finalRefinedFileName, 'w') or die("can't open file");
  64.           $stringXmlHead = '<?xml version="1.0"?>'."\r\n";
  65.           fwrite($ourFileHandle, $stringXmlHead);
  66.           $rootNode = "<DrupalConnector>"."\r\n";  
  67.           fwrite($ourFileHandle, $rootNode);
  68.           $nodeIdTagStart = "<NodeId>";
  69.           fwrite($ourFileHandle, $nodeIdTagStart);
  70.           $nodeId = $value->nid;
  71.           fwrite($ourFileHandle, $nodeId);
  72.           $nodeIdTagEnd = "</NodeId>"."\r\n";
  73.           fwrite($ourFileHandle, $nodeIdTagEnd);
  74.           $nodeTitleTagStart = "<NodeTitle>";
  75.           fwrite($ourFileHandle, $nodeTitleTagStart);
  76.           $nodeTitle = "<![CDATA[".$value->title."]]>";
  77.           fwrite($ourFileHandle, $nodeTitle);
  78.           $nodeTitleTagEnd = "</NodeTitle>"."\r\n";
  79.           fwrite($ourFileHandle, $nodeTitleTagEnd);
  80.           $nodeBodyTagStart = "<NodeBody>";
  81.           fwrite($ourFileHandle, $nodeBodyTagStart);
  82.           $nodeBody = "<![CDATA[".$value->body."]]>";
  83.           fwrite($ourFileHandle, $nodeBody);
  84.           $nodeBodyTagEnd = "</NodeBody>"."\r\n";
  85.           fwrite($ourFileHandle, $nodeBodyTagEnd);
  86.           $rootNodeEnd = "</DrupalConnector>"."\r\n";
  87.           fwrite($ourFileHandle, $rootNodeEnd);
  88.          
  89.           $zipName.= "_".$nodeId;
  90.          
  91.          
  92.           $nodes []= $value->nid;
  93.    }
  94.    fclose($ourFileHandle);
  95.    
  96.    print_r($zipName);
  97.    
  98.   // compress("./sites/all/tempfolder");
  99.    Zip('./sites/all/tempfolder','./compressed.zip');
  100.    
  101.     $nodes_ser = implode('-', $nodes); 
  102.     drupal_goto('translate/bulk', 'nodes='. $nodes_ser);
  103.  
  104. }
  105.  
  106.  
  107. /*
  108. function recurse_zip($src,&$zip,$path_length) {
  109.         $dir = opendir($src);
  110.         while(false !== ( $file = readdir($dir)) ) {
  111.             if (( $file != '.' ) && ( $file != '..' )) {
  112.                 if ( is_dir($src . '/' . $file) ) {
  113.                     recurse_zip($src . '/' . $file,$zip,$path_length);
  114.                 }
  115.                 else {
  116.                     $zip->addFile($src . '/' . $file,substr($src . '/' . $file,$path_length));
  117.                 }
  118.             }
  119.         }
  120.         closedir($dir);
  121. }
  122.  
  123.  
  124. //Call this function with argument = absolute path of file or directory name.
  125. function compress($src)
  126. {
  127.         if(substr($src,-1)==='/')
  128.         {$src=substr($src,0,-1);}
  129.         $arr_src=explode('/',$src);
  130.         $filename=end($src);
  131.         unset($arr_src[count($arr_src)-1]);
  132.         $path_length=strlen(implode('/',$arr_src).'/');
  133.         $f=explode('.',$filename);
  134.         $filename=$f[0];
  135.         $filename=(($filename=='')? 'backup.zip' : $filename.'.zip');
  136.         $zip = new ZipArchive;
  137.         $res = $zip->open($filename, ZipArchive::CREATE);
  138.         if($res !== TRUE){
  139.                 echo 'Error: Unable to create zip file';
  140.                 exit;}
  141.         if(is_file($src)){
  142.         $zip->addFile($src,substr($src,$path_length));
  143.         }
  144.         else{
  145.                 if(!is_dir($src)){
  146.                      $zip->close();
  147.                      @unlink($filename);
  148.                      echo 'Error: File not found';
  149.                      exit;}
  150.         recurse_zip($src,$zip,$path_length);
  151.         }
  152.         $zip->close();
  153.         header("Location: $filename");
  154.         exit;
  155. }
  156. */
  157.  
  158. function Zip($source, $destination)
  159.  {  
  160.  if (!extension_loaded('zip') || !file_exists($source))
  161.      {      
  162.      return false;  
  163.      }  
  164.  
  165.  $zip = new ZipArchive();  
  166.  if (!$zip->open($destination, ZIPARCHIVE::CREATE))
  167.      {      
  168.      return false;
  169.      }    
  170.  
  171.  $source = str_replace('\\', '/', realpath($source));  
  172.  if (is_dir($source) === true)  
  173.  {    
  174.          $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
  175.          foreach ($files as $file)
  176.          {          
  177.              $file = str_replace('\\', '/', realpath($file));
  178.              if (is_dir($file) === true)  
  179.              {              
  180.              $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));  
  181.              }          
  182.              else if (is_file($file) === true)    
  183.              {        
  184.              $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));  
  185.              }  
  186.          }    
  187.  }
  188.  
  189.  else if (is_file($source) === true)  
  190.          {    
  191.          $zip->addFromString(basename($source), file_get_contents($source));
  192.          }  
  193.  return $zip->close();
  194.  }
  195.  
  196.  
  197.  
  198.  
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement