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

  • AdjustImage
  • AdjustThumbnail
  • EditBlock
  • GalleryBlock
  • GalleryController
  • Hooks
  • Image
  • ImageUpload
  • ManageBlock
  • Model
  • Module
  • NodeRelation
  • PopImage
  • PopOrUploadImage
  • SaveOperation
  • Thumbnail
  • ThumbnailDecorator
  • ThumbnailOperation
  • UploadImage
  • UploadOperation
  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\Images;
 13 
 14 use ICanBoogie\Debug;
 15 use ICanBoogie\I18n;
 16 use ICanBoogie\Event;
 17 use ICanBoogie\Events;
 18 use ICanBoogie\Modules;
 19 use ICanBoogie\Operation;
 20 
 21 use Brickrouge\Element;
 22 use Brickrouge\Form;
 23 use Brickrouge\Group;
 24 use Brickrouge\Text;
 25 
 26 use Icybee\Modules\Contents\ConfigBlock as ContentsConfigBlock;
 27 use Icybee\Modules\Nodes\Node;
 28 use Icybee\Modules\Pages\PageController;
 29 use Icybee\Modules\Views\ActiveRecordProvider\AlterResultEvent;
 30 
 31 class Hooks
 32 {
 33     /*
 34      * Events
 35      */
 36 
 37     /**
 38      * Finds the images associated with the records provided to the view.
 39      *
 40      * In order to avoid each record of the view to load its own image during rendering, we load
 41      * them all and update the records.
 42      *
 43      * The method is canceled if there is only 3 records because bunch loading takes 3 database
 44      * requests.
 45      *
 46      * TODO-20120713: Use an event to load images when the first `image` property is accessed.
 47      *
 48      * @param AlterResultEvent $event
 49      * @param \Icybee\Modules\Contents\ViewProvider $provider
 50      */
 51     static public function on_contents_provider_alter_result(AlterResultEvent $event, \Icybee\Modules\Contents\ViewProvider $provider)
 52     {
 53         global $core;
 54 
 55         $result = $event->result;
 56 
 57         if (!is_array($result) || count($result) < 3 || !(current($result) instanceof \Icybee\Modules\Contents\Content)
 58         || !$core->registry['images.inject.' . $event->module->flat_id])
 59         {
 60             return;
 61         }
 62 
 63         $node_keys = array();
 64 
 65         foreach ($result as $node)
 66         {
 67             $node_keys[] = $node->nid;
 68         }
 69 
 70         $image_keys = $core->models['registry/node']
 71         ->select('targetid, value')
 72         ->where(array('targetid' => $node_keys, 'name' => 'image_id'))
 73         ->where('value + 0 != 0')
 74         ->pairs;
 75 
 76         if (!$image_keys)
 77         {
 78             return;
 79         }
 80 
 81         $images = $core->models['images']->find($image_keys);
 82 
 83         foreach ($result as $node)
 84         {
 85             $nid = $node->nid;
 86 
 87             if (empty($image_keys[$nid]))
 88             {
 89                 continue;
 90             }
 91 
 92             $imageid = $image_keys[$nid];
 93             $image = $images[$imageid];
 94 
 95             $node->image = new NodeRelation($node, $image);
 96         }
 97     }
 98 
 99     /**
100      * Adds control for the image associated with the content.
101      *
102      * @param Event $event
103      * @param \Icybee\Modules\Nodes\EditBlock $block
104      */
105     static public function on_contents_editblock_alter_children(Event $event, \Icybee\Modules\Nodes\EditBlock $block)
106     {
107         global $core;
108 
109         $flat_id = $event->module->flat_id;
110         $inject = $core->registry['images.inject.' . $flat_id];
111 
112         if (!$inject)
113         {
114             return;
115         }
116 
117         $group = null;
118 
119         if (isset($event->attributes[Element::GROUPS]['contents']))
120         {
121             $group = 'contents';
122         }
123 
124         $imageid = null;
125 
126         if ($block->record)
127         {
128             $imageid = $block->record->metas['image_id'];
129         }
130 
131         $event->children['image_id'] = new PopOrUploadImage
132         (
133             array
134             (
135                 Form::LABEL => $core->registry['images.inject.' . $flat_id . '.title'] ?: 'Image',
136                 Element::GROUP => $group,
137                 Element::REQUIRED => $core->registry['images.inject.' . $flat_id . '.required'],
138                 Element::DESCRIPTION => $core->registry['images.inject.' . $flat_id . '.description'],
139 
140                 'value' => $imageid
141             )
142         );
143     }
144 
145     /**
146      * Alters the config block of contents modules with controls for the associated image.
147      *
148      * @param Event $event
149      * @param \Icybee\Modules\Contents\ConfigBlock $block
150      */
151     static public function on_contents_configblock_alter_children(Event $event, ContentsConfigBlock $block)
152     {
153         global $core;
154 
155         $core->document->css->add(DIR . 'public/admin.css');
156 
157         $module_id = $event->module->id;
158 
159         $views = array
160         (
161             $module_id . '/home' => array
162             (
163                 'title' => I18n\t("Records home", array(), array('scope' => $module_id))
164             ),
165 
166             $module_id . '/list' => array
167             (
168                 'title' => I18n\t("Records list", array(), array('scope' => $module_id))
169             ),
170 
171             $module_id . '/view' => array
172             (
173                 'title' => I18n\t("Record detail", array(), array('scope' => $module_id))
174             )
175         );
176 
177         $thumbnails = array();
178 
179         foreach ($views as $view_id => $view)
180         {
181             $id = \ICanBoogie\normalize($view_id);
182 
183             $thumbnails["global[thumbnailer.versions][$id]"] = new \Brickrouge\Widget\PopThumbnailVersion
184             (
185                 array
186                 (
187                     Element::GROUP => 'images__inject_thumbnails',
188                     Form::LABEL => $view['title'],// . ' <span class="small">(' . $id . ')</span>',
189                     Element::DESCRIPTION => 'Identifiant de la version&nbsp;: <q>' . $id . '</q>.'
190                 )
191             );
192         }
193 
194         $target_flat_id = $event->module->flat_id;
195 
196         $event->children = array_merge
197         (
198             $event->children, array
199             (
200                 "global[images.inject][$target_flat_id]" => new Element
201                 (
202                     Element::TYPE_CHECKBOX, array
203                     (
204                         Element::LABEL => 'image_inject_activate',
205                         Element::GROUP => 'images__inject_toggler'
206                     )
207                 ),
208 
209                 "global[images.inject][$target_flat_id.default]" => new PopImage
210                 (
211                     array
212                     (
213                         Form::LABEL => 'image_inject_default_image',
214                         Element::GROUP => 'images__inject'
215                     )
216                 ),
217 
218                 "global[images.inject][$target_flat_id.title]" => new Text
219                 (
220                     array
221                     (
222                         Group::LABEL => 'image_inject_control_title',
223                         Element::GROUP => 'images__inject',
224 
225                         'placeholder' => 'Image'
226                     )
227                 ),
228 
229                 "global[images.inject][$target_flat_id.description]" => new Text
230                 (
231                     array
232                     (
233                         Group::LABEL => 'image_inject_control_description',
234                         Element::GROUP => 'images__inject'
235                     )
236                 ),
237 
238                 "global[images.inject][$target_flat_id.required]" => new Element
239                 (
240                     Element::TYPE_CHECKBOX, array
241                     (
242                         Element::LABEL => 'image_inject_required',
243                         Element::GROUP => 'images__inject'
244                     )
245                 )
246             ),
247 
248             $thumbnails
249         );
250 
251         #
252         # Listen to the block `alert_attributes` event to add our groups.
253         #
254 
255         $event->attributes[Element::GROUPS] = array_merge
256         (
257             $event->attributes[Element::GROUPS], array
258             (
259                 'images__inject_toggler' => array
260                 (
261                     'title' => 'Associated image',
262                     'class' => 'group-toggler'
263                 ),
264 
265                 'images__inject' => array
266                 (
267 
268                 ),
269 
270                 'images__inject_thumbnails' => array
271                 (
272                     'description' => 'Use the following elements to configure the
273                     thumbnails to create for the associated image. Each view provided by the
274                     module has its own thumbnail configuration:'
275                 )
276             )
277         );
278     }
279 
280     static public function on_nodes_save(Event $event, \Icybee\Modules\Nodes\SaveOperation $operation)
281     {
282         $params = &$event->request->params;
283 
284         if (!isset($params['image_id']))
285         {
286             return;
287         }
288 
289         $entry = $operation->module->model[$event->rc['key']];
290         $imageid = $params['image_id'];
291 
292         $entry->metas['image_id'] = $imageid ? $imageid : null;
293     }
294 
295     static public function before_contents_config(Event $event, \Icybee\Modules\Contents\ConfigOperation $operation)
296     {
297         if (!isset($event->request->params['global']['images.inject']))
298         {
299             return;
300         }
301 
302         $module_flat_id = $operation->module->flat_id;
303         $options = &$event->request->params['global']['images.inject'];
304 
305         $options += array
306         (
307             $module_flat_id => false,
308             $module_flat_id . '.required' => false,
309             $module_flat_id . '.default' => null
310         );
311 
312         $options[$module_flat_id] = filter_var($options[$module_flat_id], FILTER_VALIDATE_BOOLEAN);
313         $options[$module_flat_id . '.required'] = filter_var($options[$module_flat_id . '.required'], FILTER_VALIDATE_BOOLEAN);
314     }
315 
316     static public function textmark_images_reference(array $args, \Textmark_Parser $textmark, array $matches)
317     {
318         global $core;
319         static $model;
320 
321         if (!$model)
322         {
323             $model = $core->models['images'];
324         }
325 
326         $align = $matches[2];
327         $alt = $matches[3];
328         $id = $matches[4];
329 
330         # for shortcut links like ![this][].
331 
332         if (!$id)
333         {
334             $id = $alt;
335         }
336 
337         $record = $model->where('nid = ? OR slug = ? OR title = ?', (int) $id, $id, $id)->order('created_at DESC')->one;
338 
339         if (!$record)
340         {
341             $matches[2] = $matches[3];
342             $matches[3] = $matches[4];
343 
344             trigger_error('should call standard one !');
345 
346             //return parent::_doImages_reference_callback($matches);
347 
348             return;
349         }
350 
351         $src = $record->path;
352         $w = $record->width;
353         $h = $record->height;
354 
355         $light_src = null;
356 
357         if ($w > 600)
358         {
359             $w = 600;
360             $h = null;
361 
362             $light_src = $src;
363 
364             $src = $record->thumbnail("w:$w;method:fixed-width;quality:80")->url;
365         }
366 
367         $params = array
368         (
369             'src' => $src,
370             'alt' => $alt,
371             'width' => $w,
372             'height' => $h
373         );
374 
375         if ($align)
376         {
377             switch ($align)
378             {
379                 case '<': $align = 'left'; break;
380                 case '=':
381                 case '|': $align = 'middle'; break;
382                 case '>': $align = 'right'; break;
383             }
384 
385             $params['align'] = $align;
386         }
387 
388         $rc = new Element('img', $params);
389 
390         if ($light_src)
391         {
392             $rc = '<a href="' . $light_src . '" rel="lightbox[]">' . $rc . '</a>';
393         }
394 
395         return $rc;
396     }
397 
398     /**
399      * Adds assets to support lightbox links.
400      *
401      * This function is a callback for the `Icybee\Modules\Pages\PageController::render` event.
402      *
403      * @param Event $event
404      */
405     static public function on_page_controller_render(PageController\RenderEvent $event, PageController $target)
406     {
407         global $core;
408 
409         if (strpos($event->html, 'rel="lightbox') === false)
410         {
411             return;
412         }
413 
414         $document = $core->document;
415         $document->css->add(DIR . 'public/slimbox.css');
416         $document->js->add(DIR . 'public/slimbox.js');
417     }
418 
419     static private $attached;
420 
421     static public function on_alter_css_class_names(\Brickrouge\AlterCSSClassNamesEvent $event, \Icybee\Modules\Nodes\Node $node)
422     {
423         global $core;
424 
425         if (self::$attached === null)
426         {
427             self::$attached = $core->models['registry/node']
428             ->select('targetid, value')
429             ->joins('INNER JOIN {prefix}nodes ON(targetid = nid)')
430             ->where('(siteid = 0 OR siteid = ?) AND name = "image_id"', $core->site_id)
431             ->pairs;
432         }
433 
434         if (empty(self::$attached[$node->nid]))
435         {
436             return;
437         }
438 
439         $event->names['has-image'] = true;
440     }
441 
442     /**
443      * Decorates `title` cell of the record with an associated image with an icon.
444      *
445      * @param \Icybee\ManageBlock\AlterRenderedCellsEvent $event
446      * @param \Icybee\Modules\Contents\ManageBlock $target
447      */
448     static public function on_manageblock_alter_rendered_cells(\Icybee\ManageBlock\AlterRenderedCellsEvent $event, \Icybee\Modules\Contents\ManageBlock $target)
449     {
450         global $core;
451 
452         if (!($core->registry["images.inject.{$target->module->flat_id}"]))
453         {
454             return;
455         }
456 
457         $rendered_cells = &$event->rendered_cells;
458         $records = $core->models['images']->including_assigned_image($event->records);
459 
460         foreach ($rendered_cells['title'] as $i => &$cell)
461         {
462             try
463             {
464                 $image = $records[$i]->image;
465 
466                 if (!$image)
467                 {
468                     continue;
469                 }
470 
471                 $cell = new ThumbnailDecorator($cell, $image);
472             }
473             catch (\Exception $e) {}
474         }
475 
476         $core->document->css->add(DIR . 'public/slimbox.css');
477         $core->document->js->add(DIR . 'public/slimbox.js');
478     }
479 
480     /*
481      * Prototype methods
482      */
483 
484     /**
485      * Returns the image associated with the node.
486      *
487      * @param Node $node
488      *
489      * @return Image|null
490      */
491     static public function prototype_get_image(\Icybee\Modules\Nodes\Node $node)
492     {
493         global $core;
494 
495         $id = $node->metas['image_id'];
496 
497         if (!$id)
498         {
499             return;
500         }
501 
502         $image = $core->models['images'][$id];
503 
504         return new NodeRelation($node, $image);
505     }
506 
507     /**
508      * Callback for the `thumbnail()` method added to the active records of the "images" module.
509      *
510      * @param Icybee\Modules\Images\Image $ar An active record of the "images" module.
511      * @param string $version The version used to create the thumbnail, or a number of options
512      * defined as CSS properties. e.g. 'w:300;h=200'.
513      * @return string The URL of the thumbnail.
514      */
515     static public function prototype_thumbnail(Image $record, $version, $additionnal_options=null)
516     {
517         return new Thumbnail($record, $version, $additionnal_options);
518     }
519 
520     /**
521      * Callback for the `thumbnail` getter added to the active records of the "images" module.
522      *
523      * The thumbnail is created using options of the 'primary' version.
524      *
525      * @param Icybee\Modules\Images\Image $ar An active record of the "images" module.
526      * @return string The URL of the thumbnail.
527      */
528     static public function prototype_get_thumbnail(Image $record)
529     {
530         return $record->thumbnail('primary');
531     }
532 }
Autodoc API documentation generated by ApiGen 2.8.0