File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Routes/RouteService.php
<?php
/**
*
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
namespace KommaApp\Routes;
use KommaApp\Routes\Models\Route;
class RouteService
{
public function saveRouteAlias($route, $alias, $priority)
{
$route = Route::findOrCreate(['route' => $route, 'priority' => $priority]);
$route->alias = $alias;
$route->save();
}
/**
* This method will get all routes for a field value.
* It requires an field, value and can have a condition.
*
* @param $field | Db field
* @param $value | Value
* @param string $condition | =, <,>, !=
* @return mixed
*/
public function getRoutesby($field, $value, $condition = '=')
{
$routes = Route::where($field, $condition, $value);
//If active is set, get By active
$routes = $routes->get();
return $routes;
}
/**
* Get route ids by the routeString
*
* @param $routeString | The string of the route field
* @param null $siteId
* @param null $languageId
* @return mixed | Array
*/
public function getRouteIdsByRoute($routeString)
{
//Call the getRoutesBy method
$routes = $this->getRoutesby('route', $routeString);
//Get the ids from the routes
return $routes->modelKeys();
}
/**
* Set a given value on a given field on multiple routes
*
* @param $routeIds | Array
* @param $field | String
* @param $value | String
*/
public function setFieldOnRouteIds($routeIds, $field, $value)
{
Route::whereIn('id', $routeIds)->update([$field => $value]);
}
/**
* This method will get the active route for an given alias
*
* @param $alias
* @return bool
*/
public function getActiveRouteByAlias($alias, $siteId = null, $languageId = null, $prefix = null)
{
if ($prefix) $alias = $prefix . '/' . $alias;
//todo site and language
//Get the route for the alias
if (!$routes = $this->getRoutesby('alias', $alias)) return false;
if ($routes->count() == 0 && $prefix == null) {
return $this->getActiveRouteByAlias($alias, $siteId, $languageId, \App::getlocale());
}
//Get only the active route
$routes = $routes->where('active', 1);
//Check if count is bigger than 0
if ($routes->count() == 0) return false;
//Return the first route
return $routes->first();
}
/**
* This method will return the alternative route
* for a given alias.
*
* @param $alias
* @return bool
*/
public function getAlternativeRouteByAlias($alias, $prefix = null)
{
if ($prefix) $alias = $prefix . '/' . $alias;
//todo site and language
//Get the route based on the alias, if fails return false
if (!$routes = $this->getRoutesby('alias', $alias)) return false;
if ($routes->count() == 0 && $prefix == null) {
return $this->getAlternativeRouteByAlias($alias, \App::getlocale());
}
if ($routes->count() == 0) return false;
$oldRoute = $routes->first();
//Get all the routes to the same page
if (!$routes = $this->getRoutesby('route', $oldRoute->route)) return false;
$filteredRoutes = $routes->where('active', 1)
->where('language_id', $oldRoute->language_id)
->where('site_id', $oldRoute->site_id)->keyBy('id');
//Check if it is an productRoute
if (preg_match('#products/[0-9]+#', $oldRoute->route)) {
//This is an productRoute, get the route bound to the proudct
$route = $filteredRoutes->where('routeable_type', "KommaApp\\Products\\Models\\Product");
if ($route->count() != 0) return $route->first();
}
//Else return the first route
return $filteredRoutes->first();
}
}