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

    I'm currently working on an update for UserImport (MODX Revolution Add-On) to support email notification of imported users. For this purpose I created a custom ExtJS form which holds the mail subject and the mailbody. The mailbody is a textarea field. Saving the form writes both fields to corresponding system settings.

    If I enter plain text I can save the form.
    If It contains HTML tags the values are saved but ExtJS seems to hang while reading the processor response.

    The following error (from Chrome console):

    Uncaught SyntaxError: Invalid regular expression: missing /
        at doDecode (ext-all.js:21)
        at Object.decode (ext-all.js:21)
        at Ext.form.Action.Submit.handleResponse (utilities.js:300)
        at Ext.form.Action.Submit.processResponse (ext-all.js:21)
        at Ext.form.Action.Submit.success (ext-all.js:21)
        at o (ext-all.js:21)
        at Ext.data.Connection.s (ext-all.js:21)
        at HTMLIFrameElement.I (ext-all.js:21)
    doDecode @ ext-all.js:21
    (anonymous) @ ext-all.js:21
    handleResponse @ utilities.js:300
    processResponse @ ext-all.js:21
    success @ ext-all.js:21
    o @ ext-all.js:21
    s @ ext-all.js:21
    I @ ext-all.js:21


    This is the processor response:

    ({"success":true,"data":{"hasheader":"1","batchsize":"0","delimiter":";","enclosure":"\"","usergroups":"","autousername":"0","setimportmarker":"1","notifyusers":"1","role":"0","mailsubject":"Notification: Your New User Account!","mailbody":"Hello,<br>\r\n<br>\r\nwe'd like to inform you, that your user credentials were imported into our system and a new user account was created for you!<br> \r\n<br>\r\n<strong>Here are your new account credentials:<\/strong><br>\r\n<br>\r\nName: [[+fullname]]<br>\r\nEmail: [[+email]]<br>\r\nYour username: [[+username]]<br>\r\nYour password: [[+password]]<br>","action":"mgr\/settings\/update","file":{"name":"","type":"","tmp_name":"","error":4,"size":0}}}</strong>)


    Please note the </strong> tag on the end of the string! I don't know where this is coming from!

    Here is the update.class.php processor:

    class SettingsUpdateProcessor extends modProcessor {
    
        public function process() {
    
            $settings = array(
                'delimiter',
                'enclosure',
                'autousername',
                'setimportmarker',
                'notifyusers',
                'mailsubject',
                'mailbody',
            );
    
            foreach ($settings as $key) {
                $value = $this->getProperty($key);
                if (isset($value)) {
                    $setting = $this->modx->getObject('modSystemSetting', 'userimport.'.$key);
                    if ($setting != null) {
                        $setting->set('value', $value);
                        $setting->save();
                    } else {
                        $this->modx->log(modX::LOG_LEVEL_ERROR, '[UserImport] SettingsUpdateProcessor: '.$key.' setting could not be found');
                    }
                }            
            }
            
            // refresh part of cache (MODx 2.1.x)
            $cacheRefreshOptions = array('system_settings' => array());
            $this->modx->cacheManager->refresh($cacheRefreshOptions);
    
            $response['success'] = true;
            $response['data'] = $this->getProperties();
            
            return $this->modx->toJSON($response);
        }
    
    }
    return 'SettingsUpdateProcessor';


    and here the ExtJS part with the textarea:

    {
                    xtype: 'textarea'
                    ,name: 'mailbody'
                    ,id: 'mailbody'
                    ,growMin: 250
                    ,grow: true
                    ,fieldLabel: _('userimport.notification_template_mail_body')
                    ,description: MODx.expandHelp ? '' : _('userimport.notification_template_mail_body_desc')
                    ,anchor: '100%'
                }


    What am I doing wrong? Any idea? [ed. note: gadgetto last edited this post 6 years, 5 months ago.]
      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 4172
      • 5,888 Posts
      what is this arround the json-response?
      (..........</strong>)
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 10378
        • 375 Posts
        Quote from: Bruno17 at Dec 06, 2017, 12:53 PM
        what is this arround the json-response?
        (..........)

        Hi Bruno,

        as I wrote: Please note the </strong> tag on the end of the string! I don't know where this is coming from!

        Do I need to encode the values?
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 42562
          • 1,145 Posts
          Very annoying problem. And I suspect that the answer is a rather simple ExtJS one.
          https://github.com/modxcms/revolution/issues/13711

          My observation

          The </strong> was exploded from the initial opening tag of the first closable HTML tag. (therefore not < br >, but < strong >).
          Because ExtJS already crashed at the first instance of a raw open angle bracket.

          To test, change your content to
          <i>Hello</i>,<br><br>we'd like to inform you,
          .

          If you encode the values, cool, but who will decode them?
          If ExtJS decodes them for you, how will it distinguish between codes that you'd rather left undecoded?

          Example: Using space in between "&" and "lt;" t prevent decoding here by this forum
          <i>Hello</i>,<br><br>we'd like to inform you that the correct syntax is<code>& lt;p class="paragraghClass"& gt;My Content & lt;/p>,
          .

          Questions for you

          1. At what time/MODX-event did you create that custom field?
          2. How are you saving to that custom textarea?

          document.getElementById("mailbody").value = myNewContent;
          //or
          Ext.get("mailbody").set({value: myNewContent}); 
          //or
          Ext.get("mailbody").dom.value = myNewContent;
          


          My Temp Solution is
          document.getElementById("mailbody").value = myNewContent.replace(/(?:<)/g, "&lt++;"); //snuff out all opening angle brackets


          and then
          if ($setting != null) {
            if($setting == "mailbody"){
              $value = str_replace("&lt++;", "<", $value); //remove your custom entity
            }
            $setting->set('value', $value);
            $setting->save();

            TinymceWrapper: Complete back/frontend content solution.
            Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
            5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
          • Is that html tag and the brackets around it also in the raw response? Just to make sure there isn't a browser plugin or dev tools inserting that.

            It almost looks like it's trying to return a jsonp response - a callback with the json instead of the json itself. That doesn't explain the html tag being there, but it would explain why the () are around the response. Do you have a field "callback" in the request?
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • 10378
              • 375 Posts
              Thanks to all responders!

              I probably didn’t explain correctly what I try to achieve:

              I created an extra with a CMP - the add-on is available in the MODX package manager and is called UserImport. The CMP is mainly a form with different fields needed for import requirements. My latest addition to the extra is the possibility to send notification mails to newly imported users.

              Therefore I added additional form fields to my CMP which can be filled with mailsubject and mailbody. The mailbody field is a textarea. Most fields of the CMP form can be saved to corresponding system settings. Starting the save process the ExtJS part submits all values - including the new mailsubject and mailbody fields - to the update processor which then stores the values in system settings.

              All values are saved correctly. Merely the the success message from the processor seems to return a malformed json response and therefore ExtJS doesn't correctly finish its work. [ed. note: gadgetto last edited this post 6 years, 5 months ago.]
                Freelancer @bitego http://www.bitego.com
                ---
                GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                More infos here: http://www.bitego.com/extras/goodnews/
                • 10378
                • 375 Posts
                Adding the following line to the update processor fixes the problem. The trick is to remove all fields from the processors response which could contain html tags:
                (I know it's only a workaround but hey - the MODX resource update processor is doing the same)

                unset ($response['data']['mailsubject'], $response['data']['mailbody']);


                Here is the full update processor including the fix:

                class SettingsUpdateProcessor extends modProcessor {
                 
                    public function process() {
                 
                        $settings = array(
                            'delimiter',
                            'enclosure',
                            'autousername',
                            'setimportmarker',
                            'notifyusers',
                            'mailsubject',
                            'mailbody',
                        );
                 
                        foreach ($settings as $key) {
                            $value = $this->getProperty($key);
                            if (isset($value)) {
                                $setting = $this->modx->getObject('modSystemSetting', 'userimport.'.$key);
                                if ($setting != null) {
                                    $setting->set('value', $value);
                                    $setting->save();
                                } else {
                                    $this->modx->log(modX::LOG_LEVEL_ERROR, '[UserImport] SettingsUpdateProcessor: '.$key.' setting could not be found');
                                }
                            }            
                        }
                         
                        // refresh part of cache (MODx 2.1.x)
                        $cacheRefreshOptions = array('system_settings' => array());
                        $this->modx->cacheManager->refresh($cacheRefreshOptions);
                 
                        $response['success'] = true;
                        $response['data'] = $this->getProperties();
                        unset ($response['data']['mailsubject'], $response['data']['mailbody']);
                
                        return $this->modx->toJSON($response);
                    }
                 
                }
                return 'SettingsUpdateProcessor';


                The problem is related to this:

                https://forums.modx.com/thread/95636/mxfb---success-message-rte-saving-error

                and this:

                https://github.com/modxcms/revolution/issues/13711
                  Freelancer @bitego http://www.bitego.com
                  ---
                  GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                  More infos here: http://www.bitego.com/extras/goodnews/