Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If Use with translatable with ?locale="en_US" Page have any RequestVars #119

Open
jripa opened this issue Apr 28, 2017 · 0 comments

Comments

@jripa
Copy link

@jripa jripa commented Apr 28, 2017

Tested on Silverstripe 3.1
/**
* get request Var "locale" and set translatable_current_locale with it
* used on googleSitemap.php to overwrite getSitemaps() and getItems()
* remove -> Translatable::disable_locale_filter();
* add Controller::curr()->setControllerLocale
*
*
* @SInCE 27.4.2017
*/
public function setControllerLocale() {
$locale = '';
$controller = Controller::curr();
$request = $controller->getRequest();
$requestVars = '';
if ($request) {
$requestVars = $request->getVars();
}
$locale = Translatable::get_current_locale();
if (class_exists('Translatable')) {
if ($requestVars) {
$locale = (isset($requestVars["locale"]) ? $requestVars["locale"] : $locale);
}
Translatable::set_current_locale($locale);
}
}

GoogleSitemap.php:

/**
 * The google site map is broken down into multiple smaller files to
 * prevent overbearing a server. By default separate {@link DataObject}
 * records are keep in separate files and broken down into chunks.
 *
 * @return ArrayList
 */
public function getSitemaps()
{
    $countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
    $sitemaps = new ArrayList();
    $filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
    $locale = '';

    if (class_exists('SiteTree')) {
        // move to extension hook. At the moment moduleexists config hook
        // does not work.
        if (class_exists('Translatable')) {
            //Translatable::disable_locale_filter();
            Controller::curr()->setControllerLocale();
               $locale = Translatable::get_current_locale();
        }

        $filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
        $class = 'SiteTree';
        $instances = Versioned::get_by_stage($class, 'Live', $filter);
        $this->extend("alterDataList", $instances, $class);
        $count = $instances->count();

        $neededForPage = ceil($count / $countPerFile);

        for ($i = 1; $i <= $neededForPage; $i++) {
            $lastEdited = $instances
                ->limit($countPerFile, ($i - 1) * $countPerFile)
                ->sort(array())
                ->max('LastEdited');

            $lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d');

            $sitemaps->push(new ArrayData(array(
                'ClassName' => 'SiteTree',
                'LastModified' => $lastModified,
                'Page' => $i. "?locale=".$locale
            )));
        }
    }

    if (count(self::$dataobjects) > 0) {
        foreach (self::$dataobjects as $class => $config) {
            $list = new DataList($class);
            $list = $list->sort('LastEdited ASC');
            $this->extend("alterDataList", $list, $class);
            $neededForClass = ceil($list->count() / $countPerFile);

            for ($i = 1; $i <= $neededForClass; $i++) {
                // determine the last modified date for this slice
                $sliced = $list
                    ->limit($countPerFile, ($i - 1) * $countPerFile)
                    ->last();

                $lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d');

                $sitemaps->push(new ArrayData(array(
                    'ClassName' => $this->sanitiseClassName($class),
                    'Page' => $i. "?locale=".$locale,
                    'LastModified' => $lastModified
                )));
            }
        }
    }

    if (count(self::$routes) > 0) {
        $needed = ceil(count(self::$routes) / $countPerFile);

        for ($i = 1; $i <= $needed; $i++) {
            $sitemaps->push(new ArrayData(array(
                'ClassName' => 'GoogleSitemapRoute',
                'Page' => $i. "?locale=".$locale
            )));
        }
    }

    return $sitemaps;
}

same on:

public function getItems($class, $page = 1)
{
    //normalise the class name
    try {
        $reflectionClass = new ReflectionClass($class);
        $class = $reflectionClass->getName();
    } catch (ReflectionException $e) {
        // this can happen when $class is GoogleSitemapRoute
        //we should try to carry on
    }

    $output = new ArrayList();
    $count = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
    $filter =  Config::inst()->get('GoogleSitemap', 'use_show_in_search');
    $redirector =  Config::inst()->get('GoogleSitemap', 'exclude_redirector_pages');
    // todo migrate to extension hook or DI point for other modules to
    // modify state filters
    if (class_exists('Translatable')) {
        //Translatable::disable_locale_filter();
         Controller::curr()->setControllerLocale();
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.