We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45950
    • 40 Posts
    Can anyone offer assistance?
    I'm interested in using formit2db to write to my custom table. but i also want it to create a page (resource) on submit from the information that was written to the table and then write the resource id of the created page to the custom table. so a user through a link on their profile can edit the page
    I know it will involve creating a hook on submit so that it creates the page

    My scenario is this: a registered user uses a form on the front-end to create a profile written to the db and that creates a profile page in an unpublished state.
    I receive an email that this person has submitted a profile
    Through a CMP i can then review the submission and set publish to true so it is viewable

    thanks in advance for any help you can provide
      • 41355
      • 4 Posts
      Hello everybody,

      I am wondering about a strange bug I have.
      The snippets are working well but every time I create a resource, it saves it twice!

      I really have two resources but of course with different IDs!

      Does anybody know where the bug might be?
      I didn't change Brunos code at all.

      Thanks in advanced for any help
        • 4172
        • 5,888 Posts
        can you try it at a clean resource with nothing, than the form and the formit-call?
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 41355
          • 4 Posts
          Quote from: Bruno17 at Jul 15, 2014, 07:46 PM
          can you try it at a clean resource with nothing, than the form and the formit-call?

          Yes I did. Here is the code from the content area of a ressource:
          [[!FormIt? &hooks=`spam,formit2resource,email` &preHooks=`resource2formit`
          &resource2formitfields=`pagetitle,content`&emailTpl=`myEmailChunk` &emailTo=`[email protected]`]]
          
          <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="hidden" value="2" />  
          	<!--input id="parent" name="parent:required" type="text" value="[[+fi.parent]]" /-->  
              <br/>
              <label for="pagetitle">
                  Pagetitle:
                  <span class="error">[[+fi.error.pagetitle]]</span>
              </label>
              <input id="pagetitle" name="pagetitle:required" type="text" value="[[+fi.pagetitle]]" />
              <br/>
              <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>


          The Template is really basic:
          <html>
          <head>
          <title>[[++site_name]] - [[*pagetitle]]</title>
          <base href="[[++site_url]]" />
          </head>
          <body>
          [[*content]]
          </body>
          </html>


          resource2formit snippet:
          <?php
          if (isset($_GET['resId'])){
              if ($doc=$modx->getObject('modResource',array('id'=>$_GET['resId']))){
                  $docarray=$doc->toArray();
                  $fields = explode(',',$scriptProperties['resource2formitfields']);
                  $fields[] = 'id';
           
                  foreach ($fields as $field){
                  
                      if ($doc->getFieldName($field) === null) {
                          /* if field isnt defined, look for TV value */
                          $tvValue = $doc->getTVValue($field);
                          if ($tvValue !== null) {
                              $hook->setValue($field,$tvValue);
                          }
                      } else {
                          /* otherwise get field value */
                          $hook->setValue($field,$docarray[$field]);
                      }    
                  }
              }
           
              //$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';  
              //$hook->addError('error_message',$errorMsg);  
          }
           
           
          return true;


          formit2resource snippet:
          <?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', '3');
          $doc->set('alias', $doc->get('alias' . '-' . $doc->get('id')));
          $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;


          It works, but it creates two resources with different IDs and same content.

          Thanks in advance. [ed. note: comnerds last edited this post 9 years, 9 months ago.]
            • 49962
            • 1 Posts
            Hello,

            Does anyone know if formit2resource and resource2formit could work with MIGX TVs ?
            I was thinking of modifying the snippets to combine input fields into JSON string (and vice versa for resource2formit). Would it work ?
              • 4172
              • 5,888 Posts
              Yes, with a bit modifications, this is possible.
              You can use getImageList to build the inputs of existing items and add new items with a little bit of javascript.
              Have all input-fields as array.
              Then just convert these arrays back to MIGX - json-format.
              Don't forget to have a unique MIGX_id for each item.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 11121
                • 79 Posts
                The problem with the code below is when someone doesn't upload a file, when it saves, the field is remaining empty and the old file is gone. Any solution to that?

                <?php
                if (isset($_GET['resId'])){
                    if ($doc=$modx->getObject('modResource',array('id'=>$_GET['resId']))){
                        $docarray=$doc->toArray();
                        $fields = explode(',',$scriptProperties['resource2formitfields']);
                        $fields[] = 'id';
                  
                        foreach ($fields as $field){
                         
                            if ($doc->getFieldName($field) === null) {
                                /* if field isnt defined, look for TV value */
                                $tvValue = $doc->getTVValue($field);
                                if ($tvValue !== null) {
                                    $hook->setValue($field,$tvValue);
                                }
                            } else {
                                /* otherwise get field value */
                                $hook->setValue($field,$docarray[$field]);
                            }    
                        }
                    }
                  
                    //$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';  
                    //$hook->addError('error_message',$errorMsg);  
                }
                  
                  
                return true;
                • I dont see any filehandling in the code above.

                  I dont get the problem at all. Some does not upload a file, but it does save? I dont get it.
                    • 11121
                    • 79 Posts
                    Quote from: gallenkamp at Jun 03, 2015, 12:15 AM
                    I dont see any filehandling in the code above.

                    I dont get the problem at all. Some does not upload a file, but it does save? I dont get it.


                    In the resource2formit script above if you left blank an input file field (like below) it overwrites the previous data.

                    I need a declaretion that if there is no input, to keep current data.

                     <input id="IMAGE" class="custom-file-input" name="IMAGE" type="file" value="[[+fi.IMAGE]]" maxlength="100000" />
                    • Id have to try it but its too late today. Blind shot: did you try calling [[!+fi.IMAGE]] - so its not from cache?

                      Anyway - if I get you right the filehandler does overwrite file uploads with the same filename. And its not even telling you that it has overwritten something, right?