We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11121
    • 79 Posts
    Quote from: gallenkamp at Jun 03, 2015, 12:42 AM
    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?


    Well, no. I give to the user, the option to update his resource. The resource contains an image (input file type). When the user updates the resource and doesn't fill the input field, then the resource updates with no image. It doesn't keep the old image.

    Same issue with this (my english are not good enough to describe it):

    https://forums.modx.com/thread/?thread=80740&page=2

    Whenever the user updates his/her profile and does not upload a picture the form blanks the extended field and the profile picture dissapears. Not sure how to get around this. Might remove it from the form and make it a separate page.
      • 38309
      • 40 Posts
      Hi I think this script is fantastic, my be good as this post is several years old.

      I have used the scrip as below but have an issue where the content is actaully another chunk but it's not saving the details when it saves the new resources.
      I have updated the code a bit, I am still having issues where the content isnt saving.
      also I would like it to just update a resource is a resource is present with the same alias.

      [[!FormIt?
          &hooks=`spam,formit2resource,email,redirect,FormItAutoResponder`
          &preHooks=`resource2formit`
          &resource2formitfields=`pagetitle,alias,description,content`
              &emailTpl=`xmlEmail`
              &emailFrom=`[email protected]`
              &emailFromName=`Provision Confirmation`
              &emailSubject=`Device [[+mac]] Provisoned`
              &emailTo=`[email protected]`
          &redirectTo=`123`
              &redirectParams=`{"user":"123","success":"1"}`
              &fiarTpl=`xmlEmailClient`
              &fiarFrom=`[email protected]`
              &fiarFromName=`Provisioning Server`
              &fiarSubject=`You have just provisioned a device`
              &fiarCC=`[email protected]`
      ]]
      
      <!--
      redirect needs to direct to the following
      http://[[int_ipadd]]/admin/resync?[[+Siteurl]]/[[+locality]]/[[+mac]].xml
       -->
      
      <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]]" />
          <input name="parent" type="hidden" value="[[~79]]" />   
          <input name="pagetitle" type="hidden" value="[[+mac]]" />
          <input name="alias" type="hidden" value="[[+mac]]" />
          <input name="description" type="hidden" value="Customer Details: [[!userID]] - [[+company]]" />
          <input name="content" type="hidden" value="[[!$xmlProfile]]" />
      
      <br class="clear" />
      
      <div class="form-buttons">
         <input type="submit" value="Download Settings to Device" />
      </div>
      
      </form>


      the xmlProfile chunk is this:

      ?xml version="1.0" encoding="utf-8"?>
      <flat-profile>
      <User_ID_1_ value="[[+mac]]"/>
      <Proxy_1_ value="sip.webaddress.net:1234"/>
      <Admin_Passwd></Admin_Passwd>
      <Enable_Web_Admin_Access>yes</Enable_Web_Admin_Access>
      </flat-profile>


      and the formit2Resource is this:

      $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', '10');
      
      /*--------my add to code-----------*/
      $doc->set('parent', '79'); //creates file in resource 79
      $doc->set('hidemenu', '1'); //hide from menu
      $doc->set('published', '1'); //publish at once
      $doc->set('richtext', '0'); //turns off Rich Text
      $doc->set('searchable', '0'); //turns off Searchable
      $doc->set('cacheable', '0'); //turns off Cacheable
      $doc->set('content_type', '2'); //sets content type to XML
      $time = date('Y-m-d H:i:s', strtotime('+10 minutes')); //Finds current time
      $doc->set('unpub_date', '$time'); //needs to set unpublish date to 10min after published
      //$doc->set('context_key', "YouContextNameHere");
      /*--------end of my add----------*/
      $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();
          }
      }
      
      $modx->cacheManager->clearCache(); 
      return true;
      [ed. note: bigben83 last edited this post 9 years, 4 months ago.]
        • 38309
        • 40 Posts
        Id happily make a donation to someone if they can come up with a solution to the above.
          • 4172
          • 5,888 Posts
          The redirect-hook has allways to be the last hook!

          this line is not needed, because you are setting the parent hardcoded in your hook, and is wrong (should be value="79"):
          <input name="parent" type="hidden" value="[[~79]]" />


          where is [[+mac]] and [[+company]] coming from?

          what exactly goes wrong?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 38309
            • 40 Posts
            Thanks for replying Bruno17

            [[+mac]] and [[+company]] are from extended fields of logged in user.

            [this form is only available to logged in users.]

            the issue I am having is this

            <input name="content" type="hidden" value="[[!$xmlProfile]]" />


            when the form saves the resource it is not placing this chunk into the content of the page

              • 4172
              • 5,888 Posts
              you can try to read the Profile-placeholders in your hook-snippet and also get the chunk there.

              $properties['mac']=$modx->getPlaceholder('mac');
              $doc->set('content',$modx->getChunk('xmlProfile',$properties));



              But I'm not sure to understand, what you are trying to create at all.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 38309
                • 40 Posts
                Thanks Bruno

                The mac and client, creating resource, and emailing all work fine,
                the only thing that I am having trouble with is the xmlprofile as it wasn't being placed into the newly created resource.
                I will try your suggestion above now.

                I will send an pm to describe what I am doing.
                  • 38309
                  • 40 Posts
                  Thanks a million Bruno

                  this code worked a treat,
                  $doc->set('content',$modx->getChunk('xmlProfile',$properties));
                  


                  I will start working on the rest of the script now, I was beginning to think that it wasn't possible.
                    • 11121
                    • 79 Posts
                    Strange new problem. When I edit a resource, with the folowing Formit, it saves as a new resource instead of updating the current one!!! I also get this error message at error log:

                    (ERROR @ /index.php) modResource: Attempt to set NOT NULL field template to NULL


                    HTML
                    [[!FormIt? 
                        &hooks=`spam,formit2resource,redirect` 
                        &preHooks=`resource2formit`
                        &resource2formitfields=`parent,Active,Name,pagetitle,Paratiriseis,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,content,IMAGE,IMAGE-2,IMAGE-3`
                        &resId=`[[~[[*id]]]]`
                        &redirectTo=`106`
                        &validate=`
                         pagetitle:required,
                         Sex:required,
                         Race:required,
                         Age:required,
                         Phone:required,
                         Email:required,
                         Pedigree:required,
                         Area:required`
                        &vTextRequired=`*Απαραίτητο πεδίο`
                        &submitVar=`dog_input`
                    ]]
                    
                    <form class="form" action="[[~[[*id]]]]" method="post" name="dog_input" enctype='multipart/form-data' >
                        <input name="nospam:blank" type="hidden" />
                        <input name="resource_id" type="hidden" value="[[+fi.id]]" />
                        <input id="pagetitle" name="pagetitle" type="hidden" value="[[+fi.Name]]" />  
                        <input id="parent" name="parent" type="hidden" value="219" />  
                        <input id="template" name="template" type="hidden" value="23" /> 
                        <input id="published" name="published" type="hidden" value="1" /> 
                        <input id="hidemenu" name="hidemenu" type="hidden" value="1" /> 
                        <input id="active" name="active" type="hidden" value="YES" /> 
                        <div id="column_all">
                        <div class="column_dog2">
                        <div class="column_dog">
                        <label for="Name"><span class="error2">Όνομα σκύλου [[!+fi.error.Name]]</span></label>
                        <div class="neces"></div>
                        <input id="Name" class='input_4' name="Name" type="text" value="[[!+fi.Name]]" placeholder="ΟΝΟΜΑ ΣΚΥΛΟΥ"/>
                        <label for="Sex"><span class="error2">Φύλλο [[!+fi.error.Sex]]</span></label>
                        <div class="neces2"></div><select name="Sex" class="fancy_3">
                                <option value="" [[!+fi.Sex:FormItIsSelected=``]]>ΕΠΙΛΕΞΤΕ ΦΥΛΟ</option>
                                <option value="ΑΡΣΕΝΙΚΟ"  [[!+fi.Sex:FormItIsSelected=`ΑΡΣΕΝΙΚΟ`]]>ΑΡΣΕΝΙΚΟ</option>
                                <option value="ΘΗΛΥΚΟ"  [[!+fi.Sex:FormItIsSelected=`ΘΗΛΥΚΟ`]]>ΘΗΛΥΚΟ</option>
                        </select>
                        <label for="Race"><span class="error2">Ράτσα [[!+fi.error.Race]]</span></label>
                        <div class="neces2"></div><select name="Race" class="fancy_3">
                               [[$RATSES_CLEAN]]
                        </select>
                        <label for="Age"><span class="error2">Ηλικία [[!+fi.error.Age]]</span></label>
                        <div class="neces2"></div><select name="Age" class="fancy_3">
                                        <option value="" [[!+fi.Age:FormItIsSelected=``]]>ΗΛΙΚΙΑ</option>
                                        <option value="1"  [[!+fi.Age:FormItIsSelected=`1`]]>1</option>
                                        <option value="2"  [[!+fi.Age:FormItIsSelected=`2`]]>2</option>
                                        <option value="3"  [[!+fi.Age:FormItIsSelected=`3`]]>3</option>
                                        <option value="4"  [[!+fi.Age:FormItIsSelected=`4`]]>4</option>
                                        <option value="5"  [[!+fi.Age:FormItIsSelected=`5`]]>5</option>
                                        <option value="6"  [[!+fi.Age:FormItIsSelected=`6`]]>6</option>
                                        <option value="7"  [[!+fi.Age:FormItIsSelected=`7`]]>7</option>
                                        <option value="8"  [[!+fi.Age:FormItIsSelected=`8`]]>8</option>
                                        <option value="9"  [[!+fi.Age:FormItIsSelected=`9`]]>9</option>
                                        <option value="10" [[!+fi.Age:FormItIsSelected=`10`]]>10</option>
                                      </select>
                         <br>
                         <label for="Weight"><span class="error2">Βάρος [[!+fi.error.Weight]]</span></label>
                        <div class="not_neces"></div><input id="Weight" class='input_4' name="Weight" type="text" value="[[+fi.Weight]]" placeholder="ΒΑΡΟΣ"/>
                        <br>
                         </div>
                        <div class="column_dog">
                        
                        <label for="Pedigree"><span class="error2">Pedigree [[!+fi.error.Pedigree]]</span></label>
                        <div class="neces2"></div><select name="Pedigree" class="fancy_3">
                                <option value="" [[!+fi.Pedigree:FormItIsSelected=``]]>PEDIGREE</option>
                                <option value="ΝΑΙ"  [[!+fi.Pedigree:FormItIsSelected=`ΝΑΙ`]]>ΝΑΙ</option>
                                <option value="ΟΧΙ"  [[!+fi.Pedigree:FormItIsSelected=`ΟΧΙ`]]>ΟΧΙ</option>
                        </select>
                    
                        <label for="Microchip"><span class="error2">Microchip [[!+fi.error.Microchip]]</span></label>
                        <div class="not_neces"></div><select name="Microchip" class="fancy_3">
                                <option value="" [[!+fi.Microchip:FormItIsSelected=``]]>MICROCHIP</option>
                                <option value="ΝΑΙ"  [[!+fi.Microchip:FormItIsSelected=`ΝΑΙ`]]>ΝΑΙ</option>
                                <option value="ΟΧΙ"  [[!+fi.Microchip:FormItIsSelected=`ΟΧΙ`]]>ΟΧΙ</option>
                        </select>
                         <br>
                        <label for="Passport"><span class="error2">Διαβατήριο [[!+fi.error.Passport]]</span></label>
                       <div class="not_neces"></div><select name="Passport" class="fancy_3">
                                <option value="" [[!+fi.Passport:FormItIsSelected=``]]>PASSPORT</option>
                                <option value="ΝΑΙ"  [[!+fi.Passport:FormItIsSelected=`ΝΑΙ`]]>ΝΑΙ</option>
                                <option value="ΟΧΙ"  [[!+fi.Passport:FormItIsSelected=`ΟΧΙ`]]>ΟΧΙ</option>
                        </select>
                         <br>
                         <label for="Phone"><span class="error2">Τηλέφωνο επικοινωνίας[[!+fi.error.Phone]]</span></label>
                        <div class="neces"></div><input id="Phone" class='input_4' name="Phone:required" type="text" value="[[+fi.Phone]]" placeholder="ΤΗΛΕΦΩΝΟ"/>
                        <br>
                        <label for="Email"><span class="error2">Email [[!+fi.error.Email]]</span></label>
                        <div class="neces"></div><input id="Email" class='input_4' name="Email" type="text" value="[[+fi.Email]]" placeholder="E-MAIL"/>
                        </div>
                        <div class="column_dog2">
                        <label for="Email"><span class="error2">Παρατηρήσεις [[!+fi.error.Paratiriseis]]</span></label>
                        <textarea id="Paratiriseis" cols="89" rows="7" placeholder="Παρατηρήσεις" name="Paratiriseis">[[+fi.Paratiriseis]]</textarea>
                        </div>
                        </div>
                        <div class="column_dog">
                        <label for="Area"><span class="error2">Νομός [[!+fi.error.Area]]</span></label>
                        <div class="neces2"></div><select name="Area" class="fancy_3">
                                          <option value="" [[!+fi.Area:FormItIsSelected=``]]>ΕΠΙΛΕΞΤΕ ΝΟMO</option>
                                          <option value="%" [[!+fi.Area:FormItIsSelected=`%`]]>ΕΜΦΑΝΙΣΗ ΟΛΩΝ</option>
                                          <option value="ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ`]]>ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ</option>
                                          <option value="ΑΡΓΟΛΙΔΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΡΓΟΛΙΔΑΣ`]]>ΑΡΓΟΛΙΔΑΣ</option>
                                          <option value="ΑΡΚΑΔΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΡΚΑΔΙΑΣ`]]>ΑΡΚΑΔΙΑΣ</option>
                                          <option value="ΑΡΤΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΡΤΑΣ`]]>ΑΡΤΑΣ</option>
                                          <option value="ΑΤΤΙΚΗΣ"  [[!+fi.Area:FormItIsSelected=`ΑΤΤΙΚΗΣ`]]>ΑΤΤΙΚΗΣ</option>
                                          <option value="ΑΧΑΪΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΧΑΪΑΣ`]]>ΑΧΑΪΑΣ</option>
                                          <option value="ΒΟΙΩΤΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΒΟΙΩΤΙΑΣ`]]>ΒΟΙΩΤΙΑΣ</option>
                                          <option value="ΓΡΕΒΕΝΩΝ"  [[!+fi.Area:FormItIsSelected=`ΓΡΕΒΕΝΩΝ`]]>ΓΡΕΒΕΝΩΝ</option>
                                          <option value="ΔΡΑΜΑΣ"  [[!+fi.Area:FormItIsSelected=`ΔΡΑΜΑΣ`]]>ΔΡΑΜΑΣ</option>
                                          <option value="ΔΩΔΕΚΑΝΗΣΟΥ"  [[!+fi.Area:FormItIsSelected=`ΔΩΔΕΚΑΝΗΣΟΥ`]]>ΔΩΔΕΚΑΝΗΣΟΥ</option>
                                          <option value="ΕΒΡΟΥ"  [[!+fi.Area:FormItIsSelected=`ΕΒΡΟΥ`]]>ΕΒΡΟΥ</option>
                                          <option value="ΕΥΒΟΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΕΥΒΟΙΑΣ`]]>ΕΥΒΟΙΑΣ</option>
                                          <option value="ΕΥΡΥΤΑΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΕΥΡΥΤΑΝΙΑΣ`]]>ΕΥΡΥΤΑΝΙΑΣ</option>
                                          <option value="ΖΑΚΥΝΘΟΥ"  [[!+fi.Area:FormItIsSelected=`ΖΑΚΥΝΘΟΥ`]]>ΖΑΚΥΝΘΟΥ</option>
                                          <option value="ΗΛΕΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΗΛΕΙΑΣ`]]>ΗΛΕΙΑΣ</option>
                                          <option value="ΗΜΑΘΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΗΜΑΘΙΑΣ`]]>ΗΜΑΘΙΑΣ</option>
                                          <option value="ΗΡΑΚΛΕΙΟΥ"  [[!+fi.Area:FormItIsSelected=`ΗΡΑΚΛΕΙΟΥ`]]>ΗΡΑΚΛΕΙΟΥ</option>
                                          <option value="ΘΕΣΠΡΩΤΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΘΕΣΠΡΩΤΙΑΣ`]]>ΘΕΣΠΡΩΤΙΑΣ</option>
                                          <option value="ΘΕΣΣΑΛΟΝΙΚΗΣ"  [[!+fi.Area:FormItIsSelected=`ΘΕΣΣΑΛΟΝΙΚΗΣ`]]>ΘΕΣΣΑΛΟΝΙΚΗΣ</option>
                                          <option value="ΙΩΑΝΝΙΝΩΝ"  [[!+fi.Area:FormItIsSelected=`ΙΩΑΝΝΙΝΩΝ`]]>ΙΩΑΝΝΙΝΩΝ</option>
                                          <option value="ΚΑΒΑΛΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΑΒΑΛΑΣ`]]>ΚΑΒΑΛΑΣ</option>
                                          <option value="ΚΑΡΔΙΤΣΗΣ"  [[!+fi.Area:FormItIsSelected=`ΚΑΡΔΙΤΣΗΣ`]]>ΚΑΡΔΙΤΣΗΣ</option>
                                          <option value="ΚΑΣΤΟΡΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΑΣΤΟΡΙΑΣ`]]>ΚΑΣΤΟΡΙΑΣ</option>
                                          <option value="ΚΕΡΚΥΡΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΕΡΚΥΡΑΣ`]]>ΚΕΡΚΥΡΑΣ</option>
                                          <option value="ΚΕΦΑΛΛΗΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΕΦΑΛΛΗΝΙΑΣ`]]>ΚΕΦΑΛΛΗΝΙΑΣ</option>
                                          <option value="ΚΙΛΚΙΣ"  [[!+fi.Area:FormItIsSelected=`ΚΙΛΚΙΣ`]]>ΚΙΛΚΙΣ</option>
                                          <option value="ΚΟΖΑΝΗΣ"  [[!+fi.Area:FormItIsSelected=`ΚΟΖΑΝΗΣ`]]>ΚΟΖΑΝΗΣ</option>
                                          <option value="ΚΟΡΙΝΘΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΟΡΙΝΘΙΑΣ`]]>ΚΟΡΙΝΘΙΑΣ</option>
                                          <option value="ΚΥΚΛΑΔΩΝ"  [[!+fi.Area:FormItIsSelected=`ΚΥΚΛΑΔΩΝ`]]>ΚΥΚΛΑΔΩΝ</option>
                                          <option value="ΛΑΚΩΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΛΑΚΩΝΙΑΣ`]]>ΛΑΚΩΝΙΑΣ</option>
                                          <option value="ΛΑΡΙΣΑΣ"  [[!+fi.Area:FormItIsSelected=`ΛΑΡΙΣΑΣ`]]>ΛΑΡΙΣΑΣ</option>
                                          <option value="ΛΑΣΙΘΙΟΥ"  [[!+fi.Area:FormItIsSelected=`ΛΑΣΙΘΙΟΥ<`]]>ΛΑΣΙΘΙΟΥ</option>
                                          <option value="ΛΕΣΒΟΥ"  [[!+fi.Area:FormItIsSelected=`ΛΕΣΒΟΥ`]]>ΛΕΣΒΟΥ</option>
                                          <option value="ΛΕΥΚΑΔΟΣ"  [[!+fi.Area:FormItIsSelected=`ΛΕΥΚΑΔΟΣ`]]>ΛΕΥΚΑΔΟΣ</option>
                                          <option value="ΜΑΓΝΗΣΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΜΑΓΝΗΣΙΑΣ`]]>ΜΑΓΝΗΣΙΑΣ</option>
                                          <option value="ΜΕΣΣΗΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΜΕΣΣΗΝΙΑΣ`]]>ΜΕΣΣΗΝΙΑΣ</option>
                                          <option value="ΞΑΝΘΗΣ"  [[!+fi.Area:FormItIsSelected=`ΞΑΝΘΗΣ`]]>ΞΑΝΘΗΣ</option>
                                          <option value="ΠΕΛΛΑΣ"  [[!+fi.Area:FormItIsSelected=`ΠΕΛΛΑΣ`]]>ΠΕΛΛΑΣ</option>
                                          <option value="ΠΙΕΡΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΠΙΕΡΙΑΣ`]]>ΠΙΕΡΙΑΣ</option>
                                          <option value="ΠΡΕΒΕΖΗΣ"  [[!+fi.Area:FormItIsSelected=`ΠΡΕΒΕΖΗΣ`]]>ΠΡΕΒΕΖΗΣ</option>
                                          <option value="ΡΕΘΥΜΝΗΣ"  [[!+fi.Area:FormItIsSelected=`ΡΕΘΥΜΝΗΣ`]]>ΡΕΘΥΜΝΗΣ</option>
                                          <option value="ΡΟΔΟΠΗΣ"  [[!+fi.Area:FormItIsSelected=`ΡΟΔΟΠΗΣ`]]>ΡΟΔΟΠΗΣ</option>
                                          <option value="ΣΑΜΟΥ"  [[!+fi.Area:FormItIsSelected=`ΣΑΜΟΥ`]]>ΣΑΜΟΥ</option>
                                          <option value="ΣΕΡΡΩΝ"  [[!+fi.Area:FormItIsSelected=`ΣΕΡΡΩΝ`]]>ΣΕΡΡΩΝ</option>
                                          <option value="ΤΡΙΚΑΛΩΝ"  [[!+fi.Area:FormItIsSelected=`ΤΡΙΚΑΛΩΝ`]]>ΤΡΙΚΑΛΩΝ</option>
                                          <option value="ΦΘΙΩΤΙΔΑΣ"  [[!+fi.Area:FormItIsSelected=`ΦΘΙΩΤΙΔΑΣ`]]>ΦΘΙΩΤΙΔΑΣ</option>
                                          <option value="ΦΛΩΡΙΝΗΣ"  [[!+fi.Area:FormItIsSelected=`ΦΛΩΡΙΝΗΣ`]]>ΦΛΩΡΙΝΗΣ</option>
                                          <option value="ΦΩΚΙΔΑΣ"  [[!+fi.Area:FormItIsSelected=`ΦΩΚΙΔΑΣ`]]>ΦΩΚΙΔΑΣ</option>
                                          <option value="ΧΑΛΚΙΔΙΚΗΣ"  [[!+fi.Area:FormItIsSelected=`ΧΑΛΚΙΔΙΚΗΣ`]]>ΧΑΛΚΙΔΙΚΗΣ</option>
                                          <option value="ΧΑΝΙΩΝ"  [[!+fi.Area:FormItIsSelected=`ΧΑΝΙΩΝ`]]>ΧΑΝΙΩΝ</option>
                                          <option value="ΧΙΟΥ"  [[!+fi.Area:FormItIsSelected=`ΧΙΟΥ`]]>ΧΙΟΥ</option>
                                      </select>
                        <br>
                        
                        <label for="Email"><span class="error2">Φωτογραφίες (αρχεία jpg, gif, png)[[+fi.error.nomination_file]]</span></label>
                        <div class="photo_input">
                        <input id="IMAGE" class="custom-file-input" name="IMAGE" type="file" value="[[!+fi.IMAGE]]" maxlength="100000" />
                        <div id="ad_image_small" ><img src="[[+fi.IMAGE:phpthumbof=`w=115&h=90&zc=t`]]" /></div>
                        </div>
                        <div class="photo_input">
                        <input id="IMAGE" class="custom-file-input" name="IMAGE-2" type="file" value="[[!+fi.IMAGE-2]]" maxlength="100000" />
                        <div id="ad_image_small" ><img src="[[+fi.IMAGE-2:phpthumbof=`w=115&h=90&zc=t`]]" /></div>
                        </div>
                        <div class="photo_input">
                        <input id="IMAGE" class="custom-file-input" name="IMAGE-3" type="file" value="[[!+fi.IMAGE-3]]" maxlength="100000" />
                        <div id="ad_image_small" ><img src="[[+fi.IMAGE-3:phpthumbof=`w=115&h=90&zc=t`]]" /></div>
                        </div>
                        </span>
                        </div>
                        </div>
                            <input type="submit" id="button_7" name="dog_input" value="Αποθήκευση αλλαγών"/>
                    </form>



                    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);
                        }
                     
                       //we need to parse the title into the alias
                       if($field == 'pagetitle'){
                          //replace spaces with -
                          $alias = preg_replace('/[\' \']/' , '-' , $value);
                     
                          //remove non alpha and a common injection string 
                          $alias = preg_replace('/[^a-z0-9\.\x0B\-]/i' , '' , $alias);
                     
                                                       //this is the standard revo regexp
                                                       // \0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\
                       }
                    }
                     // now set the alias
                     $doc->set('alias', $alias);
                      
                    $doc->set('template', $template);
                    $isnew = $doc->isNew();
                     
                    if ($doc->save()) {
                        if ($isnew) {
                            $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();
                        }
                    }
                    
                    $url = $modx->makeUrl(84);
                    
                    ////////////formit2file part ////////////////////////////////////
                    
                    // initialize output;
                    $output = true;
                    // valid extensions
                    $ext_array = array('jpg', 'jpeg', 'gif', 'png');
                      
                    // create unique path for this form submission
                    //$uploadpath = 'assets/img-annonceurs/';
                      
                    // you can create some logic to automatically
                    // generate some type of folder structure here.
                    // the path that you specify will automatically
                    // be created by the script if it doesn't already
                    // exist.
                      
                    // EXAMPLE:
                    // this would put all file uploads into a new,
                    // unique folder every day.
                    $uploadpath = 'assets/images/users/'.date('Y-m-d').'/';
                      
                    // get full path to unique folder
                    $target_path = $modx->config['base_path'] . $uploadpath;
                      
                    // get uploaded file names:
                    $submittedfiles = array_keys($_FILES);
                      
                    // loop through files
                    foreach ($submittedfiles as $sf) {
                    
                        // Get Filename and make sure its good.
                        $filename = basename( $_FILES[$sf]['name'] );
                      
                        // Get file's extension
                        $ext = pathinfo($filename, PATHINFO_EXTENSION);
                        $ext = mb_strtolower($ext); // case insensitive
                      
                        // is the file name empty (no file uploaded)
                        if($filename != '') {
                              
                            // is this the right type of file?
                            if(in_array($ext, $ext_array)) {
                          
                                // change name to: resource_id-input_name.ext
                                $filename = $doc->get('id').'-'.$sf.'.'.$ext;
                    
                    
                                // full path to new file
                                $myTarget = $target_path . $filename;
                    			//URL path
                    			$urlPath = $uploadpath . $filename;
                                  
                                // create directory to move file into if it doesn't exist
                                if(!is_dir($target_path)) {
                                    mkdir($target_path, 0755, true);
                                    } else {
                                    $modx->log(modX::LOG_LEVEL_ERROR, 'Upload:Path already exists' );    
                                    }
                    
                                // is the file moved to the proper folder successfully?
                                if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                                  // place file path to tv
                                 if (!$doc->setTVValue($sf, $urlPath)) $modx->log(xPDO::LOG_LEVEL_ERROR, 'Upload:Saving TV error');
                    
                                    // set the permissions on the file
                                    if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                    
                                } else {
                                    // File not uploaded
                                    $errorMsg = 'There was a problem uploading the file.';
                                    $hook->addError($sf, $errorMsg);
                                    $output = false; // generate submission error
                                }
                              
                            } else {
                                // File type not allowed
                                $errorMsg = 'Type of file not allowed.';
                                $hook->addError($sf, $errorMsg);
                                $output = false; // generate submission error
                            }
                          
                        // if no file, don't error, but return blank
                        } else {
                           // $hook->setValue($sf, '');  // dont need that
                    	   $doc->setTVValue($sf, '');    // insert empty value to TV
                        }
                      
                    }
                    
                    $modx->cacheManager->refresh(); // suggested by gallenkamp
                    return $output;



                    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 {
                    if ($field == "[[+fi.IMAGE]]" & $docarray[$field] !== null) { 
                                      $hook->setValue($field,$docarray[$field]);
                                  }
                                }    
                            }
                        }
                      
                        //$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';  
                        //$hook->addError('error_message',$errorMsg);  
                    }  
                    return true;
                    [ed. note: gordonas last edited this post 9 years, 1 month ago.]
                      • 53989
                      • 2 Posts
                      It looks like the last post was over 2 years ago, hopefully this thread is still active. Thanks Bruno17 for this handy snippet! It is working very well for creating new resources and, for the most part, works when editing resources. However, it does not retrieve the values set for either Check Box, Listbox or Radio Options template variables. Every time I open a resource to edit, all the other values are retrieved but the select is set to default value. It will save the Listbox with a new value, but always goes back to default upon editing. Currently, running MODX 2.6.1. For reference, this is the code I use for the Formit Listbox:

                      <select id=example name=example>
                         <option value=1 [[!+fi.example:FormItIsSelected=`1`]]>Example 1</option>
                         <option value=2 [[!+fi.example:FormItIsSelected=`2`]]>Example 2</option>
                         <option value=3 [[!+fi.example:FormItIsSelected=`3`]]>Example 3</option>
                         <option value=4 [[!+fi.example:FormItIsSelected=`4`]]>Example 4</option>
                         <option value=5 [[!+fi.example:FormItIsSelected=`5`]]>Example 5</option>
                         <option value=6 [[!+fi.example:FormItIsSelected=`6`]]>Example 6</option>
                         <option value=7 [[!+fi.example:FormItIsSelected=`7`]]>Example 7</option>
                      </select>


                      Am I missing something?