Source of file Commerce_Controller.php
Size: 7,047 Bytes - Last Modified: 2021-12-24T05:16:32+00:00
/var/www/docs.ssmods.com/process/src/code/control/Commerce_Controller.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 | <?php /** * Top level controller that all commerce controllers should extend. There are * some methods that have to be taken from ContentController to allow the * Commerce module to operate with just the core framework, or with the CMS. * * Currently this class acts pretty much as just a container for these classes. */ abstract class Commerce_Controller extends Controller { /** * The URL segment that is matched from the routing rules. This MUST be set * on your extension class, as it is used to generate $Link() * * @var String * @config */ private static $url_segment; protected $dataRecord; /** * Returns the associated database record */ public function data() { return $this->dataRecord; } public function getDataRecord() { return $this->data(); } public function setDataRecord($dataRecord) { $this->dataRecord = $dataRecord; return $this; } public function Link($action = null) { return Controller::join_links( Director::BaseURL(), $this->config()->url_segment, $action ); } /** * Init actions that happen globally to all commerce interfaces. * * At the moment this is used to set config where database access is required */ public function init() { // Set the default currency symbol if($siteconfig = SiteConfig::current_site_config()) { Currency::config()->currency_symbol = $siteconfig->Currency()->HTMLNotation; } parent::init(); } /** * Function to find relevent postage rates, based on supplied country and * zip/postal code data. * * We make this a function as part of Commerce Controller, as there are * several points in the payment/order process that will make use of this * functionality * * @param $country String listing the country to search, this has to be an ISO 3166 code * @param $zipcode String listing the zip/postage code to filter by */ public function getPostageAreas($country, $zipcode) { $return = new ArrayList(); $countries = new ArrayList(); $cart = ShoppingCart::create(); $all_rates = $this ->SiteConfig() ->PostageAreas(); // First find all area's for this country directly (no wildcards) foreach($all_rates as $rate) { if(!(strpos(strtolower($rate->Country), strtolower($country)) === false)) $countries->add($rate); } // If we have no countries in the list specificly, then check for wildcards if(!$countries->exists()) { foreach($all_rates as $rate) { if($rate->Country == "*") $countries->add($rate); } } // If we have a list of countries check them for post codes foreach($countries as $rate) { $rate_codes = explode(",",$rate->ZipCode); foreach($rate_codes as $rate_to_check) { $curr_length = strlen($rate_to_check); if(strtolower(substr($zipcode, 0, $curr_length)) == strtolower($rate_to_check)) { $return->add($rate); } } } // If we still don't have anything to return, check or list of countries // for a wildcard if(!$return->exists()) { foreach($countries as $rate) { if($rate->ZipCode == "*") $return->add($rate); } } // Now we have a list of locations, start checking for additional // rules an remove if not applicable. $total_cost = str_replace(",","",$cart->SubTotalCost()); $total_weight = str_replace(",","",$cart->TotalWeight()); $total_items = str_replace(",","",$cart->TotalItems()); $max_cost = 0; $max_weight = 0; $max_items = 0; // First loop through and find items that are invalid foreach($return as $location) { if($location->Calculation == "Price" && ((float)$total_cost < $location->Unit)) $return->remove($location); if($location->Calculation == "Weight" && ((float)$total_weight < $location->Unit)) $return->remove($location); if($location->Calculation == "Items" && ((float)$total_items < $location->Unit)) $return->remove($location); } // Now find max values based on units foreach($return as $location) { if($location->Calculation == "Price" && ($location->Unit > $max_cost)) $max_cost = $location->Unit; if($location->Calculation == "Weight" && ($location->Unit > $max_weight)) $max_weight = $location->Unit; if($location->Calculation == "Items" && ($location->Unit > $max_items)) $max_items = $location->Unit; } // Now loop through again and calculate which brackets each // Location fits in foreach($return as $location) { if($location->Calculation == "Price" && ($location->Unit < $max_cost)) $return->remove($location); if($location->Calculation == "Weight" && ($location->Unit < $max_weight)) $return->remove($location); if($location->Calculation == "Items" && ($location->Unit < $max_items)) $return->remove($location); } return $return; } /** * Process and render search results. This has been hacked a bit to load * products into the list (if they exists). Will need to come up with a more * elegant solution to dealing with complex searches of objects though. * * @param array $data The raw request data submitted by user * @param SearchForm $form The form instance that was submitted * @param SS_HTTPRequest $request Request generated for this action */ public function results($data, $form, $request) { $results = $form->getResults(); // For the moment this will also need to be added to your // Page_Controller::results() method (until a more elegant solution can // be found if(class_exists("Product")) { $products = Product::get()->filterAny(array( "Title:PartialMatch" => $data["Search"], "SKU" => $data["Search"], "Description:PartialMatch" => $data["Search"] )); $results->merge($products); } $results = $results->sort("Title","ASC"); $data = array( 'Results' => $results, 'Query' => $form->getSearchQuery(), 'Title' => _t('SearchForm.SearchResults', 'Search Results') ); return $this ->owner ->customise($data) ->renderWith(array( 'Page_results', 'SearchResults', 'Page' )); } } |