--- s080703/samizdat/lib/samizdat//helpers/application_helper.rb 2008-05-11 10:28:07.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat//helpers/application_helper.rb 2008-08-20 03:41:24.000000000 +0200 @@ -251,12 +264,37 @@ [:select, 'focus', focuses, (focus ? (focus.id or focus.uriref) : nil)] ] end + # some calendar constants + thisyear= Time.now.year + d = [] and 0.upto(31){ |i| + d += (i == 0 ? [[0, ("SELECT DAY")]] : [[i, i.to_s]]) } and Days_s = d + m = [] and 1.upto(12){ |i| + m += [[i, i.to_s]] } and Months_s = m + y = [] and 0.upto(3){ |i| # default 4 years' calendar + y += [[thisyear+i, (thisyear+i).to_s]] } and Years_s = y + # form fields for vote on focus rating # - def focus_fields(focus) + def focus_fields(focus,calendar = nil) return [] unless @member.allowed_to?('vote') + 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 + + if @request.route =~ /vote$/ fields = focus_select(focus) + else + fields = [] + end if @request.advanced_ui? fields.push( @@ -274,6 +312,19 @@ [1, _('1 (Likely)')], [2, _('2 (Yes)')] ], 0] ) + + end #if calendar else + + end + + # TODO: this should probably be moved to helpers.rb or calendar_helpers.rb + # or else a fast way to decide if a focus is a date message would be needed + def match_cal_focus_title(title) # Y10K unsafe + title =~ %r{\A[2-9][0-9]{3}-[01][0-9]-[0-3][0-9]\z} + end + + def match_cal_focus_title_rdf + %{ ?title ~ '^[2-9][0-9]{3}-[01][0-9]-[0-3][0-9]$' } end # render link to resource with a tooltip --- s080703/samizdat/lib/samizdat//helpers/resource_helper.rb 2008-05-10 18:35:32.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat//helpers/resource_helper.rb 2008-08-19 23:29:06.000000000 +0200 @@ -23,7 +23,7 @@ info = resource_href(id, info) if id info << ': ' << focus.print_rating info << ' (' << _('vote') << ')' if @member.allowed_to?('vote') @@ -49,6 +49,16 @@ %{" href="resource/#{related}/vote">} << _('Add another focus') << '
') + + if config['calendar'] + fbox << box(nil, + %{} << + _('Put this in the calendar') << '
' + ) + end # if config['calendar'] + end fbox end --- s080703/samizdat/lib/samizdat//controllers/resource_controller.rb 2008-05-10 18:35:32.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat//controllers/resource_controller.rb 2008-08-20 04:18:20.000000000 +0200 @@ -60,6 +60,101 @@ end end + def calendar_add + @member.assert_allowed_to('vote') + + 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'].kind_of? Integer + 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 not focus_id # create a date message + @message = Message.new + @message.creator = @member + @message.content = Content.new(nil, @session.login, + focus_title, 'text/textile', ' ') + @message.lang = @message.validate_lang((@request['lang'] or @request.language)) + @message.desc = description + @message.open = true # date message is open for editing by members + @message.insert! + focus_id= @message.id + end # if focus_id + + # add link; editing is open, so links can be added/removed by logged-in members + # TODO: can this be done without direct transactions in the database? + 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(/"/,'"') + '":/' + + @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' + 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 + @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, calendar) + + [ [: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 @@ -87,11 +182,12 @@ @layout = nil end - private + # private # shifted lower down for calendar_patch-0.7 # messages that are related to this focus # - def related_dataset + def related_dataset(calendar_days = nil) + if not (config['calendar'] and @id.to_i == config['calendar']) RdfDataSet.new(%{ SELECT ?msg WHERE (rdf::subject ?stmt ?msg) @@ -104,7 +200,38 @@ (s::hidden ?msg ?hidden) LITERAL ?rating > 0 AND ?parent IS NULL AND ?current IS NULL #{exclude_hidden} ORDER BY ?date DESC}) + else # calendar superfocus + # TODO rewrite this using .collect instead of .each + if calendar_days + check_near_future = ' AND ?title <= :soon' + order = 'DESC' + n_days = (calendar_days.kind_of? Integer) ? calendar_days : 2 + near_future = (Time.now + n_days * 86400).strftime("%Y-%m-%d") + else + check_near_future = '' + order = 'ASC' + end + today=Time.now.strftime("%Y-%m-%d") + RdfDataSet.new(%{ +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) + (dc::title ?msg ?title) + (s::hidden ?msg ?hidden) +LITERAL ?rating > 0 AND ?parent IS NULL AND ?current IS NULL #{exclude_hidden} AND + ?title >= :now #{check_near_future} +ORDER BY ?title #{order}}, limit_page, + { :now => today.to_s, + :soon => near_future.to_s }) end + end + + private # related resources # --- s080703_monolanguage-0.3_frontpage_controller.rb 2008-08-20 04:43:08.000000000 +0200 +++ /usr/lib/ruby/1.8/samizdat/controllers/frontpage_controller.rb 2008-08-20 03:40:44.000000000 +0200 @@ -10,6 +10,8 @@ require 'samizdat/helpers/syndication_helper' +require 'samizdat/controllers/resource_controller' + class FrontpageController < Controller include SyndicationHelper @@ -39,15 +41,55 @@ return @content_for_layout = body end + @calendar_id = (config['calendar'].kind_of? Integer) ? config['calendar'] : nil + tag_parameters + 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_id or + (not match_cal_focus_title(f_title) and + focus != @calendar_id) + @tag_min = usage if ((usage < @tag_min) and (usage > 0)) + @tag_max = usage if usage > @tag_max + end + } + d_tag = Math.log(@tag_max/@tag_min)/@n_sizes.to_f + d_tag = 0.001 if d_tag < 0.001 + + calendar_resource_href = (@calendar_id)? + resource_href(@calendar_id, Resource.new(@request, @calendar_id).title) : '' + + if @calendar_id + cal_controller = ResourceController.new(@request, @calendar_id) + # calendar in left column + calendar_days = (config['calendar_days'].kind_of? Integer) ? + config['calendar_days'] : 30 + dataset = cal_controller.related_dataset(calendar_days) + calendar_upcoming = dataset[0].collect { |r,| + m = Message.new(r) + # TODO: class="calendar" -> *.css ? + %{