PageRenderTime 76ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/video.php

https://github.com/Airzign/Shiksha-Sankalp
PHP | 264 lines | 248 code | 1 blank | 15 comment | 60 complexity | 9223b62e8128f221918bc2607e30abb9 MD5 | raw file
  1. <?php
  2. require_once('auth.php');
  3. require_once('config.php');
  4. ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  9. <title>SS Admin | Video</title>
  10. <link href="loginmodule.css" rel="stylesheet" type="text/css" />
  11. <script type="text/javascript">
  12. function confirmDelete(){
  13. return window.confirm('Are you sure you want to delete this?');
  14. }
  15. </script>
  16. </head>
  17. <body>
  18. <h1> Video Admin </h1>
  19. <?php include('menu.php'); ?>
  20. <div class="admin_link">
  21. <a href="?action=4">Add a new entry</a>
  22. </div>
  23. <?php
  24. /*
  25. * Actions to be performed for various values of $action
  26. * 0 => Show All the videos.
  27. * 1 => Update the entry submitted.
  28. * 2 => Delete the entry.
  29. * 3 => Add a new entry.
  30. * 4 => Show the form to get details for new entry.
  31. * 5 => Show the form to edit an entry.
  32. */
  33. $action=0;
  34. $message='';
  35. $is_message_error=false;
  36. $video_img_dir='../images/video/';
  37. $allowed_file_types=array('image/gif','image/jpeg','image/pjpeg','image/png');
  38. if(array_key_exists('action',$_GET)) {
  39. $action=$_GET['action'];
  40. }
  41. if(array_key_exists('action',$_POST)) {
  42. $action=stripslashes($_POST['action']);
  43. }
  44. if($action == 1) {
  45. /* Update */
  46. $id = $_POST['id'];
  47. $heading = stripslashes($_POST['heading']);
  48. $description = stripslashes($_POST['description']);
  49. $result = mysql_query("select smallimgurl from video where id=$id");
  50. $link = stripslashes($_POST['link']);
  51. $assoc = mysql_fetch_assoc($result);
  52. $old_small_img_filename = $assoc['smallimgurl'];
  53. $new_small_img_filename = $old_small_img_filename;
  54. $update_small_img = True;
  55. if(array_key_exists('smallimg',$_FILES) && $_FILES['smallimg']['name'] !== '') {
  56. if($_FILES["smallimg"]["size"] > SMALL_IMG_FILE_SIZE) {
  57. $message .= 'The small image is larger than the maximum allowed size('.SMALL_IMG_FILE_SIZE/(1024*1024).'MB) so was not uploaded.<br/>';
  58. $is_message_error = true;
  59. $update_small_img = False;
  60. } else {
  61. $allowed=false;
  62. foreach($allowed_file_types as $type)
  63. if($_FILES['smallimg']['type'] == $type)
  64. $allowed=true;
  65. if(!$allowed) {
  66. $message .= 'The small image file is not a valid image so was not uploaded.<br/>';
  67. $is_message_error = true;
  68. $update_small_img = False;
  69. } else {
  70. if(!file_exists($video_img_dir.$_FILES['smallimg']['name'])) {
  71. $small_img_filename=$_FILES['smallimg']['name'];
  72. } else {
  73. $fileparts=explode('.',$_FILES['smallimg']['name']);
  74. $extension='.'.array_pop($fileparts);
  75. $name=implode('.',$fileparts).'_';
  76. $filecount=0;
  77. while(file_exists($video_img_dir.$name.$filecount.$extension))
  78. $filecount=$filecount+1;
  79. $small_img_filename=$name.$filecount.$extension;
  80. }
  81. }
  82. move_uploaded_file($_FILES['smallimg']['tmp_name'],$video_img_dir.$small_img_filename);
  83. }
  84. }
  85. if(array_key_exists('delete_old_small_img',$_POST) && isset($_POST['delete_old_small_img']))
  86. if($old_small_img_filename!='') {
  87. $new_small_img_filename = '';
  88. if(file_exists($video_img_dir.$old_small_img_filename))
  89. unlink($video_img_dir.$old_small_img_filename);
  90. }
  91. if(array_key_exists('smallimg',$_FILES) && $_FILES['smallimg']['name']!='' && $update_small_img) {
  92. $new_small_img_filename = $small_img_filename;
  93. if($old_small_img_filename!='')
  94. if(file_exists($video_img_dir.$old_small_img_filename))
  95. unlink($video_img_dir.$old_small_img_filename);
  96. }
  97. if(mysql_query("update video set heading='$heading',smallimgurl='$new_small_img_filename',link='$link',description='$description' where id = $id"))
  98. $message .= 'The Video was updated successfully.';
  99. }
  100. if($action == 2) {
  101. /* Deletion */
  102. $id=$_GET['id'];
  103. $query = mysql_query("select * from video where id=$id");
  104. $row=mysql_fetch_assoc($query);
  105. if($row['smallimgurl'] !== null && $row['smallimgurl'] !== '' && file_exists($video_img_dir.$row['smallimgurl']))
  106. unlink($video_img_dir.$row['smallimgurl']);
  107. if(mysql_query("delete from video where id=$id"))
  108. $message .= 'The video was deleted successfully.';
  109. }
  110. if($action == 3) {
  111. /* Addition */
  112. $heading=stripslashes($_POST['heading']);
  113. $description = stripslashes($_POST['description']);
  114. $link = stripslashes($_POST['link']);
  115. $small_img_filename = '';
  116. if($_FILES['smallimg']['name'] !== '') {
  117. if($_FILES["smallimg"]["size"] > SMALL_IMG_FILE_SIZE) {
  118. $message .= 'The small image is larger than the maximum allowed size so was not uploaded.<br/>';
  119. $is_message_error = true;
  120. } else {
  121. $allowed=false;
  122. foreach($allowed_file_types as $type)
  123. if($_FILES['smallimg']['type'] == $type)
  124. $allowed=true;
  125. if(!$allowed) {
  126. $message .= 'The small image file is not a valid image so was not uploaded.<br/>';
  127. $is_message_error = true;
  128. } else {
  129. if(!file_exists($video_img_dir.$_FILES['smallimg']['name'])) {
  130. $small_img_filename=$_FILES['smallimg']['name'];
  131. } else {
  132. $fileparts=explode('.',$_FILES['smallimg']['name']);
  133. $extension='.'.array_pop($fileparts);
  134. $name=implode('.',$fileparts).'_';
  135. $filecount=0;
  136. while(file_exists($video_img_dir.$name.$filecount.$extension))
  137. $filecount=$filecount+1;
  138. $small_img_filename=$name.$filecount.$extension;
  139. }
  140. }
  141. move_uploaded_file($_FILES['smallimg']['tmp_name'],$video_img_dir.$small_img_filename);
  142. }
  143. }
  144. if(mysql_query("INSERT INTO video VALUES (default,'$heading','$description','$small_img_filename','$link',default);"))
  145. $message .= 'New video created successfully.';
  146. }
  147. if($action == 4) {
  148. /* Blank form */
  149. ?>
  150. <form action="video.php" method="post" enctype="multipart/form-data">
  151. <p>
  152. <div class="admin_label"><label for="id_heading">Heading:</label></div>
  153. <input class="input_wide" id="id_heading" type="text" name="heading" />
  154. </p>
  155. <p>
  156. <div class="admin_label"><label for="id_description">Description:</label></div>
  157. <textarea name="description" id="id_description"></textarea>
  158. </p>
  159. <p>
  160. <div class="admin_label"><label for="id_smallimg">Small Img File:</label></div>
  161. <input type="file" name="smallimg" id="id_smallimg" />
  162. Only jpg/gif images of size less than <?php echo SMALL_IMG_FILE_SIZE/(1024*1024); ?>MB.
  163. </p>
  164. <p>
  165. <div class="admin_label"><label for="id_link">Video link:</label></div>
  166. <input class="input_wide" type="text" name="link" id="id_link" />
  167. </p>
  168. <input type="hidden" name="action" value="3" />
  169. <input type="submit" value="Upload" />
  170. <input type="button" value="Cancel" onclick="javascript:window.location='?';" />
  171. </form>
  172. <?php
  173. }
  174. if($action == 5) {
  175. /* Editing form */
  176. $id = $_GET['id'];
  177. $result = mysql_query("select * from video where id=$id");
  178. $row = mysql_fetch_assoc($result);
  179. ?>
  180. <form action="video.php" method="post" enctype="multipart/form-data">
  181. <p>
  182. <div class="admin_label"><label for="id_heading">Heading:</label></div>
  183. <input class="input_wide" id="id_heading" type="text" name="heading" value="<?php echo $row['heading']; ?>"/>
  184. </p>
  185. <p>
  186. <div class="admin_label"><label for="id_description">Description:</label></div>
  187. <textarea name="description" id="id_description"><?php echo $row['description']; ?></textarea>
  188. </p>
  189. <?php if($row['smallimgurl']!='') { ?>
  190. <p>Small Img File:
  191. <a href="<?php echo $video_img_dir,$row['smallimgurl']; ?>"><?php echo $row['smallimgurl']; ?></a>
  192. <input type="checkbox" name="delete_old_small_img" value="Yes" id="id_delete_old_small_img"/><label for="id_delete_old_small_img">Delete</label>
  193. <input type="file" name="smallimg"/>
  194. Only jpg/gif images of size less than <?php echo SMALL_IMG_FILE_SIZE/(1024*1024); ?>MB.
  195. </p>
  196. <?php } else { ?>
  197. <p>
  198. <div class="admin_label"><label for="id_smallimg">Small Img File:</label></div>
  199. <input type="file" name="smallimg" id="id_smallimg" />
  200. Only jpg/gif images of size less than <?php echo SMALL_IMG_FILE_SIZE/(1024*1024); ?>MB.
  201. </p>
  202. <?php } ?>
  203. <p>
  204. <div class="admin_label"><label for="id_link">Video link:</label></div>
  205. <input style="width:500px;" type="text" name="link" id="id_link" value="<?php echo $row['link']; ?>"/>
  206. <a href="<?php echo $row['link']; ?>">Existing link</a>
  207. </p>
  208. <input type="submit" value="Update" />
  209. <input type="button" value="Cancel" onclick="javascript:window.location='?';" />
  210. <input type="hidden" name="action" value="1"/>
  211. <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  212. </form>
  213. <?php
  214. } /* End of actions */
  215. // Do not show the videos if an entry form was displayed
  216. if($action != 4 && $action !=5) {
  217. $q = "SELECT * FROM video ORDER BY tm DESC";
  218. $r = mysql_query($q);
  219. if($message != '')
  220. if(!$is_message_error)
  221. echo '<div class="admin_message admin_success">',$message,'</div>';
  222. else
  223. echo '<div class="admin_message admin_error">',$message,'</div>';
  224. if ( $r == false || mysql_num_rows($r) == 0 ) {
  225. ?>
  226. <h4>No videos to display</h4>
  227. <p>
  228. No entries have been made on this page.
  229. Please check back soon, or click the
  230. link below to add an entry!
  231. <div class="admin_link" style="float:left">
  232. <a href="?action=4">Add a New Entry</a>
  233. </div>
  234. <div style="float:right">
  235. <a href="admin.php">Back to dashboard</a>
  236. </div>
  237. <div style="clear:both"></div>
  238. </p>
  239. <?php
  240. } else {
  241. while ( $a = mysql_fetch_assoc($r) ) {
  242. $id = stripslashes($a['id']);
  243. ?>
  244. <div class="admin_edit">
  245. <a href="?action=5&id=<?php echo $id; ?>">
  246. <div class="admin_edit_inner">
  247. <h3><?php echo stripslashes($a['heading']); ?></h3>
  248. <p>
  249. <?php echo nl2br(stripslashes($a['description'])); ?>
  250. </p>
  251. </div>
  252. </a>
  253. <div class="admin_delete">
  254. <input type="button" value="Delete" onclick="javascript:if(confirmDelete())window.location='?action=2&id=<?php echo $id; ?>'"/>
  255. </div>
  256. </div>
  257. <?php
  258. }
  259. }
  260. }
  261. ?>
  262. </body>
  263. </html>