PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/app/code/community/BL/CustomGrid/Block/Widget/Grid/Editor/Renderer/Static/Default.php

https://bitbucket.org/workhousemarc/fuelsmart
PHP | 367 lines | 310 code | 41 blank | 16 comment | 44 complexity | 1840b34298a65a4ab518125d109a4dd2 MD5 | raw file
  1. <?php
  2. /**
  3. * NOTICE OF LICENSE
  4. *
  5. * This source file is subject to the Open Software License (OSL 3.0)
  6. * that is bundled with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://opensource.org/licenses/osl-3.0.php
  9. *
  10. * @category BL
  11. * @package BL_CustomGrid
  12. * @copyright Copyright (c) 2012 BenoƮt Leulliette <benoit.leulliette@gmail.com>
  13. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. */
  15. class BL_CustomGrid_Block_Widget_Grid_Editor_Renderer_Static_Default
  16. extends BL_CustomGrid_Block_Widget_Grid_Editor_Renderer_Abstract
  17. {
  18. protected function _renderTextValue($value, $renderOptions, $formOptions, $fieldType)
  19. {
  20. return ($value !== '' ? htmlspecialchars($value) : '');
  21. }
  22. protected function _renderLongTextValue($value, $renderOptions, $formOptions, $fieldType)
  23. {
  24. return $this->_defaultValue;
  25. }
  26. protected function _getChoiceValues($formOptions, $key)
  27. {
  28. // Must work the same way as edit block (see BL_CustomGrid_Block_Widget_Grid_Form_Static_Default)
  29. $cbKey = $key.'_callback';
  30. $cbParamsKey = $cbKey.'_params';
  31. $choiceValues = null;
  32. if (isset($formOptions[$key])) {
  33. $choiceValues = $formOptions[$key];
  34. } elseif (isset($formOptions[$cbKey])) {
  35. $choiceValues = call_user_func_array(
  36. $formOptions[$cbKey],
  37. (isset($formOptions[$cbParamsKey])
  38. ? (is_array($formOptions[$cbParamsKey]) ? $formOptions[$cbParamsKey] : array())
  39. : array($this->getGridBlockType(), $this->getEditedValue(), $this->getEditParams(), $this->getEditedEntity()))
  40. );
  41. }
  42. return $choiceValues;
  43. }
  44. protected function _sortChoiceValues($a, $b)
  45. {
  46. if (!isset($a['path_id'])) {
  47. $a['path_id'] = '';
  48. }
  49. if (!isset($b['path_id'])) {
  50. $b['path_id'] = '';
  51. }
  52. return (($res = strcmp($a['path_id'], $b['path_id'])) != 0 ? $res : strcmp($a['label'], $b['label']));
  53. }
  54. protected function _renderChoiceValue($value, $renderOptions, $formOptions, $fieldType, $choices, $allowMultiple=false)
  55. {
  56. if (!is_array($value)) {
  57. if ($allowMultiple) {
  58. $value = explode(
  59. (isset($renderOptions['separator']) ? $renderOptions['separator'] : ','),
  60. $value
  61. );
  62. } else {
  63. $value = array($value);
  64. }
  65. }
  66. $selected = array_intersect_key($choices, array_flip($value));
  67. if (isset($renderOptions['without_path'])
  68. && (bool)$renderOptions['without_path']) {
  69. $labels = array();
  70. foreach ($selected as $value) {
  71. $labels[] = $value['label'];
  72. }
  73. $rendered = implode(
  74. (isset($renderOptions['display_separator']) ? $renderOptions['display_separator'] : ', '),
  75. $labels
  76. );
  77. } else {
  78. foreach ($selected as $key => $value) {
  79. $selected[$key] += array(
  80. 'path_id' => '',
  81. 'path_labels' => array(),
  82. );
  83. }
  84. uasort($choices, array($this, '_sortChoiceValues'));
  85. $currentPathId = null;
  86. $currentPath = array();
  87. $currentDepth = 0;
  88. $rendered = array();
  89. $spacesCount = (isset($renderOptions['spaces_count']) ? $renderOptions['spaces_count'] : 3);
  90. $spacesCount = ($spacesCount > 0 ? $spacesCount : 3);
  91. foreach ($selected as $key => $value) {
  92. if (($value['value'] === '') // @todo callback to test for other kinds of emptiness ?
  93. || (isset($renderOptions['with_empty_value'])
  94. && (bool)$renderOptions['with_empty_value'])) {
  95. continue;
  96. }
  97. if ($currentPathId != $value['path_id']) {
  98. $path = explode('_', $value['path_id']);
  99. $depth = count($path);
  100. for ($i=0; $i<$currentDepth; $i++) {
  101. if ($path[$i] != $currentPath[$i]) {
  102. break;
  103. }
  104. }
  105. for ($j=$i; $j<$depth; $j++) {
  106. $rendered[] = str_repeat('&nbsp;', $j*$spacesCount).$value['path_labels'][$j];
  107. }
  108. $currentPath = $path;
  109. $currentDepth = $depth;
  110. }
  111. $rendered[] = str_repeat('&nbsp;', $currentDepth*$spacesCount).$value['label'];
  112. }
  113. $rendered = implode('<br />', $rendered); // @todo separator, etc... (=> tax rules / ..)
  114. }
  115. return $rendered;
  116. }
  117. protected function _renderCheckboxesValue($value, $renderOptions, $formOptions, $fieldType)
  118. {
  119. $options = array();
  120. $values = array();
  121. if (!is_null($choices = $this->_getChoiceValues($formOptions, 'values'))) {
  122. $options = (!is_array($choices) ? array($choices) : $choices);
  123. } elseif (is_array($choices = $this->_getChoiceValues($formOptions, 'options'))) {
  124. $options = $choices;
  125. }
  126. foreach ($options as $k => $v) {
  127. if (is_string($v)) {
  128. $values[] = array('label' => $v, 'value' => $k);
  129. } elseif (isset($v['value'])) {
  130. if (!isset($v['label'])) {
  131. $v['label'] = $v['value'];
  132. }
  133. $values[] = array('label' => $v['label'], 'value' => $v['value']);
  134. }
  135. }
  136. return $this->_renderChoiceValue($value, $renderOptions, $formOptions, $fieldType, $values);
  137. }
  138. protected function _renderMultiSelectValue($value, $renderOptions, $formOptions, $fieldType)
  139. {
  140. $values = array();
  141. if (is_array($choices = $this->_getChoiceValues($formOptions, 'values'))) {
  142. $pathsCount = 1;
  143. foreach ($choices as $v) {
  144. if (is_array($v['value'])) {
  145. foreach ($v['value'] as $subValue) {
  146. $values[$subValue['value']] = array(
  147. 'value' => $subValue['value'],
  148. 'label' => $subValue['label'],
  149. 'path_id' => $pathsCount,
  150. 'path_labels' => array($v['label']),
  151. );
  152. }
  153. $pathsCount++;
  154. } else {
  155. $values[$v['value']] = array(
  156. 'value' => $v['value'],
  157. 'label' => $v['label'],
  158. );
  159. }
  160. }
  161. }
  162. return $this->_renderChoiceValue($value, $renderOptions, $formOptions, $fieldType, $values, true);
  163. }
  164. protected function _renderRadiosValue($value, $renderOptions, $formOptions, $fieldType)
  165. {
  166. $values = array();
  167. if (is_array($choices = $this->_getChoiceValues($formOptions, 'values'))) {
  168. foreach ($choices as $option) {
  169. if (is_array($option)) {
  170. $values[$option['value']] = array(
  171. 'value' => $option['value'],
  172. 'label' => $option['label']
  173. );
  174. } elseif ($option instanceof Varien_Object) {
  175. $values[$option->getValue()] = array(
  176. 'value' => $option->getValue(),
  177. 'label' => $option->getLabel(),
  178. );
  179. }
  180. }
  181. }
  182. return $this->_renderChoiceValue($value, $renderOptions, $formOptions, $fieldType, $values, true);
  183. }
  184. protected function _collectSelectValues($options, $pathId, $pathLabels)
  185. {
  186. $values = array();
  187. $pathsCount = 0;
  188. foreach ($options as $option) {
  189. if (is_array($option['value'])) {
  190. array_unshift($pathLabels, $option['label']);
  191. $values = array_merge(
  192. $values,
  193. $this->_collectSelectValues(
  194. $option['value'],
  195. $pathId.'_'.(++$pathsCount),
  196. $pathLabels
  197. )
  198. );
  199. array_pop($pathLabels);
  200. } else {
  201. $values[$option['value']] = array(
  202. 'value' => $option['value'],
  203. 'label' => $option['label'],
  204. 'path_id' => $pathId,
  205. 'path_labels' => $pathLabels,
  206. );
  207. }
  208. }
  209. return $values;
  210. }
  211. protected function _renderSelectValue($value, $renderOptions, $formOptions, $fieldType)
  212. {
  213. $values = array();
  214. if (!is_array($choices = $this->_getChoiceValues($formOptions, 'values')) || empty($choices)) {
  215. if (!is_null($choices = $this->_getChoiceValues($formOptions, 'options'))) {
  216. if (is_array($choices)) {
  217. foreach ($choices as $choiceValue => $label) {
  218. $values[$choiceValue] = array('value' => $choiceValue, 'label' => $label);
  219. }
  220. } elseif (is_string($choices)) {
  221. $values[$choices] = array(
  222. 'value' => $choices,
  223. 'label' => $choices,
  224. );
  225. }
  226. }
  227. } else {
  228. $pathsCount = 0;
  229. foreach ($choices as $key => $option) {
  230. if (!is_array($option)) {
  231. $values[$key] = array(
  232. 'value' => $key,
  233. 'label' => $option,
  234. );
  235. } elseif (is_array($option['value'])) {
  236. $values = array_merge(
  237. $values,
  238. $this->_collectSelectValues(
  239. $option['value'],
  240. ++$pathsCount,
  241. array($option['label'])
  242. )
  243. );
  244. } else {
  245. $values[$option['value']] = array(
  246. 'value' => $option['value'],
  247. 'label' => $option['label'],
  248. );
  249. }
  250. }
  251. }
  252. return $this->_renderChoiceValue($value, $renderOptions, $formOptions, $fieldType, $values);
  253. }
  254. protected function _renderDateValue($value, $renderOptions, $formOptions, $fieldType)
  255. {
  256. if (empty($value)) {
  257. return '';
  258. }
  259. if (!($value instanceof Zend_Date)) {
  260. if (preg_match('/^[0-9]+$/', $value)) {
  261. if ((int)$value > 3155760000) {
  262. $value = 0;
  263. }
  264. $value = new Zend_Date($this->_toTimestamp((int)$value));
  265. } else {
  266. $format = (isset($renderOptions['input_format'])
  267. ? $renderOptions['input_format']
  268. : Varien_Date::DATETIME_INTERNAL_FORMAT);
  269. $locale = (isset($renderOptions['input_locale']) ? $renderOptions['input_locale'] : null);
  270. try {
  271. $value = new Zend_Date($value, $format, $locale);
  272. } catch (Exception $e) {
  273. return $this->_defaultValue;
  274. }
  275. }
  276. }
  277. if (isset($renderOptions['output_format'])) {
  278. $format = $renderOptions['output_format'];
  279. } else {
  280. $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  281. }
  282. $locale = (isset($renderOptions['output_locale']) ? $renderOptions['output_locale'] : null);
  283. return $value->toString($format, null, $locale);
  284. }
  285. protected function _getRenderedValue()
  286. {
  287. // @todo whenever needed, make (- a far more powerful -) use of render options :)
  288. $editedConfig = $this->getEditedConfig();
  289. $fieldType = $editedConfig['type'];
  290. $renderOptions = $editedConfig['renderer'];
  291. $formOptions = $editedConfig['form'];
  292. $renderableValue = $this->getRenderableValue();
  293. $renderedValue = '';
  294. switch ($fieldType) {
  295. case 'checkboxes':
  296. $renderedValue = $this->_renderCheckboxesValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  297. break;
  298. case 'date':
  299. $renderedValue = $this->_renderDateValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  300. break;
  301. case 'radios':
  302. $renderedValue = $this->_renderRadiosValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  303. break;
  304. case 'select':
  305. $renderedValue = $this->_renderSelectValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  306. break;
  307. case 'multiselect':
  308. $renderedValue = $this->_renderMultiSelectValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  309. break;
  310. case 'checkbox':
  311. case 'radio':
  312. // @todo ? (don't think this is ever used)
  313. $renderedValue = $this->_defaultValue;
  314. break;
  315. case 'editor':
  316. case 'textarea':
  317. $renderedValue = $this->_renderLongTextValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  318. break;
  319. case 'text':
  320. default:
  321. $renderedValue = $this->_renderTextValue($renderableValue, $renderOptions, $formOptions, $fieldType);
  322. break;
  323. }
  324. return $renderedValue;
  325. }
  326. }