Redfruits is a stable software to create HML pages or piece of HMTL code, like forms or list.

Now in the last version of redfruits you can use the new ‘insert’ function. This function is present in all the HTML containers (lists, forms, div, fieldsets, etc.). You can insert a component before another component using the id of the second component or by the array index.

For example:

$form = new ADForm();
$form->add(new ADTextField('first_name', 'my first name'));
$form->add(new ADTextField('last_name', 'my last name'));
//new insert function
$form->insert(new ADTextField('year', '2010'), 'last_name');
//Equal to $form->insert(new ADTextField('year', '2010'), 1);
echo $form->render();

Another useful way is to use the other new function in the ADContainer definition: getLastClassStyle($classStyle). This function returns the index of the last component with a given class style. See the next code:

require_once('tcp/wp-content/plugins/redfruits/gui/ADCoreGui.php');

$form = new ADForm();
$form->add(new ADTextField('first_name', 'my first name'))->setClassStyle('style1');
$form->add(new ADTextField('last_name', 'my last name'))->setClassStyle('style2');

$index = $form->getLastClassStyle('style2');
$form->insert(new ADTextField('year', '2010'), $index);

echo $form->render();

RedFruits is growing!!!

See you.

To make a plugin for wordpress using RedFruits is very simple. In the wordpress’ forum was a question about this and we are using that example to show how simple is to make it.

In this example the developer wants to make a plugin that shows a form in a post. Ok, we are going to make a shortcode to show the form.

First of all is to make the plugin folder an file. The plugin’s name will be ‘FormInPost’.
You have to create a folder in the plugins folder called FormInPost and to create a file, inside it,called FormInPost.php. Write the next code in the new file:

<?php
/*
Plugin Name: FormInPost
Plugin URI: http://FormInPost.com
Description: FormInPost example!!
Author: RedFruits team
Version: 1.0
Author URI: http://redfruits.wordpress.com
*/
if (!defined('REDFRUITS_PATH')) define('REDFRUITS_PATH', dirname(dirname(__FILE__)).'/redfruits/');
if (!defined('FORMINPOST_PATH')) define('FORMINPOST_PATH', dirname(__FILE__).'/');
if (!defined('ADMIN_RENDER')) define('ADMIN_RENDER', 'ADWordpressRender');

require_once(REDFRUITS_PATH.'wp-extension/ADWPPlugin.class.php');

class FormInPost extends ADWPPlugin
{
	function __construct()
	{
		parent::__construct();
		$this->addShortCode('FormInPost', FORMINPOST_PATH.'shortcodes/FormInPostSC.class.php', 'FormInPostSC');
	}
}
new FormInPost();
?>

Now, the shortcode will be created in a folder (inside FormInPost folder). The new folder will be named like shortcodes. Inside it, you have to create FormInPostSC.class.php. You have to Write the next code in the new file:

<?php
class FormInPostSC extends ADLayer
{
	function render()
	{
		if (is_single()) //only shows when the post is being displayed, not in the category list
		{
			$this->setClassStyle('FormInPost');
			$form = $this->add(new ADForm('FormInPost_form'));
			$form->add(new ADLabeledField('First name:', new ADTextField('first_name', '', 20, 40)));
			$form->add(new ADLabeledField('Last name:', new ADTextField('last_name', '', 20, 40)));
			$form->add(new ADSubmit('FormInPost_save', 'save'));
			$form->setValues($_REQUEST);
			$form->addRequirement('first_name', 'required', 'the first name field must be completed');
			if (isset($_REQUEST['FormInPost_save']))
				if ($form->runRequirements($_REQUEST))
				{
					//TODO save the data
					global $post;
					echo 'TODO: Save data \'', $_REQUEST['first_name'], '\' and \'', $_REQUEST['last_name'], '\' in post id ',$post->ID, '';
				}
			return parent::render(ADMIN_RENDER);
		}
	}
}
?>

The most important thing is to make a class (RedFruits is an Object Oriented API) that implements ADWPPlugin.

The form in the second file draws in the explorer two Textboxes, first name and last name, and a submit button. the first name is a required field (see the addRequirement function). When the submit is pressed the page is reloaded and the if (isset($_REQUEST['FormInPost_save'])) is true. The runRequirements function test for the required field.

Remember to write [FormInPost] (see the first param in the addShortCode function) in the post where you want to show the form.

That’s all.

But now you want to change the look. This must be made with the renders. In next posts we are going to see how to make a render.

See you.

The last release of RedFruits has some changes.

ADContainer

Now you can add an array of components (ADComponent) instead to add one per one. See the next code to see the differences:

The traditional way

