Wiki source code of FAQTutorialManual

Version 45.1 by Guillaume Lerouge on 2009/10/28

Show last authors
1 1 Creating a FAQ Application
2
3 This tutorial will show you how to build a Frequently Asked Questions (FAQs) Application much like the one you can find on the [FAQ page>xwiki:FAQ.WebHome]. This is a very simple application that makes use of XWiki's classes, properties, and objects. It also uses a technique that you may frequently use as the basis for several different kinds of applications.
4
5 All through this tutorial you should refer to the [XWiki Data Model> DevGuide.DataModel] for information on XWiki's data model. You might also use the [XWiki Scripting Guide> DevGuide.Scripting] to get you started with scripting in XWiki and manipulating XWiki objects. In addition, this tutorial will introduce the concepts of Authoring Templates and Page Design Sheets, patterns that you will find particularly useful in creating XWiki applications. Completing this tutorial is a recommended prerequisite for anyone who wants to build custom applications on the XWiki engine.
6
7 1.1 Application Overview
8
9 The FAQ application allows users to post a question by entering the question into a simple form field and then submitting the form. The question then appears in a list along with all other questions that have been previously posted. Users can click on the questions to view both the question and answer in a consistently styled view. If the question has not yet been answered, any user can post an answer to the question by editing the page. In edit mode, the page will display a web form that is the same for every FAQ page.
10
11 Let us begin by taking a look at what we are going to build. The system has the following views. Click any view link below to preview a screenshot. (Note: Firefox users can middle click or click down on the mouse scroll-wheel to open the links in a new tab.)
12
13 * {attach:The FAQs Summary View|FAQsSummary.png}
14 * {attach:A Question and Answer Page in Display Mode|FAQSheetView.png}
15 * {attach:A Question and Answer Page in Edit Mode|FAQSheetEdit.png}
16 1.1 Authoring Templates and Page Design Sheets
17
18 An Authoring Template is a template for creating documents of a specific type. Unlike a regular content page in edit mode with one field for freeform editing, an Authoring Template presents a custom set of form fields for creating a document with specific type of data. These form elements are defined by the properties of a class.
19
20 In object oriented programming, remember that a ~~class~~ is a template for an object. Using the analogy of a cookie cutter, the ~~class~~ is the ~~cookie cutter~~ and the ~~objects~~ are the actual ~~cookies~~. An Authoring Template provides one way to represent a class visually so that users can fill out a form to set unique properties (values in form fields). When the user submits the form, they are creating a unique ~~object~~ of the ~~class~~ type.
21
22 Precisely, an Authoring Template is a prototype document used to create other specific instances of documents of the same type, along with a method of exposing the creation process to the user: the properties edit form. Remember that a XWiki Document can contain objects and this is the case of an authoring template: it is a XWiki Document with an empty object of a specific class, which is duplicated to create more and more documents based on that model, using the data inserted by the user in the editing form as specific values for the particular object instance in the current copy.
23
24 The Page Design Sheet is like a style sheet that defines what each document will look like when rendered. Even though the unique object instances will have different values for their properties, they will always have a consistent presentation display because they will be rendered through the Page Design Sheet. The XWiki API available in scripting languages provides a mechanism that will help us use the same sheet for both editing and view modes. We will see how we can achieve this once we get to the FAQ Design Sheet section.
25
26 1.1 Go to the Class Editor Wizard
27
28 Five pages, which collectively make up a Class Editor Wizard application, have been developed to assist you in this process. Enter a search query for the keyword "XWikiClasses". This should return a document called XWikiClasses in the XWiki space (i.e. XWiki.XWikiClasses). This is the homepage of the class wizard creator: you are now ready to start building your FAQs application.
29
30 1.1 Create the FAQ Class
31
32 * On the Class Editor wizard entry page (XWiki.XWikiClasses), under the heading "Create a new data type", enter the following web space and class name:
33 ** Space: FAQ
34 ** Class: FAQ
35
36 {image:CreateANewClass.PNG}
37
38 * Click the "Create this Class" button. You should then see a code page with the following code:
39
40 {code:none}
41 {{velocity}}
42 ## Replace Main with the Space where you want your documents to be created.
43 ## Replace the default parent with the one of your choice and save the document.
44 ##
45 #set($className = $doc.name.substring(0, $doc.name.indexOf("Class")))
46 #set($defaultParent = "${doc.space}.${className}Class")
47 #set($defaultSpace = 'Main')
48 {{/velocity}}
49 {{include document="XWiki.ClassSheet"/}}
50
51 {code}
52
53 In the code, change the word "Main" with the name of the space where you want you FAQ pages to be created as the commented instructions in the page code suggest. Replace the word "Main" with the word "FAQ". The line of code should now look like this:
54
55 {code:none}
56 #set($defaultSpace = 'FAQ')
57
58 {code}
59
60 You can also change the default parent of the new FAQ documents that are going to be created. To do so, replace the variables with the name of your document. The line of code should now look like this:
61
62 {code:none}
63 #set($defaultParent = 'FAQ.WebHome')
64
65 {code}
66
67 Click the "Save & View" button. The class is now created and you should be looking at a page titled "Class: FAQ" that looks like this:
68
69 {image:FAQClass1.PNG}
70
71 1.1 Add Properties to the Class
72
73 Under the page title, you should see the words "The class does not have any properties yet. You can use the __class editor__ to define them." Let's just follow those instructions!
74
75 * Click on the 'class editor' link: a list of existing XClasses will be displayed
76 * Note that the link trail in the header is something like "FAQ: XWiki Space » Data types » FAQClass". This shows you that you are indeed on the class page.
77
78 In our document, we'll store both a ~~question~~ and an ~~answer~~. So we need a property for each.
79
80 * Enter the text ~~question~~ in the "NAME" field of the Class Editor panel: the "ADD PROPERTY" panel is in the right column.
81 * Choose a TextArea type for the property and then click the "ADD PROPERTY" button. The TextArea will ultimately give us a multi-line text field in our authoring template.
82
83 {image:AddQuestionProperty.PNG}
84
85 * Configure this property with the following values (actually, if you are using the current XWiki version, you don't need to change anything but the Pretty name – all the rest are the default values):
86
87 {image:QuestionProperty.PNG}
88
89 * Now add a property called ~~answer~~ in the same way that you did for the "question" property (choosing TextArea for the property type)
90 * Choose it from the property list on the left and configure this property with the same values as the "question" property, except for the ~~name~~ and ~~pretty name~~ which will, obviously, match the new property we're creating
91 * When you are done adding and configuring the properties, click the "Save & View" button.
92
93 1.1 Create the Page Design Sheet
94
95 * After the previous step you are now on the FAQClass page which should look like this:
96
97 {image:FAQClass2.PNG}
98
99 * Click the first button ("CREATE THE DOCUMENT SHEET") to create the document sheet (the Page Design Sheet). This sheet determines how your page's objects will be rendered to the user. The document is automatically created.
100 * Click on "View the sheet document"
101 * Edit that page in Object edition mode
102 * Using the menu on the right, add a XWiki.SheetClass object to the page
103 ** *Adding the XWiki.SheetClass object is important. It's because of this object that users will be sent to form edition mode when editing FAQ entries*
104 * Click "Save & View"
105 * If you edit the page in wiki mode, you will see the following code:
106
107 {code:none}
108
109 {{velocity}}
110 ## You can modify this page to customize the presentation of your object.
111 ## At first you should keep the default presentation and just save the document.
112 = Document $doc.name =
113
114 #set($class = $doc.getObject('FAQ.FAQClass').xWikiClass)
115
116 #foreach($prop in $class.properties)
117 ; $prop.prettyName
118 : $doc.display($prop.getName())
119 #end
120 {{/velocity}}
121 {code}
122
123 Let's take a moment now and analyze this code:
124
125 * The first line retrieves the FAQ.FAQClass from the wiki
126 * Then we iterate through all its properties and display their values for the current document in a definition list.
127
128 As we mentioned, XWiki provides a mechanism that help us create sheets used for both view and edit mode: this is the display function used in the line: {code:none} : $doc.display($prop.getName()){code}. It detects the current mode (edit or view) and displays the property referenced by its argument as the mode dictates:
129 * In view mode it will display the value of the property
130 * In edit mode it will display a form field that will allow the user to edit it
131
132 This way we can use a single Design Sheet to both display and edit our FAQ entries. See the [XWiki API reference > DevGuide.API] and [XWiki Scripting > DevGuide.Scripting] pages for more details about this.
133
134 * Click "Save & View"
135
136 1.1 Create the Authoring Template
137
138 * Click on "FAQClass" in the breadcrumb. The document should look like this:
139
140 {image:FAQClass3.PNG}
141
142 * Notice that now, there is a link for the FAQSheet in place of the button that was previously there
143 * Click on the "CREATE THE DOCUMENT TEMPLATE" button. The Authoring Template will be automatically created. If you click on "View the template document" and edit that page in wiki mode, you will see the following code:
144
145 {code:none}
146 {{include document="FAQ.FAQSheet"/}}
147 {code}
148
149 * Note that we changed the space name preceding the page name also because we want all of our FAQ pages to reside in the same XWiki space
150 * Remember that all our documents will be copies of the Authoring Template used as a prototype so the content will be copied in all our FAQs documents and will execute the Design Sheet code in the context of the current document. See the [dedicated page > DevGuide.IncludeInVelocity] for more information regarding this technique.
151
152 Now we need to associate the prototype object with this document to turn it into a true authoring template.
153
154 * If you're on the template page, click on "FAQClass" in the breadcrumb to get back to the FAQ Class page
155 * At the bottom of the page, look for the following error message: "The template does not contain an object of type FAQClass. Add a FAQ object to the template »."
156 * Click on "Add a FAQ object to the template »"
157 * Congratulations: you just created an Authoring Template! You're almost done now.
158
159 {image:FAQClass4.PNG}
160
161 1.1 Create a homepage for your application
162
163 You want your users to be able to see a list of all existing questions and answers and to add new questions. The best way to do this is to put the FAQ application in its own space and to use that space's homepage to display existing questions.
164
165 * Go to the "FAQ.WebHome" page
166 * Edit that page in wiki mode
167 * Input a title such as "FAQs" in the title field
168
169 1.1.1 Displaying existing FAQ entries
170
171 You will need to write the following code:
172
173 * A HQL query that will find all your FAQ documents
174 ** The HQL query looks for all documents that have a FAQ.FAQClass object other than the template
175 ** If no document has been created yet, a warning message is displayed
176 * A piece of velocity tode to display all those documents
177 ** The velocity code loops in that list
178 ** For each item, the full document is loaded in memory so that values can be retrieved from it
179 ** For each document, the question is retrieved and displayed as a link towards the FAQ entry
180
181 Here's the resulting code:
182
183 {code:none}
184 = Existing FAQ entries =
185
186 {{velocity}}
187 #set($sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='FAQ.FAQClass' and obj.name<>'FAQ.FAQTemplate'")
188 #set($results = $xwiki.searchDocuments($sql))
189 #if($results.empty)
190 No FAQ has been created yet!
191 #else
192 #foreach ($item in $results)
193 #set($faq = $xwiki.getDocument($item))
194 * [[${faq.display("question").replace("<p>", "").replace("</p>", "")}>>${item}]]
195 #end
196 #end
197 {{/velocity}}
198 {code}
199
200 * Copy that code and paste it in your page
201 * Click "Save and View"
202 * New FAQ entries will now be displayed on the page once you create them
203
204 1.1.1 Creating new FAQ entries
205
206 Now you need to write some code to let your users create new FAQ entries. To do this, you will have to create a form in which the user can enter the name of the questions she wants to create. Once typed in, the form calls the same page to trigger the new document creation based on the parameters entered by the user:
207
208 * The first part of the code checks whether the page has a parameter. If so:
209 ** The name of the document that will be created is computed
210 ** The name is cleaned to remove extra characters
211 ** A check is done to verify the document doesn't exist yet
212 ** If everything's ok, the user is sent to the new document in inline edition mode
213 * The second part of the code is the actual FAQ creation form
214 ** It builds the name of the document to create it in the "FAQ" space
215 ** It sets the document parent as being the current document
216 ** It defines the template to use to create the new document
217
218 {code:none}
219 {{velocity}}
220 #if("$!request.docName" != '')
221 ## Request for creating a new instance
222 #set($targetDocName = "${request.spaceName}.${request.docName}")
223 #set($targetDocName = $targetDocName.replace("?", "").replace("#", "").trim())
224 #if(!$xwiki.exists($targetDocName) && $xwiki.hasAccessLevel('edit', $context.user, $targetDocName))
225 $response.sendRedirect($xwiki.getURL($targetDocName, 'inline', "template=${request.template}&parent=${request.parent}"))
226 ## Stop processing, since we already sent a redirect.
227 #stop
228 #end
229 #end
230
231 = Add a new question =
232
233 #if("$!targetDocName" != '' && $xwiki.exists($targetDocName))
234 {{warning}}The target document already exists. Please choose a different name, or [[view the existing document>>$targetDocName]]{{/warning}}
235 #elseif("$!targetDocName" != '')
236 {{warning}}You don't have permission to create that document{{/warning}}
237 #end
238
239 {{html}}
240 <form action="" id="newdoc" method="post">
241 <div>
242 <input type="hidden" name="parent" value="${doc.fullName}"/>
243 <input type="hidden" name="template" value="FAQ.FAQTemplate"/>
244 <input type="hidden" name="sheet" value="1"/>
245 <input type="hidden" name="spaceName" value="FAQ"/>
246 Document: <input type="text" name="docName" value="Enter your question here" class="withTip" size="50"/>
247 <span class="buttonwrapper"><input type="submit" value="Create this FAQ" class="button"/></span>
248 </div>
249 </form>
250 {{/html}}
251 {{/velocity}}
252 {code}
253
254 * Copy that code and paste it in your page, below the code that's already there
255 * Click "Save and View"
256 * A form to create new FAQ entries is now available on the page
257
258 1.1 Test the Application
259
260 Now let's just create a new document in our application to test it out.
261
262 * Under the "Add a new question" header, enter a document title in the ~~Document~~ field and click ~~Create this FAQ~~. For example, enter ~~What is the meaning of life?~~.
263 * You can then enter your question in longer form using the ~~Question~~ field on the template, like this:
264
265 {image:FAQSheetEdit.PNG}
266
267 * Click ~~Save & View~~ and then you will see the newly created document, like this:
268
269 {image:FAQSheetView.PNG}
270
271 * Go back to the "FAQ.WebHome" page to see the list of existing questions
272
273 1.1 Conclusion
274
275 This tutorial has shown how to use the Class Wizard app and it has detailed the concepts of classes, objects, properties and introduced the authoring templates and page design sheets. You may also have learned a little bit about Velocity scripting in documents. You can use these basic concepts to build custom applications at the document or presentation layer of XWiki without having to compile or deploy code.
276
277 As always, please take the time to make this document better for other users if you find ways that it can be improved as you read it for the first time.

Get Connected