More initial work
This commit is contained in:
187
application/classes/Controller/Admin/Module.php
Normal file
187
application/classes/Controller/Admin/Module.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides MODULE management
|
||||
*
|
||||
* @package Membership Database
|
||||
* @category Controllers/Admin
|
||||
* @author Deon George
|
||||
* @copyright (c) 2014 Deon George
|
||||
* @license http://dev.leenooks.net/license.html
|
||||
*/
|
||||
class Controller_Admin_Module extends Controller_Module {
|
||||
protected $secure_actions = array(
|
||||
'add'=>TRUE,
|
||||
'edit'=>TRUE,
|
||||
'list'=>TRUE,
|
||||
);
|
||||
|
||||
protected function _classes($dir,$class,$array=NULL,$key='') {
|
||||
$result = array();
|
||||
|
||||
if (is_null($array)) {
|
||||
$key = 'classes/Controller';
|
||||
$array = Arr::get(Kohana::list_files('classes'),$key);
|
||||
}
|
||||
|
||||
if (! $class)
|
||||
return array_keys($array);
|
||||
|
||||
if (! $dir) {
|
||||
if (! empty($array[$key.'/'.$class]))
|
||||
$result = Arr::merge($result,$this->_classes('','',$array[$key.'/'.$class],$key.'/'.$class));
|
||||
|
||||
if (! empty($array[$key.'/'.$class.'.php']))
|
||||
array_push($result,$key.'/'.$class);
|
||||
|
||||
} else {
|
||||
if (! empty($array[$key.'/'.$dir]))
|
||||
$result = Arr::merge($result,$this->_classes('',$class,$array[$key.'/'.$dir],$key.'/'.$dir));
|
||||
|
||||
if (! empty($array[$key.'/'.$dir.'/'.$class.'.php']))
|
||||
array_push($result,$key.'/'.$dir.'/'.$class);
|
||||
}
|
||||
|
||||
foreach ($result as $k=>$v)
|
||||
$result[$k] = str_replace('.php','',str_replace('/','_',preg_replace('/^classes\//','',$v)));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of methods for a class
|
||||
*/
|
||||
protected function _methods($class) {
|
||||
$class = Kohana::classname($class);
|
||||
// Get a list of methods this module has
|
||||
$methods = $secure_actions = $auth_required = array();
|
||||
|
||||
// List of classes where all our methods are, including this one.
|
||||
$classes = URL::$method_directory;
|
||||
array_unshift($classes,'');
|
||||
|
||||
foreach ($classes as $c) {
|
||||
$x = URL::dir($c);
|
||||
$cp = $this->_classes($x,$class);
|
||||
|
||||
foreach ($cp as $cn)
|
||||
if (class_exists($cn)) {
|
||||
$sc = preg_replace(sprintf('/^Controller_%s%s_?/',$x ? $x.'_' : '',$class),'',$cn);
|
||||
$r = new ReflectionClass($cn);
|
||||
|
||||
$rdp = $r->getDefaultProperties();
|
||||
$secure_actions[$cn] = $rdp['secure_actions'];
|
||||
$auth_required[$cn] = $rdp['auth_required'];
|
||||
|
||||
foreach ($r->getMethods() as $method)
|
||||
if ($method->class == $cn AND preg_match('/^action_/',$method->name))
|
||||
array_push($methods,str_replace('action_',strtolower($x.($sc ? '_'.$sc : '').':'),$method->name));
|
||||
}
|
||||
}
|
||||
|
||||
return array('methods'=>$methods,'secure_actions'=>$secure_actions,'auth_required'=>$auth_required);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a Module Configuration
|
||||
*/
|
||||
public function action_edit() {
|
||||
$id = $this->request->param('id');
|
||||
$mo = ORM::factory('Module',$id);
|
||||
|
||||
$methods = array();
|
||||
|
||||
if (! $mo->loaded()) {
|
||||
SystemMessage::factory()
|
||||
->title(_('Invalid Module ID'))
|
||||
->type('error')
|
||||
->body(sprintf(_('Module with ID %s doesnt appear to exist?'),$id));
|
||||
|
||||
HTTP::redirect(URL::link('admin','module/list'));
|
||||
}
|
||||
|
||||
$mm = $this->_methods($mo->name);
|
||||
$methods['exist'] = array();
|
||||
foreach ($mo->module_method->find_all() as $mmo) {
|
||||
if (in_array($mmo->name,$mm['methods'])) {
|
||||
$k = array_search($mmo->name,$mm['methods']);
|
||||
unset($mm['methods'][$k]);
|
||||
|
||||
$mmo->status('INDB');
|
||||
} else
|
||||
$mmo->status('ORPHAN');
|
||||
|
||||
if (! empty($mm['secure_actions'][$mmo->controller()][$mmo->method()]))
|
||||
unset($mm['secure_actions'][$mmo->controller()][$mmo->method()]);
|
||||
|
||||
array_push($methods['exist'],$mmo);
|
||||
}
|
||||
|
||||
$methods['missing'] = array();
|
||||
foreach ($mm['methods'] as $k=>$method) {
|
||||
$mmo = ORM::factory('Module_Method');
|
||||
$mmo->module_id = $mo->id;
|
||||
$mmo->name = $method;
|
||||
|
||||
if (! empty($mm['auth_required'][$mmo->controller()]) AND $mm['auth_required'][$mmo->controller()])
|
||||
$mmo->status('MISSING');
|
||||
|
||||
array_push($methods['missing'],$mmo);
|
||||
}
|
||||
|
||||
Block::factory()
|
||||
->title(sprintf('%s: %s ',_('Defined Module Methods For'),$mo->display('name')))
|
||||
->title_icon('icon-cog')
|
||||
->body(Table::factory()
|
||||
->data($methods['exist'])
|
||||
->columns(array(
|
||||
'id'=>'ID',
|
||||
'name'=>'Name',
|
||||
'notes'=>'Notes',
|
||||
'menu_display'=>'Menu',
|
||||
'status()'=>'Status',
|
||||
))
|
||||
->prepend(array(
|
||||
'id'=>array('url'=>URL::link('admin','module_method/edit/')),
|
||||
))
|
||||
);
|
||||
|
||||
Block::factory()
|
||||
->title(sprintf('%s: %s ',_('Missing Module Methods For'),$mo->display('name')))
|
||||
->title_icon('icon-exclamation-sign')
|
||||
->body(Table::factory()
|
||||
->data($methods['missing'])
|
||||
->columns(array(
|
||||
'name'=>'Name',
|
||||
'status()'=>'Status',
|
||||
))
|
||||
->prepend(array(
|
||||
'name'=>array('url'=>URL::link('admin','module_method/add/'.$mo->id.'/')),
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* List our installed modules
|
||||
*/
|
||||
public function action_list() {
|
||||
Block::factory()
|
||||
->title('Defined Modules')
|
||||
->title_icon('icon-cog')
|
||||
->body(Table::factory()
|
||||
->data(ORM::factory('Module')->where('parent_id','is',NULL)->find_all())
|
||||
->jssort(TRUE)
|
||||
->columns(array(
|
||||
'id'=>'ID',
|
||||
'name'=>'Name',
|
||||
'notes'=>'Notes',
|
||||
'status'=>'Active',
|
||||
'external'=>'External',
|
||||
))
|
||||
->prepend(array(
|
||||
'id'=>array('url'=>URL::link('admin','module/edit/')),
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
109
application/classes/Controller/Admin/Module/Method.php
Normal file
109
application/classes/Controller/Admin/Module/Method.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides MODULE management
|
||||
*
|
||||
* @package Membership Database
|
||||
* @category Controllers/Admin
|
||||
* @author Deon George
|
||||
* @copyright (c) 2014 Deon George
|
||||
* @license http://dev.leenooks.net/license.html
|
||||
*/
|
||||
class Controller_Admin_Module_Method extends Controller_Admin_Module {
|
||||
/**
|
||||
* Add a method to the database
|
||||
*/
|
||||
public function action_add() {
|
||||
$id = $this->request->param('id');
|
||||
$method = $this->request->param('sid');
|
||||
|
||||
$mo = ORM::factory('Module',$id);
|
||||
$mm = $this->_methods($mo->name);
|
||||
|
||||
if (! $mo->loaded() OR ! in_array($method,$mm['methods']))
|
||||
HTTP::redirect(URL::link('admin','module/list'));
|
||||
|
||||
if ($_POST) {
|
||||
$mmo = $mo->module_method;
|
||||
$mmo->name = $method;
|
||||
$mmo->module_id = $mo->id;
|
||||
$mmo->values($_POST);
|
||||
|
||||
if (! $this->save($mmo))
|
||||
throw HTTP_Exception::factory(501,'Unable to save data :post',array(':post'=>serialize($_POST)));
|
||||
|
||||
HTTP::redirect(URL::link('admin','module/edit/'.$mo->id));
|
||||
}
|
||||
|
||||
Block::factory()
|
||||
->title(sprintf(_('Add Method (%s) to Database for (%s)'),strtoupper($method),strtoupper($mo->name)))
|
||||
->title_icon('icon-plus-sign')
|
||||
->type('form-horizontal')
|
||||
->body(View::factory('module/method/admin/add')
|
||||
->set('name',$method)
|
||||
->set('o',$mo)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a Module Configuration
|
||||
*/
|
||||
public function action_edit() {
|
||||
$id = $this->request->param('id');
|
||||
$mmo = ORM::factory('Module_Method',$id);
|
||||
|
||||
if (! $mmo->loaded()) {
|
||||
SystemMessage::factory()
|
||||
->title(_('Invalid Method ID'))
|
||||
->type('error')
|
||||
->body(sprintf(_('Method with ID %s doesnt appear to exist?'),$id));
|
||||
|
||||
HTTP::redirect(URL::link('admin','module/list'));
|
||||
}
|
||||
|
||||
if ($_POST) {
|
||||
$mmo->values($_POST);
|
||||
|
||||
if (! $this->save($mmo))
|
||||
throw HTTP_Exception::factory(501,'Unable to save data :post',array(':post'=>serialize($_POST)));
|
||||
|
||||
foreach (ORM::factory('Group')->find_all() as $go) {
|
||||
// If the group was defined and no longer
|
||||
if ($mmo->has('group',$go) AND (! isset($_POST['groups']) OR ! in_array($go->id,$_POST['groups']))) {
|
||||
$gmo = ORM::factory('Group_Method',array('method_id'=>$mmo->id,'group_id'=>$go->id));
|
||||
|
||||
if (! $gmo->delete())
|
||||
SystemMessage::factory()
|
||||
->title(_('Unable to DELETE Group Method'))
|
||||
->type('error')
|
||||
->body(sprintf(_('Unable to delete Group Method for method %s and group %s'),$mmo->name,$go->name));
|
||||
|
||||
// If the group was not defined and now is
|
||||
} elseif (! $mmo->has('group',$go) AND isset($_POST['groups']) AND in_array($go->id,$_POST['groups'])) {
|
||||
$gmo = ORM::factory('Group_Method')
|
||||
->values(array(
|
||||
'method_id'=>$mmo->id,
|
||||
'group_id'=>$go->id,
|
||||
));
|
||||
|
||||
if (! $this->save($gmo))
|
||||
SystemMessage::factory()
|
||||
->title(_('Unable to SAVE Group Method'))
|
||||
->type('error')
|
||||
->body(sprintf(_('Unable to save Group Method for method %s and group %s'),$mmo->name,$go->name));
|
||||
}
|
||||
}
|
||||
|
||||
HTTP::redirect(URL::link('admin','module/edit/'.$mmo->module_id));
|
||||
}
|
||||
|
||||
Block::factory()
|
||||
->title(sprintf(_('Configure access to method (%s::%s)'),$mmo->controller(),$mmo->method()))
|
||||
->title_icon('icon-plus-sign')
|
||||
->type('form')
|
||||
->body(View::factory('module/method/admin/edit')
|
||||
->set('o',$mmo)
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user