Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Widgets;
use DirectoryIterator;
use Maslosoft\EmbeDi\EmbeDi;
use Maslosoft\MiniView\MiniView;
use Maslosoft\Staple\Staple;
use Maslosoft\Staple\Widgets\Vo\GalleryFile;
/**
* Gallery
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Gallery
{
const DefaultPath = 'gallery';
/**
* Default width and height of large image. Should fit nicely on displays
*/
const DefaultWidth = 1600;
const DefaultHeight = 1200;
/**
* Default thumbnail width and height, should fit nicely in layout
*/
const DefaultThumbWidth = 237;
const DefaultThumbHeight = 237;
const DefaultThumbCss = 'img-thumbnail img-margins img-responsive';
/**
* Path relative to application root
* @var string
*/
public $path = '';
public $width = self::DefaultWidth;
public $height = self::DefaultHeight;
public $thumbWidth = self::DefaultThumbWidth;
public $thumbHeight = self::DefaultThumbHeight;
public $thumbCss = self::DefaultThumbCss;
public $lg = null;
public $md = 4;
public $sm = 2;
public $xs = 2;
public $options = [
'path' => self::DefaultPath,
'width' => self::DefaultWidth,
'height' => self::DefaultHeight,
'thumbWidth' => self::DefaultThumbWidth,
'thumbHeight' => self::DefaultThumbHeight,
'thumbCss' => self::DefaultThumbCss
];
/**
* View
* @var MiniView
*/
public $view = null;
public function __construct($options = [])
{
if (is_string($options))
{
$this->options['path'] = $options;
unset($options);
}
if (!empty($options))
{
$this->options = array_merge($this->options, $options);
}
if (empty($this->options['path']))
{
$this->options['path'] = self::DefaultPath;
}
// Apply configuration
EmbeDi::fly()->apply($this->options, $this);
// Setup view
$this->view = new MiniView($this);
$this->path = sprintf('%s/%s', Staple::fly()->getContentPath(), $this->options['path']);
}
public function getBaseUrl()
{
return $this->options['path'];
}
public function getFiles()
{
$files = [];
$dirIt = new DirectoryIterator($this->path);
foreach ($dirIt as $file)
{
if ($file->isDir() || $file->isDot())
{
continue;
}
$ext = strtolower($file->getExtension());
if (in_array($ext, ['jpg', 'gif', 'png']))
{
$files[$file->getFilename()] = new GalleryFile($file, $this);
}
}
ksort($files);
$files = array_values($files);
return $files;
}
public function getSizes()
{
$sizes = [];
$names = ['lg', 'md', 'sm', 'xs'];
foreach ($names as $size)
{
if ($this->{$size} === null)
{
continue;
}
$value = (int) $this->{$size};
if (12 % $value > 0)
{
throw new \InvalidArgumentException(sprintf('Value for `%s` must divide number 12 equally, `%s` given', $size, $value));
}
if ($value < 1 || $value > 12)
{
throw new \InvalidArgumentException(sprintf('Value for `%s` must be between 1 and 12, `%s` given', $size, $value));
}
$sizes[] = sprintf('col-%s-%s', $size, 12 / $value);
}
return implode(' ', $sizes);
}
public function __toString()
{
try
{
return $this->view->render('gallery', [], true);
}
catch (Exception $exc)
{
echo $exc->getTraceAsString();
}
}
}
API documentation generated by ApiGen