Update of /cvs/scoop/scoop/lib
In directory sodium.sabren.com:/tmp/cvs-serv5844/lib

Modified Files:
	Scoop.pm 
Log Message:

New flexible user preferences stuff.

-janra



Index: Scoop.pm
===================================================================
RCS file: /cvs/scoop/scoop/lib/Scoop.pm,v
retrieving revision 1.136
retrieving revision 1.137
diff -C2 -d -r1.136 -r1.137
*** Scoop.pm	30 Jul 2004 08:12:54 -0000	1.136
--- Scoop.pm	4 Aug 2004 21:49:47 -0000	1.137
***************
*** 110,113 ****
--- 110,116 ----
  	$self->_set_perms();
  	
+ 	# User pref definitions
+ 	$self->_set_pref_items();
+ 
  	# User prefs, at long last!
  	$self->_set_prefs();
***************
*** 122,126 ****
  	$self->_load_box_data();
  	
! 	$self->_check_subscription();
  	
  	# Hotlist stuff
--- 125,129 ----
  	$self->_load_box_data();
  	
! #	$self->_check_subscription();
  	
  	# Hotlist stuff
***************
*** 475,478 ****
--- 478,509 ----
  }
  
+ sub _set_pref_items {
+ 	my $self = shift;
+ 
+ 	if (my $cached = $self->cache->fetch_data({resource => 'pref_items',
+ 							element => 'PREF_ITEMS'})) {
+ 		$self->{PREF_ITEMS} = $cached;
+ 		return $self;
+ 	}
+ 
+ 	warn "Reloading pref cache.\n" if $CACHE_DEBUG;
+ 	# get all the pref data
+ 	my ($rv, $sth) = $self->db_select({
+ 		WHAT => '*',
+ 		FROM => 'pref_items'});
+ 
+ 	# stick it in $self
+ 	while (my $pref = $sth->fetchrow_hashref()) {
+ 		$self->{PREF_ITEMS}->{$pref->{prefname}} = $pref;
+ 	}
+ 
+         # And also update the cache
+ 	$self->cache->cache_data({resource => 'pref_items',
+ 				element => 'PREF_ITEMS',
+ 				data => $self->{PREF_ITEMS}});
+ 
+ 	return $self;
+ }
+ 
  sub _set_boxes {
  	my $self = shift;
***************
*** 1084,1091 ****
  	warn "  (Scoop::_set_prefs) Before setting prefs!\n" if ($DEBUG);
  
- 	#foreach my $pname (keys %{$self->{prefs}}) {
- 	#	warn "  (Scoop::_set_prefs) Pref $pname is $self->{prefs}->{$pname}\n" if ($DEBUG);
- 	#}
- 	
  	delete $self->{prefs};
  	if ($force) {
--- 1115,1118 ----
***************
*** 1093,1144 ****
  	}
  	
- 	#my ($rv, $sth) = $self->db_select({
- 	#	WHAT => '*',
- 	#	FROM => 'userprefs',
- 	#	WHERE => qq|uid = $self->{UID}|});	
- 	
  	unless ($self->{USER_DATA_CACHE}->{$self->{UID}}) {
  		$self->user_data($self->{UID});
  	}
  	
- 	#while (my $p = $sth->fetchrow_hashref()) {
  	$self->{prefs} = $self->{USER_DATA_CACHE}->{$self->{UID}}->{prefs};
  	
! 	#$sth->finish;
  	
  	return $self;
  }
  
! sub _check_subscription {
! 	my $self = shift;
! 	# Do we even use subscriptions?
! 	return unless ($self->{UI}->{VARS}->{use_subscriptions});
! 	# Is this user a subscriber?
! 	return unless ($self->{prefs}->{subscriber} == 1);
! 	# Is the subscription expired?
! 	return unless ($self->{prefs}->{subscription_expire} < time);
! 	
! 	# Hmmm. Seems to be buggy. If we get here, check for absolutely sure
! 	my $time = time;
! 	my ($rv, $sth) = $self->db_select({
! 		WHAT => 'prefvalue',
! 		FROM => 'userprefs',
! 		WHERE => "uid = $self->{UID} AND prefname = 'subscription_expire'"
! 	});
! 	my $exp = $sth->fetchrow();
! 	$sth->finish();
! 	
! 	return unless ($exp < $time);
! 	
! 	# If we get here, then the subscription is expired. Remove it
! 	# from their prefs.
! 	($rv, $sth) = $self->db_delete({
! 		FROM  => 'userprefs',
! 		WHERE => "uid = $self->{UID} AND (prefname = 'subscriber' OR prefname = 'subscription_expire' OR prefname = 'showad')"
! 	});
! 	
! 	# and reset the prefs
! 	$self->_set_prefs(1);
! }
  	
  sub _set_cache {
--- 1120,1166 ----
  	}
  	
  	unless ($self->{USER_DATA_CACHE}->{$self->{UID}}) {
  		$self->user_data($self->{UID});
  	}
  	
  	$self->{prefs} = $self->{USER_DATA_CACHE}->{$self->{UID}}->{prefs};
  	
