Jump to content

Functions to help make WHMCS development easier and more consistent


websavers

Recommended Posts

Hey all,

Here's a few functions that I include in a class found in each module's lib folder that I find super helpful. They basically work around WHMCS's occasional shortsightedness when it comes to how they expect modules to be used. Hope they help others here.

    /**
     * 
     * Called most often by hooks.php to obtain common module
     * vars since they are not passed into the hook as $vars 
     * 
     * NOTE: This only works when directly within the module's
     * lib folder due to use of dirname()
     * 
     * Returns $vars with module settings and lang included
     * *
     * 
     * @return array
     */

    public static function merge_vars(array $vars){
        $modulepath = dirname(__DIR__, 1); //1 = up one
        $modulename = basename($modulepath); //folder name = module name

        // Get module settings from DB
        $pdo = Capsule::connection()->getPdo();
        $statement = $pdo->prepare("SELECT setting,value FROM tbladdonmodules WHERE module=:modulename");
        $statement->execute([':modulename' => $modulename]);
        $settings = $statement->fetchAll(\PDO::FETCH_KEY_PAIR);

        $translations = array();

        // Get module translations
        if (empty($vars['_lang'])){
            $active_lang = $GLOBALS['aInt']->language;
            include_once("$modulepath/lang/$active_lang.php");
            $translations = array('_lang' => $_ADDONLANG);
        }

        return array_merge($vars, $settings, $translations);
    }

    /**
     * Called most often by hooks.php using admin footer hook
     * to obtain service info from request vars.
     * 
     * Sadly there's two URL parameter formats:
     * admin/clientsservices.php?userid=149&productselect=1243
     * admin/clientsservices.php?userid=149&id=1243
     * # Product Addons
     * admin/clientsservices.php?userid=149&productselect=a559
     * admin/clientsservices.php?userid=149&id=1243&aid=559
     * 
     * Returns array with service / hosting plan data
     * *
     * 
     * @return array
     */
    public static function get_adminarea_service(){

        $id = empty($_REQUEST['id'])? $_REQUEST['productselect'] : $_REQUEST['id'];

        if (!empty($_REQUEST['aid'])){ //Addon good format
            $serviceid = $_REQUEST['id'];
            $aid = $_REQUEST['aid'];
            return self::get_addon($serviceid, $aid);
        } 
        else if (str_starts_with($id, 'a')){ //Addon stupid format
            $aid = ltrim($id, 'a');
            $serviceid = Capsule::table('tblhostingaddons')->where('id', '=', $aid)->value('hostingid');
            return self::get_addon($serviceid, $aid); //Do this for consistency of data.
        }
        else{ // Regular Service
            $results = localAPI('GetClientsProducts', array('serviceid' => $id));
            return $results['products']['product'][0];
        }

    }

Note: get_admin_area_service requires PHP 8 due to the str_starts_with function call.

Edited by websavers
Link to comment
Share on other sites

Just realized the get_addon function wasn't included:

    /**
     * 
     * For unknown reasons you can't feed GetClientsAddons 
     * a specific addon ID. The addonid parameter it accepts
     * is for the parent addon, not a provisioned service's addon.
     * 
     * Returns $addon array exactly like the WHMCS API
     * 
     * Reference: https://developers.whmcs.com/api-reference/getclientsaddons/
     * *
     * 
     * @return array
     */

    public static function get_addon($serviceid, $addonid){
        $results = localAPI('GetClientsAddons', array('serviceid' => $serviceid));
        $addon = null;
        foreach ($results['addons']['addon'] as $a){
            if ($a['id'] == $addonid){
                $addon = $a;
                break;
            }
        }
        return $addon;
    }

Note: I realize an SQL query might be more efficient, however ensuring the result formatting matches the array notation and data from GetClientsAddons is important to me, and so this was the simplest way to ensure that consistency.

Link to comment
Share on other sites

  • 1 month later...

Thanks for the great share. However, the below doesn't seem to work as expected. Have you tried this code recently?

However, the module translation doesn't seem to work as expected. I can't seem to find aInt in globals either.

Appreciate your reply 🙂

Thanks!

Link to comment
Share on other sites

6 hours ago, layer said:

However, the module translation doesn't seem to work as expected. I can't seem to find aInt in globals either.

Hm, I think that was set in the Admin Area. Would you be using this in the Client Area? If so, I believe it can be obtained from the PHP session as 'Language' there. If you are in the Admin Area, I suppose we could alternatively get it from `tbladmins`.

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