<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>config/initializers/dummy_models.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -502,6 +502,9 @@ class ApplicationController &lt; ActionController::Base
       end
     end
     related &lt;&lt; 'public_query'
+    related &lt;&lt; 'related-tools-create-or-link-or-remove'
+    related &lt;&lt; 'related-tools-restore'
+    related &lt;&lt; 'related-tools-import'
     related.each do |related_controller|
       expire_fragment_for_all_versions(item,
                                        { :urlified_name =&gt; item.basket.urlified_name,</diff>
      <filename>app/controllers/application.rb</filename>
    </modified>
    <modified>
      <diff>@@ -12,6 +12,9 @@ class TopicsController &lt; ApplicationController
   def show
     prepare_item_variables_for('Topic')
     @topic = @item
+    # If we are serving a cached page, we still need
+    # some details in @topic, so lets make a dummy one
+    @topic = DummyModel.new({ :id =&gt; @cache_id, :basket =&gt; @current_basket }) if @topic.nil?
 
     respond_to do |format|
       format.html</diff>
      <filename>app/controllers/topics_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -577,8 +577,15 @@ module ApplicationHelper
   def link_to_related_item_function(options={})
     options = { :link_text =&gt; &quot;#{options[:function].capitalize} an Existing Related Item&quot; }.merge(options)
     link_text = options.delete(:link_text)
-    link = link_to(link_text, { :controller =&gt; 'search', :action =&gt; 'find_related' }.merge(options), 
-                              { :popup =&gt; ['links', 'height=500,width=500,scrollbars=yes,top=100,left=100,resizable=yes'] })
+    disabled = false
+    disabled = true if options[:function] == 'remove' &amp;&amp; @total_item_counts &lt; 1
+    if options[:function] == 'restore'
+      restore_count = ContentItemRelation::Deleted.count(:conditions =&gt; { :topic_id =&gt; options[:relate_to_topic] })
+      disabled = true if restore_count &lt; 1
+      link_text += &quot; (#{restore_count})&quot;
+    end
+    link = disabled ? link_text : link_to(link_text, { :controller =&gt; 'search', :action =&gt; 'find_related' }.merge(options), 
+                                                     { :popup =&gt; ['links', 'height=500,width=500,scrollbars=yes,top=100,left=100,resizable=yes'] })
     content_tag('li', link)
   end
 </diff>
      <filename>app/helpers/application_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,18 +1,23 @@
 &lt;%
 topics_only = defined?(topics_only) ? topics_only : false
 
-# These queries uses zoom and aren't caches
-@public_item_counts = public_related_items_for(item, { :topics_only =&gt; topics_only, :count_only =&gt; true })
-@private_item_counts = private_related_items_for(item, { :topics_only =&gt; topics_only, :count_only =&gt; true })
-
-# The public query uses mysql and is cached
-cache({ :related =&gt; 'public_query', :id =&gt; @cache_id }) do
-  @public_items = public_related_items_for(item, { :topics_only =&gt; topics_only })
-  &quot;(this just ensures queries don't run when unless needed - ignore this file)&quot;
+# These queries uses zoom and aren't cached
+@total_item_counts = 0
+@content_relations_count = ContentItemRelation.count(:conditions =&gt; { :topic_id =&gt; @cache_id })
+if @content_relations_count &gt; 0
+  @public_item_counts = public_related_items_for(item, { :topics_only =&gt; topics_only, :count_only =&gt; true })
+  @private_item_counts = private_related_items_for(item, { :topics_only =&gt; topics_only, :count_only =&gt; true })
+  @public_item_counts.each { |v| @total_item_counts += v.last }
+  @private_item_counts.each { |v| @total_item_counts += v.last }
+
+  # The public query uses mysql and is cached
+  cache({ :related =&gt; 'public_query', :id =&gt; @cache_id }) do
+    @public_items = public_related_items_for(item, { :topics_only =&gt; topics_only })
+    &quot;(this just ensures queries don't run when unless needed - ignore this file)&quot;
+  end
+  # The private query uses zoom and isn't cached
+  @private_items = private_related_items_for(item, { :topics_only =&gt; topics_only })
 end
-# The private query uses zoom and isn't cached
-@private_items = private_related_items_for(item, { :topics_only =&gt; topics_only })
-
 -%&gt;
 
 &lt;% if topics_only -%&gt;
@@ -32,21 +37,29 @@ end
   &lt;%= render(:partial =&gt; &quot;topics/secondary_wrapper_start&quot;,
              :locals =&gt; { :div_id =&gt; &quot;related&quot;, :class_suffix =&gt; &quot;-blue&quot; }) -%&gt;
 
-  &lt;h2 class=&quot;related-link&quot;&gt;Related Items&lt;/h2&gt;
+  &lt;h2 class=&quot;related-link&quot;&gt;Related Items (&lt;%= @total_item_counts %&gt;)&lt;/h2&gt;
 
   &lt;% if current_user_can_see_add_links? -%&gt;
-    &lt;% cache({ :part =&gt; 'related-tools' }) do -%&gt;
-      &lt;div id=&quot;related-items-tools&quot;&gt;
-        &lt;ul&gt;
+    &lt;div id=&quot;related-items-tools&quot;&gt;
+      &lt;ul&gt;
+        &lt;% cache({ :part =&gt; 'related-tools-create-or-link-or-remove', :id =&gt; @cache_id }) do -%&gt;
           &lt;%= header_add_links(:relate_to_topic =&gt; item, :link_text =&gt; 'Create', :class =&gt; 'first') -%&gt;
           &lt;%= link_to_related_item_function(:link_text =&gt; 'Link Existing', :function =&gt; 'add', :relate_to_topic =&gt; item) -%&gt;
           &lt;%= link_to_related_item_function(:link_text =&gt; 'Remove', :function =&gt; 'remove', :relate_to_topic =&gt; item) -%&gt;
-          &lt;%= link_to_related_item_function(:link_text =&gt; 'Restore', :function =&gt; 'restore', :relate_to_topic =&gt; item) -%&gt;
-          &lt;%= link_to_add_set_of_related_items(:link_text =&gt; 'Import Set', :relate_to_topic =&gt; item) -%&gt;
-        &lt;/ul&gt;
-      &lt;/div&gt;
-      &lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;
-    &lt;% end -%&gt;
+        &lt;% end -%&gt;
+        &lt;% if @basket_moderator -%&gt;
+          &lt;% cache({ :part =&gt; 'related-tools-restore', :id =&gt; @cache_id }) do -%&gt;
+            &lt;%= link_to_related_item_function(:link_text =&gt; 'Restore', :function =&gt; 'restore', :relate_to_topic =&gt; item) -%&gt;
+          &lt;% end -%&gt;
+        &lt;% end -%&gt;
+        &lt;% if @site_admin -%&gt;
+          &lt;% cache({ :part =&gt; 'related-tools-import', :id =&gt; @cache_id }) do -%&gt;
+            &lt;%= link_to_add_set_of_related_items(:link_text =&gt; 'Import Set', :relate_to_topic =&gt; item) -%&gt;
+          &lt;% end -%&gt;
+        &lt;% end -%&gt;
+      &lt;/ul&gt;
+    &lt;/div&gt;
+    &lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;
   &lt;% end -%&gt;
 
   &lt;div id=&quot;detail-linked-toprow&quot;&gt;
@@ -71,8 +84,6 @@ end
                :locals =&gt; { :item =&gt; item, :zoom_class =&gt; 'WebLink' }) -%&gt;
   &lt;/div&gt;
 
-  &lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;
-
   &lt;%= render(:partial =&gt; &quot;topics/secondary_wrapper_end&quot;) -%&gt;
 
 &lt;% end -%&gt;</diff>
      <filename>app/views/topics/_related_items.html.erb</filename>
    </modified>
    <modified>
      <diff>@@ -1,7 +1,12 @@
 &lt;%
 pretty_class_plural = zoom_class_plural_humanize(zoom_class)
 controller = zoom_class_controller(zoom_class)
-total_items = (@public_item_counts[zoom_class] + @private_item_counts[zoom_class])
+if @total_item_counts &gt; 0
+  total_items = (@public_item_counts[zoom_class] + @private_item_counts[zoom_class])
+  only_public_items = (@public_item_counts[zoom_class] &gt; 0 &amp;&amp; @private_item_counts[zoom_class] &lt; 1)
+else
+  total_items = 0
+end
 are_still_images = (zoom_class == 'StillImage')
 display_num = are_still_images ? NUMBER_OF_RELATED_IMAGES_TO_DISPLAY : NUMBER_OF_RELATED_THINGS_TO_DISPLAY_PER_TYPE
 display_heading = defined?(display_heading) &amp;&amp; display_heading != false ? display_heading : nil
@@ -15,33 +20,34 @@ display_no_item_text = defined?(display_no_item_text) ? display_no_item_text : f
     &lt;h4&gt;&lt;%= &quot;#{pretty_class_plural} (#{total_items})&quot; -%&gt;&lt;/h4&gt;
   &lt;% end -%&gt;
   &lt;% if total_items &gt; 0 -%&gt;
-    &lt;ul&gt;
+    &lt;%= '&lt;ul&gt;' unless only_public_items -%&gt;
       &lt;% if @public_item_counts[zoom_class] &gt; 0 -%&gt;
-        &lt;li&gt;
-          &lt;strong&gt;Public&lt;/strong&gt;
-          &lt;% cache ({ :related =&gt; controller, :id =&gt; @cache_id }) do -%&gt;
-            &lt;%= related_items_display_of @public_items[zoom_class],
-                                         { :are_still_images =&gt; are_still_images,
-                                           :item =&gt; item,
-                                           :zoom_class =&gt; zoom_class,
-                                           :display_num =&gt; display_num,
-                                           :total_num =&gt; @public_item_counts[zoom_class] } -%&gt;
-          &lt;% end -%&gt;
-        &lt;/li&gt;
-      &lt;% end -%&gt;
-      &lt;% if @private_item_counts[zoom_class] &gt; 0 -%&gt;
-        &lt;li&gt;
-          &lt;strong&gt;Private&lt;/strong&gt;
-          &lt;%= related_items_display_of @private_items[zoom_class],
+        &lt;% unless only_public_items -%&gt;
+          &lt;li&gt;&lt;strong&gt;Public&lt;/strong&gt;
+        &lt;% end -%&gt;
+        &lt;% cache ({ :related =&gt; controller, :id =&gt; @cache_id }) do -%&gt;
+          &lt;%= related_items_display_of @public_items[zoom_class],
                                        { :are_still_images =&gt; are_still_images,
                                          :item =&gt; item,
                                          :zoom_class =&gt; zoom_class,
                                          :display_num =&gt; display_num,
-                                         :total_num =&gt; @private_item_counts[zoom_class],
-                                         :privacy_type =&gt; 'private' } -%&gt;
-        &lt;/li&gt;
+                                         :total_num =&gt; @public_item_counts[zoom_class] } -%&gt;
+        &lt;% end -%&gt;
+        &lt;%= '&lt;/li&gt;' unless only_public_items -%&gt;
+      &lt;% end -%&gt;
+      &lt;% if @private_item_counts[zoom_class] &gt; 0 -%&gt;
+        &lt;%= '&lt;li&gt;' unless only_public_items -%&gt;
+        &lt;strong&gt;Private&lt;/strong&gt;
+        &lt;%= related_items_display_of @private_items[zoom_class],
+                                     { :are_still_images =&gt; are_still_images,
+                                       :item =&gt; item,
+                                       :zoom_class =&gt; zoom_class,
+                                       :display_num =&gt; display_num,
+                                       :total_num =&gt; @private_item_counts[zoom_class],
+                                       :privacy_type =&gt; 'private' } -%&gt;
+        &lt;%= '&lt;/li&gt;' unless only_public_items -%&gt;
       &lt;% end -%&gt;
-    &lt;/ul&gt;
+    &lt;%= '&lt;/ul&gt;' unless only_public_items -%&gt;
   &lt;% elsif display_no_item_text -%&gt;
     &lt;h4&gt;This &lt;%= pretty_class_plural.singularize -%&gt; is not related to any topics at this time.&lt;/h4&gt;
   &lt;% end -%&gt;</diff>
      <filename>app/views/topics/_related_items_section.html.erb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>60e78b84c4bf4dfa3d0a3b87e4a6257f575c31ba</id>
    </parent>
  </parents>
  <author>
    <name>Kieran Pilkington</name>
    <email>kieran@katipo.co.nz</email>
  </author>
  <url>http://github.com/kete/kete/commit/06d043040a98f957e8ae1b0d1bdd50e44ef76d9f</url>
  <id>06d043040a98f957e8ae1b0d1bdd50e44ef76d9f</id>
  <committed-date>2009-01-27T21:16:06-08:00</committed-date>
  <authored-date>2009-01-27T21:16:06-08:00</authored-date>
  <message>refinement: adding permissions to the related item links, separating into three caches, disabling restore and remove links when appropriate, making a quick mysql query to see if zoom ones are necessary, when only public related items are being displayed, hide the public label.</message>
  <tree>df139580939dfae125cb96ea7706e190d45d942c</tree>
  <committer>
    <name>Kieran Pilkington</name>
    <email>kieran@katipo.co.nz</email>
  </committer>
</commit>
