Add basic profile field editing.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 14 Jan 2011 10:07:29 +0000 (11:07 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 14 Jan 2011 10:07:29 +0000 (11:07 +0100)
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
src/main/resources/i18n/sone.en.properties
src/main/resources/static/css/sone.css
src/main/resources/templates/editProfile.html

index 4f7e1f3..fc1a0bb 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web;
 
+import java.util.Map;
+
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.Page.Request.Method;
@@ -63,6 +65,7 @@ public class EditProfilePage extends SoneTemplatePage {
                Integer birthDay = profile.getBirthDay();
                Integer birthMonth = profile.getBirthMonth();
                Integer birthYear = profile.getBirthYear();
+               Map<String, String> fields = profile.getFields();
                if (request.getMethod() == Method.POST) {
                        if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
                                firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
@@ -75,8 +78,45 @@ public class EditProfilePage extends SoneTemplatePage {
                                profile.setMiddleName(middleName.length() > 0 ? middleName : null);
                                profile.setLastName(lastName.length() > 0 ? lastName : null);
                                profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
+                               for (int fieldIndex = 0; fieldIndex < profile.getFieldNames().size(); ++fieldIndex) {
+                                       String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + fieldIndex, 400);
+                                       profile.setField(fieldIndex, value);
+                               }
                                currentSone.setProfile(profile);
+                               webInterface.getCore().saveSone(currentSone);
                                throw new RedirectException("index.html");
+                       } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
+                               String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
+                               try {
+                                       profile.addField(fieldName);
+                                       currentSone.setProfile(profile);
+                                       fields = profile.getFields();
+                                       webInterface.getCore().saveSone(currentSone);
+                               } catch (IllegalArgumentException iae1) {
+                                       dataProvider.set("fieldName", fieldName);
+                                       dataProvider.set("duplicateFieldName", true);
+                               }
+                       } else {
+                               int deleteFieldIndex = getFieldIndex(request, "delete-field-");
+                               if (deleteFieldIndex > -1) {
+                                       throw new RedirectException("deleteProfileField.html?field=" + deleteFieldIndex);
+                               }
+                               int moveUpFieldIndex = getFieldIndex(request, "move-up-field-");
+                               if (moveUpFieldIndex > -1) {
+                                       profile.moveFieldUp(moveUpFieldIndex);
+                                       currentSone.setProfile(profile);
+                                       throw new RedirectException("editProfile.html#profile-fields");
+                               }
+                               int moveDownFieldIndex = getFieldIndex(request, "move-down-field-");
+                               if (moveDownFieldIndex > -1) {
+                                       profile.moveFieldDown(moveDownFieldIndex);
+                                       currentSone.setProfile(profile);
+                                       throw new RedirectException("editProfile.html#profile-fields");
+                               }
+                               int editFieldIndex = getFieldIndex(request, "edit-field-");
+                               if (editFieldIndex > -1) {
+                                       throw new RedirectException("editProfileField.html?field=" + editFieldIndex);
+                               }
                        }
                }
                dataProvider.set("firstName", firstName);
@@ -85,6 +125,30 @@ public class EditProfilePage extends SoneTemplatePage {
                dataProvider.set("birthDay", birthDay);
                dataProvider.set("birthMonth", birthMonth);
                dataProvider.set("birthYear", birthYear);
+               dataProvider.set("fields", fields);
        }
 
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Searches for a part whose names starts with the given {@code String} and
+        * extracts the number from the located name.
+        *
+        * @param request
+        *            The request to get the parts from
+        * @param partNameStart
+        *            The start of the name of the requested part
+        * @return The parsed number, or {@code -1} if the number could not be
+        *         parsed
+        */
+       private int getFieldIndex(Request request, String partNameStart) {
+               for (String partName : request.getHttpRequest().getParts()) {
+                       if (partName.startsWith(partNameStart)) {
+                               return Numbers.safeParseInteger(partName.substring(partNameStart.length()), -1);
+                       }
+               }
+               return -1;
+       }
 }
index f372edf..de68d65 100644 (file)
@@ -70,7 +70,17 @@ Page.EditProfile.Birthday.Title=Birthday
 Page.EditProfile.Birthday.Label.Day=Day:
 Page.EditProfile.Birthday.Label.Month=Month:
 Page.EditProfile.Birthday.Label.Year=Year:
+Page.EditProfile.Fields.Title=Custom Fields
+Page.EditProfile.Fields.Description=Here you can enter custom fields into your profile. These fields can contain anything you want and be as terse or as verbose as you wish. Just remember that when it comes to anonymity, sometimes less is more.
+Page.EditProfile.Fields.Button.Edit=edit
+Page.EditProfile.Fields.Button.MoveUp=move up
+Page.EditProfile.Fields.Button.MoveDown=move down
+Page.EditProfile.Fields.Button.Delete=delete
+Page.EditProfile.Fields.AddField.Title=Add Field
+Page.EditProfile.Fields.AddField.Label.Name=Name:
+Page.EditProfile.Fields.AddField.Button.AddField=Add Field
 Page.EditProfile.Button.Save=Save Profile
