<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Enabling WYSIWYG</title><meta name="generator" content="DocBook XSL Stylesheets V1.70.1"><link rel="home" href="index.html" title="OpenACS Core Documentation"><link rel="up" href="tutorial-advanced.html" title="Chapter�10.�Advanced Topics"><link rel="previous" href="tutorial-schedule-procs.html" title="Scheduled Procedures"><link rel="next" href="tutorial-parameters.html" title="Adding in parameters for your package"><link rel="stylesheet" href="openacs.css" type="text/css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><a href="http://openacs.org"><img src="/doc/images/alex.jpg" border="0" alt="Alex logo"></a><table width="100%" summary="Navigation header" border="0"><tr><td width="20%" align="left"><a accesskey="p" href="tutorial-schedule-procs.html">Prev</a> </td><th width="60%" align="center">Chapter�10.�Advanced Topics</th><td width="20%" align="right"> <a accesskey="n" href="tutorial-parameters.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="tutorial-wysiwyg-editor"></a>Enabling WYSIWYG</h2></div></div></div><div class="authorblurb"><p>by <a href="mailto:nima.mazloumi@gmx.de" target="_top">Nima Mazloumi</a></p> OpenACS docs are written by the named authors, and may be edited by OpenACS documentation staff. </div><p>Most of the forms in OpenACS are created using the form builder, see <a href="form-builder.html" title="Using Form Builder: building html forms dynamically">the section called “Using Form Builder: building html forms dynamically”</a>. For detailed information on the API take a look <a href="/api-doc/proc-view?proc=ad_form" target="_top">here</a>.</p><p>The following section shows how you can modify your form to allow WYSIWYG functionalities.</p><p>Convert your page to use <code class="code">ad_form</code> (some changes but worth it)</p><p>Here an examples. From:</p><pre class="programlisting"> template::form create my_form template::element create my_form my_form_id -label "The ID" -datatype integer -widget hidden template::element create my_form my_input_field_1 -html { size 30 } -label "Label 1" -datatype text -optional template::element create my_form my_input_field_2 -label "Label 2" -datatype text -help_text "Some Help" -after_html {<a name="#">Anchor</a>} </pre><p>To:</p><pre class="programlisting"> ad_form -name my_form -form { my_form_id:key(acs_object_id_seq) {my_input_field_1:text,optional {label "Label 1"} {html {size 30}}} {my_input_field_2:text {label "Label 2"} {help_text "Some Help"} {after_html {<a name="#">Anchor</a>}}} } ... </pre><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Warning</h3><p>You must not give your your form the same name that your page has. Otherwise HTMLArea won't load.</p></div><p>Convert your textarea widget to a richtext widget and enable htmlarea.</p><p>The <code class="code">htmlarea_p</code>-flag can be used to prevent WYSIWYG functionality. Defaults to true if left away.</p><p>From:</p><pre class="programlisting"> {my_input_field_2:text </pre><p>To:</p><pre class="programlisting"> {my_input_field_2:richtext(richtext) {htmlarea_p "t"} </pre><p>The richtext widget presents a list with two elements: text and content type. To learn more on existing content types search in Google for "MIME-TYPES" or take a look at the <code class="code">cr_mime_types</code> table.</p><p>Make sure that both values are passed as a list to your <code class="code">ad_form</code> or you will have problems displaying the content or handling the data manipulation correctly.</p><p>Depending on the data model of your package you either support a content format or don't. If you don't you can assume <code class="code">"text/html"</code> or <code class="code">"text/richtext"</code> or <code class="code">"text/enhanced"</code>.</p><p>The relevant parts in your <code class="code">ad_form</code> definition are the switches <code class="code">-new_data</code>, <code class="code">-edit_data</code>, <code class="code">-on_request</code> and <code class="code">-on_submit</code>.</p><p>To allow your data to display correctly you need to add an <code class="code">-on_request</code> block. If you have the format stored in the database pass this as well else use <code class="code">"text/html"</code>:</p><pre class="programlisting"> set my_input_field_2 [template::util::richtext::create $my_input_field_2 "text/html"] </pre><p>Now make sure that your SQL queries that do the data manipulation retrieve the correct value. If you simply use <code class="code">my_input_field_2</code> you will store a list. Thus you need to add an <code class="code">-on_submit</code> block:</p><pre class="programlisting"> set my_input_field_2 [ template::util::richtext::get_property contents $my_input_field_2] set format [ template::util::richtext::get_property format $my_input_field_2] #This is optional </pre><p>Now the correct values for <code class="code">my_input_field_2</code> and <code class="code">format</code> are passed to the <code class="code">-new_data</code> and <code class="code">-edit_data</code> blocks which don't need to get touched.</p><p>To make HTMLArea optional per package instance define a string parameter <code class="code">UseWysiwygP</code> which defaults <code class="code">0</code> for your package using the APM.</p><p>In your edit page make the following changes</p><pre class="programlisting"> # Is WYSIWYG enabled? set use_wysiwyg_p [parameter::get -parameter "UseWysiwygP" -default "f"] ... {htmlarea_p $use_wysiwyg_p} </pre><p>The <code class="code">-on_request</code> switch should set this value for your form.</p><pre class="programlisting"> set htmlarea_p $use_wysiwyg_p </pre><p>All you need now is a configuration page where the user can change this setting. Create a <code class="code">configure.tcl</code> file:</p><pre class="programlisting"> ad_page_contract { This page allows a faq admin to change the UseWysiwygP setting } { {return_url ""} } set title "Should we support WYSIWYG?" set context [list $title] set use_wysiwyg_p ad_form -name categories_mode -form { {enabled_p:text(radio) {label "Enable WYSIWYG"} {options {{Yes t} {No f}}} {value $use_wysiwyg_p} } {return_url:text(hidden) {value $return_url}} {submit:text(submit) {label "Change"}} } -on_submit { parameter::set_value -parameter "UseWysiwygP" -value $enabled_p if {![empty_string_p $return_url]} { ns_returnredirect $return_url } } </pre><p>In the corresponding ADP file write</p><pre class="programlisting"> <master> <property name="title">@title@</property> <property name="context">@context@</property> <formtemplate id="categories_mode"></formtemplate> </pre><p>And finally reference this page from your admin page</p><pre class="programlisting"> #TCL: set return_url [ad_conn url] #ADP: <a href=configure?<%=[export_url_vars return_url]%>>Configure</a> </pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-schedule-procs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-parameters.html">Next</a></td></tr><tr><td width="40%" align="left">Scheduled Procedures </td><td width="20%" align="center"><a accesskey="u" href="tutorial-advanced.html">Up</a></td><td width="40%" align="right"> Adding in parameters for your package</td></tr></table><hr><address><a href="mailto:docs@openacs.org">docs@openacs.org</a></address></div><a name="comments"></a><center><a href="http://openacs.org/doc/current/tutorial-wysiwyg-editor.html#comments">View comments on this page at openacs.org</a></center></body></html>