showing module code once when multiple joomla modules in same page - php

In a joomla menu item page i placed same module instances multiple times(i duplicated in module manager).Now it's a severe requirement that some codes of my module have to be only one time in page.Because my module's each instance injects same code again and again.
how to avoid it so that only some codes will appear in page only once when i have multiple module instances of my module in that same page?
searching through web i found some unreliable ways -
1.Using session in module i track whether same module loading two or more times and restrict code injecting one time only
2.using a constant like this way -
if(!defined('MODULE_ALREADY_LOADED')){
//echo codes that you only want done once
//this will set the constant and now the other modules won't load/echo the code
define('MODULE_ALREADY_LOADED', true);
}
But not sure how much compatible the 2nd way is with joomla 2.5 as well as joomla 3 versions !.If there is a better error free way to do this then provide to me asap.

in your module code, you can use addscript method (it checks the duplicate scripts) instead of writing js directly. So, externalize your scripts, then in your code :
$document = JFactory::getDocument();
$document->addScript('your_script.js');

Related

Installing extensions in homemade CMS

I've built my own CMS for learning purposes (and also just for fun), but now I would like to expand it, so that it supports some basic form of extensions (plugins). I would like to work with some kind of observer-pattern where extensions register themselves by the ExtensionManager for certain updates (for example onRenderTitle()). Every extension would consist of one mandatory class ExtensionData that would be executed for every extension in the system. The extensions could register themselves in this class by using a callback (the ExtensionManager would be passed on to every ExtensionData class).
But now every extension has to register itself with every page load (because PHP loses state between every page load), causing potential performance problems.
Am I doing this completely wrong? Or is this the way to go? I have no experience in writing a CMS that supports extensions (that's what I'm trying to learn). I looked at how Wordpress works with extensions, but I haven't found how it loads them up.
Plugins in Wordpress are registered just by being placed in the wp-content/plugins folder, and they have to have the necessary docblocks in its main file. It is also a convention in WP that your plugin's main file has the same name as your plugin plus the .php extension, e.g. plugins/myplugin/myplugin.php.
As far as interaction with the core of your CMS goes, you can have your system to fire events and the plugins would then listen for them. WP uses actions and filters for it. Your code for a simple event listener could look following then:
action('action_name', function() {
// some code to be executed after event is fired
});
Your action() function could just update a global array of actions. When you want to fire the event, you just filter through the array of assigned actions.
foreach ($actions['action_name'] as $func) {
call_user_func($func);
}

How to use one module with distinct configurations?

So I am using the latestnews_modules in joomla, and I am rendering latest_news at the footer of the site, but also I need to render at the right other latest_news, but I see only one view default.php, how can I make that one module works different in each position???
if I create another module like the previous, they bot have the same behavior, how can this be solved, help!!
There should be configurable settings for the module:
Go to the module you wish to change
Look on the right; you should see a 'panel' headed 'Parameters'
You can set things such as the total count, the section/category to pull articles from, authors to include, and the order.
If your requirements are different from that offered by the module, you'll need to either create your own custom extension, or find a third-party extension that does what you want.
What are you trying to achieve?
a) If you want different "behaviour", i.e. different functionality you need two different modules and in that case would need to clone the latestnews module with your modifications. By the way, if you are modifying the code of core modules you really should do this using template overrides in case you end up losing your changes after a security patch - unlikely for this module, but possible.
b) If you want the same behaviour with different parameters do as Bobby Jack says and change the settings for one of the modules by going to the Module Manager, selecting the module and changing it's settings, e.g. how many items to display and from what category.
c) If you want different styling for the two modules use the Module Class Suffix to change the class attribute of one of the modules and write CSS to override the display of the relevant module instance.

How can plugin systems be designed so they don't waste so many resources?

