| 1 | # |
|---|
| 2 | # navi_day.rb |
|---|
| 3 | # |
|---|
| 4 | # navi_day: 「前の日記」や「次の日記」を“月またぎ”に対応させる。 |
|---|
| 5 | # |
|---|
| 6 | # 日単位での表示の時の「前の日記」や「次の日記」のリンクが、 |
|---|
| 7 | # 異なる月の日記を正しく指せない場合があるという tDiary の制限を |
|---|
| 8 | # 解消するプラグインです。以前よりある navi_user.rb と機能的には |
|---|
| 9 | # 同じですが、navi_user.rb よりは処理がずっと軽くなっています。 |
|---|
| 10 | # また、日記の表示状態(非表示の日記)に対する考慮もなされています。 |
|---|
| 11 | # |
|---|
| 12 | # tDiary 2.0 以降で使えると思います。セキュアモードでも使えますが、 |
|---|
| 13 | # セキュアモードの場合は、モバイル端末からのアクセスに対しては |
|---|
| 14 | # このプラグインは効力を持ちません(tDiary セキュア環境の制限: |
|---|
| 15 | # モバイル端末の場合は本文を出力するときに calc_links が呼ばれる |
|---|
| 16 | # ため)。 |
|---|
| 17 | # |
|---|
| 18 | # navi_user.rb と併用すると navi_user.rb の方が優先されますので、 |
|---|
| 19 | # このプラグインを使うときには必ず navi_user.rb を外してください。 |
|---|
| 20 | # |
|---|
| 21 | # Copyright (C) 2007, MIYASAKA Masaru <alkaid@coral.ocn.ne.jp> |
|---|
| 22 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 23 | # |
|---|
| 24 | # Last Modified : May 27, 2007 |
|---|
| 25 | # |
|---|
| 26 | |
|---|
| 27 | # for tDiary 2.0.X |
|---|
| 28 | if not TDiaryMonth.method_defined?(:diaries) then |
|---|
| 29 | eval( <<-MODIFY_CLASS, TOPLEVEL_BINDING ) |
|---|
| 30 | module TDiary |
|---|
| 31 | class TDiaryMonth |
|---|
| 32 | attr_reader :diaries |
|---|
| 33 | end |
|---|
| 34 | end |
|---|
| 35 | MODIFY_CLASS |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | class NaviDayCGI |
|---|
| 39 | attr_reader :params |
|---|
| 40 | def referer; nil; end |
|---|
| 41 | def initialize |
|---|
| 42 | @params = Hash.new([]) |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | |
|---|
| 46 | alias :calc_links_navi_day_backup :calc_links |
|---|
| 47 | |
|---|
| 48 | def calc_links |
|---|
| 49 | if not (@conf.secure and @conf.mobile_agent?) and \ |
|---|
| 50 | (/day|edit/ =~ @mode or \ |
|---|
| 51 | (@conf.mobile_agent? and /latest|month|nyear/ =~ @mode)) then |
|---|
| 52 | if /(latest|month|nyear)/ === @mode |
|---|
| 53 | today = @diaries.keys.sort[-1] |
|---|
| 54 | else |
|---|
| 55 | today = @date.strftime('%Y%m%d') |
|---|
| 56 | end |
|---|
| 57 | days = @diaries.keys |
|---|
| 58 | days |= [today] |
|---|
| 59 | days.sort! |
|---|
| 60 | days.unshift(nil).push(nil) |
|---|
| 61 | today_index = days.index(today) |
|---|
| 62 | |
|---|
| 63 | days[0 .. today_index - 1].reverse_each do |prev_day| |
|---|
| 64 | @prev_day = prev_day |
|---|
| 65 | break unless @prev_day |
|---|
| 66 | break if (@mode == 'edit') or @diaries[@prev_day].visible? |
|---|
| 67 | end |
|---|
| 68 | |
|---|
| 69 | days[today_index + 1 .. -1].each do |next_day| |
|---|
| 70 | @next_day = next_day |
|---|
| 71 | break unless @next_day |
|---|
| 72 | break if (@mode == 'edit') or @diaries[@next_day].visible? |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | if not @prev_day or not @next_day then |
|---|
| 76 | cgi = NaviDayCGI.new |
|---|
| 77 | years = [] |
|---|
| 78 | @years.each do |k, v| |
|---|
| 79 | v.each do |m| |
|---|
| 80 | years << k + m |
|---|
| 81 | end |
|---|
| 82 | end |
|---|
| 83 | this_month = @date.strftime('%Y%m') |
|---|
| 84 | years |= [this_month] |
|---|
| 85 | years.sort! |
|---|
| 86 | years.unshift(nil).push(nil) |
|---|
| 87 | this_month_index = years.index(this_month) |
|---|
| 88 | |
|---|
| 89 | years[0 .. this_month_index - 1].reverse_each do |prev_month| |
|---|
| 90 | break unless not @prev_day and prev_month |
|---|
| 91 | cgi.params['date'] = [prev_month] |
|---|
| 92 | diaries = TDiaryMonth.new(cgi, '', @conf).diaries |
|---|
| 93 | days = diaries.keys.sort |
|---|
| 94 | days.unshift(nil) |
|---|
| 95 | days.reverse_each do |prev_day| |
|---|
| 96 | @prev_day = prev_day |
|---|
| 97 | break unless @prev_day |
|---|
| 98 | break if (@mode == 'edit') or diaries[@prev_day].visible? |
|---|
| 99 | end |
|---|
| 100 | end |
|---|
| 101 | |
|---|
| 102 | years[this_month_index + 1 .. -1].each do |next_month| |
|---|
| 103 | break unless not @next_day and next_month |
|---|
| 104 | cgi.params['date'] = [next_month] |
|---|
| 105 | diaries = TDiaryMonth.new(cgi, '', @conf).diaries |
|---|
| 106 | days = diaries.keys.sort |
|---|
| 107 | days.push(nil) |
|---|
| 108 | days.each do |next_day| |
|---|
| 109 | @next_day = next_day |
|---|
| 110 | break unless @next_day |
|---|
| 111 | break if (@mode == 'edit') or diaries[@next_day].visible? |
|---|
| 112 | end |
|---|
| 113 | end |
|---|
| 114 | end |
|---|
| 115 | else |
|---|
| 116 | calc_links_navi_day_backup |
|---|
| 117 | end |
|---|
| 118 | end |
|---|