- // The offer edit form
- function my_module_edit_offer_form($form, &$form_state, $id) {
- // DEBUG drupal_set_message("Edit offer $id");
- // Get the values from the database
- $name = db_query("SELECT name FROM {offer} WHERE id = $id")->fetchField();
- $mail = db_query("SELECT mail FROM {offer} WHERE id = $id")->fetchField();
- $phone = db_query("SELECT phone FROM {offer} WHERE id = $id")->fetchField();
- $compname = db_query("SELECT compname FROM {offer} WHERE id = $id")->fetchField();
- $comptype = db_query("SELECT comptype FROM {offer} WHERE id = $id")->fetchField();
- $companies = db_query("SELECT companies FROM {offer} WHERE id = $id")->fetchField();
- $message = db_query("SELECT message FROM {offer} WHERE id = $id")->fetchField();
- // Show the result
- // Static
- $form['contact'] = array(
- '#title' => 'Kontaktinformation',
- '#type' => 'fieldset',
- );
- $form['contact']['name'] = array(
- '#markup' => "Namn: $name <br>",
- );
- $form['contact']['compname'] = array(
- '#markup' => "Företagsnamn: $compname <br>",
- );
- $form['contact']['comptype'] = array(
- '#markup' => "Företagstyp: $comptype <br>",
- );
- $form['contact']['mail'] = array(
- '#markup' => "Mail: $mail <br>",
- );
- $form['contact']['phone'] = array(
- '#markup' => "Telefon: $phone <br>",
- );
- // Editable
- $form['msg'] = array(
- '#title' => 'Meddelande',
- '#type' => 'fieldset',
- );
- $form['msg']['id'] = array(
- '#type' => 'hidden',
- '#value' => $id
- );
- $form['msg']['message'] = array(
- '#type' => 'textarea',
- '#default_value' => $message,
- '#cols' => 50,
- '#rows' => 20,
- '#resizable' => TRUE
- );
- // submit button
- $form['msg']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit')
- );
- // Start company list here
- // Load the offers that need to be displayed.
- $header = array(
- 'name' => array('data' => 'Company', 'field' => 'u.name'),
- 'mail' => array('data' => t('Mail'), 'field' => 'u.mail'),
- );
- // Get the offers from the db
- $query = db_select('users', 'u');
- $query->condition('u.uid', 0, '<>');
- user_build_filter_query($query);
- // Set up query
- $count_query = clone $query;
- $count_query->addExpression('COUNT(u.uid)');
- $query = $query->extend('PagerDefault')->extend('TableSort');
- $query
- ->fields('u', array('uid', 'name', 'mail'))
- ->limit(5000)
- ->orderByHeader($header);
- $result = $query->execute();
- // Build a table listing the companies in branschguiden
- $options = array();
- $roles = array_map('check_plain', user_roles(TRUE));
- $accounts = array();
- // Print each company
- foreach($result as $account) {
- // get the role of each account
- $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
- foreach ($roles_result as $user_role) {
- $users_roles = $roles[$user_role->rid];
- // drupal_set_message('<pre>' . print_r($users_roles, TRUE)); // Debug
- // show only branschguiden companies
- if ($users_roles == 'companies') {
- $options[$account->uid] = array(
- 'name' => $account->name,
- 'mail' => $account->mail
- );
- } // end only branscguiden companies
- } // end foreach role_result
- } // End foreach account
- // set up the array of default values in tableselect
- $uids = array();
- $companies = 'mail1@test.org,test2@test.org';
- $comp_mail = explode(',', $companies);
- foreach ($comp_mail as $mail) {
- // drupal_set_message('<pre>' . print_r($mail, TRUE)); // Debug
- $uids[] = db_query("SELECT uid FROM {users} WHERE mail = '$mail'")->fetchField();
- }
- drupal_set_message('<pre>' . print_r($uids, TRUE)); // Debug
- drupal_set_message('<pre>' . print_r($options, TRUE)); // Debug
- $form['msg']['accounts'] = array(
- '#type' => 'tableselect',
- '#header' => $header,
- '#options' => $options,
- '#default_value' => $uids,
- '#empty' => 'No companies',
- );
- // submit button
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit')
- );
- return $form;
- }