Skip to content

Plugin and Back Again

Andy Theuninck edited this page Oct 3, 2017 · 2 revisions

The idea here is to create a plugin that intercepts controls, does some work, then yields control back again.

First we'll need a plugin class:

plugins/BounceBounce/BounceBounce.php

<?php

use COREPOS\pos\plugins\Plugin;

class BounceBounce extends Plugin
{
}

This class doesn't do anything other than make our plugin properly discoverable.

Next we need a parser to grab control:

plugins/BounceBounce/BBParser.php

<?php

use COREPOS\pos\parser\Parser;

class BBParser extends Parser
{
    public function check($str)
    {
        return $str == 'BOUNCE';
    }

    public function parse($str)
    {
        $ret = $this->default_json();
        $plugin = new BounceBounce();
        $ret['main_frame'] = $plugin->pluginUrl() . '/BBPage.php';

        return $ret;
    }
}

When the user enters BOUNCE we're going to redirect the browser to a page in our plugin, BBPage.php.

plugins/BounceBounce/BBPage.php

<?php

use COREPOS\pos\lib\gui\NoInputCorePage;
use COREPOS\pos\lib\MiscLib;
use COREPOS\pos\lib\TransRecord;
include_once(__DIR__ . '/../../lib/AutoLoader.php');

class BBPage extends NoInputCorePage
{
    public function preprocess()
    {
        try {
            $input = $this->form->reginput;
            if (is_numeric($input)) {
                TransRecord::addhousecoupon('BOUNCE', 0, $input/100, 'BOUNCE!');
            }
            if (is_numeric($input) || trim(strtoupper($input) == 'CL')) {
                $this->change_page(MiscLib::baseURL() . '/gui-modules/pos2.php');
                return false;
            }
        } catch (Exception $ex) { }

        return true;
    }

    public function body_content()
    {
        $this->addOnloadCommand("\$('#reginput').focus()");
        echo <<<HTML
<div class="baseHeight">
  <div class="centeredDisplay colored">
    <span class="larger">Bounce Plugin</span>
    <form name="overrideform" method="post" id="bounceform">
      <input type="text" id="reginput" name='reginput' tabindex="0" onblur="$('#reginput').focus()" />
    </form>
    <span>Enter a price</span>
    <p>
      <span class="smaller">[clear] to cancel</span>
    </p>
  </div>
</div>
HTML;
}

AutoLoader::dispatch();

There's a little more going on here. The content of the page is a form that accepts one input. In preprocess() we're examining that input. If it's a number we're going to add some kind of coupon to the transaction (it's a contrived example). If it's a number or if the user pressed clear we're going to return control from our plugin. In all other cases we'll just show the input form again.

There's some obvious lacking functionality here. If the user enters help it'd be nice to change something about the display indicating what they did wrong. But this covers the minimum necessary to move the browser to a plugin page and then send it back again.

Clone this wiki locally
You can’t perform that action at this time.