We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41101
    • 40 Posts
    Thanks Bob.

    Greatly appreciated.
      • 41101
      • 40 Posts
      I've finally got somewhere (probably much to your relief Bob lol).

      I created a new TV (file) which is a file upload field and created a new media source that opens the users device file storage. Set basePathRelative to 'No'. I might have changed something else here but I can't remember.

      Then in the NewsPublisher outerTpl I added the enctype to multipart form data.

      I also then changed the npFileTpl slightly so it doesn't use the filebrowser
      <div id="np-[[+npx.fieldName]]-container" class="np-file">
              [[+np.error_[[+npx.fieldName]]]]
              <label class="fieldlabel" for="np-[[+npx.fieldName]]" title="[[+npx.help]]">[[+npx.caption]]: </label>
              <input name="[[+npx.fieldName]]" class="file" id="np-[[+npx.fieldName]]" type="file"  value="[[+np.[[+npx.fieldName]]]]" />
              <!--<button type="button" onclick="var popup=window.open('[[+npx.browserUrl]]', 'Select file...', 'width=' + Math.min(screen.availWidth,1000) + ',height=' + Math.min(screen.availHeight*0.9,700) + 'status=no,location=no,toolbar=no,menubar=no');popup.focus();browserPathInput=getElementById('np-[[+npx.fieldName]]');">[[%np_launch_file_browser]]</button>-->
          </div>


      Finally (not the greatest idea but it works) I edited the Newspublisher class file to include the image upload processing.
      // John Davison - ImageUpload Edit
      			if(!empty($_FILES['file']['name'])) {
      				if ($_FILES['file']['error'] > 0) {
      					$this->setError('Errors with file upload' . $_FILES['file']['error']);
      					$linkURL = '';
      				} else {
      					function findexts ($filename) 
      					 { 
      						 $filename = strtolower($filename) ; 
      						 $exts = split("[/\\.]", $filename) ; 
      						 $n = count($exts)-1; 
      						 $exts = $exts[$n]; 
      						 return $exts; 
      					 } 
      					$ext = findexts($_FILES['file']['name']);
      					$ran = rand();
      					$ran2 = $ran . '.';
      					$destination = "assets/uploads/";
      					$destination = $destination . $ran2.$ext;
      					move_uploaded_file($_FILES['file']['tmp_name'], $destination);
      					/* create the image link URL */
      			        $linkURL = '<img src="'. $destination . '" style="max-width:400px;" /> <br />';
      				}
      			} 
      			
                  /* these *might* be in the $_POST array. Set them if not */
                  $fields['published'] = isset($_POST['published'])? $_POST['published']: $this->published;
                  $fields['hidemenu'] = isset($_POST['hidemenu'])? $_POST['hidemenu']: $this->hideMenu;
                  $fields['template'] = isset ($_POST['template']) ? $_POST['template'] : $this->template;
                  $fields['parent'] = isset ($_POST['parent']) ? $_POST['parent'] : $this->parentId;
                  $fields['searchable'] = isset ($_POST['searchable']) ? $_POST['searchable'] : $this->searchable;
                  $fields['cacheable'] = isset ($_POST['cacheable']) ? $_POST['cacheable'] : $this->cacheable;
                  $fields['richtext'] = isset ($_POST['richtext']) ? $_POST['richtext'] : $this->richtext;
                  $fields['createdby'] = $this->modx->user->get('id');
                  $fields['content']  = $this->header . $linkURL . $fields['content'] . $this->footer;
                  $fields['context_key'] = $this->modx->context->get('key');


      As it's just a couple of Super Users who will have access to this I'm not too concerned about the security as the resource that contains the NewsPublisher snippet call is restricted to those users.

      A bit hacky but it seems to suffice.

      Cheers.

        • 3749
        • 24,544 Posts
        Looks good. Be sure to back up the NP class file so you can restore it after upgrading NewsPublisher
          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