Example #1
0
 /**
  * test stripLanguagePrefix() function.
  *
  * @return void
  */
 public function testStripLanguagePrefix()
 {
     $tests = ['http://www.example.com/es_ES/article/demo1.html' => 'http://www.example.com/article/demo1.html', 'http://www.example.com/en_US/article/demo2.html' => 'http://www.example.com/article/demo2.html', '/es_ES/article/demo3.html' => '/article/demo3.html', '/en_US/article/demo4.html' => '/article/demo4.html', 'es_ES/article/demo5.html' => '/article/demo5.html', 'en_US/article/demo6.html' => '/article/demo6.html', 'http://www.example.es_ES/en_US/article/demo7.html' => 'http://www.example.es_ES/article/demo7.html'];
     foreach ($tests as $input => $expected) {
         $actual = stripLanguagePrefix($input);
         $this->assertEquals($expected, $actual);
     }
 }
 /**
  * Calculates the URL to which visitor should be redirected according to
  * content's & visitor's language.
  *
  * @param \Cake\Datasource\EntityInterface $content Content to inspect
  * @return string Redirection URL, empty on error
  */
 protected function _calculateRedirection(EntityInterface $content)
 {
     foreach (['translation', 'parent'] as $method) {
         if ($has = $content->{$method}()) {
             return option('url_locale_prefix') ? '/' . $has->get('language') . stripLanguagePrefix($has->get('url')) : $has->get('url');
         }
     }
     return '';
 }
 /**
  * Adds a new link to the given menu.
  *
  * @param int $menuId Menu's ID for which add a link
  * @return void
  */
 public function add($menuId)
 {
     $this->loadModel('Menu.Menus');
     $this->loadModel('Content.Contents');
     $menu = $this->Menus->get($menuId);
     $link = $this->Menus->MenuLinks->newEntity();
     $link->set(['activation' => 'auto', 'status' => 1, 'menu_id' => $menuId]);
     $this->Menus->MenuLinks->addBehavior('Tree', ['scope' => ['menu_id' => $menu->id]]);
     if ($this->request->data()) {
         $link = $this->Menus->MenuLinks->patchEntity($link, $this->request->data, ['fieldList' => ['parent_id', 'title', 'url', 'description', 'target', 'expanded', 'active', 'activation', 'status']]);
         if ($this->Menus->MenuLinks->save($link)) {
             $this->Menus->MenuLinks->recover();
             $this->Flash->success(__d('menu', 'Link successfully created!'));
             if (!empty($this->request->data['action_add'])) {
                 $this->redirect(['plugin' => 'Menu', 'controller' => 'links', 'action' => 'add', $menuId]);
             } elseif (!empty($this->request->data['action_menu'])) {
                 $this->redirect(['plugin' => 'Menu', 'controller' => 'links', 'action' => 'menu', $menuId]);
             }
         } else {
             $this->Flash->danger(__d('menu', 'Link could not be saved, please check your information'));
         }
     }
     $contentLinks = [];
     $contents = $this->Contents->find()->select(['id', 'slug', 'content_type_slug', 'title'])->all();
     foreach ($contents as $content) {
         $contentLinks[stripLanguagePrefix($content->get('url'))] = __d('menu', '{0} [{1}]', [$content->title, $content->content_type_slug]);
     }
     $parentsTree = $this->Menus->MenuLinks->find('treeList', ['spacer' => '--'])->map(function ($link) {
         if (strpos($link, '-') !== false) {
             $link = str_replace_last('-', '- ', $link);
         }
         return $link;
     });
     $this->title(__d('menu', 'Create New Link'));
     $this->set(compact('menu', 'link', 'contentLinks', 'parentsTree'));
     $this->Breadcrumb->push('/admin/menu/manage')->push(__d('menu', 'Editing menu "{0}"', $menu->title), ['plugin' => 'Menu', 'controller' => 'manage', 'action' => 'edit', $menuId])->push(__d('menu', 'Links'), ['plugin' => 'Menu', 'controller' => 'links', 'action' => 'menu', $menuId])->push(__d('menu', 'Add new link'), '#');
 }