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

  • ActiveRecordProvider
  • CacheManager
  • Collection
  • Hooks
  • Module
  • Provider
  • TemplateResolver
  • View
  • ViewEditor
  • ViewEditorElement
  • ViewOptions
  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\Views;
 13 
 14 use ICanBoogie\ActiveRecord;
 15 use ICanBoogie\Operation;
 16 use ICanBoogie\PropertyNotDefined;
 17 use ICanBoogie\Route;
 18 
 19 use Icybee\Modules\Cache\Collection as CacheCollection;
 20 use Icybee\Modules\Nodes\Node;
 21 use Icybee\Modules\Sites\Site;
 22 use ICanBoogie\Routing\Pattern;
 23 
 24 class Hooks
 25 {
 26     /*
 27      * EVENTS
 28      */
 29 
 30     /**
 31      * Updates view targets.
 32      *
 33      * @param Operation\ProcessEvent $event
 34      * @param \Icybee\Modules\Pages\SaveOperation $operation
 35      */
 36     static public function on_page_save(Operation\ProcessEvent $event, \Icybee\Modules\Pages\SaveOperation $operation)
 37     {
 38         global $core;
 39 
 40         $request = $event->request;
 41         $contents = $request['contents'];
 42         $editor_ids = $request['editors'];
 43         $nid = $event->response->rc['key'];
 44 
 45         if ($editor_ids)
 46         {
 47             foreach ($editor_ids as $content_id => $editor_id)
 48             {
 49                 if ($editor_id != 'view')
 50                 {
 51                     continue;
 52                 }
 53 
 54                 if (empty($contents[$content_id]))
 55                 {
 56                     // TODO-20120811: should remove view reference
 57 
 58                     continue;
 59                 }
 60 
 61                 $content = $contents[$content_id];
 62 
 63                 if (strpos($content, '/') !== false)
 64                 {
 65                     $view_target_key = 'views.targets.' . strtr($content, '.', '_');
 66 
 67                     $core->site->metas[$view_target_key] = $nid;
 68                 }
 69             }
 70         }
 71     }
 72 
 73     /**
 74      * Adds views cache manager to the cache collection.
 75      *
 76      * @param CacheCollection\CollectEvent $event
 77      * @param CacheCollection $collection
 78      */
 79     static public function on_cache_collection_collect(CacheCollection\CollectEvent $event, CacheCollection $collection)
 80     {
 81         $event->collection['icybee.views'] = new CacheManager;
 82     }
 83 
 84     /*
 85      * PROTOTYPE
 86      */
 87 
 88     static private $pages_model;
 89     static private $url_cache_by_siteid = array();
 90 
 91     /**
 92      * Returns the relative URL of a record for a view type.
 93      *
 94      * @param ActiveRecord $target
 95      * @param string $type View type.
 96      *
 97      * @return string
 98      */
 99     static public function url(ActiveRecord $target, $type='view')
100     {
101         global $core;
102 
103         if (self::$pages_model === false)
104         {
105             #
106             # we were not able to get the "pages" model in a previous call, we don't try again.
107             #
108 
109             return '#';
110         }
111         else
112         {
113             try
114             {
115                 self::$pages_model = $core->models['pages'];
116             }
117             catch (\Exception $e)
118             {
119                 return '#';
120             }
121         }
122 
123         $constructor = isset($target->constructor) ? $target->constructor : $target->model->id;
124         $constructor = strtr($constructor, '.', '_');
125 
126         $key = 'views.targets.' . $constructor . '/' . $type;
127         $site_id = !empty($target->siteid) ? $target->siteid : $core->site_id;
128 
129         if (isset(self::$url_cache_by_siteid[$site_id][$key]))
130         {
131             $pattern = self::$url_cache_by_siteid[$site_id][$key];
132         }
133         else
134         {
135             $pattern = false;
136             $page_id = null;
137 
138             if ($site_id)
139             {
140                 $site = $core->models['sites'][$site_id];
141                 $page_id = $site->metas[$key];
142 
143                 if ($page_id)
144                 {
145                     $pattern = self::$pages_model[$page_id]->url_pattern;
146                 }
147             }
148 
149             self::$url_cache_by_siteid[$site_id][$key] = $pattern;
150         }
151 
152         if (!$pattern)
153         {
154             return '#uknown-target-for:' . $constructor . '/' . $type;
155         }
156 
157         return Pattern::from($pattern)->format($target);
158     }
159 
160     /**
161      * Return the URL type 'view' for the node.
162      *
163      * @param Node $node
164      */
165     static public function get_url(ActiveRecord $node)
166     {
167         return $node->url('view');
168     }
169 
170     /**
171      * Return the absolute URL type for the node.
172      *
173      * @param Node $node
174      * @param string $type The URL type.
175      */
176     static public function absolute_url(ActiveRecord $node, $type='view')
177     {
178         global $core;
179 
180         try
181         {
182             $site = $node->site ? $node->site : $core->site;
183         }
184         catch (PropertyNotDefined $e)
185         {
186             $site = $core->site;
187         }
188 
189         return $site->url . substr($node->url($type), strlen($site->path));
190     }
191 
192     /**
193      * Return the _primary_ absolute URL for the node.
194      *
195      * @param Node $node
196      *
197      * @return string The primary absolute URL for the node.
198      */
199     static public function get_absolute_url(Node $node)
200     {
201         return $node->absolute_url('view');
202     }
203 
204     static private $view_target_cache = array();
205 
206     /**
207      * Returns the target page of a view.
208      *
209      * @param Site $site
210      * @param string $viewid Identifier of the view.
211      *
212      * @return \Icybee\Modules\Pages\Page
213      */
214     static public function resolve_view_target(Site $site, $viewid)
215     {
216         global $core;
217 
218         if (isset(self::$view_target_cache[$viewid]))
219         {
220             return self::$view_target_cache[$viewid];
221         }
222 
223         $targetid = $site->metas['views.targets.' . strtr($viewid, '.', '_')];
224 
225         return self::$view_target_cache[$viewid] = $targetid ? $core->models['pages'][$targetid] : false;
226     }
227 
228     static private $view_url_cache = array();
229 
230     /**
231      * Returns the URL of a view.
232      *
233      * @param Site $site
234      * @param string $viewid
235      *
236      * @return string
237      */
238     static public function resolve_view_url(Site $site, $viewid)
239     {
240         if (isset(self::$view_url_cache[$viewid]))
241         {
242             return self::$view_url_cache[$viewid];
243         }
244 
245         $target = $site->resolve_view_target($viewid);
246 
247         return self::$view_url_cache[$viewid] = $target ? $target->url : '#unknown-target-for-view-' . $viewid;
248     }
249 
250     /*
251      * MARKUPS
252      */
253 
254     /**
255      * Renders the specified view.
256      *
257      * @param array $args
258      * @param mixed $engine
259      * @param mixed $template
260      *
261      * @return mixed
262      */
263     static public function markup_call_view(array $args, $engine, $template)
264     {
265         global $core;
266 
267         return $core->editors['view']->render($args['name'], $engine, $template);
268     }
269 }
Autodoc API documentation generated by ApiGen 2.8.0