Complex classes like ProductGroup_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductGroup_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 1666 | * @return array |
||
| 1667 | */ |
||
| 1668 | public function searchResultsArrayFromSession() |
||
| 1669 | { |
||
| 1670 | if (! isset(self::$_result_array[$this->ID]) || self::$_result_array[$this->ID] === null) { |
||
| 1671 | self::$_result_array[$this->ID] = explode(',', Session::get($this->SearchResultsSessionVariable(false))); |
||
| 1672 | } |
||
| 1673 | if (! is_array(self::$_result_array[$this->ID]) || ! count(self::$_result_array[$this->ID])) { |
||
| 1674 | self::$_result_array[$this->ID] = array(0 => 0); |
||
| 1675 | } |
||
| 1676 | |||
| 1677 | return self::$_result_array[$this->ID]; |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | public function getNumberOfProducts() |
||
| 1681 | { |
||
| 1682 | return Product::get()->filter(array('ParentID' => $this->ID))->count(); |
||
| 1683 | } |
||
| 1684 | } |
||
| 1685 | |||
| 1686 | class ProductGroup_Controller extends Page_Controller |
||
| 1687 | { |
||
| 1688 | /** |
||
| 1689 | * standard SS variable. |
||
| 1690 | * |
||
| 1691 | * @var array |
||
| 1692 | */ |
||
| 1693 | private static $allowed_actions = array( |
||
| 1694 | 'debug' => 'ADMIN', |
||
| 1695 | 'filterforgroup' => true, |
||
| 1696 | 'ProductSearchForm' => true, |
||
| 1697 | 'searchresults' => true, |
||
| 1698 | 'resetfilter' => true, |
||
| 1699 | ); |
||
| 1700 | |||
| 1701 | /** |
||
| 1702 | * The original Title of this page before filters, etc... |
||
| 1703 | * |
||
| 1704 | * @var string |
||
| 1705 | */ |
||
| 1706 | protected $originalTitle = ''; |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * list of products that are going to be shown. |
||
| 1710 | * |
||
| 1711 | * @var DataList |
||
| 1712 | */ |
||
| 1713 | protected $products = null; |
||
| 1714 | |||
| 1715 | /** |
||
| 1716 | * Show all products on one page? |
||
| 1717 | * |
||
| 1718 | * @var bool |
||
| 1719 | */ |
||
| 1720 | protected $showFullList = false; |
||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * The group filter that is applied to this page. |
||
| 1724 | * |
||
| 1725 | * @var ProductGroup |
||
| 1726 | */ |
||
| 1727 | protected $filterForGroupObject = null; |
||
| 1728 | |||
| 1729 | /** |
||
| 1730 | * Is this a product search? |
||
| 1731 | * |
||
| 1732 | * @var bool |
||
| 1733 | */ |
||
| 1734 | protected $isSearchResults = false; |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * standard SS method. |
||
| 1738 | */ |
||
| 1739 | public function init() |
||
| 1740 | { |
||
| 1741 | parent::init(); |
||
| 1742 | $this->originalTitle = $this->Title; |
||
| 1743 | Requirements::themedCSS('ProductGroup', 'ecommerce'); |
||
| 1744 | Requirements::themedCSS('ProductGroupPopUp', 'ecommerce'); |
||
| 1745 | Requirements::javascript('ecommerce/javascript/EcomProducts.js'); |
||
| 1746 | //we save data from get variables... |
||
| 1747 | $this->saveUserPreferences(); |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | /**************************************************** |
||
| 1751 | * ACTIONS |
||
| 1752 | /****************************************************/ |
||
| 1753 | |||
| 1754 | /** |
||
| 1755 | * standard selection of products. |
||
| 1756 | */ |
||
| 1757 | public function index() |
||
| 1758 | { |
||
| 1759 | //set the filter and the sort... |
||
| 1760 | $this->addSecondaryTitle(); |
||
| 1761 | $this->products = $this->paginateList($this->ProductsShowable(null)); |
||
| 1762 | if ($this->returnAjaxifiedProductList()) { |
||
| 1763 | return $this->renderWith('AjaxProductList'); |
||
| 1764 | } |
||
| 1765 | return array(); |
||
| 1766 | } |
||
| 1767 | |||
| 1768 | /** |
||
| 1769 | * cross filter with another product group.. |
||
| 1770 | * |
||
| 1771 | * e.g. socks (current product group) for brand A or B (the secondary product group) |
||
| 1772 | * |
||
| 1773 | * @param HTTPRequest |
||
| 1774 | */ |
||
| 1775 | public function filterforgroup($request) |
||
| 1776 | { |
||
| 1777 | $this->resetfilter(); |
||
| 1778 | $otherGroupURLSegment = Convert::raw2sql($request->param('ID')); |
||
| 1779 | $arrayOfIDs = array(0 => 0); |
||
| 1780 | if ($otherGroupURLSegment) { |
||
| 1781 | $otherProductGroup = ProductGroup::get()->filter(array('URLSegment' => $otherGroupURLSegment))->first(); |
||
| 1782 | if ($otherProductGroup) { |
||
| 1783 | $this->filterForGroupObject = $otherProductGroup; |
||
| 1784 | $arrayOfIDs = $otherProductGroup->currentInitialProductsAsCachedArray($this->getMyUserPreferencesDefault('FILTER')); |
||
| 1785 | } |
||
| 1786 | } |
||
| 1787 | $this->addSecondaryTitle(); |
||
| 1788 | $this->products = $this->paginateList($this->ProductsShowable(array('ID' => $arrayOfIDs))); |
||
| 1789 | if ($this->returnAjaxifiedProductList()) { |
||
| 1790 | return $this->renderWith('AjaxProductList'); |
||
| 1791 | } |
||
| 1792 | |||
| 1793 | return array(); |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * get the search results. |
||
| 1798 | * |
||
| 1799 | * @param HTTPRequest |
||
| 1800 | */ |
||
| 1801 | public function searchresults($request) |
||
| 1802 | { |
||
| 1803 | $this->resetfilter(); |
||
| 1804 | $this->isSearchResults = true; |
||
| 1805 | //reset filter and sort |
||
| 1806 | $resultArray = $this->searchResultsArrayFromSession(); |
||
| 1807 | if (!$resultArray || !count($resultArray)) { |
||
| 1808 | $resultArray = array(0 => 0); |
||
| 1809 | } |
||
| 1810 | $defaultKeySort = $this->getMyUserPreferencesDefault('SORT'); |
||
| 1811 | $myKeySort = $this->getCurrentUserPreferences('SORT'); |
||
| 1812 | $searchArray = null; |
||
| 1813 | if ($defaultKeySort == $myKeySort) { |
||
| 1814 | $searchArray = $resultArray; |
||
| 1815 | } |
||
| 1816 | $this->addSecondaryTitle(); |
||
| 1817 | $this->products = $this->paginateList($this->ProductsShowable(array('ID' => $resultArray), $searchArray)); |
||
| 1818 | |||
| 1819 | return array(); |
||
| 1820 | } |
||
| 1821 | |||
| 1822 | /** |
||
| 1823 | * resets the filter only. |
||
| 1824 | */ |
||
| 1825 | public function resetfilter() |
||
| 1826 | { |
||
| 1827 | $defaultKey = $this->getMyUserPreferencesDefault('FILTER'); |
||
| 1828 | $filterGetVariable = $this->getSortFilterDisplayNames('FILTER', 'getVariable'); |
||
| 1829 | $this->saveUserPreferences( |
||
| 1830 | array( |
||
| 1831 | $filterGetVariable => $defaultKey, |
||
| 1832 | ) |
||
| 1833 | ); |
||
| 1834 | |||
| 1835 | return array(); |
||
| 1836 | } |
||
| 1837 | |||
| 1838 | /**************************************************** |
||
| 1839 | * TEMPLATE METHODS PRODUCTS |
||
| 1840 | /****************************************************/ |
||
| 1841 | |||
| 1842 | /** |
||
| 1843 | * Return the products for this group. |
||
| 1844 | * This is the call that is made from the template... |
||
| 1845 | * The actual final products being shown. |
||
| 1846 | * |
||
| 1847 | * @return PaginatedList |
||
| 1848 | **/ |
||
| 1849 | public function Products() |
||
| 1850 | { |
||
| 1851 | //IMPORTANT! |
||
| 1852 | //two universal actions! |
||
| 1853 | $this->addSecondaryTitle(); |
||
| 1854 | $this->cachingRelatedJavascript(); |
||
| 1855 | |||
| 1856 | //save products to session for later use |
||
| 1857 | $stringOfIDs = ''; |
||
| 1858 | $array = $this->getProductsThatCanBePurchasedArray(); |
||
| 1859 | if (is_array($array)) { |
||
| 1860 | $stringOfIDs = implode(',', $array); |
||
| 1861 | } |
||
| 1862 | //save list for future use |
||
| 1863 | Session::set(EcommerceConfig::get('ProductGroup', 'session_name_for_product_array'), $stringOfIDs); |
||
| 1864 | |||
| 1865 | return $this->products; |
||
| 1866 | } |
||
| 1867 | |||
| 1868 | /** |
||
| 1869 | * you can overload this function of ProductGroup Extensions. |
||
| 1870 | * |
||
| 1871 | * @return bool |
||
| 1872 | */ |
||
| 1873 | protected function returnAjaxifiedProductList() |
||
| 1874 | { |
||
| 1875 | return Director::is_ajax() ? true : false; |
||
| 1876 | } |
||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * is the product list cache-able? |
||
| 1880 | * |
||
| 1881 | * @return bool |
||
| 1882 | */ |
||
| 1883 | public function ProductGroupListAreCacheable() |
||
| 1884 | { |
||
| 1885 | if ($this->productListsHTMLCanBeCached()) { |
||
| 1886 | //exception 1 |
||
| 1887 | if ($this->IsSearchResults()) { |
||
| 1888 | return false; |
||
| 1889 | } |
||
| 1890 | //exception 2 |
||
| 1891 | $currentOrder = ShoppingCart::current_order(); |
||
| 1892 | if ($currentOrder->getHasAlternativeCurrency()) { |
||
| 1893 | return false; |
||
| 1894 | } |
||
| 1895 | //can be cached... |
||
| 1896 | return true; |
||
| 1897 | } |
||
| 1898 | |||
| 1899 | return false; |
||
| 1900 | } |
||
| 1901 | |||
| 1902 | /** |
||
| 1903 | * is the product list ajaxified. |
||
| 1904 | * |
||
| 1905 | * @return bool |
||
| 1906 | */ |
||
| 1907 | public function ProductGroupListAreAjaxified() |
||
| 1908 | { |
||
| 1909 | return $this->IsSearchResults() ? false : true; |
||
| 1910 | } |
||
| 1911 | |||
| 1912 | /** |
||
| 1913 | * Unique caching key for the product list... |
||
| 1914 | * |
||
| 1915 | * @return string | Null |
||
| 1916 | */ |
||
| 1917 | public function ProductGroupListCachingKey() |
||
| 1918 | { |
||
| 1919 | if ($this->ProductGroupListAreCacheable()) { |
||
| 1920 | $displayKey = $this->getCurrentUserPreferences('DISPLAY'); |
||
| 1921 | $filterKey = $this->getCurrentUserPreferences('FILTER'); |
||
| 1922 | $filterForGroupKey = $this->filterForGroupObject ? $this->filterForGroupObject->ID : 0; |
||
| 1923 | $sortKey = $this->getCurrentUserPreferences('SORT'); |
||
| 1924 | $pageStart = isset($_GET['start']) ? intval($_GET['start']) : 0; |
||
| 1925 | $isFullList = $this->IsShowFullList() ? 'Y' : 'N'; |
||
| 1926 | |||
| 1927 | $this->cacheKey( |
||
| 1928 | implode( |
||
| 1929 | '_', |
||
| 1930 | array( |
||
| 1931 | $displayKey, |
||
| 1932 | $filterKey, |
||
| 1933 | $filterForGroupKey, |
||
| 1934 | $sortKey, |
||
| 1935 | $pageStart, |
||
| 1936 | $isFullList, |
||
| 1937 | ) |
||
| 1938 | ) |
||
| 1939 | ); |
||
| 1940 | } |
||
| 1941 | |||
| 1942 | return; |
||
| 1943 | } |
||
| 1944 | |||
| 1945 | /** |
||
| 1946 | * adds Javascript to the page to make it work when products are cached. |
||
| 1947 | */ |
||
| 1948 | public function CachingRelatedJavascript() |
||
| 1949 | { |
||
| 1950 | if ($this->ProductGroupListAreAjaxified()) { |
||
| 1951 | Requirements::customScript(" |
||
| 1952 | if(typeof EcomCartOptions === 'undefined') { |
||
| 1953 | var EcomCartOptions = {}; |
||
| 1954 | } |
||
| 1955 | EcomCartOptions.ajaxifyProductList = true; |
||
| 1956 | EcomCartOptions.ajaxifiedListHolderSelector = '#".$this->AjaxDefinitions()->ProductListHolderID()."'; |
||
| 1957 | EcomCartOptions.ajaxifiedListAdjusterSelectors = '.".$this->AjaxDefinitions()->ProductListAjaxifiedLinkClassName()."'; |
||
| 1958 | EcomCartOptions.hiddenPageTitleID = '#".$this->AjaxDefinitions()->HiddenPageTitleID()."'; |
||
| 1959 | ", |
||
| 1960 | 'cachingRelatedJavascript_AJAXlist' |
||
| 1961 | ); |
||
| 1962 | } else { |
||
| 1963 | Requirements::customScript(" |
||
| 1964 | if(typeof EcomCartOptions === 'undefined') { |
||
| 1965 | var EcomCartOptions = {}; |
||
| 1966 | } |
||
| 1967 | EcomCartOptions.ajaxifyProductList = false; |
||
| 1968 | ", |
||
| 1969 | 'cachingRelatedJavascript_AJAXlist' |
||
| 1970 | ); |
||
| 1971 | } |
||
| 1972 | $currentOrder = ShoppingCart::current_order(); |
||
| 1973 | if ($currentOrder->TotalItems(true)) { |
||
| 1974 | $responseClass = EcommerceConfig::get('ShoppingCart', 'response_class'); |
||
| 1975 | $obj = new $responseClass(); |
||
| 1976 | $obj->setIncludeHeaders(false); |
||
| 1977 | $json = $obj->ReturnCartData(); |
||
| 1978 | Requirements::customScript(" |
||
| 1979 | if(typeof EcomCartOptions === 'undefined') { |
||
| 1980 | var EcomCartOptions = {}; |
||
| 1981 | } |
||
| 1982 | EcomCartOptions.initialData= ".$json."; |
||
| 1983 | ", |
||
| 1984 | 'cachingRelatedJavascript_JSON' |
||
| 1985 | ); |
||
| 1986 | } |
||
| 1987 | } |
||
| 1988 | |||
| 1989 | /** |
||
| 1990 | * you can overload this function of ProductGroup Extensions. |
||
| 1991 | * |
||
| 1992 | * @return bool |
||
| 1993 | */ |
||
| 1994 | protected function productListsHTMLCanBeCached() |
||
| 1995 | { |
||
| 1996 | return Config::inst()->get('ProductGroup', 'actively_check_for_can_purchase') ? false : true; |
||
| 1997 | } |
||
| 1998 | |||
| 1999 | /***************************************************** |
||
| 2000 | * DATALIST: totals, number per page, etc.. |
||
| 2001 | *****************************************************/ |
||
| 2002 | |||
| 2003 | /** |
||
| 2004 | * returns the total numer of products (before pagination). |
||
| 2005 | * |
||
| 2006 | * @return bool |
||
| 2007 | **/ |
||
| 2008 | public function TotalCountGreaterThanOne($greaterThan = 1) |
||
| 2009 | { |
||
| 2010 | return $this->TotalCount() > $greaterThan; |
||
| 2011 | } |
||
| 2012 | |||
| 2013 | /** |
||
| 2014 | * have the ProductsShowable been limited. |
||
| 2015 | * |
||
| 2016 | * @return bool |
||
| 2017 | **/ |
||
| 2018 | public function TotalCountGreaterThanMax() |
||
| 2019 | { |
||
| 2020 | return $this->RawCount() > $this->TotalCount(); |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | /**************************************************** |
||
| 2024 | * TEMPLATE METHODS MENUS AND SIDEBARS |
||
| 2025 | /****************************************************/ |
||
| 2026 | |||
| 2027 | /** |
||
| 2028 | * title without additions. |
||
| 2029 | * |
||
| 2030 | * @return string |
||
| 2031 | */ |
||
| 2032 | public function OriginalTitle() |
||
| 2033 | { |
||
| 2034 | return $this->originalTitle; |
||
| 2035 | } |
||
| 2036 | /** |
||
| 2037 | * This method can be extended to show products in the side bar. |
||
| 2038 | */ |
||
| 2039 | public function SidebarProducts() |
||
| 2040 | { |
||
| 2041 | return; |
||
| 2042 | } |
||
| 2043 | |||
| 2044 | /** |
||
| 2045 | * returns child product groups for use in |
||
| 2046 | * 'in this section'. For example the vegetable Product Group |
||
| 2047 | * May have listed here: Carrot, Cabbage, etc... |
||
| 2048 | * |
||
| 2049 | * @return ArrayList (ProductGroups) |
||
| 2050 | */ |
||
| 2051 | public function MenuChildGroups() |
||
| 2052 | { |
||
| 2053 | return $this->ChildGroups(2, '"ShowInMenus" = 1'); |
||
| 2054 | } |
||
| 2055 | |||
| 2056 | /** |
||
| 2057 | * After a search is conducted you may end up with a bunch |
||
| 2058 | * of recommended product groups. They will be returned here... |
||
| 2059 | * We sort the list in the order that it is provided. |
||
| 2060 | * |
||
| 2061 | * @return DataList | Null (ProductGroups) |
||
| 2062 | */ |
||
| 2063 | public function SearchResultsChildGroups() |
||
| 2064 | { |
||
| 2065 | $groupArray = explode(',', Session::get($this->SearchResultsSessionVariable($isForGroup = true))); |
||
| 2066 | if (is_array($groupArray) && count($groupArray)) { |
||
| 2067 | $sortStatement = $this->createSortStatementFromIDArray($groupArray, 'ProductGroup'); |
||
| 2068 | |||
| 2069 | return ProductGroup::get()->filter(array('ID' => $groupArray, 'ShowInSearch' => 1))->sort($sortStatement); |
||
| 2070 | } |
||
| 2071 | |||
| 2072 | return; |
||
| 2073 | } |
||
| 2074 | |||
| 2075 | /**************************************************** |
||
| 2076 | * Search Form Related controllers |
||
| 2077 | /****************************************************/ |
||
| 2078 | |||
| 2079 | /** |
||
| 2080 | * returns a search form to search current products. |
||
| 2081 | * |
||
| 2082 | * @return ProductSearchForm object |
||
| 2083 | */ |
||
| 2084 | public function ProductSearchForm() |
||
| 2085 | { |
||
| 2086 | $onlySearchTitle = $this->originalTitle; |
||
| 2087 | if ($this->dataRecord instanceof ProductGroupSearchPage) { |
||
| 2088 | if ($this->HasSearchResults()) { |
||
| 2089 | $onlySearchTitle = 'Last Search Results'; |
||
| 2090 | } |
||
| 2091 | } |
||
| 2092 | $form = ProductSearchForm::create( |
||
| 2093 | $this, |
||
| 2094 | 'ProductSearchForm', |
||
| 2095 | $onlySearchTitle, |
||
| 2096 | $this->currentInitialProducts(null, $this->getMyUserPreferencesDefault('FILTER')) |
||
| 2097 | ); |
||
| 2098 | $filterGetVariable = $this->getSortFilterDisplayNames('FILTER', 'getVariable'); |
||
| 2099 | $sortGetVariable = $this->getSortFilterDisplayNames('SORT', 'getVariable'); |
||
| 2100 | $additionalGetParameters = $filterGetVariable.'='.$this->getMyUserPreferencesDefault('FILTER').'&'. |
||
| 2101 | $sortGetVariable.'='.$this->getMyUserPreferencesDefault('SORT'); |
||
| 2102 | $form->setAdditionalGetParameters($additionalGetParameters); |
||
| 2103 | |||
| 2104 | return $form; |
||
| 2105 | } |
||
| 2106 | |||
| 2107 | /** |
||
| 2108 | * Does this page have any search results? |
||
| 2109 | * If search was carried out without returns |
||
| 2110 | * then it returns zero (false). |
||
| 2111 | * |
||
| 2112 | * @return int | false |
||
| 2113 | */ |
||
| 2114 | public function HasSearchResults() |
||
| 2115 | { |
||
| 2116 | $resultArray = $this->searchResultsArrayFromSession(); |
||
| 2117 | if ($resultArray) { |
||
| 2118 | $count = count($resultArray) - 1; |
||
| 2119 | |||
| 2120 | return $count ? $count : 0; |
||
| 2121 | } |
||
| 2122 | |||
| 2123 | return 0; |
||
| 2124 | } |
||
| 2125 | |||
| 2126 | /** |
||
| 2127 | * Should the product search form be shown immediately? |
||
| 2128 | * |
||
| 2129 | * @return bool |
||
| 2130 | */ |
||
| 2131 | public function ShowSearchFormImmediately() |
||
| 2132 | { |
||
| 2133 | if ($this->IsSearchResults()) { |
||
| 2134 | return true; |
||
| 2135 | } |
||
| 2136 | if ((!$this->products) || ($this->products && $this->products->count())) { |
||
| 2137 | return false; |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | return true; |
||
| 2141 | } |
||
| 2142 | |||
| 2143 | /** |
||
| 2144 | * Show a search form on this page? |
||
| 2145 | * |
||
| 2146 | * @return bool |
||
| 2147 | */ |
||
| 2148 | public function ShowSearchFormAtAll() |
||
| 2149 | { |
||
| 2150 | return true; |
||
| 2151 | } |
||
| 2152 | |||
| 2153 | /** |
||
| 2154 | * Is the current page a display of search results. |
||
| 2155 | * |
||
| 2156 | * This does not mean that something is actively being search for, |
||
| 2157 | * it could also be just "showing the search results" |
||
| 2158 | * |
||
| 2159 | * @return bool |
||
| 2160 | */ |
||
| 2161 | public function IsSearchResults() |
||
| 2162 | { |
||
| 2163 | return $this->isSearchResults; |
||
| 2164 | } |
||
| 2165 | |||
| 2166 | /** |
||
| 2167 | * Is there something actively being searched for? |
||
| 2168 | * |
||
| 2169 | * This is different from IsSearchResults. |
||
| 2170 | * |
||
| 2171 | * @return bool |
||
| 2172 | */ |
||
| 2173 | public function ActiveSearchTerm() |
||
| 2174 | { |
||
| 2175 | $data = Session::get(Config::inst()->get('ProductSearchForm', 'form_data_session_variable')); |
||
| 2176 | if (!empty($data['Keyword'])) { |
||
| 2177 | return $this->IsSearchResults(); |
||
| 2178 | } |
||
| 2179 | } |
||
| 2180 | |||
| 2181 | /**************************************************** |
||
| 2182 | * Filter / Sort / Display related controllers |
||
| 2183 | /****************************************************/ |
||
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Do we show all products on one page? |
||
| 2187 | * |
||
| 2188 | * @return bool |
||
| 2189 | */ |
||
| 2190 | public function ShowFiltersAndDisplayLinks() |
||
| 2191 | { |
||
| 2192 | if ($this->TotalCountGreaterThanOne()) { |
||
| 2193 | if ($this->HasFilters()) { |
||
| 2194 | return true; |
||
| 2195 | } |
||
| 2196 | if ($this->DisplayLinks()) { |
||
| 2197 | return true; |
||
| 2198 | } |
||
| 2199 | } |
||
| 2200 | |||
| 2201 | return false; |
||
| 2202 | } |
||
| 2203 | |||
| 2204 | /** |
||
| 2205 | * Do we show the sort links. |
||
| 2206 | * |
||
| 2207 | * A bit arbitrary to say three, |
||
| 2208 | * but there is not much point to sort three or less products |
||
| 2209 | * |
||
| 2210 | * @return bool |
||
| 2211 | */ |
||
| 2212 | public function ShowSortLinks($minimumCount = 3) |
||
| 2213 | { |
||
| 2214 | if ($this->TotalCountGreaterThanOne($minimumCount)) { |
||
| 2215 | return true; |
||
| 2216 | } |
||
| 2217 | |||
| 2218 | return false; |
||
| 2219 | } |
||
| 2220 | |||
| 2221 | /** |
||
| 2222 | * Is there a special filter operating at the moment? |
||
| 2223 | * Is the current filter the default one (return inverse!)? |
||
| 2224 | * |
||
| 2225 | * @return bool |
||
| 2226 | */ |
||
| 2227 | public function HasFilter() |
||
| 2228 | { |
||
| 2229 | return $this->getCurrentUserPreferences('FILTER') != $this->getMyUserPreferencesDefault('FILTER') |
||
| 2230 | || $this->filterForGroupObject; |
||
| 2231 | } |
||
| 2232 | |||
| 2233 | /** |
||
| 2234 | * Is there a special sort operating at the moment? |
||
| 2235 | * Is the current sort the default one (return inverse!)? |
||
| 2236 | * |
||
| 2237 | * @return bool |
||
| 2238 | */ |
||
| 2239 | public function HasSort() |
||
| 2240 | { |
||
| 2241 | $sort = $this->getCurrentUserPreferences('SORT'); |
||
| 2242 | if ($sort != $this->getMyUserPreferencesDefault('SORT')) { |
||
| 2243 | return true; |
||
| 2244 | } |
||
| 2245 | } |
||
| 2246 | |||
| 2247 | /** |
||
| 2248 | * @return boolean |
||
| 2249 | */ |
||
| 2250 | public function HasFilterOrSort() |
||
| 2251 | { |
||
| 2252 | return $this->HasFilter() || $this->HasSort(); |
||
| 2253 | } |
||
| 2254 | |||
| 2255 | /** |
||
| 2256 | * @return boolean |
||
| 2257 | */ |
||
| 2258 | public function HasFilterOrSortFullList() |
||
| 2259 | { |
||
| 2260 | return $this->HasFilterOrSort() || $this->IsShowFullList(); |
||
| 2261 | } |
||
| 2262 | |||
| 2263 | /** |
||
| 2264 | * are filters available? |
||
| 2265 | * we check one at the time so that we do the least |
||
| 2266 | * amount of DB queries. |
||
| 2267 | * |
||
| 2268 | * @return bool |
||
| 2269 | */ |
||
| 2270 | public function HasFilters() |
||
| 2271 | { |
||
| 2272 | $countFilters = $this->FilterLinks()->count(); |
||
| 2273 | if ($countFilters > 1) { |
||
| 2274 | return true; |
||
| 2275 | } |
||
| 2276 | $countGroupFilters = $this->ProductGroupFilterLinks()->count(); |
||
| 2277 | if ($countGroupFilters > 1) { |
||
| 2278 | return true; |
||
| 2279 | } |
||
| 2280 | if ($countFilters + $countGroupFilters > 1) { |
||
| 2281 | return true; |
||
| 2282 | } |
||
| 2283 | |||
| 2284 | return false; |
||
| 2285 | } |
||
| 2286 | |||
| 2287 | /** |
||
| 2288 | * Do we show all products on one page? |
||
| 2289 | * |
||
| 2290 | * @return bool |
||
| 2291 | */ |
||
| 2292 | public function IsShowFullList() |
||
| 2293 | { |
||
| 2294 | return $this->showFullList; |
||
| 2295 | } |
||
| 2296 | |||
| 2297 | /** |
||
| 2298 | * returns the current filter applied to the list |
||
| 2299 | * in a human readable string. |
||
| 2300 | * |
||
| 2301 | * @return string |
||
| 2302 | */ |
||
| 2303 | public function CurrentDisplayTitle() |
||
| 2304 | { |
||
| 2305 | $displayKey = $this->getCurrentUserPreferences('DISPLAY'); |
||
| 2306 | if ($displayKey != $this->getMyUserPreferencesDefault('DISPLAY')) { |
||
| 2307 | return $this->getUserPreferencesTitle('DISPLAY', $displayKey); |
||
| 2308 | } |
||
| 2309 | } |
||
| 2310 | |||
| 2311 | /** |
||
| 2312 | * returns the current filter applied to the list |
||
| 2313 | * in a human readable string. |
||
| 2314 | * |
||
| 2315 | * @return string |
||
| 2316 | */ |
||
| 2317 | public function CurrentFilterTitle() |
||
| 2318 | { |
||
| 2319 | $filterKey = $this->getCurrentUserPreferences('FILTER'); |
||
| 2320 | $filters = array(); |
||
| 2321 | if ($filterKey != $this->getMyUserPreferencesDefault('FILTER')) { |
||
| 2322 | $filters[] = $this->getUserPreferencesTitle('FILTER', $filterKey); |
||
| 2323 | } |
||
| 2324 | if ($this->filterForGroupObject) { |
||
| 2325 | $filters[] = $this->filterForGroupObject->MenuTitle; |
||
| 2326 | } |
||
| 2327 | if (count($filters)) { |
||
| 2328 | return implode(', ', $filters); |
||
| 2329 | } |
||
| 2330 | } |
||
| 2331 | |||
| 2332 | /** |
||
| 2333 | * returns the current sort applied to the list |
||
| 2334 | * in a human readable string. |
||
| 2335 | * |
||
| 2336 | * @return string |
||
| 2337 | */ |
||
| 2338 | public function CurrentSortTitle() |
||
| 2339 | { |
||
| 2340 | $sortKey = $this->getCurrentUserPreferences('SORT'); |
||
| 2341 | if ($sortKey != $this->getMyUserPreferencesDefault('SORT')) { |
||
| 2342 | return $this->getUserPreferencesTitle('SORT', $sortKey); |
||
| 2343 | } |
||
| 2344 | } |
||
| 2345 | |||
| 2346 | /** |
||
| 2347 | * short-cut for getMyUserPreferencesDefault("DISPLAY") |
||
| 2348 | * for use in templtes. |
||
| 2349 | * |
||
| 2350 | * @return string - key |
||
| 2351 | */ |
||
| 2352 | public function MyDefaultDisplayStyle() |
||
| 2353 | { |
||
| 2354 | return $this->getMyUserPreferencesDefault('DISPLAY'); |
||
| 2355 | } |
||
| 2356 | |||
| 2357 | /** |
||
| 2358 | * Number of entries per page limited by total number of pages available... |
||
| 2359 | * |
||
| 2360 | * @return int |
||
| 2361 | */ |
||
| 2362 | public function MaxNumberOfProductsPerPage() |
||
| 2363 | { |
||
| 2364 | return $this->MyNumberOfProductsPerPage() > $this->TotalCount() ? $this->TotalCount() : $this->MyNumberOfProductsPerPage(); |
||
| 2365 | } |
||
| 2366 | |||
| 2367 | /**************************************************** |
||
| 2368 | * TEMPLATE METHODS FILTER LINK |
||
| 2369 | /****************************************************/ |
||
| 2370 | |||
| 2371 | /** |
||
| 2372 | * Provides a ArrayList of links for filters products. |
||
| 2373 | * |
||
| 2374 | * @return ArrayList( ArrayData(Name, Link, SelectKey, Current (boolean), LinkingMode)) |
||
| 2375 | */ |
||
| 2376 | public function FilterLinks() |
||
| 2377 | { |
||
| 2378 | $cacheKey = 'FilterLinks_'.($this->filterForGroupObject ? $this->filterForGroupObject->ID : 0); |
||
| 2379 | if ($list = $this->retrieveObjectStore($cacheKey)) { |
||
| 2380 | //do nothing |
||
| 2381 | } else { |
||
| 2382 | $list = $this->userPreferencesLinks('FILTER'); |
||
| 2383 | foreach ($list as $obj) { |
||
| 2384 | $key = $obj->SelectKey; |
||
| 2385 | if ($key != $this->getMyUserPreferencesDefault('FILTER')) { |
||
| 2386 | $count = count($this->currentInitialProductsAsCachedArray($key)); |
||
| 2387 | if ($count == 0) { |
||
| 2388 | $list->remove($obj); |
||
| 2389 | } else { |
||
| 2390 | $obj->Count = $count; |
||
| 2391 | } |
||
| 2392 | } |
||
| 2393 | } |
||
| 2394 | $this->saveObjectStore($list, $cacheKey); |
||
| 2395 | } |
||
| 2396 | $selectedItem = $this->getCurrentUserPreferences('FILTER'); |
||
| 2397 | foreach ($list as $obj) { |
||
| 2398 | $canHaveCurrent = true; |
||
| 2399 | if ($this->filterForGroupObject) { |
||
| 2400 | $canHaveCurrent = false; |
||
| 2401 | } |
||
| 2402 | $obj->Current = $selectedItem == $obj->SelectKey && $canHaveCurrent ? true : false; |
||
| 2403 | $obj->LinkingMode = $obj->Current ? 'current' : 'link'; |
||
| 2404 | $obj->Ajaxify = true; |
||
| 2405 | } |
||
| 2406 | |||
| 2407 | return $list; |
||
| 2408 | } |
||
| 2409 | |||
| 2410 | /** |
||
| 2411 | * returns a list of items (with links). |
||
| 2412 | * |
||
| 2413 | * @return ArrayList( ArrayData(Name, FilterLink, SelectKey, Current (boolean), LinkingMode)) |
||
| 2414 | */ |
||
| 2415 | public function ProductGroupFilterLinks() |
||
| 2416 | { |
||
| 2417 | if ($array = $this->retrieveObjectStore('ProductGroupFilterLinks')) { |
||
| 2418 | //do nothing |
||
| 2419 | } else { |
||
| 2420 | $arrayOfItems = array(); |
||
| 2421 | |||
| 2422 | $baseArray = $this->currentInitialProductsAsCachedArray($this->getMyUserPreferencesDefault('FILTER')); |
||
| 2423 | |||
| 2424 | //also show |
||
| 2425 | $items = $this->ProductGroupsFromAlsoShowProducts(); |
||
| 2426 | $arrayOfItems = array_merge($arrayOfItems, $this->productGroupFilterLinksCount($items, $baseArray, true)); |
||
| 2427 | //also show inverse |
||
| 2428 | $items = $this->ProductGroupsFromAlsoShowProductsInverse(); |
||
| 2429 | $arrayOfItems = array_merge($arrayOfItems, $this->productGroupFilterLinksCount($items, $baseArray, true)); |
||
| 2430 | |||
| 2431 | //parent groups |
||
| 2432 | $items = $this->ProductGroupsParentGroups(); |
||
| 2433 | $arrayOfItems = array_merge($arrayOfItems, $this->productGroupFilterLinksCount($items, $baseArray, true)); |
||
| 2434 | |||
| 2435 | //child groups |
||
| 2436 | $items = $this->MenuChildGroups(); |
||
| 2437 | $arrayOfItems = array_merge($arrayOfItems, $this->productGroupFilterLinksCount($items, $baseArray, true)); |
||
| 2438 | |||
| 2439 | ksort($arrayOfItems); |
||
| 2440 | $array = array(); |
||
| 2441 | foreach ($arrayOfItems as $arrayOfItem) { |
||
| 2442 | $array[] = $this->makeArrayItem($arrayOfItem); |
||
| 2443 | } |
||
| 2444 | $this->saveObjectStore($array, 'ProductGroupFilterLinks'); |
||
| 2445 | } |
||
| 2446 | $arrayList = ArrayList::create(); |
||
| 2447 | foreach ($array as $item) { |
||
| 2448 | $arrayList->push(ArrayData::create($item)); |
||
| 2449 | } |
||
| 2450 | return $arrayList; |
||
| 2451 | } |
||
| 2452 | |||
| 2453 | /** |
||
| 2454 | * counts the total number in the combination.... |
||
| 2455 | * |
||
| 2456 | * @param DataList $items - list of |
||
| 2457 | * @param Arary $baseArray - list of products on the current page |
||
| 2458 | * |
||
| 2459 | * @return array |
||
| 2460 | */ |
||
| 2461 | protected function productGroupFilterLinksCount($items, $baseArray, $ajaxify = true) |
||
| 2462 | { |
||
| 2463 | $array = array(); |
||
| 2464 | if ($items && $items->count()) { |
||
| 2465 | foreach ($items as $item) { |
||
| 2466 | $arrayOfIDs = $item->currentInitialProductsAsCachedArray($this->getMyUserPreferencesDefault('FILTER')); |
||
| 2467 | $newArray = array_intersect_key( |
||
| 2468 | $arrayOfIDs, |
||
| 2469 | $baseArray |
||
| 2470 | ); |
||
| 2471 | $count = count($newArray); |
||
| 2472 | if ($count) { |
||
| 2473 | $array[$item->Title] = array( |
||
| 2474 | 'Item' => $item, |
||
| 2475 | 'Count' => $count, |
||
| 2476 | 'Ajaxify' => $ajaxify, |
||
| 2477 | ); |
||
| 2478 | } |
||
| 2479 | } |
||
| 2480 | } |
||
| 2481 | |||
| 2482 | return $array; |
||
| 2483 | } |
||
| 2484 | |||
| 2485 | /** |
||
| 2486 | * @param array itemInArray (Item, Count, UserFilterAction) |
||
| 2487 | * |
||
| 2488 | * @return ArrayData |
||
| 2489 | */ |
||
| 2490 | protected function makeArrayItem($itemInArray) |
||
| 2491 | { |
||
| 2492 | $item = $itemInArray['Item']; |
||
| 2493 | $count = $itemInArray['Count']; |
||
| 2494 | $ajaxify = $itemInArray['Ajaxify']; |
||
| 2495 | $filterForGroupObjectID = $this->filterForGroupObject ? $this->filterForGroupObject->ID : 0; |
||
| 2496 | $isCurrent = $item->ID == $filterForGroupObjectID; |
||
| 2497 | if ($ajaxify) { |
||
| 2498 | $link = $this->Link('filterforgroup/'.$item->URLSegment); |
||
| 2499 | } else { |
||
| 2500 | $link = $item->Link(); |
||
| 2501 | } |
||
| 2502 | return array( |
||
| 2503 | 'Title' => $item->Title, |
||
| 2504 | 'Count' => $count, |
||
| 2505 | 'SelectKey' => $item->URLSegment, |
||
| 2506 | 'Current' => $isCurrent ? true : false, |
||
| 2507 | 'MyLinkingMode' => $isCurrent ? 'current' : 'link', |
||
| 2508 | 'FilterLink' => $link, |
||
| 2509 | 'Ajaxify' => $ajaxify ? true : false, |
||
| 2510 | ); |
||
| 2511 | } |
||
| 2512 | |||
| 2513 | /** |
||
| 2514 | * Provides a ArrayList of links for sorting products. |
||
| 2515 | */ |
||
| 2516 | public function SortLinks() |
||
| 2517 | { |
||
| 2518 | $list = $this->userPreferencesLinks('SORT'); |
||
| 2519 | $selectedItem = $this->getCurrentUserPreferences('SORT'); |
||
| 2520 | if ($list) { |
||
| 2521 | foreach ($list as $obj) { |
||
| 2522 | $obj->Current = $selectedItem == $obj->SelectKey ? true : false; |
||
| 2523 | $obj->LinkingMode = $obj->Current ? 'current' : 'link'; |
||
| 2524 | $obj->Ajaxify = true; |
||
| 2525 | } |
||
| 2526 | |||
| 2527 | return $list; |
||
| 2528 | } |
||
| 2529 | } |
||
| 2530 | |||
| 2531 | /** |
||
| 2532 | * Provides a ArrayList for displaying display links. |
||
| 2533 | */ |
||
| 2534 | public function DisplayLinks() |
||
| 2535 | { |
||
| 2536 | $list = $this->userPreferencesLinks('DISPLAY'); |
||
| 2537 | $selectedItem = $this->getCurrentUserPreferences('DISPLAY'); |
||
| 2538 | if ($list) { |
||
| 2539 | foreach ($list as $obj) { |
||
| 2540 | $obj->Current = $selectedItem == $obj->SelectKey ? true : false; |
||
| 2541 | $obj->LinkingMode = $obj->Current ? 'current' : 'link'; |
||
| 2542 | $obj->Ajaxify = true; |
||
| 2543 | } |
||
| 2544 | |||
| 2545 | return $list; |
||
| 2546 | } |
||
| 2547 | } |
||
| 2548 | |||
| 2549 | /** |
||
| 2550 | * Link that returns a list of all the products |
||
| 2551 | * for this product group as a simple list. |
||
| 2552 | * |
||
| 2553 | * @return string |
||
| 2554 | */ |
||
| 2555 | public function ListAllLink() |
||
| 2556 | { |
||
| 2557 | if ($this->filterForGroupObject) { |
||
| 2558 | return $this->Link('filterforgroup/'.$this->filterForGroupObject->URLSegment).'?showfulllist=1'; |
||
| 2559 | } else { |
||
| 2560 | return $this->Link().'?showfulllist=1'; |
||
| 2561 | } |
||
| 2562 | } |
||
| 2563 | |||
| 2564 | /** |
||
| 2565 | * Link that returns a list of all the products |
||
| 2566 | * for this product group as a simple list. |
||
| 2567 | * |
||
| 2568 | * @return string |
||
| 2569 | */ |
||
| 2570 | public function ListAFewLink() |
||
| 2571 | { |
||
| 2572 | return str_replace('?showfulllist=1', '', $this->ListAllLink()); |
||
| 2573 | } |
||
| 2574 | |||
| 2575 | /** |
||
| 2576 | * Link that returns a list of all the products |
||
| 2577 | * for this product group as a simple list. |
||
| 2578 | * |
||
| 2579 | * It resets everything - not just filter.... |
||
| 2580 | * |
||
| 2581 | * @return string |
||
| 2582 | */ |
||
| 2583 | public function ResetPreferencesLink($escapedAmpersands = true) |
||
| 2584 | { |
||
| 2585 | $ampersand = '&'; |
||
| 2586 | if ($escapedAmpersands) { |
||
| 2587 | $ampersand = '&'; |
||
| 2588 | } |
||
| 2589 | $getVariableNameFilter = $this->getSortFilterDisplayNames('FILTER', 'getVariable'); |
||
| 2590 | $getVariableNameSort = $this->getSortFilterDisplayNames('SORT', 'getVariable'); |
||
| 2591 | |||
| 2592 | return $this->Link().'?'. |
||
| 2593 | $getVariableNameFilter.'='.$this->getMyUserPreferencesDefault('FILTER').$ampersand. |
||
| 2594 | $getVariableNameSort.'='.$this->getMyUserPreferencesDefault('SORT').$ampersand. |
||
| 2595 | 'reload=1'; |
||
| 2596 | } |
||
| 2597 | |||
| 2598 | /** |
||
| 2599 | * Link to the search results. |
||
| 2600 | * |
||
| 2601 | * @return string |
||
| 2602 | */ |
||
| 2603 | public function SearchResultLink() |
||
| 2604 | { |
||
| 2605 | if ($this->HasSearchResults() && !$this->isSearchResults) { |
||
| 2606 | return $this->Link('searchresults'); |
||
| 2607 | } |
||
| 2608 | } |
||
| 2609 | |||
| 2610 | /**************************************************** |
||
| 2611 | * INTERNAL PROCESSING: PRODUCT LIST |
||
| 2612 | /****************************************************/ |
||
| 2613 | |||
| 2614 | /** |
||
| 2615 | * turns full list into paginated list. |
||
| 2616 | * |
||
| 2617 | * @param SS_List |
||
| 2618 | * |
||
| 2619 | * @return PaginatedList |
||
| 2620 | */ |
||
| 2621 | protected function paginateList(SS_List $list) |
||
| 2622 | { |
||
| 2623 | if ($list && $list->count()) { |
||
| 2624 | if ($this->IsShowFullList()) { |
||
| 2625 | $obj = PaginatedList::create($list, $this->request); |
||
| 2626 | $obj->setPageLength(EcommerceConfig::get('ProductGroup', 'maximum_number_of_products_to_list') + 1); |
||
| 2627 | |||
| 2628 | return $obj; |
||
| 2629 | } else { |
||
| 2630 | $obj = PaginatedList::create($list, $this->request); |
||
| 2631 | $obj->setPageLength($this->MyNumberOfProductsPerPage()); |
||
| 2632 | |||
| 2633 | return $obj; |
||
| 2634 | } |
||
| 2635 | } |
||
| 2636 | } |
||
| 2637 | |||
| 2638 | /**************************************************** |
||
| 2639 | * INTERNAL PROCESSING: USER PREFERENCES |
||
| 2640 | /****************************************************/ |
||
| 2641 | |||
| 2642 | /** |
||
| 2643 | * Checks out a bunch of $_GET variables |
||
| 2644 | * that are used to work out user preferences |
||
| 2645 | * Some of these are saved to session. |
||
| 2646 | * |
||
| 2647 | * @param array $overrideArray - override $_GET variable settings |
||
| 2648 | */ |
||
| 2649 | protected function saveUserPreferences($overrideArray = array()) |
||
| 2650 | { |
||
| 2651 | |||
| 2652 | //save sort - filter - display |
||
| 2653 | $sortFilterDisplayNames = $this->getSortFilterDisplayNames(); |
||
| 2654 | foreach ($sortFilterDisplayNames as $type => $oneTypeArray) { |
||
| 2655 | $getVariableName = $oneTypeArray['getVariable']; |
||
| 2656 | $sessionName = $oneTypeArray['sessionName']; |
||
| 2657 | if (isset($overrideArray[$getVariableName])) { |
||
| 2658 | $newPreference = $overrideArray[$getVariableName]; |
||
| 2659 | } else { |
||
| 2660 | $newPreference = $this->request->getVar($getVariableName); |
||
| 2661 | } |
||
| 2662 | if ($newPreference) { |
||
| 2663 | $optionsVariableName = $oneTypeArray['configName']; |
||
| 2664 | $options = EcommerceConfig::get($this->ClassName, $optionsVariableName); |
||
| 2665 | if (isset($options[$newPreference])) { |
||
| 2666 | Session::set('ProductGroup_'.$sessionName, $newPreference); |
||
| 2667 | //save in model as well... |
||
| 2668 | } |
||
| 2669 | } else { |
||
| 2670 | $newPreference = Session::get('ProductGroup_'.$sessionName); |
||
| 2671 | } |
||
| 2672 | //save data in model... |
||
| 2673 | $this->setCurrentUserPreference($type, $newPreference); |
||
| 2674 | } |
||
| 2675 | /* save URLSegments in model |
||
| 2676 | $this->setCurrentUserPreference( |
||
| 2677 | "URLSegments", |
||
| 2678 | array( |
||
| 2679 | "Action" => $this->request->param("Action"), |
||
| 2680 | "ID" => $this->request->param("ID") |
||
| 2681 | ) |
||
| 2682 | ); |
||
| 2683 | */ |
||
| 2684 | |||
| 2685 | //clearing data.. |
||
| 2686 | if ($this->request->getVar('reload')) { |
||
| 2687 | //reset other session variables... |
||
| 2688 | Session::set($this->SearchResultsSessionVariable(false), ''); |
||
| 2689 | Session::set($this->SearchResultsSessionVariable(true), ''); |
||
| 2690 | |||
| 2691 | return $this->redirect($this->Link()); |
||
| 2692 | } |
||
| 2693 | |||
| 2694 | //full list .... |
||
| 2695 | if ($this->request->getVar('showfulllist')) { |
||
| 2696 | $this->showFullList = true; |
||
| 2697 | } |
||
| 2698 | } |
||
| 2699 | |||
| 2700 | /** |
||
| 2701 | * Checks for the most applicable user preferences for this user: |
||
| 2702 | * 1. session value |
||
| 2703 | * 2. getMyUserPreferencesDefault. |
||
| 2704 | * |
||
| 2705 | * @param string $type - FILTER | SORT | DISPLAY |
||
| 2706 | * |
||
| 2707 | * @return string |
||
| 2708 | * |
||
| 2709 | * @todo: move to controller? |
||
| 2710 | */ |
||
| 2711 | protected function getCurrentUserPreferences($type) |
||
| 2712 | { |
||
| 2713 | $sessionName = $this->getSortFilterDisplayNames($type, 'sessionName'); |
||
| 2714 | if ($sessionValue = Session::get('ProductGroup_'.$sessionName)) { |
||
| 2715 | $key = Convert::raw2sql($sessionValue); |
||
| 2716 | } else { |
||
| 2717 | $key = $this->getMyUserPreferencesDefault($type); |
||
| 2718 | } |
||
| 2719 | |||
| 2720 | return $key; |
||
| 2721 | } |
||
| 2722 | |||
| 2723 | /** |
||
| 2724 | * Provides a dataset of links for a particular user preference. |
||
| 2725 | * |
||
| 2726 | * @param string $type SORT | FILTER | DISPLAY - e.g. sort_options |
||
| 2727 | * |
||
| 2728 | * @return ArrayList( ArrayData(Name, Link, SelectKey, Current (boolean), LinkingMode)) |
||
| 2729 | */ |
||
| 2730 | protected function userPreferencesLinks($type) |
||
| 2731 | { |
||
| 2732 | //get basics |
||
| 2733 | $sortFilterDisplayNames = $this->getSortFilterDisplayNames(); |
||
| 2734 | $options = $this->getConfigOptions($type); |
||
| 2735 | |||
| 2736 | //if there is only one option then do not bother |
||
| 2737 | if (count($options) < 2) { |
||
| 2738 | return; |
||
| 2739 | } |
||
| 2740 | |||
| 2741 | //get more config names |
||
| 2742 | $translationCode = $sortFilterDisplayNames[$type]['translationCode']; |
||
| 2743 | $getVariableName = $sortFilterDisplayNames[$type]['getVariable']; |
||
| 2744 | $arrayList = ArrayList::create(); |
||
| 2745 | if (count($options)) { |
||
| 2746 | foreach ($options as $key => $array) { |
||
| 2747 | //$isCurrent = ($key == $selectedItem) ? true : false; |
||
| 2748 | |||
| 2749 | $link = '?'.$getVariableName."=$key"; |
||
| 2750 | if ($type == 'FILTER') { |
||
| 2751 | $link = $this->Link().$link; |
||
| 2752 | } else { |
||
| 2753 | $link = $this->request->getVar('url').$link; |
||
| 2754 | } |
||
| 2755 | $arrayList->push(ArrayData::create(array( |
||
| 2756 | 'Name' => _t('ProductGroup.'.$translationCode.strtoupper(str_replace(' ', '', $array['Title'])), $array['Title']), |
||
| 2757 | 'Link' => $link, |
||
| 2758 | 'SelectKey' => $key, |
||
| 2759 | //we add current at runtime, so we can store the object without current set... |
||
| 2760 | //'Current' => $isCurrent, |
||
| 2761 | //'LinkingMode' => $isCurrent ? "current" : "link" |
||
| 2762 | ))); |
||
| 2763 | } |
||
| 2764 | } |
||
| 2765 | |||
| 2766 | return $arrayList; |
||
| 2767 | } |
||
| 2768 | |||
| 2769 | /**************************************************** |
||
| 2770 | * INTERNAL PROCESSING: TITLES |
||
| 2771 | /****************************************************/ |
||
| 2772 | |||
| 2773 | /** |
||
| 2774 | * variable to make sure secondary title only gets |
||
| 2775 | * added once. |
||
| 2776 | * |
||
| 2777 | * @var bool |
||
| 2778 | */ |
||
| 2779 | protected $secondaryTitleHasBeenAdded = false; |
||
| 2780 | |||
| 2781 | /** |
||
| 2782 | * add a secondary title to the main title |
||
| 2783 | * in case there is, for example, a filter applied |
||
| 2784 | * e.g. Socks | MyBrand. |
||
| 2785 | * |
||
| 2786 | * @param string |
||
| 2787 | */ |
||
| 2788 | protected function addSecondaryTitle($secondaryTitle = '') |
||
| 2789 | { |
||
| 2790 | $pipe = _t('ProductGroup.TITLE_SEPARATOR', ' | '); |
||
| 2791 | if (! $this->secondaryTitleHasBeenAdded) { |
||
| 2792 | if (trim($secondaryTitle)) { |
||
| 2793 | $secondaryTitle = $pipe.$secondaryTitle; |
||
| 2794 | } |
||
| 2795 | if ($this->IsSearchResults()) { |
||
| 2796 | if ($array = $this->searchResultsArrayFromSession()) { |
||
| 2797 | //we remove 1 item here, because the array starts with 0 => 0 |
||
| 2798 | $count = count($array) - 1; |
||
| 2799 | if ($count > 3) { |
||
| 2800 | $toAdd = $count. ' '._t('ProductGroup.PRODUCTS_FOUND', 'Products Found'); |
||
| 2801 | $secondaryTitle .= $this->cleanSecondaryTitleForAddition($pipe, $toAdd); |
||
| 2802 | } |
||
| 2803 | } else { |
||
| 2804 | $toAdd = _t('ProductGroup.SEARCH_RESULTS', 'Search Results'); |
||
| 2805 | $secondaryTitle .= $this->cleanSecondaryTitleForAddition($pipe, $toAdd); |
||
| 2806 | } |
||
| 2807 | } |
||
| 2808 | if (is_object($this->filterForGroupObject)) { |
||
| 2809 | $toAdd = $this->filterForGroupObject->Title; |
||
| 2810 | $secondaryTitle .= $this->cleanSecondaryTitleForAddition($pipe, $toAdd); |
||
| 2811 | } |
||
| 2812 | if ($this->IsShowFullList()) { |
||
| 2813 | $toAdd = _t('ProductGroup.LIST_VIEW', 'List View'); |
||
| 2814 | $secondaryTitle .= $this->cleanSecondaryTitleForAddition($pipe, $toAdd); |
||
| 2815 | } |
||
| 2816 | $filter = $this->getCurrentUserPreferences('FILTER'); |
||
| 2817 | if ($filter != $this->getMyUserPreferencesDefault('FILTER')) { |
||
| 2818 | $toAdd = $this->getUserPreferencesTitle('FILTER', $this->getCurrentUserPreferences('FILTER')); |
||
| 2819 | $secondaryTitle .= $this->cleanSecondaryTitleForAddition($pipe, $toAdd); |
||
| 2820 | } |
||
| 2821 | if ($this->HasSort()) { |
||
| 2822 | $toAdd = $this->getUserPreferencesTitle('SORT', $this->getCurrentUserPreferences('SORT')); |
||
| 2823 | $secondaryTitle .= $this->cleanSecondaryTitleForAddition($pipe, $toAdd); |
||
| 2824 | } |
||
| 2825 | if ($secondaryTitle) { |
||
| 2826 | $this->Title .= $secondaryTitle; |
||
| 2827 | if (isset($this->MetaTitle)) { |
||
| 2828 | $this->MetaTitle .= $secondaryTitle; |
||
| 2829 | } |
||
| 2830 | } |
||
| 2831 | //dont update menu title, because the entry in the menu |
||
| 2832 | //should stay the same as it links back to the unfiltered |
||
| 2833 | //page (in some cases). |
||
| 2834 | $this->secondaryTitleHasBeenAdded = true; |
||
| 2835 | } |
||
| 2836 | } |
||
| 2837 | |||
| 2838 | /** |
||
| 2839 | * removes any spaces from the 'toAdd' bit and adds the pipe if there is |
||
| 2840 | * anything to add at all. Through the lang files, you can change the pipe |
||
| 2841 | * symbol to anything you like. |
||
| 2842 | * |
||
| 2843 | * @param string $pipe |
||
| 2844 | * @param string $toAdd |
||
| 2845 | * @return string |
||
| 2846 | */ |
||
| 2847 | protected function cleanSecondaryTitleForAddition($pipe, $toAdd) |
||
| 2848 | { |
||
| 2849 | $toAdd = trim($toAdd); |
||
| 2850 | $length = strlen($toAdd); |
||
| 2851 | if ($length > 0) { |
||
| 2852 | $toAdd = $pipe.$toAdd; |
||
| 2853 | } |
||
| 2854 | return $toAdd; |
||
| 2855 | } |
||
| 2856 | |||
| 2857 | /**************************************************** |
||
| 2858 | * DEBUG |
||
| 2859 | /****************************************************/ |
||
| 2860 | |||
| 2861 | public function debug() |
||
| 2862 | { |
||
| 2863 | $member = Member::currentUser(); |
||
| 2864 | if (!$member || !$member->IsShopAdmin()) { |
||
| 2865 | $messages = array( |
||
| 2866 | 'default' => 'You must login as an admin to use debug functions.', |
||
| 2867 | ); |
||
| 2868 | Security::permissionFailure($this, $messages); |
||
| 2869 | } |
||
| 2870 | $this->ProductsShowable(); |
||
| 2871 | $html = EcommerceTaskDebugCart::debug_object($this->dataRecord); |
||
| 2872 | $html .= '<ul>'; |
||
| 2873 | |||
| 2874 | $html .= '<li><hr /><h3>Available options</h3><hr /></li>'; |
||
| 2875 | $html .= '<li><b>Sort Options for Dropdown:</b><pre> '.print_r($this->getUserPreferencesOptionsForDropdown('SORT'), 1).'</pre> </li>'; |
||
| 2876 | $html .= '<li><b>Filter Options for Dropdown:</b><pre> '.print_r($this->getUserPreferencesOptionsForDropdown('FILTER'), 1).'</pre></li>'; |
||
| 2877 | $html .= '<li><b>Display Styles for Dropdown:</b><pre> '.print_r($this->getUserPreferencesOptionsForDropdown('DISPLAY'), 1).'</pre> </li>'; |
||
| 2878 | |||
| 2879 | $html .= '<li><hr /><h3>Selection Setting (what is set as default for this page)</h3><hr /></li>'; |
||
| 2880 | $html .= '<li><b>MyDefaultFilter:</b> '.$this->getMyUserPreferencesDefault('FILTER').' </li>'; |
||
| 2881 | $html .= '<li><b>MyDefaultSortOrder:</b> '.$this->getMyUserPreferencesDefault('SORT').' </li>'; |
||
| 2882 | $html .= '<li><b>MyDefaultDisplayStyle:</b> '.$this->getMyUserPreferencesDefault('DISPLAY').' </li>'; |
||
| 2883 | $html .= '<li><b>MyNumberOfProductsPerPage:</b> '.$this->MyNumberOfProductsPerPage().' </li>'; |
||
| 2884 | $html .= '<li><b>MyLevelOfProductsToshow:</b> '.$this->MyLevelOfProductsToShow().' = '.(isset($this->showProductLevels[$this->MyLevelOfProductsToShow()]) ? $this->showProductLevels[$this->MyLevelOfProductsToShow()] : 'ERROR!!!! $this->showProductLevels not set for '.$this->MyLevelOfProductsToShow()).' </li>'; |
||
| 2885 | |||
| 2886 | $html .= '<li><hr /><h3>Current Settings</h3><hr /></li>'; |
||
| 2887 | $html .= '<li><b>Current Sort Order:</b> '.$this->getCurrentUserPreferences('SORT').' </li>'; |
||
| 2888 | $html .= '<li><b>Current Filter:</b> '.$this->getCurrentUserPreferences('FILTER').' </li>'; |
||
| 2889 | $html .= '<li><b>Current display style:</b> '.$this->getCurrentUserPreferences('DISPLAY').' </li>'; |
||
| 2890 | |||
| 2891 | $html .= '<li><hr /><h3>DATALIST: totals, numbers per page etc</h3><hr /></li>'; |
||
| 2892 | $html .= '<li><b>Total number of products:</b> '.$this->TotalCount().' </li>'; |
||
| 2893 | $html .= '<li><b>Is there more than one product:</b> '.($this->TotalCountGreaterThanOne() ? 'YES' : 'NO').' </li>'; |
||
| 2894 | $html .= '<li><b>Number of products per page:</b> '.$this->MyNumberOfProductsPerPage().' </li>'; |
||
| 2895 | |||
| 2896 | $html .= '<li><hr /><h3>SQL Factors</h3><hr /></li>'; |
||
| 2897 | $html .= '<li><b>Default sort SQL:</b> '.print_r($this->getUserSettingsOptionSQL('SORT'), 1).' </li>'; |
||
| 2898 | $html .= '<li><b>User sort SQL:</b> '.print_r($this->getUserSettingsOptionSQL('SORT', $this->getCurrentUserPreferences('SORT')), 1).' </li>'; |
||
| 2899 | $html .= '<li><b>Default Filter SQL:</b> <pre>'.print_r($this->getUserSettingsOptionSQL('FILTER'), 1).'</pre> </li>'; |
||
| 2900 | $html .= '<li><b>User Filter SQL:</b> <pre>'.print_r($this->getUserSettingsOptionSQL('FILTER', $this->getCurrentUserPreferences('FILTER')), 1).'</pre> </li>'; |
||
| 2901 | $html .= '<li><b>Buyable Class name:</b> '.$this->getBuyableClassName().' </li>'; |
||
| 2902 | $html .= '<li><b>allProducts:</b> '.print_r(str_replace('"', '`', $this->allProducts->sql()), 1).' </li>'; |
||
| 2903 | |||
| 2904 | $html .= '<li><hr /><h3>Search</h3><hr /></li>'; |
||
| 2905 | $resultArray = $this->searchResultsArrayFromSession(); |
||
| 2906 | $productGroupArray = explode(',', Session::get($this->SearchResultsSessionVariable(true))); |
||
| 2907 | $html .= '<li><b>Is Search Results:</b> '.($this->IsSearchResults() ? 'YES' : 'NO').' </li>'; |
||
| 2908 | $html .= '<li><b>Products In Search (session variable : '.$this->SearchResultsSessionVariable(false).'):</b> '.print_r($resultArray, 1).' </li>'; |
||
| 2909 | $html .= '<li><b>Product Groups In Search (session variable : '.$this->SearchResultsSessionVariable(true).'):</b> '.print_r($productGroupArray, 1).' </li>'; |
||
| 2910 | |||
| 2911 | $html .= '<li><hr /><h3>Other</h3><hr /></li>'; |
||
| 2912 | if ($image = $this->BestAvailableImage()) { |
||
| 2913 | $html .= '<li><b>Best Available Image:</b> <img src="'.$image->Link.'" /> </li>'; |
||
| 2914 | } |
||
| 2915 | $html .= '<li><b>BestAvailableImage:</b> '.($this->BestAvailableImage() ? $this->BestAvailableImage()->Link : 'no image available').' </li>'; |
||
| 2916 | $html .= '<li><b>Is this an ecommerce page:</b> '.($this->IsEcommercePage() ? 'YES' : 'NO').' </li>'; |
||
| 2917 | $html .= '<li><hr /><h3>Related Groups</h3><hr /></li>'; |
||
| 2918 | $html .= '<li><b>Parent product group:</b> '.($this->ParentGroup() ? $this->ParentGroup()->Title : '[NO PARENT GROUP]').'</li>'; |
||
| 2919 | |||
| 2920 | $childGroups = $this->ChildGroups(99); |
||
| 2921 | if ($childGroups->count()) { |
||
| 2922 | $childGroups = $childGroups->map('ID', 'MenuTitle'); |
||
| 2923 | $html .= '<li><b>Child Groups (all):</b><pre> '.print_r($childGroups, 1).' </pre></li>'; |
||
| 2924 | } else { |
||
| 2925 | $html .= '<li><b>Child Groups (full tree): </b>NONE</li>'; |
||
| 2926 | } |
||
| 2927 | $html .= '<li><b>a list of Product Groups that have the products for the CURRENT product group listed as part of their AlsoShowProducts list:</b><pre>'.print_r($this->ProductGroupsFromAlsoShowProducts()->map('ID', 'Title')->toArray(), 1).' </pre></li>'; |
||
| 2946 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.