--- /tmp/tmp_snapshot/samizdat/lib/samizdat/helpers/application_helper.rb 2007-06-17 17:14:42.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat/helpers/application_helper.rb 2007-08-03 19:46:10.000000000 +0200 @@ -181,9 +183,46 @@ end end + # form fields for vote on focus rating # - def focus_fields(focus, advanced = @request.advanced_ui?) + # some calendar constants + days_s = [ [0, _("SELECT DAY")] ] + for i in 1..31 + days_s += [ [i, i.to_s] ] + end + Days_s = days_s + + months_s = [] + Months_inv = Date::MONTHS.invert + for i in 1..12 +# months_s += [ [i, Months_inv[i] ] ] # needs l10n + months_s += [ [i, i.to_s ] ] + end + Months_s = months_s + + years_s = [] + thisyear= Time.now.year + + for i in 0..3 # default 4 years' calendar + years_s += [ [thisyear+i, (thisyear+i).to_s] ] + end + Years_s = years_s + +# def focus_fields(focus, advanced = @request.advanced_ui?) + def focus_fields(focus, advanced = @request.advanced_ui?, calendar = nil) + if 'start' == calendar + fields = + [ [:hidden, 'id', id], + [:label, 'calendar_date', _('Select the date at which this event is planned for')], + [:select, 'cal_year', Years_s, Time.now.year], + [:select, 'cal_month', Months_s, Time.now.month], + [:select, 'cal_day', Days_s, 0], # default day is invalid day + [:hidden, 'rating', 1], + [:hidden, 'calendar', 'send'], + ] + else + focuses = Focus.collect_focuses {|f,| [ f, Resource.new(@request, f).title ] } @@ -191,9 +230,15 @@ focuses.unshift( [(focus.id or focus.uriref), focus.name] ) end focuses.unshift [nil, _('SELECT FOCUS')] - fields = - [ [:label, 'focus', _('Select focus that this resource will be related to')], + + if @request.access('vote') + fields = + [ [:label, 'focus', _('Select focus that this resource will be related to')], [:select, 'focus', focuses, (focus ? (focus.id or focus.uriref) : nil)] ] + else + fields = [] + end + if advanced fields.push( [:label, 'focus_id', _("Enter focus id if it's not in the list")], @@ -209,6 +254,8 @@ else fields.push([:hidden, 'rating', 1]) end + + end #end if calendar end # transform date to a standard string representation --- /tmp/tmp_snapshot/samizdat/lib/samizdat/helpers/resource_helper.rb 2007-04-17 22:24:45.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat/helpers/resource_helper.rb 2007-08-02 00:35:26.000000000 +0200 @@ -36,19 +36,54 @@ def focus_box(related, focuses) return '' unless focuses.kind_of? Array and (focuses.size > 0 or @request.access('vote')) + +# HACKS - ignore +# # check if translation focus should be shown or not +# resource = Resource.new(@request,related) +# if resource.type == 'Message' +# message = Message.new(related) +# @t_id = (message.is_parent_lang_preferred?(@request.accept_language)) ? +# related : message.select_translation(@request.accept_language).id +# else +# @t_id = related +# end + +# Focus.translation_focus == focus ? +# focus_info(@t_id, focus) : focus_info(related, focus) + fbox = box( _('Related Focuses'), focuses.sort_by {|f| -f.sort_index }.collect {|focus| focus_info(related, focus) }.join, 'focuses' ) + + resource = Resource.new(@request, related) + if resource.type == 'Message' + message = Message.new(related) + related_parent = (message.is_translation?) ? + message.parent : message.id + else + related_parent = resource.id + end + if @request.access('vote') fbox << box(nil, '
} << + %{" href="resource/#{related_parent}/vote">} << _('Add another focus') << '
') + if config['calendar'] + fbox << box(nil, + %{} << + _('Put this in the calendar') << '
' + ) + end # if config['calendar'] + + end fbox end --- /tmp/tmp_snapshot/samizdat/lib/samizdat/controllers/resource_controller.rb 2007-05-02 20:43:56.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat/controllers/resource_controller.rb 2007-08-13 01:10:55.477879768 +0200 @@ -45,8 +47,20 @@ if focus.kind_of?(Focus) and rating and action_confirmed? # commit vote # rating is validated by Focus#rating= db.transaction {|db| focus.rating = rating } - @request.redirect(@id) + + if Focus.translation_focus == focus.id + @message = Message.cached(@id) + @t_id = (@message.is_parent_lang_preferred?(@request.accept_language)) ? + @message.id : @message.select_translation(@request.accept_language).id + else + @t_id = @id + end + + @request.redirect(@t_id) +# @request.redirect(@id) + else # display vote form +# calendar= 'start' # HACK test only vote_form = secure_form( 'resource/' + @id.to_s + '/vote', *focus_fields(focus) + [ [:br], [:submit, nil, _('Submit')] ] @@ -56,6 +70,123 @@ end end + # vote on focus rating + # + def calendar_add + @request.access('vote') or raise AuthError, + sprintf(_('Your current access level (%s) does not allow to vote'), + _(@request.role)) + + focus, focus_id, rating, cal_year, cal_month, cal_day, creator, format, description = + @request.values_at %w[focus focus_id rating cal_year cal_month cal_day creator format description] + + # julian date if date is valid, otherwise set to nil + jd= Date::valid_civil?(cal_year.to_i, cal_month.to_i, cal_day.to_i) + + if config['calendar'] + if jd + dd = cal_day.to_s + dd = "0" + dd if cal_day.to_i < 10 + mm = cal_month.to_s + mm = "0" + mm if cal_month.to_i < 10 + + focus_title= cal_year.to_s + "-" + mm + "-" + dd + + # find out if a message for this date already exists + db.transaction do |db| + focus_id, = db.select_one 'SELECT id FROM Message + WHERE title = ? AND version_of is NULL', focus_title + end + if focus_id + # do nothing + else + # calendar focus is created as open for editing by members + if config['locale']['languages'][0] + lang= config['locale']['languages'][0] + else + lang= NULL + end + +# message = PublishMessage.new(session) ## what should go here post-20070501? +# MessageController.publish # maybe + + @message = Message.new + @message.creator = Member.cached(@session.member) # @message.set_creator + @message.content = Content.new(nil, @session.login, + focus_title, 'text/textile', ' ') + # set_lang + @message.lang = @message.validate_lang((@request['lang'] or @request.language)) + @message.desc = description #set_desc # descriptions not used <= 0.6.0.20070501 + @message.open = true # set_open + # set_focus + + @message.insert! + focus_id= @message.id + + end # if focus_id + + # add link (editing is open, so this is not hardwiring) + db.transaction do |db| + f_content, = db.select_one 'select content from message + where id = ? ', focus_id + m_title, = db.select_one 'select title from message + where id = ? ', @id + db.do 'update message set content = ? where id = ? ', + f_content + %{\n} + '"' + m_title.gsub(/"/,'"') + '":/' + # 070812 %{ to ' + @id.to_s + %{\n}, focus_id + db.do 'update message set format = ? where id = ? ', + "text/textile", focus_id + db.do 'update message set html_full = null where id = ? ', + focus_id + db.do 'update message set html_short = null where id = ? ', + focus_id + Content.regenerate_html(focus_id) + end # db.transaction do |db| + + calendar_superfocus = config['calendar'] + else # invalid date - try again + calendar = 'start' # todo: probably will fail in 20070501, needs more work + end + end # if 'send' == calendar + + + + if focus_id = Resource.validate_id(focus_id) + # manual entry overrides selection + focus = focus_id + end + focus_id = focus # needed for resource.new + focus = Focus.new(@request, focus, @id) if focus # focus becomes an object + + if focus.kind_of?(Focus) and rating and action_confirmed? # commit vote + # rating is validated by focus#rating= + db.transaction {|db| focus.rating = rating } + if Resource.validate_id(calendar_superfocus) + # focus_r = Resource.new(session, focus_id) # focus as resource object + focus_r = Resource.new(@request, focus_id) # focus as resource object + calendar_superfocus = Focus.new(@request, calendar_superfocus, focus_r.id) + if calendar_superfocus.kind_of?(Focus) + db.transaction {|db| calendar_superfocus.rating = rating } + end + @request.redirect(@id) + else + # session.redirect(id.to_s) + @request.redirect(@id) + end + + + else # display vote form + calendar= 'start' # this is the calendar_add method, not the vote method + vote_form = secure_form( 'resource/' + @id.to_s + '/calendar_add', + *focus_fields(focus, false, calendar) + # no advanced calendar option + [ [:br], [:submit, nil, _('Submit')] ] + ) + @title = _('Vote') + ': ' + @title + @content_for_layout = box(_('Vote'), vote_form) + box(@title, @resource.short) + end + end + + # RSS feed of related resources # def rss @@ -83,11 +214,61 @@ @layout = nil end + + def cal_collect_related(n_days = 2,skip = 0) + n_days = 2 if not n_days.kind_of? Integer + limit_page_cal = + (config['calendar'] and @id.to_i == config['calendar']) ? + limit_page * 100 : limit_page # TODO: this is a bad hack: FIX IT! + + list = cache.fetch_or_add(%{resource/#{@id}/related/#{skip}}) do + rdf.select_all( %{ +SELECT ?msg +WHERE (rdf::subject ?stmt ?msg) + (rdf::predicate ?stmt dc::relation) + (rdf::object ?stmt #{@id}) + (s::inReplyTo ?msg ?parent) + (dct::isVersionOf ?msg ?current) + (s::rating ?stmt ?rating) + (dc::date ?msg ?date) + (s::hidden ?msg ?hidden) +LITERAL ?rating > 0 AND ?parent IS NULL AND ?current IS NULL #{exclude_hidden} +ORDER BY ?date DESC}, limit_page_cal, limit_page_cal * skip + ).collect {|m,| m } # unwrap DBI::Row + end + + if config['calendar'] and @id.to_i == config['calendar'] + # TODO rewrite this using .collect instead of .each + today=Time.now.strftime("%Y-%m-%d") + near_future=(Time.now + n_days * 86400).strftime("%Y-%m-%d") + + list_titles = [] + list.each do |msg| + m_title, = rdf.select_one %{ +SELECT ?title +WHERE (dc::title #{msg} ?title)} + # ignore all past and far future events + if today <= m_title and m_title <= near_future + list_titles += [[m_title, msg]] # add a pair + end + end + list_titles.sort! { |a,b| a[0] <=> b[0] } # show soonest dates first + list = list_titles.collect { |pair| pair[1] } + end + + list.collect {|msg| yield msg } + end + + private # messages that are related to this focus # def collect_related(skip) + limit_page_cal = + (config['calendar'] and @id.to_i == config['calendar']) ? + limit_page * 100 : limit_page # TODO: this is a bad hack: FIX IT! + list = cache.fetch_or_add(%{resource/#{@id}/related/#{skip}}) do rdf.select_all( %{ SELECT ?msg @@ -100,9 +414,27 @@ (dc::date ?msg ?date) (s::hidden ?msg ?hidden) LITERAL ?rating > 0 AND ?parent IS NULL AND ?current IS NULL #{exclude_hidden} -ORDER BY ?date DESC}, limit_page, limit_page * skip +ORDER BY ?date DESC}, limit_page_cal, limit_page_cal * skip ).collect {|m,| m } # unwrap DBI::Row end + + if config['calendar'] and @id.to_i == config['calendar'] + # TODO rewrite this using .collect instead of .each + today=Time.now.strftime("%Y-%m-%d") + + list_titles = [] + list.each do |msg| + m_title, = rdf.select_one %{ +SELECT ?title +WHERE (dc::title #{msg} ?title)} + if m_title >= today # ignore old events + list_titles += [[m_title, msg]] # add a pair + end + end + list_titles.sort! { |a,b| a[0] <=> b[0] } # show soonest dates first + list = list_titles.collect { |pair| pair[1] } + end + list.collect {|msg| yield msg } end @@ -128,4 +460,5 @@ '' end end + end --- /tmp/tmp_snapshot/samizdat/lib/samizdat/controllers/frontpage_controller.rb 2007-05-07 22:14:58.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat/controllers/frontpage_controller.rb 2007-08-13 01:22:05.219063632 +0200 @@ -8,6 +8,9 @@ # # vim: et sw=2 sts=2 ts=8 tw=0 +# require 'samizdat/controllers/sms2message_controller' +require 'samizdat/controllers/resource_controller' + class FrontpageController < Controller def index @@ -34,16 +38,49 @@ render_features = (full_front_page or 0 == skip) render_updates = (full_front_page or 0 == skip_feature) + calendar_focus_id = config['calendar'] if config['calendar'].kind_of? Integer + tag_min = 9.9e9 + tag_max = 1.0 + n_sizes = 5 + vary_log = 0.2.freeze + e = Math.exp(1).freeze + size_min = Math.exp(-vary_log * (n_sizes/2).to_f) *100 # as percentage + if full_front_page # render focuses focuses_title = _('Top Focuses') + Focus.collect_focuses {|focus, usage| + f_title = Resource.new(@request, focus).title + if not calendar_focus_id or + (not f_title =~ %r{\A[2-9][0-9]{3}-[01][0-9]-[0-3][0-9]\z} and # Y10K unsafe + focus != calendar_focus_id) + tag_min = usage if usage < tag_min + tag_max = usage if usage > tag_max + end + } + d_tag = Math.log(tag_max/tag_min)/n_sizes.to_f + focuses = + %{