/app/code/core/Mage/XmlConnect/Block/Adminhtml/Mobile/Form/Element/Country.php

https://bitbucket.org/acidel/buykoala · PHP · 170 lines · 92 code · 20 blank · 58 comment · 15 complexity · 5983a898d9b062a08b8e112929291607 MD5 · raw file

  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_XmlConnect
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * XmlConnect Country selector form element
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Adminhtml_Mobile_Form_Element_Country
  34. extends Varien_Data_Form_Element_Checkboxes
  35. {
  36. /**
  37. * Flag of using the border in the table's TD
  38. *
  39. * @var bool
  40. */
  41. protected $_useBorderClass = false;
  42. /**
  43. * Init Element
  44. *
  45. * @param array $attributes
  46. */
  47. public function __construct($attributes=array())
  48. {
  49. parent::__construct($attributes);
  50. $this->setType('checkbox');
  51. $this->setExtType('country');
  52. }
  53. /**
  54. * Retrieve HTML
  55. *
  56. * @return string
  57. */
  58. public function getElementHtml()
  59. {
  60. $values = $this->_prepareValues();
  61. if (empty($values)) {
  62. return '';
  63. }
  64. $columns = (int)$this->getData('columns');
  65. $columns = $columns ? $columns : 1;
  66. $rows = ceil(count($values) / $columns);
  67. $row = $column = 0;
  68. $options = array();
  69. foreach ($values as $value) {
  70. if (empty($value['value'])) {
  71. continue;
  72. }
  73. $options[$row++][$column] = $value;
  74. if ($row == $rows) {
  75. $row = 0;
  76. $column++;
  77. }
  78. }
  79. while ($row < $rows) {
  80. $options[$row++][$column] = '';
  81. }
  82. $id = $this->getData('id');
  83. $id = empty($id) ? '' : ' id="' . $id . '-table"';
  84. $class = $this->getData('class');
  85. $html = PHP_EOL . "<table class=\"countries {$class}\"{$id}>" . PHP_EOL;
  86. $zebrine = '';
  87. $stripy = false;
  88. if (strpos($class, 'stripy')) {
  89. $stripy = true;
  90. }
  91. $columns--;
  92. foreach ($options as $row) {
  93. $html .= "<tr{$zebrine}>" . PHP_EOL;
  94. if ($stripy) {
  95. $zebrine = empty($zebrine) ? ' class="odd"' : '';
  96. $this->_useBorderClass = true;
  97. foreach ($row as $idx => $option) {
  98. /**
  99. * for istore (as shown by $stripy) use border settings in TD
  100. */
  101. if ($idx == $columns) {
  102. /**
  103. * for last table's column TD should not have a border
  104. */
  105. $this->_useBorderClass = false;
  106. }
  107. $html .= $this->_optionToHtml($option);
  108. }
  109. } else {
  110. foreach ($row as $option) {
  111. $html .= $this->_optionToHtml($option);
  112. }
  113. }
  114. $html .= PHP_EOL . '</tr>' . PHP_EOL;
  115. }
  116. $html .= '</table>' . PHP_EOL . $this->getAfterElementHtml();
  117. return $html;
  118. }
  119. /**
  120. * Get HTML code for the one option
  121. *
  122. * @param array $option
  123. * @return string
  124. */
  125. protected function _optionToHtml($option)
  126. {
  127. if (empty($option)) {
  128. $html = '<td>&nbsp;</td><td>&nbsp;</td>';
  129. } else {
  130. $id = $this->getHtmlId() . '_' . $this->_escape($option['value']);
  131. $isNameLeft = $this->getData('place_name_left');
  132. $border = $this->_useBorderClass ? ' class="border"' : '';
  133. $html = '<td' . $border . '><input id="' . $id . '"';
  134. foreach ($this->getHtmlAttributes() as $attribute) {
  135. $value = $this->getDataUsingMethod($attribute, $option['value']);
  136. if ($value) {
  137. $html .= ' ' . $attribute . '="' . $value . '"';
  138. }
  139. }
  140. $html .= ' value="' . $option['value'] . '" /></td>';
  141. $label = '<td><label for="' . $id . '" style="white-space: nowrap;">' . $option['label'] . '</label></td>';
  142. if ($isNameLeft) {
  143. $html = $label . $html;
  144. } else {
  145. $html = $html . $label;
  146. }
  147. }
  148. return $html;
  149. }
  150. }