Posts Tagged ‘forms’

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: https://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.

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!!!

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();
?>

RedFruits

Posted: October 13, 2010 in development
Tags: , , ,

In this blog we are going to show the potential of this new API for PHP called RedFruits.

RedFruits is a PHP API (or framework) to write webs in PHP, quickly and safety. It’s an Object Oriented API and allows to write web sites with no HTML code. Web sites and, now, plugins for WordPress.

You can download it from the wordpress’ plugins repository http://wordpress.org/extend/plugins/redfruits/.

RedFruits defines a set of objects to create all the web’s visual components. It can be combined with HTML code, CSS and Javascript. For example you can only made the forms with RedFruits and the rest in HTML classic code. See an example:

<?php
require_once(REDFRUITS_PATH.'gui/ADCoreGui.php');
$form = new ADForm();
$form->add(new ADLabeledField('User:', new ADTextField('name', '', 8, 8)));
$form->add(new ADLabeledField('Pwd:', new ADTextField('pass', '', 8, 8)));
$form->add(new ADSubmit('login', 'login'));
echo $form->render('ADSimpleRender');
?>;

the code below shows a typical login form.