moved more assignment related stuff to the Assignment class

This commit is contained in:
Andreas Gohr
2016-02-09 10:07:03 +01:00
parent fb31ca9fa3
commit 1a8d1235c6
2 changed files with 49 additions and 14 deletions

View File

@ -8,6 +8,7 @@
// must be run within Dokuwiki
use dokuwiki\Form\Form;
use plugin\struct\meta\Assignments;
use plugin\struct\meta\Schema;
use plugin\struct\meta\SchemaEditor;
@ -48,24 +49,19 @@ class admin_plugin_struct_assignments extends DokuWiki_Admin_Plugin {
public function handle() {
global $INPUT;
/** @var \helper_plugin_struct_db $helper */
$helper = plugin_load('helper', 'struct_db');
$this->sqlite = $helper->getDB();
$assignments = new Assignments();
if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
$assignment = $INPUT->arr('assignment');
$ok = false;
if ($INPUT->str('action') === 'delete') {
$sql = 'DELETE FROM schema_assignments WHERE assign = ? AND tbl = ?';
$ok = $assignments->remove($assignment['assign'], $assignment['tbl']);
} else if($INPUT->str('action') === 'add') {
$ok = $assignments->add($assignment['assign'], $assignment['tbl']);
}
if ($INPUT->str('action') === 'add') {
$sql = 'REPLACE INTO schema_assignments (assign, tbl) VALUES (?,?)';
}
$opts = array($assignment['assign'], $assignment['tbl']);
if(empty($sql) || empty($assignment['assign']) || empty($assignment['tbl']) || !$this->sqlite->query($sql, $opts)) {
if(empty($sql) || empty($assignment['assign']) || empty($assignment['tbl']) || !$ok) {
msg('something went wrong while saving', -1);
}
}
}
/**
@ -80,9 +76,8 @@ class admin_plugin_struct_assignments extends DokuWiki_Admin_Plugin {
$schemas = $this->sqlite->res2arr($res);
$this->sqlite->res_close($res);
$res = $this->sqlite->query('SELECT * FROM schema_assignments ORDER BY assign');
$assignments = $this->sqlite->res2arr($res);
$this->sqlite->res_close($res);
$ass = new Assignments();
$assignments = $ass->getAll();
echo '<ul>';
foreach ($assignments as $assignment) {

View File

@ -2,6 +2,13 @@
namespace plugin\struct\meta;
/**
* Class Assignments
*
* Manages the assignment of schemas (table names) to pages and namespaces
*
* @package plugin\struct\meta
*/
class Assignments {
/** @var \helper_plugin_sqlite|null */
@ -31,6 +38,39 @@ class Assignments {
$this->sqlite->res_close($res);
}
/**
* Add a new assignment to the assignment table
*
* @param string $assign
* @param string $table
* @return bool
*/
public function add($assign, $table) {
$sql = 'REPLACE INTO schema_assignments (assign, tbl) VALUES (?,?)';
return (bool) $this->sqlite->query($sql, array($assign, $table));
}
/**
* Remove an existing assignment from the assignment table
*
* @param string $assign
* @param string $table
* @return bool
*/
public function remove($assign, $table) {
$sql = 'DELETE FROM schema_assignments WHERE assign = ? AND tbl = ?';
return (bool) $this->sqlite->query($sql, array($assign, $table));
}
/**
* Get the whole assignments table
*
* @return array
*/
public function getAll() {
return $this->assignments;
}
/**
* Returns a list of table names assigned to the given page
*