2010-01-10

How to use the XML helper with XSLT

Important! All Kohana-related posts are now at http://kohana.lillem4n.se.

A friend of mine needed to use the Kohana XML-module to create XML with a XSLT-stylesheet attatched to it. This is a quick example of how you could do that. There are several other ways, but this is pretty straight forward.

The following code should be placed inside a controller method:

// First we need a DOM document do begin with
$dom = new DomDocument('1.0', 'UTF-8');

// Format the output so we can read it more easily
$dom->formatOutput = true;

// This sets the used XSL stylesheet to "example.xsl"
$dom->appendChild($dom->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="example.xsl"'));

// We need a root node in the XML
$xml_root = $dom->appendChild($dom->createElement('root'));

// This is the big XML data... database queries and other nice data should be in here
// Right now there is only a tiny bit of foo-node and bar-content
$someXmlContent = array('foo' => 'bar');

// Put the $someXmlContent inside the root-node
xml::toXML($someXmlContent, $xml_root);

// Write to screen
echo $dom->saveXML();

/** Produces
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<root>
<foo>bar</foo>
</root>
*/

// End of PHP code

Hashes: #kohana xslt #xslt #kohana xml module #kohana module #kohana #xml #php

2010-01-06

New form rules

Important! All Kohana-related posts are now at http://kohana.lillem4n.se.

The module formrules is updated with two new methods: futureTimeStamp() and pastTimeStamp().

This can simply be passed in a form validation to check a string for both validation to strtotime() and so it is either in the future or in the past. Example:

$post->add_rules('someFormField', 'formrules::futureTimeStamp')


This is an update to this post.

Hashes: #kohana form rules module #kohana module #kohana #form validation #php

2009-11-04

Obama and ACTA

This is so important, its found a place even in this blog:
http://www.boingboing.net/2009/11/03/secret-copyright-tre.html

Hashes: #ACTA #obama #copyright #usa #eu

2009-11-03

Kohana modules hack

When adding modules to kohana, most oftenly you need to add that module to the config file to have it loaded. Now I find this mostly an unnecessary hassle. (Its useful when sharing module path between applications, though).

However, just comment out the module settings, replacing it with this code snippet:

$config['modules'] = array();
foreach (scandir(MODPATH) as $folder) {
if ($folder != '.' && $folder != '..') {
$config['modules'][] = MODPATH . $folder;
}
}

That'll load all your modules automatically. :)

Hashes: #kohana #php

Excel export module

Important! All Kohana-related posts are now at http://kohana.lillem4n.se.

This is a excel export module to Kohana. Simpel use like this:
$excelExport = new Excel_export();
$excelExport->addRow(array('first row, first column','first row, second column'));
$excelExport->addRow(array('second row, first column','second row, second column'));
$excelExport->download('myExcelFile.xls');

Full documentation of all available methods and parameters are in the module library source.

Bug reports, comments, improvements; very welcome. :)

Hashes: #kohana excel export module #kohana module #kohana #excel export #php

2009-11-01

Kohana extended form rules

Important! All Kohana-related posts are now at http://kohana.lillem4n.se.

New version available here, a post about this issue is located here.

This is a module with rules you'd like when validating forms in kohana. It is the same concept as the valid::-rules.

For example:
$post->add_rules('some_field', 'required', 'digit', 'formrules::positive');


This will require "some_field" to be a digit (whole number) above zero.

For now, positive is actually the only rule in this module. So it is more like the foundation of a module. :) More rules will follow.

Hashes: #kohana form rules module #kohana module #kohana #form validation #php

2009-10-17

Kohana XML helper module

Important! All Kohana-related posts are now at http://kohana.lillem4n.se.

This module is actually just a helper (for now, at least). But it is very handy when working with converting various data to XML in PHP. A few examples:

Simple example of the two different return values.
As DOMDocument:
<?php
$doc = xml::toXML(array('root'=>array('fnupp'=>'dah')));
$doc->formatOutput = true;

echo $doc->saveXML();
?>


As DOMNode:
<?php
$doc = new DOMDocument();
$container = $doc->appendChild($doc->createElement('root'));

xml::toXML(array('fnupp'=>'dah'), $container);

echo $doc->saveXML();
?>


How the $container works:
xml::toXML(array('fnupp' => 'dah'))

will output:
<fnupp>dah</fnupp>

xml::toXML(array('fnupp' => 'dah'), 'root')

will output:
<root>
<fnupp>dah</fnupp>
</root>

That was just a few examples, full documentation is within the helper PHP-file. That includes passing SQL and getting it back as XML directly, working with attributes etc.

All feedback is very welcome!

Hashes: #kohana xml module #kohana module #kohana #xml #php