Question about settings and plugin, etc?

7 posts by 2 authors in: Forums > CMS Builder
Last Post: October 7   (RSS)

I have a modified Settings.dat.default.php file that presets many options for me such as a list of plugins, media.ini on, admin and dev email addresses etc. It was a pain to maintain when new versions came out  and the settings file was reorganized. I was hoping for a better way. Now with the default settings file now constructed programmatically, I see an opportunity to achieve my goal.

it would be great if we could provide a list of plugins and other settings in a custom config  file or something similar that could be integrated into the install. My goal is to remove the manual necessity to make these changes on each new project as very little is different from one client to the next.

For instance my email address for dev and admin don’t change, I always want certain plugins, I always have a custom help menu pointing at a help table, I always use media.ini (which has been customized with image sizes and info1-5 titles), my custom branding doesn’t change, and the list goes on. The only thing that changes for sure is DB info.

Jeff Shields

Hi Jeff, 

We have a number of users who create their own customized CMSB distributions in various ways.

For installs, perhaps the easiest way would be to have a folder with your customized files, e.g.: 

  • /cmsb/data/settings.dat.php
  • /cmsb/data/media.ini.php

Then when you download a new version zip drag those files in.  So if you have these folders: 

  • /my_source_files/cmsbuilder_3_73_build2773_release/
  • /my_source_files/cmsb_my_customized_files
  • /my_source_files/cmsb_my_release_version

Then everytime you download a new version you just drag/copy the release files in the "my_release_version" folder, and then drag the my_customized_files folder in, and you're good to go.

Because there's no /data/isInstalled.php semaphore file yet, it will run the install routine, and because the media and settings files are already there it should use them.

That might be the simplest, but you could also have a plugin check for files and customize them as needed.

Let me know if that will work for you.

Dave Edis - Senior Developer
interactivetools.com

that is what I have been doing, A couple of times recent changes to the settings file broke that approach and I had to repair the files. Anyway, I will continue with what I was doing.

Jeff Shields

I added this function to my plugin with "pluginAction_addHandlerAndLink('Activate My Plugins', 'activateMyPlugins', 'admins');"

/**
 * Activate plugins in the /_activate-plugins.php file when the plugin is
 * activated.
 *
 * This function is executed when the plugin is activated. It reads the
 * "plugins.txt" file in this folder and activates each plugin listed in
 * the file.
 *
 * @return void
 */
function activateMyPlugins()
{

    // Get a list of all active plugins
    // @var array $activePlugins
    $activePlugins = array_filter(
        // Split the string into an array
        array_map('trim', explode(',', str_replace("\n", '', settings('activePlugins')))),
        // Remove empty strings from the array
        function ($value) {
            return $value !== '';
        }
    );

    $pluginsFile = fopen(__DIR__ . '/plugins.txt', 'r');
    $pluginName = '';
    while (!feof($pluginsFile)) {
        $pluginName = fgets($pluginsFile);
        $pluginName = trim($pluginName);
        //check if plugin is active or not
        if (! in_array($pluginName, $activePlugins)) {
            if ($pluginName != '') {
                activatePlugin($pluginName);
            }
        }
    }
}

I have a simple text file that lists the plugins I always want to install. I can then run the command from the action column from my plugin to activate my standard plugins.

Jeff Shields

By kitsguru - October 7 - edited: October 7

I solved my final issue with settings. Instead of copying files into the data directory with predefined settings, I created two functions in my app plugin. This system plugin is used in all my projects and provides many functions, including adding records to specific tables, validating data entry, and more.

pluginAction_addHandlerAndLink('Settings Production', 'mySettingsProduction', 'admins');
pluginAction_addHandlerAndLink('Settings Development', 'mySettingsDevelopment', 'admins');

The two functions use Settngs::update to change the values for production and development servers. Some values are always set.

use Itools\Cmsb\Settings;

function mySettingsDevelopment()
{
    Settings::update([
        'websiteUrl' => '/',
        'programName' => 'My App',
        "advanced.login_expiry_limit" => 30,
        "advanced.login_expiry_unit" => "days",
        "advanced.showExpandedMenu" => "1",
        'advanced.useMediaLibrary' => '1',
        'advanced.useDatepicker' => '1',
        'advanced.codeGeneratorExpertMode' => '1',
        'advanced.phpHideErrors' => '0',
        'advanced.ignoreDeveloperWarnings' => '0',
        'footerHTML' => '<p>A custom app by me</p>',
        'helpUrl' => '?menu=help',
        'timezone' => 'America/Vancouver',
        'vendorLocation' => 'British Columbia, Canada',
        'vendorName' => 'me',
        'vendorUrl' => 'https://example.com',
        'mail.adminEmail' => 'me+admin@example.com',
        'mail.developerEmail' => 'me+dev@example.com',
        'mail.outgoingMail' => 'sendAndLog',
        'mail.smtp_method' => 'ssl',
        'mail.smtp_hostname' => 'mail.example.com',
        'mail.smtp_password' => '', // use password manager to set after running this
        'mail.smtp_port' => '465',
        'mail.smtp_username' => 'me@example.com',
        'mail.phpEmailErrors' => '0',
    ]);
}

function mySettingsProduction()
{
    Settings::update([
        'websiteUrl' => '/',
        'programName' => 'My App',
        "advanced.login_expiry_limit" => 30,
        "advanced.login_expiry_unit" => "hours",
        "advanced.showExpandedMenu" => "1",
        'advanced.useMediaLibrary' => '1',
        'advanced.useDatepicker' => '1',
        'advanced.codeGeneratorExpertMode' => '1',
        'advanced.phpHideErrors' => '1',
        'advanced.ignoreDeveloperWarnings' => '1',
        'footerHTML' => '<p>A custom app by me</p>',
        'helpUrl' => '?menu=help',
        'timezone' => 'America/Vancouver',
        'vendorLocation' => 'British Columbia, Canada',
        'vendorName' => 'me',
        'vendorUrl' => 'https://example.com',
        'mail.adminEmail' => 'me+admin@example.com',
        'mail.developerEmail' => 'me+dev@example.com',
        'mail.outgoingMail' => 'sendOnly',
        'mail.smtp_method' => 'php',
        'mail.smtp_hostname' => '',
        'mail.smtp_password' => '',
        'mail.smtp_port' => '',
        'mail.smtp_username' => '',
        'mail.phpEmailErrors' => '1',
    ]);
}
Jeff Shields

Hi Jeff, 

That's great!  Thanks for sharing your elegant solution.  I like how you can enable all your custom settings from the plugin menu.

It can be useful to see all the strategies everyone uses for managing their dev/prod environments.  

Here's how I have mine setup.  I work with a lot of websites so this setup is optimized for the use-case: 

  • I run WampServer on Windows for an instant WAMP stack that let's me toggle between PHP versions as needed for testing
  • My dev web server on my desktop responds to anything at *.localhost and automatically maps it to a folder 
  • So, for example, https://example.com.localhost/ automatically maps to my local drive C:/vhosts/example.com/htdocs/ use Apache <VirtualHost *> rules
  • If a folder doesn't exist a specialized 404 page gives me the option to create it with a click
  • Then I use hostname-specific settings files for dev environments such as /data/settings.example.com.localhost.dat so there's no possibility of overwriting a live settings file
  • I'll often change the title and color of the dev CMSB so it's clear when I'm on which

Mine only works locally though, not helpful if you need to show something to a remote user.

Thanks for sharing!

Dave Edis - Senior Developer
interactivetools.com