Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • AlterEvent
  • CollectEvent
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace Icybee\Modules\Pages;
 13 
 14 use ICanBoogie\ActiveRecord;
 15 use ICanBoogie\Event;
 16 
 17 use Brickrouge\Element;
 18 use Brickrouge\ElementIsEmpty;
 19 
 20 class LanguagesElement extends Element
 21 {
 22     static public function markup(array $args, \Patron\Engine $patron, $template)
 23     {
 24         if ($template)
 25         {
 26             throw new \Exception('Templates are currently not supported :(');
 27         }
 28 
 29         return new static();
 30     }
 31 
 32     public function __construct(array $attributes=array())
 33     {
 34         parent::__construct
 35         (
 36             'div', array
 37             (
 38                 'class' => 'btn-group i18n-languages'
 39             )
 40         );
 41     }
 42 
 43     protected function render_inner_html()
 44     {
 45         global $core;
 46 
 47         $page = $core->request->context->page;
 48         $translations_by_language = $this->collect();
 49 
 50         new LanguagesElement\CollectEvent($this, array('languages' => &$translations_by_language));
 51 
 52         if (count($translations_by_language) == 1)
 53         {
 54             throw new ElementIsEmpty;
 55         }
 56 
 57         /*
 58         if ($template)
 59         {
 60             return $patron($template, $translations_by_language);
 61         }
 62         */
 63 
 64         $page_language = $page->language;
 65         $links = array();
 66 
 67         foreach ($translations_by_language as $language => $record)
 68         {
 69             $link = new Element
 70             (
 71                 'a', array
 72                 (
 73                     Element::INNER_HTML => $language,
 74 
 75                     'class' => 'btn language--' . \Brickrouge\normalize($language),
 76                     'href' => $record->url
 77                 )
 78             );
 79 
 80             if ($language == $page_language)
 81             {
 82                 $link->add_class('active');
 83             }
 84 
 85             $links[$language] = $link;
 86         }
 87 
 88         new LanguagesElement\AlterEvent($this, array('links' => &$links, 'languages' => &$translations_by_language, 'page' => $page));
 89 
 90         return implode('', $links);
 91     }
 92 
 93     protected function collect()
 94     {
 95         global $core;
 96 
 97         $page = $core->request->context->page;
 98         $source = $page->node ?: $page;
 99         $translations = $source->translations;
100         $translations_by_language = array();
101 
102         if ($translations)
103         {
104             $translations[$source->nid] = $source;
105             $translations_by_language = array_flip
106             (
107                 $core->models['sites']->select('language')->where('status = 1')->order('weight, siteid')->all(\PDO::FETCH_COLUMN)
108             );
109 
110             if ($source instanceof Page)
111             {
112                 foreach ($translations as $translation)
113                 {
114                     if (!$translation->is_accessible)
115                     {
116                         continue;
117                     }
118 
119                     $translations_by_language[$translation->language] = $translation;
120                 }
121             }
122             else // nodes
123             {
124                 foreach ($translations as $translation)
125                 {
126                     if (!$translation->is_online)
127                     {
128                         continue;
129                     }
130 
131                     $translations_by_language[$translation->language] = $translation;
132                 }
133             }
134 
135             foreach ($translations_by_language as $language => $translation)
136             {
137                 if (is_object($translation))
138                 {
139                     continue;
140                 }
141 
142                 unset($translations_by_language[$language]);
143             }
144         }
145 
146         if (!$translations_by_language)
147         {
148             $translations_by_language = array
149             (
150                 ($source->language ? $source->language : $page->language) => $source
151             );
152         }
153 
154         return $translations_by_language;
155     }
156 }
157 
158 namespace Icybee\Modules\Pages\LanguagesElement;
159 
160 /**
161  * Event class for the `Icybee\Modules\Pages\LanguagesElement::collect` event.
162  */
163 class CollectEvent extends \ICanBoogie\Event
164 {
165     /**
166      * Reference to the languages.
167      *
168      * @var array[string]\ICanBoogie\ActiveRecord
169      */
170     public $languages;
171 
172     /**
173      * The event is constructed with the `render:before` event.
174      *
175      * @param \Icybee\Modules\Pages\LanguagesElement $target
176      * @param array $payload
177      */
178     public function __construct(\Icybee\Modules\Pages\LanguagesElement $target, array $payload)
179     {
180         parent::__construct($target, 'collect', $payload);
181     }
182 }
183 
184 /**
185  * Event class for the `Icybee\Modules\Pages\LanguagesElement::alter` event.
186  */
187 class AlterEvent extends \ICanBoogie\Event
188 {
189     /**
190      * Reference to the links array.
191      *
192      * @var array[string]\Brickrouge\Element
193      */
194     public $links;
195 
196     /**
197      * Reference to the language records.
198      *
199      * @var array[string]\ICanBoogie\ActiveRecord
200      */
201     public $languages;
202 
203     /**
204      * The current page.
205      *
206      * @var \Icybee\Modules\Pages\Page
207      */
208     public $page;
209 
210     /**
211      * The event is constructed with the `alter` event.
212      *
213      * @param \Icybee\Modules\Pages\LanguagesElement $target
214      * @param array $payload
215      */
216     public function __construct(\Icybee\Modules\Pages\LanguagesElement $target, array $payload)
217     {
218         parent::__construct($target, 'alter', $payload);
219     }
220 }
Autodoc API documentation generated by ApiGen 2.8.0