Source of file ContinentalContent.php
Size: 16,896 Bytes - Last Modified: 2021-12-23T10:36:28+00:00
/var/www/docs.ssmods.com/process/src/code/Extensions/ContinentalContent.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 | <?php /** * Created by Nivanka Fonseka (nivanka@silverstripers.com). * User: nivankafonseka * Date: 2/8/15 * Time: 11:38 AM * To change this template use File | Settings | File Templates. * * * MELBOURNE == * 103.4.18.145 * 103.4.18.149 * 220.244.123.208 * 220.246.2.112 * * SYDNEY == * 223.252.22.128 * 221.133.191.80 * 220.245.102.48 * */ class ContinentalContent extends DataExtension { private static $disable_continental_fields = false; private static $continents = array(); private static $content_fields_types = array( 'Text', 'Varchar', 'HTMLText', 'HTMLVarchar' ); private static $exclude_field_names = array( 'URLSegment' ); private static $country_codes = array( "BD" => "Bangladesh", "BE" => "Belgium", "BF" => "Burkina Faso", "BG" => "Bulgaria", "BA" => "Bosnia and Herzegovina", "BB" => "Barbados", "WF" => "Wallis and Futuna", "BL" => "Saint Barthelemy", "BM" => "Bermuda", "BN" => "Brunei", "BO" => "Bolivia", "BH" => "Bahrain", "BI" => "Burundi", "BJ" => "Benin", "BT" => "Bhutan", "JM" => "Jamaica", "BV" => "Bouvet Island", "BW" => "Botswana", "WS" => "Samoa", "BQ" => "Bonaire, Saint Eustatius and Saba ", "BR" => "Brazil", "BS" => "Bahamas", "JE" => "Jersey", "BY" => "Belarus", "BZ" => "Belize", "RU" => "Russia", "RW" => "Rwanda", "RS" => "Serbia", "TL" => "East Timor", "RE" => "Reunion", "TM" => "Turkmenistan", "TJ" => "Tajikistan", "RO" => "Romania", "TK" => "Tokelau", "GW" => "Guinea-Bissau", "GU" => "Guam", "GT" => "Guatemala", "GS" => "South Georgia and the South Sandwich Islands", "GR" => "Greece", "GQ" => "Equatorial Guinea", "GP" => "Guadeloupe", "JP" => "Japan", "GY" => "Guyana", "GG" => "Guernsey", "GF" => "French Guiana", "GE" => "Georgia", "GD" => "Grenada", "GB" => "United Kingdom", "GA" => "Gabon", "SV" => "El Salvador", "GN" => "Guinea", "GM" => "Gambia", "GL" => "Greenland", "GI" => "Gibraltar", "GH" => "Ghana", "OM" => "Oman", "TN" => "Tunisia", "JO" => "Jordan", "HR" => "Croatia", "HT" => "Haiti", "HU" => "Hungary", "HK" => "Hong Kong", "HN" => "Honduras", "HM" => "Heard Island and McDonald Islands", "VE" => "Venezuela", "PR" => "Puerto Rico", "PS" => "Palestinian Territory", "PW" => "Palau", "PT" => "Portugal", "SJ" => "Svalbard and Jan Mayen", "PY" => "Paraguay", "IQ" => "Iraq", "PA" => "Panama", "PF" => "French Polynesia", "PG" => "Papua New Guinea", "PE" => "Peru", "PK" => "Pakistan", "PH" => "Philippines", "PN" => "Pitcairn", "PL" => "Poland", "PM" => "Saint Pierre and Miquelon", "ZM" => "Zambia", "EH" => "Western Sahara", "EE" => "Estonia", "EG" => "Egypt", "ZA" => "South Africa", "EC" => "Ecuador", "IT" => "Italy", "VN" => "Vietnam", "SB" => "Solomon Islands", "ET" => "Ethiopia", "SO" => "Somalia", "ZW" => "Zimbabwe", "SA" => "Saudi Arabia", "ES" => "Spain", "ER" => "Eritrea", "ME" => "Montenegro", "MD" => "Moldova", "MG" => "Madagascar", "MF" => "Saint Martin", "MA" => "Morocco", "MC" => "Monaco", "UZ" => "Uzbekistan", "MM" => "Myanmar", "ML" => "Mali", "MO" => "Macao", "MN" => "Mongolia", "MH" => "Marshall Islands", "MK" => "Macedonia", "MU" => "Mauritius", "MT" => "Malta", "MW" => "Malawi", "MV" => "Maldives", "MQ" => "Martinique", "MP" => "Northern Mariana Islands", "MS" => "Montserrat", "MR" => "Mauritania", "IM" => "Isle of Man", "UG" => "Uganda", "TZ" => "Tanzania", "MY" => "Malaysia", "MX" => "Mexico", "IL" => "Israel", "FR" => "France", "IO" => "British Indian Ocean Territory", "SH" => "Saint Helena", "FI" => "Finland", "FJ" => "Fiji", "FK" => "Falkland Islands", "FM" => "Micronesia", "FO" => "Faroe Islands", "NI" => "Nicaragua", "NL" => "Netherlands", "NO" => "Norway", "NA" => "Namibia", "VU" => "Vanuatu", "NC" => "New Caledonia", "NE" => "Niger", "NF" => "Norfolk Island", "NG" => "Nigeria", "NZ" => "New Zealand", "NP" => "Nepal", "NR" => "Nauru", "NU" => "Niue", "CK" => "Cook Islands", "XK" => "Kosovo", "CI" => "Ivory Coast", "CH" => "Switzerland", "CO" => "Colombia", "CN" => "China", "CM" => "Cameroon", "CL" => "Chile", "CC" => "Cocos Islands", "CA" => "Canada", "CG" => "Republic of the Congo", "CF" => "Central African Republic", "CD" => "Democratic Republic of the Congo", "CZ" => "Czech Republic", "CY" => "Cyprus", "CX" => "Christmas Island", "CR" => "Costa Rica", "CW" => "Curacao", "CV" => "Cape Verde", "CU" => "Cuba", "SZ" => "Swaziland", "SY" => "Syria", "SX" => "Sint Maarten", "KG" => "Kyrgyzstan", "KE" => "Kenya", "SS" => "South Sudan", "SR" => "Suriname", "KI" => "Kiribati", "KH" => "Cambodia", "KN" => "Saint Kitts and Nevis", "KM" => "Comoros", "ST" => "Sao Tome and Principe", "SK" => "Slovakia", "KR" => "South Korea", "SI" => "Slovenia", "KP" => "North Korea", "KW" => "Kuwait", "SN" => "Senegal", "SM" => "San Marino", "SL" => "Sierra Leone", "SC" => "Seychelles", "KZ" => "Kazakhstan", "KY" => "Cayman Islands", "SG" => "Singapore", "SE" => "Sweden", "SD" => "Sudan", "DO" => "Dominican Republic", "DM" => "Dominica", "DJ" => "Djibouti", "DK" => "Denmark", "VG" => "British Virgin Islands", "DE" => "Germany", "YE" => "Yemen", "DZ" => "Algeria", "US" => "United States", "UY" => "Uruguay", "YT" => "Mayotte", "UM" => "United States Minor Outlying Islands", "LB" => "Lebanon", "LC" => "Saint Lucia", "LA" => "Laos", "TV" => "Tuvalu", "TW" => "Taiwan", "TT" => "Trinidad and Tobago", "TR" => "Turkey", "LK" => "Sri Lanka", "LI" => "Liechtenstein", "LV" => "Latvia", "TO" => "Tonga", "LT" => "Lithuania", "LU" => "Luxembourg", "LR" => "Liberia", "LS" => "Lesotho", "TH" => "Thailand", "TF" => "French Southern Territories", "TG" => "Togo", "TD" => "Chad", "TC" => "Turks and Caicos Islands", "LY" => "Libya", "VA" => "Vatican", "VC" => "Saint Vincent and the Grenadines", "AE" => "United Arab Emirates", "AD" => "Andorra", "AG" => "Antigua and Barbuda", "AF" => "Afghanistan", "AI" => "Anguilla", "VI" => "U.S. Virgin Islands", "IS" => "Iceland", "IR" => "Iran", "AM" => "Armenia", "AL" => "Albania", "AO" => "Angola", "AQ" => "Antarctica", "AS" => "American Samoa", "AR" => "Argentina", "AU" => "Australia", "AT" => "Austria", "AW" => "Aruba", "IN" => "India", "AX" => "Aland Islands", "AZ" => "Azerbaijan", "IE" => "Ireland", "ID" => "Indonesia", "UA" => "Ukraine", "QA" => "Qatar", "MZ" => "Mozambique" ); private static $array_generated_fields = array(); private static $current_continent = ''; private static $affected_tables = array(); private static $provider = 'MaxMind'; private static $debug_messages = array(); /** * @param $callback * @return mixed */ protected static function without_continental_fields($callback) { $before = self::$disable_continental_fields; self::$disable_continental_fields = true; $result = $callback(); self::$disable_continental_fields = $before; return $result; } public static function add_debug_message($message) { self::$debug_messages[] = $message; } public static function get_debug_messages() { if(count(self::$debug_messages)) { return implode('<br><br>', self::$debug_messages); } return false; } /** * @param $class * @param $extension * @param $args * @return array|mixed */ public static function get_extra_config($class, $extension, $args) { if(self::$disable_continental_fields) return array(); foreach (ClassInfo::subclassesFor($class) as $subClass) { $config = self::make_continental_fields($subClass); foreach($config as $name => $value) { Config::inst()->update($subClass, $name, $value); } } DataObject::reset(); return self::make_continental_fields($class); } /** * @param $class * @param $config * @return mixed */ public static function get_configs_for_class($class, $config){ return self::without_continental_fields(function() use ($class, $config) { return Config::inst()->get($class, $config, Config::UNINHERITED); }); } /** * @param $class * @return mixed * * make fields for the dataobjects for the continents */ public static function make_continental_fields($class){ if(isset(self::$array_generated_fields[$class])){ return self::$array_generated_fields[$class]; } else{ $arrBaseFields = self::get_configs_for_class($class, 'db'); $arrBaseIndexes = self::get_configs_for_class($class, 'indexes'); $arrBaseManyManyExtra = self::get_configs_for_class($class, 'many_many_ExtraFields'); $arrMultipleFields = Config::inst()->get('ContinentalContent', 'content_fields_types'); $arrExcludeTypes = Config::inst()->get('ContinentalContent', 'exclude_field_names'); $arrNewFields = $arrBaseFields; $indexes = $arrBaseIndexes; $arrNewManyManyExtra = $arrBaseManyManyExtra; foreach(self::GetContinentSuffixes() as $strName => $strSuffix){ if($arrBaseFields) foreach($arrBaseFields as $strKey => $strType){ if(!in_array($strKey, $arrExcludeTypes) && !in_array("{$class}.{$strKey}", $arrExcludeTypes)){ if(in_array(self::GetFieldType($strType), $arrMultipleFields)){ $arrNewFields[$strKey . '_' . $strSuffix] = $strType; if($indexes && array_key_exists($strKey, $arrBaseIndexes)) $indexes[] = $strKey . '_' . $strSuffix; } } } if($arrBaseManyManyExtra) foreach($arrBaseManyManyExtra as $strRelation => $arrFields){ foreach($arrFields as $strKey => $strType){ if(in_array(self::GetFieldType($strType), $arrMultipleFields)) $arrNewManyManyExtra[$strRelation][$strKey . '_' . $strSuffix] = $strType; } } } if(count($arrNewFields) != count($arrBaseFields)) self::$affected_tables[] = $class; self::$array_generated_fields[$class] = array( 'db' => $arrNewFields, 'indexes' => $arrBaseIndexes, // 'many_many_extraFields' => $arrNewManyManyExtra ); return self::$array_generated_fields[$class]; } } /** * @param $strType * @return string */ public static function GetFieldType($strType){ return strpos($strType, '(') === false ? $strType : substr($strType, 0, strpos($strType, '('));; } /** * @return array */ public static function GetContinents(){ $arrRet = array(); foreach(Config::inst()->get('ContinentalContent', 'continents') as $key => $filter) $arrRet[self::UpdateContinentExtensionName($key)] = $filter; return $arrRet; } public static function GetContinentSuffixes(){ $arrRet = array(); foreach(Config::inst()->get('ContinentalContent', 'continents') as $key => $filter) $arrRet[Convert::raw2url($key)] = self::UpdateContinentExtensionName($key); return $arrRet; } public static function UpdateContinentExtensionName($strName){ $default_replacements = array( '/&/u' => '-and-', '/&/u' => '-and-', '/\s|\+/u' => '-', // remove whitespace/plus '/[_.-]+/u' => '', // underscores and dots to dashes '/[^A-Za-z0-9\-]+/u' => '' // remove non-ASCII chars, only allow alphanumeric and dashes ); $strName = strtolower($strName); foreach($default_replacements as $regex => $replace){ $strName = preg_replace($regex, $replace, $strName); } return $strName; } /** * @param FieldList $fields */ public function updateCMSFields(FieldList $fields) { if(Config::inst()->get('ContinentalContent', 'AutoAddCMSFields')){ $arrBaseDB = Config::inst()->get(get_class($this->owner), 'db'); foreach(self::GetContinentSuffixes() as $strContinent => $strSuffix){ if($arrBaseDB) foreach($arrBaseDB as $strName => $strType){ if(array_key_exists($strName . '_' . $strSuffix, $arrBaseDB)){ if($dataField = $fields->dataFieldByName($strName)){ $newField = clone $dataField; $newField->setName($strName . '_' . $strSuffix); $newField->setTitle($dataField->Title() . ' (' . $strContinent . ')'); $fields->insertAfter($newField, $strName); } } } } } } /** * @return bool */ public static function IsViewingThroughProxy(){ $bRet = false; $strProxyIP = Config::inst()->get('ContinentalContent', 'proxy_ip'); if($strProxyIP != '0.0.0.0'){ $bRet = $strProxyIP == ContinentalContentUtils::IPAddress(); } return $bRet; } /** * @param $strContinent */ public static function ForceUpdateContinent($strContinent){ self::$current_continent = $strContinent; } /** * @return int|string */ public static function CurrentContinent(){ if(self::$current_continent) return self::$current_continent; self::$current_continent = CONTINENTAL_DEFAULT; if(!self::IsViewingThroughProxy() && Session::get('SESSION_MAP_LOCATION')){ self::$current_continent = strtolower(trim(Session::get('SESSION_MAP_LOCATION'))); } else if ($forwarder = ForwardedIP::get()->filter('IP', ContinentalContentUtils::IPAddress())->first()) { foreach(self::GetContinents() as $key => $filter) { if($filter['Country'] == $forwarder->Continent){ self::$current_continent = $key; ContinentalContent::add_debug_message('Forwarder Matched : ' . $forwarder->Continent); break; } } } else if($location = ContinentalContentUtils::GetLocation()){ foreach(self::GetContinents() as $key => $filter){ $cityMatched = -1; $subDivisionMatched = -1; $countryMatched = -1; if(isset($filter['City'])) { $cityMatched = 0; $cities = is_array($filter['City']) ? $filter['City'] : array($filter['City']); $intersectCities = array_intersect($cities, $location->CityNames); if(count($intersectCities)) { $cityMatched = 1; } } if(isset($filter['SubDivision'])) { $subDivisionMatched = 0; $subdivisions = is_array($filter['SubDivision']) ? $filter['SubDivision'] : array($filter['SubDivision']); $intersectSubdivisions = array_intersect($subdivisions, $location->SubDivisions); if(count($intersectSubdivisions)) { $subDivisionMatched = 1; } } if(isset($filter['Country'])) { $countryMatched = 0; $countries = is_array($filter['Country']) ? $filter['Country'] : array($filter['Country']); if(in_array($location->Country, $countries)){ $countryMatched = 1; } } if($cityMatched != 0 && $subDivisionMatched != 0 && $countryMatched != 0) { self::$current_continent = $key; ContinentalContent::add_debug_message('Matched Location : ' . $key); break; } } } return self::$current_continent; } /** * @return array */ public function getAffectedTables(){ return self::$affected_tables; } /** * @param $class * @param $select * @param $fallback * @return string */ protected function localiseSelect($class, $select, $fallback) { return "CASE WHEN (\"{$class}\".\"{$select}\" IS NOT NULL AND \"{$class}\".\"{$select}\" != '') THEN \"{$class}\".\"{$select}\" ELSE \"{$class}\".\"{$fallback}\" END"; } /** * @param SQLQuery $query * @param DataQuery $dataQuery */ public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) { $controller = Controller::curr(); if(!is_subclass_of($controller ,'LeftAndMain')){ $includedTables = ContinentalContent::getAffectedTables(); $strContinent = ContinentalContent::CurrentContinent(); if($strContinent != CONTINENTAL_DEFAULT){ foreach($query->getSelect() as $alias => $select) { if(!preg_match('/^"(?<class>\w+)"\."(?<field>\w+)"$/i', $select, $matches)) continue; $class = $matches['class']; $field = $matches['field']; if(!in_array($class, $includedTables)) continue; $strNewField = $field . '_' . $strContinent; $arrFields = ContinentalContent::make_continental_fields($class); if(isset($arrFields['db']) && isset($arrFields['db'][$strNewField])){ $expression = $this->localiseSelect($class, $strNewField, $field); $query->selectField($expression, $alias); } } } } } /** * @param $base * @param $action */ public function updateRelativeLink(&$base, &$action) { if(Config::inst()->get('ContinentalContent', 'custom_urls') == 'Y'){ if($strContinent = ContinentalContent::CurrentContinent()){ $bAddContinent = $strContinent != 100; if($bAddContinent && strpos($base, $strContinent) === false) $base = Controller::join_links($strContinent, $base); } } } public static function ForceLocationFromSession($strLocation){ Session::set('SESSION_MAP_LOCATION', $strLocation); Session::save(); } public static function ClearForceLocation(){ Session::clear('SESSION_MAP_LOCATION'); Session::save(); } } |