On the template repository, you can find an example in the file ModuleTemplate/Lib/TemplateConf.php
You can use it for your project according to your preferences. The name of this class should be consist of a module unique identifier and ends with the Conf word.
Good examples:
MyFirstModuleConf
MikoCallTrackingConf
TheBestModuleConf
Non-workable examples:
TemplateConf
MyFirstModuleConf1
TheBestModuleConfig
After applying the instructions your config class will be renamed automatically.
Asterisk configs generators
generateConfig
Generates core modules config files with cli messages before and after generation.
Example:
publicfunctiongenerateConfig():void;
extensions.conf
extensionGlobals
Prepares additional parameters for [globals] section in extensions.conf file
Example:
publicfunctionextensionGlobals():string;
extensionGenContexts
Prepares additional contexts sections in extensions.conf file
Example:
/*** Adds the context "miko_new_context" with the extension "10000107" * * @returnstring*/publicfunctionextensionGenContexts():string{ $conf ="[miko_new_context]\n"; $conf .='exten => 10000107,1,Answer()'."\n\t"; $conf .=' same => n,Set(CHANNEL(hangup_handler_wipe)=hangup_handler_meetme,s,1)'."\n\t"; $conf .=' same => n,AGI(cdr_connector.php,meetme_dial)'."\n\t"; $conf .=' same => n,Meetme(${mikoidconf},${mikoparamconf})'."\n\t"; $conf .=' same => n,Hangup()'."\n\n";return $conf;}
getIncludeInternal
Prepares additional includes for [internal] context section in the extensions.conf file
Example:
/*** Prepares additional includes for [internal] context section in the extensions.conf file** @returnstring*/publicfunctiongetIncludeInternal():string{ $conf =''; $settings =ModuleSmartIVR::findFirst();if ($settings !==null) { $conf ='include => module_smartivr '.PHP_EOL; }return $conf;}
extensionGenInternal
Prepares additional rules for [internal] context section in the extensions.conf file
Example:
publicfunctionextensionGenInternal():string;
getIncludeInternalTransfer
Prepares additional includes for [internal-transfer] context section in the extensions.conf file
Prepares additional parameters for each incoming context for each incoming route before dial in the extensions.conf file
Example:
/*** Prepares additional parameters for each incoming context for each * incoming route before dial in the extensions.conf file** @paramstring $rout_number** @returnstring*/publicfunctiongenerateIncomingRoutBeforeDialPreSystem(string $rout_number):string{return'';}
generateIncomingRoutBeforeDial
Prepares additional parameters for each incoming context for each incoming route before dial in extensions.conf file
Example:
/*** Sends AMI UserEvent with name Interception and the next parameters:* CALLERID(num), CHANNEL, FROM_DID on every incomming call.** @param $rout_number** @returnstring*/publicfunctiongenerateIncomingRoutBeforeDial(string $rout_number):string{ $conf ="\t"; $conf ='same => n,UserEvent(Interception,CALLERID: ${CALLERID(num)},chan: ${CHANNEL},FROM_DID: ${FROM_DID})'; return $conf;}
generateIncomingRoutBeforeDialSystem
Prepares additional parameters for each incoming context for each incoming route before dial in the extensions.conf file
Example:
/** * Prepares additional parameters for each incoming context for each * incoming route before dial in the extensions.conf file * * @paramstring $rout_number * * @returnstring */publicfunctiongenerateIncomingRoutBeforeDialSystem(string $rout_number):string {return''; }
generateIncomingRoutAfterDialContext
Prepares additional parameters for each incoming context * and incoming route after dial command in an extensions.conf file
Override pjsip options for provider in the pjsip.conf file
Example:
/*** Override pjsip options for provider in the pjsip.conf file** @paramstring $uniqid the provider unique identifier* @paramarray $options list of pjsip options** @returnarray*/publicfunctionoverrideProviderPJSIPOptions(string $uniqid,array $options):array{}
overridePJSIPOptions
Override pjsip options for peer in the pjsip.conf file
Prepares additional parameters for [featuremap] section in the features.conf file
Example:
publicfunctiongetFeatureMap():string;
Other
/** * Prepares settings dataset for a PBX module */publicfunctiongetSettings():void;/** * Returns the messages variable * * @returnarray */publicfunctiongetMessages():array;/** * Returns models list of models which affect the current module settings * * @returnarray */publicfunctiongetDependenceModels():array;
getMethodPriority
This method allows overriding the execution priority of a method when called through hookModulesMethod. By defining this method in the Conf class of a module, you can flexibly control the execution priority of HOOK methods. This provides the ability to specify a high priority for a method that generates CRON tasks in an external module, and a low priority for a method that generates peers for pjsip.conf. By changing the priority, you can control the order in which module methods are applied.
Example:
/** * Allows overriding the execution priority of a method when called * through hookModulesMethod. * * @paramstring $methodName The name of the method. * @returnint The priority value assigned to the method. */publicfunctiongetMethodPriority(string $methodName =''):int{switch ($methodName) {caseSystemConfigInterface::CREATE_CRON_TASKS: $result =200; // Set a high priority for the method "CREATE_CRON_TASKS"break;caseAsteriskConfigInterface::GENERATE_PEERS_PJ: $result =50000; // Set a low priority for the method "GENERATE_PEERS_PJ"break;default: $result =$this->priority; }return $result;}}
REST API Core generators
getPBXCoreRESTAdditionalRoutes
Returns array of additional routes for PBXCoreREST interface from module
/*** Process CoreAPI requests under root rights** @paramarray $request** @returnPBXApiResult*/publicfunctionmoduleRestAPICallback(array $request):PBXApiResult{ $res =newPBXApiResult(); $res->processor =__METHOD__; $action =strtoupper($request['action']);if($action ==='CHECK'){ $templateMain =newPT1CCoreMain(); $res = $templateMain->checkModuleWorkProperly(); } else { $res->success =false; $res->messages[] ='API action not found in moduleRestAPICallback'; }return $res;}
System generators
System events
onAfterPbxStarted
The callback function will execute after PBX started
Example:
publicfunctiononAfterPbxStarted():void;
onBeforeModuleEnable
Process before enable action in web interface
Example:
publicfunctiononBeforeModuleEnable():bool;
onAfterModuleEnable
Process after enable action in web interface
Example:
/*** Process enable action** @returnvoid*/publicfunctiononAfterModuleEnable():void{PBX::dialplanReload();}
onBeforeModuleDisable
Process before disable action in web interface
Example:
publicfunctiononBeforeModuleDisable():bool;
onAfterModuleDisable
Process after disable action in web interface
Example:
/*** Process after disable action in web interface** @returnvoid*/publicfunctiononAfterModuleDisable():void{PBX::dialplanReload();}
modelsEventChangeData
This method calls in the WorkerModelsEvents after receive each models change
Example:
/*** Receive information about mikopbx main database changes** @param $data*/publicfunctionmodelsEventChangeData($data):void{// f.e. if user changes PBXLanguage, we will restart all workersif ( $data['model'] ===PbxSettings::class&& $data['recordId'] ==='PBXLanguage' ){ $templateMain =newTemplateMain(); $templateMain->startAllServices(true); }}
modelsEventNeedReload
This method calls in the WorkerModelsEvents after finished process models changing
Adds assets to the assets manager after it has been prepared.
Example:
/*** Modifies the system assets on the fly.* * @paramPhalcon\Assets\Manager $assets The assets manager * for additional modifications from module.* @paramPhalcon\Mvc\Dispatcher $dispatcher The dispatcher instance.** @returnvoid*/publicfunctiononAfterAssetsPrepared(Manager $assets,Dispatcher $dispatcher):void{ $currentController = $dispatcher->getControllerName(); $currentAction = $dispatcher->getActionName();if ($currentController==='Extensions'and $currentAction==='modify') { $assets->collection(AssetProvider::FOOTER_JS)->addJs("js/cache/{$this->moduleUniqueId}/extensions-modify.js",true); $assets->collection(AssetProvider::HEADER_CSS)->addCss("css/cache/{$this->moduleUniqueId}/extensions-modify.css",true); }}
ACL
authenticateUser
Authenticates a user over an external module.
Example:
/*** Authenticates a user over an external module.** @paramstring $login The user login entered on the login page.* @paramstring $password The user password entered on the login page.** @returnarray The session data.*/publicfunctionauthenticateUser(string $login,string $password):array{if ($login==='cdrView'&& $password==='cdrView' ) {return ['role'=>'cdrView','homePage'=>'call-detail-records/index' ]; }return [];}
onAfterACLPrepared
Adds roles and permissions to the ACL list after it has been prepared.
Example:
/** * Adds roles and permissions to the ACL list after it has been prepared. * * @paramAclList $aclList The ACL list. * * @returnvoid */publicfunctiononAfterACLPrepared(AclList $aclList):void{// Add role for CDR viewers $aclList->addRole(newAclRole('cdrView','Only CDR viewers'));// Add components and permissions for Extensions $aclList->addComponent(newComponent('Extensions'), ['index']);// Allow CDR viewers to access Extensions $aclList->allow('cdrView','Extensions', ['index']);// Add components and permissions for Call Detail Records $aclList->addComponent(newComponent('CallDetailRecords'), ['index','get-new-records']);// Allow CDR viewers to access Call Detail Records (all actions) $aclList->allow('cdrView','CallDetailRecords','*');// Add components and permissions for ModuleUsersUiNewMenuItem $aclList->addComponent(newComponent('ModuleUsersUiNewMenuItem'), ['index']);// Allow CDR viewers to access ModuleUsersUiNewMenuItem $aclList->allow('cdrView','ModuleUsersUiNewMenuItem',['index']);// Add components and permissions for /pbxcore/api/sip $aclList->addComponent(newComponent('/pbxcore/api/sip'), ['getPeersStatuses']); // Allow CDR viewers to access /pbxcore/api/sip with getPeersStatuses action $aclList->allow('cdrView','/pbxcore/api/sip',['getPeersStatuses']);// Add components and permissions for /pbxcore/api/advices $aclList->addComponent(newComponent('/pbxcore/api/advices'), ['getList']);// Allow CDR viewers to access /pbxcore/api/advices with getList action $aclList->allow('cdrView','/pbxcore/api/advices',['getList']);}
Routes
onAfterExecuteRoute
Example:
/** * Executes after a route has been executed by the controller. * * @paramController $controller The controller instance. * * @returnvoid */publicfunctiononAfterExecuteRoute(Controller $controller):void{// Check if the controller is an instance of ExtensionsControllerif (is_a($controller,ExtensionsController::class)) {// Intercept the form submission of Extensions with // fields mod_usrgr_select_group and user_id $userGroup = $controller->request->getPost('mod_usrgr_select_group'); $userId = $controller->request->getPost('user_id');// Check if the user group field is not emptyif (!empty($userGroup)) { $parameters = ['conditions'=>'user_id = :user_id:','bind'=> ['user_id'=> $userId, ] ];// Find the existing group membership based on user ID $curUserGroup =GroupMembers::findFirst($parameters);// Update or create the group membershipif ($curUserGroup !==null) {// Update the group ID with the selected user group $curUserGroup->group_id = $userGroup; } else {// Create a new group membership $curUserGroup =newGroupMembers(); $curUserGroup->user_id = $userId; $curUserGroup->group_id = $userGroup; }// Save the changes to the database $curUserGroup->save(); } }}
Volt
onVoltBlockCompile
The function compiles a Volt block and provides the file path of the compiled template. All available blocks can be found in the src/AdminCabinet/Views folder. For example, in the Asterisk manager modify form, there is a block called MainFields, which is represented in the Volt file using the following syntax:
/** * Compiles a Volt block and returns the compiled template file path. * * @paramstring $controller The name of the controller. * @paramstring $blockName The name of the block. * @paramView $view The View instance. * * @returnstring The compiled template file path. */publicfunctiononVoltBlockCompile(string $controller,string $blockName,View $view):string{ $result ='';// Combine the controller and block name to create a unique case identifier $caseIdentifier ="$controller:$blockName";// Check the combined case identifier to determine the template file pathswitch ($caseIdentifier) {case'AsteriskManagers:MainFields':// Add fields to the main tab on the AsteriskManager edit page $result ="{$this->moduleDir}/App/Views/AsteriskManagers/mainfields";break;case'Extensions:GeneralTabFields':// Add fields to the general tab in the advanced settings section // on the Extension edit page $result ="{$this->moduleDir}/App/Views/Extensions/generaltabfields";break;case'Extensions:TabularMenu':// Add an additional tab to the Extension edit page $result ="{$this->moduleDir}/App/Views/Extensions/tabularmenu";break;case'Extensions:AdditionalTab':// Add content for an additional tab on the Extension edit page $result ="{$this->moduleDir}/App/Views/Extensions/additionaltab";break;default:// No specific template file path for other case identifiers }return $result;}
Forms
onBeforeFormInitialize
Initializes the form before it is rendered. It useful to add an extra form fields before it compile.
Example:
/** * Initializes the form before it is rendered. * * @paramForm $form The form object. * @parammixed $entity The entity associated with the form. * @parammixed $options Additional options for the form initialization. * * @returnvoid */publicfunctiononBeforeFormInitialize(Form $form, $entity, $options):void{// Check if the form is an instance of ExtensionEditFormif (is_a($form,ExtensionEditForm::class)) {// Define an array of DTMF types with their corresponding translations $arrDTMFType = ['auto'=>$this->translation->_('auto'),'inband'=>$this->translation->_('inband'),'info'=>$this->translation->_('info'),'rfc4733'=>$this->translation->_('rfc4733'),'auto_info'=>$this->translation->_('auto_info'), ];// Create a Select element for the 'module_dtmfmode' field $dtmfmode =newSelect('module_dtmfmode', $arrDTMFType, ['using'=> ['id','name', ],'useEmpty'=>false,'value'=>'auto','class'=>'ui selection dropdown', ] );// Add the DTMF mode field to the form $form->add($dtmfmode); }}
Sidebar menu
onBeforeHeaderMenuShow
Modifies the sidebar menu items before it is shown.
Example:
/** * Modifies the sidebar menu items before it is shown. * * @paramarray $menuItems The menu items for modifications. * * @returnvoid */publicfunctiononBeforeHeaderMenuShow(array&$menuItems):void{// Add Module Users UI Admin menu item $menuItems['ModuleUsersUIAdmin']=['caption'=>'module_usersUiMainMenuItem','iconclass'=>'','submenu'=>['/module-users-ui/access-groups'=>['caption'=>'module_usersUiAccessGroups','iconclass'=>'gear','action'=>'index','param'=>'','style'=>'', ], ] ];}