Skip to content

Commit

Permalink
coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
ssahara committed Nov 14, 2020
1 parent 9f7e851 commit afb157b
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 191 deletions.
16 changes: 8 additions & 8 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ protected function makelist() {
foreach ($conf as $button) {
$ico = '../../plugins/custombuttons/';
if (!$button['icon']) {
$ico .= 'genpng.php?text=' . $button["label"];
$ico .= 'genpng.php?text='. $button['label'];
} else {
$ico .= 'ico/' . $button['icon'];
$ico .= 'ico/'. $button['icon'];
}

if ($button["type"] == 1) {
if ($button['type'] == 1) {
$buttonlist[] = array(
'type' => 'format',
'title' => $button["label"],
'title' => $button['label'],
'icon' => $ico,
'open' => $button["pretag"],
'close' => $button["posttag"]
'open' => $button['pretag'],
'close' => $button['posttag']
);
} else {
$buttonlist[] = array(
'type' => 'insert',
'title' => $button["label"],
'title' => $button['label'],
'icon' => $ico,
'insert' => $button["code"],
'insert' => $button['code'],
'block' => true
);
}
Expand Down
360 changes: 177 additions & 183 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,187 +7,181 @@
*/
class admin_plugin_custombuttons extends DokuWiki_Admin_Plugin {

/**
* Return true for access only by admins (config:superuser) or false if managers are allowed as well
*
* @return bool
*/
public function forAdminOnly() {
return true;
}

/**
* return prompt for admin menu
*/
function getMenuText($language) {
return $this->getLang('name');
}

/**
* Read config
*
* @return bool|mixed
*/
protected function loadCBData() {
$file = @file_get_contents(DOKU_PLUGIN."custombuttons/config.json");
if(!$file) return false;
return json_decode($file, true);
}

/**
* Store config
*
* @param $conf
*/
protected function saveCBData($conf) {
$configfile = DOKU_PLUGIN."custombuttons/config.json";
if(is_writable($configfile) || (!file_exists($configfile) && is_writable(DOKU_PLUGIN."custombuttons"))) {
file_put_contents($configfile, json_encode($conf));
} else {
msg($this->getLang('txt_error'), -1);
}
}

protected function reloadBar() {
touch(DOKU_CONF."local.php");
}

public function handle() {

if (isset($_REQUEST['add'])) {
if (!checkSecurityToken()) return;

$conf = $this->loadCBData();
if(!$conf) {
$conf = array();
}
$type = 0;
if ($_REQUEST["pretag"] != "" && $_REQUEST["posttag"] != "") {
$type = 1;
}
array_push($conf, array(
"label" => $_REQUEST["label"],
"code" => $_REQUEST["code"],
"type" => $type,
"pretag" => $_REQUEST["pretag"],
"posttag" => $_REQUEST["posttag"],
"icon" => $_REQUEST["icon"],
));

$this->saveCBData($conf);
$this->reloadBar();
} elseif (isset($_REQUEST['delete'])) {
if (!checkSecurityToken()) return;

$conf = $this->loadCBData();
unset($conf[$_REQUEST["delete"]]);
$this->saveCBData($conf);
$this->reloadBar();
}
}

public function html() {
global $ID;
$conf = $this->loadCBData();

echo '
<div id = "custombuttons">
<h1>'.$this->getLang ( 'name' ).'</h1>
<h3>'.$this->getLang ( 'btnslist' ).'</h3>
<form id = "cb_button_list" action = "'.wl ( $ID ).'" method = "post">
<input type = "hidden" name = "do" value = "admin" />
<input type = "hidden" name = "page" value = "'.$this->getPluginName ( ).'" />';
formSecurityToken ( );
echo '
<table class = "inline">
<tr>
<th>'.$this->getLang ( 'btnslist_label' ).'</th>
<th>'.$this->getLang ( 'btnslist_code' ).'</th>
<th>'.$this->getLang ( 'btnslist_delete' ).'</th>
</tr>';
if ( $conf ) {
foreach ( $conf as $key => $button ) {
echo '
<tr>';
if ( !$button["type"] ) {
echo '
<td>'.hsc ( $button["label"] ).'</td>
<td>'.hsc ( $button["code"] ).'</td>';
} else {
$icon = '';
if ( $button['icon'] ) $icon = '<img src="' . DOKU_BASE.'lib/plugins/custombuttons/ico/'.$button['icon']. '" /> ';
echo '
<td>'.$icon.hsc ( $button["label"] ).'</td>
<td>'.hsc ( $button["pretag"] ).hsc ( $button["code"] ).hsc ( $button["posttag"] ).'</td>';
};
echo '
<td><input type = "radio" name = "delete" value = "'.$key.'"/></td>
</tr>';
}
}
echo '
</table>
<input type = "submit" class = "button" value = "'.$this->getLang ( 'btn_delete' ).'"/>
</form>
</br></br>
<h3>'.$this->getLang('addbtn').'</h3>
<form id = "cb_add_button" action = "'.wl ( $ID ).'" method = "post">
<input type = "hidden" name = "do" value = "admin" />
<input type = "hidden" name = "add" value = "1" />
<input type = "hidden" name = "page" value = "'.$this->getPluginName ( ).'" />';
formSecurityToken ( );
echo '
<table>
<tr>
<th>'.$this->getLang ( 'addbtn_icon' ).'</th>
<td>
<select name = "icon">
<option value = "">'.$this->getLang ( 'addbtn_textonly' ).'</option>';
$files = glob ( dirname ( __FILE__ ).'/ico/*.png' );
foreach ( $files as $file ) {
$file = hsc ( basename ( $file ) );
echo '
<option value = "'.$file.'">'.$file.'</option>';
};
echo '
</select>
</td>
<td></td>
</tr>
<tr>
<th>'.$this->getLang ( 'addbtn_label' ).'</th>
<td>
<input type = "text" name = "label" />
</td>
<td></td>
</tr>
<tr>
<th>'.$this->getLang ( 'addbtn_pretag' ).'</th>
<td>
<input type = "text" name = "pretag" />
</td>
<td>*</td>
</tr>
<tr>
<th>'.$this->getLang ( 'addbtn_posttag' ).'</th>
<td>
<input type="text" name = "posttag" />
</td>
<td>*</td>
</tr>
<tr>
<th>'.$this->getLang ( 'addbtn_code' ).'</th>
<td>
<input type = "text" name = "code" />
</td>
<td></td>
</tr>
</table>
<input type = "submit" class = "button" value = "'.$this->getLang ( 'btn_add' ).'" />
</form>
<div id = "cb_comment" >'.$this->getLang ( 'txt_comment' ).'</div>
</div>';
}
/**
* Return true for access only by admins (config:superuser) or false if managers are allowed as well
*
* @return bool
*/
public function forAdminOnly() {
return true;
}

/**
* return prompt for admin menu
*/
public function getMenuText($language) {
return $this->getLang('name');
}

/**
* Read config
*
* @return bool|mixed
*/
protected function loadCBData() {
$file = @file_get_contents(DOKU_PLUGIN.'custombuttons/config.json');
if (!$file) return false;
return json_decode($file, true);
}

/**
* Store config
*
* @param $conf
*/
protected function saveCBData($conf) {
$configfile = DOKU_PLUGIN.'custombuttons/config.json';
if (is_writable($configfile) || (!file_exists($configfile) && is_writable(DOKU_PLUGIN.'custombuttons'))) {
file_put_contents($configfile, json_encode($conf));
} else {
msg($this->getLang('txt_error'), -1);
}
}

protected function reloadBar() {
touch(DOKU_CONF.'local.php');
}

/**
* Execute the requested action
*/
public function handle() {

if (isset($_REQUEST['add'])) {
if (!checkSecurityToken()) return;

$conf = $this->loadCBData();
if(!$conf) {
$conf = array();
}
$type = 0;
if ($_REQUEST["pretag"] != "" && $_REQUEST["posttag"] != "") {
$type = 1;
}
array_push($conf, array(
'label' => $_REQUEST["label"],
'code' => $_REQUEST["code"],
'type' => $type,
'pretag' => $_REQUEST["pretag"],
'posttag' => $_REQUEST["posttag"],
'icon' => $_REQUEST["icon"],
));

$this->saveCBData($conf);
$this->reloadBar();
} elseif (isset($_REQUEST['delete'])) {
if (!checkSecurityToken()) return;

$conf = $this->loadCBData();
unset($conf[$_REQUEST["delete"]]);
$this->saveCBData($conf);
$this->reloadBar();
}
}

/**
* Render HTML output
*/
public function html() {
global $ID;
$conf = $this->loadCBData();

echo '<div id="custombuttons">';
echo '<h1>'.$this->getLang('name').'</h1>';

// list of custom buttons
echo '<h3>'.$this->getLang('btnslist').'</h3>';
echo '<form id="cb_button_list" action="'.wl($ID).'" method="post">'
.'<input type="hidden" name="do" value="admin" />'
.'<input type="hidden" name="page" value="'.$this->getPluginName().'" />';

formSecurityToken();

echo '<table class = "inline">';
echo '<tr>'
.'<th>'.$this->getLang('btnslist_label').'</th>'
.'<th>'.$this->getLang('btnslist_code').'</th>'
.'<th>'.$this->getLang('btnslist_delete').'</th>'
.'</tr>';
if ($conf) {
foreach ($conf as $key => $button) {
echo '<tr>';
if (!$button['type']) {
echo '<td>'.hsc($button['label']).'</td>'
.'<td>'.hsc($button['code']).'</td>';
} else {
$icon = '';
if ($button['icon']) {
$icon = '<img src="'. DOKU_BASE.'lib/plugins/custombuttons/ico/'.$button['icon'].'" /> ';
}
echo '<td>'.$icon.hsc($button['label']).'</td>'
.'<td>'.hsc($button['pretag']).hsc($button['code']).hsc($button['posttag']).'</td>';
};
echo '<td><input type="checkbox" name="delete" value="'.$key.'"/></td>';
echo '</tr>';
}
}
echo '</table>';
echo '<input type="submit" class="button" value="'.$this->getLang('btn_delete').'"/>';
echo '</form>';
echo '</br></br>';

// add custom button form
echo '<h3>'.$this->getLang('addbtn').'</h3>';
echo '<form id="cb_add_button" action="'.wl($ID).'" method="post">'
.'<input type="hidden" name="do" value="admin" />'
.'<input type="hidden" name="add" value="1" />'
.'<input type="hidden" name="page" value="'.$this->getPluginName().'" />';
formSecurityToken();
echo '<table>';
echo '<tr>';
echo '<th>'.$this->getLang('addbtn_icon').'</th>';
echo '<td>'
.'<select name="icon">'
.'<option value="">'.$this->getLang('addbtn_textonly').'</option>';
$files = glob(dirname(__FILE__).'/ico/*.png');
foreach ($files as $file) {
$file = hsc(basename($file));
echo '<option value="'.$file.'">'.$file.'</option>';
};
echo '</select>';
echo '</td>';
echo '<td></td>';
echo '</tr>';
echo '<tr>'
.'<th>'.$this->getLang('addbtn_label').'</th>'
.'<td><input type="text" name="label" /></td>'
.'<td></td>'
.'</tr>';
echo '<tr>'
.'<th>'.$this->getLang('addbtn_pretag').'</th>'
.'<td><input type="text" name="pretag" /></td>'
.'<td>*</td>'
.'</tr>';
echo '<tr>'
.'<th>'.$this->getLang('addbtn_posttag').'</th>'
.'<td><input type="text" name="posttag" /></td>'
.'<td>*</td>'
.'</tr>';
echo '<tr>'
.'<th>'.$this->getLang('addbtn_code').'</th>'
.'<td><input type="text" name="code" /></td>'
.'<td></td>'
.'</tr>';
echo '</table>';
echo '<input type="submit" class="button" value="'.$this->getLang('btn_add').'" />';
echo '</form>';
echo '<div id="cb_comment">'.$this->getLang('txt_comment').'</div>';
echo '</div>';
}
}

0 comments on commit afb157b

Please sign in to comment.