! 	$self->{TRUSTLEV} = '2' if ( $self->have_perm('super_mojo') );
  	
  	return $self;
  }
  
! ### shouldn't this check the subscriptions table, not the userprefs?
! ##sub _check_subscription {
! ##	my $self = shift;
! ##	# Do we even use subscriptions?
! ##	return unless ($self->{UI}->{VARS}->{use_subscriptions});
! ##	# Is this user a subscriber?
! ##	return unless ($self->{prefs}->{subscriber} == 1);
! ##	# Is the subscription expired?
! ##	return unless ($self->{prefs}->{subscription_expire} < time);
! ##	
! ##	# Hmmm. Seems to be buggy. If we get here, check for absolutely sure
! ##	my $time = time;
! ##	my ($rv, $sth) = $self->db_select({
! ##		WHAT => 'prefvalue',
! ##		FROM => 'userprefs',
! ##		WHERE => "uid = $self->{UID} AND prefname = 'subscription_expire'"
! ##	});
! ##	my $exp = $sth->fetchrow();
! ##	$sth->finish();
! ##	
! ##	return unless ($exp < $time);
! ##	
! ##	# If we get here, then the subscription is expired. Remove it
! ##	# from their prefs.
! ##	($rv, $sth) = $self->db_delete({
! ##		FROM  => 'userprefs',
! ##		WHERE => "uid = $self->{UID} AND (prefname = 'subscriber' OR prefname = 'subscription_expire' OR prefname = 'showad')"
! ##	});
! ##	
! ##	# and reset the prefs
! ##	$self->_set_prefs(1);
! ##}
  	
  sub _set_cache {
***************
*** 1167,1204 ****
  	my $self = shift;
  	
! 	return unless ($self->{UID} && ($self->{UID} > 0) && $self->{prefs});
  
- 	# Reset a bunch of stuff w/user prefs, if they're there.
- 	if ($self->{prefs}->{imagedir}) {
- 		$self->{UI}->{VARS}->{imagedir} = $self->{prefs}->{imagedir};
- 	}
- 	
- 	if ($self->{prefs}->{maxstories}) {
- 		$self->{UI}->{VARS}->{maxstories} = $self->{prefs}->{maxstories};
- 		warn "  (Scoop::_update_pref_config) maxstories is $self->{UI}->{VARS}->{maxstories}" if ($DEBUG);
- 	}
  	
! 	if ($self->{prefs}->{maxtitles}) {
! 		$self->{UI}->{VARS}->{maxtitles} = $self->{prefs}->{maxtitles};
! 		warn "  (Scoop::_update_pref_config) maxtitles is $self->{UI}->{VARS}->{maxtitles}" if ($DEBUG);
! 	}
  	
! 	if ($self->{prefs}->{norm_font_face} || $self->{prefs}->{norm_font_size}) {
! 		my $size = $self->{UI}->{BLOCKS}->{norm_font_size};
! 		$size = $self->{prefs}->{norm_font_size} if ($self->{prefs}->{norm_font_size});
! 		
! 		my $face = $self->{UI}->{BLOCKS}->{norm_font_face};
! 		$face = $self->{prefs}->{norm_font_face} if ($self->{prefs}->{norm_font_face});
! 		
! 		$self->{UI}->{BLOCKS}->{norm_font_size} = $size;
! 		$self->{UI}->{BLOCKS}->{norm_font_face} = $face;
! 		
! 		$self->{UI}->{BLOCKS}->{norm_font} = qq|<FONT FACE="$face" SIZE="$size">|;
! 		$self->{UI}->{BLOCKS}->{title_font} =~ s/FACE=".*?"/FACE="$face"/i;
! 		$self->{UI}->{BLOCKS}->{box_title_font} =~ s/FACE=".*?"/FACE="$face"/i;
! 		warn "  (Scoop::_update_pref_config) norm_font is $self->{UI}->{BLOCKS}->{norm_font}" if ($DEBUG);
! 	}
! 
! 	warn "  (Scoop::_update_pref_config) Cache norm_font is $self->cache->{DATA}->{BLOCKS}->{norm_font}\n" if $DEBUG;
  	return;
  }
