Jump to content

Where to put code


Recommended Posts

I am probably missing something obvious here but where should i put the code for an admin Widget for my Addon Module? I want it to live inside my module, not the widgets folder. Is this possible? At the moment it is in my hooks.php like this:

add_hook('AdminHomeWidgets', 1, function() {
    return new AccessCodeWidget();
});

then the class is above. I was trying to put the class into a separate file so that my hooks.php isnt full of loads of code. I tried putting it in its own file AccessCodeWidget.php in the lib folder but then 

$codeToUse = App::getFromRequest('codeToUse');

Stopped working, even if is put Using App; in the file and set the namespace to be that of my module?

Link to comment
Share on other sites

Sorry, I cant seem to edit my post so commenting again to add a bit more detail to the last code sample:

class AccessCodeWidget extends \WHMCS\Module\AbstractWidget
{
    protected $title = 'Generate Access Code';
    protected $description = '';
    protected $weight = 150;
    protected $columns = 1;
    protected $cache = false;
    protected $cacheExpiry = 120;
    protected $requiredPermission = '';

    public function getData()
    {
        return array();
    }

    public function generateOutput($data)
    {
		$codeToUse = App::getFromRequest('codeToUse');
		$codeToSet = '';

if ($codeToUse!=''){
			$fullCode = base64_encode(Tools::encrypt(str_replace('-','',$codeToUse)));
            $codeToSet = substr($fullCode,0,3) . '-' . substr($fullCode,3,3) . '-' . substr($fullCode,6,3);
        }

       	        return <<<EOF
		<br>
		<div class="container-fluid">
		<div class="row">
		<div class="col-xs-12">
		<div class="form-group">

		<div class="input-group">
			<input id="codeToCheck" type="text" class="form-control" placeholder="Enter 9 Digit Code..." value={$codeToUse}>
			<span id="generateCode" class="input-group-btn"><button class="btn btn-info" type="button">Generate</button></span>
		</div>
		<br>
		<div class="input-group">
			<input id="validationCode" type="text" class="form-control" placeholder="___-___-___" value="{$codeToSet}" readonly>
			<span id="copyCode" class="input-group-btn"><button class="btn btn-info" type="button">Copy</button></span>
		</div>


		</div>

		</div>
		</div>
		</div>

		<script>
		$(document).ready(function() {
		$('#generateCode').click(function() {
        refreshWidget('AccessCodeWidget', 'codeToUse=' + $('#codeToCheck').val());
		});
		$('#copyCode').click(function() {
        var copyText = document.getElementById("validationCode");
		copyText.select();
		document.execCommand("copy");
		});
});
</script>
EOF;
    }
}

 

Link to comment
Share on other sites

Place the hook code within the hooks.php file  within the module's folder.  Then to access the class, just put a "use" at the top of the hook such as :

use WHMCS\Module\Addon\YourMODULEname\Widgets\AccessCodeWidget;

and have a folder in the "lib" folder within the module's called "Widgets" and a file within that called "AccessCodeWidget.php" and the auto loader should load up the classes. 

Within AccessCodeWidget.php file, you want to put that in to the Widgets namespace such as:

namespace WHMCS\Module\Addon\YourMODULEname\Widgets;

(widgets is just a name I gave, you could name it whatever you want)

Link to comment
Share on other sites

Hey, thanks for the reply, this is what i tried, I have just tried it again to be sure but the widget doesnt show up. I am thinking it is to do with this line of code:

$codeToUse = App::getFromRequest('codeToUse');

It works when it is in the hooks.php file but not when it is in its own class. Please see the code below for the use statements i include:

namespace WHMCS\Module\Addon\fourmatrix\Widgets;

use APP;
use WHMCS\Session;
use WHMCS\Database\Capsule;
use WHMCS\Service\Service;
use WHMCS\Module\AbstractWidget;
use WHMCS\Module\Addon\fourmatrix\Tools;

class AccessCodeWidget extends \WHMCS\Module\AbstractWidget

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated