Brian Harned Posted October 6, 2008 Share Posted October 6, 2008 I was hoping to get some help with creating a Rule in FusionPro Desktop for a business card that will then be used on a Printable site. I am a noob to scripting but I'm just looking for direction. I'm not expecting anyone to write the Rule for me. :-) That being said.... I have attached a PDF and sample data. Currently I have 5 rules. Four of the rules are modified versions of the Formatting Phone Numbers rule from this Printable Forum . Those are then referenced in the 'Phone Numbers' rule. I'm hoping to use the single 'Phone Numbers' rule in the FusionPro template to handle everything. There may be up to 4 different phone numbers on the card, but their position will vary depending on which numbers the customer provides. The Main number is required but there could be a Direct, Fax and/or Cell. For instance if the Cell and Fax are present, then Fax is on the same line as Main, and Cell is on line 2. If Direct is present it is always placed on the same line as Main. The first 2 records' contents correspond to the first 2 'if-else' statements but they only work in that order. If I move the second 'if-else' to the bottom of the rule, it is no longer valid. I know I will need to create statements for each data instance, but I can't do that until my samples work. Again, once complete, this template will be posted online for use on a Printable site. The data will always be entered by the client through the UI. Here is the script I have so far. Maybe there is an obvious syntax error that I'm not aware of... if (Field("Direct") != "") if (Field("Fax") != "") if (Field("Cell") != "") { return Rule("Phone# Format_MAIN") + " Direct " + Rule("Phone# Format_DIRECT") + "<br>Fax</br> " + Rule("Phone# Format_FAX") + " Cell " + Rule("Phone# Format_CELL") } else if (Field("Direct") != "") if (Field("Fax") != "") if (Field("Cell") == "") { return Rule("Phone# Format_MAIN") + " Direct " + Rule("Phone# Format_DIRECT") + "<br>Fax</br> " + Rule("Phone# Format_FAX") } else if (Field("Direct") != "") if (Field("Fax") == "") if (Field("Cell") != "") { return Rule("Phone# Format_MAIN") + " Direct " + Rule("Phone# Format_DIRECT") + "<br>Cell</br> " + Rule("Phone# Format_CELL") } else if (Field("Direct") == "") if (Field("Fax") != "") if (Field("Cell") != "") { return Rule("Phone# Format_MAIN") + " Fax " + Rule("Phone# Format_FAX") + "<br>Cell</br> " + Rule("Phone# Format_CELL") } Please contact me if you need any more information. Any assistance will be greatly appreciated. Regards, Brian HarnedBusinessCardsFinal_test1.pdfdata.txt Link to comment Share on other sites More sharing options...
WestPress Posted October 7, 2008 Share Posted October 7, 2008 Brian, try this: if (Field("Direct") != "" && Field("Fax") != "" && Field("Cell") != "") return Rule("Phone# Format_MAIN") + " Direct " + Rule("Phone# Format_DIRECT") + "<p>Fax " + Rule("Phone# Format_FAX") + " Cell " + Rule("Phone# Format_CELL") else if (Field("Direct") != "" && Field("Fax") != "" && Field("Cell") == "") return Rule("Phone# Format_MAIN") + " Direct " + Rule("Phone# Format_DIRECT") + "<p>Fax " + Rule("Phone# Format_FAX") else if (Field("Direct") != "" && Field("Fax") == "" && Field("Cell") != "") return Rule("Phone# Format_MAIN") + " Direct " + Rule("Phone# Format_DIRECT") + "<p>Cell " + Rule("Phone# Format_CELL") else if (Field("Direct") == "" && Field("Fax") != "" && Field("Cell") != "") return Rule("Phone# Format_MAIN") + " Fax " + Rule("Phone# Format_FAX") + "<p>Cell " + Rule("Phone# Format_CELL") else return Rule("Phone# Format_MAIN") Link to comment Share on other sites More sharing options...
Brian Harned Posted October 7, 2008 Author Share Posted October 7, 2008 Thanks a million cdaters! Works like a champ! Link to comment Share on other sites More sharing options...
Dan Korn Posted October 7, 2008 Share Posted October 7, 2008 If the requirements are fully defined in psuedocode, then the actual code follows from that (making some assumptions here to fill in the gaps in the requirements): // There may be up to 4 different phone numbers on the card, // but their position will vary depending on which numbers // the customer provides. // The Main number is required var PhoneMain = Rule("Phone# Format_MAIN"); if (!PhoneMain) ReportError("Main phone number is required!"); // but there could be a Direct, Fax and/or Cell. var PhoneDirect = Rule("Phone# Format_DIRECT"); var PhoneFax = Rule("Phone# Format_FAX"); var PhoneCell = Rule("Phone# Format_CELL"); var Main = PhoneMain; var Direct = PhoneDirect ? " Direct " + PhoneDirect : ""; var Fax = PhoneFax ? " Fax " + PhoneFax : ""; var Cell = PhoneCell ? " Cell " + PhoneCell : ""; var line1 = ""; var line2 = ""; // If Direct is present it is always // placed on the same line as Main. if (Direct) { line1 = Main + Direct; line2 = Fax + Cell; } else { // if the Cell and Fax are present, // then Fax is on the same line as Main, // and Cell is on line 2. if (Cell && Fax) { line1 = Main + Fax; line2 = Cell; } else { // ASSUME that whatever we have goes on one line. line1 = Main + Cell + Fax; } } var result = line1; if (line2) result += "<br>\n" + line2; return result; Note that there is no ending </br> tag in FusionPro. There's just the standalone <br> tag. (FusionPro markup is not well-formed XML.) Note also that I've added some newlines ("\n") to the return values to make the separation of lines clearer at rule validation time. (The extra white space has no effect on the returned markup at composition time. The extra spaces before the labels also go away at composition time.) Link to comment Share on other sites More sharing options...
WestPress Posted October 8, 2008 Share Posted October 8, 2008 Your code is a much more advanced than mine, heh. What does the question mark mean in these lines? var Direct = PhoneDirect ? " Direct " + PhoneDirect : ""; var Fax = PhoneFax ? " Fax " + PhoneFax : ""; var Cell = PhoneCell ? " Cell " + PhoneCell : ""; I like your signature too, that is pretty clever. I had to execute it to see what it did, I'm not THAT familiar with JavaScript. Link to comment Share on other sites More sharing options...
Dan Korn Posted October 8, 2008 Share Posted October 8, 2008 Your code is a much more advanced than mine, heh. Well, it's longer, but I think it's easier to follow and figure out what it's trying to do. It's more maintainable. In other words, if the requirements change, it will be easier to know where to modify the code. What does the question mark mean in these lines? var Direct = PhoneDirect ? " Direct " + PhoneDirect : ""; var Fax = PhoneFax ? " Fax " + PhoneFax : ""; var Cell = PhoneCell ? " Cell " + PhoneCell : ""; The combination of the question mark and colon is called the conditional operator, or sometimes the ternary operator: http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Operators/Special_Operators#conditional_operator also: http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Conditional_Operator It's kind of a shorthand for an if/else statement. For instance, this code: if (age >= 18) status = "adult"; else status ="minor"; Can be replaced with this: status = (age >= 18) ? "adult" : "minor"; Link to comment Share on other sites More sharing options...
Brian Harned Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks for your help too Dan. It really makes more sense to do it that way in the long run. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.