Expansions advanced:


Here there are some advanced example about Webmatic expansions, see Expansions basis and APIs references for more information about Webmatic expansions.

You can download this expansion examples (and other expansions) from www.valarsoft.com.


Form example:


This example shows how to create a page with a simple form and fields validation.

Filename: "config.php" located in "core/expansion/form_example/"

<?php

// Form expansion example (config file)

// An expansion config file must start with this line
defined('WM3_cEXEC') or die('Denied');

// General expansion configuration
$_EXPANSION['id'] = 1003; // Expansion unique ID (must be >= 1000 and different from any other expansion ID)
$_EXPANSION['version'] = '1.2'; // Expansion version
$_EXPANSION['wm_version'] = '3.0.7'; // Minimum Webmatic version needed to use the expansion
$_EXPANSION['language_path'] = 'lang/'; // Expansion language path to use different languages in an expansion

// Administrator stage config
$_EXPANSION['admin']['enabled'] = false;

// User stage config
$_EXPANSION['user']['enabled'] = false;

// Page config
$_EXPANSION[0]['page']['enabled'] = true;
$_EXPANSION[0]['page']['exec'] = 'index.php';

// Panel config
$_EXPANSION[0]['panel']['enabled'] = false;

?>

Index file for form example expansion:

Filename: "index.php" located in "core/expansion/form_example/"

<?php

defined('WM3_cEXEC') or die('Denied');

// Form example
// This example create a page with a simple form and fields validation

$pageID = $w3->getVariable('pageID'); // Take pageID variable from get or post

// Create a wm3GUIForm object
// parameters are: form action, form title and form name
$form_action = 'index.php?section='.WM3_cSECTION_PAGES.'&pageID='.$pageID;
$form = new wm3GUIForm($w3,$form_action,'Form title','form_name');
$form->setSubmitLabel('Send data'); // Set submit button label

// Create form textfields
// Parameters are: field name, field value, field label, field size, field maxlenght
// and a boolean (true for a password field, false for normal textfield)
$name = new wm3GUITextField('name', $w3->getVariable('name'), 'Name:');
$age = new wm3GUITextField('age', $w3->getVariable('age'), 'Age:',10);
$email = new wm3GUITextField('email',$w3->getVariable('email'),'Email:');

// Create form select
// Parameters are: field name, field value, field label, an array of wm3GUIOption objects
$gender = new wm3GUISelect('gender',$w3->getVariable('gender'),'Gender:',array(new wm3GUIOption('Male','1'),new wm3GUIOption('Female','2')));

// Create form checkbox
// Parameters are: field name, field value, field label
$check = new wm3GUICheckBox('check',$w3->getVariable('check'),'Checkme:');

// Add validation for name field (can not be empty)
// Parameters are: validation constant and error message
$name->addValidation(WM3_cVALIDATE_NOT_NULL,'Error: Empty name');

// Add validation for age field (must be >= 18)
// Parameters are: validation constant, error message and an integer
$age->addValidation(WM3_cVALIDATE_GREATER_EQUAL_THAN,'Error: Age must be greater or equal to 18',18);

// Add validation for email field (must be an email)
// Parameters are: validation constant and error message
$email->addValidation(WM3_cVALIDATE_EMAIL,'Error: Invalid email');

// Add the fields to the form
$form->addField($name);
$form->addField($age);
$form->addField($gender);
$form->addField($email);
$form->addField($check);

// Add also an hidden field
$form->addField(new wm3GUIHiddenField('test','1'));

// Execute form
if($form->wasSubmit()) // The form was submit
 {
  if(!$form->validate()) echo $form->render(); // Form validation error (redraw the form)
  else
   {
    // Form validation passed
    echo "Form fields submitted:<br>";
    echo "<br>Name: " .$w3->getVariable('name');
    echo "<br>Age: " .$w3->getVariable('age');
    echo "<br>Gender: " .$w3->getVariable('gender');
    echo "<br>Email: " .$w3->getVariable('email');
    echo "<br>Checkme: ".$w3->getVariable('check');
    echo "<br>Test (hidden field): ".$w3->getVariable('test');
    echo "<br><br>You can now process sended data.";
   }
 }
