PageRenderTime 131ms CodeModel.GetById 49ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/cms/modules/gallery.lib.php

https://github.com/akash6190/pragyan
PHP | 374 lines | 356 code | 6 blank | 12 comment | 24 complexity | 705c3aeec1178c27672c395ecb97d583 MD5 | raw file
  1<?php
  2if(!defined('__PRAGYAN_CMS'))
  3{ 
  4	header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
  5	echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
  6	echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
  7	exit(1);
  8}
  9
 10/**
 11 * @package pragyan
 12 * @author Harini A
 13 * @copyright (c) 2008 Pragyan Team
 14 * @license http://www.gnu.org/licenses/ GNU Public License
 15 * For more details, see README
 16 */
 17class gallery implements module, fileuploadable {
 18	private $userId;
 19	private $moduleComponentId;
 20	private $action;
 21	public function getHtml($gotuid, $gotmoduleComponentId, $gotaction) {
 22		$this->userId = $gotuid;
 23		$this->moduleComponentId = $gotmoduleComponentId;
 24		$this->action = $gotaction;
 25		if ($this->action == "view")
 26			return $this->actionView();
 27		if ($this->action == "create")
 28			return $this->createModule($this->moduleComponentId);
 29		if ($this->action == "edit")
 30			return $this->actionEdit($this->moduleComponentId);
 31	}
 32
 33	/**
 34	 * Funtion which tells the cms uploaded file access is defined by which action
 35	 */
 36	public static function getFileAccessPermission($pageId, $moduleComponentId, $userId, $fileName) {
 37		return getPermissions($userId, $pageId, "view");
 38	}
 39
 40	public static function getUploadableFileProperties(& $fileTypesArray, & $maxFileSizeInBytes) {
 41		$fileTypesArray = array (
 42			'jpg',
 43			'jpeg',
 44			'png',
 45			'gif'
 46		);
 47		$maxFileSizeInBytes = 2 * 1024 * 1024;
 48	}
 49
 50	public static function getFileAccessAction() {
 51		return "view";
 52	}
 53
 54	public function actionView() {
 55		global $sourceFolder,$cmsFolder;
 56		global $templateFolder;
 57		global $urlRequestRoot;
 58		global $moduleFolder;
 59		global $uploadFolder;
 60		// Ajax request for returning the views of the image
 61		if(isset($_GET['subaction'])&&$_GET['subaction']=='ajax') {
 62		if($_GET['ref']){
 63			$arr=explode("/",$_GET['ref']);
 64			$arr = $arr[sizeof($arr)-1];
 65			$query="SELECT* FROM `gallery_pics` WHERE upload_filename='".$arr."' AND page_modulecomponentid='$this->moduleComponentId' LIMIT 1";
 66			$result=mysql_query($query);
 67			if($result){
 68				$newrate = mysql_result($result,0,'pic_rate')+1;
 69				$query="UPDATE `gallery_pics` SET `pic_rate`='".$newrate."' WHERE upload_filename='".$arr."' AND page_modulecomponentid='$this->moduleComponentId'";
 70				mysql_query($query);
 71			}}
 72		else if($_GET['getView']){
 73			$arr1=explode("/",$_GET['getView']);
 74			$arr1 = $arr1[sizeof($arr1)-1];
 75			$query="SELECT* FROM `gallery_pics` WHERE upload_filename='".$arr1."' AND page_modulecomponentid='$this->moduleComponentId' LIMIT 1";
 76			$result1=mysql_query($query);
 77			if($result1){
 78				$view = mysql_result($result1,0,'pic_rate');
 79				echo $view;
 80			}
 81			}
 82		else if($_GET['rateIt']){
 83			$arr3 = $_GET['rateRef'];
 84			$query="SELECT `vote_avg`,`voters` FROM `gallery_pics` WHERE upload_filename='".$arr3."' AND page_modulecomponentid='$this->moduleComponentId' LIMIT 1";
 85			$result3=mysql_query($query);
 86			if($result3){
 87				$voteAvg = mysql_result($result3,0,'vote_avg');
 88				$voters = mysql_result($result3,0,'voters');
 89				$newAvg = (($voters*$voteAvg)+$_GET['rateIt'])/($voters+1);
 90				$voters=$voters+1;
 91				$query="UPDATE `gallery_pics` SET `vote_avg`='".$newAvg."',`voters`='".$voters."' WHERE upload_filename='".$arr3."' AND page_modulecomponentid='$this->moduleComponentId'";
 92				$result = mysql_query($query);
 93				if (!$result){echo "a";}
 94				else{
 95					$query="SELECT* FROM `gallery_pics` WHERE upload_filename='".$arr3."' AND page_modulecomponentid='$this->moduleComponentId' LIMIT 1";
 96					$result3 = mysql_query($query);
 97					if($result3){
 98						$rating = mysql_result($result3,0,'vote_avg');
 99						$voters = mysql_result($result3,0,'voters');
100						echo $rating."-".$voters;
101					}
102					else{
103						echo "b";
104					}
105				}
106				}
107			}
108			disconnect();
109			exit(0);
110		}
111		// Ajax request for views ends here
112		$content =<<<JS
113			<script type="text/javascript" src="$urlRequestRoot/$cmsFolder/$moduleFolder/gallery/highslide-with-gallery.js"></script>
114			<link rel="stylesheet" type="text/css" href="$urlRequestRoot/$cmsFolder/$moduleFolder/gallery/highslide.css" />
115			<script type="text/javascript">
116				hs.graphicsDir = '$urlRequestRoot/$cmsFolder/$moduleFolder/gallery/graphics/';
117				hs.align = 'center';
118				hs.transitions = ['expand', 'crossfade'];
119				hs.fadeInOut = true;
120				hs.dimmingOpacity = 0.8;
121				hs.outlineType = 'rounded-white';
122				hs.captionEval = 'this.thumb.alt';
123				hs.marginBottom = 105;
124				hs.numberPosition = 'caption';
125
126				hs.addSlideshow({
127					interval: 5000,
128					repeat: false,
129					useControls: true,
130					overlayOptions: {
131						className: 'text-controls',
132						position: 'bottom center',
133						relativeTo: 'viewport',
134						offsetY: -60
135					},
136					thumbstrip: {
137						position: 'bottom center',
138						mode: 'horizontal',
139						relativeTo: 'viewport'
140					}
141				});
142			</script>
143JS;
144		$gallQuery = "SELECT * from `gallery_name` where `page_modulecomponentid`='$this->moduleComponentId'";
145		$gallResult = mysql_query($gallQuery);
146		$row = mysql_fetch_assoc($gallResult);
147		$content .= "<h2><center>{$row['gallery_name']}</center></h2><br/><center><h3>{$row['gallery_desc']}</center></h3>";
148		$perPage = $row['imagesPerPage'];
149		$viewCheck = $row['allowViews'];
150		$ratingCheck = $row['allowRatings'];
151		include_once ("$sourceFolder/" . 'upload.lib.php');
152		$query = "SELECT `upload_filename` FROM `gallery_pics` WHERE `page_modulecomponentid` ='". $this->moduleComponentId."'";
153		$pic_result = mysql_query($query) or die(mysql_error());
154		$arr = array ();
155		while ($row = mysql_fetch_assoc($pic_result))
156			$arr[] = $row;
157		$numPic = count($arr);
158		if(isset($_GET['gallerypage']))
159			$page = (int)escape($_GET['gallerypage']) - 1;
160		else
161			$page = 0;
162		$start = $page * $perPage;
163		if($start > $numPic) {
164			$start = 0;
165			$page = 0;
166		}
167		$end = $start + $perPage;
168		if($end > $numPic)
169			$end = $numPic;
170		$content .= '<div class="highslide-gallery" style="width: 100%; margin: auto">';
171		for ($i = $start; $i < $end; $i++) {
172			$gallQuery2 = "SELECT * FROM `gallery_pics` where `upload_filename`='{$arr[$i]['upload_filename']}' AND `page_modulecomponentid`= '$this->moduleComponentId'";
173			$gallResult2 = mysql_query($gallQuery2);
174			$row2 = mysql_fetch_assoc($gallResult2);
175			if ($row2) {
176				$content .= "<input type=\"hidden\" id=\""."thumb_"."{$row2['upload_filename']}\" value=\"{$row2['pic_rate']}\" />";
177				$content .= "<input type=\"hidden\" id=\""."thumb1_"."{$row2['upload_filename']}\" value=\"{$row2['vote_avg']}\" />";
178				$content .= "<input type=\"hidden\" id=\""."thumb2_"."{$row2['upload_filename']}\" value=\"{$row2['voters']}\" />";
179				$content .= "<input type=\"hidden\" id=\""."thumb3_"."{$row2['upload_filename']}\" value=\"0\" />";
180				$content .= "<a href=\"./" . $arr[$i]['upload_filename'] . '"  class=\'highslide\' onclick="return hs.expand(this,0,0,0,document.getElementById(\'thumb_' .$row2['upload_filename'].'\'),'.$viewCheck.',document.getElementById(\'thumb1_' .$row2['upload_filename'].'\'),document.getElementById(\'thumb2_' .$row2['upload_filename'].'\'),'.$ratingCheck.',document.getElementById(\'thumb3_' .$row2['upload_filename'].'\'))">';
181				$content .= "<img src=\"./thumb_" . $arr[$i]['upload_filename'] . "\" alt='{$row2['gallery_filecomment']}' title='Click to enlarge' /></a>   &nbsp;";
182			}
183		}
184		$content .= '</div>';
185		$nextVal = $page + 2;
186		if($start == 0)
187			$prevButton = "&lt;&lt;Prev ";
188		else
189			$prevButton = "<a href='./+view&gallerypage=" . $page . "'> &lt;&lt;Prev</a> ";
190		if($end == $numPic)
191			$nextButton = " Next&gt;&gt;";
192		else
193			$nextButton = " <a href='./+view&gallerypage=" . $nextVal . "'> Next&gt;&gt; </a>";
194		$pages = "";
195		$pageStart = 1;
196		$pageEnd = ceil($numPic/$perPage);
197		if($page > 4) {
198			$pageStart = $page - 3;
199			$pages .= "... ";
200		}
201		if($pageEnd - $page > 5)
202			$pageEnd = $page + 5;
203		$pageVal = $page + 1;
204		for($i = $pageStart; $i <= $pageEnd; $i++)
205			if($i == $pageVal)
206				$pages .= " $pageVal ";
207			else
208				$pages .= " <a href='./+view&gallerypage={$i}'>{$i}</a>&nbsp;";
209		if(ceil($numPic/$perPage) - $page > 5)
210			$pages .= " ...";
211		$content .= "<p>" . $prevButton . $pages . $nextButton . "</p>";
212		return $content;
213	}
214	public function createModule($nextId) {
215		$gallQuery = "INSERT INTO `gallery_name` (`page_modulecomponentid`, `gallery_name`, `gallery_desc`) VALUES('$nextId', 'New Gallery', 'Edit your new gallery')";
216		$gallResult = mysql_query($gallQuery);
217	}
218	public function actionEdit($moduleComponentId) {
219		global $sourceFolder;
220		global $templateFolder;
221		global $urlRequestRoot;
222		global $uploadFolder;
223		require_once ("$sourceFolder/upload.lib.php");
224		$arr = getUploadedFiles($moduleComponentId, 'gallery');
225		if (isset ($_POST['btnDeleteImage']) && isset ($_POST['imagename']) && $_POST['imagename'] != '') {
226			deleteFile($moduleComponentId, 'gallery', $_POST['imagename']);
227			$gallQuery = "DELETE FROM `gallery_pics` WHERE `upload_filename`='".escape($_POST['imagename'])."'";
228			$gallResult = mysql_query($gallQuery);
229		} 
230		else if (isset ($_POST['btnEditComment']) && isset ($_POST['imagename']) && $_POST['imagename'] != '') {
231			$imageName =  escape($_POST['imagename']);
232			$comment = escape($_POST['desc']);
233			$gallQuery = "UPDATE `gallery_pics` SET `gallery_filecomment`=\"$comment\" WHERE `upload_filename`=\"$imageName\"";
234			$gallResult = mysql_query($gallQuery);
235		}
236		if (isset ($_POST['btnEditGallname']) && isset ($_POST['gallName']) && isset ($_POST['gallDesc']) && $_POST['gallName'] != '' && $_POST['gallDesc'] != '') {
237			if(is_numeric($_POST['imagesPerPage']))
238				$perPage = (int)escape($_POST['imagesPerPage']);
239				$viewCount = ( $_POST['allowViews'] ? 1 : 0 );
240				$ratingCount = ( $_POST['allowRatings'] ? 1 : 0 );
241			$gallQuery = "UPDATE `gallery_name` SET `gallery_name`='".escape($_POST['gallName'])."',`gallery_desc`='".escape($_POST['gallDesc'])."', `imagesPerPage`='".$perPage."',`allowViews`='".$viewCount."',`allowRatings`='".$ratingCount."' WHERE `page_modulecomponentid`='$moduleComponentId'";
242			$gallResult = mysql_query($gallQuery);
243		}
244
245		$content2 = getFileUploadForm($this->moduleComponentId, "gallery", './+edit', 10000000, 5);
246		$allowableTypes = array (
247			'jpeg',
248			'jpg',
249			'png',
250			'gif'
251		);
252
253		$uploadSuccess = submitFileUploadForm($this->moduleComponentId, "gallery", $this->userId, false, $allowableTypes);
254		if (is_array($uploadSuccess) && isset ($uploadSuccess[0])) {
255			for($i=0;$i<count($uploadSuccess);$i++){
256				$gallQuery3 = "INSERT INTO `gallery_pics` (`upload_filename`, `page_modulecomponentid`, `gallery_filecomment`) VALUES('$uploadSuccess[$i]', '$this->moduleComponentId', 'No Comment')";
257				$gallResult3 = mysql_query($gallQuery3);
258			}
259		}
260		$arr = getUploadedFiles($this->moduleComponentId, 'gallery');
261		global $ICONS;
262		$content2="<fieldset><legend>{$ICONS['Gallery Edit']['small']}Edit Gallery</legend>".$content2;
263		
264		$result = mysql_fetch_array(mysql_query("SELECT * FROM `gallery_name` WHERE `page_modulecomponentid` = '{$this->moduleComponentId}'"));
265		if($result){
266			$checkViews = ($result['allowViews'] == 1 ? 'checked="checked" ': '' );
267			$checkRatings = ($result['allowRatings'] == 1 ? 'checked="checked" ': '' );
268		}
269		$content2 .=<<<GALFORM
270					<br /><br />
271					<script type="text/javascript">
272						<!--
273						function validate() {
274							var strValidChars = "0123456789.-";
275							var strString = document.getElementById('perPage').value;
276
277							if (strString.length == 0)
278								alert("Empty Images Per Page will be taken as default value(10).");
279
280							for (i = 0; i < strString.length; i++) {
281								if (strValidChars.indexOf(strString.charAt(i)) == -1) {
282									alert("The value in the Images Per Page field doesn't seems to be valid number. An invalid number will be replaced by default value(10).");
283									break;							  	
284								}
285							}
286						}
287						-->
288					</script>
289					<form name="edit" method="POST" action="./+edit">
290					<table>
291						<tr><th colspan=2>Edit gallery name and description</th></tr>
292						<tr>
293							<td>New Gallery Name</td>
294							<td><input type='text' name="gallName" value='{$result['gallery_name']}'></td>
295						</tr>
296						<tr>
297							<td>New Gallery Description</td>
298							<td><input type='text' name="gallDesc" value='{$result['gallery_desc']}'></td>
299						</tr>
300						<tr>
301							<td>Show Gallery views ?</td>
302							<td><input type="checkbox" name="allowViews" $checkViews></td>
303						</tr>
304						<tr>
305							<td>Show Gallery rating ?</td>
306							<td><input type="checkbox" name="allowRatings" $checkRatings></td>
307						</tr>
308						<tr>
309							<td>Images Per Page</td>
310							<td><input type="text" id=perPage name="imagesPerPage" value='{$result['imagesPerPage']}'></td>
311						</tr>
312						<tr>
313							<td><input type="submit" name="btnEditGallname" value="Save Settings"></td>
314						</tr>
315					</table>
316					</form>
317					<br /><br />
318GALFORM;
319		$gallQuery2 = "SELECT * FROM `gallery_pics` where `page_modulecomponentid`= '$this->moduleComponentId'";
320		$gallResult2 = mysql_query($gallQuery2);
321		$fileArray = array ();
322		while ($row2 = mysql_fetch_assoc($gallResult2))
323			$fileArray[] = $row2;
324		if ($fileArray) {
325			for ($i = 0; $i < count($fileArray); $i++) {
326				$galleryFilename = $fileArray[$i]['upload_filename'];
327				$galleryComment = $fileArray[$i]['gallery_filecomment'];
328				$galleryComment = $galleryComment!=""?$galleryComment:"< No Comments >";
329				
330				$content2 .= "<div class='galleryimagebox'><form name=\"edit\" method=\"POST\" action=\"./+edit\">";
331				$content2 .=<<<IMGFORM
332				<span style="float:left">
333					<center>
334						<img src="thumb_$galleryFilename" alt="$galleryFilename" title="Click on the image to delete it"/>
335					</center>
336					<div class="highslide-caption" id="caption$i">$galleryComment</div>
337					<input type="hidden" name="imagename" value="$galleryFilename" />
338					<input type="text" name="desc">
339					<br/><input type="submit" name="btnEditComment" value="Update comment">
340					<input type="submit" name="btnDeleteImage" value="Delete" />
341				</span>
342IMGFORM;
343				$content2 .= "</form></div>";
344			}
345		}
346		return $content2."</fieldset>";
347	}
348	public function deleteModule($moduleComponentId) {
349		global $sourceFolder;
350		require_once("$sourceFolder/upload.lib.php");
351		$arr = getUploadedFiles($moduleComponentId, 'gallery');
352		$content = true;
353		for ($c = 0; $c < count($arr); $c++) {
354			$content = deleteFile($moduleComponentId, 'gallery', $arr[$c]['upload_filename']) && $content;
355		}
356		$gallQuery = "DELETE FROM `gall_name` where `page_modulecomponentid`='$moduleComponentId'";
357		$gallResult = mysql_query($gallQuery);
358		$gallQuery2 = "DELETE FROM `gall_pics` where `page_modulecomponentid`='$moduleComponentId'";
359		$gallResult2 = mysql_query($gallQuery2);
360		return $content;
361	}
362	public function copyModule($moduleComponentId,$newId) {
363		$gallQuery = "SELECT * FROM `gallery_pics` WHERE page_modulecomponentid = '" . $moduleComponentId."'";
364		$gallResult = mysql_query($gallQuery);
365		$gallRow = mysql_fetch_assoc($gallResult);
366		$destinationPage_moduleComponentId = $newId;
367		while ($gallRow) {
368			fileCopy($moduleComponentId, 'gallery', $gallRow['upload_filename'], $destinationPage_moduleComponentId, 'gallery', $gallRow['upload_filename'], $this->userId);
369			$thumb ="thumb_".$gallRow['upload_filename'];
370			fileCopy($moduleComponentId, 'gallery', $thumb, $destinationPage_moduleComponentId, 'gallery', $gallRow['upload_filename'], $this->userId);
371		}
372		return true;
373	}
374}