--- 1189,1203 ----
  	my $self = shift;
  	
! #	return unless ($self->{UID} && ($self->{UID} > 0) && $self->{prefs});
  
  	
! 	# Reset a bunch of stuff w/user prefs or the pref defaults
! 	$self->{UI}->{BLOCKS}->{norm_font_size} = $self->pref('norm_font_size');
! 	$self->{UI}->{BLOCKS}->{norm_font_face} = $self->pref('norm_font_face');
! 	$self->{UI}->{BLOCKS}->{maxstories} = $self->pref('maxstories');
! 	$self->{UI}->{BLOCKS}->{maxtitles} = $self->pref('maxtitles');
! 	$self->{UI}->{BLOCKS}->{imagedir} = $self->pref('imagedir');
  	
! #	warn "  (Scoop::_update_pref_config) Cache norm_font is $self->cache->{DATA}->{BLOCKS}->{norm_font}\n" if $DEBUG;
  	return;
  }
***************
*** 1272,1276 ****
  		my $user=$S->user_data($uid);
  		if ($user->{is_new_account}) {
- 			
  			# Run user_confirm hook
  			$S->run_hook('user_confirm', $user->{nickname});
--- 1271,1274 ----
***************
*** 1281,1286 ****
  				$S->param->{'op'}="user";
  				$S->param->{'tool'}="prefs";
  				# this is passed to the prefs page so it can display a welcome message
! 				$S->param->{'firstlogin'}=1;
  			}
  			warn "  (Scoop::check_for_login) First login for user ID <<$uid>>, setting is_new_account=0\n" if ($DEBUG);
--- 1279,1286 ----
  				$S->param->{'op'}="user";
  				$S->param->{'tool'}="prefs";
+ 				$S->param->{'uid'}=$uid;
+ 				$S->param->{'nick'}=$user->{nickname};
  				# this is passed to the prefs page so it can display a welcome message
! 				$S->param->{'firstlogin'}="1";
  			}
  			warn "  (Scoop::check_for_login) First login for user ID <<$uid>>, setting is_new_account=0\n" if ($DEBUG);
***************
*** 1675,1678 ****
--- 1675,1679 ----
  	while (my $u = $sth->fetchrow_hashref()) {
  		# Handle anonymous specially...
+ 		next unless $S->{PREF_ITEMS}->{$u->{prefname}}->{enabled};
  		$S->{USER_DATA_CACHE}->{$u->{uid}}->{prefs}->{$u->{prefname}} = $u->{prefvalue};
  	}
***************
*** 1871,1879 ****
  		# time zone and how many seconds the value will be offset from UTC
  		my ($offset, $offset_as_string);
! 		if ($S->{prefs}->{time_zone}) {
! 			$offset = &Time::Timezone::tz_offset(lc($S->{prefs}->{time_zone}));
! 		} else {
! 			$offset = &Time::Timezone::tz_offset($S->{UI}->{VARS}->{time_zone});
! 		}
  
  		# Create a string that describes the field's offset from UTC in the format
--- 1872,1876 ----
  		# time zone and how many seconds the value will be offset from UTC
  		my ($offset, $offset_as_string);
! 		$offset = &Time::Timezone::tz_offset(lc($S->pref('time_zone')));
  
  		# Create a string that describes the field's offset from UTC in the format
***************
*** 1934,1978 ****
  	
  	my $adjust_time = $fieldname;
! 	my $zone = uc($S->{UI}->{VARS}->{time_zone});
  	
