1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Ansel_Block_Gallery extends Horde_Core_Block
14: {
15: 16: 17:
18: private $_gallery = null;
19:
20: 21:
22: public function __construct($app, $params = array())
23: {
24: parent::__construct($app, $params);
25:
26: $this->_name = _("Gallery");
27: }
28:
29: 30:
31: protected function _params()
32: {
33: $params = array(
34: 'gallery' => array(
35: 'name' => _("Gallery"),
36: 'type' => 'enum',
37: 'default' => '__random',
38: 'values' => array('__random' => _("Random gallery"))
39: ),
40: 'perpage' => array(
41: 'name' => _("Maximum number of photos to display (0 means unlimited)"),
42: 'type' => 'int',
43: 'default' => 20
44: ),
45: 'use_lightbox' => array(
46: 'name' => _("Use a lightbox to view photos"),
47: 'type' => 'checkbox',
48: 'default' => true
49: )
50: );
51:
52: $storage = $GLOBALS['injector']->getInstance('Ansel_Storage');
53: if (empty($GLOBALS['conf']['gallery']['listlimit']) ||
54: ($storage->countGalleries(
55: $GLOBALS['registry']->getAuth(),
56: array('perm' => Horde_Perms::READ)) < $GLOBALS['conf']['gallery']['listlimit'])) {
57:
58: foreach ($storage->listGalleries() as $gal) {
59: $params['gallery']['values'][$gal->id] = $gal->get('name');
60: }
61: }
62:
63: return $params;
64: }
65:
66: 67:
68: protected function _title()
69: {
70: try {
71: $gallery = $this->_getGallery();
72: } catch (Horde_Exception $e) {
73: return Ansel::getUrlFor('view', array('view' => 'List'), true)->link() . $this->getName() . '</a>';
74: }
75:
76:
77: if (isset($this->_params['gallery']) &&
78: $this->_params['gallery'] == '__random') {
79: $name = _("Random Gallery") . ': ' . $gallery->get('name');
80: } else {
81: $name = $gallery->get('name');
82: }
83: $viewurl = Ansel::getUrlFor('view',
84: array('view' => 'Gallery',
85: 'gallery' => $gallery->id,
86: 'slug' => $gallery->get('slug')),
87: true);
88:
89: return $viewurl->link() . htmlspecialchars($name) . '</a>';
90: }
91:
92: 93:
94: protected function _content()
95: {
96: try {
97: $gallery = $this->_getGallery();
98: } catch (Horde_Exception $e) {
99: return $e->getMessage();
100: }
101:
102: $params = array('gallery_id' => $gallery->id,
103: 'count' => $this->_params['perpage']);
104: if (!empty($this->_params['use_lightbox'])) {
105: $params['lightbox'] = true;
106: }
107: $html = Ansel::embedCode($params);
108:
109:
110: $viewurl = Ansel::getUrlFor('view', array('view' => 'Gallery',
111: 'gallery' => $gallery->id,
112: 'slug' => $gallery->get('slug')),
113: true);
114: $html .= '<noscript>';
115: $html .= $viewurl->link(array('title' => sprintf(_("View %s"), $gallery->get('name'))));
116: if ($iid = $gallery->getKeyImage(Ansel::getStyleDefinition('ansel_default')) &&
117: $gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
118:
119: $html .= '<img src="' . Ansel::getImageUrl($gallery->getKeyImage(Ansel::getStyleDefinition('ansel_default')), 'thumb', true) . '" alt="' . htmlspecialchars($gallery->get('name')) . '" />';
120: } else {
121: $html .= Horde::img('thumb-error.png');
122: }
123:
124: return $html . '</a></noscript>';
125: }
126:
127: 128: 129: 130: 131:
132: private function _getGallery($retry = false)
133: {
134:
135: if ($this->_gallery instanceof Ansel_Gallery) {
136: return $this->_gallery;
137: }
138:
139:
140: if (isset($this->_params['gallery']) && $this->_params['gallery'] != '__random') {
141: $this->_gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($this->_params['gallery']);
142: } else {
143: $this->_gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getRandomGallery();
144: }
145:
146:
147:
148:
149: if ($this->_params['gallery'] == '__random' &&
150: !empty($this->_gallery) &&
151: !$this->_gallery->countImages() &&
152: $this->_gallery->hasSubGalleries() && !$retry) {
153:
154: $this->_gallery = null;
155: $this->_gallery = $this->_getGallery(true);
156: }
157:
158: if (empty($this->_gallery)) {
159: throw new Horde_Exception_NotFound(_("Gallery does not exist."));
160: } elseif (!$this->_gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW) ||
161: !$this->_gallery->isOldEnough() || $this->_gallery->hasPasswd()) {
162: throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery."));
163: }
164:
165:
166: return $this->_gallery;
167: }
168:
169: }
170: