1<?php
2/*
3 * e107 website system
4 *
5 * Copyright (C) 2008-2012 e107 Inc (e107.org)
6 * Released under the terms and conditions of the
7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
8 *
9 * Cron Administration
10 *
11 * $URL: https://e107.svn.sourceforge.net/svnroot/e107/trunk/e107_0.8/e107_admin/cron.php $
12 * $Id: cron.php 12492 2011-12-30 16:09:10Z e107steved $
13 *
14 */
15
16/**
17 *
18 * @package     e107
19 * @subpackage    frontend
20 * @version     $Id$
21 *    Ultra-simple Image-Gallery
22 */
23/*
24 * THIS SCRIPT IS HIGHLY EXPERIMENTAL. USE AT OWN RISK.
25 */
26
27
28/**
29 * Class plugin_gallery_index_controller.
30 */
31class plugin_gallery_index_controller extends eControllerFront
32{
33
34	/**
35	 * Plugin name - used to check if plugin is installed
36	 * Set this only if plugin requires installation
37	 * @var string
38	 */
39	protected $plugin = 'gallery';
40
41	/**
42	 * Default controller access
43	 * @var integer
44	 */
45	protected $userclass = e_UC_PUBLIC;
46
47	/**
48	 * User input filter
49	 * Format 'action' => array(var => validationArray)
50	 * @var array
51	 */
52	protected $filter = array(
53		'category' => array(
54			'cat' => array('regex', '/[\w\pL\s\-+.,]+/u'),
55		),
56		'list'     => array(
57			'cat' => array('regex', '/[\w\pL\s\-+.,]+/u'),
58			'frm' => array('int'),
59		),
60	);
61
62	/**
63	 * @var array
64	 */
65	protected $catList;
66
67	public function init()
68	{
69		e107::plugLan('gallery', 'front');
70		e107::css('gallery', 'css/gallery.css');
71		$this->catList = e107::getMedia()->getCategories('gallery');
72	}
73
74	public function actionIndex()
75	{
76		if(isset($_GET['cat']) && !empty($_GET['cat']))
77		{
78			$this->_forward('list');
79		}
80		else
81		{
82			$this->_forward('category');
83		}
84	}
85
86
87	private function getTemplate()
88	{
89		$template = e107::getTemplate('gallery');
90
91		$oldKeys = array(
92			'list_start', 'list_item', 'list_caption', 'list_end',
93			'cat_start', 'cat_item', 'cat_caption', 'cat_end'
94		);
95
96		if(isset($template['list_start']))
97		{
98			foreach($oldKeys as $k)
99			{
100				list($main,$sub) = explode("_",$k);
101				$template[$main][$sub] = $template[$k];
102				unset($template[$k]);
103
104			}
105
106
107		}
108
109		return $template;
110	}
111
112
113
114	public function actionCategory()
115	{
116		//	print_a("Hi there");
117
118		$template = $this->getTemplate();
119		$template = array_change_key_case($template);
120		$sc = e107::getScBatch('gallery', true);
121		$sc->breadcrumb();
122
123		$text = "";
124
125		if(defset('BOOTSTRAP') === true || defset('BOOTSTRAP') === 2) // Convert bootsrap3 to bootstrap2 compat.
126		{
127			$template['cat_start'] = str_replace('row', 'row-fluid', $template['cat_start']);
128		}
129
130		$text = e107::getParser()->parseTemplate($template['cat']['start'], true, $sc);
131
132		foreach($this->catList as $val)
133		{
134			$sc->setVars($val);
135			$text .= e107::getParser()->parseTemplate($template['cat']['item'], true);
136		}
137
138		$text .= e107::getParser()->parseTemplate($template['cat']['end'], true, $sc);
139
140		if(isset($template['cat']['caption']))
141		{
142			$title = e107::getParser()->parseTemplate($template['cat']['caption'], true, $sc);
143
144			$this->addTitle($title)->addBody($text);
145		}
146		else
147		{
148			$this->addTitle(LAN_PLUGIN_GALLERY_TITLE)->addBody($text);
149		}
150
151
152	}
153
154	public function actionList()
155	{
156		$plugPrefs = e107::getPlugConfig('gallery')->getPref();
157
158		$request = $this->getRequest();
159
160		// use only filtered variables
161		$cid = $request->getRequestParam('cat');
162
163		if($cid && !isset($this->catList[$cid]))
164		{
165			// get ID by SEF
166			$_cid = null;
167			foreach($this->catList as $id => $row)
168			{
169				if($cid === $row['media_cat_sef'])
170				{
171					$_cid = $id;
172					break;
173				}
174			}
175			$cid = $_cid;
176		}
177
178		if(empty($cid) || !isset($this->catList[$cid]))
179		{
180			$this->_forward('category');
181			return;
182		}
183
184		$tp = e107::getParser();
185		$template = $this->getTemplate();
186		$template = array_change_key_case($template);
187
188		$sc = e107::getScBatch('gallery', true);
189
190
191		if(defset('BOOTSTRAP') === true || defset('BOOTSTRAP') === 2) // Convert bootsrap3 to bootstrap2 compat.
192		{
193			$template['list_start'] = str_replace('row', 'row-fluid', $template['list_start']);
194		}
195
196		$sc->total = e107::getMedia()->countImages($cid);
197		$sc->amount = varset($plugPrefs['perpage'], 12);
198		$sc->curCat = $cid;
199		$sc->from = $request->getRequestParam('frm', 0);
200
201
202
203
204		$orderBy = varset($plugPrefs['orderby'], 'media_id DESC');
205
206		$list = e107::getMedia()->getImages($cid, $sc->from, $sc->amount, null, $orderBy);
207		$catname = $tp->toHTML($this->catList[$cid]['media_cat_title'], false, 'defs');
208		$cat = $this->catList[$cid];
209
210		$inner = "";
211
212
213
214		foreach($list as $row)
215		{
216			$sc->setVars($row)
217				->addVars($cat);
218
219			$inner .= $tp->parseTemplate($template['list']['item'], true, $sc);
220		}
221
222		$sc->breadcrumb();
223
224		$text = $tp->parseTemplate($template['list']['start'], true, $sc);
225		$text .= $inner;
226		$text .= $tp->parseTemplate($template['list']['end'], true, $sc);
227
228		if(isset($template['list']['caption']))
229		{
230			$title = $tp->parseTemplate($template['list']['caption'], true, $sc);
231			$this->addTitle($title)->addBody($text);
232		}
233		else
234		{
235			$this->addTitle(LAN_PLUGIN_GALLERY_TITLE)
236				->addTitle($catname)
237				->addBody($text);
238		}
239
240	}
241}
242