Advertisement
DrupalCustom

JonAttemp

Feb 17th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.90 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Testing some ideas about heirarchical forms.
  6.  */
  7.  
  8. /**
  9.  * Instance of hook_menu().
  10.  */
  11. function test_menu() {
  12.   $items = array();
  13.  
  14.   // Our little test form.
  15.   $items['test/select'] = array(
  16.     'title' => t('Source / Target'),
  17.     'description' => t('Choose a source and a target'),
  18.     'page callback' => 'drupal_get_form',
  19.     'page arguments' => array('test_source_target'),
  20.     'access arguments' => array('access content'),
  21.     'type' => MENU_CALLBACK,
  22.   );
  23.  
  24.   return $items;
  25. }
  26.  
  27. // Select a source and a target.
  28. function test_source_target($form, &$form_state) {
  29.   $form = array();
  30.  
  31.   global $finalFunctionUserName;
  32.   global $finalFunctionPassword;  
  33.   global $ticket;
  34.    
  35.   $arrayForSendTranslation = array();
  36.   $arrayForSendTranslation = formModCredentialProvider_test();
  37.  
  38.   $finalFunctionUserName = $arrayForSendTranslation[0];
  39.   $finalFunctionPassword = $arrayForSendTranslation[1];
  40.  
  41.  
  42.   $LoginClient = new SoapClient("https://freeway.demo.lionbridge.com/vojo/FreewayAuth.asmx?wsdl", array("trace" => 1));
  43.   $ServicesLink = new SoapClient("https://freeway.demo.lionbridge.com/vojo/Service.asmx?wsdl", array("trace" => 1));
  44.  
  45.   try {
  46.     $arrResponse = $LoginClient->Logon(array('Username' => $finalFunctionUserName, 'Password' => $finalFunctionPassword));
  47.  
  48.     $ticket = ($arrResponse->LogonResult);
  49.     $getSrcLang = $ServicesLink->GetSourceLanguages(array('Ticket' => $ticket));
  50.  
  51.     //$array has the list of source languages.
  52.     $array = array();
  53.     $arrayTarget = array();
  54.     $forTarLang = array();
  55.    
  56.     foreach ($getSrcLang->GetSourceLanguagesResult->Languages->Language as $language) {
  57.       $array[$language->ID] = $language->Description . "_" . $language->ID;
  58.       $forTarLang[] = $language->ID;
  59.     }
  60.    
  61.  
  62.   }
  63.   catch (SoapFault $exception) {
  64.     drupal_get_messages();
  65.     drupal_set_message("The following exception took place.1" . $exception . "Kindly check Freeway mapping");
  66.     drupal_goto("user");
  67.   }
  68.  
  69.  
  70.  
  71.  
  72.   // Because this is a test we have some hard coded values.
  73.   // The sources.
  74.   $source = array(
  75.     'cat' => t('Cat'),
  76.     'dog' => t('Dog'),
  77.     'reptile' => t('Reptile'),
  78.   );
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.   // If we have a value for the first dropdown from $form_state['values'] we use
  86.   // this both as the default value for the first dropdown and also as a
  87.   // parameter to pass to the function that retrieves the options for the
  88.   // second dropdown.
  89.   $selected = isset($form_state['values']['source']) ? $form_state['values']['source'] : key($array);
  90.  
  91.   $form['source'] = array(
  92.     '#type' => 'select',
  93.     '#title' => t('Source'),
  94.     '#options' => $array,
  95.     '#default_value' => $selected,
  96.     '#ajax' => array(
  97.       'callback' => 'test_source_target_callback',
  98.       'wrapper' => 'dropdown-second-replace',
  99.     ),
  100.   );
  101.  
  102.   $form['target'] = array(
  103.     '#type' => 'select',
  104.     '#title' => $options_first[$selected] . ' ' . t('Instruments'),
  105.     '#prefix' => '<div id="dropdown-second-replace">',
  106.     '#suffix' => '</div>',
  107.     '#multiple' => true,
  108.     '#options' => _get_target($selected),
  109.     '#default_value' => isset($form_state['values']['target']) ? $form_state['values']['target'] : '',
  110.   );
  111.  
  112.   $form['submit'] = array(
  113.     '#type' => 'submit',
  114.     '#value' => t('Submit'),
  115.   );
  116.  
  117.   return $form;
  118. }
  119.  
  120. /**
  121.  * Selects just the second dropdown to be returned for re-rendering
  122.  */
  123. function test_source_target_callback($form, $form_state) {
  124.   return $form['target'];
  125. }
  126.  
  127. // Fetch the target.
  128. function _get_target($selected) {
  129.   watchdog('test', $selected);
  130.  
  131.   global $finalFunctionUserName;
  132.   global $finalFunctionPassword;  
  133.  
  134.   $arrayT = array();
  135.  
  136.   $LoginClient = new SoapClient("https://freeway.demo.lionbridge.com/vojo/FreewayAuth.asmx?wsdl", array("trace" => 1));
  137.   $ServicesLink = new SoapClient("https://freeway.demo.lionbridge.com/vojo/Service.asmx?wsdl", array("trace" => 1));
  138.   try {
  139.  
  140.     $arrResponse = $LoginClient->Logon(array('Username' => $finalFunctionUserName, 'Password' => $finalFunctionPassword));
  141.     $ticket = ($arrResponse->LogonResult);
  142.     $getTarLang = $ServicesLink->GetTargetLanguages(array('Ticket' => $ticket, 'SourceLanguageID' => $selected));
  143.      
  144.       foreach ($getTarLang->GetTargetLanguagesResult->Languages->Language as $languageT) {
  145.         $arrayT[$languageT->ID] = $languageT->Description . "_" . $languageT->ID;
  146.       }
  147.             drupal_get_messages();
  148.             drupal_set_message("Here check the arr length ".count($arrayT));
  149.             drupal_goto("user");
  150.    
  151.     }
  152.    
  153.  
  154.    
  155.    
  156.    
  157. catch (SoapFault $exception) {
  158.     drupal_get_messages();
  159.     drupal_set_message("The following exception took place." . $exception . "Kindly check Freeway mapping");
  160.     drupal_goto("user");
  161.   }
  162.  
  163.  
  164.   // The targets.
  165. /*
  166.  $target = array(
  167.     'cat' => array(
  168.       'house_cat' => 'House cat',
  169.       'lion' => 'Lion',
  170.       'Leopard' => 'Leopard',
  171.       'cheetah' => 'Cheetah',
  172.     ),
  173.     'dog' => array(
  174.       'alsatian' => 'Alsatian',
  175.       'collie' => 'Collie',
  176.     ),
  177.     'reptile' => array(
  178.       'lizard' => 'Lizard',
  179.       't_rex' => 'T-Rex',
  180.       'tortoise' => 'Tortoise',
  181.     ),
  182.   );
  183.  
  184.   */
  185.  $target =  $arrayT;
  186.  
  187.  
  188.   return $target[$selected];
  189. }
  190.  
  191.  
  192. function formModCredentialProvider_test() {
  193.  
  194.   $arrayWithCredentials = array();
  195.   global $user;
  196.   /*
  197.  
  198.    1.Get the current mode.
  199.    1.1. If curr mode = 0 ;
  200.    Navigate to a page saying "Kindly use user mapping options to provide Freeway Credentials"
  201.    1.2  If curr mode = 1 ;
  202.    Select freeway uname and pwrd from global table and set them to the variables.
  203.    if uname = '' or pwrd = '' ; -> navigate to page saying "The Freway credentials have not been entered
  204.    correctly in tha mapping page"
  205.    else set the variable with the value obtained.
  206.  
  207.    1.3      else if curr mode = 2;
  208.    Get currently logged in user name.
  209.    Select freeway uname and pwrd from non global table where drupal username = 'currently logged in user name'
  210.    if uname = '' or pwrd = '' ; -> navigate to page saying "The Freway credentials have not been entered
  211.    correctly in tha mapping page"
  212.    else set the variable with the value obtained.
  213.  
  214.    */
  215.  
  216.   $resultUsPw = db_query("SELECT mode_id FROM {freeway_mode} where sr_id = 1");
  217. //  $rowsUsPw = mysql_num_rows($resultUsPw);
  218.   $rowsUsPw = $resultUsPw->rowCount();
  219.  
  220.   if ($rowsUsPw == 0) {
  221.     drupal_get_messages();
  222.     drupal_set_message("The Freeway user mapping for Drupal users has not been completed. Kindly associate
  223.         Drupal users with Freeway credentials");
  224.     drupal_goto('user/' . $user->uid . '/freewayUserMapping');
  225.   }
  226.  
  227.   else {
  228.     //while ($recordUsPw  = db_fetch_object($resultUsPw))
  229.     foreach($resultUsPw as $recordUsPw )
  230.     {
  231.       $modeValueUsPw = $recordUsPw->mode_id;
  232.     }
  233.  
  234.     if ($modeValueUsPw == '0') {
  235.       drupal_get_messages();
  236.       drupal_set_message("Kindly use user mapping options to provide Freeway Credentials");
  237.       drupal_goto('user/' . $user->uid . '/freewayUserMapping');
  238.     }
  239.     else if ($modeValueUsPw == '1') {
  240.  
  241.       $globalResult = db_query("SELECT freeway_global_username,freeway_global_password FROM {freeway_global_user} where sr_id = 1");
  242.       //$rowsGlobalUsPw = mysql_num_rows($globalResult);
  243.      
  244.       $rowsGlobalUsPw = $globalResult->rowCount();
  245.       if ($rowsGlobalUsPw == 0) {
  246.         drupal_get_messages();
  247.         drupal_set_message("There is no Freeway credential mapped for the Global user role logged ");
  248.         drupal_goto('user/' . $user->uid . '/freewayGlobalUser');
  249.       }
  250.       else {
  251. //        while ($recordUsPwGl  = db_fetch_object($globalResult))
  252.         foreach($globalResult as $recordUsPwGl)
  253.         {
  254.           $freewayGlobalUserName = $recordUsPwGl->freeway_global_username;
  255.           $freewayGlobalPassword = $recordUsPwGl->freeway_global_password;
  256.  
  257.           if ((strlen($freewayGlobalUserName) == '0') || (strlen($freewayGlobalPassword) == '0')) {
  258.             drupal_get_messages();
  259.             drupal_set_message("Kindly use user mapping options to provide Freeway Credentials for the Global user");
  260.             drupal_goto('user/' . $user->uid . '/freewayGlobalUser');
  261.           }
  262.           else {
  263.             $finalUserName = $freewayGlobalUserName;
  264.             $finalPassword = $freewayGlobalPassword;
  265.           }
  266.  
  267.         }
  268.       }
  269.     }
  270.  
  271.     else if ($modeValueUsPw == '2') {
  272.       $userNameCurr = $user->name;
  273.       // TODO Please convert this statement to the D7 database API syntax.
  274.       $nonGlobalResult = db_query("SELECT freeway_username ,freeway_password  FROM {freeway_users} where drupal_username = '$userNameCurr'");
  275.  
  276.     //  $rowsNonGlobalUsPw = mysql_num_rows($nonGlobalResult);
  277.         $rowsNonGlobalUsPw = $nonGlobalResult->rowCount();
  278.      
  279.       if ($rowsNonGlobalUsPw == 0) {
  280.         drupal_get_messages();
  281.         drupal_set_message("There are no Freeway credentials mapped for the currently logged in user " . $userNameCurr . ".");
  282.         drupal_goto('user/' . $user->uid . '/freewayNonGlobalUser');
  283.       }
  284.       else {
  285.         //while ($recordUsPwNonGl  = db_fetch_object($nonGlobalResult))
  286.         foreach ($nonGlobalResult as $recordUsPwNonGl )
  287.         {
  288.           $freewayNonGlobalUserName = $recordUsPwNonGl->freeway_username;
  289.           $freewayNonGlobalPassword = $recordUsPwNonGl->freeway_password;
  290.  
  291.           if ((strlen($freewayNonGlobalUserName) == '0') || (strlen($freewayNonGlobalPassword) == '0')) {
  292.             drupal_get_messages();
  293.             drupal_set_message("There are no Freeway credentials mapped for the currently logged in user " . $userNameCurr . ".");
  294.             drupal_goto('user/' . $user->uid . '/freewayNonGlobalUser');
  295.           }
  296.           else {
  297.             $finalUserName = $freewayNonGlobalUserName;
  298.             $finalPassword = $freewayNonGlobalPassword;
  299.  
  300.           }
  301.         }
  302.  
  303.       }
  304.  
  305.     }
  306.  
  307.  
  308.  
  309.  
  310.   }
  311.  
  312.   $arrayWithCredentials[0] = $finalUserName;
  313.   $arrayWithCredentials[1] = $finalPassword;
  314.  
  315.   return $arrayWithCredentials;
  316.  
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement