We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    Hey folks, I’m trying to create a plugin that will create a document when an existing user clicks a submit button. It’s not anything special, just to help me understand the logic of plugins and eventually turn this idea in to something for a project of mine. I wish the plugin to only work on the document ID 189.

    The code I have so far and I’m not sure if it’s correct...

    if ( $modx->documentIdentifier == 189 ) {
    $wuid = '-'.$_SESSION['webInternalKey'];
    	$table_name_docs = $modx->getFullTableName( 'site_content' );
         	$fields = array(
                             'type'	=>  'document',
    			'contentType'	=> 'text/html',
    			'content'	=> 'data',
    			'pagetitle' => 'data',
    			'alias'	=> 'data',
    			'longtitle'	=> 'data',
    			'description'	=> 'data',
    			'published'	=> '1', 
     			'parent'	=> '375', 
    			'isfolder'	=> '1', 
    			'template'	=> '0',
    			'cacheable'	=> '0',
    			'createdby'	=> $wuid,
    			'createdon'	=> time(),
    			'editedon'	=> time(),
    			'publishedon'	=> time(),
    			'hidemenu'	=> '0',
               		'privateweb'	=> '0'
    
    			);
    
    	$modx->db->insert( $fields, $table_name_docs);
    
        $modx->db->connect();
        include_once "manager/processors/cache_sync.class.processor.php";
        $sync = new synccache();
        $sync->setCachepath("assets/cache/");
        $sync->setReport(true);
        $sync->emptyCache();
    
       return;
    }
    


    System events clicked is OnDocFormSave (i’m not sure which one to use!)

    My form is very simple.

    <h3>ADD MY LOCATION</h3>
    <div style="text-align:left;">
    <form  action="/[~[*id*]~]" method="post" >
    <button type="submit" >CREATE MY PAGE</button>
    </form>
    </div>
    



    I’m learning PHP and modx as a lot of you know so I if this is all wrong, any pointers would be great.

      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 3749
      • 24,544 Posts
      onDocFormSave will only fire in the Manager. It looks like you want a front-end event. You might try OnWUsrFormSave.
        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
        • 25551 ☆ A M B ☆
        • 1,231 Posts
        Hi Bob

        The form doesn’t seem to work with the plugin but if I set the if statement to use the same ID as the profile update page and save my details, the plugin works and creates the new document.

        Without much knowledge of how this all works, what is it that the difference between saving the profile and clicking a submit button to create the page?
          Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
          AugmentBLU - MODX Partner

          BLUcart - MODX Revolution E-Commerce & Shopping Cart
          • 3749
          • 24,544 Posts
          Quote from: rossco at Apr 26, 2009, 02:44 PM

          Hi Bob

          The form doesn’t seem to work with the plugin but if I set the if statement to use the same ID as the profile update page and save my details, the plugin works and creates the new document.

          Without much knowledge of how this all works, what is it that the difference between saving the profile and clicking a submit button to create the page?

          I’m assuming that you’re "saving the profile" in the Manager? That would trigger an event in the "back end." Clicking on a submit button happens in the "front end" and won’t trigger that system event. What system event is your plugin listening for? The front-end events aren’t documented very well so I’m not sure which (if any) would work.

          It occurred to me that you could do it another way, without a plugin. You could set the action of your form to go to another page that would contain a snippet that creates the new document using the form data from the $_POST data. After creating the new doc, you could either display a "thank you" message, or forward to another page with $modx->sendRedirect().
            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
            • 25551 ☆ A M B ☆
            • 1,231 Posts
            I tried several of the events and the one that was checked when I saved the profile that allowed the page creation was OnWebSaveUser. This was done from the front end in a browser not signed in to the manager. It only works when the profile is saved... When I try another page, replacing the ID with the new form document ID it doesn’t work. Seem it must relate to the webloginPE snippet or events to trigger the document creation? I need to find out which events it’s using in that snippet to allow the creation.

            The other idea to use a snippet might be another way but I was wondering if I can use the plugin feature as I will be looking at other events down the line if I can work out how to create the document successfully.
              Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
              AugmentBLU - MODX Partner

              BLUcart - MODX Revolution E-Commerce & Shopping Cart
              • 25551 ☆ A M B ☆
              • 1,231 Posts
              Ok, I changed the event to OnWebPageComplete which creates the page... I created a link to that page so the user can navigate to that page which has a thankyou message on it.  Couple things that may be of concern are.

              How to stop multiple page creation if the user clicks back in the browser

              Is there a way to put an error report in if the page is not created? 

              How to only allow access to the link once so the user can no longer see the link after a successful page creation (see idea below). 

              I thought about the child count idea but how to specify the count by createdby ID?  How do I add the ability to have &createdby=`[[AuthorID]]`  to that snippet?  I already created a snippet to get the author ID of a particular document but how to add the &createdby param to the snippet below?  If I can get the count to show the count for a given createdby ID I can then use Phx to control if the link is visible to the user if the count is greater than 1.
              // initialize and get the number of children for a particular parent
              $childCount = 0;
              if ($children = $modx->getChildIds($parent, $depth)) {
                  $childCount = count($children);
              }
              // do something with $childCount or just return it
              return $childCount;




              Thanks!


              I just thought of another addition that may work?

              In the plugin I could have a statement to check a parent, then check the child docs. If the child doc count for createdby ID -20 <= 1 then carry on with the plugin, else warn the page is already created? Still learning lol.
                Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                AugmentBLU - MODX Partner

                BLUcart - MODX Revolution E-Commerce & Shopping Cart
                • 3749
                • 24,544 Posts
                I think your last idea is a good one. You could also use a snippet that did the same thing to print the link (or not).
                  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
                  • 25551 ☆ A M B ☆
                  • 1,231 Posts
                  I have nearly managed to get my plugin working but I still have one piece of code I need help with... I need to check the createdby ID for child docs of a particular parent. If any of the ID match the current user, to do something, if no child docs match do something else. So far I haven’t managed to figure how to do that. I managed to get a child count but that was it.
                    Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                    AugmentBLU - MODX Partner

                    BLUcart - MODX Revolution E-Commerce & Shopping Cart
                    • 16034
                    • 107 Posts
                    It’s not the fastest way to do it, but using getAllChildren and looping through the result, checking for createdby-field should do the trick.

                    Something like this (untested):
                    <?php
                    // Set $parentDocId to something sensible above
                    // Set $userId to something sensible above
                    $children = $modx->getAllChildren($parentDocId, 'menuindex', 'ASC', 'id, createdby');
                    foreach ($children as $child) {
                      if ($child['createdby'] == $userId) {
                        return "Found a match";
                      }
                    }
                    return "No match found";
                    
                      MODx snippet-glossary 101:
                      Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control