We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    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!
      • 44580
      • 189 Posts
      Hi Aaron, this looks pretty neat. That's what I like about MODX - the ways of doing things seems limited only by people's imagination. For a while now I have been using MIGX to achieve the same results. Whilst it is complicated, and could benefit from more documentation and tutorials, it is incredibly powerful. At its simplest, it does the "SMUT"ty stuff well (nice acronym BTW). If you have not, you should check it out.
      • Interesting. I've never considered the Dashboard as a place for interactive functionality. I've made a few informational widgets, as well as links to user-specific areas of the Manager when its menus are locked down for security, but I would have used a CMP for something like this.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 49407
          • 159 Posts
          Thanks Robert!

          Susan, the reason I chose the dashboard as it's residence is because it was quick to implement and makes the tool very easy to find. I dislike looking for things because I seem to forget rather easily. I'd lose my head if it weren't attached.
          • That I can understand perfectly, I've just got too many years of habit behind me to be able to easily think outside of the accustomed box. This concept means another side of that box getting knocked open.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org