websavers Posted January 9, 2023 Share Posted January 9, 2023 (edited) 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 January 9, 2023 by websavers 0 Quote Link to comment Share on other sites More sharing options...
websavers Posted January 12, 2023 Author Share Posted January 12, 2023 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. 1 Quote Link to comment Share on other sites More sharing options...
layer Posted February 15, 2023 Share Posted February 15, 2023 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! 0 Quote Link to comment Share on other sites More sharing options...
websavers Posted February 16, 2023 Author Share Posted February 16, 2023 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`. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.