Feature Request: Expert Expert Code Generator

8 posts by 3 authors in: Forums > CMS Builder
Last Post: September 30, 2019   (RSS)

I would love to see a code generator that generated only tags and those tags are click-to-copy.

Output of a detail page would just be something like this:

list($page_elementsRecords, $page_elementsMetaData) = getRecords(array(
'tableName' => 'page_elements',
'where' => '', // load first record
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$page_elementsRecord = @$page_elementsRecords[0]; // get first record

<?php echo htmlencode($page_elementsRecord['num']) ?>
<?php echo $page_elementsRecord['areas_served']; ?>
<?php echo $page_elementsRecord['practice_areas']; ?>
<?php echo $page_elementsRecord['legal_notice']; ?>
<?php echo $page_elementsRecord['feedback']; ?>

<?php foreach ($page_elementsRecord['page_headers'] as $index => $upload): ?>
<?php echo htmlencode($upload['urlPath']) ?>
<?php echo $upload['width'] ?>
<?php echo $upload['height'] ?>
<?php echo htmlencode($upload['info1']) ?>
<?php echo htmlencode($upload['info2']) ?>
<?php echo $upload['extension'] ?>
<?php endforeach ?>

<?php echo $page_elementsMetaData['_listPage'] ?>
<?php echo urlencode(thisPageUrl()) ?>

Using a click-to-copy javascript for each tag so it can be copied and pasted quicker. Right now, I highlight the snippet I need and Ctrl+C and then Ctrl+V. A lot of the time, I just wind up typing out the whole snippet if I think it would be quicker. All of the other text (even in expert mode) is way more than I need and just in the way.

So, is Expert-Expert Mode a possibility?

Thanks,

Kenny H

An "expert expert" mode sounds nice. I'm not really concerned about a "js click-to-copy" though.

By Dave - September 26, 2019 - edited: September 26, 2019

Hi Guys, 

We actually wrote the code generators as CMSB plugins so you can easily modify and extend them.

Here's how you do that: 

  • Copy a code generator, such as "List Page" from /lib/menus/_codeGenerator/listPage.php to /plugins/customCodeGen_listPage.php
  • Rename the functions by replacing "cg2_listpage" with "codeGenExpert_listpage" (or something else) 
  • Activate the plugin and confirm it shows up on the Code Generator menu under "Other Generators"

You can then optimize it however you like.  Do you want to give that a try? 

If you have any questions about how the code works let me know, and any If you come up with something cool feel free to share it in the forums.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

Nice! Learned another new thing about CMSB today :) Thanks Dave!

Hi Dave - 

I am excited about being able to do this. I have followed your steps so far but when I go to activate it I get Call to undefined function cg_typeByVersion() in /home/lawweb/public_html/webadmin/plugins/expert_listPage.php on line 14.

I have tried to change anything else yet - I just wanted to get one up and running first

<?php
/*
Plugin Name: Expert List Page Generator
Description: Adds "Expert List Page" to Code Generator
Version: 1.00
Requires at least: 2.15
*/

// Note: This library is automatically included by /lib/menus/_codeGenerator/actionHandler.php
// ... but can be duplicated and added to the /plugins/ folder to create a new code generator.
// ... Just be sure to change the function names or you'll get errors about duplicate functions.

// register generator
addGenerator('expert_ListPage', 'List Page', 'Show many records.', cg_typeByVersion(2));

// dispatch function
function expertListPage($function, $name, $description, $type) {

  // show options menu, and errors on submit
  expertListPage_getOptions($function, $name, $description, $type);

  // show code
  $instructions   = []; // show as bullet points
  $filenameSuffix = 'list'; // eg: tablename-FILENAMESUFFIX.php
  $code           = expertListPage_getCode();
  cg2_showCode($function, $name, $instructions, $filenameSuffix, $code);
  exit;
}


// user specified options
function expertListPage_getOptions($function, $name, $description, $type) {

  // error checking
  if (@$_REQUEST['_showCode']) {
    $errorsAndAlerts = '';
    if (!@$_REQUEST['tableName']) { alert("Please select a section!<br>\n"); }
    if (!alert()) { return; } // if form submitted and no errors than return and generate code
  }

  // set form defaults
  $defaults['howMany']          = 'all';
  $defaults['orderBy']          = 'default';
  $defaults['showUploads']      = 'all';
  $defaults['showUploadsCount'] = '1';
  $defaults['allowSearching']   = 0;
  $defaults['limit']            = 5;
  $defaults['perPage']          = 10;
  foreach ($defaults as $key => $value) {
    if (!array_key_exists($key, $_REQUEST)) { $_REQUEST[$key] = $value; }
  }

  // prepare adminUI() placeholders
  $adminUI = [];
  
  // form tag and hidden fields
  $adminUI['HIDDEN_FIELDS'] = [ [ 'name' => '_showCode', 'value' => '1' ] ];
  
  // main content
  $adminUI['CONTENT'] = ob_capture(function() { ?>

    <div class="form-horizontal">

      <?php echo adminUI_separator(t('Viewer Options')); ?>
      
      <?php cg2_option_selectSection(); ?>

      <div class="form-group">
        <div class="col-sm-2 control-label"><?php et('How Many')?></div>
        <div class="col-sm-10">
          <div class="radio">
            <label>
              <?php echo cg2_inputRadio('howMany', 'all'); ?>
              <?php et('Show all records')?>
            </label>
          </div>
          
          <div class="radio">
            <label>
              <?php echo cg2_inputRadio('howMany', 'firstN'); ?>
              <?php echo sprintf(t('Show the first %s records only'), cg2_inputText('limit', 3)); ?>
            </label>
          </div>
          
          <div class="radio">
            <label>
              <?php echo cg2_inputRadio('howMany', 'paged'); ?>
              <?php echo sprintf(t('Show %s records per page with prev &amp; next page links'), cg2_inputText('perPage', 3)); ?>
            </label>
          </div>
          
        </div>
      </div>

      <?php cg2_option_sorting() ?>
      <?php cg2_option_uploads() ?>

      <div class="form-group">
        <div class="col-sm-2 control-label"><?php et('Allow Searching')?></div>
        <div class="col-sm-10 control-label">
          <div class="text-left">
            <?php echo cg2_inputCheckbox('allowSearching'); ?>
            <label for="allowSearching">
              <?php et('Filter results based on search form input (disable for multiple viewers on one page)')?>
            </label>
          </div>
        </div>
      </div>

      <div class="center">
        <?php echo adminUI_button(['label' => t('Show Code') ]); ?>
      </div>
    
    </div>

  <?php });
  
  // compose and output the page
  cg_adminUI($adminUI, $name, $function);
  exit;
}

//
function expertListPage_getCode() {
  $tableName  = @$_REQUEST['tableName'];
  $schema     = loadSchema($tableName);
  $menuName   = @$schema['menuName'] ?: $tableName;

  // define variable names
  $tableRecordsVar = '$' .preg_replace("/[^\w]/", '_', $tableName). "Records";
  $metaDataVar     = '$' .preg_replace("/[^\w]/", '_', $tableName). "MetaData";
  $recordVar       = '$record';

  // define getRecords() options
  $options = [];
  $options[] = "'tableName'   => '$tableName',";
  if      (@$_REQUEST['howMany'] == 'firstN')    { $options[] = "'limit'       => '{$_REQUEST['limit']}',"; }
  else if (@$_REQUEST['howMany'] == 'paged')     { $options[] = "'perPage'     => '{$_REQUEST['perPage']}',"; }
  else                                           { /* default to showing all */ }
  if      (@$_REQUEST['orderBy'] == 'random')    { $options[] = "'orderBy'     => 'RAND()',"; }
  if      (@$_REQUEST['showUploads'] == 'all')   { $options[] = "'loadUploads' => true,"; }
  elseif  (@$_REQUEST['showUploads'] == 'limit') { $options[] = "'loadUploads' => true,"; }
  else                                           { $options[] = "'loadUploads' => false,"; }
  if      (@$_REQUEST['allowSearching'])         { $options[] = "'allowSearch' => true,"; }
  else                                           { $options[] = "'allowSearch' => false,"; }
  $padding           = "    ";
  $getRecordsOptions = "\n$padding" . implode("\n$padding", $options) . "\n  ";

  ### generate code
  ob_start();
?><#php header('Content-type: text/html; charset=utf-8'); #>
<#php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  <?php cg2_code_loadLibraries(); ?>

  // load records from '<?php echo $tableName ?>'
  list(<?php echo $tableRecordsVar ?>, <?php echo $metaDataVar ?>) = getRecords(array(<?php echo $getRecordsOptions; ?>));

#><?php cg2_code_header(); ?>
<?php cg2_code_instructions('List'); ?>

  <!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
    <h1><?php echo $menuName ?> - <?php echo t('List Page Viewer'); ?></h1>
    <#php foreach (<?php echo $tableRecordsVar ?> as <?php echo $recordVar ?>): #>
<?php cg2_code_schemaFields($schema, $recordVar, $tableName); ?>
<?php if (@$_REQUEST['showUploads']) { cg2_code_uploads($schema, $recordVar); } ?>
      <hr>
    <#php endforeach #>

    <#php if (!<?php echo $tableRecordsVar ?>): #>
      <?php echo t('No records were found!'); ?><br><br>
    <#php endif #>
  <!-- /STEP2: Display Records -->

<?php if (@$_REQUEST['howMany'] == 'paged'): ?>
  <!-- STEP3: Display Page Links (Paste anywhere below "Load Record List") -->
    <#php if (<?php echo $metaDataVar ?>['prevPage']): #>
      <a href="<#php echo <?php echo $metaDataVar ?>['prevPageLink'] #>"><?php echo t('&lt;&lt;  prev'); ?></a>
    <#php else: #>
      &lt;&lt; prev
    <#php endif #>

    - page <#php echo <?php echo $metaDataVar ?>['page'] #> of <#php echo <?php echo $metaDataVar ?>['totalPages'] #> -

    <#php if (<?php echo $metaDataVar ?>['nextPage']): #>
      <a href="<#php echo <?php echo $metaDataVar ?>['nextPageLink'] #>"><?php echo t('next'); ?> &gt;&gt;</a>
    <#php else: #>
      next &gt;&gt;
    <#php endif #>
  <!-- /STEP3: Display Page Links -->
<?php endif ?>
<?php cg2_code_footer(); ?>

<?php
  // return code
  $code = ob_get_clean();
  return $code;
}

?>

Getting closer. It let me activate the plugin after adding the code you specified. I went to Code Generator and got the the following error

Fatal error: Cannot redeclare cg_typeByVersion() (previously declared in /home/lawweb/public_html/webadmin/lib/menus/_codeGenerator/generator_functions.php:7) in /home/lawweb/public_html/webadmin/lib/menus/_codeGenerator/generator_functions.php on line 16

By Dave - September 30, 2019

Hi Kenny, 

Thanks for the feedback.  Try this fix: 

  • Edit /lib/menus/_codeGenerator\actionHandler.php
  • Replace: include "generator_functions.php";
  • With: require_once "generator_functions.php";

Let me know if that works for you and I'll include that patch in the next release as well.

Dave Edis - Senior Developer
interactivetools.com