
--- /tmp/tmp_snapshot/samizdat/lib/samizdat/helpers/application_helper.rb	2007-08-14 01:40:09.000000000 +0200
+++ /usr/lib/ruby/1.8/samizdat/helpers/application_helper.rb	2007-08-21 16:26:23.000000000 +0200
@@ -211,10 +211,50 @@
 
   # form fields for vote on focus rating
   #
-  def focus_fields(focus)
+  # 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)
+  def focus_fields(focus,calendar = nil)
     return [] unless @request.access('vote')
 
-    fields = focus_select(focus)
+    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.access('vote')
+      fields = focus_select(focus)
+    else
+      fields = []
+    end
 
     if @request.advanced_ui?
       fields.push(
@@ -232,6 +272,9 @@
           [1, _('1 (Likely)')],
           [2, _('2 (Yes)')] ], 0]
     )
+
+    end  #if calendar else
+
   end
 
   # render link to resource with a tooltip
--- /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-25 02:43:56.000000000 +0200
@@ -42,13 +42,34 @@
       }.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,
         '<p><a class="action" title="' <<
         _('Click to relate this resource to another focus') <<
-        %{" href="resource/#{related}/vote">} <<
+        %{" href="resource/#{related_parent}/vote">} <<
         _('Add another focus') <<
         '</a></p>')
+
+      if config['calendar']  
+        fbox << box(nil,
+                    %{<p><a title="} << 
+                      _('Click to put this resource in the calendar') <<
+                      %{" href="resource/#{related_parent}/calendar_add">} <<
+                      _('Put this in the calendar') << '</a></p>'
+                    )
+      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-27 11:28:48.000000000 +0200
@@ -45,7 +47,18 @@
     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
       vote_form = secure_form( 'resource/' + @id.to_s + '/vote',
         *focus_fields(focus) +
@@ -56,6 +69,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(/"/,'&quot;') + '":/' +  # 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, 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
@@ -100,10 +414,28 @@
       (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
-    list.collect {|msg| yield msg }
+
+    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[0..limit_page_cal].collect {|msg| yield msg }
   end
 
   # related resources
@@ -128,4 +460,5 @@
       ''
     end
   end
+
 end

--- /tmp/tmp_snapshot/samizdat/lib/samizdat/controllers/frontpage_controller.rb	2007-06-21 20:52:49.000000000 +0200
+++ /usr/lib/ruby/1.8/samizdat/controllers/frontpage_controller.rb	2007-09-29 11:04:50.609618472 +0200
@@ -10,6 +10,10 @@
 
 require 'samizdat/helpers/syndication_helper'
 
+require 'samizdat/controllers/resource_controller'
+
+# require 'samizdat/controllers/sms2message_controller'
+
 class FrontpageController < Controller
   include SyndicationHelper
 
@@ -37,16 +43,54 @@
     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
+            (not match_cal_focus_title(f_title) and
+            focus != calendar_focus_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_focus_id)?
+	resource_href(calendar_focus_id, Resource.new(@request, calendar_focus_id).title) : ''
+
       focuses =
+        %{<div class="focus">} + calendar_resource_href +
+          %{</div>\n<div class="focus">} +
         Focus.collect_focuses {|focus, usage|
-          %{<div class="focus">} + resource_href(
-            focus,
-            Resource.new(@request, focus).title + %{ (#{usage})}
-          ) + %{</div>\n}
-        }.join
-
+          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
+              (not match_cal_focus_title(f_title) and
+              focus != calendar_focus_id)
+            t = usage
+	    t = 1 if t < 1
+            t_size = (size_min * Math.exp( vary_log * Math.log(t/tag_min)/d_tag )).to_i
+
+#            %{<div class="focus">} + resource_href(
+            %{<span style="font-size: #{t_size}%">} +
+            resource_href(
+              focus,
+              f_title + %{ (#{usage})}
+#              ) + %{</div>\n}
+              ) + %{</span>\n}
+          end
+        }.join + %{</div>\n}
       focuses <<
         '<div class="foot"><div class="nav"><a href="foci">' <<
         _('more') << '</a></div></div>'
@@ -57,6 +101,13 @@
         (skip_feature > 0 ? sprintf(_(', page %s'), skip_feature + 1) : '')
       features = collect_features(skip_feature) {|msg|
         Resource.new(@request, msg).short }
+#      calendar_focus_id = config['calendar']  if config['calendar'].kind_of? Integer
+      if calendar_focus_id
+        cal_controller = ResourceController.new(@request, calendar_focus_id)
+        features = 
+          cal_controller.cal_collect_related(2) {|r,| Resource.new(@request, r).short } +
+          features
+      end
       features = features.join <<
         %{<div class="foot">} <<
         nav_rss(rss_features) <<
