CodeIgniter Forums
Matchbox Helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Matchbox Helper (/showthread.php?tid=9232)



Matchbox Helper - El Forum - 06-17-2008

[eluser]Phil Sturgeon[/eluser]
Have been working on doing some more interesting things with Matchbox than the usual sort of thing. Making things truely modular and dynamic (even routes) and while I was doing it I realized we should be able to do more checks to see if modules exist and controllers are about.

Havent worked on this for a massive amount of time so it may not be ideal, but its a kick in the right direction. Give it a try:

matchbox_helper.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
/*****
  * The Matchbox helper gives you lots of handy functions to use with your modules
  * @author        Philip Sturgeon
  * @email        [email protected]
  * @filename    matchbox_helper.php
  * @title        Matchbox Helper
  * @url        http://www.styledna.net
  * @version    1.0
  *****/

    function module_directories() {
        $CI =& get_instance();
        return $CI->matchbox->directory_array();
    }

    /***
    Returns an array of modules
      * @return        array
    ***/
    function module_array() {
        
        foreach (module_directories() as $directory) {
            
            foreach(glob(APPPATH.$directory.'/*', GLOB_ONLYDIR) as $module):
                $modules[] = basename($module);
            endforeach;
            
        }
        asort($modules);
        
        return $modules;
    }
    
    
    /***
    Returns true/false if the module exists
      * @param        $module string        The name of the module we are testing
      * @return        string
    ***/
    function is_module($module) {
        if(!$module) return FALSE;
        
        foreach (module_directories() as $directory) {
            if(is_dir(APPPATH.$directory.'/'.$module)) {
                return TRUE;
            }
        }
        
        return FALSE;
    }
    
    /***
    Returns true/false if the module has a controller with the name given
      * @param        $controller string        The name of the controller we are testing
      * @param        $module string        The name of the module we are testing
      * @return        string
    ***/
    function module_controller($controller, $module) {
        if(!$controller) return FALSE;
        
        foreach (module_directories() as $directory) {
            if(file_exists(APPPATH.$directory.'/'.$module.'/controllers/'.$controller.EXT)) {
                return TRUE;
            }
        }
        
        return FALSE;
    }
    
?>



Matchbox Helper - El Forum - 06-17-2008

[eluser]TheLoops[/eluser]
Please excuse my ignorance :red:
How (and when/for what) would I use this?


Matchbox Helper - El Forum - 06-18-2008

[eluser]Phil Sturgeon[/eluser]
A fair question. Depends how modular you want your system to be.

For example, I have navigation that cycles through all modules and picks out only modules that have a controller named 'admin'. This lets me build my admin navigation built PURELY from dropping a module in. I have something similar on the frontend for user navigation and sitemap.

I also use is_module() to check a module exists before trying to run content based on that module being there. On my CMS I have a products module which relies on suppliers. If suppliers is not there, it wont bother asking about them.

It allows everything to run alot more dynamically if you are managing several sites with the same cms. I can at the moment give a client a site with custom content by asking which modules they want, dropping them all on their site and changing the database settings alone.

Handy for me ^_^


Matchbox Helper - El Forum - 06-18-2008

[eluser]TheLoops[/eluser]
Ah, ok that makes sense, thanks.
Not yet sure if I'll go with it though Wink


Matchbox Helper - El Forum - 06-18-2008

[eluser]Phil Sturgeon[/eluser]
Hah thats up to you of course. I made plenty of sites without ever needing this, its probably only for a select few. I just dont like keeping handy things to myself, if 2 other CI users ever use this then the post was worth it.