else
 {
  // Form default values
  $check->setValue(WM3_cENABLED);

  echo $form->render(); // Render the form (first time)
 }

?>


Form example language files content:

Filename: "language_en.php" located in "core/expansion/form_example/lang/"

<?php

// Expansion language file example

// An expansion language file must start with this line
defined('WM3_cEXEC') or die('Denied');

// Expansion words
$this->_WORD_EXPANSION[0] = "Form example"; // Expansion title

?>


List example:


This example shows how to create a simple list from an array of data.

Filename: "config.php" located in "core/expansion/list_example/"

<?php

// List expansion example (config file)

// An expansion config file must start with this line
defined('WM3_cEXEC') or die('Denied');

// General expansion configuration
$_EXPANSION['id'] = 1004; // Expansion unique ID (must be >= 1000 and different from any other expansion ID)
$_EXPANSION['version'] = '1.2'; // Expansion version
$_EXPANSION['wm_version'] = '3.0.7'; // Minimum Webmatic version needed to use the expansion
$_EXPANSION['language_path'] = 'lang/'; // Expansion language path to use different languages in an expansion

// Administrator stage config
$_EXPANSION['admin']['enabled'] = false;

// User stage config
$_EXPANSION['user']['enabled'] = false;

// Page config
$_EXPANSION[0]['page']['enabled'] = true;
$_EXPANSION[0]['page']['exec'] = 'index.php';

// Panel config
$_EXPANSION[0]['panel']['enabled'] = false;

?>

Index file for list example expansion:

Filename: "index.php" located in "core/expansion/list_example/"

<?php

defined('WM3_cEXEC') or die('Denied');

// List example
// This example create a simple list from $data array

$pageID = $w3->getVariable('pageID'); // Take pageID variable from get or post

$data = array(
 array('1','Good', 'Green', '100'),
 array('2','Good', 'Red', '80' ),
 array('3','Bad', 'Yellow','60' ),
 array('4','Very good', 'Blue', '55' ),
 array('5','Normal', 'Orange','90' ),
 array('6','Good', 'Blue', '90' ),
 array('7','Very bad', 'Violet','65' ),
 array('8','Very very good','Violet','120'),
 array('9','Very very bad', 'Red', '12'),
);

// Create a wm3GUIList object
// parameters are: list data (must be an array of objects or an array of arrays), list title
$list = new wm3GUIList($w3,$data,"List title");

// Set labels for list caption
// parameter: an array of strings
$list->setCaptionLabels(array('#ID','Quality','Color','Price'));

// Set widths for list caption (and list columns)
// parameter: an array of integer used as percentage (the sum must be 100)
$list->setCaptionWidths(array(5,45,30,20));

// Create a page navigator for the list (each page have 6 items max)
$list->setTotalItems(count($data)); // The total items
$list->setPagesNavigatorLink('index.php?section='.WM3_cSECTION_PAGES.'&pageID='.$pageID); // Link to be render for list page navigator
$list->setUseAutoPage(true); // Enable the auto page mode
$list->setRenderPagesNavigator(true); // Enable list page navigator
$list->setMaxPageItems(6); // Max 6 items per page
$list->setPage($w3->getVariable('pg')); // Variable used for list pagination

// Highlight an item (the second item)
$list->setCurrentID(1);

// You can also set a content for list bottom
$list->setBottomContent("<hr>Bottom content");

// Render list
echo $list->render();

?>


List example language files content:

Filename: "language_en.php" located in "core/expansion/list_example/lang/"

<?php

// Expansion language file example

// An expansion language file must start with this line
defined('WM3_cEXEC') or die('Denied');

// Expansion words
$this->_WORD_EXPANSION[0] = "List example"; // Expansion title

?>