button.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Copyright (C) 2010 - 2019
  17. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. if (!class_exists('button')) {
  22. class button {
  23. public static $collapse = 'hide-md-dn';
  24. static function create($array) {
  25. $button_icons = !empty($_SESSION['theme']['button_icons']['text']) ? $_SESSION['theme']['button_icons']['text'] : 'auto';
  26. //parse styles into array
  27. if (!empty($array['style'])) {
  28. $tmp = explode(';',$array['style']);
  29. foreach ($tmp as $style) {
  30. if (!empty($style)) {
  31. $style = explode(':', $style);
  32. if (is_array($style) && @sizeof($style) == 2) {
  33. $styles[trim($style[0])] = trim($style[1]);
  34. }
  35. }
  36. }
  37. $array['style'] = $styles;
  38. unset($styles);
  39. }
  40. //button: open
  41. $button = "<button ";
  42. $button .= "type='".(!empty($array['type']) ? $array['type'] : 'button')."' ";
  43. $button .= !empty($array['name']) ? "name=".self::quote($array['name'])." " : null;
  44. $button .= !empty($array['value']) ? "value=".self::quote($array['value'])." " : null;
  45. $button .= !empty($array['id']) ? "id='".$array['id']."' " : null;
  46. $button .= !empty($array['label']) ? "alt=".self::quote($array['label'])." " : (!empty($array['title']) ? "alt=".self::quote($array['title'])." " : null);
  47. if ($button_icons == 'only' || $button_icons == 'auto' || $array['title']) {
  48. if (!empty($array['title']) || !empty($array['label'])) {
  49. $button .= "title=".(!empty($array['title']) ? self::quote($array['title']) : self::quote($array['label']))." ";
  50. }
  51. }
  52. $button .= !empty($array['onclick']) ? "onclick=".self::quote($array['onclick'])." " : null;
  53. $button .= !empty($array['onmouseover']) ? "onmouseenter=".self::quote($array['onmouseover'])." " : null;
  54. $button .= !empty($array['onmouseout']) ? "onmouseleave=".self::quote($array['onmouseout'])." " : null;
  55. //detect class addition (using + prefix)
  56. $button_class = !empty($array['class']) && substr($array['class'],0,1) == '+' ? 'default '.substr($array['class'], 1) : $array['class'] ?? '';
  57. $button .= "class='btn btn-".(!empty($button_class) ? $button_class : 'default')." ".(isset($array['disabled']) && $array['disabled'] ? 'disabled' : null)."' ";
  58. //ensure margin* styles are not applied to the button element when a link is defined
  59. if (!empty($array['style']) && is_array($array['style']) && @sizeof($array['style']) != 0) {
  60. $styles = '';
  61. foreach ($array['style'] as $property => $value) {
  62. if (empty($array['link']) || !substr_count($property, 'margin')) {
  63. $styles .= $property.': '.$value.'; ';
  64. }
  65. }
  66. $button .= $styles ? "style=".self::quote($styles)." " : null;
  67. unset($styles);
  68. }
  69. $button .= isset($array['disabled']) && $array['disabled'] ? "disabled='disabled' " : null;
  70. $button .= ">";
  71. //icon
  72. if (!empty($array['icon']) && (
  73. $button_icons == 'only' ||
  74. $button_icons == 'always' ||
  75. $button_icons == 'auto' ||
  76. !$array['label']
  77. )) {
  78. $icon_class = is_array($array['icon']) ? $array['icon']['text'] : $array['icon'];
  79. $button .= "<span class='".(substr($icon_class, 0, 3) != 'fa-' ? 'fa-solid fa-' : null).$icon_class." fa-fw'></span>";
  80. }
  81. //label
  82. if (!empty($array['label']) && (
  83. $button_icons != 'only' ||
  84. !$array['icon'] ||
  85. $array['class'] == 'link'
  86. )) {
  87. if (!empty($array['icon']) && $button_icons != 'always' && $button_icons != 'never' && isset($array['collapse']) && $array['collapse'] !== false) {
  88. if ($array['collapse'] != '') {
  89. $collapse_class = $array['collapse'];
  90. }
  91. else if (self::$collapse !== false) {
  92. $collapse_class = self::$collapse;
  93. }
  94. }
  95. $pad_class = !empty($array['icon']) ? 'pad' : null;
  96. $button .= "<span class='button-label ".($collapse_class ?? '')." ".$pad_class."'>".$array['label']."</span>";
  97. }
  98. //button: close
  99. $button .= "</button>";
  100. //link
  101. if (!empty($array['link'])) {
  102. $anchor = "<a ";
  103. $anchor .= "href='".$array['link']."' ";
  104. $anchor .= "target='".(!empty($array['target']) ? $array['target'] : '_self')."' ";
  105. //ensure only margin* styles are applied to the anchor element
  106. if (!empty($array['style']) && is_array($array['style']) && @sizeof($array['style']) != 0) {
  107. $styles = '';
  108. foreach ($array['style'] as $property => $value) {
  109. if (substr_count($property, 'margin')) {
  110. $styles .= $property.': '.$value.'; ';
  111. }
  112. }
  113. $anchor .= $styles ? "style=".self::quote($styles)." " : null;
  114. unset($styles);
  115. }
  116. $anchor .= isset($array['disabled']) && $array['disabled'] ? "class='disabled' onclick='return false;' " : null;
  117. $anchor .= ">";
  118. $button = $anchor.$button."</a>";
  119. }
  120. return $button;
  121. unset($button);
  122. }
  123. private static function quote($value) {
  124. return substr_count($value, "'") ? '"'.$value.'"' : "'".$value."'";
  125. }
  126. }
  127. }
  128. /*
  129. //usage
  130. 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]);
  131. echo button::create([
  132. 'type'=>'button',
  133. 'label'=>$text['button-label'],
  134. 'icon'=>'icon',
  135. 'name'=>'btn',
  136. 'id'=>'btn',
  137. 'value'=>'value',
  138. 'link'=>'url',
  139. 'target'=>'_blank',
  140. 'onclick'=>'javascript',
  141. 'onmouseover'=>'javascript',
  142. 'onmouseout'=>'javascript',
  143. 'class'=>'name',
  144. 'style'=>'css',
  145. 'title'=>$text['button-label'],
  146. 'collapse'=>'class',
  147. 'disabled'=>false
  148. ]);
  149. //options
  150. type 'button' (default) | 'submit' | 'link'
  151. label button text
  152. icon name with full vendor style and prefix (e.g. 'fa-solid fa-user' instead of 'fa-user' or 'user')
  153. value submitted value (if type is also set to 'submit')
  154. target '_blank' | '_self' (default) | etc
  155. onclick javascript
  156. onmouseover javascript (actually uses onmouseenter so doesn't bubble to child elements)
  157. onmouseout javascript (actually uses onmouseleave so doesn't bubble to child elements)
  158. class css class[es]
  159. style css style[s]
  160. title tooltip text (if not set, defaults to value of label)
  161. collapse overide the default hide class ('hide-md-dn')
  162. disabled boolean true/false, or a value that evaluates to a boolean
  163. //notes
  164. 1) all parameters are optional, but at least set a value for label or icon
  165. 2) overide the default hide class ('hide-md-dn') for all buttons that follow by using...
  166. button::$collapse = '...';
  167. 3) setting either collapse (instance or default) to false (boolean) will cause the button label to always be visible
  168. //example: enable/disable buttons with javascript
  169. //javascript
  170. onclick='button_enable('disabled_button');
  171. //button
  172. echo button::create(['type'=>'button', ... ,'id'=>'disabled_button','disabled'=>true]);
  173. //javascript
  174. onclick='button_disable('enabled_button');
  175. //button
  176. echo button::create(['type'=>'button', ... ,'id'=>'enabled_button']);
  177. //enable button class button
  178. echo "<script>\n";
  179. echo " function button_enable(button_id) {\n";
  180. echo " button = document.getElementById(button_id);\n";
  181. echo " button.disabled = false;\n";
  182. echo " button.classList.remove('disabled');\n";
  183. echo " if (button.parentElement.nodeName == 'A') {\n";
  184. echo " anchor = button.parentElement;\n";
  185. echo " anchor.classList.remove('disabled');\n";
  186. echo " anchor.setAttribute('onclick','');\n";
  187. echo " }\n";
  188. echo " }\n";
  189. echo "</script>\n";
  190. //disable button class button
  191. echo "<script>\n";
  192. echo " function button_disable(button_id) {\n";
  193. echo " button = document.getElementById(button_id);\n";
  194. echo " button.disabled = true;\n";
  195. echo " button.classList.add('disabled');\n";
  196. echo " if (button.parentElement.nodeName == 'A') {\n";
  197. echo " anchor = button.parentElement;\n";
  198. echo " anchor.classList.add('disabled');\n";
  199. echo " anchor.setAttribute('onclick','return false;');\n";
  200. echo " }\n";
  201. echo " }\n";
  202. echo "</script>\n";
  203. //note: the javascript functions above are already contained in the template.php file.
  204. */
  205. ?>