<?php
require_once(REDFRUITS_PATH.'gui/ADCoreGui.php');
$form = new ADForm();
$form->add(new ADLabeledField('Firts name:', new ADTextField('first_name', '', 20, 40)));
$form->add(new ADLabeledField('Last name:', new ADTextField('last_name', '', 20, 40)));
$form->add(new ADSubmit('save', 'save'));
echo $form->render('ADWordpressRender');

The new way

$form = new ADForm();
$form->add(array(
	new ADLabeledField('Firts name:', new ADTextField('first_name', '', 20, 40)),
	new ADLabeledField('Last name:', new ADTextField('last_name', '', 20, 40)),
	new ADSubmit('save', 'save')
));
echo $form->render('ADWordpressRender');
?>

ADUList

In this version you can add children with the new array way using the new function called addContainer.
ADUList is an ADContainer but it’s rendered in different way. Each component added to the ADUList component is rendered within a ‘LI’ tag.
If you want to add more than one component to a ‘LI’ you need to make an ADContainer or use the new function.

The most useful example for this new feature is the classical menu:

<ul>
	<li>Menu A
		<ul>
			<li> sub menu A 1</li>
			<li> sub menu A 2</li>
		</ul>
	</li>
	<li>Menu B
		<ul>
			<li> sub menu B 1</li>
			<li> sub menu B 2</li>
		</ul>
	</li>
</ul>

With RedFruits:

$menu = new ADUList();
$menu-&gt;addContainer(array (
	'Menu A',
	new ADUList(array(
		'menu A 1',
		'menu A 2'
	)),
));
$menu-&gt;addContainer(array (
	'Menu B',
	new ADUList(array(
		'menu B 1',
		'menu B 2'
	))
));
echo $menu;

RedFruits is growing!!! That’s all for now.

Redfruits uses Renders to ‘render’ the pages (or others objects) into HTML. Redfruitsh has a default render called ADDefaultRender.

You can create a render from zero, or better to extend the default render. The ADWordpressRender is the more successfully render of the Redfruits’ renders.

For example, with redfruits, you can define a form in this way:

<?php
require_once(REDFRUITS_PATH.'gui/ADCoreGui.php');
$form = new ADForm();
$form->add(new ADLabeledField('Firts name:', new ADTextField('first_name', '', 20, 40)));
$form->add(new ADLabeledField('Last name:', new ADTextField('last_name', '', 20, 40)));
$form->add(new ADSubmit('save', 'save'));

echo $form;
echo $form->render('ADWordpressRender');
?>

Redfruits renders this example in two different formats, as you can see in the next piece of html code:

Default render:

<form  method="post">
<label  for="first_name">Firts name:</label>
<input type="text" id="first_name" name="first_name" value="" size="20" maxlength="40"/>
<label  for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name" value="" size="20" maxlength="40"/>
<input type="submit" id="save" name="save" value="save"/>
</form>
<div class="wrap"><form  name="frm" id="internalId_0" method="post">

WordPressRender:

<div class="clear"></div>
<table class="form-table">
<tr valign="top">
<th scope="row"><label  for="first_name">Firts name:</label>

</td>
<td><input type="text" id="first_name" name="first_name" value="" size="20" maxlength="40"/></td>
</tr>
<tr valign="top">
<th scope="row"><label  for="last_name">Last name:</label>
</td>
<td><input type="text" id="last_name" name="last_name" value="" size="20" maxlength="40"/></td>
</tr>
</table>

<p class="submit"><input type="submit" id="save" class="button-primary" name="save" value="save"/></p>
</form>
</div>

If you have a web site create with Redfruits, you can change its look by making others renders, or you can create a plugin for WordPress reusing your code.
In next posts we see how to create a WordPress’ plugin with Redfruits, and then, we can see the ‘power’ of the Redfruits’ render for WordPress.

You can see an example in this successfully plugin called TWPCTree. This plugin has a widget to show category trees for custom taxonomies, one of the new features in WordPress 3.0.

Thats’ all for now!!!

No problem. RedFruits has an Object called ADPage. This object allows to create HTML pages and of course page layouts.

How RedFruits implements layouts?

It’s so simple, please read the next example:

//
//FILE: AdminLayout.class.php
//
require_once(REDFRUITS_PATH.'redfruits/gui/ADCoreGui.php');

class AdminLayout extends ADPAge
{
	function __construct($title = 'Admin layout')
	{
		parent::__construct($title);
		$this->addCssPath('layout.css');
		$header = $this->add(new ADLayer('header'));
		$header->add($this->getHeader());
		$bodyContent = $this->add(new ADLayer('body-content'));
		$leftMenu = $bodyContent->add(new ADLayer('left-menu'));
		$leftMenu->add($this->getLeftMenu());
		$content = $bodyContent->add(new ADLayer('main-content'));
		$content->add($this->getContent());
		$footer = $this->add(new ADLayer('footer'));
		$footer->add($this->getFooter());
	}

