remove old readme
[atutor.git] / docs / mods / _standard / links / tools / add.php
1 <?php
2 /****************************************************************************/
3 /* ATutor                                                                                                                                       */
4 /****************************************************************************/
5 /* Copyright (c) 2002-2010                                                  */
6 /* Inclusive Design Institute                                               */
7 /* http://atutor.ca                                                                                                                     */
8 /*                                                                                                                                                      */
9 /* This program is free software. You can redistribute it and/or                        */
10 /* modify it under the terms of the GNU General Public License                          */
11 /* as published by the Free Software Foundation.                                                        */
12 /****************************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../../../../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 require (AT_INCLUDE_PATH.'../mods/_standard/links/lib/links.inc.php');
18
19 if (!manage_links()) {
20         $msg->addError('ACCESS_DENIED');
21         header('Location: '.AT_BASE_HREF.'mods/_standard/links/index.php');
22         exit;
23 }
24
25 if (!isset($_POST['approved'])) {
26         $_POST['approved'] = 1;
27 }
28
29 if (isset($_POST['cancel'])) {
30         $msg->addFeedback('CANCELLED');
31         header('Location: '.AT_BASE_HREF.'mods/_standard/links/tools/index.php');
32         exit;
33
34
35 if (isset($_POST['add_link']) && isset($_POST['submit'])) {
36
37         //check category_id and see if user is allowed..
38         $cat = explode('-', $_POST['cat']);
39         $cat_id = intval($cat[0]);
40         $owner_type = intval($cat[1]);
41         $owner_id = intval($cat[2]);
42
43         if (!links_authenticate($owner_type, $owner_id)) {
44                 $msg->addError('ACCESS_DENIED');
45                 header('Location: '.AT_BASE_HREF.'mods/_standard/links/index.php');
46                 exit;
47         }
48
49         $missing_fields = array();
50         if ($_POST['cat'] == 0 || $_POST['cat'] == '') {
51                 $missing_fields[] = _AT('category');
52         }
53         if (trim($_POST['title']) == '') {
54                 $missing_fields[] = _AT('title');
55         }
56         if (trim($_POST['url']) == '' || $_POST['url'] == 'http://') {
57                 $missing_fields[] = _AT('url');
58         }
59         if (trim($_POST['description']) == '') {
60                 $missing_fields[] = _AT('description');
61         }
62
63         if ($missing_fields) {
64                 $missing_fields = implode(', ', $missing_fields);
65                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
66         }
67
68         if (!$msg->containsErrors() && isset($_POST['submit'])) {
69                 $_POST['title']  = $addslashes($_POST['title']);
70                 $_POST['url'] == $addslashes($_POST['url']);
71                 $_POST['description']  = $addslashes($_POST['description']);
72
73                 //Check length of the post, if it's exceeded 64 as defined in the db. 
74                 $_POST['title'] = validate_length($_POST['title'], 64);
75                 $_POST['description'] = validate_length($_POST['description'], 250);
76                 
77                 $name = get_display_name($_SESSION['member_id']);
78                 $email = '';
79
80                 $approved = intval($_POST['approved']);
81
82                 $sql = "INSERT INTO ".TABLE_PREFIX."links VALUES (NULL, $cat_id, '$_POST[url]', '$_POST[title]', '$_POST[description]', $approved, '$name', '$email', NOW(), 0)";
83                 mysql_query($sql, $db);
84         
85                 $msg->addFeedback('LINK_ADDED');
86
87                 header('Location: '.AT_BASE_HREF.'mods/_standard/links/tools/index.php');
88                 exit;
89         } else {
90                 $_POST['title']  = stripslashes($_POST['title']);
91                 $_POST['url'] == stripslashes($_POST['url']);
92                 $_POST['description']  = stripslashes($_POST['description']);
93         }
94 }
95
96 if (!isset($_POST['url'])) {
97         $_POST['url'] = 'http://';
98 }
99
100 $categories = get_link_categories(true);
101
102 if (empty($categories)) {
103         $msg->addError('LINK_CAT_EMPTY');
104         header('Location: '.AT_BASE_HREF.'mods/_standard/links/tools/index.php');
105         exit;
106 }
107
108 $onload = 'document.form.title.focus();';
109
110 require(AT_INCLUDE_PATH.'header.inc.php');
111
112 $msg->printErrors();
113
114 ?>
115 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
116 <input type="hidden" name="add_link" value="true" />
117
118 <div class="input-form">
119         <div class="row">
120                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="title"><?php echo _AT('title'); ?></label><br />
121                 <input type="text" name="title" size="40" id="title" value="<?php echo $_POST['title']; ?>"/>
122         </div>
123
124         <div class="row">
125                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="cat"><?php echo _AT('category'); ?></label><br />
126                 <select name="cat" id="cat"><?php
127                         if ($pcat_id) {
128                                 $current_cat_id = $pcat_id;
129                                 $exclude = false; /* don't exclude the children */
130                         } else {
131                                 $current_cat_id = $cat_id;
132                                 $exclude = true; /* exclude the children */
133                         }
134                         select_link_categories($categories, 0, $_POST['cat'], FALSE, '', TRUE);
135                         ?>
136                 </select>
137         </div>
138         
139         <div class="row">
140                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="url"><?php echo _AT('url'); ?></label><br />
141                 <input type="text" name="url" size="40" id="url" value="<?php echo AT_print($_POST['url'], 'input.text'); ?>" />
142         </div>
143
144         <div class="row">
145                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="description"><?php echo _AT('description'); ?></label><br />
146                 <textarea name="description" cols="55" rows="5" id="description" style="width:90%;"><?php echo AT_print($_POST['description'], 'input.text'); ?></textarea>
147         </div>
148
149         <div class="row">
150                 <?php echo _AT('approve'); ?><br />
151                 <?php
152                         if ($_POST['approved']) {
153                                 $y = 'checked="checked"';
154                                 $n = '';
155                         } else if (isset ($_POST['approved'])) {
156                                 $n = 'checked="checked"';
157                                 $y = '';
158                         } else {
159                                 $y = 'checked="checked"';
160                                 $n = '';
161                         }
162                 ?>
163                 <input type="radio" id="yes" name="approved" value="1" <?php echo $y; ?>><label for="yes"><?php echo _AT('yes'); ?></label>  <input type="radio" id="no" name="approved" value="0" <?php echo $n; ?>><label for="no"><?php echo _AT('no'); ?></label>
164         </div>
165         
166         <div class="row buttons">
167                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" accesskey="s" />
168                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?> " />
169         </div>
170 </div>
171 </form>
172
173 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>