Skip to content
Browse files

mongodb, universal admin settings

  • Loading branch information...
1 parent 1f2c193 commit fc23e964e3e3c29665b5c0295369112d2663afe0 Malte Buttjer committed
Showing with 283 additions and 27 deletions.
  1. +8 −0 assets/admin.css
  2. +182 −0 assets/admin.js
  3. +12 −0 config.php
  4. +22 −8 controller/admin.php
  5. +4 −5 controller/login.php
  6. +1 −0 index.php
  7. +4 −11 lib.php
  8. +3 −0 views/admin.php
  9. +0 −1 views/contenttypes.php
  10. +47 −2 views/settings.php
View
8 assets/admin.css
@@ -0,0 +1,8 @@
+form nav {
+ display: inline-block;
+}
+form nav a {
+ margin: 10px;
+ font-family: sans-serif;
+ font-size: 11px;
+}
View
182 assets/admin.js
@@ -0,0 +1,182 @@
+window.onload = function() {
+ var els = document.getElementsByClassName('addfield');
+ for (var i = 0; i < els.length ; i++ ){
+ els[i].addEventListener('click', addfield, false);
+ }
+ var els = document.getElementsByClassName('addfieldset');
+ for (var i = 0; i < els.length ; i++ ){
+ els[i].addEventListener('click', addfieldset, false);
+ }
+ var els = document.getElementsByClassName('remove');
+ for (var i = 0; i < els.length ; i++ ){
+ els[i].addEventListener('click', removefield, false);
+ }
+
+ var els = document.getElementsByClassName('adddocument');
+ for (var i = 0; i < els.length ; i++ ){
+ els[i].addEventListener('click', adddocument, false);
+ }
+}
+
+function removefield(ev) {
+ event.preventDefault();
+
+ ev.target.parentNode.parentNode.parentNode.removeChild(ev.target.parentNode.parentNode);
+}
+
+function addfield(ev) {
+ event.preventDefault();
+
+ var name = prompt('Create new field', 'fieldname');
+ if (name != '' && name != null) {
+
+ var key = ev.target.getAttribute('data-parentkey') + '[' + name + ']';
+
+ var legend = document.createElement('legend');
+ legend.innerHTML = name;
+
+ var remove = document.createElement('a');
+ remove.setAttribute('href','#');
+ remove.setAttribute('class','remove');
+ remove.innerHTML = "remove";
+ remove.addEventListener('click', removefield, false);
+
+ var nav = document.createElement('nav');
+ nav.appendChild(remove);
+
+ var input = document.createElement('input');
+ input.setAttribute('type','text');
+ input.setAttribute('value','');
+ input.setAttribute('name',key);
+ input.setAttribute('id',key);
+
+ var fieldset = document.createElement('fieldset');
+ fieldset.appendChild(legend);
+ fieldset.appendChild(input);
+ fieldset.appendChild(nav);
+ ev.target.parentNode.parentNode.insertBefore(fieldset, ev.target.parentNode.nextSibling);
+
+ }
+}
+
+function addfieldset(ev) {
+
+ event.preventDefault();
+
+ var name = prompt('Create new fieldset', 'fieldsetname');
+ if (name != '' && name != null) {
+
+ var key = ev.target.getAttribute('data-parentkey') + '[' + name + ']';
+
+ var legend = document.createElement('legend');
+ legend.innerHTML = name;
+
+ var add = document.createElement('a');
+ add.setAttribute('href','#');
+ add.setAttribute('class','addfield');
+ add.setAttribute('data-parentkey',key);
+ add.innerHTML = "addfield";
+ add.addEventListener('click', addfield, false);
+
+ var addset = document.createElement('a');
+ addset.setAttribute('href','#');
+ addset.setAttribute('class','addfieldset');
+ addset.setAttribute('data-parentkey',key);
+ addset.innerHTML = "addfieldset";
+ addset.addEventListener('click', addfieldset, false);
+
+ var remove = document.createElement('a');
+ remove.setAttribute('href','#');
+ remove.setAttribute('class','remove');
+ remove.innerHTML = "remove";
+ remove.addEventListener('click', removefield, false);
+
+ var nav = document.createElement('nav');
+ nav.appendChild(add);
+ nav.appendChild(addset);
+ nav.appendChild(remove);
+
+ var fieldset = document.createElement('fieldset');
+ fieldset.appendChild(legend);
+ fieldset.appendChild(nav);
+
+ ev.target.parentNode.parentNode.insertBefore(fieldset, ev.target.parentNode.nextSibling);
+ }
+}
+
+function adddocument(ev) {
+
+ event.preventDefault();
+
+ var name = prompt('Create new document', 'name');
+ if (name != '' && name != null) {
+
+ var key = ev.target.getAttribute('data-parentkey') + '[' + name + ']';
+
+ var legend = document.createElement('legend');
+ legend.innerHTML = name;
+
+ var add = document.createElement('a');
+ add.setAttribute('href','#');
+ add.setAttribute('class','addfield');
+ add.setAttribute('data-parentkey',key);
+ add.innerHTML = "addfield";
+ add.addEventListener('click', addfield, false);
+
+ var add = document.createElement('a');
+ add.setAttribute('href','#');
+ add.setAttribute('class','addfield');
+ add.setAttribute('data-parentkey',key);
+ add.innerHTML = "addfield";
+ add.addEventListener('click', addfield, false);
+
+ var addset = document.createElement('a');
+ addset.setAttribute('href','#');
+ addset.setAttribute('class','addfieldset');
+ addset.setAttribute('data-parentkey',key);
+ addset.innerHTML = "addfieldset";
+ addset.addEventListener('click', addfieldset, false);
+
+ var remove = document.createElement('a');
+ remove.setAttribute('href','#');
+ remove.setAttribute('class','remove');
+ remove.innerHTML = "remove";
+ remove.addEventListener('click', removefield, false);
+
+ var nav = document.createElement('nav');
+ nav.appendChild(add);
+ nav.appendChild(addset);
+ nav.appendChild(remove);
+
+ var namelegend = document.createElement('legend');
+ namelegend.innerHTML = 'name';
+
+ var nameinput = document.createElement('input');
+ nameinput.setAttribute('type','text');
+ nameinput.setAttribute('value',name);
+ nameinput.setAttribute('name',key + '[name]');
+ nameinput.setAttribute('id',key + '[name]');
+
+ var nameremove = document.createElement('a');
+ nameremove.setAttribute('href','#');
+ nameremove.setAttribute('class','remove');
+ nameremove.innerHTML = "remove";
+ nameremove.addEventListener('click', removefield, false);
+
+ var namenav = document.createElement('nav');
+ namenav.appendChild(nameremove);
+
+ var namefieldset = document.createElement('fieldset');
+ namefieldset.appendChild(namelegend);
+ namefieldset.appendChild(nameinput);
+ namefieldset.appendChild(namenav);
+
+ var fieldset = document.createElement('fieldset');
+ fieldset.appendChild(legend);
+ fieldset.appendChild(nav);
+ fieldset.appendChild(namefieldset);
+
+ ev.target.parentNode.parentNode.insertBefore(fieldset, ev.target.parentNode.nextSibling);
+
+ }
+}
View
12 config.php
@@ -0,0 +1,12 @@
+<?php
+
+function db() {
+ try {
+ return new Mongo();
+ }
+ catch(MongoConnectionException $e) {
+ die('Could not connect. Check to make sure MongoDB is running.');
+ }
+}
+
+$key = "213124%*4!#$;\.k~'(_@%*4!#$;\.k~'(_@%*4!#$;\.k~'(_@";
View
30 controller/admin.php
@@ -12,6 +12,7 @@ function load() {
}
function index() {
+
$data = array(
"title" => array(
"ADMIN"
@@ -24,8 +25,6 @@ function index() {
)
);
- $db = $this->db();
-
$this->render($data, 'admin');
}
@@ -51,13 +50,30 @@ function settings() {
"Settings" => "admin/settings",
"Content" => "admin/content",
"Logout" => "login/destination/admin"
- ),
- "content" => "bawm"
+ )
);
- $db = $this->db();
+ $db = db();
+ $col = $db->bb->settings;
+
- $this->render($data, 'admin', 'settings', 'contenttypes');
+ if ($_POST) {
+ $col->drop();
+ if (isset($_POST['settings']) && is_array($_POST['settings'])) {
+ foreach ($_POST['settings'] as $id => $set) {
+ $id = new MongoId($id);
+ $col->insert($set, true);
+ }
+ }
+ }
+
+ $res = $col->find();
+
+ foreach ($res as $key => $doc) {
+ $data['settings'][$key] = $doc;
+ }
+
+ $this->render($data, 'admin', 'settings');
}
@@ -76,8 +92,6 @@ function content() {
)
);
- $db = $this->db();
-
$this->render($data, 'admin');
}
View
9 controller/login.php
@@ -7,13 +7,12 @@ function session() {
unset($_SESSION['user']);
$data = array();
- if($_POST) {
- $username = $_POST['username'];
+ if ($_POST) {
+ $username = $_POST['username'];
$password = $_POST['password'];
- $key = "213124%*4!#$;\.k~'(_@%*4!#$;\.k~'(_@%*4!#$;\.k~'(_@";
- if($username == "admin" && crypt($password, $key) == "21OZ4/WxREgV.") {
+ if ($username == "admin" && crypt($password, $key) == "21OZ4/WxREgV.") {
$_SESSION['user'] = 1;
- }
+ }
else {
$data["error"] = "username or password incorrect";
}
View
1 index.php
@@ -1,5 +1,6 @@
<?php
+include('config.php');
include('lib.php');
$app = new Lib();
View
15 lib.php
@@ -116,9 +116,10 @@ function show_403() {
function render() {
$args = func_get_args();
- if(count($args) <= 1) {
+ if (count($args) <= 1) {
$files[] = $args[0];
- } else {
+ }
+ else {
$data = $args[0];
extract($data);
unset($data);
@@ -126,7 +127,7 @@ function render() {
$files = $args;
}
- foreach($files as $file) {
+ foreach ($files as $file) {
$view[] = realpath($this->options['root'] . '/views/' . $file . '.php');
}
@@ -140,14 +141,6 @@ function location($path = '') {
return true;
}
- function db() {
- try {
- return new Mongo();
- }
- catch(MongoConnectionException $e) {
- die('Could not connect. Check to make sure MongoDB is running.');
- }
- }
}
class Ind extends Contr {
View
3 views/admin.php
@@ -4,6 +4,9 @@
<meta charset="utf-8">
<title>Page Title</title>
</head>
+ <script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
+ <script type="text/javascript" charset="utf-8" src="../../assets/admin.js"></script>
+ <link type="text/css" rel="stylesheet" media="screen" href="../../assets/admin.css" />
<body>
<header>
View
1 views/contenttypes.php
@@ -1 +0,0 @@
-<p>Contenttypes</p>
View
49 views/settings.php
@@ -1,2 +1,47 @@
-<p>settings n stuff</p>
-<?php if($next = next($view)) include($next) ?>
+<?php
+function formgen($items, $parentkey) {
+ $output = '';
+ foreach ($items as $key => $item):
+ if (is_array($item)):
+ $output .= '<fieldset>';
+ $output .= '<legend>';
+ if (isset($item['name'])) {
+ $output .= $item['name'];
+ }
+ else {
+ $output .= $key;
+ }
+ $output .= '</legend>';
+ $output .= '<nav>';
+ $output .= '<a class="addfield" href="#" data-parentkey="' . $parentkey . '[' . $key . ']' . '">add field</a>';
+ $output .= '<a class="addfieldset" href="#" data-parentkey="' . $parentkey . '[' . $key . ']' . '">add fieldset</a>';
+ $output .= '<a class="remove" href="#">remove</a>';
+ $output .= '</nav>';
+ $output .= formgen($item, $parentkey . '[' . $key . ']');
+ $output .= '</fieldset>';
+ else:
+ $output .= '<fieldset>';
+ $output .= '<legend>';
+ $output .= $key;
+ $output .= '</legend>';
+ if ($key == '_id') {
+ $output .= '<input name="' . $parentkey . '[' . $key . ']' . '" id="' . $parentkey . '[' . $key . ']' . '" value="' . $item . '" readonly="readonly" />';
+ } else {
+ $output .= '<input type="text" name="' . $parentkey . '[' . $key . ']' . '" id="' . $parentkey . '[' . $key . ']' . '" value="' . $item . '" />';
+ $output .= '<nav><a class="remove" href="#">remove</a></nav>';
+ }
+ $output .= '</fieldset>';
+ endif;
+ endforeach;
+ return $output;
+}
+?>
+
+<form name="settings" method="post" action="">
+ <input type="submit" name="Submit" value="Submit" />
+ <nav>
+ <a class="adddocument" href="#" data-parentkey="settings">add document</a>
+ </nav>
+ <?php print (isset($settings)) ? formgen($settings, 'settings') : ''; ?>
+ <input type="submit" name="Submit" value="Submit" />
+</form>

0 comments on commit fc23e96

Please sign in to comment.
Something went wrong with that request. Please try again.