Skip to content

Commit

Permalink
sample data, selecting list item sets the form
Browse files Browse the repository at this point in the history
  • Loading branch information
DubFriend committed Mar 8, 2014
1 parent 0f23678 commit a9efa1e
Show file tree
Hide file tree
Showing 15 changed files with 787 additions and 19 deletions.
4 changes: 2 additions & 2 deletions crud.html
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
</li>
<li>
<span>Checkbox</span>
<span data-field="checkbox"></span>
<span data-field="checkbox[]"></span>
</li>
<li>
<span>Hidden</span>
Expand Down Expand Up @@ -274,7 +274,7 @@
}
});
// nameForminator.list.set([{text: 'foo'}, {radio: 'basdf'}]);
nameForminator.form.set('checkbox[]', ['a', 'b']);
// nameForminator.form.set('checkbox[]', ['a', 'b']);
console.log(nameForminator.form.get());
</script>
</body>
Expand Down
93 changes: 92 additions & 1 deletion crud.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,96 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_STRICT|E_ALL);

require 'sequel.php';

define('RESULTS_PER_PAGE', 3);

?>
$sql = new Sequel(new PDO(
'mysql:dbname=forminator_demo;host=localhost',
'root',
'P0l.ar-B3ar'
));

function getWhereStartsWith(array $collection, $startOfKey) {
$filtered = array();
foreach($collection as $key => $value) {
if(preg_match('/^' . $startOfKey . '/', $key)) {
$filtered[$key] = $value;
}
}
return $filtered;
}

function stripLeadingKey(array $collection, $startOfKey) {
$stripped = array();
foreach($collection as $key => $value) {
$strippedKey = '';
preg_replace('/^' . $startOfKey . '/', $strippedKey, $key);
$stripped[$strippedKey] = $value;
}
return $stripped;
}

function getOrders() {
return stripLeadingKey(getWhereStartsWith($_GET, 'order_'), 'order_');
}

function getFilters() {
return stripLeadingKey(getWhereStartsWith($_GET, 'filter_'), 'filter_');
}

$response = null;
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
$where = implode(' AND ', array_map(function ($key) {
return $key . ' = ?';
}, array_keys(getFilters())));

$orderArray = array();
foreach(getOrders() as $key => $value) {
$orderArray[] = $key . ' ' . $value;
}
$order = implode(', ', $orderArray);

$pageNumber = (isset($_GET['page']) ? $_GET['page'] : 1) - 1;

$results = array_map(
function ($row) {
$row['checkbox'] = explode(',', $row['checkbox']);
$row['checkbox2'] = explode(',', $row['checkbox2']);
return $row;
},
$sql->query(
'SELECT * FROM forminator ' .
($where ? ' WHERE ' . $where : '') .
($order ? ' ORDER BY ' . $order : '') .
' LIMIT ' . (RESULTS_PER_PAGE * $pageNumber) . ', ' . RESULTS_PER_PAGE
)->toArray()
);

$response = array('results' => $results, 'status' => 200);
break;
case 'POST':
switch(strtolower($_GET['action'])) {
case 'create':
$id = $sql->insert('forminator', $_POST);
$response = array('id' => $id, 'status' => 200);
break;
case 'update':
$sql->update('forminator', $_POST);
$response = array('status' => 200);
break;
case 'delete':
$sql->delete('forminator', $_GET['id']);
$response = array('status' => 200);
break;
default:
throw new Exception('invalid action : ' . $_GET['action']);
}
break;
default:
throw new Exception('invalid request method: ' . $_SERVER['REQUEST_METHOD']);
}
echo json_encode($response);
?>
2 changes: 1 addition & 1 deletion crud.pre.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
}
});
// nameForminator.list.set([{text: 'foo'}, {radio: 'basdf'}]);
nameForminator.form.set('checkbox[]', ['a', 'b']);
// nameForminator.form.set('checkbox[]', ['a', 'b']);
console.log(nameForminator.form.get());
</script>
</body>
Expand Down
21 changes: 19 additions & 2 deletions forminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,12 @@ var createInputRadio = function (fig) {
};

self.set = my.buildSetter(function (newValue) {
self.$().filter('[value="' + newValue + '"]').prop('checked', true);
if(!newValue) {
self.$().prop('checked', false);
}
else {
self.$().filter('[value="' + newValue + '"]').prop('checked', true);
}
});

self.$().change(function () {
Expand Down Expand Up @@ -1721,7 +1726,19 @@ var forminator = {};
forminator.init = function (fig) {
var factory = createFactory(fig),
form = factory.form(),
list = factory.list();
list = factory.list(),
fieldMap = fig.fieldMap || {};


if(list && form) {
list.subscribe('selected', function (listItem) {
form.set(map(listItem.get(), function (value, fieldName) {
console.log(value, fieldName);
return callIfFunction(fieldMap[fieldName], value) || value;
}));
});
}


return {
form: form,
Expand Down
2 changes: 1 addition & 1 deletion forminator.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion html/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</li>
<li>
<span>Checkbox</span>
<span data-field="checkbox"></span>
<span data-field="checkbox[]"></span>
</li>
<li>
<span>Hidden</span>
Expand Down
Empty file added install.sql
Empty file.
54 changes: 54 additions & 0 deletions sample_data/generate_data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_STRICT|E_ALL);

define('NUMBER_OF_ROWS_TO_GENERATE', 5000);

require '../sequel.php';

function getRandomFromArray(array $array) {
return $array[rand(0, count($array) - 1)];
}

function getRandomFromArrayMultiple(array $array) {
$returnArray = array();
foreach($array as $value) {
if(rand(0, 1)) {
$returnArray[] = $value;
}
}
return implode(',', $returnArray);
};

$names = explode("\n", file_get_contents('names.txt'));
function getRandomName($names, $numberOfNames = null) {
$numberOfNames = $numberOfNames > 0 ? $numberOfNames : 1;
$namesSample = array();
for($i = 0; $i < $numberOfNames; $i += 1) {
$namesSample[] = getRandomFromArray($names);
}
return implode(', ', $namesSample);
}

$sql = new Sequel(new PDO(
'mysql:dbname=forminator_demo;host=localhost',
'root',
'P0l.ar-B3ar'
));

for($i = 0; $i < NUMBER_OF_ROWS_TO_GENERATE; $i += 1) {
$sql->insert('forminator', array(
'text' => getRandomName($names),
'text2' => getRandomName($names, rand(0, 5)),
'textarea' => getRandomName($names, rand(0, 5)),
'textarea2' => getRandomName($names, rand(0, 5)),
'`select`' => getRandomFromArray(array('a', 'b')),
'select2' => getRandomFromArray(array('a', 'b')),
'radio' => getRandomFromArray(array('a', 'b')),
'radio2' => getRandomFromArray(array('a', 'b')),
'checkbox' => getRandomFromArrayMultiple(array('a', 'b')),
'checkbox2' => getRandomFromArrayMultiple(array('a', 'b'))
));
}
echo 'done';
?>
Loading

0 comments on commit a9efa1e

Please sign in to comment.