We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25284
    • 141 Posts
    Hi,

    I’m creating my first CMP and need some help.
    I’m trying to create a product import CMP for the visioncart module.
    What I need to do is to read in a bunch of directory names, ask which directory the user wants to import, then import the contents of that directory.

    I’ve got my CMP created and the first step is working, I have a drop down of the subdirectories displayed in my backend page.
    I’m using a smarty template that contains a form element.

    When I click submit, the browser refreshes back to the main modx manager homepage.
    I guess this is to be expected. I’ve tried a form action of "" (empty) and also hardcoded to ’/manager/index.php?a=72’ (which is the url when I go to my CMP)
    Obviously, both are posting back to ’/manager/index.php’ which is the homepage.


    So, my questions are:

    • Can you have multistage CMP’s posting back to itself?
    • If you can, how do I enter the correct URL
    • if not, how would I achieve my results?

    I’ve a feeling people are going to say ’Do it with ExtJs’, but I’ve not used that and any pointers would be greatly appreciated.

    For reference, here’s my controller code
    <?php
    
      $modx->setLogLevel(modx::LOG_LEVEL_DEBUG);
      $modx->log(modX::LOG_LEVEL_DEBUG,'In importProducts module');
      
      $baseUploadPath = $modx->getOption('proshotUploadBasePath');
      $modx->log(modX::LOG_LEVEL_DEBUG,'baseUploadPath = ['.$baseUploadPath.']');
      
      // Are we importing, or displaying a list of folders?
      if (isset($_REQUEST['folder'])) {
        // import folder contents
        $productsToImport = getDirectoryList($baseUploadPath . "/" . $_REQUEST['folder']);
        $modx->log(modX::LOG_LEVEL_DEBUG,'productsToImport = ['.print_r($productsToImport, true).']');
      } else {
        $productsToImport = getDirectoryList($baseUploadPath);
        $modx->log(modX::LOG_LEVEL_DEBUG,'productToImport = ['.print_r($productsToImport, true).']');
        $modx->smarty->assign( 'url', '/manager/index.php?a=72');
        $modx->smarty->assign( 'folders', $productsToImport );
        return $modx->smarty->fetch( 'selectImport.tpl' ); 
      }
      
    
    function getDirectoryList($directory) {
    
        // create an array to hold directory list
        $results = array();
        // create a handler for the directory
        $handler = opendir($directory);
        // open directory and walk through the filenames
        while ($file = readdir($handler)) {
          // if file isn't this directory or its parent, add it to the results
          if ($file != "." && $file != "..") {
            $results[] = $file;
          }
    
        }
        // tidy up: close the handler
        closedir($handler);
        // done!
        return $results;
    }
    
    return 'Error, should have returned by now.';  
    ?> 
    


    and my ’selectImport.tpl’ smarty template:
    <p>Please select a folder to import:</p>
    <form action="{$url}" method="GET">
        <select name="folder">
            {html_options values=$folders output=$folders}
        </select>
        <input type="submit" name="importProducts" value="Import" />
    </form>
    

    Thanks
    Robin

      • 25284
      • 141 Posts
      sorry to reply to my own question so quickly.
      Using a POST method makes it work... not sure I understand why.

        • 4172
        • 5,888 Posts
        did you try without action?

        <form method="POST">
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 25284
          • 141 Posts
          Hi Bruno,

          Thanks for your reply.
          To confirm, I’ve got it working by changing the method from GET to POST and using a blank action, so my form now look like
          <form action="" method="POST">
              <select name="folder">
                  {html_options values=$folders output=$folders}
              </select>
              <input type="submit" name="importProducts" value="Import" />
          </form>
          


          I’m tempted to mark this thread as solved, but it is bugging me why a GET doesn’t work.
          Any idea?

          Robin
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            GET may not work because you need to specify an action for your form. I’ve noticed that omitting the action behaves differently in different browsers... and I think it behaves differently when you use get vs. post for the method. Bottom line: don’t omit the action parameter.

            You can read the action from your custom manager page:

            $a = (int) $_GET['a'];
            $my_cmp_url = 'index.php?a='.$a;
            // ... then...
            $form = '<form method="post" action="'.$my_cmp_url.'" />';  // etc...


            If you have more URL parameters, you may have to filter them one by one.
              • 25284
              • 141 Posts
              Brilliant, I understand now.
              Thanks