PageRenderTime 28ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/template/pages/news-edit.php

https://gitlab.com/Nightprince/Warcry-CMS
PHP | 436 lines | 315 code | 82 blank | 39 comment | 13 complexity | 64cf20beb24062ac150010d2ef89ba39 MD5 | raw file
  1. <?PHP
  2. if (!defined('init_pages'))
  3. {
  4. header('HTTP/1.0 404 not found');
  5. exit;
  6. }
  7. $id = isset($_GET['id']) ? (int)$_GET['id'] : false;
  8. //Get errors
  9. if ($error = $ERRORS->DoPrint('editNews'))
  10. {
  11. echo $error;
  12. }
  13. unset($error);
  14. ?>
  15. <!-- Secondary navigation -->
  16. <nav id="secondary" class="disable-tabbing">
  17. <ul>
  18. <li><a href="index.php?page=news">News</a></li>
  19. <li><a href="index.php?page=news-post">Post</a></li>
  20. <li class="current"><a href="#">Edit</a></li>
  21. <li><a href="#thirdtab">Settings</a></li>
  22. </ul>
  23. </nav>
  24. <?php
  25. //check for permissions
  26. if (!$CURUSER->getPermissions()->isAllowed(PERMISSION_NEWS))
  27. {
  28. $CORE->ErrorBox('You do not have the required permissions.');
  29. }
  30. //Check if we have an ID
  31. if (!$id)
  32. {
  33. $CORE->ErrorBox('The news id is missing.');
  34. }
  35. //lookup the record
  36. $res = $DB->prepare("SELECT * FROM `news` WHERE `id` = :id LIMIT 1");
  37. $res->bindParam(':id', $id, PDO::PARAM_INT);
  38. $res->execute();
  39. //verify that we found the record
  40. if ($res->rowCount() == 0)
  41. {
  42. $CORE->ErrorBox('The news record is invalid or missing.');
  43. }
  44. $row = $res->fetch();
  45. ?>
  46. <!-- The content -->
  47. <section id="content">
  48. <div class="tab" id="maintab">
  49. <h2>News Management</h2>
  50. <div class="form">
  51. <form method="post" action="<?php echo $config['BaseURL']; ?>/admin/execute.php?take=editNews&id=<?php echo $row['id']; ?>" id="newsForm" name="editNewsForm">
  52. <section>
  53. <label for="label">
  54. Headline*
  55. <small>250 characters maximum.</small>
  56. </label>
  57. <div>
  58. <input id="label" name="title" type="text" class="required" value="<?php echo $row['title']; ?>"/>
  59. </div>
  60. </section>
  61. <section>
  62. <label for="textarea">
  63. Short Text*
  64. <small>500 characters maximum.</small>
  65. </label>
  66. <div>
  67. <textarea class="required" id="textarea" rows="7" name="shortText"><?php echo $row['shortText']; ?></textarea>
  68. </div>
  69. </section>
  70. <section>
  71. <label for="textarea">
  72. Content*
  73. </label>
  74. <div>
  75. <textarea class="required bbcode" id="textarea" name="text"><?php echo $row['text']; ?></textarea>
  76. </div>
  77. </section>
  78. <input type="hidden" name="image" id="newsImage" value="<?php echo $row['image']; ?>" />
  79. <script>
  80. $(document).ready(function()
  81. {
  82. productUpdatePreview('<?php echo $config['BaseURL'] . '/uploads/news/icons/' . $row['image']; ?>', '<?php echo $row['image']; ?>');
  83. });
  84. </script>
  85. </form>
  86. <section>
  87. <label for="textarea">
  88. Icon
  89. <small>Leave bank to set the default image.</small>
  90. </label>
  91. <div>
  92. <div id="image_Loading" style="display: none;">
  93. Loading...<br /><br /><br />
  94. </div>
  95. <div id="image_PreviewSection" style="display: none; margin-bottom: 5px;">
  96. </div>
  97. <form id="croppingForm" method="POST" onsubmit="return false;" action="<?php echo $config['BaseURL']; ?>/admin/execute.php?take=cropImage" name="cropForm">
  98. <input type="hidden" name="path" value="/admin/tempUploads" />
  99. <input type="hidden" id="jCrop-imageName" name="imgName" value="" />
  100. <input type="hidden" name="resize" value="200" />
  101. <input type="hidden" id="x" name="x" />
  102. <input type="hidden" id="y" name="y" />
  103. <input type="hidden" id="w" name="w" />
  104. <input type="hidden" id="h" name="h" />
  105. <div id="image_CroppingSection" style="display: none; margin-bottom: 5px;">
  106. <div id="uploadedImagePreview" style="width: 200px !important; display: inline-block;"></div>
  107. <div id="CropResult" style="display: inline-block; max-width: 210px; padding-left: 5px; vertical-align: top;">
  108. <button class="button primary" onclick="cropFormSubmit();">Crop</button>
  109. </div>
  110. </div>
  111. </form>
  112. <form id="uploadForm" method="POST" name="thumbForm" enctype="multipart/form-data">
  113. <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
  114. <input id="label" type="file" name="file" onchange="ajaxFormSubmit()"/>
  115. <input type="submit" value="submit" style="display: none;" />
  116. </form>
  117. </div>
  118. </section>
  119. <div class="clear"></div>
  120. </div>
  121. <br />
  122. <p>
  123. <span class="button-group">
  124. <input type="button" class="button primary submit" value="Submit" onclick="return submitNews();"/>
  125. <a href="index.php?page=news" class="button">Cancel</a>
  126. </span>
  127. </p>
  128. </div>
  129. <script type="text/javascript" src="template/js/forms.js"></script>
  130. <script src="template/js/jquery.color.js" type="text/javascript"></script>
  131. <script src="template/js/jquery.Jcrop.min.js" type="text/javascript"></script>
  132. <script src="template/js/jquery.form.js" type="text/javascript"></script>
  133. <script src="template/js/sceditor/jquery.sceditor.js" type="text/javascript"></script>
  134. <script src="template/js/sceditor/jquery.sceditor.bbcode.js" type="text/javascript"></script>
  135. <script type="text/javascript">
  136. var $configURL = '<?php echo $config['BaseURL']; ?>';
  137. //apply the BBcode Editors
  138. $(document).ready(function(e)
  139. {
  140. <?php
  141. if ($formData = $ERRORS->multipleError_accessFormData('editNews'))
  142. {
  143. echo '
  144. var savedFormData = $.parseJSON(', json_encode(json_encode($formData)), ');
  145. restoreFormData(\'editNewsForm\', savedFormData);';
  146. }
  147. unset($formData);
  148. ?>
  149. $("textarea.bbcode").sceditor({
  150. plugins: 'bbcode',
  151. style: 'template/css/bbcode-default-iframe.css'
  152. });
  153. //custom settings for validation
  154. $("#newsForm").validate(
  155. {
  156. rules:
  157. {
  158. shortText:
  159. {
  160. minlength: 1,
  161. maxlength: 500
  162. },
  163. }
  164. });
  165. });
  166. //A function to submit the news
  167. function submitNews()
  168. {
  169. $('#newsForm').submit();
  170. }
  171. //////////////////////////////////////////////////////////////////
  172. // CROPPING & RESIZING
  173. //////////////////////////////////////////////////////////////////
  174. // Create variables (in this scope) to hold the API and image size
  175. var $jcrop_api, $boundx, $boundy;
  176. var $maxWidth = 229;
  177. var $jcrop_update = function(c)
  178. {
  179. $('#x', $currentTab).val(c.x);
  180. $('#y', $currentTab).val(c.y);
  181. $('#w', $currentTab).val(c.w);
  182. $('#h', $currentTab).val(c.h);
  183. }
  184. var $jcrop_clearCoords = function()
  185. {
  186. $('#croppingForm input', $currentTab).val('');
  187. }
  188. var $jcrop_options =
  189. {
  190. onChange: $jcrop_update,
  191. onSelect: $jcrop_update,
  192. onRelease: $jcrop_clearCoords,
  193. allowResize: true,
  194. allowSelect: false,
  195. setSelect: [229, 148, 0, 0],
  196. trueSize: [],
  197. aspectRatio: 1.555
  198. }
  199. var $jcrop_arg2 = function()
  200. {
  201. // Use the API to get the real image size
  202. var bounds = this.getBounds();
  203. $boundx = bounds[0];
  204. $boundy = bounds[1];
  205. // Store the API in the jcrop_api variable
  206. $jcrop_api = this;
  207. }
  208. function cropFormSubmit()
  209. {
  210. var cropForm = $("#croppingForm", $currentTab);
  211. var cropppingSection = $('#image_CroppingSection', $currentTab);
  212. cropppingSection.hide();
  213. $('#image_Loading', $currentTab).html('Loading...');
  214. $('#image_Loading', $currentTab).css('display', 'block');
  215. cropForm.ajaxSubmit(
  216. {
  217. url: 'execute.php?take=cropImage',
  218. success: function(data)
  219. {
  220. //hide the loading
  221. $('#image_Loading', $currentTab).fadeOut('fast');
  222. //check if we got errors
  223. var ajaxStatus = null;
  224. if (data.indexOf("@AjaxError@") > -1)
  225. {
  226. ajaxStatus = false;
  227. }
  228. else
  229. {
  230. ajaxStatus = true;
  231. }
  232. if (!ajaxStatus)
  233. {
  234. //we got error
  235. $('#image_Loading', $currentTab).html(data);
  236. $('#image_Loading', $currentTab).fadeIn('fast');
  237. //unpopulate the hidden input for the product
  238. $("#newsImage", $currentTab).val("");
  239. }
  240. else
  241. {
  242. var image_src = $configURL + "/admin/tempUploads/"+ data;
  243. //update preview
  244. productUpdatePreview(image_src, data);
  245. }
  246. }
  247. });
  248. return false;
  249. }
  250. //////////////////////////////////////////////////////////////////
  251. // Ajax Upload
  252. //////////////////////////////////////////////////////////////////
  253. var ajaxOptions =
  254. {
  255. url: 'ajax.php?phase=1',
  256. beforeSubmit: function(a,f,o)
  257. {
  258. $('#image_CroppingSection', $currentTab).hide();
  259. $('#image_PreviewSection', $currentTab).hide();
  260. $('#image_Loading', $currentTab).html('Loading...');
  261. $('#image_Loading', $currentTab).css('display', 'block');
  262. },
  263. success: function(data)
  264. {
  265. //check if we got errors
  266. var ajaxStatus = null;
  267. if (data.indexOf("@AjaxError@") > -1)
  268. {
  269. ajaxStatus = false;
  270. }
  271. else
  272. {
  273. ajaxStatus = true;
  274. }
  275. if (!ajaxStatus)
  276. {
  277. //we got error
  278. $('#image_Loading', $currentTab).html(data);
  279. //unpopulate the hidden input for the product
  280. $("#newsImage", $currentTab).val("");
  281. }
  282. else
  283. {
  284. //no errors
  285. $('#image_Loading', $currentTab).css('display', 'none');
  286. var image_src = $configURL + "/admin/tempUploads/"+ data;
  287. //update preview
  288. productUpdatePreview(image_src, data);
  289. }
  290. }
  291. }
  292. function ajaxFormSubmit()
  293. {
  294. $('#uploadForm', $currentTab).ajaxSubmit(ajaxOptions);
  295. }
  296. //////////////////////////////////////////////////////////////////
  297. // A function to help me switch stuff
  298. //////////////////////////////////////////////////////////////////
  299. function productUpdatePreview(imageSrc, imageName)
  300. {
  301. var cropppingSection = $('#image_CroppingSection', $currentTab);
  302. var previewSection = $('#image_PreviewSection', $currentTab);
  303. var $imageHeight;
  304. var $imageWidth;
  305. //hide the section in case we switched images
  306. cropppingSection.hide();
  307. previewSection.hide();
  308. function getWidthAndHeight()
  309. {
  310. $imageHeight = this.height;
  311. $imageWidth = this.width;
  312. proceed();
  313. return true;
  314. }
  315. var myImage = new Image();
  316. myImage.name = imageName;
  317. myImage.onload = getWidthAndHeight;
  318. myImage.src = imageSrc;
  319. function proceed()
  320. {
  321. if ($imageWidth > 229 || $imageHeight > 148)
  322. {
  323. //open the cropping section
  324. var previewContainer = cropppingSection.find('#uploadedImagePreview');
  325. previewContainer.fadeIn();
  326. //empty the container
  327. previewContainer.html('');
  328. //append image
  329. var image = document.createElement("img");
  330. image.src = imageSrc;
  331. $(image).css({ maxWidth: $maxWidth, });
  332. previewContainer.append(image);
  333. cropppingSection.fadeIn("slow");
  334. //set image name input
  335. $("#jCrop-imageName", $currentTab).val(imageName);
  336. $jcrop_options.trueSize = [$imageWidth, $imageHeight];
  337. //start the crop
  338. $(image).Jcrop($jcrop_options , $jcrop_arg2);
  339. }
  340. else
  341. {
  342. //empty the container
  343. previewSection.html('');
  344. //append image
  345. var image = document.createElement("img");
  346. image.src = imageSrc;
  347. $(previewSection).append(image);
  348. previewSection.fadeIn("slow");
  349. //populate the hidden input for the product
  350. $("#newsImage", $currentTab).val(imageName);
  351. }
  352. }
  353. }
  354. </script>