Source of file CountryPrice_TestController.php
Size: 18,496 Bytes - Last Modified: 2021-12-23T10:40:38+00:00
/var/www/docs.ssmods.com/process/src/code/control/cms/CountryPrice_TestController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 | <?php class CountryPrice_TestController extends ContentController { private static $allowed_actions = array( "whoami" => true, "addproducts" => true, "setmycountry" => true, "resetmycountry" => true, "currencypercountry" => true, "countrieswithoutdistributor" => true, "stockistcountrieswithoutcountry" => true ); public function init() { parent::init(); if (! Permission::check('ADMIN')) { return Security::permissionFailure($this, 'This page is secured and you need administrator rights to access it. You can also save the page in the CMS to get 15 minutes access without being logged in.'); } } public function index() { echo "<hr />"; echo "<hr />"; echo "<hr />"; foreach (self::$allowed_actions as $action => $notNeeded) { if (!in_array($action, array("setmycountry"))) { DB::alteration_message("<a href=\"".$this->Link($action)."\">$action</a>"); } } DB::alteration_message("<a href=\"/dev/ecommerce/\">All E-commerce Tools</a>"); echo "<hr />"; $countries = CountryPrice_EcommerceCountry::get_real_countries_list(); $countryLinks = array(); foreach ($countries as $country) { $code = $country->Code; $name = $country->Name; $countryLinks[] = "<a href=\"".$this->Link("setmycountry/$code/?countryfortestingonly=$code")."\">$name</a>"; } DB::alteration_message("Available Countries: ".implode(", ", $countryLinks)); $countries = EcommerceCountry::get()->exclude(array('Code' => $countries->column('Code'))); $countryLinks = array(); foreach ($countries as $country) { $code = $country->Code; $name = $country->Name; $countryLinks[] = "<a href=\"".$this->Link("setmycountry/$code/?countryfortestingonly=$code")."\">$name</a>"; } DB::alteration_message("Other Countries: ".implode(", ", $countryLinks)); } public function Link($action = null) { return "/distributors-test/".$action; } public function addproducts() { $sc = ShoppingCart::singleton(); $count = 1; $products = Product::get()->filter("AllowPurchase", 1)->limit(20)->Sort("Rand()"); foreach ($products as $product) { if ($product->canPurchase() && $count < 3) { $count++; $sc->addBuyable($product, rand(1, 5)); } } $count = 1; if (class_exists('ProductVariation')) { $productVariations = ProductVariation::get()->filter("AllowPurchase", 1)->limit(20)->Sort("Rand()"); foreach ($productVariations as $productVariation) { if ($productVariation->canPurchase() && $count < 3) { $count++; $sc->addBuyable($productVariation, rand(1, 5)); } } } return $this->redirect($sc->Link()); } public function resetmycountry($request) { $this->resetSessionVars(); $o = ShoppingCart::current_order(); if ($o) { $o->delete(); } DB::alteration_message("Cleared countries"); return $this->redirect("/dev/ecommerce/ecommercetaskcartmanipulation_current"); } public function setmycountry($request) { $value = strtoupper(Convert::raw2sql($request->param("ID"))); if ($o = ShoppingCart::current_order()) { $this->resetSessionVars(); $o->SetCountryFields($value); $o->CurrencyCountry = $value; $o->OriginatingCountryCode = $value; $o->write(); $o->SetCountryFields($value); $o->CurrencyCountry = $value; $o->OriginatingCountryCode = $value; $o->write(); CountryPrice_OrderDOD::localise_order(); CountryPrice_OrderDOD::localise_order(); CountryPrice_OrderDOD::localise_order(); return $this->redirectBack('/dev/ecommerce/ecommercetaskcartmanipulation_current/'); } user_error("There is no cart available."); } private function resetSessionVars() { Session::clear("countryfortestingonly"); Session::clear("MyCloudFlareCountry"); Session::clear("ipfortestingonly"); Session::save(); } public function currencypercountry($request) { $currencies = CountryPrice_EcommerceCurrency::get_currency_per_country(); $usedCurrencies = CountryPrice_EcommerceCurrency::get_currency_per_country_used_ones(); $useArray = array(); $nonUseArray = array(); foreach ($currencies as $country => $currency) { $countryName = EcommerceCountry::find_title($country); $currencyObject = EcommerceCurrency::create_new($currency); if (isset($usedCurrencies[$country])) { $useArray[] = "$country: $currency ($countryName, ".$currencyObject->getTitle().")"; } else { $nonUseArray[] = "$country: $currency ($countryName, ".$currencyObject->getTitle().")"; } } echo "<h2>Countries ready for sale with their currency:</h2>"; foreach ($useArray as $line) { DB::alteration_message($line); } echo "<h2>Countries NOT ready for sale with their currency:</h2>"; foreach ($nonUseArray as $line) { DB::alteration_message($line); } return $this->index(); } public function countrieswithoutdistributor($request) { $list = EcommerceCountry::get()->filter(array("DistributorID" => 0)); if (!$list->count()) { DB::alteration_message('All countries have distributors', 'created'); } else { foreach ($list as $country) { DB::alteration_message("<a href=\"".$country->CMSEditLink()."\">".$country->Code." ".$country->Name."</a>"); } } return $this->index(); } public function stockistcountrieswithoutcountry($request) { $mainPage = StockistSearchPage::get()->first(); $list = StockistCountryPage::get()->where("Country = '' OR Country IS NULL")->exclude(array("ParentID" => $mainPage->ID)); foreach ($list as $stockistCountry) { DB::alteration_message("<a href=\"".$stockistCountry->CMSEditLink()."\">".$stockistCountry->Title."</a>"); } return $this->index(); } public function whoami() { $descriptionArray = array( "MyCountryCode" => "This is the key function", "MyCountryTitle" => "", "MyCurrency" => "Based on the currency set in the order.", "MyDistributor" => "You have to set the ". _t('Distributor.SINGULAR_NAME', 'Distributor') . "'s countries in order to work out someone's ". _t('Distributor.SINGULAR_NAME', 'Distributor') . ".", "MyDistributorCountry" => "This is the country that is being used for the sale. For dodgy countries, we use the backup country.", "MyDeliveryCostNote" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and default country.", "MyShippingDeliveryInfo" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and default country.", "MyShippingReturnInfo" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and default country.", "MyProductNotAvailableNote" => "Set in country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . ", default country, AND the Ecommerce Config.", "MyStockistSearchPage" => "", "MyStockistCountryPage" => "The stockist page that is related to the visitor's country", "MyCountryFAQPage" => "", "MyBackupCountryCode" => "This country is used if the information for the selected country is not available.", "MyDefaultDistributor" => "You can set one default ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " (head office) with a tickbox for any ". _t('Distributor.SINGULAR_NAME', 'Distributor') , ); $array = array( "MyCountryCode" => "String", "MyCountryTitle" => "String", "MyCurrency" => "Object", "MyDistributor" => "Object", "MyDistributorCountry" => "Object", "MyDeliveryCostNote" => "String", "MyShippingDeliveryInfo" => "String", "MyShippingReturnInfo" => "String", "MyProductNotAvailableNote" => "String", "MyStockistSearchPage" => "Object", "MyStockistCountryPage" => "Object", "MyCountryFAQPage" => "Object", "MyBackupCountryCode" => "String", "MyDefaultDistributor" => "Object", ); echo "<h1>Current Settings</h1>"; echo "<p>Most of these settings can be adjusted in the country, ". _t('Distributor.SINGULAR_NAME', 'Distributor') . " and generic e-commerce settings.</p>"; foreach ($array as $name => $type) { $style = "created"; $notSet = false; $string = ""; if ($type == "String") { $string = $this->$name(); if (is_object($string)) { $string = $string->raw(); } } else { $obj = $this->$name(); if (!$obj) { $string = "Object Not Found"; $notSet = true; $style = "deleted"; } else { switch ($name) { case "MyDistributorCountry": case "MyCountryFAQPage": case "MyDefaultDistributor": case "MyDistributor": $string = $obj->Name; break; case "MyStockistSearchPage": case "MyStockistCountryPage": $string = $obj->Title; break; default: $string = print_r($obj, 1); } } } if (! $string) { $string = "NOT SET"; $style = "deleted"; } DB::alteration_message("<i>$name</i>: <u><strong>".$string."</strong></u> ... <br /><sup>".$descriptionArray[$name]."</sup><hr />", $style); } echo "<h1>Change Settings</h1>"; $links = array( "resetmycountry/" => "Go back to the standard country", ); $links["/dev/tasks/TEST_GEOIP_COUNTRY_CODE_BY_NAME"] = "test GEOIP function"; foreach ($links as $link => $desc) { DB::alteration_message("<a href=\"".$this->Link($link)."\">".$desc."</a>"); } return $this->index(); } /** * * @return String */ public function MyCurrency() { return ShoppingCart::current_order()->CurrencyUsed()->Name; } /** * * @return String */ public function MyCountryCode() { return EcommerceCountry::get_country(); } /** * * @return String */ public function MyBackupCountryCode() { return CountryPrice_EcommerceCountry::get_backup_country()->Code; } /** * @return String */ public function MyCountryTitle() { $code = EcommerceCountry::get_country(); return EcommerceCountry::find_title($code); } public function MyDefaultDistributor() { return Distributor::get_default_distributor(); } /** * returns note about Delivery Cost * Needs to be in model so we can access from the order in the template. * @return Varchar Field */ public function MyDeliveryCostNote() { $note = EcommerceCountry::get_country_object()->DeliveryCostNote; if (!$note) { if ($distributor = $this->MyDistributor()) { $note = $distributor->DeliveryCostNote; } if (!$note) { $note = CountryPrice_EcommerceCountry::get_backup_country()->DeliveryCostNote; } } return DBField::create_field("Varchar", $note); } /** * returns note about Shipping Estimation Time * Needs to be in model so we can access from the order in the template. * @return Varchar Field */ public function MyShippingDeliveryInfo() { $info = EcommerceCountry::get_country_object()->ShippingEstimation; if (!$info) { if ($distributor = $this->MyDistributor()) { $info = $distributor->ShippingEstimation; } if (!$info) { $info = CountryPrice_EcommerceCountry::get_backup_country()->ShippingEstimation; } } return DBField::create_field("Varchar", $info); } /** * returns the Distributor for the user * Needs to be in model so we can access from the order in the template. * @return Varchar Field */ public function MyDistributor() { $code = EcommerceCountry::get_country(); $distributor = Distributor::get_one_for_country($code); return $distributor; } /** * returns note about shipping return information * Needs to be in model so we can access from the order in the template. * @return Varchar Field */ public function MyShippingReturnInfo() { $info = EcommerceCountry::get_country_object()->ReturnInformation; if (!$info) { if ($distributor = $this->MyDistributor()) { $info = $distributor->ReturnInformation; } if (!$info) { $info = CountryPrice_EcommerceCountry::get_backup_country()->ReturnInformation; } } return DBField::create_field("Varchar", $info); } /** * returns note to show when product / variation is not available in the specific country * Needs to be in model so we can access from the order in the template. * @return Varchar Field */ public function MyProductNotAvailableNote() { $note = EcommerceCountry::get_country_object()->ProductNotAvailableNote; if (!$note) { if ($distributor = $this->MyDistributor()) { $note = $distributor->ProductNotAvailableNote; } if (!$note) { $note = CountryPrice_EcommerceCountry::get_backup_country()->ProductNotAvailableNote; } if (!$note) { $note = $this->EcomConfig()->NotForSaleMessage; } } return DBField::create_field("HTMLText", $note); } /** * provides information on where to buy a product * if it can not be bought online... * @return StockistCountryPage | Null */ public function MyStockistCountryPage($attempts = 0) { $exit = false; switch ($attempts) { case 0: $countryCode = EcommerceCountry::get_country(); break; case 1: $countryCode = CountryPrice_EcommerceCountry::get_distributor_country(); break; case 2: $countryCode = EcommerceConfig::get('EcommerceCountry', 'default_country_code'); break; break; default: $countryCode = "NZ"; $exit = true; } if ($countryCode) { $countryStockistPage = StockistCountryPage::get()->filter(array("Country" => $countryCode))->first(); if (!$countryStockistPage) { if ($countryObject = DataObject::get_one("EcommerceCountry", "Code = '$countryCode'")) { $rows = DB::query( " SELECT \"StockistCountryPageID\" FROM \"StockistCountryPage_AdditionalCountries\" WHERE \"EcommerceCountryID\" = ".$countryObject->ID ); if ($rows) { foreach ($rows as $row) { $countryStockistPage = StockistCountryPage::get()->byID($rows["StockistCountryPageID"]); if ($countryStockistPage) { break; } } } } } if ($countryStockistPage || $exit) { return $countryStockistPage; } } $attempts++; return $this->MyStockistCountryPage($attempts); } /** * * @return EcommerceCountry */ public function MyDistributorCountry() { return CountryPrice_EcommerceCountry::get_distributor_country(); return $countryObject; } /** * * @return StockistSearchPage */ public function StockistSearchPage() { return StockistSearchPage::get()->filter(array("ClassName" => "StockistSearchPage"))->first(); } public function MyStockistSearchPage() { $country = EcommerceCountry::get_country(); $countryPage = StockistCountryPage::get()->filter(array("Country" => $country))->First(); $sql = " SELECT StockistCountryPageID FROM StockistCountryPage_AdditionalCountries WHERE EcommerceCountryID = '".EcommerceCountry::get_country_id($country)."'"; $idForPage = DB::query($sql)->value(); if ($idForPage) { StockistCountryPage::get()->byID($idForPage); } if (!$countryPage) { if (!$countryPage) { $countryPage = $this->StockistSearchPage(); } } return $countryPage; } /** * * returns the DistributorFAQPage ONLY if Specific Content has been entered * for the country. * @return DistributorFAQPage */ public function MyCountryFAQPage() { $country = $this->MyDistributorCountry(); if ($country && $country->FAQContent) { return DistributorFAQPage::get()->First(); } } } |