edit.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. $type = "";
  3. $uid = "";
  4. if (isset($_GET['uid'])) {
  5. $uid = $_GET['uid'];
  6. }
  7. if (isset($_GET['type'])) {
  8. $type = $_GET['type'];
  9. } else {
  10. die("Missing arguments.");
  11. }
  12. if (!hasPermission("edit.$type")) {
  13. die("Not enough permissions.");
  14. }
  15. //Save to database script:
  16. if (isset($_POST['name']) and $type !== "CSS") {
  17. $name = $conn->escapeString($_POST['name']);
  18. $uid = $_POST['uid'];
  19. $sql = "";
  20. if (isset($_POST['content']) and ($type === "Section" or $type === "Component")) {
  21. $content = $conn->escapeString($_POST['content']);
  22. $sql = "UPDATE $type SET name='$name', content='$content' WHERE UID='$uid'";
  23. } else if (isset($_POST['sections']) and $type === "Page") {
  24. $css = $_POST['css'];
  25. $sections = $_POST['sections'];
  26. $sql = "UPDATE Page SET name='$name', sections='$sections', CSS='$css' WHERE UID='$uid'";
  27. } else if ($type === "Menu") {
  28. $value = $conn->escapeString($_POST['value']);
  29. $valuetype = $_POST['type'];
  30. $sql = "UPDATE Menu SET name='$name', valuetype='$valuetype', value='$value' WHERE UID='$uid'";
  31. }
  32. if ($conn->query($sql)) {
  33. echo "<script>display_message('Successfully saved $type.')</script>";
  34. } else {
  35. echo "<script>display_message('Something broke: " . $conn->lastErrorMsg() . "', 'danger')</script>";
  36. }
  37. } else if (isset($_POST['css'])) {
  38. $file = fopen("../stylesheets/StyleSheet.css", "w");
  39. fwrite($file, $_POST['css']);
  40. fclose($file);
  41. }
  42. //View building script:
  43. if ($type !== "CSS") {
  44. $query = $conn->query("SELECT * FROM $type WHERE UID='$uid'");
  45. $row = $query->fetchArray();
  46. $name = isset($row['name']) ? $row['name'] : $row['User'];
  47. }
  48. $inputs = array();
  49. switch ($type) {
  50. case "Section":
  51. case "Component":
  52. $content = $row['content'];
  53. $inputs = array("<label>Section content:</label> <br /> <textarea id='code' name='content'>$content</textarea><div id='editor' style='height: 500px; width: 100%;'></div>");
  54. break;
  55. case "Page":
  56. $sections = $row['sections'];
  57. $css = $row['CSS'];
  58. $inputs = array(
  59. "<div class='input-group'><span class='input-group-addon' id='section-addon'>Page setions</span><input class='form-control' type='text' readonly id='sections' name='sections' value='$sections' aria-describedby='section-addon' data-toggle='tooltip' title='Drag the sections around in the preview to change the order. Use the dropdown below to add sections.'/><div class='input-group-btn'><button class='btn btn-primary' id='clean-json' type='button'>Cleanup JSON <span class='fa fa-code'></span></button></div></div>",
  60. "<div class='input-group'><span class='input-group-addon' id='add-section-addon'>Add section</span><select class='form-control' id='sectionselect' name='sectionselect' aria-describedby='add-section-addon'/></select><div class='input-group-btn'><button class='btn btn-primary' id='add-section' type='button'>Add <span class='fa fa-plus'></span></button></div></div>",
  61. "<label>Custom CSS:</label> <br /> <textarea id='code' name='css'>$css</textarea><div id='editor' style='height: 500px; width: 100%;'></div>"
  62. );
  63. break;
  64. case "CSS":
  65. $name = "StyleSheet.css";
  66. $filename = "../stylesheets/$name";
  67. $readfile = fopen($filename, "r");
  68. $css = fread($readfile, filesize($filename));
  69. $inputs = array(
  70. "<label>CSS:</label> <br /> <textarea id='code' name='css'>$css</textarea><div id='editor' style='height: 500px; width: 100%;'></div>"
  71. );
  72. break;
  73. case "Menu":
  74. $itemtype = $row['valuetype'];
  75. $isPage = ""; $isLink = "";
  76. if ($itemtype == "page") {
  77. $isPage = "selected";
  78. } else {
  79. $isLink = "selected";
  80. }
  81. $page_options = "";
  82. $pagequery = $conn->query("SELECT * FROM Page ORDER BY ListId");
  83. while ($pagerow = $pagequery->fetchArray()) {
  84. $page_name = $pagerow['name'];
  85. $page_selected = "";
  86. if ($row['value'] == $page_name) {
  87. $page_selected = "selected";
  88. }
  89. $page_options .= "<option value='$page_name' $page_selected>$page_name</option>";
  90. }
  91. $menu_value = $row['value'];
  92. $inputs = array("<div class='input-group'><span class='input-group-addon' id='type-addon'>Menuitem type:</span>
  93. <select class='form-control' name='type' id='typeselect' aria-describedby='type-addon'>
  94. <option value='page' $isPage>Page</option>
  95. <option value='link' $isLink>Link</option>
  96. </select></div>",
  97. "<div class='input-group' id='pageselect'><span class='input-group-addon' id='value-addon'>Value:</span>
  98. <select class='form-control' name='value' aria-describedby='value-addon'>
  99. $page_options
  100. </select></div>" .
  101. "<div class='input-group' id='linkselect'><span class='input-group-addon' id='link-addon'>Link:</span><input class='form-control' name='value' type='text' value='$menu_value'/></div>",
  102. "<script>updateSelect();</script>");
  103. break;
  104. case "Users":
  105. ob_start();
  106. include "pages/edit/user.php";
  107. $inputs = array(ob_get_clean());
  108. break;
  109. }
  110. ?>
  111. <div class="row">
  112. <div class="col-lg-12">
  113. <h1 class="page-header"><?php echo "Edit $type"; ?></h1>
  114. </div>
  115. <!-- /.col-lg-12 -->
  116. </div>
  117. <div class="row">
  118. <div class="col-lg-6">
  119. <form action="" method="POST">
  120. <input name="uid" type="hidden" value="<?php echo $uid; ?>"/>
  121. <div class="input-group"><span class="input-group-addon" id="name-addon"><?php echo $type; ?> name:</span><input class="form-control" aria-describedby="name-addon" name="name" type="text" value="<?php echo $name ?>"/></div> <br />
  122. <?php
  123. foreach ($inputs as $input) {
  124. echo $input . "<br />";
  125. }
  126. ?>
  127. <button class="btn btn-lg btn-primary" style="float: left;" type="submit"><i class="fa fa-floppy-o"></i> Save</button>
  128. </form>
  129. </div>
  130. <!-- /.col-lg-6 -->
  131. <div class="col-lg-6">
  132. <?php
  133. if (isset($row['CSS']))
  134. echo "<style>".$row['CSS']."</style>";
  135. echo "<div id='preview' class='$type'>";
  136. if ($type === "Page") {
  137. $sections = json_decode($row['sections']);
  138. if ($sections === NULL)
  139. $sections = explode(',', $row['sections']);
  140. $app = new App(true);
  141. foreach ($sections as $section) {
  142. $app->addSection($section);
  143. }
  144. } else if ($type === "Section") {
  145. echo $row['content'];
  146. }
  147. echo '</div>';
  148. ?>
  149. </div>
  150. <!-- /.col-lg-6 -->
  151. </div>
  152. <script src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.1/ace.js'></script>
  153. <script src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.1/mode-html.js'></script>
  154. <script src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.1/mode-css.js'></script>
  155. <script src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.1/theme-monokai.js'></script>
  156. <script>
  157. var code = $("#code").hide();
  158. var editor = ace.edit("editor");
  159. editor.setTheme("ace/theme/monokai");
  160. editor.getSession().setMode("ace/mode/html");
  161. editor.getSession().setUseWrapMode(true);
  162. if (code.prop('name') === "css") {
  163. editor.getSession().setMode("ace/mode/css");
  164. }
  165. editor.getSession().setValue(code.val());
  166. editor.getSession().on('change', function() {
  167. code.val(editor.getSession().getValue());
  168. refreshPreview(editor.getSession().getValue());
  169. });
  170. function makeSortable() {
  171. $("#preview").sortable({ opacity: 0.6, cursor: 'move', update: function() {
  172. var order = $(this).sortable("toArray");
  173. $("#sections").val(JSON.stringify(order));
  174. }}).disableSelection();
  175. }
  176. function refreshPreview(str) {
  177. var preview = $("#preview");
  178. if (preview.hasClass("Section")) {
  179. $("#preview").html(str);
  180. } else if (preview.hasClass("Page")) {
  181. $.get( "actions/get_sections.php", { sections: $("#sections").val() } )
  182. .done(function( data ) {
  183. $("#preview").html(data);
  184. });
  185. }
  186. }
  187. function updateSelectBox() {
  188. $.get( "actions/get_sections.php", { listall: true } )
  189. .done(function(data) {
  190. var allsections = JSON.parse(data);
  191. var usedsections = JSON.parse($("#sections").val());
  192. usedsections.forEach( function (element, index, array) {
  193. delete allsections[element];
  194. });
  195. var selectBox = $('#sectionselect');
  196. selectBox.empty();
  197. $.each(allsections, function(key, value) {
  198. selectBox
  199. .append($("<option></option>")
  200. .attr("value",key)
  201. .text(value));
  202. });
  203. });
  204. }
  205. $(document).ready(function() {
  206. if ($("#preview").attr('class') == "Page") {
  207. makeSortable();
  208. updateSelectBox();
  209. }
  210. $("#add-section").click(function() {
  211. var sections = JSON.parse($("#sections").val());
  212. sections.push($("#sectionselect").val());
  213. sections = sections.map(function (uid) {
  214. return parseInt(uid);
  215. });
  216. $("#sections").val(JSON.stringify(sections));
  217. refreshPreview();
  218. updateSelectBox();
  219. });
  220. $("#clean-json").click(function () {
  221. var sectionsString = $("#sections").val();
  222. var sections;
  223. try {
  224. sections = JSON.parse(sectionsString);
  225. sections = sections.map(function (uid) {
  226. return parseInt(uid);
  227. });
  228. $("#sections").val(JSON.stringify(sections));
  229. } catch (e) {
  230. sections = sectionsString.split(",");
  231. $.get( "actions/get_sections.php", { getids: JSON.stringify(sections) } )
  232. .done(function( data ) {
  233. console.log(data);
  234. $("#sections").val(data);
  235. });
  236. }
  237. });
  238. });
  239. </script>