forked from xeoncross/micromvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added admin module and application migration back into project. Now s…
…upporting multiple configuration files.
- Loading branch information
Showing
21 changed files
with
1,382 additions
and
39 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ Thumbs.db | |
*~ | ||
*.cache | ||
*.log | ||
uploads/* | ||
Uploads | ||
config.php | ||
ignore | ||
backups/* | ||
Backups |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
<?php | ||
/** | ||
* Admin (Database Management) Class | ||
* | ||
* This class allows easy scaffolding of database records to help with admin | ||
* tasks such as approving, disabling, reviewing, searching, and deleting. | ||
* | ||
* @package MicroMVC | ||
* @author David Pennington | ||
* @copyright (c) 2011 MicroMVC Framework | ||
* @license http://micromvc.com/license | ||
********************************** 80 Columns ********************************* | ||
*/ | ||
abstract class Admin_Controller extends Controller | ||
{ | ||
public $template = 'layout'; | ||
|
||
/** | ||
* Administration Area | ||
* | ||
* @param array $config settings | ||
*/ | ||
protected function admin($config = array(), $page) | ||
{ | ||
// Setup defaults | ||
$config = $config + config(NULL, 'admin'); | ||
$config['admin_url'] = site_url($this->route); | ||
|
||
$model = $config['model']; | ||
$pp = $config['per_page']; | ||
|
||
// URI Structure: /forum/admin/2/topic/desc/title/YQ~~ | ||
$page = int($page, 1); | ||
$field = get('field', '', TRUE); | ||
$sort = get('sort') == 'asc' ? 'asc' : 'desc'; | ||
$column = get('column', '', TRUE); | ||
$term = get('term', '', TRUE); | ||
$where = array(); | ||
$order_by = array(); | ||
|
||
// Valid? | ||
if($column AND in_array($column, $config['columns'])) | ||
{ | ||
// Get the correct database column identifier | ||
$i = ORM::$db->i; | ||
|
||
$where = array($i.$column.$i.' LIKE '.ORM::$db->quote("%$term%")); | ||
} | ||
|
||
// Valid? | ||
if($field AND in_array($field, $config['columns'])) | ||
{ | ||
$order_by = array($field => $sort); | ||
} | ||
|
||
// Load rows | ||
$result = $model::fetch($where,$pp,(($page*$pp)-$pp),$order_by); | ||
$count = $model::count($where); | ||
|
||
//If not found | ||
if( ! $result) | ||
{ | ||
$this->content = new View('no_rows','admin'); | ||
$this->content->set($config); | ||
return; | ||
} | ||
|
||
//Make the results into an object | ||
$result = (object) $result; | ||
|
||
//Allow the controller to process these rows | ||
$this->pre_admin_display($result); | ||
|
||
//Allow hooks to change the $row data before showing it | ||
event('administration_' . $model::$table, $result); | ||
|
||
//We must reset the array pointer | ||
reset($result); | ||
|
||
//For each post - place it in a template | ||
$data = array( | ||
'rows' => $result, | ||
'columns' => $config['columns'], | ||
'config' => $config, | ||
'page' => $page, | ||
'field' => $field, | ||
'sort' => $sort, | ||
'column' => $column, | ||
'term' => $term, | ||
); | ||
|
||
$query_string = '?'. http_build_query(array( | ||
'field' => $field, | ||
'sort' => $sort, | ||
'column' => $column, | ||
'term' => $term | ||
)); | ||
|
||
// Pagination URI | ||
$url = $this->route. '/[[page]]'. $query_string; | ||
|
||
// Create the pagination | ||
$this->pagination = new Pagination($count, $url, $page, $pp); | ||
|
||
//Load the form view (and return it) | ||
$view = new view('admin','admin'); | ||
$view->set($data); | ||
|
||
// Create new session token | ||
Session::token(); | ||
|
||
$this->content = $view; | ||
} | ||
|
||
|
||
/** | ||
* Process the given row ID's | ||
* | ||
* @param array $ids the ids to alter | ||
* @param string $action the action name | ||
*/ | ||
protected function process($config = array(), $return_to = NULL) | ||
{ | ||
// Setup defaults | ||
$config = $config + config(NULL, 'admin'); | ||
|
||
// If there is no page to return to - then go back to admin | ||
if( ! $return_to) | ||
{ | ||
$return_to = $config['admin_url']; | ||
} | ||
|
||
// Validate | ||
if( ! Session::token(post('token')) OR ! post('ids') OR ! is_array(post('ids')) OR ! post('action') OR empty($config['actions'][post('action')])) | ||
{ | ||
redirect(base64_url_decode($return_to)); | ||
exit(); | ||
} | ||
|
||
$process = $config['actions'][post('action')]; | ||
$model = $config['model']; | ||
|
||
// Run each actions | ||
foreach(post('ids') as $id) | ||
{ | ||
$object = new $model($id); | ||
|
||
// Remove? | ||
if(isset($process['delete'])) | ||
{ | ||
$object->delete(); | ||
continue; | ||
} | ||
|
||
// We are approving/banning/activating/promoting/etc... | ||
if(isset($process['columns'])) | ||
{ | ||
foreach($process['columns'] as $column => $value) | ||
{ | ||
$object->$column = $value; | ||
} | ||
$object->save(); | ||
} | ||
} | ||
|
||
// And back we go! | ||
redirect(base64_url_decode($return_to)); | ||
exit(); | ||
} | ||
|
||
|
||
/** | ||
* This method can be extended by the controller to allowing additional | ||
* processing of the rows before display. | ||
* | ||
* @param object $results and object containing the result rows | ||
*/ | ||
protected function pre_admin_display($results) {} | ||
|
||
|
||
/** | ||
* Save user session before rendering the final layout template | ||
*/ | ||
public function render() | ||
{ | ||
Session::save(); | ||
|
||
headers_sent() OR header('Content-Type: text/html; charset = utf-8'); | ||
|
||
$layout = new View($this->template, 'admin'); | ||
$layout->set((array) $this); | ||
$layout->menu = $this->load_menu(); | ||
print $layout; | ||
|
||
$layout = NULL; | ||
|
||
if(config('debug_mode')) | ||
{ | ||
print new View('debug', 'system'); | ||
} | ||
} | ||
|
||
protected function load_menu() | ||
{ | ||
// Fetch all the module directories | ||
$modules = dir::contents(SP, FALSE, 'dir'); | ||
|
||
$menu = array(); | ||
// Build the admin menu from all modules | ||
foreach($modules as $module) | ||
{ | ||
// If this module has a config file | ||
if(is_file($module->getPathname(). '/config'. EXT)) | ||
{ | ||
// Load the config | ||
$config = config(NULL, $module->getBasename()); | ||
|
||
// If this module has any admin menus to add | ||
if(isset($config['admin_menu'])) | ||
{ | ||
$menu = array_merge($menu, $config['admin_menu']); | ||
} | ||
} | ||
} | ||
|
||
return $menu; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<div id="admin"> | ||
|
||
<script type="text/javascript"> | ||
//Select / Deselect all checkboxes | ||
function CheckAll(fmobj) { | ||
fmobj = document.getElementById(fmobj); | ||
for (var i=0;i<fmobj.elements.length;i++) { | ||
var e = fmobj.elements[i]; | ||
if ((e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled)) { | ||
e.checked = fmobj.allbox.checked; | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<div class="box"> | ||
|
||
<?php if( ! empty($config['create_url'])) { ?> | ||
<div class="create_new"> | ||
<a href="<?php print $config['create_url']; ?>" class="button green">Create New</a> | ||
</div> | ||
<?php } ?> | ||
|
||
<form method="get" id="admin_search" action="<?php print $config['admin_url']; ?>"> | ||
Search for <input name="term" value="<?php print h(get('term')); ?>" /> in | ||
<select name="column"> | ||
<?php | ||
foreach($columns as $value) | ||
{ | ||
print '<option value="'. $value. '"'. (get('column') == $value ? ' selected="selected"' : '').'>'. $value. '</option>'; | ||
} | ||
?> | ||
</select> | ||
<input type="submit" value="Go" /> | ||
</form> | ||
|
||
</div> | ||
|
||
<form id="admin_form" action="<?php print $config['process_url']. '/'. base64_url_encode(URL::path() . URL::query_string()); ?>" method="post"> | ||
<input type="hidden" name="token" value="<?php print session('token'); ?>" /> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th> | ||
<input name="allbox" type="checkbox" title="Check All" onclick="CheckAll('admin_form');" /> | ||
</th> | ||
<?php | ||
foreach($columns as $key => $value) | ||
{ | ||
if(!is_int($key)) $value = $key; | ||
|
||
$class = 'down'; | ||
$temp_sort = $sort; | ||
|
||
// Get the page number | ||
$url = $config['admin_url']. '/'. $page; | ||
|
||
//If this is the current column | ||
if($value == $field) | ||
{ | ||
// Reverse the sort | ||
$temp_sort = ($sort == 'desc' ? 'asc' : 'desc'); | ||
$class = ($sort == 'asc' ? 'down' : 'up'); | ||
} | ||
|
||
$query_string = '?'. http_build_query(array( | ||
'field' => $value, | ||
'sort' => $temp_sort, | ||
'column' => $column, | ||
'term' => $term | ||
)); | ||
|
||
$url = site_url($url. '/'. $query_string); //str_replace('&sort='. get('sort'), "&sort=$sort", $query_string)); | ||
|
||
print '<th><a href="'. $url . '" class="sort_by '. $class . '">'; | ||
print ucwords(str_replace('_', ' ', $value)). '</a></th>'; | ||
} | ||
?> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<?php | ||
if(!empty($rows)) | ||
{ | ||
|
||
//Print each of the result rows | ||
foreach($rows as $row) | ||
{ | ||
print '<tr>'; | ||
print '<td style="min-width:50px"><input type="checkbox" name="ids[]" value="'. $row->id. '"/>'; | ||
print '<a href="'. $config['update_url']. '/'. $row->id. '" class="edit">Edit</a></td>'; | ||
|
||
foreach($columns as $key => $value) print '<td>'. $row->$value. '</td>'; | ||
|
||
print '</tr>'; | ||
} | ||
} | ||
?> | ||
</tbody> | ||
|
||
<tfoot> | ||
<tr> | ||
<td colspan="<?php print count($columns) + 1; ?>"> | ||
<?php if( $config['actions']) { ?> | ||
<select name="action"> | ||
<option>with selected...</option> | ||
<?php foreach($config['actions'] as $name => $action) | ||
{ | ||
$option = $action ? $action['name'] : $name; | ||
print '<option value="'. $name. '">'. $option. '</option>'; | ||
} | ||
?> | ||
</select> | ||
|
||
<input type="submit" value="Apply" /> | ||
<?php } ?> | ||
</td> | ||
</tr> | ||
</tfoot> | ||
|
||
</table> | ||
</form> | ||
|
||
</div> |
Oops, something went wrong.