We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30025
    • 32 Posts
    timharris777 Reply #1, 14 years ago
    I have been using Revo. Its great so far. I however would like to be able to create a document from the front end with a form. Kind of like newsPublisher did in Evo. Does anyone have an idea of how I could do this?
      • 4172
      • 5,888 Posts
      example with formit:

      [[!FormIt? 
      &hooks=`spam,formit2resource,email` 
      &preHooks=`resource2formit`
      &resource2formitfields=`parent,pagetitle,strasse,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="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>


      code for snippet formit2resource:

      <?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', '1');
      $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;
      ?>
      



      resource2formit:

      <?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;

      [ed. note: Bruno17 last edited this post 9 years, 3 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 30025
        • 32 Posts
        timharris777 Reply #3, 14 years ago
        I’ll give this a try and see if I can get it to work. It might take a week or so, but I will post any problems I have.
          • 30025
          • 32 Posts
          timharris777 Reply #4, 14 years ago
          It works great!! Looks like I didn’t have to wait a week to try it.

          My only issues is with TVs. I can create a document with the required template for a TV to be available but the TV content from the form will not be added to the resource’s TV field. I am creating the resource with the correct template that has the TV assigned to it. Can’t figure out what I’m doing wrong.

          Anyone have any ideas?
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Saving TVs for a new resource is a nuisance, whether Evo or Revo. You can’t save a TV value until the resource is saved and you know what the resource ID is. Then you can save the TV value.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 4172
              • 5,888 Posts
              modified the code above for save TVs, which seems to work.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 17422
                • 225 Posts
                I integrate your code, and work fine smiley thanks

                and now if i want update this resource, in a another page.

                I need to create a snippet to retrieve the value and generate the form and modify the snippet formit2db for retrieve the id of document and update ?
                  • 4172
                  • 5,888 Posts
                  I need to create a snippet to retrieve the value and generate the form and modify the snippet formit2db for retrieve the id of document and update ?

                  I hope you can use preHooks to set your values.
                  I’m upgrading to 2.0.1 at this time.
                  When its finished I can try it out and post my solution for this.
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 17422
                    • 225 Posts
                    thank you very much for your help,

                    I’ll watch for use the prehooks

                    Laurent
                      • 4172
                      • 5,888 Posts
                      ok, added preHook db2formit and modified snippetcall, formit2db and the form. Now you can create and edit resources.
                      add &resId=yourResourceID to edit resource to your URL, where yourResourceID is your id for resource you want to edit.
                      in your snippetcall set your fields (resourcefields or TVs) you want to edit with &db2formitfields=
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!