PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/code/groupview/SilvercartGroupViewHandler.php

https://bitbucket.org/silvercart/silvercart/
PHP | 355 lines | 137 code | 25 blank | 193 comment | 24 complexity | 9b8903fe8f5676bc6d5d9a00dd808b7b MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010, 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Groupview
  22. */
  23. /**
  24. * handles the base logic for product- and productgroup-visualisation
  25. *
  26. * @package Silvercart
  27. * @subpackage Groupview
  28. * @author Sebastian Diel <sdiel@pixeltricks.de>
  29. * @copyright 2010 pixeltricks GmbH
  30. * @since 14.02.2011
  31. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  32. */
  33. class SilvercartGroupViewHandler {
  34. /**
  35. * a list of possible group view types
  36. *
  37. * @var array
  38. */
  39. protected static $groupViews = array();
  40. /**
  41. * a list of possible group view types
  42. *
  43. * @var array
  44. */
  45. protected static $groupHolderViews = array();
  46. /**
  47. * a list of removed group view types. It is implemented to provide the
  48. * configuration example in _config.php of silvercart.
  49. *
  50. * @var array
  51. */
  52. protected static $removedGroupViews = array();
  53. /**
  54. * a list of removed group view types. It is implemented to provide the
  55. * configuration example in _config.php of silvercart.
  56. *
  57. * @var array
  58. */
  59. protected static $removedGroupHolderViews = array();
  60. /**
  61. * the code of the group view which is choosen by default
  62. *
  63. * @var string
  64. */
  65. protected static $defaultGroupView = null;
  66. /**
  67. * the code of the group view which is choosen by default
  68. *
  69. * @var string
  70. */
  71. protected static $defaultGroupHolderView = null;
  72. /**
  73. * adds a new group view type for product lists to the handler.
  74. *
  75. * @param string $groupView the class name of the group view to add
  76. *
  77. * @return void
  78. *
  79. * @author Sebastian Diel <sdiel@pixeltricks.de>
  80. * @since 14.02.2011
  81. */
  82. public static function addGroupView($groupView) {
  83. if (in_array($groupView, self::$removedGroupViews)) {
  84. return;
  85. }
  86. if (class_exists($groupView)) {
  87. $gv = new $groupView();
  88. self::$groupViews[$gv->getCode()] = $groupView;
  89. }
  90. }
  91. /**
  92. * adds a new group view type for product group lists to the handler.
  93. *
  94. * @param string $groupHolderView the class name of the group view to add
  95. *
  96. * @return void
  97. *
  98. * @author Sebastian Diel <sdiel@pixeltricks.de>
  99. * @since 14.02.2011
  100. */
  101. public static function addGroupHolderView($groupHolderView) {
  102. if (in_array($groupHolderView, self::$removedGroupHolderViews)) {
  103. return;
  104. }
  105. if (class_exists($groupHolderView)) {
  106. $gv = new $groupHolderView();
  107. self::$groupHolderViews[$gv->getCode()] = $groupHolderView;
  108. }
  109. }
  110. /**
  111. * removes a group view for product lists from the handler
  112. *
  113. * @param string $groupView the class name of the group view to remove
  114. *
  115. * @return void
  116. *
  117. * @author Sebastian Diel <sdiel@pixeltricks.de>
  118. * @since 14.02.2011
  119. */
  120. public static function removeGroupView($groupView) {
  121. if (in_array($groupView, self::$removedGroupViews)) {
  122. return;
  123. }
  124. self::$removedGroupViews[] = $groupView;
  125. if (in_array($groupView, self::$groupViews)) {
  126. foreach (self::$groupViews as $index => $value) {
  127. if ($groupView == $value) {
  128. unset (self::$groupViews[$index]);
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * removes a group view for product group lists from the handler
  135. *
  136. * @param string $groupHolderView the class name of the group view to remove
  137. *
  138. * @return void
  139. *
  140. * @author Sebastian Diel <sdiel@pixeltricks.de>
  141. * @since 14.02.2011
  142. */
  143. public static function removeGroupHolderView($groupHolderView) {
  144. if (in_array($groupHolderView, self::$removedGroupHolderViews)) {
  145. return;
  146. }
  147. self::$removedGroupHolderViews[] = $groupHolderView;
  148. if (in_array($groupHolderView, self::$groupHolderViews)) {
  149. foreach (self::$groupHolderViews as $index => $value) {
  150. if ($groupHolderView == $value) {
  151. unset (self::$groupHolderViews[$index]);
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * set the group view to use by default for product lists
  158. *
  159. * @param string $defaultGroupView the class name of the group view to use by default
  160. *
  161. * @return void
  162. */
  163. public static function setDefaultGroupView($defaultGroupView = null) {
  164. if (is_null($defaultGroupView)
  165. || !in_array($defaultGroupView, self::$groupViews)) {
  166. foreach (self::$groupViews as $code => $groupView) {
  167. self::$defaultGroupView = $code;
  168. return;
  169. }
  170. self::addGroupView($defaultGroupView);
  171. }
  172. $tmp = array_flip(self::$groupViews);
  173. self::$defaultGroupView = $tmp[$defaultGroupView];
  174. }
  175. /**
  176. * set the group view to use by default for product group lists
  177. *
  178. * @param string $defaultGroupHolderView the class name of the group view to use by default
  179. *
  180. * @return void
  181. */
  182. public static function setDefaultGroupHolderView($defaultGroupHolderView = null) {
  183. if (is_null($defaultGroupHolderView)
  184. || !in_array($defaultGroupHolderView, self::$groupHolderViews)) {
  185. foreach (self::$groupHolderViews as $code => $groupHolderView) {
  186. self::$defaultGroupHolderView = $code;
  187. return;
  188. }
  189. self::addGroupHolderView($defaultGroupHolderView);
  190. }
  191. $tmp = array_flip(self::$groupHolderViews);
  192. self::$defaultGroupHolderView = $tmp[$defaultGroupHolderView];
  193. }
  194. /**
  195. * returns the class name of the default group view
  196. *
  197. * @return string
  198. */
  199. public static function getDefaultGroupView() {
  200. return self::$defaultGroupView;
  201. }
  202. /**
  203. * returns the class name of the default group view
  204. *
  205. * @return string
  206. */
  207. public static function getDefaultGroupHolderView() {
  208. return self::$defaultGroupHolderView;
  209. }
  210. /**
  211. * sets the group view to the given type
  212. *
  213. * @param string $groupView the code of the group view to set
  214. *
  215. * @return void
  216. *
  217. * @author Sebastian Diel <sdiel@pixeltricks.de>
  218. * @since 10.02.2011
  219. * @see self::$groupViews
  220. */
  221. public static function setGroupView($groupView) {
  222. if (array_key_exists($groupView, self::$groupViews)) {
  223. Session::set('SilvercartGroupView', $groupView);
  224. Session::save();
  225. }
  226. }
  227. /**
  228. * sets the group view to the given type
  229. *
  230. * @param string $groupHolderView the code of the group view to set
  231. *
  232. * @return void
  233. *
  234. * @author Sebastian Diel <sdiel@pixeltricks.de>
  235. * @since 10.02.2011
  236. * @see self::$groupHolderViews
  237. */
  238. public static function setGroupHolderView($groupHolderView) {
  239. if (array_key_exists($groupHolderView, self::$groupHolderViews)) {
  240. Session::set('SilvercartGroupHolderView', $groupHolderView);
  241. Session::save();
  242. }
  243. }
  244. /**
  245. * returns all group views
  246. *
  247. * @return string
  248. */
  249. public static function getGroupViews() {
  250. return self::$groupViews;
  251. }
  252. /**
  253. * returns all group views
  254. *
  255. * @return string
  256. */
  257. public static function getGroupHolderViews() {
  258. return self::$groupHolderViews;
  259. }
  260. /**
  261. * returns the class name of a group view by its code
  262. *
  263. * @param string $code the code of the group view
  264. *
  265. * @return string
  266. */
  267. public static function getGroupView($code) {
  268. if (array_key_exists($code, self::$groupViews)) {
  269. return self::$groupViews[$code];
  270. }
  271. return false;
  272. }
  273. /**
  274. * returns the class name of a group view by its code
  275. *
  276. * @param string $code the code of the group view
  277. *
  278. * @return string
  279. */
  280. public static function getGroupHolderView($code) {
  281. if (array_key_exists($code, self::$groupHolderViews)) {
  282. return self::$groupHolderViews[$code];
  283. }
  284. return false;
  285. }
  286. /**
  287. * return the actual group view
  288. *
  289. * @return string
  290. */
  291. public static function getActiveGroupView() {
  292. if (is_null(Session::get('SilvercartGroupView'))) {
  293. if (is_null(self::getDefaultGroupView())) {
  294. self::setDefaultGroupView();
  295. }
  296. self::setGroupView(self::getDefaultGroupView());
  297. }
  298. return Session::get('SilvercartGroupView');
  299. }
  300. /**
  301. * return the actual group view
  302. *
  303. * @return string
  304. */
  305. public static function getActiveGroupHolderView() {
  306. if (is_null(Session::get('SilvercartGroupHolderView'))) {
  307. if (is_null(self::getDefaultGroupHolderView())) {
  308. self::setDefaultGroupHolderView();
  309. }
  310. self::setGroupHolderView(self::getDefaultGroupHolderView());
  311. }
  312. return Session::get('SilvercartGroupHolderView');
  313. }
  314. /**
  315. * returns the actual group view type as UpperCamelCase
  316. *
  317. * @return string
  318. */
  319. public static function getActiveGroupViewAsUpperCamelCase() {
  320. return strtoupper(substr(self::getActiveGroupView(), 0, 1)) . strtolower(substr(self::getActiveGroupView(), 1));
  321. }
  322. /**
  323. * returns the actual group view type as UpperCamelCase
  324. *
  325. * @return string
  326. */
  327. public static function getActiveGroupHolderViewAsUpperCamelCase() {
  328. return strtoupper(substr(self::getActiveGroupHolderView(), 0, 1)) . strtolower(substr(self::getActiveGroupHolderView(), 1));
  329. }
  330. }