Skip to content

Instantly share code, notes, and snippets.

@starckio
Last active January 18, 2018 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starckio/308aabebb2be8ec6b4c48f467c5b8a8d to your computer and use it in GitHub Desktop.
Save starckio/308aabebb2be8ec6b4c48f467c5b8a8d to your computer and use it in GitHub Desktop.
Create a page with send image[s] from the frontend.
<?php
return function($site, $pages, $page) {
$alert = null;
$messages = null;
if(r::is('post') && get('submit')) {
if(!empty(get('subject'))) {
// lets tell the bot that everything is ok
go($page->url());
exit;
}
$data = array(
'title' => esc(get('title')),
'text' => esc(get('text'))
);
$rules = array(
'title' => array('required'),
'text' => array('required'),
);
$messages = array(
'title' => 'Please enter a title',
'text' => 'Please enter a short text'
);
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
} else {
// everything is ok, let's try to create a new registration
try {
$newPage = $page->children()->create(uniqid(), 'default', $data);
$success = 'The creation of the page was a success';
$data = array();
$upload = new Upload($newPage->root() . DS . '{safeFilename}', array(
'input' => 'file',
'overwrite' => true,
'accept' => 'image/*'
));
} catch(Exception $e) {
echo 'Creating the page failed: ' . $e->getMessage();
}
}
}
return compact('alert', 'messages', 'data', 'success');
};
<?php snippet('header') ?>
<main class="main" role="main">
<h1><?= $page->subtitle()->or($page->title()) ?></h1>
<?php if(isset($success)): ?>
<div class="alert success">
<?php echo html($success) ?>
</div>
<?php else: ?>
<?php if($alert): ?>
<div class="alert error">
<p>Make sure you have entered all the information correctly.</div>
</div>
<?php endif ?>
<form enctype="multipart/form-data" id="contactform" method="post">
<div class="field<?php e(isset($alert['title']), ' error'); ?>">
<label for="title">Title <abbr title="required">*</abbr></label>
<input type="text" id="title" name="title" value="<?= isset($data['title']) ? $data['title'] : '' ?>">
</div>
<div class="field<?php e(isset($alert['text']), ' error'); ?>">
<label for="text">Message <abbr title="required">*</abbr></label>
<textarea id="text" name="text"><?= isset($data['text']) ? $data['text'] : '' ?></textarea>
</div>
<div class="field honeypot">
<label for="subject">Sujet <abbr title="required">*</abbr></label>
<input type="text" id="subject" name="subject">
</div>
<div class="field">
<label for="file">Choose a file</label>
<input type="file" name="file[]" id="file" class="inputfile hidden" data-multiple-caption="{count} files selected" multiple="multiple" accept="image/*"/>
</div>
<p class="help">All fields with * are required</p>
<div class="field">
<button type="submit" name="submit" value="Envoyer">Envoyer</button>
</div>
</form>
<?php endif ?>
</main>
<?php snippet('footer') ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment