PageRenderTime 64ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/music/index.php

https://github.com/cbulock/Band-CMS
PHP | 268 lines | 236 code | 19 blank | 13 comment | 43 complexity | bd86e522380b10d47ed48df257c6a45d MD5 | raw file
  1. <?php
  2. /*/////////////////////////////////////////////////
  3. This code is copyright 2006 Michigan Web Dev and
  4. only to be used with permisson and when licensed.
  5. Code may be modified but may not be distributed.
  6. Michigan Web Dev may not be liable for any loss
  7. caused by this code and gives no warranty.
  8. /////////////////////////////////////////////////*/
  9. include('../../includes/config.php');
  10. $auth = authenticate();
  11. // *** This is the start of the pagination code - this is needed here becase if a
  12. // *** redirect is needed due to an improper page number passed in a url argument,
  13. // *** it has to happen before any output.
  14. $page = 0;
  15. if (isset($_REQUEST['page']))
  16. $page = intval($_REQUEST['page']) - 1;
  17. $numberOfAlbums = db_count("albums");
  18. $numberOfPages = ceil($numberOfAlbums / MUSIC_PER_PAGE);
  19. if ($page * MUSIC_PER_PAGE > $numberOfAlbums)
  20. header('location: index.php');
  21. // *** End of the first part of the pagination code
  22. include('../../includes/header.php');
  23. if($auth['loggedIn'])
  24. {
  25. switch ($_REQUEST['act'])
  26. {
  27. //** Album handling code
  28. case "postalbum":
  29. $posted_date = $_REQUEST['day'] . " " . $_REQUEST['month'] . " " . $_REQUEST['year'];
  30. $timestamp = strtotime($posted_date);
  31. if($timestamp == -1)
  32. {
  33. echo "<div class='error'>There was a problem posting your album - invalid release date.</div>";
  34. break;
  35. }
  36. if (!$_FILES['image']['size']) {
  37. echo "<div class='error'>You must select an image!</div>\n";
  38. break;
  39. }
  40. $types = array(2 => "jpg", 3 => "png");
  41. $image = $_FILES['image']['tmp_name'];
  42. $info = getimagesize($image);
  43. if ($info[2] <> 2 && $info[2] <> 3) { //check that uploaded file is supported
  44. echo "<div class='error'>Not a supported file or image. Image must be a JPEG or PNG.</div>\n";
  45. exit;
  46. }
  47. $addresult = db_insert("albums", array("name" => $_REQUEST['name'], "date" => $timestamp));
  48. if (isset($addresult)) {
  49. $newimg = resize($image, MUSIC_ALBUM_WIDTH);
  50. saveImage($newimg,"../../images/music/","album" .$addresult . "_large.jpg");
  51. echo "<div class='notice'>Album successfully added.</div><br />\n";
  52. $numberOfAlbums = db_count("albums");
  53. $numberOfPages = ceil($numberOfAlbums / MUSIC_PER_PAGE);
  54. } else {
  55. echo "<div class='error'>There was a problem adding your album.</div>";
  56. }
  57. break;
  58. case "deletealbum":
  59. if(db_delete("albums", $_REQUEST['index'])) {
  60. if (!unlink("../../images/music/album" . $_REQUEST['index'] . "_large.jpg")) echo "<div class='error'>An error occured removing album art image.</div>\n";
  61. foreach(db_get_table("tracks", "`album` = " . $_REQUEST['index']) as $item)
  62. {
  63. db_delete('tracks', $item['index']);
  64. }
  65. echo "<div class='notice'>Album successfully deleted.</div>";
  66. $numberOfAlbums = db_count("albums");
  67. $numberOfPages = ceil($numberOfAlbums / MUSIC_PER_PAGE);
  68. if ($page > $numberOfPages - 1) $page = $numberOfPages - 1;
  69. if ($numberOfAlbums == 0) $page = 0;
  70. } else {
  71. echo "<div class='error'>There was a problem deleting your album.</div>";
  72. }
  73. break;
  74. case "modifyalbum":
  75. $posted_date = $_REQUEST['day'] . " " . $_REQUEST['month'] . " " . $_REQUEST['year'];
  76. $timestamp = strtotime($posted_date);
  77. if($timestamp == -1)
  78. {
  79. echo "<div class='error'>There was a problem posting your album - invalid release date.</div>";
  80. break;
  81. }
  82. if ($_FILES['image']['size']) {
  83. $changeFile = TRUE;
  84. $types = array(2 => "jpg", 3 => "png");
  85. $image = $_FILES['image']['tmp_name'];
  86. $info = getimagesize($image);
  87. if ($info[2] <> 2 && $info[2] <> 3) { //check that uploaded file is supported
  88. echo "<div class='error'>ERROR: Not a support file or image. Image must be a JPEG or PNG.</div></div>\n";
  89. exit;
  90. }
  91. }
  92. if(db_update("albums", $_REQUEST['index'], array("name" => $_REQUEST['name'], "date" => $timestamp))) {
  93. if ($changeFile) {
  94. if (!unlink("../../images/music/album" . $_REQUEST['index'] . "_large.jpg")) {
  95. echo "<div class='error'>An error occured removing old album art image.</div>\n";
  96. } else {
  97. $newimg = resize($image, MUSIC_ALBUM_WIDTH);
  98. saveImage($newimg,"../../images/music/","album" .$_REQUEST['index'] . "_large.jpg");
  99. }
  100. }
  101. echo "<div class='notice'>Album successfully modified.</div>";
  102. } else {
  103. echo "<div class='error'>There was a problem modifying your album.</div>";
  104. }
  105. break;
  106. //** Track handling code
  107. case "posttrack":
  108. $trackcount = db_count("tracks", "`album` = " . $_REQUEST['album']);
  109. if($trackcount != 0)
  110. {
  111. switch($_REQUEST['trackoption'])
  112. {
  113. case "first":
  114. $selectedtrack = 1;
  115. break;
  116. case "last":
  117. $selectedtrack = $trackcount + 1;
  118. break;
  119. case "after":
  120. $selectedtrack = $_REQUEST['aftertrack'] + 1;
  121. if($selectedtrack > $trackcount + 1) $selectedtrack = $trackcount + 1;
  122. break;
  123. }
  124. }
  125. else
  126. {
  127. $selectedtrack = 1;
  128. }
  129. if(db_insert_ordered("tracks", "track", array("name" => $_REQUEST['name'], "album" => $_REQUEST['album'], "track" => $selectedtrack, "lyrics" => $_REQUEST['lyrics']), "`album` = " . $_REQUEST['album']))
  130. echo "<div class='notice'>Track successfully added.</div>";
  131. else
  132. echo "<div class='error'>There was a problem adding your track.</div>";
  133. break;
  134. case "deletetrack":
  135. $thistrack = db_get_item("tracks", $_REQUEST['index']);
  136. if(db_delete_ordered("tracks", "track", $_REQUEST['index'], "`album` = " . $thistrack['album']))
  137. echo "<div class='notice'>Track successfully deleted.</div>";
  138. else
  139. echo "<div class='error'>There was a problem deleting your track.</div>";
  140. break;
  141. case "modifytrack":
  142. if(db_update("tracks", $_REQUEST['index'], array("name" => $_REQUEST['name'], "lyrics" => $_REQUEST['lyrics'])))
  143. echo "<div class='notice'>Track successfully modified.</div>";
  144. else
  145. echo "<div class='error'>There was a problem modifying your track.</div>";
  146. break;
  147. }
  148. ?>
  149. <div class="admin_item">
  150. <div class='item_title'>Add a new album:</div>
  151. <div class="item_body">
  152. <form name="modify" action="index.php?act=postalbum&amp;page=<?php echo $page + 1; ?>" method="post" enctype="multipart/form-data">
  153. <table>
  154. <tr>
  155. <td>
  156. Name
  157. </td>
  158. <td>
  159. <input size="50" type="text" name="name" value="">
  160. </td>
  161. </tr>
  162. <?php
  163. $timestamp = time();
  164. include('when.php');
  165. ?>
  166. <tr>
  167. <td>
  168. Album Art
  169. </td>
  170. <td>
  171. <input size="50" type='file' name='image' />
  172. </td>
  173. </tr>
  174. <tr>
  175. <td>
  176. </td>
  177. <td>
  178. <br />
  179. <input type="hidden" name="index" value="<?php echo $albumindex; ?>">
  180. <input type="submit" name="submit" value="Add this item">
  181. </td>
  182. </tr>
  183. </table>
  184. </form>
  185. <br />
  186. </div>
  187. </div>
  188. <?php
  189. }
  190. if (db_count("albums") > 0)
  191. {
  192. $musicArray = db_get_table("albums", "1", "`date` DESC", "LIMIT " . $page * MUSIC_PER_PAGE . "," . MUSIC_PER_PAGE);
  193. foreach($musicArray as $item)
  194. {
  195. echo "<div class='music_item'>";
  196. echo "<div class='item_title'>";
  197. if($item['name'])
  198. echo $item['name'];
  199. else
  200. echo "<i>untitled</i>";
  201. echo " - released " . date(MUSIC_DATE, $item['date']);
  202. if($auth['loggedIn']) echo " - <a href='modify.php?act=modifyalbum&amp;page=" . intval($page + 1) . "&amp;index=" . $item['index'] . "'>Modify Album</a> - <a href='modify.php?act=posttrack&amp;page=" . intval($page + 1) . "&amp;index=" . $item['index'] . "'>Add Track</a>";
  203. echo "</div>";
  204. echo "<div class='item_body'>";
  205. echo "<table><tr><td width=" . MUSIC_ALBUM_WIDTH . ">\n";
  206. echo "<img src='../../images/music/album" . $item['index'] . "_large.jpg' />\n";
  207. echo "</td><td valign='top'>";
  208. $tracks = db_get_table("tracks", "`album` = " . $item['index'], "`track` ASC");
  209. if(!$tracks)
  210. {
  211. echo "<i>No tracks</i>";
  212. }
  213. else
  214. {
  215. foreach($tracks as $track)
  216. {
  217. $linka = ""; $linkb = "";
  218. if ($track['lyrics']) {
  219. $linka = "<a href='track.php?track=" . $track['index'] . "&amp;album=" . $item['index'] . "'>";
  220. $linkb = "</a>";
  221. }
  222. if($auth['loggedIn']) echo "(<a href='modify.php?act=modifytrack&amp;page=" . intval($page + 1) . "&amp;index=" . $track['index'] . "'>Edit</a>) - \n";
  223. echo $track['track'] . " - " . $linka . $track['name'] . $linkb . "\n";
  224. echo "<br />";
  225. }
  226. }
  227. echo "</td></tr></table></div></div>";
  228. }
  229. if($numberOfAlbums > MUSIC_PER_PAGE) //we need to paginate
  230. {
  231. echo "<div class='pagelist'>Go to page: ";
  232. for($x = 1; $x <= $numberOfPages; $x++)
  233. {
  234. if($x == $page + 1)
  235. echo "<span class='pagelist_item'>" . $x . "</span>";
  236. else
  237. echo "<a class='pagelist_item' href='index.php?page=" . $x . "'>" . $x . "</a>";
  238. }
  239. echo "</div>";
  240. }
  241. }
  242. else
  243. {
  244. echo "<div class='music_item'>";
  245. echo "<div class='item_title'>" . MUSIC_NAME . "</div>";
  246. echo "<div class='item_body'><em>There are no albums to display.</em></div></div>";
  247. }
  248. include('../../includes/footer.php');
  249. ?>