We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19388
    • 297 Posts
    Hi,
    I’m trying to make my first plugin. What I want to do is to show two new file fields in "Manager user", to upload a user photo and a company logo. The first I’m trying is to upload the user photo in the avalaible database field using this code:
    <?php
    /**
     * Register a form field to forms
     */
    switch ($modx->event->name) {
        case 'OnUserFormPrerender':
            /* if you want to add custom scripts, css, etc, register them here */
            break;
        case 'OnUserFormRender':
      
            /* now do the HTML */
            $fields = '
          <div class="x-form-item x-tab-item">
            <label class="x-form-item-label" style="width:150px;">Foto</label>
            <div class="x-form-element">
              <label for="foto"></label>
              <input type="file" name="foto" id="foto" class="x-form-text x-form-field">
            </div>
          </div>
                
          <div class="x-form-item x-tab-item">
            <label class="x-form-item-label" style="width:150px;">Logo empresa</label>
            <div class="x-form-element">
              <label for="logo_empresa"></label>
              <input type="file" name="logo_empresa" id="logo_empresa" class="x-form-text x-form-field">
            </div>
          </div>        
    
          <div class="x-form-item x-tab-item">
            <label class="x-form-item-label" style="width:150px;">Logo empresa</label>
            <div class="x-form-element">
              <label for="prueba"></label>
              <input name="prueba" type="input" class="x-form-text x-form-field" id="prueba" value="prueba">
        </div>
          </div>                        
          ';
        
          $modx->event->output($fields);
    
          break;
        case 'OnUserFormSave':
            /* do processing logic here. */
        
            $resource =& $scriptProperties['user'];
            $resource->set('photo',$_FILES['foto']['name']);
            //$resource->save();
            
            if ( $resource->save()) {
                 $output = "Photo saved";
             } else {
                 $output = "Error";
             }    
            return $output;
            
            break;
    }


    The fields appear correctly, but no message is shown and data is no saved.
    I thought it could be because the generated form is no multipart, so I created an input field with a test text and tried to save it in "photo" field too. Same problem, data was not saved.

    Questions:
    - How to solve it?
    - How to show an error/succesfull message or log it.
    - This fields doesn’t affect to "save" button status, so it remains inactive unless I edit another field.

    Thanks in advance
      • 3749
      • 24,544 Posts
      Photo is an extended field, so just trying to set it as a regular field won’t work. You want something more like this:

      <?php
      case 'OnUserFormSave':
              /* do processing logic here. */
              /* the user object is already available */
         
              $profile = $user->getOne('Profile');
              $extendedFields = $profile->get('extended);
      
              $extendedFields['photo'] = $_FILES['foto']['name']);
            
              $profile->set('extended',$extendedFields);
      
              $profile->save();
      
              /* The return value from OnUserFormSave is ignored  so there's no point in returning anything.
                * You could write to the log here, instead.
                */
              
              
              break;


      I think you’d have to use JS to show the user a message.
        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
        • 19388
        • 297 Posts
        It works for normal fields ( input ). I will try to do it for files now.
        I’ll investigate to change the "save" field status too.
        If I "return" an output message, it is automatically logged.
          • 19388
          • 297 Posts
          And thanks of course!! wink
            • 19388
            • 297 Posts
            I tried some things but file upload doesn’t work.

            The code i’m using now is:
            <?php
            /**
             * Register a form field to forms
             */
            switch ($modx->event->name) {
                case 'OnUserFormPrerender':
                    /* if you want to add custom scripts, css, etc, register them here */
                    break;
                case 'OnUserFormRender':
                    $v = '';
                    if (isset($scriptProperties['user'])) {
                        /* on the update screen, so set the value */
                        $user =& $scriptProperties['user'];
                        $profile = $user->getOne('Profile');
                        $extendedFields = $profile->get('extended'); 
                        $v = $extendedFields['photo']; 
                      
                    } else {
                        /* on the create screen, so set the default */
                        $v = '';
                    }  
                
                
                    /* now do the HTML */
                    $fields = '
                  <div class="x-form-item x-tab-item">
                    <label class="x-form-item-label" style="width:150px;">Foto</label>
                    <div class="x-form-element">
                      <label for="foto"></label>
                      <input type="file" name="foto" id="foto" class="x-form-text x-form-field">
                    </div>
                  </div>
                        
                  <div class="x-form-item x-tab-item">
                    <label class="x-form-item-label" style="width:150px;">Logo empresa</label>
                    <div class="x-form-element">
                      <label for="logo_empresa"></label>
                      <input type="file" name="logo_empresa" id="logo_empresa" class="x-form-text x-form-field">
                    </div>
                  </div>        
            
                  <div class="x-form-item x-tab-item">
                    <label class="x-form-item-label" style="width:150px;">Logo empresa</label>
                    <div class="x-form-element">
                      <label for="prueba">Prueba</label>
                      <input name="prueba" type="input" class="x-form-text x-form-field" id="prueba" value="'.$v.'">
                    </div>
                  </div>                        
                  ';
                
                  $modx->event->output($fields);
            
                  break;
                case 'OnUserFormSave':
                    /* do processing logic here. */
            
            
                
                      $user =& $scriptProperties['user'];
                      $profile = $user->getOne('Profile');
                      $extendedFields = $profile->get('extended'); 
                      $extendedFields['prueba'] = $_POST['prueba'];      
                
                      //return $_FILES['foto']['name'];
                      
                      if (is_uploaded_file($_FILES['foto']['tmp_name'])) {
                        $uploaddir = '/home/carroza/www/assets/images/zona_socios/fotos/';
                        $uploadfile = $uploaddir . basename($_FILES['foto']['name']);
                        
                        if (move_uploaded_file($_FILES['foto']['tmp_name'], $uploadfile)) {
                            $extendedFields['foto'] = $uploadfile;         
                            $output = "OK.\n";
                        } else {
                            $output = "Error\n";
                        }      
                      } else {
                        $output = 'Error, no subida.';
                      }
                
                
                      $profile->set('extended',$extendedFields);
                      $profile->save();  
                      return $output;  
                  
                    break;
            }
            return;​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


            The form is not multipart, so I tried to change /manager/assets/modext/sections/security/profile/update.js (line 85) to:
            MODx.panel.UpdateProfile = function(config) {
                config = config || {};
                Ext.applyIf(config,{
                    title: _('general_information')
            		,fileUpload: true		
            


            The result is the same, file is not beign uploaded.
            I’m not fammiliar with extjs and the MODx core, so it’s beign difficult for me to do this sad
            Any help?
              • 3749
              • 24,544 Posts
              Quote from: mIDO at Mar 03, 2011, 05:15 AM

              If I "return" an output message, it is automatically logged.

              You’re right. I didn’t know that. I see that if you return false, "Plugin failed" goes in the error log."

              You might be able to show a message with one of these:

              $modx->setLogTarget('ECHO');

              or
              $modx->setLogTarget('HTML');


              I’m afraid I’m no help on the file upload.


                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
                • 19388
                • 297 Posts
                You have been so helpfull, thank you laugh
                I hope someone else can help me and other MODx members to do it, because I know this is a very requested feature.
                  • 3749
                  • 24,544 Posts
                  Just out of curiosity, did setLogTarget() make the error message appear on the screen?
                    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
                    • 16313
                    • 71 Posts
                    Thanks Miguel for your great job.
                    One note.
                    There is necessary to add "fileUpload: true" to file modx.panel.user.js and change compress_js key from System setting.

                    path: /manager/assets/modext/widgets/security
                    MODx.panel.User = function(config) {
                        config = config || {};
                        Ext.applyIf(config,{
                            url: MODx.config.connectors_url+'security/user.php'
                            ,baseParams: {}
                            ,fileUpload: true