244a245,267
>      $fields = _simplenews_get_profile_fields();
>   if (!empty($fields)) {
>     $profile_fields = '<br />'. t('Available user profile fields:');
>     $profile_field_substitution = array();
>     $c = 0;
>     $cc = count($fields);
>     foreach ($fields as $name => $title) {
>       $profile_field_substitution['@'. $name] = $title;
>       $profile_fields .= ' @'. $name .' ('. $title .')'. ($c < $cc - 1 ? ',' : '.');
>       ++$c;
>     }
>   }
>   
>   //Personalizing newsletter
>   //different description depending on token.module
>   $description = t('This will be the body of your newsletter. Available variables are:') . ' %site ' . t('(the name of your website),') . ' %uri ' . t('(a link to your homepage),') . ' %uri_brief ' . t('(homepage link without the http://),') . ' %mymail ' . t('(your e-mail address),') . ' %date ' . t('(today\'s date),') . ' %login_uri ' . t('(link to login page).') . (isset($profile_fields) ? $profile_fields : '');
>   
>   //implement token user
>   //TODO: [reg-since] and [log-since] don't return days, they return years and weeks
>   if (module_exists('token')) {
>     $description .= ' Also [user] ' . t('(User\'s name),') . ' [uid] ' . t('(User\'s ID),') . ' [mail] ' . t('(User\'s email address),') . ' [reg-date] ' . t('(User\'s registration date),') . ' [reg-since] ' . t('(Days since the user registered),') . ' [log-date] ' . t('(User\'s last login date),') . ' [log-since] ' . t('(Days since the user\'s last login),') . ' [date-in-tz] ' . t('(The current date in the user\'s timezone),') . ' [account-url] ' . t('(The URL of the user\'s profile page.),') . ' [account-edit] ' . t('(The URL of the user\'s account editing page.).');
>   }     
>   //end of Personalizing newsletter
253c276
<       '#description' => t('This will be the body of your newsletter. Available variables are:') . ' %site ' . t('(the name of your website),') . ' %uri ' . t('(a link to your homepage),') . ' %uri_brief ' . t('(homepage link without the http://),') . ' %mymail ' . t('(your e-mail address),') . ' %date ' . t('(today\'s date),') . ' %login_uri ' . t('(link to login page).'),
---
>       '#description' => $description,  //for Personalizing newsletter
495a519
>   global $user;
496a521,537
>   if (!isset($node->simplenews_prepare_mail)) {
>   // Replace user profile variables upon node view with data from current user.
>   if (!$teaser) {
>     $node->body = simplenews_replace_profile_vars($node->body, $user);
>   }
>   else {
>     $node->teaser = simplenews_replace_profile_vars($node->teaser, $user);
>   }
> }
>   //Personalizing newsletter
>   //only replaced token when not send at all subscribers, otherwise all newsletters have the same values.
>   //TODO: better solution for _POST['send']
>   if ((module_exists('token')) AND ($_POST['send'] != 1)) {
>     $node->body = token_replace($node->body, 'user', $user);
>   }
>   //end of Personalizing newsletter*/
> 
1156a1198,1199
>   // Prevent user profile replacements in hook_view().
>   $node->simplenews_prepare_mail = 1;
1320c1363
<  *   An object with at least $mail->to, $mail->subject, and $mail->message.
---
>  *   An object with at least $mail->to, $mail->subject, and $mail->body.
1322a1366,1375
>   // Replace user profile variables.
>   $mail->body = simplenews_replace_profile_vars($mail->body, $mail->to);
> 
>   //Personalizing newsletter
>   if (module_exists('token')) {
>     $account = user_load(array('mail' => $mail->to));
>     $mail->body = token_replace($mail->body, 'user', $account);
>   }
>   //end of Personalizing newsletter
>   
2686a2740,2788
> /**
>  * Replace user profile fields in newsletter text.
>  * 
>  * @param string $body
>  *   A newsletter text to process.
>  * @param string $user
>  *   Either an user object or an email address for user profile information
>  *   substitution.
>  */
> function simplenews_replace_profile_vars($text, $user) {
>   if (!is_object($user)) {
>     $user = _simplenews_user_load($user);
>   }
>   if (module_exists('profile')) {
>     profile_load_profile($user);
>     $fields = _simplenews_get_profile_fields();
>     foreach ($fields as $name => $title) {
>       if (isset($user->$name)) {
>         $profile_field_substitution['@'. $name] = $user->$name;
>       }
>       else {
>         $profile_field_substitution['@'. $name] = '';
>       }
>     }
>     $text = strtr($text, $profile_field_substitution);
>   }
>   
>   return $text;
> }
> 
> /**
>  * Helper function to fetch user profile fields.
>  */
> function _simplenews_get_profile_fields() {
>   $fields = array();
>   if (module_exists('profile')) {
>     $results = db_query("SELECT name, title FROM {profile_fields} ORDER BY category, weight");
>     
>     while ($row = db_fetch_object($results)) { 
>       // Don't include private fields.
>       if ($row->visibility != PROFILE_PRIVATE) {
>         $fields[$row->name] = $row->title;
>       }
>     }
>   }
>   
>   return $fields;
> }
> 