! 	if ($S->{prefs}->{time_zone}) {
! 		warn "  (Scoop::date_format) Local zone is $S->{UI}->{VARS}->{time_zone}.\n" if ($DEBUG);
! 		warn "  (Scoop::date_format) User's zone is $S->{prefs}->{time_zone}.\n" if ($DEBUG);
! 
! 		$zone = $S->{prefs}->{time_zone};
  
! 		# get the difference in seconds between the Scoop server's time zone and UTC
! 		my $loc_offset = &Time::Timezone::tz_offset($S->{UI}->{VARS}->{time_zone});
! 		warn "  (Scoop::date_format) Local offset is $loc_offset.\n" if ($DEBUG);
  
! 		# get the difference in seconds between the user's time zone and UTC in seconds
! 		my $user_offset = &Time::Timezone::tz_offset(lc($S->{prefs}->{time_zone}));
! 		warn "  (Scoop::date_format) User's offset is $user_offset.\n" if ($DEBUG);
  
! 		# calculate the difference in seconds between the Scoop server's time zone
! 		# and the user's time zone
! 		my $diff = ($user_offset + -($loc_offset));
  
! 		# if the difference is a positive number, prepend a plus sign to it
! 		unless ($diff =~ /^([-])/) {
! 			$diff = "+$diff";
! 		}
  
! 		# figure out whether the difference is positive or negative (i.e. what sign it has)
! 		$diff =~ s/^(.)//;
! 		my $pl_min = $1;
  
  
! 		# convert the name of the date field into a MySQL function call
! 		# that returns the value of the field converted into the user's time zone
! 		if ($pl_min eq '+') {
! 			$adjust_time = $S->db_date_add($fieldname, "$diff SECOND");
! 		} else {
! 			$adjust_time = $S->db_date_sub($fieldname, "$diff SECOND");
! 		}
  
! 		warn "  (Scoop::date_format) Time diff is $diff\n" if ($DEBUG);
  
- 	}
  	
  	return ($adjust_time, $zone);
--- 1931,1971 ----
  	
  	my $adjust_time = $fieldname;
! 	my $zone = uc($S->pref('time_zone'));
  	
! 	warn "  (Scoop::date_format) Local zone is $S->{UI}->{VARS}->{time_zone}.\n" if ($DEBUG);
! 	warn "  (Scoop::date_format) User's zone is $zone.\n" if ($DEBUG);
  
! 	# get the difference in seconds between the Scoop server's time zone and UTC
! 	my $loc_offset = &Time::Timezone::tz_offset(lc($S->{UI}->{VARS}->{time_zone}));
! 	warn "  (Scoop::date_format) Local offset is $loc_offset.\n" if ($DEBUG);
  
! 	# get the difference in seconds between the user's time zone and UTC in seconds
! 	my $user_offset = &Time::Timezone::tz_offset(lc($zone));
! 	warn "  (Scoop::date_format) User's offset is $user_offset.\n" if ($DEBUG);
  
! 	# calculate the difference in seconds between the Scoop server's time zone
! 	# and the user's time zone
! 	my $diff = ($user_offset + -($loc_offset));
  
! 	# if the difference is a positive number, prepend a plus sign to it
! 	unless ($diff =~ /^([-])/) {
! 		$diff = "+$diff";
! 	}
  
! 	# figure out whether the difference is positive or negative (i.e. what sign it has)
! 	$diff =~ s/^(.)//;
! 	my $pl_min = $1;
  
  
! 	# convert the name of the date field into a MySQL function call
! 	# that returns the value of the field converted into the user's time zone
! 	if ($pl_min eq '+') {
! 		$adjust_time = $S->db_date_add($fieldname, "$diff SECOND");
! 	} else {
! 		$adjust_time = $S->db_date_sub($fieldname, "$diff SECOND");
! 	}
  
! 	warn "  (Scoop::date_format) Time diff is $diff\n" if ($DEBUG);
  
  	
  	return ($adjust_time, $zone);
***************
*** 2019,2023 ****
  		SET => "last_visit = NOW()",
  		WHERE => "ip = '$ip' AND uid = '$uid'"});
! 	warn "rv = $rv\n" if $DEBUG;
  	
  	$sth->finish;
--- 2012,2016 ----
  		SET => "last_visit = NOW()",
  		WHERE => "ip = '$ip' AND uid = '$uid'"});
! 	warn "rv = $rv" if $DEBUG;
  	
  	$sth->finish;