Created
November 9, 2011 03:38
-
-
Save walterdavis/1350264 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
function humanize($string){ | |
return htmlspecialchars(ucwords(preg_replace('/_+/',' ',$string))); | |
} | |
function logout(){ | |
$_SESSION = array(); | |
session_destroy(); | |
header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']); | |
} | |
define('MYACTIVERECORD_CONNECTION_STR', 'mysql://test:pass@localhost/test'); | |
require_once('lib/MyActiveRecord.php'); | |
class widgets extends MyActiveRecord{ | |
}; | |
class people extends MyActiveRecord{ | |
function find_or_create_by_name($name){ | |
if($person = MyActiveRecord::FindFirst('people', 'name = "' . $name . '"')){ | |
//yay | |
}else{ | |
$person = MyActiveRecord::Create('people'); | |
$person->name = $name; | |
$person->save(); | |
} | |
return $person; | |
} | |
}; | |
$person = MyActiveRecord::Create('people'); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>Sortable List Demo</title> | |
<link rel="stylesheet" type="text/css" media="screen" href="css/style.css" /> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/scriptaculous/1.9/scriptaculous.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
document.observe('dom:loaded', function(){ | |
Sortable.create('sort_me',{ | |
tag:'div', | |
onChange:function(elm){ | |
new Ajax.Request('save_sort_order.php',{ | |
parameters:Sortable.serialize('sort_me'), | |
onSuccess:function(){ | |
$('sort_me').highlight({restorecolor:'#ffffff'}); | |
} | |
}); | |
}, | |
constraint:false | |
}); | |
}); | |
</script> | |
<!--[if IE]> | |
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div id="person"> | |
<?php | |
if(isset($_SESSION['name'])){ | |
if(isset($_GET['logout'])){ | |
logout(); | |
}elseif ($person = MyActiveRecord::FindFirst('people', 'name = "' . $_SESSION['name'] . '"')){ | |
print ' <p><a class="right" href="?logout=true">Log Out</a>Welcome, ' . $person->name . '</p>'; | |
if (!empty($person->layout)){ | |
$widgets = array(); | |
foreach(explode(',',$person->layout) as $id) | |
$widgets[] = MyActiveRecord::FindById('widgets',$id); | |
}else{ | |
print ' <p>Drag the widgets to your desired order.</p>'; | |
} | |
}else{ | |
logout(); | |
} | |
}elseif(isset($_POST['name']) && !empty($_POST['name'])){ | |
$_SESSION['name'] = $_POST['name']; | |
$person->find_or_create_by_name($_POST['name']); | |
header('Location: http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']); | |
}else{ | |
?> | |
<form action="index.php" method="post" accept-charset="utf-8"> | |
<p><label for="name">Enter your name</label><input name="name" id="name" value=""/></p> | |
<p><input type="submit" value="Sign In"/></p> | |
</form> | |
<?php | |
} | |
if(!isset($widgets)) $widgets = MyActiveRecord::FindAll('widgets'); | |
?> | |
</div> | |
<div id="sort_me"> | |
<?php | |
$template = ' <div id="widget_%s" class="widget"> | |
<h1>%s</h1> | |
</div> | |
'; | |
foreach($widgets as $widget){ | |
printf($template, $widget->id, humanize($widget->name)); | |
} | |
?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment