I've put together a dashboard widget that creates/updates schemas and maps. I based the core code off the code found in FormIt2db with some slight modifications and added a dashboard widget that uses FormIt to pass the data to the hook.
Make sure you have FormIt installed.
First create a snippet called "mapSchemaTool"...
/* -------------------------------------------
* Extension: SMUT (Schema & Map Update Tool)
* -------------------------------------------
* Version: 1.0.0
* Since: Dec. 26, 2014
* Author: Aaron Kent <a.kent@one****erosa.com>
* License: GNU GPLv2 (or later)
*
* SMUT (Schema & Map Update Tool)
* This Dashboard Widget will Create or Update your Schemas & Maps for any Single Package
* based on a packagename and table prefix combination you provide.
*
* It's good practice to use different prefixes for your different packages.
*
* SMUT allows you to create or update your requisite package files in as little as 2 seconds.
*
* SMUT utilizes FormIt and therefor, FormIt is prerequisite for SMUT to function.
* In a future version, FormIt will not be required.
*
* SMUT is ultra light.
*
* To install, first place this snippet into a category, both labeled "mapSchemaTool".
* Then, place the widget code into a new widget in your dashboard.
* To use; simple supply SMUT with your table prefix and packagename, then click "SMUT It!"
* or press the enter key.
*
* SMUT will create package schema and map files even if your tables don't exist yet!
*
* Aaron Kent
* a.kent@one****erosa.com
*
* mapSchemaTool
*
* @package mapSchemaTool
* @subpackage mapSchemaTool snippet
*/
$parameters = $hook->getValues();
$prefix = $parameters['smutPrefix'];
$packagename = $parameters['smutPackage'];
$packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/');
$modelpath = $packagepath . 'model/';
$schemapath = $modelpath . 'schema/';
$schemafile = $schemapath . $packagename . '.mysql.schema.xml';
$manager = $modx->getManager();
$generator = $manager->getGenerator();
if ( !file_exists( $schemafile )) {
$op = 'created';
if ( !is_dir( $packagepath )) {
mkdir( $packagepath, 0777 );
}
if ( !is_dir( $modelpath )) {
mkdir( $modelpath, 0777 );
}
if ( !is_dir( $schemapath )) {
mkdir( $schemapath, 0777 );
}
// Writes the schema from an existing database
if (!$generator->writeSchema($schemafile, $packagename, '', $prefix, true)) {
$response = 'Error: Could not generate XML schema!';
$modx->log(modX::LOG_LEVEL_ERROR, $response, '', 'mapSchemaTool');
$modx->setPlaceholders( array(
'response' => $response,
'prefix' => $prefix,
'package' => $packagename,
), 'smut.' );
return false;
}
} else {
$op = 'updated';
}
$generator->parseSchema( $schemafile, $modelpath );
$modx->log( modX::LOG_LEVEL_WARN, 'mapSchemaTool '.$op.' schemas and maps for all tables with prefix: '.
$prefix.' packagename: '.$packagename, '', 'mapSchemaTool' );
$response = 'SMUT '.$op.' schemas and maps for all tables with prefix: '.$prefix.' packagename: '.$packagename;
$modx->setPlaceholders( array(
'response' => $response,
'prefix' => $prefix,
'package' => $packagename,
), 'smut.' );
return true;
Next, create an HTML dashboard widget named "SMUT"...
[[FormIt?
&hooks=`mapSchemaTool`
]]
<p style="padding-bottom:10px;">[[+smut.response]]</p>
<form name="smutForm" action="" method="post" class="form">
<label>Prefix <input type="text" name="smutPrefix" id="smutPrefix" value="[[+smut.prefix]]"></label>
<label>Package <input type="text" name="smutPackage" id="smutPackage" value="[[+smut.package]]"</label>
<input type="submit" name="makeItSo" value="SMUT It!">
</form>
Go back to your dashboard and there you will see SMUT, try it!
I would really love to package this for distribution OR see it as part of core once I update this to work independently of FormIt.
I got PackMan but it doesn't seem to work for widgets? If it does I need explanation on how.
I have looked at a package to see how it's built and I have a general Idea of how I might manually build one but I'm still a lot green with MODX so help would be appreciated.
Well, enjoy!