Improves on plugins, post method, improves on database for plugins, css styling

This commit is contained in:
Diego Najar 2017-06-19 23:14:38 +02:00
parent ca9d1d81b9
commit c54e64a94d
8 changed files with 133 additions and 41 deletions

View File

@ -28,6 +28,8 @@ class Plugin {
// (array) Database fields, only for initialize // (array) Database fields, only for initialize
public $dbFields; public $dbFields;
public $formButtons;
function __construct() function __construct()
{ {
$this->dbFields = array(); $this->dbFields = array();
@ -40,6 +42,8 @@ class Plugin {
// Class Name // Class Name
$this->className = $reflector->getName(); $this->className = $reflector->getName();
$this->formButtons = true;
// Call the method init() from the children // Call the method init() from the children
$this->init(); $this->init();
@ -60,6 +64,8 @@ class Plugin {
} }
} }
// DEPRECATED
// 2017-06-19
public function setDb($args) public function setDb($args)
{ {
foreach($this->dbFields as $key=>$value) { foreach($this->dbFields as $key=>$value) {
@ -189,6 +195,11 @@ class Plugin {
return $this->className; return $this->className;
} }
public function formButtons()
{
return $this->formButtons;
}
public function isCompatible() public function isCompatible()
{ {
$bluditRoot = explode('.', BLUDIT_VERSION); $bluditRoot = explode('.', BLUDIT_VERSION);
@ -255,7 +266,18 @@ class Plugin {
public function post() public function post()
{ {
$this->setDb($_POST); $args = $_POST;
foreach($this->dbFields as $key=>$value) {
if( isset($args[$key]) ) {
$value = Sanitize::html( $args[$key] );
if($value==='false') { $value = false; }
elseif($value==='true') { $value = true; }
settype($value, gettype($this->dbFields[$key]));
$this->db[$key] = $value;
}
}
return $this->save();
} }
} }

View File

@ -38,18 +38,22 @@ if( !method_exists($plugin, 'form') ) {
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{ {
// Call the method post of the plugin
$plugin->post();
// Add to syslog // Add to syslog
$Syslog->add(array( $Syslog->add(array(
'dictionaryKey'=>'plugin-configured', 'dictionaryKey'=>'plugin-configured',
'notes'=>$plugin->name() 'notes'=>$plugin->name()
)); ));
// Call the method post of the plugin
if( $plugin->post() ) {
// Create an alert // Create an alert
Alert::set( $Language->g('The changes have been saved') ); Alert::set( $Language->g('The changes have been saved') );
} }
else {
// Create an alert
Alert::set( $Language->g('Complete all fields') );
}
}
// ============================================================================ // ============================================================================
// Main after POST // Main after POST

View File

@ -673,11 +673,30 @@ div.plugin-links > span.separator {
#jsformplugin > div > label, #jsformplugin > div > label,
#jsformplugin > div > input[type=text], #jsformplugin > div > input[type=text],
#jsformplugin > div > input[type=checkbox], #jsformplugin > div > input[type=checkbox],
#jsformplugin > div > button[type=submit],
#jsformplugin > div > textarea, #jsformplugin > div > textarea,
#jsformplugin > div > select { #jsformplugin > div > select {
display: table-cell; display: table-cell;
} }
#jsformplugin > div > button[type=submit] {
margin-left: 200px;
border-radius: 2px;
padding: 1px 20px;
border: 0;
box-shadow: inset 0 0 5px rgba(0,0,0,.05);
text-shadow: 0 -1px 0 rgba(0,0,0,.1);
line-height: 28px;
min-height: 30px;
font-size: 1rem;
cursor: pointer;
}
#jsformplugin > div > button[type=submit].blue {
background: #007add !important;
color: #fff;
}
#jsformplugin > div > span.tip { #jsformplugin > div > span.tip {
color: #999; color: #999;
margin-top: 5px; margin-top: 5px;

View File

@ -149,20 +149,6 @@ $(document).ready(function() {
?> ?>
</div> </div>
<?php
if( AUTO_SCROLL ) {
?>
<script>
// Auto scroll
$(document).ready(function () {
$('html, body').animate({
scrollTop: $('#bl-view').offset().top
}, 'slow');
});
</script>
<?php
}
?>
</div> </div>
<!-- Javascript --> <!-- Javascript -->

View File

@ -13,10 +13,12 @@ HTML::formOpen(array('id'=>'jsformplugin'));
// Print the plugin form // Print the plugin form
echo $plugin->form(); echo $plugin->form();
if($plugin->formButtons()) {
// Form buttons // Form buttons
echo '<div class="uk-form-row uk-margin-bottom"> echo '<div class="uk-form-row uk-margin-bottom">
<button class="uk-button uk-button-primary" type="submit">'.$L->g('Save').'</button> <button class="uk-button uk-button-primary" type="submit">'.$L->g('Save').'</button>
<a class="uk-button" href="'.HTML_PATH_ADMIN_ROOT.'plugins">'.$L->g('Cancel').'</a> <a class="uk-button" href="'.HTML_PATH_ADMIN_ROOT.'plugins">'.$L->g('Cancel').'</a>
</div>'; </div>';
}
HTML::formClose(); HTML::formClose();

View File

@ -67,9 +67,6 @@ if(!defined('JSON_PRETTY_PRINT')) {
// Protecting against Symlink attacks // Protecting against Symlink attacks
define('CHECK_SYMBOLIC_LINKS', TRUE); define('CHECK_SYMBOLIC_LINKS', TRUE);
// Auto scroll
define('AUTO_SCROLL', TRUE);
// Alert status ok // Alert status ok
define('ALERT_STATUS_OK', 0); define('ALERT_STATUS_OK', 0);

View File

@ -173,3 +173,9 @@ function pluginEnabled($pluginName) {
return false; return false;
} }
function printDebug($array) {
echo '<pre>';
var_dump($array);
echo '</pre>';
}

View File

@ -4,6 +4,7 @@ class pluginLinks extends Plugin {
public function init() public function init()
{ {
// JSON database
$jsondb = json_encode(array( $jsondb = json_encode(array(
'Bludit'=>'https://bludit.com', 'Bludit'=>'https://bludit.com',
'Donate'=>'https://paypal.me/bludit' 'Donate'=>'https://paypal.me/bludit'
@ -14,20 +15,44 @@ class pluginLinks extends Plugin {
'label'=>'Links', 'label'=>'Links',
'jsondb'=>$jsondb 'jsondb'=>$jsondb
); );
$this->formButtons = false;
} }
// Method called when a POST request is sent
public function post() public function post()
{ {
// Get current jsondb value from database
$jsondb = $this->getValue('jsondb', $unsanitized=false); $jsondb = $this->getValue('jsondb', $unsanitized=false);
// Convert JSON to Array
$links = json_decode($jsondb, true); $links = json_decode($jsondb, true);
if( isset($_POST['deleteLink']) ) {
// Values from $_POST
$name = $_POST['deleteLink'];
// Delete the link
unset($links[$name]);
}
elseif( isset($_POST['addLink']) ) {
// Values from $_POST
$name = $_POST['linkName']; $name = $_POST['linkName'];
$url = $_POST['linkURL']; $url = $_POST['linkURL'];
$links[$url] = $name; // Check empty string
if( empty($name) ) { return false; }
$this->db['jsondb'] = json_encode($links); // Add the link
$this->save(); $links[$name] = $url;
}
// Sanitize the new values and replace the current values of the database
$this->db['label'] = Sanitize::html($_POST['label']);
$this->db['jsondb'] = Sanitize::html(json_encode($links));
// Save the database
return $this->save();
} }
// Method called on plugin settings on the admin area // Method called on plugin settings on the admin area
@ -37,24 +62,55 @@ class pluginLinks extends Plugin {
$html = '<div>'; $html = '<div>';
$html .= '<label>'.$Language->get('Label').'</label>'; $html .= '<label>'.$Language->get('Label').'</label>';
$html .= '<input id="jslabel" name="label" type="text" value="'.$this->getValue('label').'">'; $html .= '<input name="label" type="text" value="'.$this->getValue('label').'">';
$html .= '<span class="tip">'.$Language->get('Title of the plugin for the sidebar').'</span>';
$html .= '</div>'; $html .= '</div>';
$html .= '<div>';
$html .= '<button name="save" class="blue" type="submit">Save</button>';
$html .= '</div>';
// New link, when the user click on save button this call the method post()
// and the new link is added to the database
$html .= '<legend>'.$Language->get('Add a new link').'</legend>';
$html .= '<div>';
$html .= '<label>'.$Language->get('Name').'</label>';
$html .= '<input name="linkName" type="text" value="">';
$html .= '</div>';
$html .= '<div>';
$html .= '<label>'.$Language->get('Url').'</label>';
$html .= '<input name="linkURL" type="text" value="">';
$html .= '</div>';
$html .= '<div>';
$html .= '<button name="addLink" class="blue" type="submit">Add</button>';
$html .= '</div>';
$html .= '<legend>'.$Language->get('Links').'</legend>';
// Get the JSON DB, getValue() with the option unsanitized HTML code // Get the JSON DB, getValue() with the option unsanitized HTML code
$jsondb = $this->getValue('jsondb', $unsanitized=false); $jsondb = $this->getValue('jsondb', $unsanitized=false);
$links = json_decode($jsondb, true); $links = json_decode($jsondb, true);
foreach($links as $name=>$url) { foreach($links as $name=>$url) {
$html .= '<div>'; $html .= '<div>';
$html .= '<input name="'.$name.'" type="text" value="'.$name.'">'; $html .= '<label>'.$Language->get('Name').'</label>';
$html .= '<input name="'.$url.'" type="text" value="'.$url.'">'; $html .= '<input type="text" value="'.$name.'" disabled>';
$html .= '</div>'; $html .= '</div>';
}
$html .= '<div>'; $html .= '<div>';
$html .= 'Nombre <input name="linkName" type="text" value="">'; $html .= '<label>'.$Language->get('Url').'</label>';
$html .= '<br>URL <input name="linkURL" type="text" value="">'; $html .= '<input type="text" value="'.$url.'" disabled>';
$html .= '</div>'; $html .= '</div>';
$html .= '<div>';
$html .= '<button name="deleteLink" type="submit" value="'.$name.'">Delete</button>';
$html .= '</div>';
$html .= '</br>';
}
return $html; return $html;
} }