0

I am working on a module written by another developer; the module shows pray times and I have to add the option for people to select a month and year and show pray times for that month. All is going fine, but I am not sure I understand how the form for this module was set up because when I try to use variable_get(praytimes_month), it never returns anything, even after submitting the form.

Right now, what I have works, but I can't set the default month because the only way for me to get something into my variable is through the cookies.

function praytimes_user_settings_form($form, &$form_state) {
      if ((variable_get('praytimes_userselect') == 'no')){

      $form['overview'] = array(
        '#markup' => t('Access denied!'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      );

        return $form;
      }

      if(!isset($_COOKIE['Drupal_visitor_praytimes_address']))
      {
        $address = variable_get('praytimes_address');
        $tz = variable_get('praytimes_tz');
        $cmonth = variable_get('prayertimes_month',date("m"));
        $cyear = variable_get('prayertimes_year',date("Y"));
      }
      else {

        $address = $_COOKIE['Drupal_visitor_praytimes_address'];
        $tz = $_COOKIE['Drupal_visitor_praytimes_tz'];
        $cmonth = $_COOKIE['Drupal_visitor_praytimes_month'];
        $cyear = $_COOKIE['Drupal_visitor_praytimes_year'];

      }

      $form['overview'] = array(
        '#markup' => t('This page allows user to configure City and Time Zone'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      );

      $form['praytimes_address'] = array(
        '#title' => t('Address'),
        '#description' => t('City, State, Contry or zipcode e.g: Dearborn, MI, USA or 48126'),
        '#type' => 'textfield',
        '#default_value' => $address,
        '#required' => TRUE,
      );


      $form['praytimes_tz'] = array(
        '#title' => t('Time Zone'),
        '#description' => t('Enter the time zone(e.g. Eastern is -5, Central is -6, Mountain is -7 and Pacific is -8).'),
        '#type' => 'textfield',
        '#default_value' => $tz,
        '#required' => TRUE,
      );
        $form['praytimes_month'] = array(
        '#title' => t('Month'),
        '#description' => t('Please enter a month in number form.'),
        '#type' => 'textfield',
        '#default_value' => $cmonth,
      );
        $form['praytimes_year'] = array(
        '#title' => t('Year'),
        '#description' => t('Please enter a Year.'),
        '#type' => 'textfield',
        '#default_value' => $cyear,
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
      );

      return $form;
    }

    /**
     * Process a validated Pray Times user setting submission.
     */
    function praytimes_user_settings_form_submit($form, &$form_state) {
      //Rebuild the form.

      $form_state['redirect'] = 'prayertimes/timetable';

      $address = $form_state['values']['praytimes_address'];
      $tz = $form_state['values']['praytimes_tz'];
      $cmonth = $form_state['values']['praytimes_month'];
      $cyear = $form_state['values']['praytimes_year'];
      $coord = getLatLong($address);
      $lat = $coord['lat'];
      $long = $coord['long'];


      user_cookie_save(array(
        'praytimes_month' => $cmonth,
        'praytimes_year' => $cyear,
        'praytimes_address'=> $address, 
        'praytimes_tz' => $tz,
        'praytimes_lat' => $lat,
        'praytimes_long' => $long,));

      // Notify user.
      drupal_set_message(t('City and Time Zone settings has been saved.'));
    }

    function praytimes_table (){
        $output = '';

        if(!isset($_COOKIE['Drupal_visitor_praytimes_address'])){
            $address = variable_get('praytimes_address');
            $cmonth = variable_get('prayertimes_month');
            $cyear = variable_get('prayertimes_year');
            $tz = variable_get('praytimes_tz');
            $lat = variable_get('praytimes_lat');
            $long = variable_get('praytimes_long');
          }
          else {
            $cmonth = $_COOKIE['Drupal_visitor_praytimes_month'];
            $cyear = $_COOKIE['Drupal_visitor_praytimes_year'];
            $address = $_COOKIE['Drupal_visitor_praytimes_address'];
            $tz = $_COOKIE['Drupal_visitor_praytimes_tz'];
            $lat = $_COOKIE['Drupal_visitor_praytimes_lat'];
            $long = $_COOKIE['Drupal_visitor_praytimes_long'];    
          }

        $dst = variable_get('praytimes_dst');
        $today = mktime(0,0,0,$cmonth,1,$cyear);
        $currentmonth = date('F', $today);
        $currentyear = date('Y', $today);
    //  $tempdate = strtotime($currentmonth.'-1-'.$currentyear);
        $tempdate = $today;
        $oneday = 24*60*60

What I want is storing the submitted month/year to $tempdate and, if nothing was submitted, have a default value.

1 Answer 1

1

variable_get('praytimes_month') doesn't return any value because you aren't saving any value with variable_set('praytimes_month', $cmonth) which you should put where there is now the user_cookie_save() call.

There is a more important mistake in your code, though: The values saved with variable_save() and obtained from variable_get() are the same for every user. This means that a user will see as default value the value submitted from the last user who used that form; users will never see the values they entered earlier, if not in case the last user who used that form entered the same values.

That is why the other developer used cookies: Because they at least return the last values entered using the same browser on the same computer.

What I would use to store data is:

  • If the users don't have an account on the site, in the session data ($_SESSION)

  • If the users have an account on the site, in a custom database table containing the user ID, and the data to store

In this way, each user will see as default values the value they entered earlier. In the case of using $_SESSION, there is still a chance a user would see what the previous user entered, since the session is opened for the browser used on a computer.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.