button.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = $_SESSION['theme']['button_icons']['text'] != '' ? $_SESSION['theme']['button_icons']['text'] : 'auto';
  26. //parse styles into array
  27. if ($array['style']) {
  28. $tmp = explode(';',$array['style']);
  29. foreach ($tmp as $style) {
  30. if ($style) {
  31. $style = explode(':', $style);
  32. $styles[trim($style[0])] = trim($style[1]);
  33. }
  34. }
  35. $array['style'] = $styles;
  36. unset($styles);
  37. }
  38. //button: open
  39. $button = "<button ";
  40. $button .= "type='".($array['type'] ? $array['type'] : 'button')."' ";
  41. $button .= $array['name'] ? "name=".self::quote($array['name'])." " : null;
  42. $button .= $array['value'] ? "value=".self::quote($array['value'])." " : null;
  43. $button .= $array['id'] ? "id='".$array['id']."' " : null;
  44. $button .= $array['label'] ? "alt=".self::quote($array['label'])." " : ($array['title'] ? "alt=".self::quote($array['title'])." " : null);
  45. if ($button_icons == 'only' || $button_icons == 'auto' || $array['title']) {
  46. if ($array['title'] || $array['label']) {
  47. $button .= "title=".($array['title'] ? self::quote($array['title']) : self::quote($array['label']))." ";
  48. }
  49. }
  50. $button .= $array['onclick'] ? "onclick=".self::quote($array['onclick'])." " : null;
  51. $button .= $array['onmouseover'] ? "onmouseenter=".self::quote($array['onmouseover'])." " : null;
  52. $button .= $array['onmouseout'] ? "onmouseleave=".self::quote($array['onmouseout'])." " : null;
  53. $button .= "class='btn btn-".($array['class'] ? $array['class'] : 'default')." ".($array['disabled'] ? 'disabled' : null)."' ";
  54. //ensure margin* styles are not applied to the button element when a link is defined
  55. if (is_array($array['style']) && @sizeof($array['style']) != 0) {
  56. foreach ($array['style'] as $property => $value) {
  57. if (!$array['link'] || !substr_count($property, 'margin')) {
  58. $styles .= $property.': '.$value.'; ';
  59. }
  60. }
  61. $button .= $styles ? "style=".self::quote($styles)." " : null;
  62. unset($styles);
  63. }
  64. $button .= $array['disabled'] ? "disabled='disabled' " : null;
  65. $button .= ">";
  66. //icon
  67. if ($array['icon'] && (
  68. $button_icons == 'only' ||
  69. $button_icons == 'always' ||
  70. $button_icons == 'auto' ||
  71. !$array['label']
  72. )) {
  73. $icon_class = is_array($array['icon']) ? $array['icon']['text'] : 'fas fa-'.$array['icon'];
  74. $button .= "<span class='".$icon_class." fa-fw'></span>";
  75. }
  76. //label
  77. if ($array['label'] && (
  78. $button_icons != 'only' ||
  79. !$array['icon'] ||
  80. $array['class'] == 'link'
  81. )) {
  82. if ($array['icon'] && $button_icons != 'always' && $button_icons != 'never' && $array['collapse'] !== false) {
  83. if ($array['collapse'] != '') {
  84. $collapse_class = $array['collapse'];
  85. }
  86. else if (self::$collapse !== false) {
  87. $collapse_class = self::$collapse;
  88. }
  89. }
  90. $pad_class = $array['icon'] ? 'pad' : null;
  91. $button .= "<span class='button-label ".$collapse_class." ".$pad_class."'>".$array['label']."</span>";
  92. }
  93. //button: close
  94. $button .= "</button>";
  95. //link
  96. if ($array['link']) {
  97. $anchor = "<a ";
  98. $anchor .= "href='".$array['link']."' ";
  99. $anchor .= "target='".($array['target'] ? $array['target'] : '_self')."' ";
  100. //ensure only margin* styles are applied to the anchor element
  101. if (is_array($array['style']) && @sizeof($array['style']) != 0) {
  102. foreach ($array['style'] as $property => $value) {
  103. if (substr_count($property, 'margin')) {
  104. $styles .= $property.': '.$value.'; ';
  105. }
  106. }
  107. $anchor .= $styles ? "style=".self::quote($styles)." " : null;
  108. unset($styles);
  109. }
  110. $anchor .= $array['disabled'] ? "class='disabled' onclick='return false;' " : null;
  111. $anchor .= ">";
  112. $button = $anchor.$button."</a>";
  113. }
  114. return $button;
  115. unset($button);
  116. }
  117. private static function quote($value) {
  118. return substr_count($value, "'") ? '"'.$value.'"' : "'".$value."'";
  119. }
  120. }
  121. }
  122. /*
  123. //usage
  124. 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]);
  125. echo button::create([
  126. 'type'=>'button',
  127. 'label'=>$text['button-label'],
  128. 'icon'=>'icon',
  129. 'name'=>'btn',
  130. 'id'=>'btn',
  131. 'value'=>'value',
  132. 'link'=>'url',
  133. 'target'=>'_blank',
  134. 'onclick'=>'javascript',
  135. 'onmouseover'=>'javascript',
  136. 'onmouseout'=>'javascript',
  137. 'class'=>'name',
  138. 'style'=>'css',
  139. 'title'=>$text['button-label'],
  140. 'collapse'=>'class',
  141. 'disabled'=>false
  142. ]);
  143. //options
  144. type 'button' (default) | 'submit' | 'link'
  145. label button text
  146. icon name without vendor prefix (e.g. 'user' instead of 'fa-user')
  147. value submitted value (if type is also set to 'submit')
  148. target '_blank' | '_self' (default) | etc
  149. onclick javascript
  150. onmouseover javascript (actually uses onmouseenter so doesn't bubble to child elements)
  151. onmouseout javascript (actually uses onmouseleave so doesn't bubble to child elements)
  152. class css class[es]
  153. style css style[s]
  154. title tooltip text (if not set, defaults to value of label)
  155. collapse overide the default hide class ('hide-md-dn')
  156. disabled boolean true/false, or a value that evaluates to a boolean
  157. //notes
  158. 1) all parameters are optional, but at least set a value for label or icon
  159. 2) overide the default hide class ('hide-md-dn') for all buttons that follow by using...
  160. button::$collapse = '...';
  161. 3) setting either collapse (instance or default) to false (boolean) will cause the button label to always be visible
  162. //example: enable/disable buttons with javascript
  163. //javascript
  164. onclick='button_enable('disabled_button');
  165. //button
  166. echo button::create(['type'=>'button', ... ,'id'=>'disabled_button','disabled'=>true]);
  167. //javascript
  168. onclick='button_disable('enabled_button');
  169. //button
  170. echo button::create(['type'=>'button', ... ,'id'=>'enabled_button']);
  171. //enable button class button
  172. echo "<script>\n";
  173. echo " function button_enable(button_id) {\n";
  174. echo " button = document.getElementById(button_id);\n";
  175. echo " button.disabled = false;\n";
  176. echo " button.classList.remove('disabled');\n";
  177. echo " if (button.parentElement.nodeName == 'A') {\n";
  178. echo " anchor = button.parentElement;\n";
  179. echo " anchor.classList.remove('disabled');\n";
  180. echo " anchor.setAttribute('onclick','');\n";
  181. echo " }\n";
  182. echo " }\n";
  183. echo "</script>\n";
  184. //disable button class button
  185. echo "<script>\n";
  186. echo " function button_disable(button_id) {\n";
  187. echo " button = document.getElementById(button_id);\n";
  188. echo " button.disabled = true;\n";
  189. echo " button.classList.add('disabled');\n";
  190. echo " if (button.parentElement.nodeName == 'A') {\n";
  191. echo " anchor = button.parentElement;\n";
  192. echo " anchor.classList.add('disabled');\n";
  193. echo " anchor.setAttribute('onclick','return false;');\n";
  194. echo " }\n";
  195. echo " }\n";
  196. echo "</script>\n";
  197. //note: the javascript functions above are already contained in the template.php file.
  198. */
  199. ?>