Advertisement
DrupalCustom

LKD

Jan 30th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.24 KB | None | 0 0
  1. function formModifications_menu() {
  2.  
  3.   $items['translate/bulk'] = array(
  4.     'title' => t('Send Multiple Contents'),
  5.     'description' => t('Translate multiple nodes'),
  6.     'page callback' => 'drupal_get_form',
  7.     'page arguments' => array('formModifications_translate_bulk'),
  8.     'access arguments' => array('access translator'),
  9.   );
  10.    
  11.    return $items;
  12. }
  13.  
  14.  
  15. function formModifications_translate_bulk(){
  16. $arrayForSendTranslation = array();
  17. $arrayForSendTranslation = formModCredentialProvider();
  18.  
  19. /*
  20. for($u=0;$u<count($arrayForSendTranslation);$u+=1){
  21. echo ($arrayForSendTranslation[$u]);
  22. echo ('<br>');
  23. }
  24. */
  25. $finalFunctionUserName = $arrayForSendTranslation[0];
  26. $finalFunctionPassword = $arrayForSendTranslation[1];
  27.  
  28.  
  29.  
  30.  
  31.  drupal_add_js(drupal_get_path('module', 'formModifications') .'/js/script.js');
  32.       // Get the nodes.
  33.       $chosenContents = "Contents Chosen :";
  34.       $nodes_ser = trim($_GET['nodes']);
  35.       $nodes = explode("-",$nodes_ser);
  36.       $form = array();
  37.       $tarLangId = "Langs:";
  38.        
  39.          
  40.      foreach( $nodes as $key => $value){
  41.             $node = node_load($value); 
  42.             $chosenContents .= $node->title." , ";
  43.             }
  44.  
  45.          $finalString = rtrim($chosenContents, ' , ');
  46.            
  47.     $form['content_information'] = array(
  48.         '#value' => variable_get('content_form_information', $finalString),
  49.         );
  50.  
  51.        
  52.             $LoginClient = new SoapClient("https://freeway.demo.lionbridge.com/vojo/FreewayAuth.asmx?wsdl", array("trace"=>1));
  53.             $ServicesLink = new SoapClient("https://freeway.demo.lionbridge.com/vojo/Service.asmx?wsdl", array("trace"=>1));
  54.  
  55.  
  56.               try{
  57.              
  58.               $arrResponse = $LoginClient->Logon(array ('Username'=>$finalFunctionUserName,'Password'=>$finalFunctionPassword));
  59.               $ticket = ($arrResponse->LogonResult);
  60.               $getSrcLang = $ServicesLink->GetSourceLanguages(array('Ticket'=>$ticket));
  61.               $getDraftProjectIds = $ServicesLink->GetProjectSummariesList(array('Ticket'=>$ticket,'NumberOfProjects'=>100,'SortOrder'=>MostRecent,'ProjectStatusCode'=>'Draft'));
  62.                      
  63.                             $array = array();
  64.                             $arrayT = array();
  65.                             $forTarLang = array();
  66.                             $listOfProjects = array();
  67.                             $hopeArray = array();
  68.                            
  69.                             foreach($getSrcLang->GetSourceLanguagesResult->Languages->Language as $language)               
  70.                             {                                      
  71.                               $array[$language->ID] = $language->Description."_".$language->ID;                                
  72.                                 $forTarLang[] = $language->ID;
  73.                             }
  74.  
  75.                    
  76.                             foreach($getDraftProjectIds->GetProjectSummariesListResult->ProjectSummaries->ProjectSummary as $projectSummary)
  77.                             {
  78.                               $listOfProjects[$projectSummary->ID] = $projectSummary-> ID;
  79.                             }
  80.                                            
  81.                                                    
  82.                                                  
  83.               }
  84.                   catch (SoapFault $exception){
  85.             drupal_set_message("The following exception took place.".$exception."Kindly check Freeway mapping");
  86.             drupal_goto("user");
  87.                  
  88.             //    return $exception;
  89.                   }
  90.  
  91.                           try{
  92.                          
  93.                           if (count($forTarLang)!=0){
  94.                                 $getTarLang = $ServicesLink->GetTargetLanguages(array('Ticket'=>$ticket,'SourceLanguageID'=>$forTarLang[0]));
  95.                                        
  96.                                 foreach($getTarLang->GetTargetLanguagesResult->Languages->Language as $languageT)  
  97.                                     {                  
  98.                                     //$arrayT[$languageT->ID] = $languageT->Description;
  99.                                     $arrayT[$languageT->ID] = $languageT->Description."_".$languageT->ID;
  100.                                     }                              
  101.                             }
  102.                          }
  103.                           catch (Exception  $exception1){
  104.                          
  105.             drupal_set_message("The following exception took place.".$exception1);
  106.             drupal_goto("user");
  107.                          //  return $exception1;
  108.                           }
  109.  
  110.  
  111.  
  112.  
  113.         $form['folks_list']=array(
  114.                 '#type'=>'select',
  115.                 '#title' => t('Freeway Source Languages'),
  116.                 '#options' => $array,
  117.                 '#default_value' => ('English (Canada)_en-ca'),
  118.                 '#attributes'=> array('onselect' => "populateTargetlangs();"),
  119.                 '#weight'=>7,
  120.             );
  121.            
  122.            
  123.            
  124.  
  125. // listbox for target language.
  126.  
  127.            
  128.  
  129.    
  130.             $form['targetLang_list']=array(
  131.                 '#type'=>'select',
  132.                 '#title' => t('Freeway Target Languages'),
  133.                 '#options' => $arrayT,
  134.                 '#multiple' => true,
  135.                 '#default_value' => $arrayT,
  136.                 '#attributes'=>array('size'=>4, 'class' => 'select_folks_list'),
  137.                
  138.                 '#weight'=>8,
  139.             );
  140.        
  141.               $form['projectId'] = array(
  142.               '#type' => 'select',
  143.               '#title' => t('List of Draft projects created by you.'),
  144.                '#weight'=>9,
  145.               '#options' => $listOfProjects,
  146.             ); 
  147.                    
  148.               $form['translate_node'] = array(
  149.               '#type' => 'submit',
  150.               '#value' => 'Send To Freeway',
  151.               '#weight'=>10,
  152.               );   
  153.                    
  154.                    
  155.                        
  156.              return $form;
  157.    
  158. }
  159.  
  160.  
  161.  
  162.  
  163. function formModifications_translate_bulk_submit($form, &$form_state){
  164. global $base_root;
  165.  
  166. $arrayForSendTranslation = array();
  167. $arrayForSendTranslation = formModCredentialProvider();
  168. /*
  169. for($u=0;$u<count($arrayForSendTranslation);$u+=1){
  170. echo ($arrayForSendTranslation[$u]);
  171. echo ('<br>');
  172. }
  173. */
  174. $finalFunctionUserName = $arrayForSendTranslation[0];
  175. $finalFunctionPassword = $arrayForSendTranslation[1];
  176.  
  177.  
  178.  
  179.  
  180.  $targetLangs = array();
  181.  $finalTarArray = array();
  182.  $zipName = "Content";  
  183.  $targetLangNames ='';
  184.  // Obtain Src Lang,TargetLanguage(s) and Project ID inside this function.
  185. $projectID = isset($form_state['values']['projectId']) ? $form_state['values']['projectId'] : key($options_first);
  186. $srcLang = isset($form_state['values']['folks_list']) ? $form_state['values']['folks_list'] : key($options_first);
  187. $targetLangs = isset($form_state['values']['targetLang_list']) ? $form_state['values']['targetLang_list'] : key($options_first);
  188.  
  189. /*
  190. echo ('#EEE>>'.($base_root . request_uri()));  
  191. echo('TARLANGS >>'.count($targetLangs));
  192. echo ('<br>');
  193. for($t=0;$t<count($targetLangs);$t+=1)
  194. {
  195. echo('TarLangs'.$targetLangs[$t]);
  196. }
  197.  
  198. echo ('<br>');
  199. if (isset($srcLang)){
  200. echo('SRCLANG ISSET>'.$srcLang);
  201. }
  202.  
  203. */
  204. if (count($targetLangs)==0){
  205.  drupal_get_messages();
  206.  drupal_set_message("Please select appropriate Freeway Target Languages.");
  207.  drupal_goto($base_root . request_uri());
  208.  
  209. }
  210.  
  211.  
  212. foreach ($targetLangs as $langs) {
  213. list($name,$tarId) = split('[_]', $langs);
  214. $finalTarArray[] = $tarId;
  215. }
  216.  
  217. /*
  218. for($t=0;$t<count($finalTarArray);$t+=1)
  219. {
  220. echo('TarLangs>'.$finalTarArray[$t]);
  221. }
  222. */
  223.  
  224.  
  225. // Block to Add task:// Add task to he project - can be hardcoded,Add File and submit.
  226.  $LoginClient = new SoapClient("https://freeway.demo.lionbridge.com/vojo/FreewayAuth.asmx?wsdl", array("trace"=>1));
  227.      $ServicesLink = new SoapClient("https://freeway.demo.lionbridge.com/vojo/Service.asmx?wsdl", array("trace"=>1));
  228.  
  229.      
  230.      if ($handle = opendir('./')) {
  231.             while (false !== ($entry = readdir($handle))) {
  232.                if(substr($entry, -3)== "zip")
  233.                {
  234.                $fileName = $entry;           
  235.                }       
  236.             }
  237.             //echo("FileName > ".$fileName);
  238.             //echo('<br>');
  239.         closedir($handle);
  240.  
  241. }
  242.      
  243.             try
  244.             {
  245.                 $arrResponse = $LoginClient->Logon(array ('Username'=>$finalFunctionUserName,'Password'=>$finalFunctionPassword));
  246.                 $ticket = ($arrResponse->LogonResult);     
  247.                    
  248.                
  249.                 for($j=0;$j<count($finalTarArray);$j+=1){
  250.                     $addTask = $ServicesLink->AddTaskToProject(array('Ticket'=>$ticket,'ProjectID'=>$projectID ,'SourceLanguageID'=> $srcLang, 'TargetLanguageID'=> $finalTarArray[$j],'ComponentID'=> '','SubjectID'=>'','TaskID'=>'LP','SubTaskID'=>'New Words','Volume'=>'1','UOMID'=>'Word')); 
  251.  
  252.                                
  253.                 }
  254.                
  255.                
  256.                 for($t=0;$t<count($finalTarArray);$t+=1){
  257.                
  258.                 $targetLangNames = $targetLangNames.$finalTarArray[$t].',';
  259.                
  260.                 }
  261.                 $interString = rtrim($targetLangNames,',');
  262.                 $finalString = rtrim($interString,',');
  263.                            
  264.                    $structureTemp = './sites/all/tempfolder/';
  265.                    $listInTemp = getFileList($structureTemp);
  266.                    
  267.                     for($v=0;$v<count($listInTemp);$v+=1)
  268.                       {
  269.                         $content=simplexml_load_file($listInTemp[$v]);
  270.                         $content->TarLang = $finalString;
  271.                         $content->SrcLang = $srcLang;
  272.                         $content->asXML($listInTemp[$v]);
  273.                       }
  274.                    
  275.                    
  276.                     $finalZipName = "DrupalContent.zip";
  277.                     Zip('./sites/all/tempfolder','./'.$finalZipName);
  278.                    
  279.                     $handle = fopen("./".$finalZipName, "r");
  280.                     $contents = fread($handle, filesize("./".$finalZipName));      
  281.                     fclose($handle);
  282.            
  283.                 $addFile = $ServicesLink->AddFileToProject(array('Ticket'=>$ticket,'ProjectID'=>$projectID,'Filename'=>$finalZipName,'FileData'=> $contents, 'SourceLanguageID'=>  $srcLang, 'TargetLanguageIDs'=> $finalTarArray,'Metadata'=> null,'IsReferenceMaterial'=>false));
  284.                
  285.                 $fileinfoArray = array();
  286.                 foreach($addFile->AddFileToProjectResult->FileInfoItems->FileInfo as $fileInfo)
  287.                 {
  288.                 $fileinfoArray[] = $fileInfo;
  289.                 }
  290.                
  291.                 $bottomFileId = $fileinfoArray[0];
  292.                 $topFileId = ($bottomFileId - (count($finalTarArray)-1));  
  293.                 for($e=0;$e<count($finalTarArray);$e+=1){
  294.  
  295.                 db_query("INSERT INTO {freeway_fileid} (freeway_file_id, freeway_tar_lang) VALUES ('$topFileId','$finalTarArray[$e]')");               
  296.                 $topFileId +=1;
  297.                
  298.                 }
  299.                 $topFileId = 0;
  300.  
  301.                 $submitProject = $ServicesLink->SubmitProject(array('Ticket'=>$ticket,'ProjectID'=>$projectID));
  302.            
  303.            
  304.             }      
  305.            
  306.            
  307.            
  308.             catch(SoapFault $exception)
  309.             {    
  310.         drupal_set_message("The following exception took place.".$exception."Kindly check Freeway mapping");
  311.             drupal_goto("user");           
  312.              //  return $exception;
  313.             }
  314.  
  315. // End of Block to add task.
  316.  
  317.  
  318. //Block for deletion of zip and tempfolder.
  319.  
  320. unlink('./'.$finalZipName);
  321. rrmdir('./sites/all/tempfolder');
  322. // End of Block for Deletions.
  323.  
  324.  
  325.  
  326.     drupal_set_message("Project with project ID ".$projectID." submitted to Freeway");
  327.     drupal_goto("user");
  328. //  drupal_goto("admin/settings/freeway");
  329.            
  330. }
  331.  
  332.  
  333.  
  334. function formModifications_form_alter(&$form,&$form_state,$form_id){
  335.  
  336.                 if ($form_id == 'node_admin_content') {
  337.                     $form['admin']['options']['operation']['#options']['send_for_translation'] = t('Send For Translation');
  338.                     $form['admin']['options']['submit']['#submit'][] = 'custom_send_for_translation_submit_admin';
  339.                 }
  340. }
  341.  
  342. function custom_send_for_translation_submit_admin($form, &$form_state) {
  343. $zipName = "Content";  
  344. $nodes = array();
  345.  $structurePreClean = './sites/all/tempfolder/';
  346.  if(is_dir($structurePreClean)) {
  347.  rrmdir('./sites/all/tempfolder');
  348. }
  349.  
  350.  
  351.          
  352.               foreach ($form_state['values']['nodes'] as $key => $value)
  353.               {
  354.                      if ($value != 0) {
  355.                               $selected[] = node_load($key);
  356.                        }
  357.  
  358.               }
  359.              
  360.               if (count($selected)==0){
  361.               drupal_get_messages();
  362.               drupal_set_message("No contents selected!");
  363.               drupal_goto('admin/content/node');
  364.              
  365.               }
  366.              
  367.  
  368.                  
  369.        foreach ($selected as $key => $value ){
  370.              $structure = './sites/all/tempfolder/';
  371.                                                                        
  372.                                                                                    
  373.               if(!is_dir($structure))
  374.               {
  375.                  mkdir($structure, 0, true);
  376.              }
  377.              //Added jan19,2012
  378.              /*
  379.              else{
  380.              rrmdir('./sites/all/tempfolder');
  381.              
  382.              }
  383.                */                                                                                                                                                      
  384.           $fileName = $value->title;                                          
  385.           $sPattern = '/\s*/m';
  386.           $sReplace = '';
  387.           $refinedFileName = preg_replace( $sPattern, $sReplace, $fileName ).".xml";
  388.           $finalRefinedFileName = $structure.$refinedFileName;                                                                                    
  389.           $ourFileHandle = fopen($finalRefinedFileName, 'w') or die("can't open file");
  390.           $stringXmlHead = '<?xml version="1.0"?>'."\r\n";
  391.           fwrite($ourFileHandle, $stringXmlHead);
  392.           $rootNode = "<DrupalConnector>"."\r\n";  
  393.           fwrite($ourFileHandle, $rootNode);
  394.                  
  395.           $nodeIdTagStart = "<NodeId>";
  396.           fwrite($ourFileHandle, $nodeIdTagStart);
  397.           $nodeId = $value->nid;
  398.           fwrite($ourFileHandle, $nodeId);
  399.           $nodeIdTagEnd = "</NodeId>"."\r\n";
  400.           fwrite($ourFileHandle, $nodeIdTagEnd);
  401.          
  402.           $drupalVerTagStart = "<DrupalVer>";
  403.           fwrite($ourFileHandle, $drupalVerTagStart);
  404.           $drupalVer= '6.0';
  405.           fwrite($ourFileHandle, $drupalVer);
  406.           $drupalVerTagEnd = "</DrupalVer>"."\r\n";
  407.           fwrite($ourFileHandle, $drupalVerTagEnd);
  408.          
  409.           $connectorVerTagStart = "<ConnectorVer>";
  410.           fwrite($ourFileHandle, $connectorVerTagStart);
  411.           $conectorVer= '1.0';
  412.           fwrite($ourFileHandle, $conectorVer);
  413.           $connectorVerTagEnd = "</ConnectorVer>"."\r\n";
  414.           fwrite($ourFileHandle, $connectorVerTagEnd);
  415.          
  416.          
  417.           $srcLangTagStart = "<SrcLang>";
  418.           fwrite($ourFileHandle, $srcLangTagStart);
  419.           $srcLang= '';
  420.           fwrite($ourFileHandle, $srcLang);
  421.           $srcLangTagEnd = "</SrcLang>"."\r\n";
  422.           fwrite($ourFileHandle, $srcLangTagEnd);
  423.          
  424.           $tarLangTagStart = "<TarLang>";
  425.           fwrite($ourFileHandle, $tarLangTagStart);
  426.           $tarLang= '';
  427.           fwrite($ourFileHandle, $tarLang);
  428.           $tarLangTagEnd = "</TarLang>"."\r\n";
  429.           fwrite($ourFileHandle, $tarLangTagEnd);
  430.          
  431.           $timeTagStart = "<TimeOfSend>";
  432.           fwrite($ourFileHandle, $timeTagStart);
  433.           $time= date("Y-m-d H:i:s");
  434.           fwrite($ourFileHandle, $time);
  435.           $timeTagEnd = "</TimeOfSend>"."\r\n";
  436.           fwrite($ourFileHandle, $timeTagEnd);
  437.          
  438.            
  439.           $nodeTitleTagStart = "<NodeTitle>";
  440.           fwrite($ourFileHandle, $nodeTitleTagStart);
  441.           $nodeTitle = "<![CDATA[".$value->title."]]>";
  442.           fwrite($ourFileHandle, $nodeTitle);
  443.           $nodeTitleTagEnd = "</NodeTitle>"."\r\n";
  444.           fwrite($ourFileHandle, $nodeTitleTagEnd);
  445.           $nodeBodyTagStart = "<NodeBody>";
  446.           fwrite($ourFileHandle, $nodeBodyTagStart);
  447.           $nodeBody = "<![CDATA[".$value->body."]]>";
  448.           fwrite($ourFileHandle, $nodeBody);
  449.           $nodeBodyTagEnd = "</NodeBody>"."\r\n";
  450.           fwrite($ourFileHandle, $nodeBodyTagEnd);
  451.           $rootNodeEnd = "</DrupalConnector>"."\r\n";
  452.           fwrite($ourFileHandle, $rootNodeEnd);
  453.          
  454.           $zipName.= "_".$nodeId;
  455.          
  456.          
  457.           $nodes []= $value->nid;
  458.    }
  459.    fclose($ourFileHandle);
  460.    
  461.   $finalZipName = $zipName.".zip";
  462.    
  463.   // Zip('./sites/all/tempfolder','./'.$finalZipName);
  464.     //zipping done
  465.     $nodes_ser = implode('-', $nodes); 
  466.     drupal_goto('translate/bulk', 'nodes='. $nodes_ser);
  467.  
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement