Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Refactor #165

Merged
merged 8 commits into from Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions _test/Validator.test.php
Expand Up @@ -45,7 +45,7 @@ public function test_validate_nonArray() {
$errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
$integer = new Decimal();

$validator = new mock\Validator();
$validator = new mock\ValueValidator();
$value = 'NaN';
$this->assertFalse($validator->validateField($integer, $label, $value));
$this->assertEquals(array($errormsg), $validator->getErrors());
Expand All @@ -56,7 +56,7 @@ public function test_validate_array() {
$errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), $label);
$integer = new Decimal();

$validator = new mock\Validator();
$validator = new mock\ValueValidator();
$value = array('NaN', 'NaN');
$this->assertFalse($validator->validateField($integer, $label, $value));
$this->assertEquals(array($errormsg, $errormsg), $validator->getErrors());
Expand All @@ -65,7 +65,7 @@ public function test_validate_array() {
public function test_validate_blank() {
$integer = new Decimal();

$validator = new mock\Validator();
$validator = new mock\ValueValidator();
$value = null;
$this->assertTrue($validator->validateField($integer, 'label', $value));
$this->assertEquals(array(), $validator->getErrors());
Expand All @@ -74,7 +74,7 @@ public function test_validate_blank() {
public function test_validate_clean() {
$text = new Text();

$validator = new mock\Validator();
$validator = new mock\ValueValidator();
$value = ' foo ';
$this->assertTrue($validator->validateField($text, 'label', $value));
$this->assertEquals('foo', $value);
Expand Down
6 changes: 2 additions & 4 deletions _test/entry.test.php
Expand Up @@ -5,14 +5,12 @@
use dokuwiki\plugin\struct\meta;

/**
* Tests for the building of SQL-Queries for the struct plugin
*
* @group plugin_struct
* @group plugins
*
* @covers action_plugin_struct_entry
*
*
* @covers action_plugin_struct_revert
* @covers action_plugin_struct_edit
*/
class entry_struct_test extends StructTest {

Expand Down
2 changes: 1 addition & 1 deletion _test/mock/Validator.php → _test/mock/ValueValidator.php
Expand Up @@ -5,7 +5,7 @@
use \dokuwiki\plugin\struct\meta;
use dokuwiki\plugin\struct\types\AbstractBaseType;

class Validator extends meta\Validator {
class ValueValidator extends meta\ValueValidator {
public function validateField(AbstractBaseType $type, $label, &$data) {
return parent::validateField($type, $label, $data);
}
Expand Down
25 changes: 12 additions & 13 deletions action/bureaucracy.php
Expand Up @@ -12,8 +12,6 @@
use dokuwiki\plugin\struct\meta\AccessTable;
use dokuwiki\plugin\struct\meta\Assignments;
use dokuwiki\plugin\struct\meta\Schema;
use dokuwiki\plugin\struct\meta\AccessTableData;
use dokuwiki\plugin\struct\meta\Validator;

/**
* Handles bureaucracy additions
Expand Down Expand Up @@ -48,7 +46,7 @@ public function register(Doku_Event_Handler $controller) {
*/
public function handle_schema(Doku_Event $event, $param) {
$args = $event->data['args'];
if($args[0] != 'struct_schema') return;
if($args[0] != 'struct_schema') return false;
$event->preventDefault();
$event->stopPropagation();

Expand All @@ -59,7 +57,7 @@ public function handle_schema(Doku_Event $event, $param) {
$schema = new Schema($helper->opt['label']);
if(!$schema->getId()) {
msg('This schema does not exist', -1);
return;
return false;
}

foreach($schema->getColumns(false) as $column) {
Expand All @@ -71,6 +69,7 @@ public function handle_schema(Doku_Event $event, $param) {
$field->column = $column;
$event->data['fields'][] = $field;
}
return true;
}

/**
Expand All @@ -95,17 +94,17 @@ public function handle_save(Doku_Event $event, $param) {

// save all the struct data of assigned schemas
$id = $event->data['id'];
$time = filemtime(wikiFN($id));

$validator = new Validator();
if(!$validator->validate($tosave, $id)) return false;
$tosave = $validator->getCleanedData();
$assignments = new Assignments();
$assigned = $assignments->getPageAssignments($id);
foreach($tosave as $table => $data) {
$time = filemtime(wikiFN($id));
$schemaData = AccessTable::byTableName($table, $id, $time);
$schemaData->saveData($data);

$assignments = new Assignments();
$assignments->assignPageSchema($id, $table);
if(!in_array($table, $assigned)) continue;
$access = AccessTable::byTableName($table, $id, $time);
$validator = $access->getValidator($data);
if($validator->validate()) {
$validator->saveData($time);
}
}

return true;
Expand Down
138 changes: 138 additions & 0 deletions action/edit.php
@@ -0,0 +1,138 @@
<?php
/**
* DokuWiki Plugin struct (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
*/

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

use dokuwiki\plugin\struct\meta\AccessTable;
use dokuwiki\plugin\struct\meta\Assignments;
use dokuwiki\plugin\struct\meta\Value;

/**
* Class action_plugin_struct_entry
*
* Handles adding struct forms to the default editor
*/
class action_plugin_struct_edit extends DokuWiki_Action_Plugin {

/**
* @var string The form name we use to transfer schema data
*/
protected static $VAR = 'struct_schema_data';

/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {
// add the struct editor to the edit form;
$controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform');
}

/**
* Enhance the editing form with structural data editing
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return bool
*/
public function handle_editform(Doku_Event $event, $param) {
global $ID;

$assignments = new Assignments();
$tables = $assignments->getPageAssignments($ID);

$html = '';
foreach($tables as $table) {
$html .= $this->createForm($table);
}

/** @var Doku_Form $form */
$form = $event->data;
$html = "<div class=\"struct_entry_form\">$html</div>";
$pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons
$form->insertElement($pos, $html);

return true;
}

/**
* Create the form to edit schemadata
*
* @param string $tablename
* @return string The HTML for this schema's form
*/
protected function createForm($tablename) {
global $ID;
global $REV;
global $INPUT;
if(auth_quickaclcheck($ID) == AUTH_READ) return '';
if(checklock($ID)) return '';
$schema = AccessTable::byTableName($tablename, $ID, $REV);
$schemadata = $schema->getData();

$structdata = $INPUT->arr(self::$VAR);
if(isset($structdata[$tablename])) {
$postdata = $structdata[$tablename];
} else {
$postdata = array();
}

// we need a short, unique identifier to use in the cookie. this should be good enough
$schemaid = 'SRCT' . substr(str_replace(array('+', '/'), '', base64_encode(sha1($tablename, true))), 0, 5);
$html = '<fieldset data-schema="' . $schemaid . '">';
$html .= '<legend>' . hsc($tablename) . '</legend>';
foreach($schemadata as $field) {
$label = $field->getColumn()->getLabel();
if(isset($postdata[$label])) {
// posted data trumps stored data
$field->setValue($postdata[$label]);
}
$html .= $this->makeField($field, self::$VAR . "[$tablename][$label]");
}
$html .= '</fieldset>';

return $html;
}

/**
* Create the input field
*
* @param Value $field
* @param String $name field's name
* @return string
*/
public function makeField(Value $field, $name) {
$trans = hsc($field->getColumn()->getTranslatedLabel());
$hint = hsc($field->getColumn()->getTranslatedHint());
$class = $hint ? 'hashint' : '';
$colname = $field->getColumn()->getFullQualifiedLabel();

$input = $field->getValueEditor($name);

// we keep all the custom form stuff the field might produce, but hide it
if(!$field->getColumn()->isVisibleInEditor()) {
$hide = 'style="display:none"';
} else {
$hide = '';
}

$html = '';
$html .= "<label $hide data-column=\"$colname\">";
$html .= "<span class=\"label $class\" title=\"$hint\">$trans</span>";
$html .= "<span class=\"input\">$input</span>";
$html .= '</label>';

return $html;
}
}

// vim:ts=4:sw=4:et: