How to structure PHP Modules? - php

I have a question for you. I'm not sure how to structure a PHP Module for CMS maybe when I create a new Page I want to select the Module which are created, for example
News Module, Gallery Module etc..
How can I structure this and implement those modules in PHP CMS ?

In your database you should hold a modules table that consists of the following:
id
module_name
module_desc
module_folder
module_active
so that you can keep modules organized, in the table where you have module_folder this should be the location of the module such as
cms_root() . "/modules/%module_folder%/main.module.php"
This is where interfaces would come in handy :)
interface IModule
{
public function __run($param);
public function __version();
public function __shutdown();
//...
}
you should also have a class called Module where the module would extend and gather rights to templates/database ect.
class Module
{
public $DB,$TPL; /*...*/
/*
Functions ehre to help the module gain access to the instance of the page.
*/
}
Also the Module class should be able to execute modules and keep track of executed modules, so in your core code you can say $Module->RunAll() and it would run them.
A module file would probably look like:
class Gallery_Module extends Module implements IModule
{
public function __version()
{
return '1.0';
}
public function __run()
{
//Assign module data to the template :)
}
public function __shutdown()
{
//Clean old records etc from DB
}
}
And within your core as said above, you can get the Module class to read active modules from the database load the files from the folder and create an instance of the class, followed by there execution.
Hope this helps.

Related

Get name of module in PrestaShop front controller

In PrestaShop (specifically v1.7.5) one can get an instance of the module class by calling
$module = Module::getInstanceByName('theModuleName');
in the controller of a custom module.
Is 'theModuleName' available via some other setting or variable or does it need to be hardcoded?
It should also be used as first parameter to getModuleLink().
You can access the module name (along with the rest from the module class) by:
$theModuleName = $this->module->name;
Using Prestashop core module "Cronjobs" as an example, you can also run module methods inside a front controller like this:
class CronjobsCallbackModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
$this->module->sendCallback();
die;
}
}
If you are working in a child from ProductListingFrontController, this->module is not defined.
If you call the module with getInstanceByName, you get an instance in order to work with it later. The string way doesn´t work in Listing controllers.
class mymoduleMyControllerModuleFrontController extends ProductListingFrontControllerCore
{
public function init()
{
parent::init();
$this -> module = Module::getInstanceByName('mymodule');
}
}

Where do I override the create_export_query for a custom module?

I am working with SugarCRM.
I have a custom module, I am trying to override the export method to not include all of the columns. I actually need to make the columns dependent on the visible columns in the list view (that I can figure out).
I have been going through all the files in SugarCRM, I notice that the built in modules have a Module.php file, where there is a 'SELECT Module.*' for the export method. I cannot find a file like that for my custom module. I am asking for help on where do I create (if needed) or where can I find the file to customize the create_export_query.
create_export_query is a method in SugarBean (/data/SugarBean.php is the base class for nearly all SugarCRM objects) and can be overriden in the bean's core class file. So if you have custom module MyModule you can find the core class in /modules/MyModule/MyModule.php
There is likely not a create_export_query() method there currently, so you can write one in. It'll look something like this:
<?php
require_once("include/SugarObjects/templates/basic/Basic.php");
class MyModule extends Basic{
public function MyModule(){
parent::Basic();
}
public function create_export_query(&$order_by, &$where, $relate_link_join=''){
$query = " select * from {$this->table_name} "; // build your query string however you like
return $query;
}
}

How to execute module bootstrap resources present inside another module's bootstrap in Zend Framework 1?

I am bootstrapping my application with Zend_Application_Module_Bootstrap inside the various module directories. How can I require that a resource inside another module's bootstrap be executed first?
// app/modules/user/Bootstrap.php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initUser()
{
}
}
// app/modules/author/Bootstrap.php
class Author_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAuthor()
{
$this->bootstrap('user'); // Fatal: Resource matching 'user' not found
}
}
I've decided to use plugins to achieve this fine-grained functionality, as execution order cannot be managed properly, therefore making module bootstraps a poor choice to place code with dependencies.
Used referenced answer below to base my decision:
load /execute module based bootstraps from each module in certain order
According to this thread from the ZF1 mailing list, you can access the module-bootstraps via the modules resource of the application bootstrap.
Man, what a mouthful. Here's what I mean:
// app/modules/user/Bootstrap.php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initUser()
{
}
}
// app/modules/author/Bootstrap.php
class Author_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAuthor()
{
$app = $this->getApplication(); // it's actually the application *bootstrap*
$app->bootstrap('modules');
$modulesResource = $app->getResource('modules');
$userBootstrap = $modulesResource->user;
$userBootstrap->bootstrap('user'); // should be cool
}
}
In my own experience, as soon as one of my module-level resources needs to be referenced in more than one module - especially during bootstrapping - I just push the bootstrapping of that resource up into the application-level bootstrap.

Codeigniter module configuration and ACL

I am developing a site in codeigniter divided into modules using HMVC. I want the modules to be enabled/disabled by an administrator, and following is how I am planning to achieve it. Since I don't have much experience in PHP/Codeigniter, the following way could be very wrong, so am looking for suggestions/feedbacks:
DB Table: Modules
ID | NAME | STATE
Above table will contain all the module information, and the state field will contain the disabled/enabled value(0 or 1).
I am going to extend the main CI_Controller, and have a function to check the status of the module:
class MY_Controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->Model('Module_model'); //loads the module model
}
function check_module_state($module_name = '')
{
return $this->Module_model->getState($module_name); //the model returns TRUE or FALSE
}
Now, in the constructor of every controller, I will extend the MY_Controller class, and call the check_module_state and if it returns FALSE, will redirect the user to a "Section Disabled Page";
class Module1 extends MY_Controller{
public function __construct()
{
parent::__construct();
if($this->check_module_state('module1') == FALSE)
redirect('module_disabled', 'location');
}
}
Will the above work, is there a better and easier alternative?
Thanks
It looks ok. How you describe the functionality sounds good. I would however say that if a module is disabled it isn't necessary to tell people that it is disabled.
How I would approach this problem is as below.
I would use the module table to construct the navigation items. If a module is enabled allow a link to be displayed to that module. If the module is disabled no link is displayed. This would not mean you can get rid of security in your controllers as people may still URL Surf. In the case of URL Surfing to a disabled module I would silently re-direct to a default module, for example, the site root (Index).

What is the cleanest way to retrieve a list of application resources across modules?

First, some context:
I am currently working on a modular Zend Framework application using Zend_Application. I wrote a custom module bootstrap that inserts custom resources into the Module Resource Autoloader, for example a 'Widget' resource.
Now, assuming the following structure:
/application
/application/modules/foo/widget/Bar.php
/application/modules/baz/widget/Qux.php
How would I be able to retrieve a list of every available widget in my application, preferably without traversing my entire directory structure?
Unfortunately I don't think there's a perfect solution to this. The best way I think is to have a standard way of 'registering' widgets in the respective module bootstraps, similar to how module-specific view helpers work.
Create a class for managing widgets which you instantiate in your main application bootstrap:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initWidgets()
{
return new Yourapp_Widgets();
}
}
and then in each module:
class Foo_Boostrap extends Zend_Application_Module_Bootstrap
{
protected function _initWidgets()
{
$widgetManager = $this->getApplication()->getResource('widgets');
$widgetManager->registerWidget('Foo_Bar');
}
}
you could then have a method on the widget manager class to return all registered widgets.

Categories