Exemplo n.º 1
0
 /**
  * Create a new LanguageDropdownField
  * @param string $name
  * @param string $title
  * @param array $dontInclude list of languages that won't be included
  * @param string $translatingClass Name of the class with translated instances where to look for used languages
  * @param string $list Indicates the source language list. Can be either Common-English, Common-Native  Locale
  */
 function __construct($name, $title, $dontInclude = array(), $translatingClass = 'SiteTree', $list = 'Common-English')
 {
     $usedlangs = array_diff(i18n::get_existing_content_languages($translatingClass), $dontInclude);
     // we accept in dontInclude both language codes and names, so another diff is required
     $usedlangs = array_diff($usedlangs, array_flip($dontInclude));
     if (isset($usedlangs[Translatable::default_lang()])) {
         unset($usedlangs[Translatable::default_lang()]);
     }
     if ('Common-English' == $list) {
         $languageList = i18n::get_common_languages();
     } else {
         if ('Common-Native' == $list) {
             $languageList = i18n::get_common_languages(true);
         } else {
             $languageList = i18n::get_locale_list();
         }
     }
     $alllangs = array_diff($languageList, (array) $usedlangs, $dontInclude);
     $alllangs = array_flip(array_diff(array_flip($alllangs), $dontInclude));
     if (isset($alllangs[Translatable::default_lang()])) {
         unset($alllangs[Translatable::default_lang()]);
     }
     asort($alllangs);
     if (count($usedlangs)) {
         asort($usedlangs);
         $labelAvail = _t('Form.LANGAVAIL', "Available languages");
         $labelOther = _t('Form.LANGAOTHER', "Other languages");
         parent::__construct($name, $title, array($labelAvail => $usedlangs, $labelOther => $alllangs), reset($usedlangs));
     } else {
         parent::__construct($name, $title, $alllangs);
     }
 }
 /**
  * Create a new LanguageDropdownField
  * @param string $name
  * @param string $title
  * @param array $excludeLocales List of locales that won't be included
  * @param string $translatingClass Name of the class with translated instances where to look for used languages
  * @param string $list Indicates the source language list. Can be either Common-English, Common-Native, Locale-English, Locale-Native
  */
 function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null)
 {
     $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
     $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
     if ('Common-English' == $list) {
         $allLocalesWithTitle = i18n::get_common_languages();
     } else {
         if ('Common-Native' == $list) {
             $allLocalesWithTitle = i18n::get_common_languages(true);
         } else {
             if ('Locale-English' == $list) {
                 $allLocalesWithTitle = i18n::get_common_locales();
             } else {
                 if ('Locale-Native' == $list) {
                     $allLocalesWithTitle = i18n::get_common_locales(true);
                 } else {
                     $allLocalesWithTitle = i18n::get_locale_list();
                 }
             }
         }
     }
     if (isset($allLocales[Translatable::default_locale()])) {
         unset($allLocales[Translatable::default_locale()]);
     }
     // Limit to allowed locales if defined
     // Check for canTranslate() if an $instance is given
     $allowedLocales = Translatable::get_allowed_locales();
     foreach ($allLocalesWithTitle as $locale => $localeTitle) {
         if ($allowedLocales && !in_array($locale, $allowedLocales) || $excludeLocales && in_array($locale, $excludeLocales) || $usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle)) {
             unset($allLocalesWithTitle[$locale]);
         }
     }
     // instance specific permissions
     foreach ($allLocalesWithTitle as $locale => $localeTitle) {
         if ($instance && !$instance->canTranslate(null, $locale)) {
             unset($allLocalesWithTitle[$locale]);
         }
     }
     foreach ($usedLocalesWithTitle as $locale => $localeTitle) {
         if ($instance && !$instance->canTranslate(null, $locale)) {
             unset($usedLocalesWithTitle[$locale]);
         }
     }
     // Sort by title (array value)
     asort($allLocalesWithTitle);
     if (count($usedLocalesWithTitle)) {
         asort($usedLocalesWithTitle);
         $source = array(_t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle, _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle);
     } else {
         $source = $allLocalesWithTitle;
     }
     parent::__construct($name, $title, $source);
 }
 /**
  * Overloaded to avoid "action doesn't exist" errors - all URL parts in
  * this controller are virtual and handled through handleRequest(), not
  * controller methods.
  *
  * @param $request
  * @param $action
  *
  * @return SS_HTTPResponse
  */
 public function handleAction($request, $action)
 {
     // if we submitted a form, let that pass
     if (!$request->isGET()) {
         return parent::handleAction($request, $action);
     }
     $url = $request->getURL();
     //
     // If the current request has an extension attached to it, strip that
     // off and redirect the user to the page without an extension.
     //
     if (DocumentationHelper::get_extension($url)) {
         $this->response = new SS_HTTPResponse();
         $this->response->redirect(DocumentationHelper::trim_extension_off($url) . '/', 301);
         $request->shift();
         $request->shift();
         return $this->response;
     }
     //
     // Strip off the base url
     //
     $base = ltrim(Config::inst()->get('DocumentationViewer', 'link_base'), '/');
     if ($base && strpos($url, $base) !== false) {
         $url = substr(ltrim($url, '/'), strlen($base));
     } else {
     }
     //
     // Handle any permanent redirections that the developer has defined.
     //
     if ($link = DocumentationPermalinks::map($url)) {
         // the first param is a shortcode for a page so redirect the user to
         // the short code.
         $this->response = new SS_HTTPResponse();
         $this->response->redirect($link, 301);
         $request->shift();
         $request->shift();
         return $this->response;
     }
     //
     // Validate the language provided. Language is a required URL parameter.
     // as we use it for generic interfaces and language selection. If
     // language is not set, redirects to 'en'
     //
     $languages = i18n::get_common_languages();
     if (!($lang = $request->param('Lang'))) {
         $lang = $request->param('Action');
         $action = $request->param('ID');
     } else {
         $action = $request->param('Action');
     }
     if (!$lang) {
         return $this->redirect($this->Link('en'));
     } else {
         if (!isset($languages[$lang])) {
             return $this->httpError(404);
         }
     }
     $request->shift(10);
     $allowed = $this->config()->allowed_actions;
     if (in_array($action, $allowed)) {
         //
         // if it's one of the allowed actions such as search or all then the
         // URL must be prefixed with one of the allowed languages.
         //
         return parent::handleAction($request, $action);
     } else {
         //
         // look up the manifest to see find the nearest match against the
         // list of the URL. If the URL exists then set that as the current
         // page to match against.
         // strip off any extensions.
         // if($cleaned !== $url) {
         // 	$redirect = new SS_HTTPResponse();
         // 	return $redirect->redirect($cleaned, 302);
         // }
         if ($record = $this->getManifest()->getPage($url)) {
             $this->record = $record;
             $this->init();
             $type = get_class($this->record);
             $body = $this->renderWith(array("DocumentationViewer_{$type}", "DocumentationViewer"));
             return new SS_HTTPResponse($body, 200);
         } else {
             if ($redirect = $this->getManifest()->getRedirect($url)) {
                 $response = new SS_HTTPResponse();
                 $to = Controller::join_links(Director::baseURL(), $base, $redirect);
                 return $response->redirect($to, 301);
             } else {
                 if (!$url || $url == $lang) {
                     $body = $this->renderWith(array("DocumentationViewer_DocumentationFolder", "DocumentationViewer"));
                     return new SS_HTTPResponse($body, 200);
                 }
             }
         }
     }
     return $this->httpError(404);
 }
 /**
  * Sets up the top level entities.
  *
  * Either manually registered through the YAML syntax or automatically
  * loaded through investigating the file system for `docs` folder.
  */
 public function setupEntities()
 {
     if ($this->registeredEntities->Count() > 0) {
         return;
     }
     if (Config::inst()->get('DocumentationManifest', 'automatic_registration')) {
         $this->populateEntitiesFromInstall();
     }
     $registered = Config::inst()->get('DocumentationManifest', 'register_entities');
     foreach ($registered as $details) {
         // validate the details provided through the YAML configuration
         $required = array('Path', 'Title');
         foreach ($required as $require) {
             if (!isset($details[$require])) {
                 throw new Exception("{$require} is a required key in DocumentationManifest.register_entities");
             }
         }
         // if path is not an absolute value then assume it is relative from
         // the BASE_PATH.
         $path = $this->getRealPath($details['Path']);
         $key = isset($details['Key']) ? $details['Key'] : $details['Title'];
         if (!$path || !is_dir($path)) {
             throw new Exception($details['Path'] . ' is not a valid documentation directory');
         }
         $version = isset($details['Version']) ? $details['Version'] : '';
         $branch = isset($details['Branch']) ? $details['Branch'] : '';
         $langs = scandir($path);
         if ($langs) {
             $possible = i18n::get_common_languages(true);
             foreach ($langs as $k => $lang) {
                 if (isset($possible[$lang])) {
                     $entity = Injector::inst()->create('DocumentationEntity', $key);
                     $entity->setPath(Controller::join_links($path, $lang, '/'));
                     $entity->setTitle($details['Title']);
                     $entity->setLanguage($lang);
                     $entity->setVersion($version);
                     $entity->setBranch($branch);
                     if (isset($details['Stable'])) {
                         $entity->setIsStable($details['Stable']);
                     }
                     if (isset($details['DefaultEntity'])) {
                         $entity->setIsDefaultEntity($details['DefaultEntity']);
                     }
                     $this->registeredEntities->push($entity);
                 }
             }
         }
     }
 }
 /**
  * Overloaded to avoid "action doesn't exist" errors - all URL parts in this controller are virtual and handled through handleRequest(), not controller methods.
  * @param {SS_HTTPRequest} $request
  * @param {string} $action
  * @return {SS_HTTPResponse}
  */
 public function handleAction($request, $action)
 {
     // if we submitted a form, let that pass
     if (!$request->isGET()) {
         return parent::handleAction($request, $action);
     }
     $url = $request->getURL();
     //If the current request has an extension attached to it, strip that off and redirect the user to the page without an extension.
     if (DocumentationHelper::get_extension($url)) {
         $request->shift();
         $request->shift();
         return $this->redirect(Director::absoluteURL(DocumentationHelper::trim_extension_off($url)) . '/', 301);
     }
     //Strip off the base url
     $base = ltrim(Config::inst()->get('DocumentationViewer', 'link_base'), '/');
     if ($base && strpos($url, $base) !== false) {
         $url = substr(ltrim($url, '/'), strlen($base));
     }
     //Handle any permanent redirections that the developer has defined.
     if ($link = DocumentationPermalinks::map($url)) {
         $request->shift();
         $request->shift();
         //the first param is a shortcode for a page so redirect the user to the short code.
         return $this->redirect($link, 301);
     }
     //Validate the language provided. Language is a required URL parameter. as we use it for generic interfaces and language selection.
     //If language is not set, redirects to 'en'
     $languages = i18n::get_common_languages();
     if (!($lang = $request->param('Lang'))) {
         $lang = $request->param('Action');
         $action = $request->param('ID');
     } else {
         $action = $request->param('Action');
     }
     if (!$lang) {
         return $this->redirect($this->Link('en'));
     } else {
         if (!isset($languages[$lang])) {
             return $this->httpError(404);
         }
     }
     $request->shift(10);
     $allowed = $this->config()->allowed_actions;
     if (in_array($action, $allowed)) {
         //if it's one of the allowed actions such as search or all then the URL must be prefixed with one of the allowed languages.
         return parent::handleAction($request, $action);
     } else {
         //look up the manifest to see find the nearest match against the list of the URL. If the URL exists then set that as the current page to match against. strip off any extensions.
         if ($record = $this->getManifest()->getPage($url)) {
             $this->record = $record;
             return $this->getResponseNegotiator()->respond($request);
         } else {
             if ($redirect = $this->getManifest()->getRedirect($url)) {
                 $to = Controller::join_links(Director::baseURL(), $base, $redirect);
                 return $this->redirect($to, 301);
             } else {
                 if (!$url || $url == $lang) {
                     return $this->getResponseNegotiator()->respond($request);
                 } else {
                     $url = explode('/', $url);
                     $url = implode('/', array_map(function ($a) {
                         return DocumentationHelper::clean_page_url($a);
                     }, $url));
                     if ($record = $this->getManifest()->getPage($url)) {
                         return $this->redirect($record->Link(), 301);
                     }
                 }
             }
         }
     }
     return $this->httpError(404);
 }