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