@@ -13,89 +13,89 @@ |
||
| 13 | 13 | class LocationCsvBulkLoader extends CsvBulkLoader |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $columnMap = array( |
|
| 20 | - 'Name' => 'Title', |
|
| 21 | - 'EmailAddress' => 'Email', |
|
| 22 | - 'Categories' => '->getCategoryByName', |
|
| 23 | - 'Import_ID' => 'Import_ID', |
|
| 24 | - 'Country' => '->getCountryByName', |
|
| 25 | - ); |
|
| 16 | + /** |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $columnMap = array( |
|
| 20 | + 'Name' => 'Title', |
|
| 21 | + 'EmailAddress' => 'Email', |
|
| 22 | + 'Categories' => '->getCategoryByName', |
|
| 23 | + 'Import_ID' => 'Import_ID', |
|
| 24 | + 'Country' => '->getCountryByName', |
|
| 25 | + ); |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - public $duplicateChecks = array( |
|
| 31 | - 'Import_ID' => 'Import_ID' |
|
| 32 | - ); |
|
| 27 | + /** |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + public $duplicateChecks = array( |
|
| 31 | + 'Import_ID' => 'Import_ID' |
|
| 32 | + ); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param $val |
|
| 36 | - * @return string|string[]|null |
|
| 37 | - */ |
|
| 38 | - public function getEscape($val) |
|
| 39 | - { |
|
| 40 | - return preg_replace("/\r|\n/", "", $val); |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * @param $val |
|
| 36 | + * @return string|string[]|null |
|
| 37 | + */ |
|
| 38 | + public function getEscape($val) |
|
| 39 | + { |
|
| 40 | + return preg_replace("/\r|\n/", "", $val); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param $obj |
|
| 45 | - * @param $val |
|
| 46 | - * @param $record |
|
| 47 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 48 | - */ |
|
| 49 | - public static function getCategoryByName(&$obj, $val, $record) |
|
| 50 | - { |
|
| 51 | - $val = Convert::raw2sql($val); |
|
| 52 | - $parts = explode(', ', $val); |
|
| 43 | + /** |
|
| 44 | + * @param $obj |
|
| 45 | + * @param $val |
|
| 46 | + * @param $record |
|
| 47 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 48 | + */ |
|
| 49 | + public static function getCategoryByName(&$obj, $val, $record) |
|
| 50 | + { |
|
| 51 | + $val = Convert::raw2sql($val); |
|
| 52 | + $parts = explode(', ', $val); |
|
| 53 | 53 | |
| 54 | - foreach ($parts as $part) { |
|
| 55 | - $category = LocationCategory::get()->filter(array('Name' => $part))->first(); |
|
| 56 | - if (!$category) { |
|
| 57 | - $category = LocationCategory::create(); |
|
| 58 | - $category->Name = $part; |
|
| 59 | - $category->write(); |
|
| 60 | - } |
|
| 61 | - $obj->Categories()->add($category); |
|
| 62 | - } |
|
| 63 | - } |
|
| 54 | + foreach ($parts as $part) { |
|
| 55 | + $category = LocationCategory::get()->filter(array('Name' => $part))->first(); |
|
| 56 | + if (!$category) { |
|
| 57 | + $category = LocationCategory::create(); |
|
| 58 | + $category->Name = $part; |
|
| 59 | + $category->write(); |
|
| 60 | + } |
|
| 61 | + $obj->Categories()->add($category); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param $obj |
|
| 67 | - * @param $val |
|
| 68 | - * @param $record |
|
| 69 | - */ |
|
| 70 | - public function getCountryByName(&$obj, $val, $record) |
|
| 71 | - { |
|
| 72 | - $val = $this->getEscape($val); |
|
| 73 | - $countries = IntlLocales::singleton()->getCountries(); |
|
| 65 | + /** |
|
| 66 | + * @param $obj |
|
| 67 | + * @param $val |
|
| 68 | + * @param $record |
|
| 69 | + */ |
|
| 70 | + public function getCountryByName(&$obj, $val, $record) |
|
| 71 | + { |
|
| 72 | + $val = $this->getEscape($val); |
|
| 73 | + $countries = IntlLocales::singleton()->getCountries(); |
|
| 74 | 74 | |
| 75 | - if (strlen((string)$val) == 2) { |
|
| 76 | - $val = strtolower($val); |
|
| 77 | - if (array_key_exists($val, $countries)) { |
|
| 78 | - $obj->Country = $val; |
|
| 79 | - } |
|
| 80 | - } |
|
| 75 | + if (strlen((string)$val) == 2) { |
|
| 76 | + $val = strtolower($val); |
|
| 77 | + if (array_key_exists($val, $countries)) { |
|
| 78 | + $obj->Country = $val; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - if (in_array($val, $countries)) { |
|
| 83 | - $obj->Country = array_search($val, $countries); |
|
| 84 | - } |
|
| 85 | - } |
|
| 82 | + if (in_array($val, $countries)) { |
|
| 83 | + $obj->Country = array_search($val, $countries); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param array $record |
|
| 89 | - * @param array $columnMap |
|
| 90 | - * @param \SilverStripe\Dev\BulkLoader_Result $results |
|
| 91 | - * @param bool $preview |
|
| 92 | - * @return int|void |
|
| 93 | - */ |
|
| 94 | - protected function processRecord($record, $columnMap, &$results, $preview = false) |
|
| 95 | - { |
|
| 96 | - $objID = parent::processRecord($record, $columnMap, $results, $preview = false); |
|
| 87 | + /** |
|
| 88 | + * @param array $record |
|
| 89 | + * @param array $columnMap |
|
| 90 | + * @param \SilverStripe\Dev\BulkLoader_Result $results |
|
| 91 | + * @param bool $preview |
|
| 92 | + * @return int|void |
|
| 93 | + */ |
|
| 94 | + protected function processRecord($record, $columnMap, &$results, $preview = false) |
|
| 95 | + { |
|
| 96 | + $objID = parent::processRecord($record, $columnMap, $results, $preview = false); |
|
| 97 | 97 | |
| 98 | - $location = Location::get()->byID($objID); |
|
| 99 | - $location->publishSingle(); |
|
| 100 | - } |
|
| 98 | + $location = Location::get()->byID($objID); |
|
| 99 | + $location->publishSingle(); |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -21,345 +21,345 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class LocatorController extends \PageController |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private static $allowed_actions = [ |
|
| 28 | - 'xml', |
|
| 29 | - 'json', |
|
| 30 | - ]; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - private static $base_filter = []; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - private static $base_exclude = [ |
|
| 41 | - 'Lat' => 0, |
|
| 42 | - 'Lng' => 0, |
|
| 43 | - ]; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - private static $base_filter_any = []; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var bool |
|
| 52 | - */ |
|
| 53 | - private static $bootstrapify = true; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * ID of map container |
|
| 57 | - * |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - private static $map_container = 'map'; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * class of location list container |
|
| 64 | - * |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - private static $list_container = 'loc-list'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * GET variable which, if isset, will trigger storeLocator init and return XML |
|
| 71 | - * |
|
| 72 | - * @var string |
|
| 73 | - */ |
|
| 74 | - private static $query_trigger = 'action_doFilterLocations'; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var DataList|ArrayList |
|
| 78 | - */ |
|
| 79 | - protected $locations; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Set Requirements based on input from CMS |
|
| 83 | - */ |
|
| 84 | - public function init() |
|
| 85 | - { |
|
| 86 | - parent::init(); |
|
| 87 | - |
|
| 88 | - // prevent init of map if no query |
|
| 89 | - $request = Controller::curr()->getRequest(); |
|
| 90 | - if (!$this->getTrigger($request)) { |
|
| 91 | - return; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $locations = $this->getLocations(); |
|
| 95 | - |
|
| 96 | - // prevent init of map if there are no locations |
|
| 97 | - if (!$locations) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // google maps api key |
|
| 102 | - $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
|
| 103 | - Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
|
| 104 | - |
|
| 105 | - $mapSettings = [ |
|
| 106 | - 'fullMapStart' => true, |
|
| 107 | - 'maxDistance' => true, |
|
| 108 | - 'storeLimit' => -1, |
|
| 109 | - 'originMarker' => true, |
|
| 110 | - 'slideMap' => false, |
|
| 111 | - 'distanceAlert' => -1, |
|
| 112 | - 'featuredLocations' => false, |
|
| 113 | - 'defaultLat' => 0, |
|
| 114 | - 'defaultLng' => 0, |
|
| 115 | - 'mapSettings' => [ |
|
| 116 | - 'zoom' => 12, |
|
| 117 | - 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
|
| 118 | - 'disableDoubleClickZoom' => true, |
|
| 119 | - 'scrollwheel' => false, |
|
| 120 | - 'navigationControl' => false, |
|
| 121 | - 'draggable' => false, |
|
| 122 | - ], |
|
| 123 | - ]; |
|
| 124 | - |
|
| 125 | - $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
|
| 126 | - $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
|
| 127 | - $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
|
| 128 | - $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
|
| 129 | - $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
|
| 130 | - |
|
| 131 | - if ($locations->filter('Featured', true)->count() > 0) { |
|
| 132 | - $mapSettings['featuredLocations'] = true; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // map config based on user input in Settings tab |
|
| 136 | - $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
| 137 | - if (0 < $limit) { |
|
| 138 | - $mapSettings['storeLimit'] = $limit; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if ($coords = $this->getAddressSearchCoords()) { |
|
| 142 | - $mapSettings['defaultLat'] = $coords->getField("Lat"); |
|
| 143 | - $mapSettings['defaultLat'] = $coords->getField("Lng"); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // pass GET variables to xml action |
|
| 147 | - $vars = $this->request->getVars(); |
|
| 148 | - unset($vars['url']); |
|
| 149 | - $url = ''; |
|
| 150 | - if (count($vars)) { |
|
| 151 | - $url .= '?' . http_build_query($vars); |
|
| 152 | - } |
|
| 153 | - $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
| 154 | - |
|
| 155 | - if ($stylePath = $this->getMapStyleJSONPath()) { |
|
| 156 | - if ($style = file_get_contents($stylePath)) { |
|
| 157 | - $mapSettings['mapSettings']['styles'] = json_decode($style); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ($imagePath = $this->getMarkerIcon()) { |
|
| 162 | - $mapSettings['markerImg'] = $imagePath; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $this->extend('modifyMapSettings', $mapSettings); |
|
| 166 | - |
|
| 167 | - $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
|
| 168 | - // mapTypeId is a javascript object |
|
| 169 | - $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
|
| 170 | - |
|
| 171 | - $this->extend('modifyMapSettingsEncoded', $encoded); |
|
| 172 | - // init map |
|
| 173 | - Requirements::customScript(" |
|
| 24 | + /** |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private static $allowed_actions = [ |
|
| 28 | + 'xml', |
|
| 29 | + 'json', |
|
| 30 | + ]; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + private static $base_filter = []; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + private static $base_exclude = [ |
|
| 41 | + 'Lat' => 0, |
|
| 42 | + 'Lng' => 0, |
|
| 43 | + ]; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + private static $base_filter_any = []; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var bool |
|
| 52 | + */ |
|
| 53 | + private static $bootstrapify = true; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * ID of map container |
|
| 57 | + * |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + private static $map_container = 'map'; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * class of location list container |
|
| 64 | + * |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + private static $list_container = 'loc-list'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * GET variable which, if isset, will trigger storeLocator init and return XML |
|
| 71 | + * |
|
| 72 | + * @var string |
|
| 73 | + */ |
|
| 74 | + private static $query_trigger = 'action_doFilterLocations'; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var DataList|ArrayList |
|
| 78 | + */ |
|
| 79 | + protected $locations; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Set Requirements based on input from CMS |
|
| 83 | + */ |
|
| 84 | + public function init() |
|
| 85 | + { |
|
| 86 | + parent::init(); |
|
| 87 | + |
|
| 88 | + // prevent init of map if no query |
|
| 89 | + $request = Controller::curr()->getRequest(); |
|
| 90 | + if (!$this->getTrigger($request)) { |
|
| 91 | + return; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $locations = $this->getLocations(); |
|
| 95 | + |
|
| 96 | + // prevent init of map if there are no locations |
|
| 97 | + if (!$locations) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // google maps api key |
|
| 102 | + $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
|
| 103 | + Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
|
| 104 | + |
|
| 105 | + $mapSettings = [ |
|
| 106 | + 'fullMapStart' => true, |
|
| 107 | + 'maxDistance' => true, |
|
| 108 | + 'storeLimit' => -1, |
|
| 109 | + 'originMarker' => true, |
|
| 110 | + 'slideMap' => false, |
|
| 111 | + 'distanceAlert' => -1, |
|
| 112 | + 'featuredLocations' => false, |
|
| 113 | + 'defaultLat' => 0, |
|
| 114 | + 'defaultLng' => 0, |
|
| 115 | + 'mapSettings' => [ |
|
| 116 | + 'zoom' => 12, |
|
| 117 | + 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
|
| 118 | + 'disableDoubleClickZoom' => true, |
|
| 119 | + 'scrollwheel' => false, |
|
| 120 | + 'navigationControl' => false, |
|
| 121 | + 'draggable' => false, |
|
| 122 | + ], |
|
| 123 | + ]; |
|
| 124 | + |
|
| 125 | + $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
|
| 126 | + $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
|
| 127 | + $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
|
| 128 | + $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
|
| 129 | + $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
|
| 130 | + |
|
| 131 | + if ($locations->filter('Featured', true)->count() > 0) { |
|
| 132 | + $mapSettings['featuredLocations'] = true; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // map config based on user input in Settings tab |
|
| 136 | + $limit = Config::inst()->get(LocatorController::class, 'limit'); |
|
| 137 | + if (0 < $limit) { |
|
| 138 | + $mapSettings['storeLimit'] = $limit; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if ($coords = $this->getAddressSearchCoords()) { |
|
| 142 | + $mapSettings['defaultLat'] = $coords->getField("Lat"); |
|
| 143 | + $mapSettings['defaultLat'] = $coords->getField("Lng"); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // pass GET variables to xml action |
|
| 147 | + $vars = $this->request->getVars(); |
|
| 148 | + unset($vars['url']); |
|
| 149 | + $url = ''; |
|
| 150 | + if (count($vars)) { |
|
| 151 | + $url .= '?' . http_build_query($vars); |
|
| 152 | + } |
|
| 153 | + $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
|
| 154 | + |
|
| 155 | + if ($stylePath = $this->getMapStyleJSONPath()) { |
|
| 156 | + if ($style = file_get_contents($stylePath)) { |
|
| 157 | + $mapSettings['mapSettings']['styles'] = json_decode($style); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ($imagePath = $this->getMarkerIcon()) { |
|
| 162 | + $mapSettings['markerImg'] = $imagePath; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $this->extend('modifyMapSettings', $mapSettings); |
|
| 166 | + |
|
| 167 | + $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
|
| 168 | + // mapTypeId is a javascript object |
|
| 169 | + $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
|
| 170 | + |
|
| 171 | + $this->extend('modifyMapSettingsEncoded', $encoded); |
|
| 172 | + // init map |
|
| 173 | + Requirements::customScript(" |
|
| 174 | 174 | $(function(){ |
| 175 | 175 | $('#map-container').storeLocator({$encoded}); |
| 176 | 176 | }); |
| 177 | 177 | ", 'jquery-locator'); |
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param HTTPRequest $request |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - public function getTrigger(HTTPRequest $request = null) |
|
| 186 | - { |
|
| 187 | - if ($request === null) { |
|
| 188 | - $request = $this->getRequest(); |
|
| 189 | - } |
|
| 190 | - $trigger = $request->getVar(Config::inst()->get(LocatorController::class, 'query_trigger')); |
|
| 191 | - |
|
| 192 | - return isset($trigger); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @return ArrayList|DataList |
|
| 197 | - */ |
|
| 198 | - public function getLocations() |
|
| 199 | - { |
|
| 200 | - if (!$this->locations) { |
|
| 201 | - $this->setLocations($this->request); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return $this->locations; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @param HTTPRequest|null $request |
|
| 209 | - * |
|
| 210 | - * @return $this |
|
| 211 | - */ |
|
| 212 | - public function setLocations(HTTPRequest $request = null) |
|
| 213 | - { |
|
| 214 | - |
|
| 215 | - if ($request === null) { |
|
| 216 | - $request = $this->request; |
|
| 217 | - } |
|
| 218 | - $filter = $this->config()->get('base_filter'); |
|
| 219 | - |
|
| 220 | - $categoryVar = $this->data()->config()->get('category_var'); |
|
| 221 | - if ($request->getVar($categoryVar)) { |
|
| 222 | - $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
| 223 | - } else { |
|
| 224 | - if ($this->getPageCategories()->exists()) { |
|
| 225 | - foreach ($this->getPageCategories() as $category) { |
|
| 226 | - $filter['Categories.ID'][] = $category->ID; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - $this->extend('updateLocatorFilter', $filter, $request); |
|
| 232 | - |
|
| 233 | - $filterAny = $this->config()->get('base_filter_any'); |
|
| 234 | - $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
| 235 | - |
|
| 236 | - $exclude = $this->config()->get('base_exclude'); |
|
| 237 | - $this->extend('updateLocatorExclude', $exclude, $request); |
|
| 238 | - |
|
| 239 | - $class = $this->data()->ClassName; |
|
| 240 | - $locations = $class::get_locations($filter, $filterAny, $exclude); |
|
| 241 | - $locations = DataToArrayListHelper::to_array_list($locations); |
|
| 242 | - |
|
| 243 | - //allow for adjusting list post possible distance calculation |
|
| 244 | - $this->extend('updateLocationList', $locations); |
|
| 245 | - |
|
| 246 | - if ($locations->canSortBy('Distance')) { |
|
| 247 | - $locations = $locations->sort('Distance'); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - if ($this->getShowRadius()) { |
|
| 251 | - $radiusVar = $this->data()->config()->get('radius_var'); |
|
| 252 | - |
|
| 253 | - if ($radius = (int)$request->getVar($radiusVar)) { |
|
| 254 | - $locations = $locations->filterByCallback(function ($location) use (&$radius) { |
|
| 255 | - return $location->Distance <= $radius; |
|
| 256 | - }); |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - //allow for returning list to be set as |
|
| 261 | - $this->extend('updateListType', $locations); |
|
| 262 | - |
|
| 263 | - $limit = $this->getLimit(); |
|
| 264 | - if ($limit > 0) { |
|
| 265 | - $locations = $locations->limit($limit); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $this->locations = $locations; |
|
| 269 | - |
|
| 270 | - return $this; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @return ArrayData|boolean |
|
| 275 | - */ |
|
| 276 | - public function getAddressSearchCoords() |
|
| 277 | - { |
|
| 278 | - $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
| 279 | - if (!$this->request->getVar($addressVar)) { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - if (class_exists(GoogleGeocoder::class)) { |
|
| 283 | - $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
| 284 | - $response = $geocoder->getResult(); |
|
| 285 | - $lat = $response->getLatitude(); |
|
| 286 | - $lng = $response->getLongitude(); |
|
| 287 | - |
|
| 288 | - return new ArrayData([ |
|
| 289 | - "Lat" => $lat, |
|
| 290 | - "Lng" => $lng, |
|
| 291 | - ]); |
|
| 292 | - } |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * @param HTTPRequest $request |
|
| 297 | - * |
|
| 298 | - * @return \SilverStripe\View\ViewableData_Customised |
|
| 299 | - */ |
|
| 300 | - public function index(HTTPRequest $request) |
|
| 301 | - { |
|
| 302 | - if ($this->getTrigger($request)) { |
|
| 303 | - $locations = $this->getLocations(); |
|
| 304 | - } else { |
|
| 305 | - $locations = ArrayList::create(); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - return $this->customise([ |
|
| 309 | - 'Locations' => $locations, |
|
| 310 | - ]); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Renders locations in xml format |
|
| 315 | - * |
|
| 316 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 317 | - */ |
|
| 318 | - public function xml() |
|
| 319 | - { |
|
| 320 | - $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
| 321 | - $data = new ArrayData([ |
|
| 322 | - "Locations" => $this->getLocations(), |
|
| 323 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 324 | - ]); |
|
| 325 | - |
|
| 326 | - return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Renders locations in json format |
|
| 331 | - * |
|
| 332 | - * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 333 | - */ |
|
| 334 | - public function json() |
|
| 335 | - { |
|
| 336 | - $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
| 337 | - $data = new ArrayData([ |
|
| 338 | - "Locations" => $this->getLocations(), |
|
| 339 | - "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 340 | - ]); |
|
| 341 | - |
|
| 342 | - return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * LocationSearch form. |
|
| 347 | - * |
|
| 348 | - * Search form for locations, updates map and results list via AJAX |
|
| 349 | - * |
|
| 350 | - * @return \SilverStripe\Forms\Form |
|
| 351 | - */ |
|
| 352 | - public function LocationSearch() |
|
| 353 | - { |
|
| 354 | - |
|
| 355 | - $form = LocatorForm::create($this, 'LocationSearch'); |
|
| 356 | - |
|
| 357 | - $this->extend('updateLocationSearch', $form); |
|
| 358 | - |
|
| 359 | - return $form |
|
| 360 | - ->setFormMethod('GET') |
|
| 361 | - ->setFormAction($this->Link()) |
|
| 362 | - ->disableSecurityToken() |
|
| 363 | - ->loadDataFrom($this->request->getVars()); |
|
| 364 | - } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param HTTPRequest $request |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + public function getTrigger(HTTPRequest $request = null) |
|
| 186 | + { |
|
| 187 | + if ($request === null) { |
|
| 188 | + $request = $this->getRequest(); |
|
| 189 | + } |
|
| 190 | + $trigger = $request->getVar(Config::inst()->get(LocatorController::class, 'query_trigger')); |
|
| 191 | + |
|
| 192 | + return isset($trigger); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @return ArrayList|DataList |
|
| 197 | + */ |
|
| 198 | + public function getLocations() |
|
| 199 | + { |
|
| 200 | + if (!$this->locations) { |
|
| 201 | + $this->setLocations($this->request); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return $this->locations; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @param HTTPRequest|null $request |
|
| 209 | + * |
|
| 210 | + * @return $this |
|
| 211 | + */ |
|
| 212 | + public function setLocations(HTTPRequest $request = null) |
|
| 213 | + { |
|
| 214 | + |
|
| 215 | + if ($request === null) { |
|
| 216 | + $request = $this->request; |
|
| 217 | + } |
|
| 218 | + $filter = $this->config()->get('base_filter'); |
|
| 219 | + |
|
| 220 | + $categoryVar = $this->data()->config()->get('category_var'); |
|
| 221 | + if ($request->getVar($categoryVar)) { |
|
| 222 | + $filter['Categories.ID'] = $request->getVar($categoryVar); |
|
| 223 | + } else { |
|
| 224 | + if ($this->getPageCategories()->exists()) { |
|
| 225 | + foreach ($this->getPageCategories() as $category) { |
|
| 226 | + $filter['Categories.ID'][] = $category->ID; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + $this->extend('updateLocatorFilter', $filter, $request); |
|
| 232 | + |
|
| 233 | + $filterAny = $this->config()->get('base_filter_any'); |
|
| 234 | + $this->extend('updateLocatorFilterAny', $filterAny, $request); |
|
| 235 | + |
|
| 236 | + $exclude = $this->config()->get('base_exclude'); |
|
| 237 | + $this->extend('updateLocatorExclude', $exclude, $request); |
|
| 238 | + |
|
| 239 | + $class = $this->data()->ClassName; |
|
| 240 | + $locations = $class::get_locations($filter, $filterAny, $exclude); |
|
| 241 | + $locations = DataToArrayListHelper::to_array_list($locations); |
|
| 242 | + |
|
| 243 | + //allow for adjusting list post possible distance calculation |
|
| 244 | + $this->extend('updateLocationList', $locations); |
|
| 245 | + |
|
| 246 | + if ($locations->canSortBy('Distance')) { |
|
| 247 | + $locations = $locations->sort('Distance'); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + if ($this->getShowRadius()) { |
|
| 251 | + $radiusVar = $this->data()->config()->get('radius_var'); |
|
| 252 | + |
|
| 253 | + if ($radius = (int)$request->getVar($radiusVar)) { |
|
| 254 | + $locations = $locations->filterByCallback(function ($location) use (&$radius) { |
|
| 255 | + return $location->Distance <= $radius; |
|
| 256 | + }); |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + //allow for returning list to be set as |
|
| 261 | + $this->extend('updateListType', $locations); |
|
| 262 | + |
|
| 263 | + $limit = $this->getLimit(); |
|
| 264 | + if ($limit > 0) { |
|
| 265 | + $locations = $locations->limit($limit); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $this->locations = $locations; |
|
| 269 | + |
|
| 270 | + return $this; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @return ArrayData|boolean |
|
| 275 | + */ |
|
| 276 | + public function getAddressSearchCoords() |
|
| 277 | + { |
|
| 278 | + $addressVar = Config::inst()->get(DistanceDataExtension::class, 'address_var'); |
|
| 279 | + if (!$this->request->getVar($addressVar)) { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + if (class_exists(GoogleGeocoder::class)) { |
|
| 283 | + $geocoder = new GoogleGeocoder($this->request->getVar($addressVar)); |
|
| 284 | + $response = $geocoder->getResult(); |
|
| 285 | + $lat = $response->getLatitude(); |
|
| 286 | + $lng = $response->getLongitude(); |
|
| 287 | + |
|
| 288 | + return new ArrayData([ |
|
| 289 | + "Lat" => $lat, |
|
| 290 | + "Lng" => $lng, |
|
| 291 | + ]); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * @param HTTPRequest $request |
|
| 297 | + * |
|
| 298 | + * @return \SilverStripe\View\ViewableData_Customised |
|
| 299 | + */ |
|
| 300 | + public function index(HTTPRequest $request) |
|
| 301 | + { |
|
| 302 | + if ($this->getTrigger($request)) { |
|
| 303 | + $locations = $this->getLocations(); |
|
| 304 | + } else { |
|
| 305 | + $locations = ArrayList::create(); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + return $this->customise([ |
|
| 309 | + 'Locations' => $locations, |
|
| 310 | + ]); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Renders locations in xml format |
|
| 315 | + * |
|
| 316 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 317 | + */ |
|
| 318 | + public function xml() |
|
| 319 | + { |
|
| 320 | + $this->getResponse()->addHeader("Content-Type", "application/xml"); |
|
| 321 | + $data = new ArrayData([ |
|
| 322 | + "Locations" => $this->getLocations(), |
|
| 323 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 324 | + ]); |
|
| 325 | + |
|
| 326 | + return $data->renderWith('Dynamic/Locator/Data/XML'); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Renders locations in json format |
|
| 331 | + * |
|
| 332 | + * @return \SilverStripe\ORM\FieldType\DBHTMLText |
|
| 333 | + */ |
|
| 334 | + public function json() |
|
| 335 | + { |
|
| 336 | + $this->getResponse()->addHeader("Content-Type", "application/json"); |
|
| 337 | + $data = new ArrayData([ |
|
| 338 | + "Locations" => $this->getLocations(), |
|
| 339 | + "AddressCoords" => $this->getAddressSearchCoords(), |
|
| 340 | + ]); |
|
| 341 | + |
|
| 342 | + return $data->renderWith('Dynamic/Locator/Data/JSON'); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * LocationSearch form. |
|
| 347 | + * |
|
| 348 | + * Search form for locations, updates map and results list via AJAX |
|
| 349 | + * |
|
| 350 | + * @return \SilverStripe\Forms\Form |
|
| 351 | + */ |
|
| 352 | + public function LocationSearch() |
|
| 353 | + { |
|
| 354 | + |
|
| 355 | + $form = LocatorForm::create($this, 'LocationSearch'); |
|
| 356 | + |
|
| 357 | + $this->extend('updateLocationSearch', $form); |
|
| 358 | + |
|
| 359 | + return $form |
|
| 360 | + ->setFormMethod('GET') |
|
| 361 | + ->setFormAction($this->Link()) |
|
| 362 | + ->disableSecurityToken() |
|
| 363 | + ->loadDataFrom($this->request->getVars()); |
|
| 364 | + } |
|
| 365 | 365 | } |
@@ -17,83 +17,83 @@ |
||
| 17 | 17 | class LocationAdmin extends ModelAdmin |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - private static $managed_models = [ |
|
| 24 | - Location::class, |
|
| 25 | - LocationCategory::class, |
|
| 26 | - ]; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + private static $managed_models = [ |
|
| 24 | + Location::class, |
|
| 25 | + LocationCategory::class, |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - private static $model_importers = [ |
|
| 32 | - Location::class => LocationCsvBulkLoader::class, |
|
| 33 | - LocationCategory::class => CsvBulkLoader::class, |
|
| 34 | - ]; |
|
| 28 | + /** |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + private static $model_importers = [ |
|
| 32 | + Location::class => LocationCsvBulkLoader::class, |
|
| 33 | + LocationCategory::class => CsvBulkLoader::class, |
|
| 34 | + ]; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private static $menu_title = 'Locator'; |
|
| 40 | - /** |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - private static $url_segment = 'locator'; |
|
| 36 | + /** |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private static $menu_title = 'Locator'; |
|
| 40 | + /** |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + private static $url_segment = 'locator'; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - public function getExportFields() |
|
| 49 | - { |
|
| 50 | - if ($this->modelClass == Location::class) { |
|
| 51 | - $fields = [ |
|
| 52 | - 'Title' => 'Name', |
|
| 53 | - 'Address' => 'Address', |
|
| 54 | - 'Address2' => 'Address2', |
|
| 55 | - 'City' => 'City', |
|
| 56 | - 'State' => 'State', |
|
| 57 | - 'PostalCode' => 'PostalCode', |
|
| 58 | - 'CountryCode' => 'Country', |
|
| 59 | - 'Phone' => 'Phone', |
|
| 60 | - 'Fax' => 'Fax', |
|
| 61 | - 'Email' => 'Email', |
|
| 62 | - 'Website' => 'Website', |
|
| 63 | - 'Featured' => 'Featured', |
|
| 64 | - 'CategoryList' => 'Categories', |
|
| 65 | - 'Lat' => 'Lat', |
|
| 66 | - 'Lng' => 'Lng', |
|
| 67 | - 'Import_ID' => 'Import_ID', |
|
| 68 | - ]; |
|
| 69 | - } |
|
| 45 | + /** |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + public function getExportFields() |
|
| 49 | + { |
|
| 50 | + if ($this->modelClass == Location::class) { |
|
| 51 | + $fields = [ |
|
| 52 | + 'Title' => 'Name', |
|
| 53 | + 'Address' => 'Address', |
|
| 54 | + 'Address2' => 'Address2', |
|
| 55 | + 'City' => 'City', |
|
| 56 | + 'State' => 'State', |
|
| 57 | + 'PostalCode' => 'PostalCode', |
|
| 58 | + 'CountryCode' => 'Country', |
|
| 59 | + 'Phone' => 'Phone', |
|
| 60 | + 'Fax' => 'Fax', |
|
| 61 | + 'Email' => 'Email', |
|
| 62 | + 'Website' => 'Website', |
|
| 63 | + 'Featured' => 'Featured', |
|
| 64 | + 'CategoryList' => 'Categories', |
|
| 65 | + 'Lat' => 'Lat', |
|
| 66 | + 'Lng' => 'Lng', |
|
| 67 | + 'Import_ID' => 'Import_ID', |
|
| 68 | + ]; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - if (!isset($fields)) { |
|
| 72 | - $fields = parent::getExportFields(); |
|
| 73 | - } |
|
| 71 | + if (!isset($fields)) { |
|
| 72 | + $fields = parent::getExportFields(); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $this->extend('updateGetExportFields', $fields); |
|
| 75 | + $this->extend('updateGetExportFields', $fields); |
|
| 76 | 76 | |
| 77 | - return $fields; |
|
| 78 | - } |
|
| 77 | + return $fields; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @param null $id |
|
| 82 | - * @param null $fields |
|
| 83 | - * @return $this|Form |
|
| 84 | - */ |
|
| 85 | - public function getEditForm($id = null, $fields = null) |
|
| 86 | - { |
|
| 87 | - $form = parent::getEditForm($id, $fields); |
|
| 80 | + /** |
|
| 81 | + * @param null $id |
|
| 82 | + * @param null $fields |
|
| 83 | + * @return $this|Form |
|
| 84 | + */ |
|
| 85 | + public function getEditForm($id = null, $fields = null) |
|
| 86 | + { |
|
| 87 | + $form = parent::getEditForm($id, $fields); |
|
| 88 | 88 | |
| 89 | - if ($this->modelClass == Location::class) { |
|
| 90 | - /** @var GridField $gridField */ |
|
| 91 | - if ($gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass))) { |
|
| 92 | - $gridField->getConfig() |
|
| 93 | - ->removeComponentsByType('GridFieldDeleteAction'); |
|
| 94 | - } |
|
| 95 | - } |
|
| 89 | + if ($this->modelClass == Location::class) { |
|
| 90 | + /** @var GridField $gridField */ |
|
| 91 | + if ($gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass))) { |
|
| 92 | + $gridField->getConfig() |
|
| 93 | + ->removeComponentsByType('GridFieldDeleteAction'); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return $form; |
|
| 98 | - } |
|
| 97 | + return $form; |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -15,72 +15,72 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class LocationToPageTask extends BuildTask |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected $title = 'Locator - Location to Page Task'; |
|
| 18 | + /** |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected $title = 'Locator - Location to Page Task'; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - private static $segment = 'locator-location-to-page-task'; |
|
| 23 | + /** |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + private static $segment = 'locator-location-to-page-task'; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var bool |
|
| 30 | - */ |
|
| 31 | - private static $auto_publish_location = false; |
|
| 28 | + /** |
|
| 29 | + * @var bool |
|
| 30 | + */ |
|
| 31 | + private static $auto_publish_location = false; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param \SilverStripe\Control\HTTPRequest $request |
|
| 35 | - */ |
|
| 36 | - public function run($request) |
|
| 37 | - { |
|
| 38 | - $this->migrateLocations(); |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * @param \SilverStripe\Control\HTTPRequest $request |
|
| 35 | + */ |
|
| 36 | + public function run($request) |
|
| 37 | + { |
|
| 38 | + $this->migrateLocations(); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * |
|
| 43 | - */ |
|
| 44 | - protected function migrateLocations() |
|
| 45 | - { |
|
| 46 | - if (!$parent = Locator::get()->first()) { |
|
| 47 | - $parent = Locator::create(); |
|
| 48 | - $parent->Title = "Locator"; |
|
| 49 | - $parent->writeToStage(Versioned::DRAFT); |
|
| 50 | - } |
|
| 41 | + /** |
|
| 42 | + * |
|
| 43 | + */ |
|
| 44 | + protected function migrateLocations() |
|
| 45 | + { |
|
| 46 | + if (!$parent = Locator::get()->first()) { |
|
| 47 | + $parent = Locator::create(); |
|
| 48 | + $parent->Title = "Locator"; |
|
| 49 | + $parent->writeToStage(Versioned::DRAFT); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** @var Location $location */ |
|
| 53 | - foreach ($this->getLocations() as $location) { |
|
| 54 | - if (!LocationPage::get()->filter('LegacyObjectID', $location->ID)->first()) { |
|
| 55 | - if ($location->hasMethod('isPublished')) { |
|
| 56 | - $published = $location->isPublished(); |
|
| 57 | - } |
|
| 52 | + /** @var Location $location */ |
|
| 53 | + foreach ($this->getLocations() as $location) { |
|
| 54 | + if (!LocationPage::get()->filter('LegacyObjectID', $location->ID)->first()) { |
|
| 55 | + if ($location->hasMethod('isPublished')) { |
|
| 56 | + $published = $location->isPublished(); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** @var LocationPage $newLocation */ |
|
| 60 | - $newLocation = Injector::inst()->create(LocationPage::class, $location->toMap(), false); |
|
| 61 | - $newLocation->setClassName(LocationPage::class); |
|
| 62 | - $newLocation->ID = null; |
|
| 63 | - $newLocation->ParentID = $parent->ID; |
|
| 64 | - $newLocation->LegacyObjectID = $location->ID; |
|
| 59 | + /** @var LocationPage $newLocation */ |
|
| 60 | + $newLocation = Injector::inst()->create(LocationPage::class, $location->toMap(), false); |
|
| 61 | + $newLocation->setClassName(LocationPage::class); |
|
| 62 | + $newLocation->ID = null; |
|
| 63 | + $newLocation->ParentID = $parent->ID; |
|
| 64 | + $newLocation->LegacyObjectID = $location->ID; |
|
| 65 | 65 | |
| 66 | - $this->extend('preLocationMigrationUpdate', $location, $newLocation); |
|
| 66 | + $this->extend('preLocationMigrationUpdate', $location, $newLocation); |
|
| 67 | 67 | |
| 68 | - $newLocation->writeToStage(Versioned::DRAFT); |
|
| 68 | + $newLocation->writeToStage(Versioned::DRAFT); |
|
| 69 | 69 | |
| 70 | - if ((isset($published) && $published) || $this->config()->get('auto_publish_location')) { |
|
| 71 | - $newLocation->publishSingle(); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - } |
|
| 70 | + if ((isset($published) && $published) || $this->config()->get('auto_publish_location')) { |
|
| 71 | + $newLocation->publishSingle(); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @return \Generator |
|
| 79 | - */ |
|
| 80 | - protected function getLocations() |
|
| 81 | - { |
|
| 82 | - foreach (Location::get() as $location) { |
|
| 83 | - yield $location; |
|
| 84 | - } |
|
| 85 | - } |
|
| 77 | + /** |
|
| 78 | + * @return \Generator |
|
| 79 | + */ |
|
| 80 | + protected function getLocations() |
|
| 81 | + { |
|
| 82 | + foreach (Location::get() as $location) { |
|
| 83 | + yield $location; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -11,76 +11,76 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class LocationPublishTask extends BuildTask |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - protected $title = 'Publish all Locations'; |
|
| 18 | - /** |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected $description = 'Migration task - pre versioning on Location'; |
|
| 22 | - /** |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - protected $enabled = true; |
|
| 14 | + /** |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + protected $title = 'Publish all Locations'; |
|
| 18 | + /** |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected $description = 'Migration task - pre versioning on Location'; |
|
| 22 | + /** |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + protected $enabled = true; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @param $request |
|
| 29 | - */ |
|
| 30 | - public function run($request) |
|
| 31 | - { |
|
| 32 | - $class = ($request->getVar('locationclass')) ? $request->getVar('locationclass') : 'Location'; |
|
| 33 | - $this->publishLocations($class); |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * @param $request |
|
| 29 | + */ |
|
| 30 | + public function run($request) |
|
| 31 | + { |
|
| 32 | + $class = ($request->getVar('locationclass')) ? $request->getVar('locationclass') : 'Location'; |
|
| 33 | + $this->publishLocations($class); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param string $class |
|
| 38 | - * @return \Generator |
|
| 39 | - */ |
|
| 40 | - protected function iterateLocations($class) |
|
| 41 | - { |
|
| 42 | - foreach ($class::get()->filter('ShowInLocator', true) as $location) { |
|
| 43 | - yield $location; |
|
| 44 | - } |
|
| 45 | - } |
|
| 36 | + /** |
|
| 37 | + * @param string $class |
|
| 38 | + * @return \Generator |
|
| 39 | + */ |
|
| 40 | + protected function iterateLocations($class) |
|
| 41 | + { |
|
| 42 | + foreach ($class::get()->filter('ShowInLocator', true) as $location) { |
|
| 43 | + yield $location; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * mark all ProductDetail records as ShowInMenus = 0. |
|
| 49 | - * |
|
| 50 | - * @param string $class |
|
| 51 | - */ |
|
| 52 | - public function publishLocations($class) |
|
| 53 | - { |
|
| 54 | - if (!isset($class) || !class_exists($class) || !$class instanceof Location) { |
|
| 55 | - $class = 'Location'; |
|
| 56 | - } |
|
| 57 | - $ct = 0; |
|
| 58 | - $publish = function ($location) use (&$ct) { |
|
| 59 | - if (!$location->isPublished()) { |
|
| 60 | - $title = $location->Title; |
|
| 61 | - $location->writeToStage('Stage'); |
|
| 62 | - $location->publish('Stage', 'Live'); |
|
| 63 | - static::write_message($title); |
|
| 64 | - ++$ct; |
|
| 65 | - } |
|
| 66 | - }; |
|
| 47 | + /** |
|
| 48 | + * mark all ProductDetail records as ShowInMenus = 0. |
|
| 49 | + * |
|
| 50 | + * @param string $class |
|
| 51 | + */ |
|
| 52 | + public function publishLocations($class) |
|
| 53 | + { |
|
| 54 | + if (!isset($class) || !class_exists($class) || !$class instanceof Location) { |
|
| 55 | + $class = 'Location'; |
|
| 56 | + } |
|
| 57 | + $ct = 0; |
|
| 58 | + $publish = function ($location) use (&$ct) { |
|
| 59 | + if (!$location->isPublished()) { |
|
| 60 | + $title = $location->Title; |
|
| 61 | + $location->writeToStage('Stage'); |
|
| 62 | + $location->publish('Stage', 'Live'); |
|
| 63 | + static::write_message($title); |
|
| 64 | + ++$ct; |
|
| 65 | + } |
|
| 66 | + }; |
|
| 67 | 67 | |
| 68 | - foreach ($this->iterateLocations($class) as $location) { |
|
| 69 | - $publish($location); |
|
| 70 | - } |
|
| 68 | + foreach ($this->iterateLocations($class) as $location) { |
|
| 69 | + $publish($location); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - static::write_message("{$ct} locations updated."); |
|
| 73 | - } |
|
| 72 | + static::write_message("{$ct} locations updated."); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @param $message |
|
| 77 | - */ |
|
| 78 | - protected static function write_message($message) |
|
| 79 | - { |
|
| 80 | - if (Director::is_cli()) { |
|
| 81 | - echo "{$message}\n"; |
|
| 82 | - } else { |
|
| 83 | - echo "{$message}<br><br>"; |
|
| 84 | - } |
|
| 85 | - } |
|
| 75 | + /** |
|
| 76 | + * @param $message |
|
| 77 | + */ |
|
| 78 | + protected static function write_message($message) |
|
| 79 | + { |
|
| 80 | + if (Director::is_cli()) { |
|
| 81 | + echo "{$message}\n"; |
|
| 82 | + } else { |
|
| 83 | + echo "{$message}<br><br>"; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -14,72 +14,72 @@ |
||
| 14 | 14 | class LocationCategoriesTask extends BuildTask |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $title = 'Location categories to many_many'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $description = 'Migration task - Converts locations to have multiple categories'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var bool |
|
| 29 | - */ |
|
| 30 | - protected $enabled = true; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @param $request |
|
| 34 | - */ |
|
| 35 | - public function run($request) |
|
| 36 | - { |
|
| 37 | - /** @var DataObject $class */ |
|
| 38 | - $class = ($request->getVar('locationclass')) ? $request->getVar('locationclass') : Location::class; |
|
| 39 | - $class::add_extension(LocationCategoryExtension::class); |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - $categories = []; |
|
| 43 | - |
|
| 44 | - $convert = function (DataObject $location) use (&$categories) { |
|
| 45 | - /** @var Location $location */ |
|
| 46 | - // skip if no category |
|
| 47 | - if ($location->CategoryID > 0) { |
|
| 48 | - $categories[$location->CategoryID][] = $location->ID; |
|
| 49 | - } |
|
| 50 | - }; |
|
| 51 | - |
|
| 52 | - foreach ($this->iterateLocations($class) as $location) { |
|
| 53 | - $convert($location); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - $catCt = 0; |
|
| 57 | - $locCT = 0; |
|
| 58 | - |
|
| 59 | - foreach ($categories as $categoryID => $locations) { |
|
| 60 | - /** @var LocationCategory $category */ |
|
| 61 | - $category = LocationCategory::get()->byID($categoryID); |
|
| 62 | - $category->Locations()->addMany($locations); |
|
| 63 | - |
|
| 64 | - $catCt++; |
|
| 65 | - $locCT += count($locations); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - echo "{$catCt} categories converted<br />"; |
|
| 69 | - echo "{$locCT} location relations converted<br />"; |
|
| 70 | - |
|
| 71 | - $time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]; |
|
| 72 | - echo "Process Time: {$time} seconds"; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $class |
|
| 77 | - * @return \Generator |
|
| 78 | - */ |
|
| 79 | - protected function iterateLocations($class) |
|
| 80 | - { |
|
| 81 | - foreach ($class::get() as $location) { |
|
| 82 | - yield $location; |
|
| 83 | - } |
|
| 84 | - } |
|
| 17 | + /** |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $title = 'Location categories to many_many'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $description = 'Migration task - Converts locations to have multiple categories'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var bool |
|
| 29 | + */ |
|
| 30 | + protected $enabled = true; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @param $request |
|
| 34 | + */ |
|
| 35 | + public function run($request) |
|
| 36 | + { |
|
| 37 | + /** @var DataObject $class */ |
|
| 38 | + $class = ($request->getVar('locationclass')) ? $request->getVar('locationclass') : Location::class; |
|
| 39 | + $class::add_extension(LocationCategoryExtension::class); |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + $categories = []; |
|
| 43 | + |
|
| 44 | + $convert = function (DataObject $location) use (&$categories) { |
|
| 45 | + /** @var Location $location */ |
|
| 46 | + // skip if no category |
|
| 47 | + if ($location->CategoryID > 0) { |
|
| 48 | + $categories[$location->CategoryID][] = $location->ID; |
|
| 49 | + } |
|
| 50 | + }; |
|
| 51 | + |
|
| 52 | + foreach ($this->iterateLocations($class) as $location) { |
|
| 53 | + $convert($location); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + $catCt = 0; |
|
| 57 | + $locCT = 0; |
|
| 58 | + |
|
| 59 | + foreach ($categories as $categoryID => $locations) { |
|
| 60 | + /** @var LocationCategory $category */ |
|
| 61 | + $category = LocationCategory::get()->byID($categoryID); |
|
| 62 | + $category->Locations()->addMany($locations); |
|
| 63 | + |
|
| 64 | + $catCt++; |
|
| 65 | + $locCT += count($locations); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + echo "{$catCt} categories converted<br />"; |
|
| 69 | + echo "{$locCT} location relations converted<br />"; |
|
| 70 | + |
|
| 71 | + $time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]; |
|
| 72 | + echo "Process Time: {$time} seconds"; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $class |
|
| 77 | + * @return \Generator |
|
| 78 | + */ |
|
| 79 | + protected function iterateLocations($class) |
|
| 80 | + { |
|
| 81 | + foreach ($class::get() as $location) { |
|
| 82 | + yield $location; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class LocationCategoryExtension extends DataExtension |
| 15 | 15 | { |
| 16 | - private static $has_one = [ |
|
| 17 | - 'Category' => LocationCategory::class, |
|
| 18 | - ]; |
|
| 16 | + private static $has_one = [ |
|
| 17 | + 'Category' => LocationCategory::class, |
|
| 18 | + ]; |
|
| 19 | 19 | } |
@@ -23,125 +23,125 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class LocationCategory extends DataObject |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private static $singular_name = 'Category'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private static $plural_name = 'Categories'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - private static $db = [ |
|
| 40 | - 'Name' => 'Varchar(100)', |
|
| 41 | - ]; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - private static $belongs_many_many = [ |
|
| 47 | - 'Locators' => Locator::class, |
|
| 48 | - 'LocationSet' => Location::class, |
|
| 49 | - ]; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - private static $table_name = 'LocationCategory'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - private static $default_sort = 'Name'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @return \SilverStripe\Forms\FieldList |
|
| 63 | - */ |
|
| 64 | - public function getCMSFields() |
|
| 65 | - { |
|
| 66 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 67 | - $fields->removeByName([ |
|
| 68 | - 'Locations', |
|
| 69 | - 'LocationSet', |
|
| 70 | - 'Locators', |
|
| 71 | - 'LinkTracking', |
|
| 72 | - 'FileTracking', |
|
| 73 | - ]); |
|
| 74 | - |
|
| 75 | - if ($this->ID) { |
|
| 76 | - // Locations |
|
| 77 | - $config = GridFieldConfig_RelationEditor::create(); |
|
| 78 | - $config->removeComponentsByType([ |
|
| 79 | - GridFieldAddExistingAutocompleter::class, |
|
| 80 | - GridFieldAddNewButton::class, |
|
| 81 | - ]) |
|
| 82 | - ->addComponents([ |
|
| 83 | - new GridFieldAddExistingSearchButton(), |
|
| 84 | - ]); |
|
| 85 | - $locations = $this->Locations(); |
|
| 86 | - $locationField = GridField::create('Locations', 'Locations', $locations, $config); |
|
| 87 | - |
|
| 88 | - $fields->addFieldsToTab('Root.Main', [ |
|
| 89 | - $locationField, |
|
| 90 | - ]); |
|
| 91 | - } |
|
| 92 | - }); |
|
| 93 | - |
|
| 94 | - $fields = parent::getCMSFields(); |
|
| 95 | - |
|
| 96 | - return $fields; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * For backwards compatability |
|
| 101 | - * @return ManyManyList |
|
| 102 | - */ |
|
| 103 | - public function Locations() |
|
| 104 | - { |
|
| 105 | - return $this->LocationSet(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param null $member |
|
| 110 | - * @param array $context |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function canView($member = null, $context = []) |
|
| 114 | - { |
|
| 115 | - return true; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param null $member |
|
| 120 | - * @param array $context |
|
| 121 | - * @return bool|int |
|
| 122 | - */ |
|
| 123 | - public function canEdit($member = null, $context = []) |
|
| 124 | - { |
|
| 125 | - return Permission::check('Location_EDIT', 'any', $member); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param null $member |
|
| 130 | - * @param array $context |
|
| 131 | - * @return bool|int |
|
| 132 | - */ |
|
| 133 | - public function canDelete($member = null, $context = []) |
|
| 134 | - { |
|
| 135 | - return Permission::check('Location_DELETE', 'any', $member); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param null $member |
|
| 140 | - * @param array $context |
|
| 141 | - * @return bool|int |
|
| 142 | - */ |
|
| 143 | - public function canCreate($member = null, $context = []) |
|
| 144 | - { |
|
| 145 | - return Permission::check('Location_CREATE', 'any', $member); |
|
| 146 | - } |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private static $singular_name = 'Category'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private static $plural_name = 'Categories'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + private static $db = [ |
|
| 40 | + 'Name' => 'Varchar(100)', |
|
| 41 | + ]; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + private static $belongs_many_many = [ |
|
| 47 | + 'Locators' => Locator::class, |
|
| 48 | + 'LocationSet' => Location::class, |
|
| 49 | + ]; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + private static $table_name = 'LocationCategory'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + private static $default_sort = 'Name'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @return \SilverStripe\Forms\FieldList |
|
| 63 | + */ |
|
| 64 | + public function getCMSFields() |
|
| 65 | + { |
|
| 66 | + $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 67 | + $fields->removeByName([ |
|
| 68 | + 'Locations', |
|
| 69 | + 'LocationSet', |
|
| 70 | + 'Locators', |
|
| 71 | + 'LinkTracking', |
|
| 72 | + 'FileTracking', |
|
| 73 | + ]); |
|
| 74 | + |
|
| 75 | + if ($this->ID) { |
|
| 76 | + // Locations |
|
| 77 | + $config = GridFieldConfig_RelationEditor::create(); |
|
| 78 | + $config->removeComponentsByType([ |
|
| 79 | + GridFieldAddExistingAutocompleter::class, |
|
| 80 | + GridFieldAddNewButton::class, |
|
| 81 | + ]) |
|
| 82 | + ->addComponents([ |
|
| 83 | + new GridFieldAddExistingSearchButton(), |
|
| 84 | + ]); |
|
| 85 | + $locations = $this->Locations(); |
|
| 86 | + $locationField = GridField::create('Locations', 'Locations', $locations, $config); |
|
| 87 | + |
|
| 88 | + $fields->addFieldsToTab('Root.Main', [ |
|
| 89 | + $locationField, |
|
| 90 | + ]); |
|
| 91 | + } |
|
| 92 | + }); |
|
| 93 | + |
|
| 94 | + $fields = parent::getCMSFields(); |
|
| 95 | + |
|
| 96 | + return $fields; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * For backwards compatability |
|
| 101 | + * @return ManyManyList |
|
| 102 | + */ |
|
| 103 | + public function Locations() |
|
| 104 | + { |
|
| 105 | + return $this->LocationSet(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param null $member |
|
| 110 | + * @param array $context |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function canView($member = null, $context = []) |
|
| 114 | + { |
|
| 115 | + return true; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param null $member |
|
| 120 | + * @param array $context |
|
| 121 | + * @return bool|int |
|
| 122 | + */ |
|
| 123 | + public function canEdit($member = null, $context = []) |
|
| 124 | + { |
|
| 125 | + return Permission::check('Location_EDIT', 'any', $member); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param null $member |
|
| 130 | + * @param array $context |
|
| 131 | + * @return bool|int |
|
| 132 | + */ |
|
| 133 | + public function canDelete($member = null, $context = []) |
|
| 134 | + { |
|
| 135 | + return Permission::check('Location_DELETE', 'any', $member); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param null $member |
|
| 140 | + * @param array $context |
|
| 141 | + * @return bool|int |
|
| 142 | + */ |
|
| 143 | + public function canCreate($member = null, $context = []) |
|
| 144 | + { |
|
| 145 | + return Permission::check('Location_CREATE', 'any', $member); |
|
| 146 | + } |
|
| 147 | 147 | } |
@@ -28,256 +28,256 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | class Location extends DataObject implements PermissionProvider |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private static $singular_name = 'Location'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private static $plural_name = 'Locations'; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - private static $db = [ |
|
| 45 | - 'Title' => 'Varchar(255)', |
|
| 46 | - 'Featured' => 'Boolean', |
|
| 47 | - 'Website' => 'Varchar(255)', |
|
| 48 | - 'Phone' => 'Varchar(40)', |
|
| 49 | - 'Email' => 'Varchar(255)', |
|
| 50 | - 'Fax' => 'Varchar(45)', |
|
| 51 | - 'Import_ID' => 'Int', |
|
| 52 | - ]; |
|
| 53 | - |
|
| 54 | - private static $many_many = [ |
|
| 55 | - 'Categories' => LocationCategory::class, |
|
| 56 | - ]; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - private static $table_name = 'Location'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var array |
|
| 65 | - */ |
|
| 66 | - private static $casting = [ |
|
| 67 | - 'distance' => 'Decimal(9,3)', |
|
| 68 | - ]; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - private static $default_sort = 'Title'; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * api access via Restful Server module |
|
| 77 | - * |
|
| 78 | - * @var bool |
|
| 79 | - */ |
|
| 80 | - private static $api_access = true; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * search fields for Model Admin |
|
| 84 | - * |
|
| 85 | - * @var array |
|
| 86 | - */ |
|
| 87 | - private static $searchable_fields = [ |
|
| 88 | - 'Title', |
|
| 89 | - 'Address', |
|
| 90 | - 'City', |
|
| 91 | - 'State', |
|
| 92 | - 'PostalCode', |
|
| 93 | - 'Country', |
|
| 94 | - 'Website', |
|
| 95 | - 'Phone', |
|
| 96 | - 'Email', |
|
| 97 | - 'Featured', |
|
| 98 | - ]; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * columns for grid field |
|
| 102 | - * |
|
| 103 | - * @var array |
|
| 104 | - */ |
|
| 105 | - private static $summary_fields = [ |
|
| 106 | - 'Title', |
|
| 107 | - 'Address', |
|
| 108 | - 'Address2', |
|
| 109 | - 'City', |
|
| 110 | - 'State', |
|
| 111 | - 'PostalCode', |
|
| 112 | - 'CountryCode', |
|
| 113 | - 'Phone' => 'Phone', |
|
| 114 | - 'Fax' => 'Fax', |
|
| 115 | - 'Email' => 'Email', |
|
| 116 | - 'Website' => 'Website', |
|
| 117 | - 'Featured', |
|
| 118 | - 'CategoryList', |
|
| 119 | - 'Lat', |
|
| 120 | - 'Lng', |
|
| 121 | - 'Import_ID', |
|
| 122 | - ]; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Coords status for $summary_fields |
|
| 126 | - * |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - public function getCoords() |
|
| 130 | - { |
|
| 131 | - return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false'; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - public function getCategoryList() |
|
| 138 | - { |
|
| 139 | - if ($this->Categories()->count()) { |
|
| 140 | - return implode(', ', $this->Categories()->column('Name')); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return ''; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @return bool|string |
|
| 148 | - */ |
|
| 149 | - public function getCountryCode() |
|
| 150 | - { |
|
| 151 | - if ($this->Country) { |
|
| 152 | - return strtoupper($this->Country); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return false; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * custom labels for fields |
|
| 160 | - * |
|
| 161 | - * @param bool $includerelations |
|
| 162 | - * @return array|string |
|
| 163 | - */ |
|
| 164 | - public function fieldLabels($includerelations = true) |
|
| 165 | - { |
|
| 166 | - $labels = parent::fieldLabels($includerelations); |
|
| 167 | - $labels['Title'] = 'Name'; |
|
| 168 | - $labels['Address2'] = 'Address 2'; |
|
| 169 | - $labels['PostalCode'] = 'Postal Code'; |
|
| 170 | - $labels['Categories.Name'] = 'Categories'; |
|
| 171 | - $labels['Featured.NiceAsBoolean'] = 'Featured'; |
|
| 172 | - |
|
| 173 | - return $labels; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @return FieldList |
|
| 178 | - */ |
|
| 179 | - public function getCMSFields() |
|
| 180 | - { |
|
| 181 | - $this->beforeUpdateCMSFields(function ($fields) { |
|
| 182 | - $fields->removeByName([ |
|
| 183 | - 'Import_ID', |
|
| 184 | - 'LinkTracking', |
|
| 185 | - 'FileTracking', |
|
| 186 | - ]); |
|
| 187 | - |
|
| 188 | - $fields->dataFieldByName('Website') |
|
| 189 | - ->setAttribute('placeholder', 'http://'); |
|
| 190 | - |
|
| 191 | - $fields->replaceField('Email', EmailField::create('Email')); |
|
| 192 | - |
|
| 193 | - $featured = $fields->dataFieldByName('Featured') |
|
| 194 | - ->setDescription('Location will display near the top of the results list'); |
|
| 195 | - $fields->insertAfter( |
|
| 196 | - 'Fax', |
|
| 197 | - $featured |
|
| 198 | - ); |
|
| 199 | - |
|
| 200 | - if ($this->ID) { |
|
| 201 | - $categories = $fields->dataFieldByName('Categories'); |
|
| 202 | - $config = $categories->getConfig(); |
|
| 203 | - $config->removeComponentsByType([ |
|
| 204 | - GridFieldAddExistingAutocompleter::class, |
|
| 205 | - ]) |
|
| 206 | - ->addComponents([ |
|
| 207 | - new GridFieldAddExistingSearchButton(), |
|
| 208 | - ]); |
|
| 209 | - } |
|
| 210 | - }); |
|
| 211 | - |
|
| 212 | - $fields = parent::getCMSFields(); |
|
| 213 | - |
|
| 214 | - // allow to be extended via DataExtension |
|
| 215 | - $this->extend('updateLocationFields', $fields); |
|
| 216 | - |
|
| 217 | - return $fields; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param null $member |
|
| 222 | - * @param array $context |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - public function canView($member = null, $context = []) |
|
| 226 | - { |
|
| 227 | - return true; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @param null $member |
|
| 232 | - * @param array $context |
|
| 233 | - * @return bool|int |
|
| 234 | - */ |
|
| 235 | - public function canEdit($member = null, $context = []) |
|
| 236 | - { |
|
| 237 | - return Permission::check('Location_EDIT', 'any', $member); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @param null $member |
|
| 242 | - * @param array $context |
|
| 243 | - * @return bool|int |
|
| 244 | - */ |
|
| 245 | - public function canDelete($member = null, $context = []) |
|
| 246 | - { |
|
| 247 | - return Permission::check('Location_DELETE', 'any', $member); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param null $member |
|
| 252 | - * @param array $context |
|
| 253 | - * @return bool|int |
|
| 254 | - */ |
|
| 255 | - public function canCreate($member = null, $context = []) |
|
| 256 | - { |
|
| 257 | - return Permission::check('Location_CREATE', 'any', $member); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @return array |
|
| 262 | - */ |
|
| 263 | - public function providePermissions() |
|
| 264 | - { |
|
| 265 | - return [ |
|
| 266 | - 'Location_EDIT' => 'Edit a Location', |
|
| 267 | - 'Location_DELETE' => 'Delete a Location', |
|
| 268 | - 'Location_CREATE' => 'Create a Location', |
|
| 269 | - ]; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - public function getWebsiteURL() |
|
| 276 | - { |
|
| 277 | - $url = $this->Website; |
|
| 278 | - |
|
| 279 | - $this->extend('updateWebsiteURL', $url); |
|
| 280 | - |
|
| 281 | - return $url; |
|
| 282 | - } |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private static $singular_name = 'Location'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private static $plural_name = 'Locations'; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + private static $db = [ |
|
| 45 | + 'Title' => 'Varchar(255)', |
|
| 46 | + 'Featured' => 'Boolean', |
|
| 47 | + 'Website' => 'Varchar(255)', |
|
| 48 | + 'Phone' => 'Varchar(40)', |
|
| 49 | + 'Email' => 'Varchar(255)', |
|
| 50 | + 'Fax' => 'Varchar(45)', |
|
| 51 | + 'Import_ID' => 'Int', |
|
| 52 | + ]; |
|
| 53 | + |
|
| 54 | + private static $many_many = [ |
|
| 55 | + 'Categories' => LocationCategory::class, |
|
| 56 | + ]; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + private static $table_name = 'Location'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var array |
|
| 65 | + */ |
|
| 66 | + private static $casting = [ |
|
| 67 | + 'distance' => 'Decimal(9,3)', |
|
| 68 | + ]; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + private static $default_sort = 'Title'; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * api access via Restful Server module |
|
| 77 | + * |
|
| 78 | + * @var bool |
|
| 79 | + */ |
|
| 80 | + private static $api_access = true; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * search fields for Model Admin |
|
| 84 | + * |
|
| 85 | + * @var array |
|
| 86 | + */ |
|
| 87 | + private static $searchable_fields = [ |
|
| 88 | + 'Title', |
|
| 89 | + 'Address', |
|
| 90 | + 'City', |
|
| 91 | + 'State', |
|
| 92 | + 'PostalCode', |
|
| 93 | + 'Country', |
|
| 94 | + 'Website', |
|
| 95 | + 'Phone', |
|
| 96 | + 'Email', |
|
| 97 | + 'Featured', |
|
| 98 | + ]; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * columns for grid field |
|
| 102 | + * |
|
| 103 | + * @var array |
|
| 104 | + */ |
|
| 105 | + private static $summary_fields = [ |
|
| 106 | + 'Title', |
|
| 107 | + 'Address', |
|
| 108 | + 'Address2', |
|
| 109 | + 'City', |
|
| 110 | + 'State', |
|
| 111 | + 'PostalCode', |
|
| 112 | + 'CountryCode', |
|
| 113 | + 'Phone' => 'Phone', |
|
| 114 | + 'Fax' => 'Fax', |
|
| 115 | + 'Email' => 'Email', |
|
| 116 | + 'Website' => 'Website', |
|
| 117 | + 'Featured', |
|
| 118 | + 'CategoryList', |
|
| 119 | + 'Lat', |
|
| 120 | + 'Lng', |
|
| 121 | + 'Import_ID', |
|
| 122 | + ]; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Coords status for $summary_fields |
|
| 126 | + * |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + public function getCoords() |
|
| 130 | + { |
|
| 131 | + return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false'; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + public function getCategoryList() |
|
| 138 | + { |
|
| 139 | + if ($this->Categories()->count()) { |
|
| 140 | + return implode(', ', $this->Categories()->column('Name')); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return ''; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @return bool|string |
|
| 148 | + */ |
|
| 149 | + public function getCountryCode() |
|
| 150 | + { |
|
| 151 | + if ($this->Country) { |
|
| 152 | + return strtoupper($this->Country); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return false; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * custom labels for fields |
|
| 160 | + * |
|
| 161 | + * @param bool $includerelations |
|
| 162 | + * @return array|string |
|
| 163 | + */ |
|
| 164 | + public function fieldLabels($includerelations = true) |
|
| 165 | + { |
|
| 166 | + $labels = parent::fieldLabels($includerelations); |
|
| 167 | + $labels['Title'] = 'Name'; |
|
| 168 | + $labels['Address2'] = 'Address 2'; |
|
| 169 | + $labels['PostalCode'] = 'Postal Code'; |
|
| 170 | + $labels['Categories.Name'] = 'Categories'; |
|
| 171 | + $labels['Featured.NiceAsBoolean'] = 'Featured'; |
|
| 172 | + |
|
| 173 | + return $labels; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @return FieldList |
|
| 178 | + */ |
|
| 179 | + public function getCMSFields() |
|
| 180 | + { |
|
| 181 | + $this->beforeUpdateCMSFields(function ($fields) { |
|
| 182 | + $fields->removeByName([ |
|
| 183 | + 'Import_ID', |
|
| 184 | + 'LinkTracking', |
|
| 185 | + 'FileTracking', |
|
| 186 | + ]); |
|
| 187 | + |
|
| 188 | + $fields->dataFieldByName('Website') |
|
| 189 | + ->setAttribute('placeholder', 'http://'); |
|
| 190 | + |
|
| 191 | + $fields->replaceField('Email', EmailField::create('Email')); |
|
| 192 | + |
|
| 193 | + $featured = $fields->dataFieldByName('Featured') |
|
| 194 | + ->setDescription('Location will display near the top of the results list'); |
|
| 195 | + $fields->insertAfter( |
|
| 196 | + 'Fax', |
|
| 197 | + $featured |
|
| 198 | + ); |
|
| 199 | + |
|
| 200 | + if ($this->ID) { |
|
| 201 | + $categories = $fields->dataFieldByName('Categories'); |
|
| 202 | + $config = $categories->getConfig(); |
|
| 203 | + $config->removeComponentsByType([ |
|
| 204 | + GridFieldAddExistingAutocompleter::class, |
|
| 205 | + ]) |
|
| 206 | + ->addComponents([ |
|
| 207 | + new GridFieldAddExistingSearchButton(), |
|
| 208 | + ]); |
|
| 209 | + } |
|
| 210 | + }); |
|
| 211 | + |
|
| 212 | + $fields = parent::getCMSFields(); |
|
| 213 | + |
|
| 214 | + // allow to be extended via DataExtension |
|
| 215 | + $this->extend('updateLocationFields', $fields); |
|
| 216 | + |
|
| 217 | + return $fields; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param null $member |
|
| 222 | + * @param array $context |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + public function canView($member = null, $context = []) |
|
| 226 | + { |
|
| 227 | + return true; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @param null $member |
|
| 232 | + * @param array $context |
|
| 233 | + * @return bool|int |
|
| 234 | + */ |
|
| 235 | + public function canEdit($member = null, $context = []) |
|
| 236 | + { |
|
| 237 | + return Permission::check('Location_EDIT', 'any', $member); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @param null $member |
|
| 242 | + * @param array $context |
|
| 243 | + * @return bool|int |
|
| 244 | + */ |
|
| 245 | + public function canDelete($member = null, $context = []) |
|
| 246 | + { |
|
| 247 | + return Permission::check('Location_DELETE', 'any', $member); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param null $member |
|
| 252 | + * @param array $context |
|
| 253 | + * @return bool|int |
|
| 254 | + */ |
|
| 255 | + public function canCreate($member = null, $context = []) |
|
| 256 | + { |
|
| 257 | + return Permission::check('Location_CREATE', 'any', $member); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @return array |
|
| 262 | + */ |
|
| 263 | + public function providePermissions() |
|
| 264 | + { |
|
| 265 | + return [ |
|
| 266 | + 'Location_EDIT' => 'Edit a Location', |
|
| 267 | + 'Location_DELETE' => 'Delete a Location', |
|
| 268 | + 'Location_CREATE' => 'Create a Location', |
|
| 269 | + ]; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + public function getWebsiteURL() |
|
| 276 | + { |
|
| 277 | + $url = $this->Website; |
|
| 278 | + |
|
| 279 | + $this->extend('updateWebsiteURL', $url); |
|
| 280 | + |
|
| 281 | + return $url; |
|
| 282 | + } |
|
| 283 | 283 | } |