[MKDoc-commit] Automagic grouping of audiences by category in preferences form

bruno at mkdoc.demon.co.uk bruno at mkdoc.demon.co.uk
Thu Jan 27 17:55:11 GMT 2005


Log Message:
-----------
Automagic grouping of audiences by category in preferences form

Tags:
----
mkdoc-1-6

Modified Files:
--------------
    mkd/flo/Record:
        Audience.pm
    mkd/flo/User:
        Preferences.pm
    mkd/templates/account/preferences/audiences:
        en.html

-------------- next part --------------
Index: Audience.pm
===================================================================
RCS file: /var/spool/cvs/mkd/flo/Record/Audience.pm,v
retrieving revision 1.7.2.4
retrieving revision 1.7.2.5
diff -Lflo/Record/Audience.pm -Lflo/Record/Audience.pm -u -r1.7.2.4 -r1.7.2.5
--- flo/Record/Audience.pm
+++ flo/Record/Audience.pm
@@ -294,6 +294,37 @@
 
 
 ##
+# $short_label = $self->short_label;
+# --------------------
+#   Returns the 'Label' attribute stripped of anything
+#   that could be considered a category name.
+##
+sub short_label
+{
+    my $self = shift;
+    my $label = $self->{Label};
+    $label =~ s/[^:]*: *//;
+    return $label;
+}
+
+
+##
+# $category = $self->category;
+# --------------------
+#   Returns the 'Label' attribute stripped of anything
+#   but what could be considered a category name.
+##
+sub category
+{
+    my $self = shift;
+    my $label = $self->{Label};
+    return undef unless $label =~ /:/;
+    $label =~ s/ *:.*//;
+    return $label;
+}
+
+
+##
 # $obj = $obj->set_label ($label);
 # --------------------------------
 #   Set the 'Label' attribute to $label.  Returns this object.
Index: Preferences.pm
===================================================================
RCS file: /var/spool/cvs/mkd/flo/User/Preferences.pm,v
retrieving revision 1.11.2.10
retrieving revision 1.11.2.11
diff -Lflo/User/Preferences.pm -Lflo/User/Preferences.pm -u -r1.11.2.10 -r1.11.2.11
--- flo/User/Preferences.pm
+++ flo/User/Preferences.pm
@@ -664,7 +664,7 @@
 # @auds = $obj->audiences;
 # ------------------------
 #   Returns a list of all flo::Record::Audience objects available
-#   to be set, ordered by name, or undef on error.
+#   to be set, ordered by label, or undef on error.
 ##
 sub audiences($)
 {
@@ -681,6 +681,54 @@
     return wantarray ? @{$self->{_audiences_}} : $self->{_audiences_};
 }
 
+##
+# @audiences = $obj->audience_categories();
+# ------------------------------------
+#   Returns a sorted list of all audience categories.
+#   Categories are implicit when a 'label' has ':' in it.
+#   Otherwise items will be in category ''.
+##
+sub audience_categories
+{
+    my $self = shift;
+
+    my $categories;
+    for my $audience (@{$self->audiences})
+    {
+        $categories->{$audience->category} = undef;
+    }
+
+    my @audience_categories;
+    for (keys %{$categories}) { push @audience_categories, $_ };
+
+    @audience_categories = sort { $a cmp $b } @audience_categories;
+
+    return wantarray ? @audience_categories : [@audience_categories];
+}
+
+##
+# @auds = $obj->audiences_by_category ('Fruit');
+# ------------------------
+#   Returns a list of all flo::Record::Audience objects available
+#   to be set that match the specified category, ordered by label, or undef on error.
+##
+sub audiences_by_category
+{
+    my $self = shift;
+    my $category = shift;
+
+    my @AUDIENCES;
+    my @AUDIENCE_BY_CATEGORY;
+
+    my @auds   = $self->audiences;
+    @AUDIENCES = sort { $a->label cmp $b->label } @auds;
+    for (@AUDIENCES)
+    {
+        push @AUDIENCE_BY_CATEGORY, $_ if $_->category eq $category;
+    }
+
+    return wantarray ? @AUDIENCE_BY_CATEGORY : [@AUDIENCE_BY_CATEGORY];
+}
 
 ##
 # @audiences = $obj->audience_names();
Index: en.html
===================================================================
RCS file: /var/spool/cvs/mkd/templates/account/preferences/audiences/Attic/en.html,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -Ltemplates/account/preferences/audiences/en.html -Ltemplates/account/preferences/audiences/en.html -u -r1.1.2.2 -r1.1.2.3
--- templates/account/preferences/audiences/en.html
+++ templates/account/preferences/audiences/en.html
@@ -25,9 +25,28 @@
     customised email newsletters and listings of documents on the site.
   </p>
 
+  <fieldset
+    class="audience-category"
+    petal:repeat="category self/preferences/audience_categories"
+  >
+    <legend
+      petal:condition="true: category"
+      petal:content="category"
+      lang="en"
+      xml:lang="en"
+      dir="ltr"
+    >Fruit</legend>
+
+    <legend
+      petal:condition="false: category"
+      lang="en"
+      xml:lang="en"
+      dir="ltr"
+    >Miscellaneous</legend>
+
   <!--? For all available audiences ?-->
   <div
-    petal:repeat="audience self/preferences/audiences"
+    petal:repeat="audience self/preferences/audiences_by_category --$category"
     petal:omit-tag="string:1"
   >
     <p
@@ -38,7 +57,7 @@
       <label
         for="audience"
         petal:attributes="for id"
-        petal:inner="audience/label"
+        petal:inner="audience/short_label"
       >Audience</label> 
       <br />
       <!--? Get the current value of the user's preference for this audience ?-->
@@ -71,4 +90,5 @@
     </p>
 
   </div>
+  </fieldset>
 </fieldset>


More information about the MKDoc-commit mailing list