2019-10-12 09:12:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
FusionPBX
|
|
|
|
|
Version: MPL 1.1
|
|
|
|
|
|
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
for the specific language governing rights and limitations under the
|
|
|
|
|
License.
|
|
|
|
|
|
|
|
|
|
The Original Code is FusionPBX
|
|
|
|
|
|
|
|
|
|
The Initial Developer of the Original Code is
|
|
|
|
|
Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
Copyright (C) 2010 - 2019
|
|
|
|
|
All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Contributor(s):
|
|
|
|
|
Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class button {
|
|
|
|
|
|
2019-10-22 09:41:16 +02:00
|
|
|
public static $collapse = 'hide-md-dn';
|
|
|
|
|
|
2019-10-12 09:12:59 +02:00
|
|
|
static function create($array) {
|
2023-05-09 07:03:00 +02:00
|
|
|
$button_icons = !empty($_SESSION['theme']['button_icons']['text']) ? $_SESSION['theme']['button_icons']['text'] : 'auto';
|
2020-01-08 18:34:26 +01:00
|
|
|
//parse styles into array
|
2023-05-09 07:03:00 +02:00
|
|
|
if (!empty($array['style'])) {
|
2020-01-08 18:34:26 +01:00
|
|
|
$tmp = explode(';',$array['style']);
|
|
|
|
|
foreach ($tmp as $style) {
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($style)) {
|
2020-01-08 18:34:26 +01:00
|
|
|
$style = explode(':', $style);
|
2023-05-10 00:52:04 +02:00
|
|
|
if (is_array($style) && @sizeof($style) == 2) {
|
|
|
|
|
$styles[trim($style[0])] = trim($style[1]);
|
|
|
|
|
}
|
2020-01-08 18:34:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$array['style'] = $styles;
|
|
|
|
|
unset($styles);
|
|
|
|
|
}
|
2019-10-12 09:12:59 +02:00
|
|
|
//button: open
|
|
|
|
|
$button = "<button ";
|
2023-05-10 00:52:04 +02:00
|
|
|
$button .= "type='".(!empty($array['type']) ? $array['type'] : 'button')."' ";
|
2023-05-09 07:03:00 +02:00
|
|
|
$button .= !empty($array['name']) ? "name=".self::quote($array['name'])." " : null;
|
|
|
|
|
$button .= !empty($array['value']) ? "value=".self::quote($array['value'])." " : null;
|
|
|
|
|
$button .= !empty($array['id']) ? "id='".$array['id']."' " : null;
|
2023-05-10 00:52:04 +02:00
|
|
|
$button .= !empty($array['label']) ? "alt=".self::quote($array['label'])." " : (!empty($array['title']) ? "alt=".self::quote($array['title'])." " : null);
|
2019-10-12 09:12:59 +02:00
|
|
|
if ($button_icons == 'only' || $button_icons == 'auto' || $array['title']) {
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['title']) || !empty($array['label'])) {
|
|
|
|
|
$button .= "title=".(!empty($array['title']) ? self::quote($array['title']) : self::quote($array['label']))." ";
|
2019-10-30 08:05:43 +01:00
|
|
|
}
|
2019-10-12 09:12:59 +02:00
|
|
|
}
|
2023-05-09 07:03:00 +02:00
|
|
|
$button .= !empty($array['onclick']) ? "onclick=".self::quote($array['onclick'])." " : null;
|
|
|
|
|
$button .= !empty($array['onmouseover']) ? "onmouseenter=".self::quote($array['onmouseover'])." " : null;
|
|
|
|
|
$button .= !empty($array['onmouseout']) ? "onmouseleave=".self::quote($array['onmouseout'])." " : null;
|
2020-11-13 02:27:11 +01:00
|
|
|
//detect class addition (using + prefix)
|
2023-05-10 00:52:04 +02:00
|
|
|
$button_class = !empty($array['class']) && substr($array['class'],0,1) == '+' ? 'default '.substr($array['class'], 1) : $array['class'] ?? '';
|
|
|
|
|
$button .= "class='btn btn-".(!empty($button_class) ? $button_class : 'default')." ".(isset($array['disabled']) && $array['disabled'] ? 'disabled' : null)."' ";
|
2020-01-08 18:36:37 +01:00
|
|
|
//ensure margin* styles are not applied to the button element when a link is defined
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['style']) && is_array($array['style']) && @sizeof($array['style']) != 0) {
|
|
|
|
|
$styles = '';
|
2020-01-08 18:34:26 +01:00
|
|
|
foreach ($array['style'] as $property => $value) {
|
2023-05-10 00:52:04 +02:00
|
|
|
if (empty($array['link']) || !substr_count($property, 'margin')) {
|
2020-01-08 18:34:26 +01:00
|
|
|
$styles .= $property.': '.$value.'; ';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$button .= $styles ? "style=".self::quote($styles)." " : null;
|
|
|
|
|
unset($styles);
|
|
|
|
|
}
|
2023-05-10 00:52:04 +02:00
|
|
|
$button .= isset($array['disabled']) && $array['disabled'] ? "disabled='disabled' " : null;
|
2019-10-12 09:12:59 +02:00
|
|
|
$button .= ">";
|
|
|
|
|
//icon
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['icon']) && (
|
2019-10-21 18:23:58 +02:00
|
|
|
$button_icons == 'only' ||
|
|
|
|
|
$button_icons == 'always' ||
|
|
|
|
|
$button_icons == 'auto' ||
|
2019-10-12 09:12:59 +02:00
|
|
|
!$array['label']
|
|
|
|
|
)) {
|
2024-08-10 02:14:52 +02:00
|
|
|
$icon_class = is_array($array['icon']) ? $array['icon']['text'] : $array['icon'];
|
|
|
|
|
$button .= "<span class='".(substr($icon_class, 0, 3) != 'fa-' ? 'fa-solid fa-' : null).$icon_class." fa-fw'></span>";
|
2019-10-12 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
//label
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['label']) && (
|
2019-10-12 09:12:59 +02:00
|
|
|
$button_icons != 'only' ||
|
|
|
|
|
!$array['icon'] ||
|
|
|
|
|
$array['class'] == 'link'
|
|
|
|
|
)) {
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['icon']) && $button_icons != 'always' && $button_icons != 'never' && isset($array['collapse']) && $array['collapse'] !== false) {
|
2019-10-22 09:41:16 +02:00
|
|
|
if ($array['collapse'] != '') {
|
|
|
|
|
$collapse_class = $array['collapse'];
|
|
|
|
|
}
|
|
|
|
|
else if (self::$collapse !== false) {
|
|
|
|
|
$collapse_class = self::$collapse;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-10 00:52:04 +02:00
|
|
|
$pad_class = !empty($array['icon']) ? 'pad' : null;
|
|
|
|
|
$button .= "<span class='button-label ".($collapse_class ?? '')." ".$pad_class."'>".$array['label']."</span>";
|
2019-10-12 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
//button: close
|
|
|
|
|
$button .= "</button>";
|
|
|
|
|
//link
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['link'])) {
|
2019-10-24 05:19:37 +02:00
|
|
|
$anchor = "<a ";
|
|
|
|
|
$anchor .= "href='".$array['link']."' ";
|
2023-05-10 00:52:04 +02:00
|
|
|
$anchor .= "target='".(!empty($array['target']) ? $array['target'] : '_self')."' ";
|
2020-01-08 18:34:26 +01:00
|
|
|
//ensure only margin* styles are applied to the anchor element
|
2023-05-10 00:52:04 +02:00
|
|
|
if (!empty($array['style']) && is_array($array['style']) && @sizeof($array['style']) != 0) {
|
|
|
|
|
$styles = '';
|
2020-01-08 18:34:26 +01:00
|
|
|
foreach ($array['style'] as $property => $value) {
|
|
|
|
|
if (substr_count($property, 'margin')) {
|
|
|
|
|
$styles .= $property.': '.$value.'; ';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$anchor .= $styles ? "style=".self::quote($styles)." " : null;
|
|
|
|
|
unset($styles);
|
|
|
|
|
}
|
2023-05-10 00:52:04 +02:00
|
|
|
$anchor .= isset($array['disabled']) && $array['disabled'] ? "class='disabled' onclick='return false;' " : null;
|
2019-10-24 05:19:37 +02:00
|
|
|
$anchor .= ">";
|
|
|
|
|
$button = $anchor.$button."</a>";
|
2019-10-12 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
return $button;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 08:05:43 +01:00
|
|
|
private static function quote($value) {
|
|
|
|
|
return substr_count($value, "'") ? '"'.$value.'"' : "'".$value."'";
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 09:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2019-10-24 05:19:37 +02:00
|
|
|
|
2019-10-21 18:23:58 +02:00
|
|
|
//usage
|
2019-10-12 09:12:59 +02:00
|
|
|
|
2019-10-30 06:11:19 +01:00
|
|
|
echo button::create(['type'=>'button','label'=>$text['button-label'],'icon'=>'icon','name'=>'btn','id'=>'btn','value'=>'value','link'=>'url','target'=>'_blank','onclick'=>'javascript','onmouseover'=>'javascript','onmouseout'=>'javascript','class'=>'name','style'=>'css','title'=>$text['button-label'],'collapse'=>'class','disabled'=>false]);
|
2019-10-21 18:23:58 +02:00
|
|
|
|
|
|
|
|
echo button::create([
|
|
|
|
|
'type'=>'button',
|
|
|
|
|
'label'=>$text['button-label'],
|
|
|
|
|
'icon'=>'icon',
|
|
|
|
|
'name'=>'btn',
|
|
|
|
|
'id'=>'btn',
|
|
|
|
|
'value'=>'value',
|
|
|
|
|
'link'=>'url',
|
|
|
|
|
'target'=>'_blank',
|
|
|
|
|
'onclick'=>'javascript',
|
2019-10-30 06:11:19 +01:00
|
|
|
'onmouseover'=>'javascript',
|
|
|
|
|
'onmouseout'=>'javascript',
|
2019-10-21 18:23:58 +02:00
|
|
|
'class'=>'name',
|
|
|
|
|
'style'=>'css',
|
2019-10-22 09:41:16 +02:00
|
|
|
'title'=>$text['button-label'],
|
2019-10-24 05:19:37 +02:00
|
|
|
'collapse'=>'class',
|
|
|
|
|
'disabled'=>false
|
2019-10-21 18:23:58 +02:00
|
|
|
]);
|
|
|
|
|
|
2019-10-24 05:19:37 +02:00
|
|
|
|
2019-10-21 18:23:58 +02:00
|
|
|
//options
|
|
|
|
|
|
|
|
|
|
type 'button' (default) | 'submit' | 'link'
|
|
|
|
|
label button text
|
2024-08-10 02:14:52 +02:00
|
|
|
icon name with full vendor style and prefix (e.g. 'fa-solid fa-user' instead of 'fa-user' or 'user')
|
2019-10-21 18:23:58 +02:00
|
|
|
value submitted value (if type is also set to 'submit')
|
|
|
|
|
target '_blank' | '_self' (default) | etc
|
|
|
|
|
onclick javascript
|
2019-10-30 06:11:19 +01:00
|
|
|
onmouseover javascript (actually uses onmouseenter so doesn't bubble to child elements)
|
|
|
|
|
onmouseout javascript (actually uses onmouseleave so doesn't bubble to child elements)
|
2019-10-21 18:23:58 +02:00
|
|
|
class css class[es]
|
|
|
|
|
style css style[s]
|
|
|
|
|
title tooltip text (if not set, defaults to value of label)
|
2019-10-22 09:41:16 +02:00
|
|
|
collapse overide the default hide class ('hide-md-dn')
|
2019-10-24 05:19:37 +02:00
|
|
|
disabled boolean true/false, or a value that evaluates to a boolean
|
|
|
|
|
|
2019-10-22 09:41:16 +02:00
|
|
|
|
|
|
|
|
//notes
|
|
|
|
|
|
|
|
|
|
1) all parameters are optional, but at least set a value for label or icon
|
|
|
|
|
2) overide the default hide class ('hide-md-dn') for all buttons that follow by using...
|
|
|
|
|
|
|
|
|
|
button::$collapse = '...';
|
|
|
|
|
|
|
|
|
|
3) setting either collapse (instance or default) to false (boolean) will cause the button label to always be visible
|
2019-10-12 09:12:59 +02:00
|
|
|
|
2019-10-24 05:19:37 +02:00
|
|
|
|
|
|
|
|
//example: enable/disable buttons with javascript
|
|
|
|
|
|
|
|
|
|
//javascript
|
|
|
|
|
onclick='button_enable('disabled_button');
|
|
|
|
|
//button
|
|
|
|
|
echo button::create(['type'=>'button', ... ,'id'=>'disabled_button','disabled'=>true]);
|
|
|
|
|
|
|
|
|
|
//javascript
|
|
|
|
|
onclick='button_disable('enabled_button');
|
|
|
|
|
|
|
|
|
|
//button
|
|
|
|
|
echo button::create(['type'=>'button', ... ,'id'=>'enabled_button']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//enable button class button
|
|
|
|
|
echo "<script>\n";
|
|
|
|
|
echo " function button_enable(button_id) {\n";
|
|
|
|
|
echo " button = document.getElementById(button_id);\n";
|
|
|
|
|
echo " button.disabled = false;\n";
|
|
|
|
|
echo " button.classList.remove('disabled');\n";
|
|
|
|
|
echo " if (button.parentElement.nodeName == 'A') {\n";
|
|
|
|
|
echo " anchor = button.parentElement;\n";
|
|
|
|
|
echo " anchor.classList.remove('disabled');\n";
|
|
|
|
|
echo " anchor.setAttribute('onclick','');\n";
|
|
|
|
|
echo " }\n";
|
|
|
|
|
echo " }\n";
|
|
|
|
|
echo "</script>\n";
|
|
|
|
|
|
|
|
|
|
//disable button class button
|
|
|
|
|
echo "<script>\n";
|
|
|
|
|
echo " function button_disable(button_id) {\n";
|
|
|
|
|
echo " button = document.getElementById(button_id);\n";
|
|
|
|
|
echo " button.disabled = true;\n";
|
|
|
|
|
echo " button.classList.add('disabled');\n";
|
|
|
|
|
echo " if (button.parentElement.nodeName == 'A') {\n";
|
|
|
|
|
echo " anchor = button.parentElement;\n";
|
|
|
|
|
echo " anchor.classList.add('disabled');\n";
|
|
|
|
|
echo " anchor.setAttribute('onclick','return false;');\n";
|
|
|
|
|
echo " }\n";
|
|
|
|
|
echo " }\n";
|
|
|
|
|
echo "</script>\n";
|
|
|
|
|
|
|
|
|
|
//note: the javascript functions above are already contained in the template.php file.
|
|
|
|
|
|
|
|
|
|
|
2019-10-12 09:12:59 +02:00
|
|
|
*/
|