PHP code example of corneltek / actionkit

1. Go to this page and download the library: Download corneltek/actionkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

corneltek / actionkit example snippets


use ActionKit\Action;
class YourAction extends Action
{
    public function run() {
    }
}

function run() {
    return $this->success('Success!!');
}

function run() {
    return $this->success('Success', ['user_id' => 1]);
}

function run() {
    return $this->error('Error', ['user_id' => 1]);
}

class YourAction extends \ActionKit\Action
{

    function schema() {
        $this->param('id')
            ->renderAs('HiddenInput');

        $this->param('password')
            ->renderAs('PasswordInput');

        $this->filterOut('hack','hack2','role');
    }

    function beforeRun() { 
        // do something
    }

    function run()
    {
        return $this->success( 'Success Helper (point to action result object)' );
        return $this->error( 'Error Helper (point to action result object)' );
    }

    function afterRun() 
    {
        // do something
    }
}

$act = new Action( $_REQUEST );
$act->invoke();
$rs = $a->getResult();

if( $result->success ) {

} else {
    // error here

}

$rs = $action->getResult();
if( $rs->success ) {
    $msg = $rs->getMessage();
    $data = $rs->getData();
}

$runner = ActionKit\Runner::getInstance();
if( $result = $runner->getResult( 'Login::...' ) ) {
    // check the action result

}

namespace User\Action\UpdateAction;
use ActionKit\RecordAction\UpdateRecordAction;

class UpdateAction extends UpdateRecordAction {

    function schema() 
    {
        // For record actions, we can convert the record columns
        $this->useRecordSchema();

        $this->param( 'username' )
            ->label( _('Username') )
            ->useSuggestion();

        $this->param( 'password' )
            ->validator(function($value) {
                if ($value) {
                    return [false, "reason"];
                }
                return [true, "success!!"];
            });

        $this->filterOut(array('auth_token'));
    }

    function validatePassword( $value , $args ) 
    {
        return $this->valid( $message );

        # or
        return $this->invalid( $message );
    }

    function suggestUsername( $value , $args ) {
        return;  # not to suggest
        return $this->suggest( "$value is used. use: " , array( ... ) );
    }

    function completeCountry( $value , $args ) {

        ...
    }
}

function successMessage(OperationResult $ret) {
    return _('Your Success Message');
}

function errorMessage(OperationResult $ret) {
    return _('Your Error Message');
}

namespace News\Action;
use ActionKit\RecordAction\CreateRecordAction;

class CreateNews extends CreateRecordAction
{
    public $recordClass = 'News\Model\News';
}

namespace News\Action;
use ActionKit\RecordAction\UpdateRecordAction;

class UpdateNews extends UpdateRecordAction
{
    public $recordClass = 'News\Model\News';
}

$record = new User\Model\User( 3 ); // primary key = 3
$a = new User\Action\UpdateUser(array( 'nickname' => 'New Name' ) , $record );
$a->invoke();   // which calls $record->update(array( 'nickname' => 'New Name' ) );

$g = new ActionKit\ActionGenerator;
$code = $g->generateClassCode( 'App\Model\User' , 'Create' )->code;

$g = new ActionKit\ActionGenerator;
$code = $g->generateClassCode( 'App\Model\User' , 'Update' )->code;

$g = new ActionKit\ActionGenerator;

$g->register('template name','...template path...');

$g->generate('SortImage', 'template name', array(
    "base_class" => "SortRecordAction",
    "record_class" => "ProductBundle\\Model\\ProductImage",
    ... template variable...
));

use ActionKit\RecordAction\BaseRecordAction;
$class = BaseRecordAction::createCRUDClass( 'App\Model\Post' , 'Create' );

$post = new Post;
$update = $post->asUpdateAction();
$create = $post->asCreateAction();
$delete = $post->asDeleteAction();

$post = new Post;
$update = $post->asUpdateAction();
$html = $update->widget('title')->render();
$html = $update->widget('title')->render( array( 'class' => '....' ));

$html = $update->render('title',array( /* attributes.... */ ));
$html = $update->render( null, array( /* attributes */ )  );

class YourAction extends Action {
    function schema() {
        $this->param('name')
            ->renderAs('TextInput');
    }
}

$action = new YourAction;
$widget = $a->widget('name');

$html = $widget->render(array(  
    'class' => 'extra-class'
    'id' => 'field-id'
));

$a->widget('user_type')->render(array( 
    'options' => array(
        'Option 1' => '1'
        'Option 2' => '2'
        'Group Option' => array(
            'Suboption 1' => '2.1'
            'Suboption 2' => '2.2'
        )
    )
));

$a->widget('confirmed','RadioInput')->render(array(
    'false', 'true'
));

$view = $action->createView('+AdminUI\Action\StackView');
$view->render(array( ... render options ... ));

$action = new SomeWhatAction;
$view = new ActionKit\View\StackView($action, array( ... options ... ));
$view->render();

$action = new User\Action\ChangePassword;
$view = new ActionKit\View\StackView( $action );
echo $view->render();

    echo $action->asView('ActionKit\View\StackView')->render();
    echo $action->asView()->render();  // implies view class ActionKit\View\StackView

echo $action->asView('ActionKit\View\StackView', array( ... view options ... ))->render();

function updateAction() {
    $changePasswordAction = new User\Action\ChangePassword( array( 
        ... values to override field values ... ) , $record );

    return $this->render('some_path.html',array( 
        'changePasswordAction' => $changePasswordAction
    ));
}