I am trying to build a basic plugin system like the kind you often find in a CMS like WordPress. You have a folder of plugins which tie into the main system's operation through event notifications using an Observer or Event design pattern.
The problem is it's impossible for the system to know which events the plugin wants to act upon - so the system has to load each plugin for every page request just to find out if that plugin is actually needed at some point. Needless to say, that's a lot of wasted resources right there--in the case of WordPress, that adds up to several extra MB of memory for each request!
Are there alternative ways to do this?
For example, is there a way to load all this once and then cache the results so that your system knows how to lazy-load plugins? In other words, the system loads a configuration file that specifies all the events that plugin wishes to tie into and then saves it in APC or something for future requests?
If that also performs poorly, then perhaps there is a special file-structure that could be used to make educated guesses about when certain plugins are unneeded to fulfill the request.
I do have a plugin management tool, but I only every used it with predominantly procedural plugins, and with all includes usually loaded at once. But for an event-based and lazy-loading API I could imagine using shallow wrappers for the plugin management, and resorting to autoloading for the actual extensions.
<?php
/**
* api: whatever
* version: 0.1
* title: plugin example
* description: ...
* config: <var name="cfg[pretty]" type="boolean" ...>
* depends: otherplugin
*/
$plugins["title_event"] = "TitleEventClass";
$plugins["secondary"] = array("Class2", "callback");
?>
In this example I'd assume the plugin API is a plain list. This example feature-plugin-123.php script would do nothing but add to an array when loaded. So even if you have a dozen feature plugins, it would only incur an extra include_once each.
But the main application / or plugin API could instead just instantiate the mentioned classes (either new $eventcb; for the raw classnames or call_user_func_array for the callbacks). Where in turn it would offload the actual task to an autoloader. Thus you have a dual system, where one part manages the list, the other locates the real code.
I'm thereby still imaganing a simple config.php which just lists plugins and settings like this:
<?php
include_once("user/feature-plugin-123.php");
include_once("user/otherplugin2.php");
include_once("user/wrapper-for-htmlpurifier.php");
$cfg["pretty"] = 1;
Again taking in mind that these are just wrappers / data scripts, with the plugin description for managability. One could as well use an actual register_even() API and define an additional wrapper function in each. But listing classnames seems the simplest option.
The aforementioned management tool is kind of rusty and ugly: http://milki.include-once.org/genericplugins/
But it's uneeded if you just need a list (sql table) and no settings management. That overhead is only for prettyprinting the plugin meta data and keeping a human-readable config.php.
In conclusion:
spl_autoload() on the include_path, and a simple event->classname registry, one wrapper script each, simply included all at once.
Wordpress and other CMS systems are very bad examples.
What we have to understand is that modular, almost always means heavier.
The best scheme that I ever worked with to solve this situation is a class based plugin, with a strict naming convention using an auto loader.
So, before using the plugin, you´ll need to create an instance, or use static functions.
You can even call the plugin like:
<?php $thePlugin::method(); ?>
eg:
<?php
spl_autoload_register('systemAutoload');
function systemAutoload($class)
{
$parts = explode('_',$class);
switch($parts[1])
{
case "Plugin":
include("/plugins/{$parts[2]}/{$parts[2]}.php");
break;
}
// ...
}
?>
Regarding Events:
You have to register this events statically to avoid bringing it in a dynamic manner.
The database would be the right place to do it. You can have an events table, and install() and uninstall() methods on the plugin class to add specific events or bind methods to other events. It´s one database query, and if you want more from it, add it to memcached, or to a flat ini file.
Works well for me. This way I was able to get a heavy system that was consuming 8mb per request to drop to 1mb, with the exactly same list of features, without advanced caching. Now we can add more features and mantain the system "clean"
Hope that helps
I would save the plugin class name along with it's subscribed events in a configuration file and then save the parsed config file in APC, for example. Then when an event is triggered the system can lazy load the appropriate plugin classes as needed.

kohana v3: using different templates for different subdomains

I have a kohana v3 app. 2 subdomains pointing to this app. what I have to setup that kohana uses a different template if the app called with subdomain2.example.com?
at the moment all calls (from subdomain1 and subdomain2) use the standard template: 'templates/default'
thank you!
daniel
First, get the subdomain name from $_SERVER['SERVER_NAME']:
list($subdomain) = explode('.', $_SERVER['SERVER_NAME'], 2);
Then choose what template to use based on the subdomain:
// Replace this with a switch() statement if you want to choose another way
$this->template = 'templates/'.$subdomain;
The above code should be placed in the Controller::before() method before you call parent::before(). This assumes that you are using the Controller_Template or an extension of it.
may anybody can help me: kohana v3: using different templates for different subdomains
danzzz, there are a few ways... (i dont have time to go into detail.. so i'll give a quick go here..) .. A) use URL rewriting to map bla.site.com to site.com/bla (and www.bla.com+bla.com to bla.com/www) ... use that first param as the trigger... then load a different module at the top of the stack so it can override anything from a lower module - this assumes anything that is overridable is kept in a module, otherwise, you can use it as a trigger any
where in the code...
and B) is really the same thing, but using that param as the view name or similar...
whenever i have something like that, i tend to leave my application folder empty, and have an application module near the top of the module stack.. that way, i can load a "skin" module higher up and have the cascading FS do all the hard work...
keep in mind that "skin" modules etc will need a strict set of rules and interfaces, if you make a change to the app, you need to know all the skins still work...

Autoloading modularity/widgets in PHP, CodeIgniter/Kohana

I was looking on many template libraries, som I have fair mess in general idea what is out there ready to download/use and what I want to use, so maybe you could help me with this.
I'm currently learning CodeIgniter, thinking about moving to Kohana later. I would like to include controllers/modules(/module function maybe?) based on needs of template/site.
Example, so you would understand:
I have xml-defined page saved in mysql, which states, that in
<div id="sidebar">, i want to use news panel/widget - something like:
<div id="sidebar">{widget:news;3;60}</div>.
I'm looking for template parser and/or way to do it, so in main application I load page, then template. then I look up what modules/widgets page/template use and load them dynamically, pass them parameters (in example news;3;60 - module news, 3 last, 60 characters limit each), and echo their result in place of where i called them.
The usage for this should be understandable - if I use news module on 27 pages, just somewhere with last 3 news, somewhere last month, etc., i want to include it simply and edit it on one place.
Other problems in my mind are: I'm thinking that it would be best to have all modules at one time (not load them one there, one here), so I can access database on one place, etc.
I'm kind of lost and maybe someone will have some idea for me :)
The two best ways to do this are:
Use my CodeIgniter Dwoo implementation and build plugins
Use wiredesignz' Widget plugin
You could of course use Smarty plugins but yuck, who still uses Smarty?
Remember when creating Dwoo plugins that the CodeIgniter instance is available to any PHP loaded in on that request, so wether its Dwoo plugins, modifiers, blocks, etc you can always use:
$CI =& get_instance();
$CI->load->model('something');
//etc
If you're using Kohana3, you could use the HMVC-capabilities. A quick way would be to create a helper-class that you can use in your views. In your view you then make a call to this helper. This helper would start a new request that would trigger the correct controller/action.
There is some kind of widget-class in the Kohana-forums, but that requires a class for the widget instead of utilizing the (already existing?) controllers via the HMVC-capabilities of Kohana3.

Categories