+Page.EditProfile.Error.DuplicateFieldName=The field name “{fieldName}” does already exist.
 
 Page.CreatePost.Title=Create Post - Sone
 Page.CreatePost.Page.Title=Create Post
@@ -169,6 +179,7 @@ WebInterface.DefaultText.LastName=Last name
 WebInterface.DefaultText.BirthDay=Day
 WebInterface.DefaultText.BirthMonth=Month
 WebInterface.DefaultText.BirthYear=Year
+WebInterface.DefaultText.FieldName=Field name
 WebInterface.DefaultText.Option.InsertionDelay=Time to wait after a Sone is modified before insert (in seconds)
 WebInterface.Confirmation.DeletePostButton=Yes, delete!
 WebInterface.Confirmation.DeleteReplyButton=Yes, delete!
index def0be0..bbe6fd4 100644 (file)
@@ -438,6 +438,28 @@ textarea {
        display: inline;
 }
 
+#sone .profile-field {
+       margin-top: 1em;
+}
+
+#sone .profile-field .name {
+       display: inline;
+       font-weight: bold;
+}
+
+#sone .profile-field .value {
+       margin-left: 2em;
+}
+
+#sone #edit-profile .profile-field .value {
+       margin-left: inherit;
+}
+
+#sone .profile-field .edit-field-name, #sone .profile-field .move-up-field, #sone .profile-field .move-down-field, #sone .profile-field .delete-field-name {
+       float: right;
+       margin-top: -1ex;
+}
+
 #sone #tail {
        margin-top: 1em;
        border-top: solid 1px #ccc;
index 918b0c1..a58ace4 100644 (file)
@@ -20,6 +20,9 @@
                        getTranslation("WebInterface.DefaultText.BirthYear", function(birthYearDefaultText) {
                                registerInputTextareaSwap("#sone #edit-profile input[name=birth-year]", birthYearDefaultText, "birth-year", true, true);
                        });
+                       getTranslation("WebInterface.DefaultText.FieldName", function(fieldNameDefaultText) {
+                               registerInputTextareaSwap("#sone #edit-profile input[name=field-name]", fieldNameDefaultText, "field-name", true, true);
+                       });
 
                        /* hide all the labels. */
                        $("#sone #edit-profile label").hide();
                        <button type="submit" name="save-profile" value="true"><%= Page.EditProfile.Button.Save|l10n|html></button>
                </div>
 
+               <h1><%= Page.EditProfile.Fields.Title|l10n|html></h1>
+
+               <p><%= Page.EditProfile.Fields.Description|l10n|html></p>
+
+               <%foreach fields field fieldLoop>
+                       <div class="profile-field">
+                               <div class="name"><% field.key|html></div>
+                               <div class="edit-field-name"><button type="submit" name="edit-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.Edit|l10n|html></button></div>
+                               <div class="delete-field-name"><button type="submit" name="delete-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.Delete|l10n|html></button></div>
+                               <div class="<%if fieldLoop.last>hidden <%/if>move-down-field"><button type="submit" name="move-down-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.MoveDown|l10n|html></button></div>
+                               <div class="<%if fieldLoop.first>hidden <%/if>move-up-field"><button type="submit" name="move-up-field-<% fieldLoop.count>" value="true"><%= Page.EditProfile.Fields.Button.MoveUp|l10n|html></button></div>
+                               <div class="value"><input type="text" name="field-<% fieldLoop.count>" value="<% field.value|html>" /></div>
+                       </div>
+               <%/foreach>
+
+               <a name="profile-fields"></a>
+               <h2><%= Page.EditProfile.Fields.AddField.Title|l10n|html></h2>
+
+               <%if duplicateFieldName>
+                       <p><%= Page.EditProfile.Error.DuplicateFieldName|l10n|replace needle="{fieldName}" replacementKey="fieldName"|html></p>
+               <%/if>
+
+               <div id="new-field">
+                       <label for="new-field"><%= Page.EditProfile.Fields.AddField.Label.Name|l10n|html></label>
+                       <input type="text" name="field-name" value="" />
+                       <button type="submit" name="add-field" value="true"><%= Page.EditProfile.Fields.AddField.Button.AddField|l10n|html></button>
+               </div>
+
+               <div>
+                       <button type="submit" name="save-profile" value="true"><%= Page.EditProfile.Button.Save|l10n|html></button>
+               </div>
+
        </form>
 
 <%include include/tail.html>