PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/include/admin/newfolder.php

https://github.com/mysnip/Core
PHP | 313 lines | 193 code | 49 blank | 71 comment | 42 complexity | 4bd73e609e344640db1fa56cb13975c1 MD5 | raw file
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2011 Phorum Development Team //
  5. // http://www.phorum.org //
  6. // //
  7. // This program is free software. You can redistribute it and/or modify //
  8. // it under the terms of either the current Phorum License (viewable at //
  9. // phorum.org) or the Phorum License that was distributed with this file //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  14. // //
  15. // You should have received a copy of the Phorum License //
  16. // along with this program. //
  17. // //
  18. ////////////////////////////////////////////////////////////////////////////////
  19. if (!defined("PHORUM_ADMIN")) return;
  20. require_once './include/api/forums.php';
  21. require_once PHORUM_PATH.'/include/api/lang.php';
  22. require_once PHORUM_PATH.'/include/api/template.php';
  23. $errors = array();
  24. // ----------------------------------------------------------------------
  25. // Handle posted form data
  26. // ----------------------------------------------------------------------
  27. if (count($_POST))
  28. {
  29. // Build a folder data array based on the posted data.
  30. $folder = array();
  31. $enable_vroot = FALSE;
  32. foreach ($_POST as $field => $value)
  33. {
  34. // The "vroot" field is a virtual field for this form. It only
  35. // indicates that the folder has to be activated as a vroot.
  36. // In the data, the vroot indicates to what vroot a forum or
  37. // folder belongs. To make a certain folder a vroot folder, we
  38. // have to set the vroot field to the same value as the forum_id
  39. // field later on.
  40. if ($field == 'vroot') {
  41. $enable_vroot = TRUE;
  42. }
  43. // All other fields are simply copied.
  44. elseif (array_key_exists($field, $PHORUM['API']['folder_fields'])) {
  45. $folder[$field] = $value;
  46. }
  47. }
  48. // Was a title filled in for the folder?
  49. if (!defined('PHORUM_DEFAULT_OPTIONS') && trim($folder['name']) == '') {
  50. $errors[] = 'The "Title" field is empty. Please, fill in a title.';
  51. }
  52. // If there were no errors, then store the data in the database.
  53. if (empty($errors))
  54. {
  55. // Some statically assigned fields.
  56. $folder['folder_flag'] = 1;
  57. // For new folders.
  58. if (!defined('PHORUM_EDIT_FOLDER')) {
  59. $folder['forum_id'] = NULL;
  60. }
  61. // Store the forum data in the database.
  62. $newfolder = phorum_api_forums_save($folder);
  63. // Handle enabling and disabling vroot support.
  64. // Currently stored as a vroot folder?
  65. if ($newfolder['vroot'] == $newfolder['forum_id']) {
  66. // And requested to disable the vroot?
  67. if (! $enable_vroot) {
  68. phorum_api_forums_save(array(
  69. 'forum_id' => $newfolder['forum_id'],
  70. 'vroot' => $newfolder['parent_id']
  71. ));
  72. }
  73. }
  74. // Currently not a vroot folder?
  75. else {
  76. // And requested to enable the vroot?
  77. if ($enable_vroot) {
  78. phorum_api_forums_save(array(
  79. 'forum_id' => $newfolder['forum_id'],
  80. 'vroot' => $newfolder['forum_id']
  81. ));
  82. }
  83. }
  84. // The message to show on the next page.
  85. $okmsg = "Folder \"{$folder['name']}\" was successfully saved";
  86. // The URL to redirect to.
  87. $url = phorum_admin_build_url(array('module=default',"parent_id=".$folder['parent_id'],'okmsg='.rawurlencode($okmsg)));
  88. phorum_api_redirect($url);
  89. exit;
  90. }
  91. }
  92. // ----------------------------------------------------------------------
  93. // Handle initializing the form for various cases
  94. // ----------------------------------------------------------------------
  95. // Initialize the form for editing an existing folder.
  96. elseif (defined("PHORUM_EDIT_FOLDER"))
  97. {
  98. $folder_id = isset($_POST['forum_id'])
  99. ? $_POST['forum_id'] : $_GET['forum_id'];
  100. $folder = phorum_api_forums_by_forum_id(
  101. $folder_id, PHORUM_FLAG_INCLUDE_INACTIVE
  102. );
  103. }
  104. // Initialize the form for creating a new folder.
  105. else
  106. {
  107. $parent_id = $PHORUM['vroot'];
  108. if (!empty($_GET['parent_id'])) {
  109. $parent_id = (int) $_GET['parent_id'];
  110. }
  111. // Prepare a folder data array for initializing the form.
  112. $folder = phorum_api_forums_save(array(
  113. 'forum_id' => NULL,
  114. 'folder_flag' => 1,
  115. 'inherit_id' => 0,
  116. 'parent_id' => $parent_id,
  117. 'name' => ''
  118. ), PHORUM_FLAG_PREPARE);
  119. }
  120. extract($folder);
  121. // The vroot parameter in the form is a checkbox, while the value in
  122. // the database is a forum_id. We have to do a translation here.
  123. if (isset($enable_vroot)) { // set when posting a form
  124. $vroot = $enable_vroot ? 1 : 0;
  125. } elseif (!empty($forum_id) && $vroot == $forum_id) {
  126. $vroot = 1;
  127. } else {
  128. $foreign_vroot = $vroot;
  129. $vroot = 0;
  130. }
  131. // If we are inheriting settings from a forum,
  132. // then disable the inherited fields in the input.
  133. $disabled_form_input = '';
  134. if ($inherit_id != -1) {
  135. $disabled_form_input = 'disabled="disabled"';
  136. }
  137. // ----------------------------------------------------------------------
  138. // Handle displaying the folder settings form
  139. // ----------------------------------------------------------------------
  140. if ($errors) {
  141. phorum_admin_error(join("<br/>", $errors));
  142. }
  143. require_once './include/admin/PhorumInputForm.php';
  144. $frm = new PhorumInputForm ("", "post");
  145. // Edit an existing folder.
  146. if (defined("PHORUM_EDIT_FOLDER"))
  147. {
  148. $frm->hidden("module", "editfolder");
  149. $frm->hidden("forum_id", $forum_id);
  150. $title = "Edit existing folder";
  151. }
  152. // Create a new folder.
  153. else
  154. {
  155. $frm->hidden("module", "newfolder");
  156. $title="Add A Folder";
  157. $folders = $folder_data;
  158. }
  159. $frm->addbreak($title);
  160. $frm->addrow("Folder Title", $frm->text_box("name", $name, 30));
  161. $frm->addrow("Folder Description", $frm->textarea("description", $description, $cols=60, $rows=10, "style=\"width: 100%;\""), "top");
  162. $parent_id_options = phorum_api_forums_get_parent_id_options($forum_id);
  163. $frm->addrow(
  164. "Put this forum below folder",
  165. $frm->select_tag('parent_id', $parent_id_options, $parent_id)
  166. );
  167. $frm->addrow("Make this forum visible in the forum index?", $frm->select_tag("active", array("No", "Yes"), $active));
  168. $row = $frm->addrow("Virtual Root for descending forums/folders", $frm->checkbox("vroot","1","enabled",($vroot)?1:0));
  169. $frm->addhelp($row,
  170. "Virtual Root for descending forums/folders",
  171. "If you enable the virtual root feature for a folder, then this folder
  172. will act as a separate Phorum installation. The folder will not be
  173. visible in its parent folder anymore and if you visit the folder, it will
  174. behave as if it were a Phorum root folder. This way you can run
  175. multiple separated forums on a single Phorum installation.<br/><br/>
  176. The users will be able to access all virtual root folders, unless you
  177. use the permission system to setup different access rules."
  178. );
  179. if ($foreign_vroot > 0) {
  180. $frm->addrow(
  181. "This folder is in the Virtual Root of:",
  182. $folders[$foreign_vroot]
  183. );
  184. }
  185. $frm->addbreak("Inherit Folder Settings");
  186. $inherit_id_options = phorum_api_forums_get_inherit_id_options($forum_id);
  187. $row = $frm->addrow(
  188. "Inherit the settings below this option from",
  189. $frm->select_tag(
  190. "inherit_id", $inherit_id_options, $inherit_id
  191. ) . $add_inherit_text
  192. );
  193. $frm->addbreak("Display Settings");
  194. $frm->addrow("Template", $frm->select_tag("template", phorum_api_template_list(TRUE), $template, $disabled_form_input));
  195. $frm->addrow("Language", $frm->select_tag("language", phorum_api_lang_list(TRUE), $language, $disabled_form_input));
  196. phorum_api_hook("admin_editfolder_form", $frm, $forum_settings);
  197. $frm->show();
  198. ?>
  199. <script type="text/javascript">
  200. //<![CDATA[
  201. // Handle changes to the setting inheritance select list.
  202. $PJ('select[name=inherit_id]').change(function updateInheritedFields()
  203. {
  204. var inherit = $PJ('select[name=inherit_id]').val();
  205. // No inheritance. All fields will be made read/write.
  206. if (inherit == -1) {
  207. updateInheritedSettings(null);
  208. }
  209. // An inheritance option is selected. Retrieve the settings for
  210. // the selection option and update the form with those. All
  211. // inherited settings are made read only.
  212. else {
  213. Phorum.call({
  214. call: 'getforumsettings',
  215. forum_id: inherit,
  216. cache_id: 'forum_settings_' + inherit,
  217. onSuccess: function (data) {
  218. updateInheritedSettings(data);
  219. },
  220. onFailure: function (err) {
  221. alert("Could not retrieve inherited settings: " + err);
  222. }
  223. });
  224. }
  225. });
  226. function updateInheritedSettings(data)
  227. {
  228. // Find the settings form.
  229. $PJ('input.input-form-submit').parents('form').each(function (idx, frm) {
  230. // Loop over all form fields.
  231. $PJ(frm).find('input[type!=hidden],textarea,select')
  232. .each(function (idx, f) {
  233. $f = $PJ(f);
  234. // Skip the form submit button.
  235. if ($f.hasClass('input-form-submit')) return;
  236. // SKip fields that are not inherited.
  237. if (f.name == 'name' ||
  238. f.name == 'description' ||
  239. f.name == 'parent_id' ||
  240. f.name == 'active' ||
  241. f.name == 'vroot' ||
  242. f.name == 'inherit_id') return;
  243. // When no data is provided, then we make the field read/write.
  244. if (!data)
  245. {
  246. $PJ(f).removeAttr('disabled');
  247. }
  248. // Data is provided. Fill the default value and make the
  249. // field read only.
  250. else
  251. {
  252. // Some browsers will not update the field when it
  253. // is disabled. Therefor, we temporarily enable it here.
  254. $PJ(f).removeAttr('disabled');
  255. if (data[f.name] !== undefined) {
  256. $f.val(data[f.name]);
  257. }
  258. $PJ(f).attr('disabled', 'disabled');
  259. }
  260. });
  261. });
  262. }
  263. // ]]>
  264. </script>