Store
Community Documentation

v3 Knowledgebase

Changing create modules to build file system

I have created several modules over the past few years and with every one I have to refer to a list of what folders are required and tediously set up all the folders and files before I can even start to work on the fun stuff. Recently I reached a point where I wanted to streamline this a bit, so I came up with this idea. It involves using a plugin to add a menu item similar to extensions->modules->create module but instead would be called extensions->modules->create module and files. Here is what we must do in order to implement this change.

1 Create some language phrases.
2 Create a Plugin
3 Duplicate two existing files
4 Modify those files to perform our desired action.

First, the language phrases. Create two language phrases using
Product: core
Module: admincp
Varname: create_new_modulefs
Text: Create module and folders
and
Product: core
Module: admincp
Varname: could_not_write_to_modules_folder
Text: Could not write to modules folder. Please temporarily change the permission on this folder

Then create a new file in the /module/admincp/include/plugin/ folder named "admincp.component_controller_index_process_menu.php" and place this code within the new file:
PHP:
$aMenus['admincp.extensions']['admincp.module']['admincp.create_new_modulefs']= 'admincp.module.addfs'


This will add a new menu item admincp->extensions->module->Create module and folders

Now we need to duplicate 2 files.
Go to /module/admincp/include/component/controller/module/add.class.php and duplicate it to the same folder so the new file is addfs.class.php.

Go to /module/admincp/template/default/controller/module/add.html.php and duplicate it to the same folder so the new file is addfs.html.php.

Now to alter the files. First open addfs.html.php and find around line 32:
HTML:
<form method="post" action="{url link="admincp.module.add"}" id="js_form" onsubmit="{$sGetJsForm}">

Change to:
HTML:
<form method="post" action="{url link="admincp.module.addfs"}" id="js_form" onsubmit="{$sGetJsForm}">


Now open file addfs.class.php and make these changes:
line 16 change:
PHP:
class Admincp_Component_Controller_Module_Add extends Phpfox_Component 

to:
PHP:
class Admincp_Component_Controller_Module_Addfs extends Phpfox_Component 

line 93 change:
PHP:
$this->url()->send('admincp', array('module''add'), Phpfox::getPhrase('admincp.module_successfully_created_redirect')); 

to:
PHP:
$this->url()->send('admincp', array('module''addfs'), Phpfox::getPhrase('admincp.module_successfully_created_redirect')); 

Line 90 or so find:
PHP:
if (($sName Phpfox::getService('admincp.module.process')->add($aVals))) 

Immediately BEFORE this line insert this code:
PHP:
//start
$sModuleName = strtolower($aVals['module_id']); //file system module must be lowercase
// Set up vars for folders that we will insert files into later
$sControllerPath = PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS . 'component' . PHPFOX_DS . 'controller' . PHPFOX_DS;
$sTemplatePath = PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'template' . PHPFOX_DS . 'default'   . PHPFOX_DS . 'controller'  . PHPFOX_DS;
// Build an array of folders to be created
$aDirects = array(
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS . 'component' . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS . 'component' . PHPFOX_DS . 'ajax'       . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS . 'component' . PHPFOX_DS . 'block'      . PHPFOX_DS,
$sControllerPath,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS . 'plugin'    . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'include'  . PHPFOX_DS . 'service'   . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'static'   . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'static'   . PHPFOX_DS . 'css'       . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'static'   . PHPFOX_DS . 'css'       . PHPFOX_DS . 'default'    . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'static'   . PHPFOX_DS . 'css'       . PHPFOX_DS . 'default'    . PHPFOX_DS . 'default' . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'static'   . PHPFOX_DS . 'image'     . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'static'   . PHPFOX_DS . 'jscript'   . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'template' . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'template' . PHPFOX_DS . 'default'   . PHPFOX_DS,
PHPFOX_DIR_MODULE . $sModuleName . PHPFOX_DS . 'template' . PHPFOX_DS . 'default'   . PHPFOX_DS . 'block'       . PHPFOX_DS,
$sTemplatePath
);

// Create the file contents for the new module's index.class.php
$sController = "
 <?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright
 * @author
 * @package
 * @version
 */

class " . ucwords(strtolower($aVals['module_id'])) . "_Component_Controller_Index extends Phpfox_Component
{
    public function 
process()
    { 
     
" . chr(36) . "this->template()->assign(array(
                
'sHello' => 'Hello World',
          
'sModule' => '" . $aVals['module_id'] ."'
                
)
             )        
            ->
setTitle('Replace with title')
            ->
setBreadcrumb('" . $aVals['module_id'] ."')
            ->
setMeta('keywords''phrase1, phrase2, phrase3')
            ->
setMeta('description''Replace with description')
            ->
assign('sSampleVariable''Hello, I am an assigned variable.')
            ->
setHeader(array(
                    
'sample.css' => 'module_phpfoxsample',
                    
'sample.js' => 'module_phpfoxsample'
                
)
            )
            ->
setHeader('<!-- Add me in the header -->'); 
    }
}

?> 
";

// Create the file contents for the new module's index.html.php
$sTemplate = "
<?php 
/**
 * [PHPFOX_HEADER]
 * 
 * @copyright
 * @author
 * @package
 * @version
 */

defined('PHPFOX') or exit('NO DICE!'); 

?>

" . chr(123) . chr(36) . "sHello" . chr(125) . " from "  . chr(123) . chr(36) . "sModule" . chr(125);

//Check if the modules folder is writable
if(Phpfox::getLib('file')->isWritable(PHPFOX_DIR_MODULE))
{
    // If writable, start writing the folders
    foreach($aDirects as $aDirect)
    {
        Phpfox::getLib('file')->mkdir($aDirect);
    }

    // Write the controller and template files
    Phpfox::getLib('file')->write($sControllerPath . 'index.class.php', $sController);
    Phpfox::getLIb('file')->write($sTemplatePath . 'index.html.php',$sTemplate);

}
else
{
    // If the modules folder is not writeable, tell the user
    $this->url()->send('admincp', array('module', 'addfs'), Phpfox::getPhrase('admincp.could_not_write_to_modules_folder'));

}

//end


With all this in place, clear your cache, refresh your browser, and try using the new menu item to create a module. It will probably tell you that it can't write to the module folder. You must change the permissions on /module to 777 to use this. It would likely be a good idea to change it back to 755 afterward if your site is publicly accessible.

After changing the permission, try creating a new module again and it should create the module and build the folders. It will also create the index files in the controller and template folders so that you can immediately start playing with them.

I developed this on 3.4 and tested it on 3.6, but it should work on most versions.