Combine pages en file upload (HELP NEEDED Corona virus project)

Hi there! Hope someone can help us out!

Im trying to figure out how to combine the two docs for creating pages and adding files to those pages frontend. I cant get it to work and have been add it for couple of days but we need this quick as our project is growing pretty quick and this would speed up the process.

Here is the link of the project: https://www.stay-sane-stay-safe.com

We want the people to be able to submit posters with the files on their own.
And that we can select those files for thumbnail and download-link.

Im able to create the (poster)pages in draft but not with the files that should go with them.

Controller (posters):

<?php

return function ($kirby, $page) {

    // if the form has been submitted…
    if ($kirby->request()->is('POST') && get('register')) {

        // check the honeypot and exit if is has been filled in
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'    => get('name'),
        ];

        $rules = [
            'name'  => ['required'],
        ];

        $messages = [
            'name'  => 'Please enter your (link: #name text: name)',
        ];

        // some of the data is invalid
        if ($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;

        } else {

            // authenticate as almighty
            $kirby->impersonate('kirby');

            // everything is ok, let's try to create a new registration
            try {
                // we store registrations as subpages of the current page
                $registration = $page->createChild([
                    'slug'     => $data['name']. date('D-M-Y-H-M'),
                    'template' => 'poster',
                    'content'  => $data,
                ]);

                if ($registration) {
                    // store referer and name in session
                    $kirby->session()->set([
                        'referer' => $page->uri(),
                        'regName'  => esc($data['name'])
                    ]);
                    go('success');
                }

            } catch (Exception $e) {
                $alert = ['Your registration failed: ' . $e->getMessage()];
            }
        }
    }

    // return data to template
    return [
        'alert' => $alert ?? null,
        'data'  => $data ?? false,
    ];
};

Snippet (registration-form):

<form class="registration-form" action="<?= $page->url() ?>" enctype="multipart/form-data" method="POST">

  <div class="form-element">
    <label for="name">Name <abbr title="required">*</abbr></label>
    <input type="text" id="name" name="name" value="<?= $data['name'] ?? null ?>" required/>
  </div>

  <input class="registration-button" type="submit" name="register" value="Register" />

</form>

Plugin:

<?php

Kirby::plugin('eventkit/posters', [
    'hooks' => [
        'kirbytags:after' => function ($text, $data, $options) {
            $session = kirby()->session();

            if ($location = $session->get('referer')) {
                if ($page = page(urldecode($location))) {
                    $title = $page->title();
                }
            }

            return Str::template($text, [
                'poster' => $title ?? '',
                'name'  => $session->get('regName') ?? ''
            ]);
        }
    ],
]);

Do you want to upload single files or multiple files?

There is no code regarding the file upload and no form field yet?

We want to upload multiple files, a zip and jpeg.

No i kept that stuff out since you guys would no best how to create it.

Basically like this:

<?php

return function ($kirby, $page) {

  $alerts  = [];
  $success = '';

    // if the form has been submitted…
    if ($kirby->request()->is('POST') && get('register')) {

        // check the honeypot and exit if is has been filled in
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'    => get('name'),
        ];

        $rules = [
            'name'  => ['required'],
        ];

        $messages = [
            'name'  => 'Please enter your (link: #name text: name)',
        ];

        $uploads = $kirby->request()->files()->get('file');

        // some of the data is invalid
        if ($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;

        } else {

            // authenticate as almighty
            $kirby->impersonate('kirby');

            // everything is ok, let's try to create a new registration
            try {
                // we store registrations as subpages of the current page
                $registration = $page->createChild([
                    'slug'     => $data['name']. date('D-M-Y-H-M'),
                    'template' => 'poster',
                    'content'  => $data,
                ]);
                if ($registration) {
                  foreach ($uploads as $upload) {
                  try {
                    $name = crc32($upload['name'].microtime()). '_' . $upload['name'];
                    $file = $registration->createFile([
                      'source'   => $upload['tmp_name'],
                      'filename' => $name,
                      'template' => 'upload',
                      'content' => [
                          'date' => date('Y-m-d h:m')
                      ]
                    ]);
                    //$success = true;
                  } catch (Exception $e) {
                    $alerts[$upload['name']] = $e->getMessage();
                  }
                }
              }
           
               
            } catch (Exception $e) {
                $alert = ['Your registration failed: ' . $e->getMessage()];
            }
            if (empty($alerts)) {
              // store referer and name in session
              $kirby->session()->set([
                  'referer' => $page->uri(),
                  'regName'  => esc($data['name'])
              ]);
              go('success');
            }

        }
    }

    // return data to template
    return [
        'alert' => $alert ?? null,
        'data'  => $data ?? false,
    ];
};

You probably need a few checks, ie. how many files may be uploaded, what file types are allowed etc. Files types and sizes can be defined in the file template, the errors will then be thrown automatically.

The max number of files you have to check additonally, see the recipe.

1 Like

Thanks so much, its working now!
Been adding the file types and limits. Hope this will speed everything up!