Adding Composers (continued)

Writing the Data

We have to add the next case in the continuations to handle the correct data:

resources/scripts/AddComposer.php
class AddComposer extends Continuations { function add() { global $gModules; $this->gLogger->debug( "Adding entry, continuation: " . $this->gContinuation ); $requestParameterModule = $gModules[ 'request-param' ]; $this->gViolations = array(); $finished = false; while ( !$finished ) { $errors = false; switch ( $this->gContinuation ) { case 0 : ... break; case 1 : ... break; case 2 : // No errors to record from optional data so send next page $this->sendPage( "addComposer-3.html", $this->gAddComposerModel, ++$this->gContinuation, $this->gViolations ); $finished = true; break; } } }

Constructing the SQL Command

Now that there is data to add a suitable SQL entry command has to be created:

/examples/sql/addComposer-3.xml
<page:content> <t:heading level="1">SQL Add Composer</t:heading> <execute-query xmlns="http://apache.org/cocoon/SQL/2.0"> <query name="createEntry" database="composers">INSERT INTO composer ( name, forenames, birth, death ) VALUES ( "{flow:name}", "{flow:firstNames}", "{flow:birth}", "{flow:death}" )</query> </execute-query> <t:p><link:link type="uri" ref="/examples/sql/sql.html">Return to SQL index</link:link></t:p> <t:p><link:link type="uri" ref="/examples/sql/logout.html">Logout</link:link> from admin pages.</t:p> </page:content>

Note the use of the variables that take the flow script variables and are expanded when the PXTemplateGenerator inputs the file.

Extending the Sitemap

A suitable addition is required to the sitemap:

/examples/sql/sitemap.xmap
<map:pipeline> <map:match pattern="addComposer.html"> <map:act type="auth-protect"> <map:parameter name="handler" value="adminHandler"/> <map:call function="AddComposer::add"/> </map:act> </map:match> <map:match pattern="addComposer.kont"> <map:call function="AddComposer::add"/> </map:match> </map:pipeline> <!-- Only called from the add user script AddComposer::add --> <map:pipeline internal-only="true"> <map:match pattern="addComposer-3.html"> <map:aggregate element="root" label="aggr-content"> <map:part src="cocoon:/menus.xml" element="menus" strip-root="true"/> <map:part src="cocoon:/addComposer-3.px" element="content" strip-root="true"/> </map:aggregate> <map:transform type="mysql" label="sql-content"/> <map:transform src="context://resources/transforms/sql2xml.xsl" label="sql-transform"/> <map:call resource="outputPage"/> </map:match> <map:match pattern="addComposer-*.html"> <map:aggregate element="root" label="aggr-content"> <map:part src="cocoon:/menus.xml" element="menus" strip-root="true"/> <map:part src="cocoon:/addComposer-{1}.px" element="content" strip-root="true"/> </map:aggregate> <map:transform src="resource://resources/transforms/pforms-violations.xsl" label="pforms-violations"> <map:parameter name="formViolations" value="{session:__violations}"/> </map:transform> <map:transform src="resource://resources/transforms/pforms-default.xsl" label="pforms-default"/> <map:transform src="resource://resources/transforms/pforms2html.xsl" label="pforms-html"/> <map:call resource="outputPage"/> </map:match> </map:pipeline>

Processing Result

Note that we do not need the PForm transforms in this matcher. Only a transformer that contains the necessary transform to process the returned confirmation data from the SQL server about the success or faiulure of the write data command is required.

resources/transforms/sql2xml.xsl
<xsl:template match="sql:result[ . = 'true' ]"> <xsl:element name="t:p"> <xsl:text>Operation complete!</xsl:text> </xsl:element> </xsl:template> <xsl:template match="sql:result"> <xsl:element name="t:p"> <xsl:text>Problem with operation: </xsl:text> <xsl:value-of select="."/> </xsl:element> </xsl:template>

The transforms that are used are relatively crude, but effective. A succesful write would appear as:

Displaying the data now gives:

The next section shows how to delete information from the database.

Copyright 2006 — 2010 Hugh Field-Richards. All Rights Reserved.