PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/Mage/Backend/Block/Widget/Button/Split.php

https://github.com/JackoPlane/magento2
PHP | 265 lines | 148 code | 22 blank | 95 comment | 21 complexity | 93c29b6b94fec218eb9531f290c60642 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Backend
  23. * @copyright Copyright (c) 2013 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Split button widget
  28. *
  29. * @method array getOptions()
  30. * @method string getButtonClass()
  31. * @method string getClass()
  32. * @method string getLabel()
  33. * @method string getTitle()
  34. * @method string getDisabled()
  35. * @method string getStyle()
  36. * @method array getDataAttribute()
  37. */
  38. class Mage_Backend_Block_Widget_Button_Split extends Mage_Backend_Block_Widget
  39. {
  40. /**
  41. * Define block template
  42. */
  43. protected function _construct()
  44. {
  45. $this->setTemplate('Mage_Backend::widget/button/split.phtml');
  46. parent::_construct();
  47. }
  48. /**
  49. * Retrieve <div> wrapper attributes html
  50. *
  51. * @return string
  52. */
  53. public function getAttributesHtml()
  54. {
  55. $title = $this->getTitle();
  56. if (!$title) {
  57. $title = $this->getLabel();
  58. }
  59. $classes = array();
  60. if ($this->hasSplit()) {
  61. $classes[] = 'actions-split';
  62. }
  63. //@TODO Perhaps use $this->getClass() instead
  64. if ($this->getButtonClass()) {
  65. $classes[] = $this->getButtonClass();
  66. }
  67. $attributes = array(
  68. 'id' => $this->getId(),
  69. 'title' => $title,
  70. 'class' => join(' ', $classes),
  71. );
  72. $html = $this->_getAttributesString($attributes);
  73. return $html;
  74. }
  75. /**
  76. * Retrieve button attributes html
  77. *
  78. * @return string
  79. */
  80. public function getButtonAttributesHtml()
  81. {
  82. $disabled = $this->getDisabled() ? 'disabled' : '';
  83. $title = $this->getTitle();
  84. if (!$title) {
  85. $title = $this->getLabel();
  86. }
  87. $classes = array();
  88. $classes[] = 'action-default';
  89. $classes[] = 'primary';
  90. // @TODO Perhaps use $this->getButtonClass() instead
  91. if ($this->getClass()) {
  92. $classes[] = $this->getClass();
  93. }
  94. if ($disabled) {
  95. $classes[] = $disabled;
  96. }
  97. $attributes = array(
  98. 'id' => $this->getId() . '-button',
  99. 'title' => $title,
  100. 'class' => join(' ', $classes),
  101. 'disabled' => $disabled,
  102. 'style' => $this->getStyle(),
  103. );
  104. //TODO perhaps we need to skip data-mage-init when disabled="disabled"
  105. if ($this->getDataAttribute()) {
  106. $this->_getDataAttributes($this->getDataAttribute(), $attributes);
  107. }
  108. $html = $this->_getAttributesString($attributes);
  109. $html .= $this->getUiId();
  110. return $html;
  111. }
  112. /**
  113. * Retrieve toggle button attributes html
  114. *
  115. * @return string
  116. */
  117. public function getToggleAttributesHtml()
  118. {
  119. $disabled = $this->getDisabled() ? 'disabled' : '';
  120. $title = $this->getTitle();
  121. if (!$title) {
  122. $title = $this->getLabel();
  123. }
  124. $classes = array();
  125. $classes[] = 'action-toggle';
  126. $classes[] = 'primary';
  127. if ($this->getClass()) {
  128. $classes[] = $this->getClass();
  129. }
  130. if ($disabled) {
  131. $classes[] = $disabled;
  132. }
  133. $attributes = array(
  134. 'title' => $title,
  135. 'class' => join(' ', $classes),
  136. 'disabled' => $disabled
  137. );
  138. $this->_getDataAttributes(array('toggle' => 'dropdown'), $attributes);
  139. $html = $this->_getAttributesString($attributes);
  140. $html .= $this->getUiId('dropdown');
  141. return $html;
  142. }
  143. /**
  144. * Retrieve options attributes html
  145. *
  146. * @param string $key
  147. * @param array $option
  148. * @return string
  149. * @SuppressWarnings(PHPMD.NPathComplexity)
  150. */
  151. public function getOptionAttributesHtml($key, $option)
  152. {
  153. $disabled = (isset($option['disabled']) && $option['disabled']) ? 'disabled' : '';
  154. if (isset($option['title'])) {
  155. $title = $option['title'];
  156. } else {
  157. $title = $option['label'];
  158. }
  159. $classes = array();
  160. $classes[] = 'item';
  161. if (!empty($option['default'])) {
  162. $classes[] = 'item-default';
  163. }
  164. if ($disabled) {
  165. $classes[] = $disabled;
  166. }
  167. $attributes = $this->_prepareOptionAttributes($option, $title, $classes, $disabled);
  168. $html = $this->_getAttributesString($attributes);
  169. $html .= $this->getUiId(isset($option['id']) ? $option['id'] : 'item' . '-' . $key);
  170. return $html;
  171. }
  172. /**
  173. * Checks if the button needs actions-split functionality
  174. *
  175. * If this function returns false then split button will be rendered as simple button
  176. *
  177. * @return bool
  178. */
  179. public function hasSplit()
  180. {
  181. return true;
  182. }
  183. /**
  184. * Add data attributes to $attributes array
  185. *
  186. * @param array $data
  187. * @param array $attributes
  188. */
  189. protected function _getDataAttributes($data, &$attributes)
  190. {
  191. foreach ($data as $key => $attr) {
  192. if (is_scalar($attr)) {
  193. $attributes['data-' . $key] = $attr;
  194. } else {
  195. $attributes['data-' . $key] = json_encode($attr);
  196. }
  197. }
  198. }
  199. /**
  200. * Prepare option attributes
  201. *
  202. * @param array $option
  203. * @param string $title
  204. * @param string $classes
  205. * @param string $disabled
  206. * @return array
  207. * @SuppressWarnings(PHPMD.NPathComplexity)
  208. */
  209. protected function _prepareOptionAttributes($option, $title, $classes, $disabled)
  210. {
  211. $attributes = array(
  212. 'id' => isset($option['id']) ? $this->getId() . '-' . $option['id'] : '',
  213. 'title' => $title,
  214. 'class' => join(' ', $classes),
  215. 'onclick' => isset($option['onclick']) ? $option['onclick'] : '',
  216. 'style' => isset($option['style']) ? $option['style'] : '',
  217. 'disabled' => $disabled,
  218. );
  219. if (isset($option['data_attribute'])) {
  220. $this->_getDataAttributes($option['data_attribute'], $attributes);
  221. }
  222. return $attributes;
  223. }
  224. /**
  225. * Render attributes array as attributes string
  226. *
  227. * @param array $attributes
  228. * @return string
  229. */
  230. protected function _getAttributesString($attributes)
  231. {
  232. /** @var $helper Mage_Backend_Helper_Data */
  233. $helper = $this->helper('Mage_Backend_Helper_Data');
  234. $html = array();
  235. foreach ($attributes as $attributeKey => $attributeValue) {
  236. if ($attributeValue === null || $attributeValue == '') {
  237. continue;
  238. }
  239. $html[] = $attributeKey . '="' . $helper->escapeHtml($attributeValue) . '"';
  240. }
  241. return join(' ', $html);
  242. }
  243. }