Skip to content

Instantly share code, notes, and snippets.

@kongondo

kongondo/from.php

Forked from somatonic/from.php
Created Jun 7, 2013
Embed
What would you like to do?
Create a page edit form on frontend using PW API and fields from the template of the page
<?php
// get a page
$editpage = $pages->get("/editme/");
$ignorefields = array("isOld","language_published");
$form = $modules->get("InputfieldForm");
$form->method = 'post';
$form->action = './';
// get the collection of inputs that can populate this page's fields
$inputfields = $editpage->getInputfields();
// make all the fields required and add them to the form
foreach($inputfields as $inputfield) {
if(in_array($inputfield->name, $ignorefields)) continue;
$form->add($inputfield);
}
// the inputfields don't already have a submit button, so we'll add one.
$submit = $modules->get("InputfieldSubmit");
$submit->name = "submit";
$submit->value = 'Submit';
// add the submit button the the form
$form->add($submit);
$out = '';
// process the form
if($this->input->post->submit) {
// now we assume the form has been submitted.
// tell the form to process input frmo the post vars.
$form->processInput($this->input->post);
// see if any errors occurred
if( count( $form->getErrors() )) {
// re-render the form, it will include the error messages
$out .= $form->render();
} else {
// successful form submission, so populate the page with the new values.
$editpage->setOutputFormatting(false);
foreach($form as $field) {
$editpage->set($field->name, $field->value);
}
// save it
$editpage->save();
$out .= "Page saved.";
$out .= $form->render();
}
} else {
$out .= $form->render();
}
include("head.inc"); // where scripts are added needed by the form
echo $out;
<script>
<?php
$jsConfig = $config->js();
$jsConfig['urls'] = array(
'root' => $config->urls->root,
'admin' => $config->urls->admin,
'modules' => $config->urls->modules,
'core' => $config->urls->core,
'files' => $config->urls->files,
'templates' => $config->urls->templates,
'adminTemplates' => $config->urls->adminTemplates,
'adminTemplates' => $config->urls->adminTemplates,
);
?>
var config = <?php echo json_encode($jsConfig); ?>;
</script>
<?
$config->styles->prepend($config->urls->adminTemplates . "styles/main.css?v=2");
$config->styles->append($config->urls->adminTemplates . "styles/inputfields.css");
$config->styles->append($config->urls->adminTemplates . "styles/ui.css?v=2");
$config->scripts->append($config->urls->JqueryUI . "JqueryUI.js");
$config->scripts->prepend($config->urls->JqueryCore . "JqueryCore.js");
$config->scripts->append($config->urls->adminTemplates . "scripts/inputfields.js");
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";
foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.