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

  • CacheManager
  • ConfigBlock
  • ConfigOperation
  • Content
  • DeleteBlock
  • EditBlock
  • HomeExcludeOperation
  • HomeIncludeOperation
  • Hooks
  • ManageBlock
  • Model
  • Module
  • Rendered
  • SaveOperation
  • Update20140330
  • ViewProvider

Traits

  • DateProperty
  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\Contents;
 13 
 14 use ICanBoogie\HTTP\ForceRedirect;
 15 
 16 use ICanBoogie\ActiveRecord;
 17 use ICanBoogie\ActiveRecord\Query;
 18 use ICanBoogie\ActiveRecord\RecordNotFound;
 19 
 20 class ViewProvider extends \Icybee\Modules\Nodes\ViewProvider
 21 {
 22     /**
 23      * Tries to rescue the record if finding the record failed.
 24      */
 25     public function __invoke()
 26     {
 27         $rc = parent::__invoke();
 28 
 29         if (!$rc && $this->returns == self::RETURNS_ONE)
 30         {
 31             $rc = $this->rescue();
 32         }
 33 
 34         return $rc;
 35     }
 36 
 37     /**
 38      * Support for the `year`, `month` and `day` conditions. Changes the order to
 39      * `date DESC, created_at DESC`.
 40      *
 41      * If the view is of type "home" the query is altered to search for nodes which are not
 42      * excluded from _home_.
 43      */
 44     protected function alter_query(Query $query, array $conditions)
 45     {
 46         foreach ($conditions as $property => $value)
 47         {
 48             switch ($property)
 49             {
 50                 case 'year':
 51                     $query->where('YEAR(date) = ?', (int) $value);
 52                     break;
 53 
 54                 case 'month':
 55                     $query->where('MONTH(date) = ?', (int) $value);
 56                     break;
 57 
 58                 case 'day':
 59                     $query->where('DAY(date) = ?', (int) $value);
 60                     break;
 61             }
 62         }
 63 
 64         if ($this->view->type == 'home')
 65         {
 66             $query->where('is_home_excluded = 0');
 67         }
 68 
 69         return parent::alter_query($query, $conditions)->order('date DESC, created_at DESC');
 70     }
 71 
 72     /**
 73      * Rescues a missing record by providing the best matching one.
 74      *
 75      * Match is computed from the slug of the module's own visible records, thus rescue if only
 76      * triggered if 'slug' is defined in the conditions.
 77      *
 78      * @return Content|null The record best matching the condition slug, or null if
 79      * none was similar enough.
 80      *
 81      * @throws RecordNotFound if the record could not be rescued.
 82      *
 83      * @todo-20140429 This should be in an exception rescue listener.
 84      */
 85     protected function rescue()
 86     {
 87         $conditions = $this->conditions;
 88 
 89         if (!empty($conditions['nid']) || empty($conditions['slug']))
 90         {
 91             return;
 92         }
 93 
 94         $slug = $conditions['slug'];
 95         $model = $this->module->model;
 96         $tries = $model->select('nid, slug')->own->visible->order('date DESC')->pairs;
 97         $key = null;
 98         $max = 0;
 99 
100         foreach ($tries as $nid => $compare)
101         {
102             similar_text($slug, $compare, $p);
103 
104             if ($p > $max)
105             {
106                 $key = $nid;
107 
108                 if ($p > 90)
109                 {
110                     break;
111                 }
112 
113                 $max = $p;
114             }
115         }
116 
117         if ($p < 60)
118         {
119             throw new RecordNotFound('Record not found.', []);
120         }
121         else if ($key)
122         {
123             $record = $model[$key];
124 
125             \ICanBoogie\log('The record %title was rescued!', [ 'title' => $record->title ]);
126 
127             throw new ForceRedirect($record->url, 301);
128         }
129     }
130 }
Autodoc API documentation generated by ApiGen 2.8.0