We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34416
    • 10 Posts
    Hello!

    formit2resource is very good thing. Great thanks to Bruno for doing it. I add a little bit of code to this snippet, for it makes new resources published and hided from menu, and makes a random alias (from current time):
    <?php
    $doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
    
    if (empty($doc)){
        $doc = $modx->newObject('modResource');
        $doc->set('createdby', $modx->user->get('id'));
    }
    else{
        $doc->set('editedby', $modx->user->get('id'));
    }
    
    $allFormFields = $hook->getValues(); 
    foreach ($allFormFields as $field=>$value)
    {
    
       if ($field !== 'spam' && $field !== 'resource_id'){
             $doc->set($field, $value);
        }
    }
    
    
    $doc->set('template', '8');
    
    /*--------my adds to code-----------*/
    $doc->set('hidemenu', '1'); //hide from menu
    $doc->set('published', '1'); //publicate at once
    $randAlias = time();
    //$randAlias = mt_rand(1000000, 9999999);
    $doc->set('alias', 'auto' . $randAlias);
    /*--------end of my adds----------*/
    $doc->save();
    
    foreach ($allFormFields as $field=>$value)
    {
        if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
        {
            /* handles checkboxes & multiple selects elements */
            if (is_array($value)) {
                $featureInsert = array();
                while (list($featureValue, $featureItem) = each($value)) {
                    $featureInsert[count($featureInsert)] = $featureItem;
                }
                $value = implode('||',$featureInsert);
            }	
            $tv->setValue($doc->get('id'), $value);
            $tv->save();
        }
    }
    
    return true;

    But I still get a little problem. Until I by my own hands save a new resource from Manager Panel it’s not available to see it by its alias. For example, resource alias is "auto1309864545", and url http://mysitename.ru/auto1309864545 followes me to 404-page.
    P.S. If my thought isn’t clear understandable please ask me questions because of I understand in English but still can’t speak correctly smiley
      • 34416
      • 10 Posts
      Hello, Bruno!
      After a few days of searching how to make my pages able to view after resource creating with formit2resource, but all in vain, I beg for your help. I understand that to make a resource viewable by its alias, the site must be renewed (Manager->Site->Clear cache). But I can’t understand how to make it within snippet "formit2resource".
      Would you be so kind to point me the way?
        • 35018
        • 1 Posts
        Quote from: barsuk at Jul 09, 2011, 07:00 AM

        Hello, Bruno!
        After a few days of searching how to make my pages able to view after resource creating with formit2resource, but all in vain, I beg for your help. I understand that to make a resource viewable by its alias, the site must be renewed (Manager->Site->Clear cache). But I can’t understand how to make it within snippet "formit2resource".
        Would you be so kind to point me the way?

        Try to add this line on the end of snippet, before " return true; " :
        $modx->cacheManager->clearCache();
          • 36704
          • 131 Posts
          Hi to all,

          First of all i would to thank Bruno for sharing this snippets and i am looking for this for a long time in modx and it's very usefull for all of us.
          I Tried this snippet and it works fine. All of the data input on that form was automatically write into a new resource documents,

          My problem is:
          Why can i get the data input from pagetitle field?
          I get only the content field data.

          I just copy & paste the code that bruno's posted.

          And also, is there anyway to customized the templating?

          Any help would be highly appreciated..
            • 21414
            • 41 Posts
            I'm curious to know if there is there a way to get the new resource id returned to the page after it has been created.

            For example: Have it create the new resource and then return the id of the new resource back to the page into a placeholder so that you could for example redirect to the newly created resource upon creation.
              • 3749
              • 24,544 Posts
              Quote from: nick2687 at Jan 25, 2012, 05:02 PM
              I'm curious to know if there is there a way to get the new resource id returned to the page after it has been created.

              For example: Have it create the new resource and then return the id of the new resource back to the page into a placeholder so that you could for example redirect to the newly created resource upon creation.

              NewsPublisher automatically forwards you to the new page once it's submitted.



              ---------------------------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
              MODx info for everyone: http://bobsguides.com/MODx.html
                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
                • 31654
                • 238 Posts
                I can't get this code to work. All I get is a blank (white) page on submit!
                I've moved the: return true; up and down the code. The only way the form refreshes and sends and email is if I put the return true; just before the $doc->save();. If I put it just after I get the blank page.
                I made sure the parent ID is correct and is a folder. I've set it to the correct template.

                Here's the call I'm using:
                [[!FormIt? &hooks=`formit2resource,email` &resource2formitfields=`parent,pagetitle,content` &emailTpl=`myEmailChunk` &emailTo=`[email protected]`]]
                


                PHP:
                <?php
                 
                $doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
                 
                if (empty($doc)){
                    $doc = $modx->newObject('modResource');
                    $doc->set('createdby', $modx->user->get('id'));
                }
                else{
                    $doc->set('editedby', $modx->user->get('id'));
                }
                 
                $allFormFields = $hook->getValues(); 
                foreach ($allFormFields as $field=>$value)
                {
                 
                   if ($field !== 'spam' && $field !== 'resource_id'){
                         $doc->set($field, $value);
                    }
                }
                 
                 
                $doc->set('template', '21');
                $doc->save();
                 
                foreach ($allFormFields as $field=>$value)
                {
                    if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
                    {
                        /* handles checkboxes & multiple selects elements */
                        if (is_array($value)) {
                            $featureInsert = array();
                            while (list($featureValue, $featureItem) = each($value)) {
                                $featureInsert[count($featureInsert)] = $featureItem;
                            }
                            $value = implode('||',$featureInsert);
                        }   
                        $tv->setValue($doc->get('id'), $value);
                        $tv->save();
                    }
                }
                 
                return true;


                HTML:
                <h2>Resource Form</h2>
                <p>[[+fi.error.error_message]]</p>
                <form class="form" action="[[~[[*id]]]]" method="post">
                    <input name="nospam:blank" type="hidden" />
                    <input name="resource_id" type="hidden" value="[[+fi.id]]" />
                    <label for="parent">
                        Parent:
                        <span class="error">[[+fi.error.parent]]</span>
                    </label>
                    <input id="parent" name="parent:required" type="text" value="[[+fi.parent]]" />   
                     
                    <label for="pagetitle">
                        Pagetitle:
                        <span class="error">[[+fi.error.pagetitle]]</span>
                    </label>
                    <input id="pagetitle" name="pagetitle:required" type="text" value="[[+fi.pagetitle]]" />
                     
                    <label for="content">
                        Content:
                        <span class="error">[[+fi.error.content]]</span>
                    </label>
                    <textarea id="content" cols="55" rows="7" name="content:required:stripTags">[[+fi.content]]</textarea>
                <br class="clear" />
                <div class="form-buttons">
                        <input type="submit" value="Create Resource" />
                    </div>
                </form>


                My setup is:
                MODX Revolution 2.1.5
                Formit: 2.0.3

                OS: Linux 2.6.18-194.el5
                PHP Version: 5.3.6
                MySQL: 5.1.56
                pdo_mysql: 5.1.56
                Server API: CGI/FastCGI
                  Web Development, Web Hosting & Search Engine Marketing by:

                  Vitalized | UK
                  w. www.vitalized.co.uk

                  Website Design | Search Engine Marketing (SEM) | UK MODx web hosting, secure, fast & 100% MODx compatible

                  Vitalized | Australia
                  w. www.vitalized-australia.com.au

                  Website Design | Search Engine Marketing (SEM) | Australian MODx web hosting, secure, fast & 100% MODx compatible
                  • 31654
                  • 238 Posts
                  Okay, I solved it!... I'm using a multi-context (multi domain) setup.
                  If anyone else has issues add this just before the: $doc->save();

                  $doc->set('context_key', "YouContextNameHere");
                  $doc->save();
                  
                    Web Development, Web Hosting & Search Engine Marketing by:

                    Vitalized | UK
                    w. www.vitalized.co.uk

                    Website Design | Search Engine Marketing (SEM) | UK MODx web hosting, secure, fast & 100% MODx compatible

                    Vitalized | Australia
                    w. www.vitalized-australia.com.au

                    Website Design | Search Engine Marketing (SEM) | Australian MODx web hosting, secure, fast & 100% MODx compatible
                    • 23050
                    • 1,842 Posts
                    Hello smiley

                    Is there any security issue to let anonymous create some ressources ? What if they write some malicious code in content input for example ?

                    Same question about uploading file. I didn't try it yet but there is a snippet to do that : http://forums.modx.com/thread/47603/solved-file-upload-with-formit (I think that if we check extension file to be .jpg or .png for example, that's enough to protect our server ?)

                    Thank you smiley
                      • 23050
                      • 1,842 Posts
                      Hi,

                      Finally, is the validator FormIt stripTags, enough to protect against malicious code ? Should I activate this validator for every input of the form ? Thanks smiley