Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 17th, 2012  |  syntax: None  |  size: 3.99 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // The offer edit form
  2. function my_module_edit_offer_form($form, &$form_state, $id) {
  3.         // DEBUG drupal_set_message("Edit offer $id");
  4.  
  5.         // Get the values from the database
  6.         $name = db_query("SELECT name FROM {offer} WHERE id = $id")->fetchField();
  7.         $mail = db_query("SELECT mail FROM {offer} WHERE id = $id")->fetchField();
  8.         $phone = db_query("SELECT phone FROM {offer} WHERE id = $id")->fetchField();
  9.         $compname = db_query("SELECT compname FROM {offer} WHERE id = $id")->fetchField();
  10.         $comptype = db_query("SELECT comptype FROM {offer} WHERE id = $id")->fetchField();
  11.         $companies = db_query("SELECT companies FROM {offer} WHERE id = $id")->fetchField();
  12.         $message = db_query("SELECT message FROM {offer} WHERE id = $id")->fetchField();
  13.  
  14.         // Show the result
  15.  
  16.         // Static
  17.         $form['contact'] = array(
  18.                 '#title' => 'Kontaktinformation',
  19.                 '#type' => 'fieldset',
  20.         );
  21.         $form['contact']['name'] = array(
  22.                 '#markup' => "Namn: $name <br>",
  23.         );
  24.         $form['contact']['compname'] = array(
  25.                 '#markup' => "Företagsnamn: $compname <br>",
  26.         );
  27.         $form['contact']['comptype'] = array(
  28.                 '#markup' => "Företagstyp: $comptype <br>",
  29.         );
  30.         $form['contact']['mail'] = array(
  31.                 '#markup' => "Mail: $mail <br>",
  32.         );
  33.         $form['contact']['phone'] = array(
  34.                 '#markup' => "Telefon: $phone <br>",
  35.         );
  36.  
  37.         // Editable
  38.         $form['msg'] = array(
  39.                 '#title' => 'Meddelande',
  40.                 '#type' => 'fieldset',
  41.         );
  42.         $form['msg']['id'] = array(
  43.                 '#type' => 'hidden',
  44.                 '#value' => $id
  45.         );
  46.         $form['msg']['message'] = array(
  47.                 '#type' => 'textarea',
  48.                 '#default_value' => $message,
  49.                 '#cols' => 50,
  50.                 '#rows' => 20,
  51.                 '#resizable' => TRUE
  52.         );
  53.  
  54.         // submit button
  55.         $form['msg']['submit'] = array(
  56.                 '#type' => 'submit',
  57.                 '#value' => t('Submit')
  58.         );
  59.  
  60.   // Start company list here
  61.  
  62.   // Load the offers that need to be displayed.
  63.   $header = array(
  64.     'name' => array('data' => 'Company', 'field' => 'u.name'),
  65.     'mail' => array('data' => t('Mail'), 'field' => 'u.mail'),
  66.   );
  67.  
  68.   // Get the offers from the db
  69.   $query = db_select('users', 'u');
  70.   $query->condition('u.uid', 0, '<>');
  71.   user_build_filter_query($query);
  72.  
  73.   // Set up query
  74.   $count_query = clone $query;
  75.   $count_query->addExpression('COUNT(u.uid)');
  76.   $query = $query->extend('PagerDefault')->extend('TableSort');
  77.   $query
  78.     ->fields('u', array('uid', 'name', 'mail'))
  79.     ->limit(5000)
  80.     ->orderByHeader($header);
  81.   $result = $query->execute();
  82.  
  83.   // Build a table listing the companies in branschguiden
  84.   $options = array();
  85.   $roles = array_map('check_plain', user_roles(TRUE));
  86.   $accounts = array();
  87.  
  88.   // Print each company
  89.   foreach($result as $account) {
  90.     // get the role of each account
  91.     $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
  92.     foreach ($roles_result as $user_role) {
  93.       $users_roles = $roles[$user_role->rid];
  94. //      drupal_set_message('<pre>' . print_r($users_roles, TRUE)); // Debug
  95.  
  96.         // show only branschguiden companies
  97.         if ($users_roles == 'companies') {
  98.  
  99.             $options[$account->uid] = array(
  100.               'name' => $account->name,
  101.               'mail' => $account->mail
  102.             );
  103.  
  104.             } // end only branscguiden companies
  105.         } // end foreach role_result
  106.   } // End foreach account
  107.  
  108.         // set up the array of default values in tableselect
  109.         $uids = array();
  110.         $companies = 'mail1@test.org,test2@test.org';
  111.         $comp_mail = explode(',', $companies);
  112.         foreach ($comp_mail as $mail) {
  113. //              drupal_set_message('<pre>' . print_r($mail, TRUE)); // Debug
  114.                 $uids[] = db_query("SELECT uid FROM {users} WHERE mail = '$mail'")->fetchField();
  115.         }
  116.                 drupal_set_message('<pre>' . print_r($uids, TRUE)); // Debug
  117.                 drupal_set_message('<pre>' . print_r($options, TRUE)); // Debug
  118.  
  119.         $form['msg']['accounts'] = array(
  120.                 '#type' => 'tableselect',
  121.                 '#header' => $header,
  122.                 '#options' => $options,
  123.                 '#default_value' => $uids,
  124.                 '#empty' => 'No companies',
  125.         );
  126.  
  127.         // submit button
  128.         $form['submit'] = array(
  129.                 '#type' => 'submit',
  130.                 '#value' => t('Submit')
  131.         );
  132.  
  133.         return $form;
  134.  
  135. }