	function getHeader()
	{
		return new ADTitle1($this->getTitle());
	}

	function getLeftMenu()
	{
		$ul = new ADUList();
		$postMnu = $ul->add(new ADContainer());
		$postMnu->add('Posts');
		$postMnu = $postMnu->add(new ADUList('mnu_posts'));
		$postMnu->add(new ADLink('posts.php', 'posts'));
		$postMnu->add(new ADLink('new_post.php', 'new post'));

		$pagesMnu = $ul->add(new ADContainer());
		$pagesMnu->add('Pages');
		$pagesMnu = $pagesMnu->add(new ADUList('mnu_pages'));
		$pagesMnu->add(new ADLink('pages.php', 'pages'));
		$pagesMnu->add(new ADLink('new_page.php', 'new page'));
		return $ul;
	}

	function getContent()
	{
		return 'content';
	}

	function getFooter()
	{
		return '2010 &copy; RedFruits';
	}
}

In this code we have defined a PHP class that extends the redfruits’ class called ADPage and It has four ‘get’ functions: getHeader, getLeftMenu, getContent and getFooter.

Now the simple way to make admin pages for this web sites is to write classes like this:

require_once('AdminLayout.class.php');
class PostEdit extends AdminLayout
{
	function __construct()
	{
		parent::__construct();
	}

	function getContent()
	{
		$w = new ADContainer();
		$w->add(new ADTitle2('Post Edit'));
		$form = $w->add(new ADForm());
		$form->add(new ADHiddenField('post_id'));
		$form->add(new ADLabel('Title:', 'title'));
		$form->add(new ADTextField('title', '', 40, 100));
		$form->add(new ADLabel('Content:', 'content'));
		$form->add(new ADTextArea('content', '', 40, 10));
		$form->add(new ADLabel('Excerpt:', 'excerpt'));
		$form->add(new ADTextArea('excerpt', '', 40, 3));
		$form->add(new ADSubmit('save', 'save'));
		$form->setValues($_REQUEST);
		return $w;
	}
}
$p = new PostEdit();
echo $p->render('ADSimpleRender');

See the code in action:A page extending the class AdminLayout
to see the example in this way you must use this css file:

div.header {
	border-bottom: 1px solid gray;
}
div.left-menu {
	width: 20%;
	float: left;
}

div.main-content {
	border-left: 1px solid gray;
	float: left;
}

div.main-content {
	padding: 1em;
}

div.footer {
	border-top: 1px solid gray;
	clear: both;
}

In this post we have use a lot of new classes. Now, we list them:
ADLayer -> div,
ADContainer -> not HTML coincidence. It helps to grouped a set of controls,
ADForm -> form,
ADHiddenField -> input type hidden
ADLabel -> label,
ADTextField -> input type text,
ADTextArea -> text area.

As you can see this Redfruits example make an hypothetical page to edit post in an hypothetical CMS.
That’s all for now

To load data in a select control you must use the object ADListModel. See the next little example:

<?php
$form = new ADForm();
...
$sel = $form->add(new ADSelectField('continents'));
$sel->setListModel(new ContinentsListModel());
...
echo $form->render();

class ContinentsListModel extends ADListModel
{
    function __construct()
    {
        $this->addValue('AF', 'Africa');
        $this->addValue('AM', 'America');
        $this->addValue('AS', 'Asia');
        $this->addValue('EU', 'Europa');
        $this->addValue('OC', 'Oceania');
    }
}
?>

There are some other ways to make this. I think the above code is more clear, but you can do this:

<?php
$form = new ADForm();
...
$sel = $form->add(new ADSelectField('continents'));
$list = new ADListModel();
$list->addValue('AF', 'Africa');
$list->addValue('AM', 'America');
$list->addValue('AS', 'Asia');
$list->addValue('EU', 'Europa');
$list->addValue('OC', 'Oceania');
$sel->setListModel($list);

echo $form->render();
?>

In this example, we are loading the data programmatically, but to get data from a data base and load them in the select control has the same difficult. RedFruits has a objects to access to data base. We’ll look later.

You can see that with RedFruits there are differents ways to solve the code needs. You only have to choose the near one to your programming needs.

That’s all!!

To Load the request values in a form it’s as simple as this:

<?php
//form example
$form = new ADForm();
$form->add(new ADTextField('name'));
$form->add(new ADCheckBoxField('type'));
...
$form->setValues($_REQUEST);
...

echo $form->render();
?>