1 <?php
  2 
  3   4   5   6   7   8   9  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  59  60  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 
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 162 
163 class CollectEvent extends \ICanBoogie\Event
164 {
165     166 167 168 169 
170     public $languages;
171 
172     173 174 175 176 177 
178     public function __construct(\Icybee\Modules\Pages\LanguagesElement $target, array $payload)
179     {
180         parent::__construct($target, 'collect', $payload);
181     }
182 }
183 
184 185 186 
187 class AlterEvent extends \ICanBoogie\Event
188 {
189     190 191 192 193 
194     public $links;
195 
196     197 198 199 200 
201     public $languages;
202 
203     204 205 206 207 
208     public $page;
209 
210     211 212 213 214 215 
216     public function __construct(\Icybee\Modules\Pages\LanguagesElement $target, array $payload)
217     {
218         parent::__construct($target, 'alter', $payload);
219     }
220 }