3.1.2Special document properties
Based on the standard property types there can be used some additional input types within the cmsWorks editors desktop.
An int property can be used for simple int values but also for a checkbox or a dropdown input.
A text property can be used as a grid input element.
Following is how the special inputs are used while generating websites.
Checkbox
If a checkbox is checked, the value 1 is stored, if not the value 0 is stored. To fetch the document property value use simply:
boolean checked = 1 == mrPage.getInt(PT_CHECKBOX_PROP_NAME, 0);
DropDown
The property type for this input element is Integer.
As an example there is a property articletype of type Integer within the document type article. To change the input field to a dropdown the configuration has to be placed within the q-custom.js of the editors desktop configuration.
"document": {
"article": {
"articletype_prop": {
"xtype": "itwselectbox",
"options": [
[ 0,'--- no type ---'],
[ 1,'News' ],
[ 2,'Interview' ],
[ 3,'Background story'],
[ 4,'Gossip']
]
}
}
}In the example articles can be of type News, Interview, Background story or Gossip. To use the type names and IDs within JSPs to generate websites creating a helper class in an JSF include is recommended to map this "feature set" and to be able to reuse it in a second JSP if necessary.
<%@page pageEncoding="UTF-8"
%><%!
/** Defines a list of constants to name the types of articles
*/
public static class ArticleType {
final static int NO_TYPE = 0;
final static int NEWS = 1;
final static int INTERVIEW = 2;
final static int BACKGROUND_STORY = 3;
final static int GOSSIP = 4;
public static String getName(int id) {
switch (id) {
case NO_TYPE : return "no type";
case NEWS : return "News";
case INTERVIEW : return "Interview";
case BACKGROUND_STORY : return "Background story";
case GOSSIP : return "Gossip";
}
return "unknown";
}
}
%>articletype.jsf to use articletypes by name
Reading the article type property value from the DocumentModel dmArticle is processed:
int articleType = dmArticle.getInt(Types.PT_ARTICLE_TYPE);
String topLine = ArticleType.getName(articleType);
if (articleType == ArticleType.GOSSIP) {
headlineColor = "green";
}
Grid
The property type for this input element is Text.
As an example there is a property somegrid of type Text within the document type article. To change the input field to a Grid the configuration has to be placed within the q-custom.js of the editors desktop configuration.
"document": {
"article": {
"somegrid_prop": {
"xtype": "itwdatagrid",
"info": "Some information about the purpose and the structure of the grid"
}
}
}The input field content is structured by rows, columns and column titles. Cell contents can be either strings or links to documents. Those links are encoded just like the links produced in the standard rich text input element.
In the JSP reading the Grid information the text has to be fetched not from getText() but vom getString(). This is to avoid using the StandardTextFilter changing the links. A textfilter class DataGrid supports the access to the grid contents:
DataGrid dg = new DataGrid(dmPage.getString(PT_SOMEGRID_PROP_NAME));
StringBuffer sbTable = new StringBuffer();
// Create a HTML table
sbTable.append("<table>");
// Create the HEADER
sbTable.append("<tr>");
for (int colIdx = 0; colIdx < dg.getColumnCount(); colIdx++) {
sbTable.append("<th>");
sbTable.append(dg.getTitle(colIdx));
sbTable.append("</th>");
}
sbTable.append("</tr>");
// Create the Body grid
for (int rowIdx = 0; rowIdx < dg.getRowCount(); rowIdx++) {
sbTable.append("<tr>");
for (int colIdx = 0; colIdx < dg.getColumnCount(); colIdx++) {
sbTable.append("<td>");
if (colIdx == 2) {// only the third column has links to documents
try {
sbTable.append(new DocumentModel(dg.getDocumentId(colIdx, rowIdx), dmPage).toLink().getUrl());
}
catch (Throwable t) {
// ignore not existing links to documents
}
}
else {
sbTable.append(dg.getField(colIdx, rowIdx));
}
sbTable.append("</td>");
} // end of for columns
sbTable.append("</tr>");
} // end of for rows
sbTable.append("</table>");
GridMap
If a Text field in a document is configured as in the example Grid, but the purpose is to create just two columns to enter key value pairs and the keys should be unique, there is a helper class that reads the map values into a HashMap.
The DataGridMap textfilter class can be used like this:
String MAP_KEY_COMPONENT_TITLE = "Component Title";
String MAP_KEY_COMPONENT_PICTURE = "Component Some Picture";
DataGridMap dgMap = new DataGridMap(dmPage.getString(PT_SOME_GRID_PROP_NAME));
String componentTitle = dgMap.get(MAP_KEY_COMPONENT_TITLE);
ModelResource dmMed = null;
try {
dmMed = new DocumentModel(dgMap.getDocumentId(MAP_KEY_COMPONENT_PICTURE), dmPage);
}
catch (Throwable t) {}
This Grid should have at least 2 columns. If not, the internal Map of the DataGridMap will be empty.
If no entry for the key Component Title is available the componentTitle value is null.
If no entry for the key Component Some Picture is available, the Exception ResourceNotFoundException will be thrown because the document id is -1 (default for "no value"). The effect after the try-catch block is that the mrMed reference is null.
