AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / home / editor / edit_content.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 define('TR_INCLUDE_PATH', '../../include/');
14
15 global $associated_forum, $_course_id, $_content_id;
16
17 require(TR_INCLUDE_PATH.'vitals.inc.php');
18 require_once(TR_INCLUDE_PATH.'lib/tinymce.inc.php');
19 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
20 require_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
21
22 Utility::authenticate(TR_PRIV_ISAUTHOR);
23
24 $cid = $_content_id;
25 $dao = new DAO();
26
27 if ($_POST) {
28         $do_check = TRUE;
29 } else {
30         $do_check = FALSE;
31 }
32
33 require(TR_INCLUDE_PATH.'../home/editor/editor_tab_functions.inc.php');
34
35 if ($_POST['close'] || $_GET['close']) {
36         if ($_GET['close']) {
37                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
38         } else {
39                 $msg->addFeedback('CLOSED');
40                 if ($cid == 0) {
41                         header('Location: '.TR_BASE_HREF.'home/course/index.php?_course_id='.$_course_id);
42                         exit;
43                 }
44         }
45         
46         if (!isset($_content_id) || $_content_id == 0) {
47                 header('Location: '.TR_BASE_HREF.'home/course/index.php?_course_id='.$_course_id);
48                 exit;
49         }
50         header('Location: '.TR_BASE_HREF.'home/course/content.php?_cid='.$_content_id);
51         exit;
52 }
53         
54 $tabs = get_tabs();     
55 $num_tabs = count($tabs);
56 for ($i=0; $i < $num_tabs; $i++) {
57         if (isset($_POST['button_'.$i]) && ($_POST['button_'.$i] != -1)) { 
58                 $current_tab = $i;
59                 $_POST['current_tab'] = $i;
60                 break;
61         }
62 }
63
64 if (isset($_GET['tab'])) {
65         $current_tab = intval($_GET['tab']);
66 }
67 if (isset($_POST['current_tab'])) {
68         $current_tab = intval($_POST['current_tab']);
69 }
70
71 if (isset($_POST['submit_file'])) {
72         paste_from_file(body_text);
73 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
74         /* we're saving. redirects if successful. */
75         save_changes(true, $current_tab);
76 }
77
78 if (isset($_POST['submit_file_alt'])) {
79         paste_from_file(body_text_alt);
80 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
81         /* we're saving. redirects if successful. */
82         save_changes(true, $current_tab);
83 }
84
85 if (isset($_POST['submit'])) {
86         /* we're saving. redirects if successful. */
87         save_changes(true, $current_tab);
88 }
89
90 if (!isset($current_tab) && isset($_POST['button_1']) && ($_POST['button_1'] == -1) && !isset($_POST['submit'])) {
91         $current_tab = 1;
92 } else if (!isset($current_tab)) {
93         $current_tab = 0;
94 }
95
96 if ($cid) {
97         $_section[0][0] = _AT('edit_content');
98 } else {
99         $_section[0][0] = _AT('add_content');
100 }
101
102 if($current_tab == 0) {
103     $_custom_head .= '
104     <link rel="stylesheet" type="text/css" href="'.TR_BASE_HREF.'include/jscripts/infusion/framework/fss/css/fss-layout.css" />
105     <link rel="stylesheet" type="text/css" href="'.TR_BASE_HREF.'include/jscripts/infusion/framework/fss/css/fss-text.css" />
106     <script type="text/javascript" src="'.$_base_path.'home/editor/js/edit.js"></script>
107     ';
108 }
109
110 if ($cid) {
111         if (isset($contentManager)) $content_row = $contentManager->getContentPage($cid);
112
113         if (!$content_row || !isset($contentManager)) {
114                 require(TR_INCLUDE_PATH.'header.inc.php');
115                 $msg->printErrors('MISSING_CONTENT');
116                 require (TR_INCLUDE_PATH.'footer.inc.php');
117                 exit;
118         }
119
120         $path   = $contentManager->getContentPath($cid);
121         $content_tests = $contentManager->getContentTestsAssoc($cid);
122
123         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
124                 $course_base_href = 'get.php/';
125         } else {
126                 $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
127         }
128
129         if ($content_row['content_path']) {
130                 $content_base_href .= $content_row['content_path'].'/';
131         }
132 } else {
133         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
134                 $content_base_href = 'get.php/';
135         } else {
136                 $content_base_href = 'content/' . $_SESSION['course_id'] . '/';
137         }
138 }
139
140 if (($current_tab == 0) || ($current_tab == 2)) {
141     if ($_POST['formatting'] == null){ 
142         // this is a fresh load from just logged in
143             if (isset($_SESSION['prefs']['PREF_CONTENT_EDITOR']) && $_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 0) {
144                         $_POST['formatting'] = 0;
145                 } else {
146                         $_POST['formatting'] = 1;
147                 }
148     }
149 }
150
151 require(TR_INCLUDE_PATH.'header.inc.php');
152
153 if ($current_tab == 0 || $current_tab == 2) 
154 {
155     $simple = true;
156     if ($_POST['complexeditor'] == '1') {
157         $simple = false;
158     }
159     load_editor($simple, false, "none");    
160 }
161
162 //TODO*************BOLOGNA****************REMOVE ME**************/
163 //loading toolbar for insert discussion topic or web link into the content
164 //if ($current_tab == 0){
165 //    if(authenticate(TR_PRIV_CONTENT,TR_PRIV_RETURN)){
166 //        $home_links = get_home_navigation();                        //vengono lette le caratteristiche di ogni modulo attivato nella home page.
167 //        $main_links = get_main_navigation($current_page);           //vengono lette le caratteristiche di ogni modulo attivo nel main navigation
168 //
169 //        $num = count($main_links);                                  //necessario elminare il primo e l'utlimo elemento poichè sono rispettivamente "Home" e "Manage"
170 //        unset($main_links[0]);                                      //"Home" label
171 //        unset($main_links[$num-1]);                                 //"Manage" label
172 //
173 //        $all_tools = $home_links;                                   //$all_tools represent a merge between $home_links and main_links without repetitions.
174 //        $check=false;
175 //        foreach($main_links as $main) {
176 //            foreach($home_links as $home) {
177 //                if($home['title'] == $main['title']) {
178 //                    $check=true;
179 //                    break;
180 //                }
181 //            }
182 //            if(!$check)
183 //                $all_tools[]=$main;
184 //            else
185 //                $check=false;
186 //        }
187 //    }
188 //}
189
190 $pid = intval($_REQUEST['pid']);
191 ?>
192
193 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?_cid=<?php echo $cid; ?>" method="post" name="form" enctype="multipart/form-data">
194 <?php
195
196         if ($cid) {
197                 //$content_row = sql_quote($content_row);
198                 if (isset($_POST['current_tab'])) {
199                         //$changes_made = check_for_changes($content_row);
200                 } else {
201                         $changes_made = array();
202
203                         $_POST['formatting'] = $content_row['formatting'];
204                         $_POST['head'] = $content_row['head'];
205                         $_POST['use_customized_head'] = $content_row['use_customized_head'];
206                         $_POST['title']      = $content_row['title'];
207                         $_POST['body_text']  = $content_row['text'];
208                         $_POST['weblink_text'] = $content_row['text'];
209                         $_POST['keywords']   = $content_row['keywords'];
210                         $_POST['test_message'] = $content_row['test_message'];
211
212 //                      $_POST['day']   = substr($content_row['release_date'], 8, 2);
213 //                      $_POST['month'] = substr($content_row['release_date'], 5, 2);
214 //                      $_POST['year']  = substr($content_row['release_date'], 0, 4);
215 //                      $_POST['hour']  = substr($content_row['release_date'], 11, 2);
216 //                      $_POST['min']= substr($content_row['release_date'], 14, 2);
217
218                         $_POST['ordering'] = $content_row['ordering'];
219 //                      $_POST['related'] = $contentManager->getRelatedContent($cid);
220                         
221                         $_POST['pid'] = $pid = $content_row['content_parent_id'];
222
223 //                      $_POST['related_term'] = $glossary_ids_related;
224                 }
225
226         } else {
227                 $cid = 0;
228                 if (!isset($_POST['current_tab'])) {
229                         $_POST['day']  = date('d');
230                         $_POST['month']  = date('m');
231                         $_POST['year'] = date('Y');
232                         $_POST['hour'] = date('H');
233                         $_POST['min']  = 0;
234
235                         if (isset($_GET['pid'])) {
236                                 $pid = $_POST['pid'] = intval($_GET['pid']);
237                                 $_POST['ordering'] = count($contentManager->getContent($pid))+1;
238                         } else {
239                                 $_POST['pid'] = 0;
240                                 $_POST['ordering'] = count($contentManager->getContent(0))+1;
241                         }
242                 }
243         }
244         
245         echo '<input type="hidden" name="_course_id" value="'.$_course_id.'" />';
246         echo '<input type="hidden" name="_cid" value="'.$cid.'" />';
247         echo '<input type="hidden" name="title" value="'.htmlspecialchars($stripslashes($_POST['title'])).'" />';
248         if ($_REQUEST['sub'] == 1)
249         {
250                 echo '<input type="hidden" name="sub" value="1" />';
251                 echo '<input type="hidden" name="folder_title" value="'.htmlspecialchars($stripslashes($_POST['folder_title'])).'" />';
252         }
253         echo '<input type="submit" name="submit" style="display:none;"/>';
254         if (($current_tab != 0) && (($_current_tab != 2))) {
255         echo '<input type="hidden" name="body_text" value="'.htmlspecialchars($stripslashes($_POST['body_text'])).'" />';
256         echo '<input type="hidden" name="weblink_text" value="'.htmlspecialchars($stripslashes($_POST['weblink_text'])).'" />';
257         echo '<input type="hidden" name="head" value="'.htmlspecialchars($stripslashes($_POST['head'])).'" />';
258                 echo '<input type="hidden" name="use_customized_head" value="'.(($_POST['use_customized_head']=="") ? 0 : $_POST['use_customized_head']).'" />';
259         echo '<input type="hidden" name="displayhead" id="displayhead" value="'.AT_print($_POST['displayhead'], 'input.hidden').'" />';
260         echo '<input type="hidden" name="complexeditor" id="complexeditor" value="'.AT_print($_POST['complexeditor'], 'input.hidden').'" />';
261         echo '<input type="hidden" name="formatting" value="'.AT_print($_POST['formatting'], 'input.hidden').'" />';
262         }
263
264         echo '<input type="hidden" name="ordering" value="'.AT_print($_POST['ordering'], 'input.hidden').'" />';
265         echo '<input type="hidden" name="pid" value="'.$pid.'" />';
266
267 //      echo '<input type="hidden" name="day" value="'.$_POST['day'].'" />';
268 //      echo '<input type="hidden" name="month" value="'.$_POST['month'].'" />';
269 //      echo '<input type="hidden" name="year" value="'.$_POST['year'].'" />';
270 //      echo '<input type="hidden" name="hour" value="'.$_POST['hour'].'" />';
271 //      echo '<input type="hidden" name="minute" value="'.$_POST['minute'].'" />';
272 //      echo '<input type="hidden" name="min" value="'.$_POST['min'].'" />';
273         
274         echo '<input type="hidden" name="alternatives" value="'.AT_print($_POST['alternatives'], 'input.hidden').'" />';
275         
276         echo '<input type="hidden" name="current_tab" value="'.$current_tab.'" />';
277
278 //      if (is_array($_POST['related']) && ($current_tab != 1)) {
279 //              foreach($_POST['related'] as $r_id) {
280 //                      echo '<input type="hidden" name="related[]" value="'.$r_id.'" />';
281 //              }
282 //      }
283         echo '<input type="hidden" name="keywords" value="'.htmlspecialchars(stripslashes($_POST['keywords'])).'" />';
284
285         //content test association
286         echo '<input type="hidden" name="test_message" value="'.AT_print($_POST['test_message'], 'input.hidden').'" />';
287         
288         /* get glossary terms */
289 //      $matches = find_terms(stripslashes($_POST['body_text']));
290 //      $num_terms = count($matches[0]);
291 //      $matches = $matches[0];
292 //      $word = str_replace(array('[?]', '[/?]'), '', $matches);
293 //
294 //      if (is_array($word)) {
295 //              /* update $_POST['glossary_defs'] with any new/changed terms */
296 //              for($i=0; $i<$num_terms; $i++) {
297 //                      $word[$i] = $word[$i];
298 //                      if (!isset($_POST['glossary_defs'][$word[$i]])) {
299 //                              $_POST['glossary_defs'][$word[$i]] = $glossary[$word[$i]];
300 //                      }
301 //              }
302 //      }
303 //
304 //      if (is_array($_POST['glossary_defs']) && ($current_tab != 2)) {
305 //              foreach($_POST['glossary_defs'] as $w => $d) {
306 //                      /* this term still exists in the content */
307 //                      if (!in_array($w, $word)) {
308 //                              unset($_POST['glossary_defs'][$w]);
309 //                              continue;
310 //                      }
311 //                      echo '<input type="hidden" name="glossary_defs['.$w.']" value="'.htmlspecialchars(stripslashes($d)).'" />';
312 //              }
313 //              if (isset($_POST['related_term'])) {
314 //                      foreach($_POST['related_term'] as $w => $d) {
315 //                              echo '<input type="hidden" name="related_term['.$w.']" value="'.$d.'" />';
316 //                      }
317 //              }
318 //      }
319
320         // adapted content
321         $sql = "SELECT pr.primary_resource_id, prt.type_id
322                   FROM ".TABLE_PREFIX."primary_resources pr, ".
323                          TABLE_PREFIX."primary_resources_types prt
324                  WHERE pr.content_id = ".$cid."
325                    AND pr.language_code = '".$_SESSION['lang']."'
326                    AND pr.primary_resource_id = prt.primary_resource_id";
327 //      $all_types_result = mysql_query($sql, $db);
328         $types = $dao->execute($sql);
329         
330         $i = 0;
331         if (is_array($types)) {
332                 foreach ($types as $type) {
333                         $row_alternatives['alt_'.$type['primary_resource_id'].'_'.$type['type_id']] = 1;
334                 }
335         }
336         
337         if ($current_tab != 2 && isset($_POST['use_post_for_alt']))
338         {
339                 echo '<input type="hidden" name="use_post_for_alt" value="1" />';
340                 if (is_array($_POST)) {
341                         foreach ($_POST as $alt_id => $alt_value) {
342                                 if (substr($alt_id, 0 ,4) == 'alt_'){
343                                         echo '<input type="hidden" name="'.$alt_id.'" value="'.$alt_value.'" />';
344                                 }
345                         }
346                 }
347         }
348         
349         //tests
350         if ($current_tab != 3){
351                 // set content associated tests
352                 if (isset($_POST['visited_tests'])) {
353                         echo '<input type="hidden" name="visited_tests" value="1" />'."\n";
354                         if (is_array($_POST['tid'])) {
355                                 foreach ($_POST['tid'] as $i=>$tid){
356                                         echo '<input type="hidden" name="tid['.$i.']" value="'.AT_print($tid, 'input.hidden').'" />';
357                                 }
358                         }
359                 } else {
360                         $i = 0;
361                         if (is_array($content_tests)) {
362                                 foreach ($content_tests as $content_test_row) {
363                                         echo '<input type="hidden" name="tid['.$i++.']" value="'.$content_test_row['test_id'].'" />';
364                                 }
365                         }
366                 }
367                 
368 //              // set pre-tests
369 //              if (is_array($_POST['pre_tid'])) {
370 //                      foreach ($_POST['pre_tid'] as $i=>$pre_tid){
371 //                              echo '<input type="hidden" name="pre_tid['.$i.']" value="'.$pre_tid.'" />';
372 //                      }
373 //              }
374 //              else
375 //              {
376 //                      $i = 0;
377 //                      $sql = 'SELECT * FROM '.TABLE_PREFIX."content_prerequisites WHERE content_id=$cid AND type='".CONTENT_PRE_TEST."'";
378 //                      $pretests_result = mysql_query($sql, $db);
379 //                      while ($pretest_row = mysql_fetch_assoc($pretests_result)) {
380 //                                      echo '<input type="hidden" name="pre_tid['.$i++.']" value="'.$pretest_row['item_id'].'" />';
381 //                      }
382 //              }
383         } 
384         
385         if ($do_check) {
386                 $changes_made = check_for_changes($content_row, $row_alternatives);
387         }
388 ?>
389
390 <div align="center">
391         <?php output_tabs($current_tab, $changes_made); ?>
392 </div>
393
394 <div class="input-form" style="width: 95%;">
395
396         <?php if ($changes_made): ?>
397                 <div class="unsaved">
398                         <span style="color:red;"><?php echo _AT('save_changes_unsaved'); ?></span> 
399                         <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" title="<?php echo _AT('save_changes'); ?> alt-s" accesskey="s" style="border: 1px solid red;" /> 
400                         <input type="submit" name="close" class="button green" value="<?php echo _AT('close'); ?>" />  <input type="checkbox" id="close" name="save_n_close" value="1" <?php if ($_SESSION['save_n_close']) { echo 'checked="checked"'; } ?> />
401                         <label for="close"><?php echo _AT('close_after_saving'); ?></label>
402                 </div>
403
404         <?php else: ?>
405                 <div class="saved">
406                         <?php //if ($cid) { echo _AT('save_changes_saved'); } ?> <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" title="<?php echo _AT('save_changes'); ?> alt-s" accesskey="s" class="button"/> <input type="submit" name="close" value="<?php echo _AT('close'); ?>"  class="button"/> <input type="checkbox" style="border:0px;" id="close" name="save_n_close" value="1" <?php if ($_SESSION['save_n_close']) { echo 'checked="checked"'; } ?>/><label for="close"><?php echo _AT('close_after_saving'); ?></label>
407                 </div>
408         <?php endif; ?>
409         <?php include('editor_tabs/'.$tabs[$current_tab][1]); ?>
410 </div>
411 </form>
412
413 <?php require(TR_INCLUDE_PATH.'footer.inc.php'); ?>