Your basically looking for a wufoo or jotform type of functionality?
You could use something like https://github.com/IhabSoliman/Bootstrap-Form-Builder (for the UI side). I would store the formposts in mongodb probably (that way you don't have to create collumns and have your schema match the form thats created).
I've done somethign simple like so:
<?php namespace OCA\Webforms\Classes;
use Illuminate\Support\Facades\Input;
use OCA\Webforms\Models\Formposts;
use OCA\Webforms\Models\Ocaform as OcaformsModel;
class Ocaforms extends \Controller
{
private $rules = [
//'ocaform_id' => 'required',
];
private $ocaform;
private $post;
public function store($secret)
{
$this->ocaform = OcaformsModel::where('secret', '=', $secret)->first();
if(! $this->ocaform){
\Redirect::back();
//@todo display error
}
$data = Input::All();
// clean the data first
array_walk_recursive($data, array($this, 'cleanData'));
// allow multiple redirects. Do it this way so we don't specify the redirect URL in form html... specify rdata="1" or something
$redirects = explode(',',$this->ocaform->redirect_url);
if( count($redirects) == 1 ){
$redirecturl = trim($redirects[0]);
}
else if(!isset($data['rdata'])) {
$redirecturl = trim($redirects[0]);
}
else {
$redirecturl = trim($redirects[intval($data['rdata'])]);
}
// set rdata as redirecturl instead of number
$data['rdata'] = $redirecturl;
$data['ocaform_id'] = $this->ocaform->id;
$data['http_referer'] = \Request::header('referer');
$validator = \Validator::make($data, $this->rules);
if ($validator->fails())
{
return \Redirect::back()->withErrors($validator)->withInput(); //@todo this needs to show a page w/ errors on it... then redirect back.
}
// save post
$this->post = Formposts::create($data);
if(! $this->post){
//@todo display error
}
$this->sendEmails();
// tokenize the redirect url
$helpers = new Helpers();
$redirecturl = $helpers->tokenizeString($redirecturl, $this->post);
return \Redirect::away($redirecturl);
}
public function sendEmails()
{
$helpers = new Helpers();
$emails = $this->ocaform->emails()->get();
foreach($emails as $email){
$subject = $helpers->tokenizeString($email->subject, $this->post);
$body = $helpers->tokenizeString($email->emailbody, $this->post);
$toemail = explode("\r\n",$email->emails);
$data = compact('body','subject','toemail');
foreach ($toemail as $to){
\Queue::push('OCA\Webforms\Classes\SendEmails@send', array('data' => $data, 'subject' => $subject, 'to' => $to));
}
}
}
public function getDatatable($ocaform_id)
{
$helpers = new Helpers();
$header = $helpers->getLastpostHeader($ocaform_id);
return \Datatable::collection(Formposts::where('ocaform_id', intval($ocaform_id))->orderBy('created_at', 'desc')->get($header))
->showColumns($header)
->make();
}
private function cleanData(&$value)
{
$value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
}
my helpers file:
<?php namespace OCA\Webforms\Classes;
use Illuminate\Support\Facades\Input;
use OCA\Webforms\Models\Formposts;
use OCA\Webforms\Models\Ocaform as OcaformsModel;
class Helpers
{
public function getLastpostHeader($ocaform_id, $withValues = null)
{
$postdata = \DB::connection('mongodb')->collection('formposts')->where('ocaform_id', intval($ocaform_id))->orderBy('created_at', 'desc')->first();
$postdata = $this->getPostdata($postdata);
if($withValues){
return $postdata;
}
return array_keys($postdata);
}
public function getPostdata($formpost)
{
if(!$formpost){
return array();
}
// convert normal collection to array
if(!is_array($formpost) && $formpost != null){
$formpost = $formpost->getAttributes();
}
$formpost['_id'] = $formpost['_id']->{'$id'};
$formpost['updated_at'] = date('M-d-Y h:i:s', $formpost['updated_at']->sec);
$formpost['created_at'] = date('M-d-Y h:i:s', $formpost['created_at']->sec);
return $formpost;
}
public function getPostdataTokens($postData, $asDataTable = null)
{
$allformdata = '';
foreach($postData as $key => $post){
$tabletokens[] = array( 'token' => "%$key%", 'example' => $post);
$tokens['search'][] = "%$key%";
$tokens['replace'][] = $post;
$allformdata .= "<strong>$key</strong>: $post\n<br/>";
}
$tokens['search'][] = "%allFormData%";
$tokens['replace'][] = $allformdata;
$tabletokens[] = array( 'allFormData' => "%allFormData%", 'example' => $allformdata);
if ($asDataTable){
return $tabletokens;
}
return $tokens;
}
public function tokenizeString($string, $formpost)
{
$tokens = $this->getPostdataTokens($this->getPostdata($formpost));
return str_replace($tokens['search'], $tokens['replace'], $string);
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community