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