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

    I’ve been writing (with the help of others here) a system to create and bind directories to a resource using a specific template (http://modxcms.com/forums/index.php/topic,53390.0.html).

    Now it is all working but the issue is that the resource must be saved first, therefore assigning an ID to it and for me to target to creating the directories and binding.

    So my question is this: Is it possible to automatically save a new document as soon as it’s created? I’m guessing a plugin fired at OnDocFormRender to perform the Save and continue editing action would probably do the trick. Is this possible? Am I going about this in the right way?

    Cheers.
      • 3749
      • 24,544 Posts
      If you put the code that creates the directory in a plugin tied to OnDocFormSave() the doc will have been saved alread and $id should be set to its ID (it is in Revo). If not $modx->lastInsertId() or $_GET[’id’] should give it to you.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28788
        • 79 Posts
        Thanks.

        Firstly and I should have said, I’m using Evo at this stage. Secondly, correct, the plugin runs on OnDocFormSave() so yes everything works once the document has been saved using $_GET[’id’].

        However that means when the document is first created my file upload TVs are unbound and my select/check boxes are calling a location that doesn’t exist. So what I’d like to do is have the new document automatically save (and continue editing) as early as possible (such as on OnDocFormPrerender).
          • 3749
          • 24,544 Posts
          Maybe I’m misunderstanding you, but I don’t see any way to do that. Every time you launched "new document" a new, empty document would be saved (it wouldn’t even have a pagetitle at that point, unless you create one in your plugin).
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28788
            • 79 Posts
            No I don’t think you are misunderstanding me, as that is exactly what I’d want to happen really, just like creating an empty file in a file system. I don’t see a problem with an empty document being created (you can do it now if you just save a new document before editing it).

            If it isn’t possible fair enough, I assumed the it would just be a case of posting/submitting the doc form via the plugin but I’m afraid I’m a bit of a PHP noob so I wasn’t sure what was possible.
              • 3749
              • 24,544 Posts
              It’s definitely doable, but my Evo Fu is rusty enough that I can’t give you the details. You might look at the NewsPublisher snippet (if you can find it) for clues.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 28788
                • 79 Posts
                Ok good starting point! Why do you suggest news publisher btw? I’ve never used it before.
                  • 3749
                  • 24,544 Posts
                  Because it’s creates new documents and saves them in Evolution. I thought some of the code might be useful.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 28788
                    • 79 Posts
                    Of course, hence the name smiley

                    Thanks for your help BobRay I’ll let you know what I find out.
                      • 4041
                      • 788 Posts
                      Give this plugin a try, it fires onDocFormPrerender... creates a new document, sets the tv values, then redirects back to the document for edit. Works fine in my limited testing.

                      //<?php
                      // table names
                      $content_table = $modx->getFullTableName('site_content');
                      $tv_table = $modx->getFullTableName('site_tmplvar_contentvalues');
                      
                      $template_id ='4';
                      
                      $e = &$modx->Event;
                      switch ($e->name) {
                          case "OnDocFormPrerender":
                      
                              // ensure this is a new document
                              if ($id == 0) {
                      
                                  // create the new document
                                  // minimum required content fields
                                  $fields = array(
                                      'pagetitle'        => time(),
                                      'type'             => 'document',
                                      'template'         => $template_id,
                                      'createdby'        => $_SESSION['mgrInternalKey'],
                                      'createdon'        => time(),
                                      'contentType'      => 'text/html'
                                      );
                                   /*
                                   // all available content fields
                                   $fields = array(
                                      'introtext'        => '',
                                      'content'          => '',
                                      'pagetitle'        => time(),
                                      'longtitle'        => '',
                                      'type'             => 'document',
                                      'description'      => '',
                                      'alias'            => '',
                                      'link_attributes'  => '',
                                      'isfolder'         => '',
                                      'richtext'         => '',
                                      'published'        => 0,
                                      'parent'           => 0,
                                      'template'         => $modx->config['default_template'],
                                      'menuindex'        => 0,
                                      'searchable'       => 0,
                                      'cacheable'        => 0,
                                      'createdby'        => $_SESSION['mgrInternalKey'],
                                      'createdon'        => time(),
                                      'editedby'         => '',
                                      'editedon'         => '',
                                      'publishedby'      => $_SESSION['mgrInternalKey'],
                                      'publishedon'      => '',
                                      'pub_date'         => '',
                                      'unpub_date'       => '',
                                      'contentType'      => 'text/html',
                                      'content_dispo'    => '',
                                      'donthit'          => '',
                                      'menutitle'        => '',
                                      'hidemenu'         => ''
                                      );
                                      */
                                      
                                  $modx->db->insert( $fields, $content_table);
                      
                                  // get the new doc id
                                  $new_docid = $modx->db->getInsertId();
                      
                                  // populate the TV(s)
                                  // copy/paste new sections for each TV
                                  $fields = array(
                                      'value'     => 'Value for the 1st TV, 4th attempt',
                                      'tmplvarid' => '1',
                                      'contentid' => $new_docid
                                      );
                                  $modx->db->insert( $fields, $tv_table);
                      
                                  $fields = array(
                                      'value'     => 'Another Value for the 2nd TV, 4th attempt',
                                      'tmplvarid' => '2',
                                      'contentid' => $new_docid
                                      );
                                  $modx->db->insert( $fields, $tv_table);
                                  
                                  
                                  // redirect back to the page to edit
                                  $header = "Location: index.php?a=27&id=".$new_docid;
                                  header($header);
                      
                              }
                              break;
                      
                          default: '';
                      return; // stop here - this is very important.
                      }
                        xforum
                        http://frsbuilders.net (under construction) forum for evolution