move code up one directory
[atutor.git] / mods / _standard / links / tools / categories_edit.php
1 <?php\r
2 /****************************************************************************/\r
3 /* ATutor                                                                                                                                       */\r
4 /****************************************************************************/\r
5 /* Copyright (c) 2002-2010                                                  */\r
6 /* Inclusive Design Institute                                               */\r
7 /* http://atutor.ca                                                                                                                     */\r
8 /*                                                                                                                                                      */\r
9 /* This program is free software. You can redistribute it and/or                        */\r
10 /* modify it under the terms of the GNU General Public License                          */\r
11 /* as published by the Free Software Foundation.                                                        */\r
12 /****************************************************************************/\r
13 // $Id$\r
14 \r
15 define('AT_INCLUDE_PATH', '../../../../include/');\r
16 require(AT_INCLUDE_PATH.'vitals.inc.php');\r
17 require(AT_INCLUDE_PATH.'../mods/_standard/links/lib/links.inc.php');\r
18 \r
19 if (!manage_links()) {\r
20         $msg->addError('ACCESS_DENIED');\r
21         header('Location: '.AT_BASE_HREF.'mods/_standard/links/index.php');\r
22         exit;\r
23 }\r
24 \r
25 $cat_id = intval($_REQUEST['cat_id']);\r
26 \r
27 if (isset($_POST['submit'])) {\r
28 \r
29         //check if cat name is empty\r
30         if ($_POST['cat_name'] == '') {\r
31                 $msg->addError(array('EMPTY_FIELDS', _AT('title')));\r
32         }\r
33 \r
34         if (!$msg->containsErrors()) {\r
35                 //authorized cat parent?\r
36                 $lid = explode('-', $_POST['cat_parent_id']);\r
37                 $parent_id = intval($lid[0]);\r
38                 $owner_type = intval($lid[1]);\r
39                 $owner_id = intval($lid[2]);\r
40 \r
41                 if (!links_authenticate($owner_type, $owner_id)) {\r
42                         $msg->addError('ACCESS_DENIED');\r
43                         header('Location: '.AT_BASE_HREF.'mods/_standard/links/tools/categories.php');\r
44                         exit;\r
45                 }\r
46 \r
47                 $cat_name = $addslashes($_POST['cat_name']);\r
48                 \r
49                 //Check length of the post, if it's exceeded 100 as defined in the db. \r
50                 $cat_name = validate_length($cat_name, 100);\r
51 \r
52                 $sql = "UPDATE ".TABLE_PREFIX."links_categories SET parent_id=$parent_id, name='$cat_name', owner_type=$owner_type, owner_id=$owner_id WHERE cat_id=".$cat_id;\r
53 \r
54                 $result = mysql_query($sql, $db);\r
55                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
56                 header('Location: categories.php');\r
57                 exit;\r
58         }\r
59 } else if (isset($_POST['cancel'])) {\r
60         $msg->addFeedback('CANCELLED');\r
61         header('Location: categories.php');\r
62         exit;\r
63 } else {\r
64         $row = get_cat_info($cat_id);\r
65 \r
66         //authorized to edit this cat?\r
67         if (!links_authenticate($row['owner_type'], $row['owner_id'])) {\r
68                 $msg->addError('ACCESS_DENIED');\r
69                 header('Location: '.AT_BASE_HREF.'mods/_standard/links/tools/categories.php');\r
70                 exit;\r
71         }\r
72 }\r
73 \r
74 /* get all the categories: */\r
75 /* $categories[category_id] = array(cat_name, cat_parent, num_courses, [array(children)]) */\r
76 $categories = get_link_categories(true);\r
77 \r
78 $onload = 'document.form.category_name.focus();';\r
79 \r
80 require(AT_INCLUDE_PATH.'header.inc.php');\r
81 \r
82 $current_cat_id = $categories[$cat_id]['cat_parent'];\r
83 \r
84 ?>\r
85 <form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">\r
86 <input type="hidden" name="cat_id" value="<?php echo $cat_id; ?>" />\r
87 <input type="hidden" name="form_submit" value="1" />\r
88 \r
89 <div class="input-form">\r
90         <div class="row">\r
91                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="category_name"><?php echo _AT('title'); ?></label><br />\r
92                 <input type="text" id="category_name" name="cat_name" value="<?php echo stripslashes(htmlspecialchars($categories[$cat_id]['cat_name'])); ?>" />\r
93         </div>\r
94 \r
95         <div class="row">\r
96                 <label for="category_parent"><?php echo _AT('cats_parent_category'); ?></label><br />\r
97                 <select name="cat_parent_id" id="category_parent"><?php\r
98                                 $exclude = true; /* exclude the children */\r
99                                 \r
100                                 //remove the current cat_id and it's sub cats from list, don't want to print them out.\r
101                                 if (is_array($categories[$current_cat_id]['children'])) {\r
102                                         foreach ($categories[$current_cat_id]['children'] as $id=>$child) {\r
103                                                 if ($child == $cat_id) {\r
104                                                         unset($categories[$current_cat_id]['children'][$id]);\r
105                                                 }\r
106                                         }\r
107                                 }\r
108                                 unset($categories[$cat_id]);\r
109 \r
110                                 $auth = manage_links();\r
111                                 if ($auth == LINK_CAT_AUTH_ALL) {\r
112                                         echo '<option value="0-'.LINK_CAT_COURSE.'-'.$_SESSION['course_id'].'">&nbsp;&nbsp;&nbsp;[ '._AT('cats_none').' ]&nbsp;&nbsp;&nbsp;</option>';\r
113                                         echo '<option value="0-'.LINK_CAT_COURSE.'-'.$_SESSION['course_id'].'"></option>';\r
114                                 }\r
115                                 select_link_categories($categories, 0, $current_cat_id, $exclude, 0, TRUE);\r
116                         ?></select>\r
117         </div>\r
118         <div class="row buttons">\r
119                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />\r
120                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>"  />\r
121         </div>\r
122 </div>\r
123 </form>\r
124 \r
125 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>