Jump to content

Custom admin form AJAX files issue


Pete
 Share

Recommended Posts

Hi guys and gals

I've created a backend module that essentially just loads in a template and its fields and this saves fine with file fields.

My issue is on that page I've created a modal window that also does the same with some other file fields and these get created in the DB with a creation date of 1st Jan 1970, +10 seconds (so basically the Pagefile::createdTemp constant). Next time I go to view this modal window with the files in, the files are deleted:

Pagefiles: Removed 'field_name' temp file(s) for page /admin/cases/43/file_name.pdf

So it seems that files saved by the modal have no date set, which is confusing as the modal page is saved, whereas the parent page - using near-identical code - saves the files with a created date.

The code used is this (more or less):

if($this->config->ajax && (isset($_SERVER['HTTP_X_FIELDNAME']) || count($_POST))) {
    $subfiles= $this->input->get('subfiles') ? true : false;
 
    // If we're dealing with a file submitted through the SubFiles modal
    if ($subfiles) {
        $this->pageContext = $this->pages->get('id=' . (int)$this->input->get('subfiles') . ', include=hidden');
    // else it's the parent page the modal was opened on
    } else {
        $this->pageContext = $this->pages->get($this->input->urlSegment2)->child('template=attachments, include=hidden');
    }

    $this->pageContext->setTrackChanges(true);
    $this->input->get->id = $this->pageContext->id; // ProcessPageEdit expects a GET or POST called 'id'

    $this->pageEdit = $this->modules->getModule("ProcessPageEdit", array('noPermissionCheck' => true));
    $this->pageEdit->setTrackChanges(true);

    $this->pageContext->trackChange($_SERVER['HTTP_X_FIELDNAME']);

    return $this->pageEdit->ajaxSave($this->pageContext);
}

Any ideas?

It's really difficult to explain, so hopefully someone knows what I'm talking about :)

Link to comment
Share on other sites

Setting the field to overwrite seems to skip the draft status on file fields introduced here: https://processwire.com/blog/posts/processwire-2.5.7-core-updates/ which works around my issue.

What I still don't understand is why when saving the page with the file field it's no changing them from draft file status to published (putting a created date in the "created" column for the file in the DB).

Link to comment
Share on other sites

Duh, turn debug on - it helps!

Because my custom form render function was skipping some fields in certain scenarios, one of them isn't there when another one is trying to use it as a dependency. Whoops! Should have this wrapped up in 5 mins - should have realised about 3 hours ago :(

Link to comment
Share on other sites

Things in a modal window or being processed via ajax are many times notorious to debug. I haven't looked at your code much but have you tried debugging every step in your modal window's code using the console, to confirm values are actually being set correctly? Something like....

$subfiles = $this->input->get('subfiles') ? true : false;
$data['message'] = $subfiles;
echo json_encode($data);
exit;
Link to comment
Share on other sites

I ended up ripping apart my code and starting again :)

The original version was based on an older version of Fredi, so I went back and found that Apeisa was doing something totally different there now. Sure enough, adapting the newer Fredi code for my purposes worked well.

It also gave me some good ideas for quick custom admin forms too but I don't think there are enough hours in the day to work on all these ideas unfortunately ;)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...