Example #1
0
use Meta\Builder\Condition;
use Meta\Builder\Validation;
use Meta\Builder\Object\View;
use Meta\Builder\Object\Form;
route_add('page-sample', function () {
    // page
    $page = new Page('page-sample', array('label' => 'My test page'));
    // listing
    $view = $page->objects[] = new View();
    $view->templateFile = 'builder-view-table.php';
    $view->paginate = true;
    $view->query = array('select' => '*', 'from' => 'users');
    $view->columns[] = new Column(array('id' => 'login', 'label' => 'Login'));
    $view->columns[] = new Column(array('id' => 'name', 'label' => 'Name', 'sortable' => true));
    $view->columns['id'] = new Column(array('id' => 'id', 'label' => 'action'));
    $view->columns['id']->formatters[] = new Formatter(array('command' => 'linkSimple', 'paramValues' => array('edit', '?action=edit&id=[id]')));
    // form edit
    $form = $page->objects[] = new Form(array('label' => 'Edit form'));
    $form->rules[] = new Condition(array('command' => 'parameterHasValue', 'paramValues' => array('action', 'edit')));
    $form->onLoad[] = new Command(array('command' => 'fetchFromTable', 'paramValues' => array('users')));
    $form->fields['id'] = new Field(array('name' => 'id', 'label' => 'ID', 'type' => 'pkeyfield'));
    $form->fields['name'] = new Field(array('name' => 'name', 'label' => 'Name', 'type' => 'text', 'isRequired' => true));
    $form->fields['submit'] = new Field(array('name' => 'submit', 'label' => 'Save', 'type' => 'btn_validated'));
    $form->fields['submit']->commands[] = new Command(array('command' => 'saveRecordInTable', 'paramValues' => array('users')));
    $form->fields['submit']->commands[] = new Command(array('command' => 'messageAlert', 'paramValues' => array('Form submitted!')));
    $form->fields['submit']->commands[] = new Command(array('command' => 'redirectToCurrentPath'));
    // if any errors occurrs, an Exception will raised
    $page->checkErrors();
    //    echo '<pre>' . var_export($page->toArray(), true) . '</pre>';
    echo $page->render();
});