made a copy
[atutor.git] / editor / edit_content.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
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
17 global $db;
18
19 $get_related_glossary = true;
20 require(AT_INCLUDE_PATH.'vitals.inc.php');
21 require(AT_INCLUDE_PATH.'lib/tinymce.inc.php');
22 require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
23
24 $cid = intval($_REQUEST['cid']);
25
26 if ($_POST) {
27         $do_check = TRUE;
28 } else {
29         $do_check = FALSE;
30 }
31
32
33 require(AT_INCLUDE_PATH.'lib/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: '.AT_BASE_HREF.'tools/content/index.php');
42                         exit;
43                 }
44         }
45         
46         if ($_REQUEST['cid'] == 0) {
47                 header('Location: '.$_base_path.'content.php?cid='.intval($_REQUEST['new_pid']));
48                 exit;
49         }
50         header('Location: '.$_base_path.url_rewrite('content.php?cid='.intval($_REQUEST['cid'])));
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 }\r
77 \r
78 if (isset($_POST['submit_file_alt'])) {\r
79         paste_from_file(body_text_alt);\r
80 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {\r
81         /* we're saving. redirects if successful. */\r
82         save_changes(true, $current_tab);\r
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) && (($_POST['desc_submit'] != '') || ($_POST['reverse'] != ''))) {
93         $current_tab = 4;  /* after clicking 'make decisions' on accessibility tab */
94 } else if (!isset($current_tab)) {
95         $current_tab = 0;
96 }
97
98 if ($cid) {
99         $_section[0][0] = _AT('edit_content');
100 } else {
101         $_section[0][0] = _AT('add_content');
102 }
103
104 if ($cid) {
105         $result = $contentManager->getContentPage($cid);
106
107         if (!($content_row = @mysql_fetch_assoc($result))) {
108                 require(AT_INCLUDE_PATH.'header.inc.php');
109                 $msg->printErrors('PAGE_NOT_FOUND');
110                 require (AT_INCLUDE_PATH.'footer.inc.php');
111                 exit;
112         }
113
114         $path   = $contentManager->getContentPath($cid);
115         $content_test = $contentManager->getContentTestsAssoc($cid);
116
117         if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
118                 $course_base_href = 'get.php/';
119         } else {
120                 $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
121         }
122
123         if ($content_row['content_path']) {
124                 $content_base_href .= $content_row['content_path'].'/';
125         }
126 } else {
127         if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
128                 $content_base_href = 'get.php/';
129         } else {
130                 $content_base_href = 'content/' . $_SESSION['course_id'] . '/';
131         }
132 }
133
134 if ($current_tab == 4) {
135         /* kludge for issue #1626: */
136         /* fixes the base href for the AChecker tab. */
137         $course_base_href = '';
138         $content_base_href = '';
139 }
140
141 //Added by Silvia
142
143 // tools/filemanager/top.php
144
145 if (!$_GET['f']) {
146         $_SESSION['done'] = 0;
147 }
148 if (!authenticate(AT_PRIV_FILES,AT_PRIV_RETURN)) {
149         authenticate(AT_PRIV_CONTENT);
150 }
151
152 $current_path = AT_CONTENT_DIR.$_SESSION['course_id'].'/';
153
154 $MakeDirOn = true;
155
156 /* get this courses MaxQuota and MaxFileSize: */
157 $sql    = "SELECT max_quota, max_file_size FROM ".TABLE_PREFIX."courses WHERE course_id=$_SESSION[course_id]";
158 $result = mysql_query($sql, $db);
159 $row    = mysql_fetch_array($result);
160 $my_MaxCourseSize       = $row['max_quota'];
161 $my_MaxFileSize         = $row['max_file_size'];
162
163 if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
164         $my_MaxCourseSize = $MaxCourseSize;
165 }
166 if ($my_MaxFileSize == AT_FILESIZE_DEFAULT) {
167         $my_MaxFileSize = $MaxFileSize;
168 } else if ($my_MaxFileSize == AT_FILESIZE_SYSTEM_MAX) {
169         $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
170 }
171
172 $MaxSubDirs  = 5;
173 $MaxDirDepth = 10;
174
175 if ($_GET['pathext'] != '') {
176         $pathext = urldecode($_GET['pathext']);
177 } else if ($_POST['pathext'] != '') {
178         $pathext = $_POST['pathext'];
179 }
180
181 if (strpos($pathext, '..') !== false) {
182         require(AT_INCLUDE_PATH.'header.inc.php');
183         $msg->printErrors('UNKNOWN');   
184         require(AT_INCLUDE_PATH.'footer.inc.php');
185         exit;
186 }
187 if($_GET['back'] == 1) {
188         $pathext  = substr($pathext, 0, -1);
189         $slashpos = strrpos($pathext, '/');
190         if($slashpos == 0) {
191                 $pathext = '';
192         } else {
193                 $pathext = substr($pathext, 0, ($slashpos+1));
194         }
195
196 }
197
198 $start_at = 2;
199 /* remove the forward or backwards slash from the path */
200 $newpath = $current_path;
201 $depth = substr_count($pathext, '/');
202
203 if ($pathext != '') {
204         $bits = explode('/', $pathext);
205         foreach ($bits as $bit) {
206                 if ($bit != '') {
207                         $bit_path .= $bit;
208
209                         $_section[$start_at][0] = $bit;
210                         $_section[$start_at][1] = '../tools/filemanager/index.php?pathext=' . urlencode($bit_path) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed;
211
212                         $start_at++;
213                 }
214         }
215         $bit_path = "";
216         $bit = "";
217 }
218
219 /* if upload successful, close the window */
220 if ($f) {
221         $onload = 'closeWindow(\'progWin\');';
222 }
223
224 /* make new directory */
225 if (isset($_POST['mkdir'])) {
226 if ($_POST['mkdir_value'] && ($depth < $MaxDirDepth) ) {
227         $_POST['dirname'] = trim($_POST['dirname']);
228
229         /* anything else should be okay, since we're on *nix..hopefully */
230         $_POST['dirname'] = ereg_replace('[^a-zA-Z0-9._]', '', $_POST['dirname']);
231
232         if ($_POST['dirname'] == '') {
233                 $msg->addError(array('FOLDER_NOT_CREATED', $_POST['dirname'] ));
234         } 
235         else if (strpos($_POST['dirname'], '..') !== false) {
236                 $msg->addError('BAD_FOLDER_NAME');
237         }       
238         else {
239                 $result = @mkdir($current_path.$pathext.$_POST['dirname'], 0700);
240                 if($result == 0) {
241                         $msg->addError(array('FOLDER_NOT_CREATED', $_POST['dirname'] ));
242                 }
243                 else {
244                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
245                 }
246         }
247 }
248 }
249 $newpath = substr($current_path.$pathext, 0, -1);
250
251
252 /* open the directory */
253 if (!($dir = @opendir($newpath))) {
254         if (isset($_GET['create']) && ($newpath.'/' == $current_path)) {
255                 @mkdir($newpath);
256                 if (!($dir = @opendir($newpath))) {
257                         require(AT_INCLUDE_PATH.'header.inc.php');
258                         $msg->printErrors('CANNOT_CREATE_DIR');                 
259                         require(AT_INCLUDE_PATH.'footer.inc.php');
260                         exit;
261                 } else {
262                         $msg->addFeedback('CONTENT_DIR_CREATED');
263                 }
264         } else {
265                 require(AT_INCLUDE_PATH.'header.inc.php');
266
267                 $msg->printErrors('CANNOT_OPEN_DIR');
268                 require(AT_INCLUDE_PATH.'footer.inc.php');
269                 exit;
270         }
271 }
272 /*
273 if (isset($_POST['cancel'])) {
274         $msg->addFeedback('CANCELLED');
275 }*/
276 //end top.inc.php
277
278 // upload.php
279 $_SESSION['done'] = 1;
280 $popup = $_REQUEST['popup'];
281 $framed = $_REQUEST['framed'];
282
283 /* get this courses MaxQuota and MaxFileSize: */
284 $sql    = "SELECT max_quota, max_file_size FROM ".TABLE_PREFIX."courses WHERE course_id=$_SESSION[course_id]";
285 $result = mysql_query($sql, $db);
286 $row    = mysql_fetch_array($result);
287 $my_MaxCourseSize       = $row['max_quota'];
288 $my_MaxFileSize = $row['max_file_size'];
289
290         if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
291                 $my_MaxCourseSize = $MaxCourseSize;
292         }
293         if ($my_MaxFileSize == AT_FILESIZE_DEFAULT) {
294                 $my_MaxFileSize = $MaxFileSize;
295         } else if ($my_MaxFileSize == AT_FILESIZE_SYSTEM_MAX) {
296                 $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
297         }
298
299 $path = AT_CONTENT_DIR . $_SESSION['course_id'].'/'.$_POST['pathext'];
300
301 if (isset($_POST['upload'])) {
302
303         if($_FILES['uploadedfile']['name'])     {
304 //              echo 'il file esiste';
305                 $_FILES['uploadedfile']['name'] = trim($_FILES['uploadedfile']['name']);
306                 $_FILES['uploadedfile']['name'] = str_replace(' ', '_', $_FILES['uploadedfile']['name']);
307
308                 $path_parts = pathinfo($_FILES['uploadedfile']['name']);
309                 $ext = $path_parts['extension'];
310
311                 /* check if this file extension is allowed: */
312                 /* $IllegalExtentions is defined in ./include/config.inc.php */
313                 if (in_array($ext, $IllegalExtentions)) {
314                         $errors = array('FILE_ILLEGAL', $ext);
315                         $msg->addError($errors);
316                         header('Location: index.php?pathext='.$_POST['pathext']);
317                         exit;
318                 }
319
320                 /* also have to handle the 'application/x-zip-compressed'  case */
321                 if (   ($_FILES['uploadedfile']['type'] == 'application/x-zip-compressed')
322                         || ($_FILES['uploadedfile']['type'] == 'application/zip')
323                         || ($_FILES['uploadedfile']['type'] == 'application/x-zip')){
324                         $is_zip = true;                                         
325                 }
326
327         
328                 /* anything else should be okay, since we're on *nix.. hopefully */
329                 $_FILES['uploadedfile']['name'] = str_replace(array(' ', '/', '\\', ':', '*', '?', '"', '<', '>', '|', '\''), '', $_FILES['uploadedfile']['name']);
330
331
332                 /* if the file size is within allowed limits */
333                 if( ($_FILES['uploadedfile']['size'] > 0) && ($_FILES['uploadedfile']['size'] <= $my_MaxFileSize) ) {
334
335                         /* if adding the file will not exceed the maximum allowed total */
336                         $course_total = dirsize($path);
337
338                         if ((($course_total + $_FILES['uploadedfile']['size']) <= ($my_MaxCourseSize + $MaxCourseFloat)) || ($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED)) {
339
340                                 /* check if this file exists first */
341                                 if (file_exists($path.$_FILES['uploadedfile']['name'])) {
342                                         /* this file already exists, so we want to prompt for override */
343
344                                         /* save it somewhere else, temporarily first                    */
345                                         /* file_name.time ? */
346                                         $_FILES['uploadedfile']['name'] = substr(time(), -4).'.'.$_FILES['uploadedfile']['name'];
347
348                                         $f = array('FILE_EXISTS',
349                                                                         substr($_FILES['uploadedfile']['name'], 5), 
350                                                                         $_FILES['uploadedfile']['name'],
351                                                                         $_POST['pathext'],
352                                                                         $_GET['popup'],
353                                                                         SEP);
354                                         $msg->addFeedback($f);
355                                 }
356
357                                 /* copy the file in the directory */
358                                 $result = move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $path.$_FILES['uploadedfile']['name'] );
359
360                                 if (!$result) {
361                                         require(AT_INCLUDE_PATH.'header.inc.php');
362                                         $msg->printErrors('FILE_NOT_SAVED');
363                                         echo '<a href="tools/filemanager/index.php?pathext=' . $_POST['pathext'] . SEP . 'popup=' . $_GET['popup'] . '">' . _AT('back') . '</a>';
364                                         require(AT_INCLUDE_PATH.'footer.inc.php');
365                                         exit;
366                                 } else {
367                                         if ($is_zip) {
368                                                 $f = array('FILE_UPLOADED_ZIP',
369                                                                                 urlencode($_POST['pathext']), 
370                                                                                 urlencode($_FILES['uploadedfile']['name']), 
371                                                                                 $_GET['popup'],
372                                                                                 SEP);
373                                                 //$msg->addFeedback($f);
374                 
375                                         } /* else */
376
377                                         $msg->addFeedback('FILE_UPLOADED');
378                                 }
379                         } else {
380                                 $msg->addError(array('MAX_STORAGE_EXCEEDED', get_human_size($my_MaxCourseSize)));
381                         }
382                 } else {
383                         $msg->addError(array('FILE_TOO_BIG', get_human_size($my_MaxFileSize)));
384                 }
385         } else {
386                 $msg->addError('FILE_NOT_SELECTED');
387         }
388 }
389
390 // end upload.php       
391
392
393 if ($current_tab == 5){
394         if ($_POST['alternatives']==1){
395                 if (isset($_POST['add'])){
396                         if (isset($_POST['resources'])){
397                                 if (isset($_POST['radio_alt'])){
398                                 
399                                         $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid' and primary_resource_id='$_POST[resources]'";
400                                 $result = mysql_query($sql, $db);
401                         
402                                 if (mysql_num_rows($result) > 0) {
403                                         while ($row = mysql_fetch_assoc($result)) {
404                                                 $sql_contr      = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]' and secondary_resource='$_POST[radio_alt]'";
405                                                 $contr          = mysql_query($sql_contr, $db);
406                                                 if (mysql_num_rows($contr) > 0) {
407                                                         $msg->addError('ALTERNATIVE_ALREADY_DECLARED');
408                                                 }
409                                                 else {
410                                                         $sql_ins = "INSERT INTO ".TABLE_PREFIX."secondary_resources VALUES (NULL, '$row[primary_resource_id]', '$_POST[radio_alt]', 'en')";
411                                                                 $ins     = mysql_query($sql_ins, $db);
412                                                                 $msg->addFeedback('ALTERNATIVE_ADDED');
413                                                         }
414                                                 }
415                                         }
416                         }
417                                 else 
418                                         $msg->addError('ALTERNATIVE_NOT_DEFINED');
419                         }
420                         else 
421                                 $msg->addError('RESOURCE_NOT_DEFINED');
422                         }
423                 }
424
425                 
426         if ($_REQUEST[act]=='delete')   {
427                 $sql = "DELETE FROM ".TABLE_PREFIX."secondary_resources WHERE secondary_resource_id='$_REQUEST[id_alt]'";
428                 $result = mysql_query($sql,$db);
429                 $sql = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$_REQUEST[id_alt]'";
430                 $result = mysql_query($sql,$db);
431                 
432                 $msg->addFeedback('ALTERNATIVE_DELETED');
433         }
434
435 }
436
437
438 if ($current_tab == 5) {
439         if (isset($_POST['save_types_and_language'])){
440                 if(($_POST['alternatives']==1) || ($_GET['alternatives']==1)){
441                         $sql    = "SELECT primary_resource_id FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid'";
442                 $result = mysql_query($sql, $db);
443
444                 if (mysql_num_rows($result) > 0) {
445                                 while ($row = mysql_fetch_assoc($result)) {
446                                         $sql_type        = "SELECT * FROM ".TABLE_PREFIX."resource_types";
447                                 $result_type = mysql_query($sql_type, $db);
448                                 
449                                 if (mysql_num_rows($result_type) > 0) {
450                                                 while ($type = mysql_fetch_assoc($result_type)) {
451                                                         $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$row[primary_resource_id]' and type_id='$type[type_id]'";
452                                                         $contr          = mysql_query($sql_contr, $db);    
453                                                         if (mysql_num_rows($contr) > 0) {
454                                                                 while ($control = mysql_fetch_assoc($contr)) {
455                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary']))
456                                                                                 continue;
457                                                                         else {
458                                                                                 $sql_del = "DELETE FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$control[primary_resource_id]' and type_id='$control[type_id]'";
459                                                                                 $result_del = mysql_query($sql_del, $db);
460                                                                         }
461                                                                 }       
462                                                         }
463                                                         else {
464                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary'])){
465                                                                         $sql_ins        = "INSERT INTO ".TABLE_PREFIX."primary_resources_types VALUES ($row[primary_resource_id], $type[type_id])";
466                                                                         $ins            = mysql_query($sql_ins, $db);
467                                                                         }       
468                                                                 
469                                                                 $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]'";
470                                                         $result_alt = mysql_query($sql_alt, $db);
471                                                 
472                                                                 if (mysql_num_rows($result_alt) > 0) {
473                                                                 while ($alt = mysql_fetch_assoc($result_alt)) {
474                                                                                 $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$alt[secondary_resource_id]' and type_id='$type[type_id]'";
475                                                                                 $contr  = mysql_query($sql_contr, $db);    
476                                                                                 if (mysql_num_rows($contr) > 0) {
477                                                                                         while ($control = mysql_fetch_assoc($contr)) {
478                                                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary']))
479                                                                                                         continue;
480                                                                                                 else {
481                                                                                                         $sql_del = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$control[secondary_resource_id]' and type_id='$control[type_id]'";
482                                                                                                         $result_del = mysql_query($sql_del, $db);
483                                                                                                         }
484                                                                                                 }               
485                                                                                         }
486                                                                                 else {
487                                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary'])){
488                                                                                                 $sql_ins        = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types VALUES ($alt[secondary_resource_id], $type[type_id])";
489                                                                                                 $ins            = mysql_query($sql_ins, $db);
490                                                                                                 }
491                                                                                         $lang=$_POST['lang_'.$alt[secondary_resource_id].'_secondary'];
492                                                                                         $sql_up = "UPDATE ".TABLE_PREFIX."secondary_resources SET language_code='$lang' WHERE secondary_resource_id=$alt[secondary_resource_id]";
493                                                                                         $up     = mysql_query($sql_up, $db);
494
495       
496                                                                                 }       
497                                                                         }                       
498                                                                 }
499                                                 
500                                                                 $lang=$_POST['lang_'.$row[primary_resource_id].'_primary'];
501                                                                 $sql_up = "UPDATE ".TABLE_PREFIX."primary_resources SET language_code='$lang' WHERE primary_resource_id=$row[primary_resource_id]";
502                                                                 $up     = mysql_query($sql_up, $db);
503                                                                 /* Added feedback message after types and language are updated Greg Oct 19/08  
504
505                                                                   */
506                                                                  $msg->addFeedback('RESOURCE_PROPERTIES_UPDATED');
507                                                         }
508                                                 }
509                                         }
510                                 }
511                 }
512                 }
513                 else {
514                         if (isset($_POST['body_text_alt'])){
515                                 $cid_wholepage = $cid.'_wholepage';
516                                 $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid' and resource='$cid_wholepage'";
517                         $result = mysql_query($sql, $db);
518                                 if (mysql_num_rows($result) > 0){
519                                 while ($row = mysql_fetch_assoc($result)) {
520                                         $sql_type        = "SELECT * FROM ".TABLE_PREFIX."resource_types";
521                                                 $result_type = mysql_query($sql_type, $db);
522                                         
523                                         if (mysql_num_rows($result_type) > 0) {
524                                                         while ($type = mysql_fetch_assoc($result_type)) {
525                                                                 $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$row[primary_resource_id]' and type_id='$type[type_id]'";
526                                                                 $contr          = mysql_query($sql_contr, $db);    
527                                                                 if (mysql_num_rows($contr) > 0) {
528                                                                         while ($control = mysql_fetch_assoc($contr)) {
529                                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary']))
530                                                                                         continue;
531                                                                                 else {
532                                                                                         $sql_del = "DELETE FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$control[primary_resource_id]' and type_id='$control[type_id]'";
533                                                                                         $result_del = mysql_query($sql_del, $db);
534                                                                                 }
535                                                                         }       
536                                                                 } else {
537                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary'])){
538                                                                                 $sql_ins        = "INSERT INTO ".TABLE_PREFIX."primary_resources_types VALUES ($row[primary_resource_id], $type[type_id])";
539                                                                                 $ins            = mysql_query($sql_ins, $db);
540                                                                         }
541                                                                 }       
542                                                                 if ($_POST['body_text_alt'] != $_POST['body_text']){
543                                                                 $sql_check_secondary    = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]'";
544                                                                 $check_secondary                = mysql_query($sql_check_secondary, $db);
545                                                                 if (mysql_num_rows($check_secondary) > 0) {
546                                                                         while ($secondary = mysql_fetch_assoc($check_secondary)) {
547                                                                                 $sql_up = "UPDATE ".TABLE_PREFIX."secondary_resources SET secondary_resource='$_POST[body_text_alt]' WHERE secondary_resource_id=$secondary[secondary_resource_id]";
548                                                                                         $up     = mysql_query($sql_up, $db);
549                                                                                         $msg->addFeedback('ALTERNATIVE_UPDATED');
550                                                                         }       
551                                                                 }else {
552                                                                         $sql_ins = "INSERT INTO ".TABLE_PREFIX."secondary_resources VALUES (NULL, '$row[primary_resource_id]', '$_POST[body_text_alt]', 'en')";
553                                                                                 $ins     = mysql_query($sql_ins, $db);
554                                                                                 $msg->addFeedback('ALTERNATIVE_ADDED');
555                                                                         }
556                                                                         $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]'";
557                                                                 $result_alt = mysql_query($sql_alt, $db);
558                                                 
559                                                                         if (mysql_num_rows($result_alt) > 0) {
560                                                                         while ($alt = mysql_fetch_assoc($result_alt)) {
561                                                                                         $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$alt[secondary_resource_id]' and type_id='$type[type_id]'";
562                                                                                         $contr  = mysql_query($sql_contr, $db);    
563                                                                                         if (mysql_num_rows($contr) > 0) {
564                                                                                                 while ($control = mysql_fetch_assoc($contr)) {
565                                                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary']))
566                                                                                                                 continue;
567                                                                                                         else {
568                                                                                                                 $sql_del = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$control[secondary_resource_id]' and type_id='$control[type_id]'";
569                                                                                                                 $result_del = mysql_query($sql_del, $db);
570                                                                                                         }
571                                                                                                 }               
572                                                                                         } else {
573                                                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary'])){
574                                                                                                         $sql_ins        = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types VALUES ($alt[secondary_resource_id], $type[type_id])";
575                                                                                                         $ins            = mysql_query($sql_ins, $db);
576                                                                                                 }
577                                                                                                 $lang   = $_POST['lang_'.$alt[secondary_resource_id].'_secondary'];
578                                                                                                 $sql_up = "UPDATE ".TABLE_PREFIX."secondary_resources SET language_code='$lang' WHERE secondary_resource_id='$alt[secondary_resource_id]'";
579                                                                                                 $up     = mysql_query($sql_up, $db);
580
581                                                                                         }
582                                                                                 $lang=$_POST['lang_'.$row[primary_resource_id].'_primary'];
583                                                                                         $sql_up = "UPDATE ".TABLE_PREFIX."primary_resources SET language_code='$lang' WHERE primary_resource_id='$row[primary_resource_id]'";
584                                                                                         $up     = mysql_query($sql_up, $db);
585
586                                                                                 }
587                                                                         }
588                                                                 }
589                                                         }
590                                                 }
591                                         }
592                                 }
593                         }
594             }
595         }
596 }               
597
598 //End Added by Silvia 
599
600 //Extended Test within content functionality - Harris @Sep 9, 2008
601 //if ($current_tab == 6){
602 //}
603
604 if (($current_tab == 0) || ($_current_tab == 5)) {
605         if (!isset($_REQUEST['setvisual']) && !isset($_REQUEST['settext'])) {
606                 if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 1) {
607                         $_POST['formatting'] = 1;
608                         $_REQUEST['settext'] = 0;
609                         $_REQUEST['setvisual'] = 0;
610
611                 } else if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 2) {
612                         $_POST['formatting'] = 1;
613                         $_POST['settext'] = 0;
614                         $_POST['setvisual'] = 1;
615
616                 } else { // else if == 0
617                         $_POST['formatting'] = 0;
618                         $_REQUEST['settext'] = 0;
619                         $_REQUEST['setvisual'] = 0;
620                 }
621         }
622         if ((!$_POST['setvisual'] && $_POST['settext']) || !$_GET['setvisual']){
623                 $onload = ' document.form.ctitle.focus(); ';
624         }
625 }
626
627 // initialize buttons, texts, radio buttons for editor
628 if ($current_tab == 0) 
629 {
630         $onload.="on_load();";
631 }
632
633 if ($current_tab == 5) 
634 {
635         $onload.="on_load();";
636 }
637
638 require(AT_INCLUDE_PATH.'header.inc.php');
639
640 if ($current_tab == 0) 
641 {
642         load_editor(false, "none");
643 }
644
645 if ($current_tab == 5) 
646 {
647         load_editor(false, "none");
648 }
649
650 $cid = intval($_REQUEST['cid']);
651 $pid = intval($_REQUEST['pid']);
652
653 ?>
654 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?cid=<?php echo $cid; ?>" method="post" name="form" enctype="multipart/form-data">
655 <?php
656
657         if ($cid) {
658                 //$content_row = sql_quote($content_row);
659                 if (isset($_POST['current_tab'])) {
660                         //$changes_made = check_for_changes($content_row);
661                 } else {
662                         $changes_made = array();
663
664                         $_POST['formatting'] = $content_row['formatting'];
665                         $_POST['head'] = $content_row['head'];
666                         $_POST['use_customized_head'] = $content_row['use_customized_head'];
667                         $_POST['title']      = $content_row['title'];
668                         $_POST['body_text']  = $content_row['text'];
669                         $_POST['keywords']   = $content_row['keywords'];
670                         $_POST['test_message'] = $content_row['test_message'];
671                         $_POST['allow_test_export'] = $content_row['allow_test_export'];
672
673                         $_POST['day']   = substr($content_row['release_date'], 8, 2);
674                         $_POST['month'] = substr($content_row['release_date'], 5, 2);
675                         $_POST['year']  = substr($content_row['release_date'], 0, 4);
676                         $_POST['hour']  = substr($content_row['release_date'], 11, 2);
677                         $_POST['minute']= substr($content_row['release_date'], 14, 2);
678
679                         $_POST['ordering'] = $_POST['new_ordering'] = $content_row['ordering'];
680                         $_POST['related'] = $contentManager->getRelatedContent($cid);
681
682                         $_POST['pid'] = $pid = $_POST['new_pid'] = $content_row['content_parent_id'];
683
684                         $_POST['related_term'] = $glossary_ids_related;
685                 }
686
687         } else {
688                 $cid = 0;
689                 if (!isset($_POST['current_tab'])) {
690                         $_POST['day']  = date('d');
691                         $_POST['month']  = date('m');
692                         $_POST['year'] = date('Y');
693                         $_POST['hour'] = date('H');
694                         $_POST['minute']  = 0;
695
696                         if (isset($_GET['pid'])) {
697                                 $pid = intval($_GET['pid']);
698                                 $_POST['pid'] = 0;
699                                 $_POST['new_pid'] = $pid;
700                                 $_POST['ordering'] = count($contentManager->getContent(0))+1;
701                                 $_POST['new_ordering'] = count($contentManager->getContent($pid))+1;
702                         } else {
703                                 $_POST['pid'] = $_POST['new_pid'] = 0;
704                                 $_POST['ordering'] = $_POST['new_ordering'] = count($contentManager->getContent($pid))+1;
705                         }
706                         $pid = 0;
707                 }
708                 //$changes_made = check_for_changes($content_row);
709         }
710 //      echo $alternatives;
711 //      echo '<input type="hidden" name="alternatives" value="$alternatives" />';
712         echo '<input type="hidden" name="cid" value="'.$cid.'" />';
713         echo '<input type="hidden" name="title" value="'.htmlspecialchars($stripslashes($_POST['title'])).'" />';
714         echo '<input type="submit" name="submit" style="display:none;"/>';
715         if (($current_tab != 0) && (($_current_tab != 5))) {
716                 echo '<input type="hidden" name="body_text" value="'.htmlspecialchars($stripslashes($_POST['body_text'])).'" />';
717                 echo '<input type="hidden" name="head" value="'.htmlspecialchars($stripslashes($_POST['head'])).'" />';
718                 echo '<input type="hidden" name="use_customized_head" value="'.(($_POST['use_customized_head']=="") ? 0 : $_POST['use_customized_head']).'" />';
719                 echo '<input type="hidden" name="displayhead" value="'.$_POST['displayhead'].'" />';
720                 echo '<input type="hidden" name="setvisual" value="'.$_POST['setvisual'].'" />';
721                 echo '<input type="hidden" name="settext" value="'.$_POST['settext'].'" />';            
722                 echo '<input type="hidden" name="formatting" value="'.$_POST['formatting'].'" />';
723         }
724         if ($current_tab != 1) {
725                 echo '<input type="hidden" name="new_ordering" value="'.$_POST['new_ordering'].'" />';
726                 echo '<input type="hidden" name="new_pid" value="'.$_POST['new_pid'].'" />';
727         }
728
729         echo '<input type="hidden" name="ordering" value="'.$_POST['ordering'].'" />';
730         echo '<input type="hidden" name="pid" value="'.$pid.'" />';
731
732         echo '<input type="hidden" name="day" value="'.$_POST['day'].'" />';
733         echo '<input type="hidden" name="month" value="'.$_POST['month'].'" />';
734         echo '<input type="hidden" name="year" value="'.$_POST['year'].'" />';
735         echo '<input type="hidden" name="hour" value="'.$_POST['hour'].'" />';
736         echo '<input type="hidden" name="minute" value="'.$_POST['minute'].'" />';
737
738         echo '<input type="hidden" name="alternatives" value="'.$_POST['alternatives'].'" />';
739         
740         echo '<input type="hidden" name="current_tab" value="'.$current_tab.'" />';
741
742         if (is_array($_POST['related']) && ($current_tab != 1)) {
743                 foreach($_POST['related'] as $r_id) {
744                         echo '<input type="hidden" name="related[]" value="'.$r_id.'" />';
745                 }
746         }
747
748         echo '<input type="hidden" name="keywords" value="'.htmlspecialchars(stripslashes($_POST['keywords'])).'" />';
749
750         //content test association
751         echo '<input type="hidden" name="test_message" value="'.$_POST['test_message'].'" />';
752         
753         /* get glossary terms */
754         $matches = find_terms(stripslashes($_POST['body_text']));
755         $num_terms = count($matches[0]);
756         $matches = $matches[0];
757         $word = str_replace(array('[?]', '[/?]'), '', $matches);
758
759         if (is_array($word)) {
760                 /* update $_POST['glossary_defs'] with any new/changed terms */
761                 for($i=0; $i<$num_terms; $i++) {
762                         $word[$i] = urlencode($word[$i]);
763                         if (!isset($_POST['glossary_defs'][$word[$i]])) {
764                                 $_POST['glossary_defs'][$word[$i]] = $glossary[$word[$i]];
765                         }
766                 }
767         }
768
769         if (is_array($_POST['glossary_defs']) && ($current_tab != 2)) {
770                 foreach($_POST['glossary_defs'] as $w => $d) {
771                         /* this term still exists in the content */
772                         if (!in_array($w, $word)) {
773                                 unset($_POST['glossary_defs'][$w]);
774                                 continue;
775                         }
776                         echo '<input type="hidden" name="glossary_defs['.$w.']" value="'.htmlspecialchars(stripslashes($d)).'" />';
777                 }
778                 if (isset($_POST['related_term'])) {
779                         foreach($_POST['related_term'] as $w => $d) {
780                                 echo '<input type="hidden" name="related_term['.$w.']" value="'.$d.'" />';
781                         }
782                 }
783         }
784
785         //tests
786         if (is_array($_POST['tid']) && $current_tab != 6){
787                 /* Test & Survey --> Other tabs triggers this condition */
788                 foreach ($_POST['tid'] as $i=>$tid){
789                         echo '<input type="hidden" name="tid['.$i.']" value="'.$tid.'" />';
790                 }
791         } elseif ($current_tab != 6){
792                 /* Edit Content (On Edit content tab), without clicking Test & Survey */
793                 $i = 0;
794                 if ($content_test){
795                         while ($content_test_row = mysql_fetch_assoc($content_test)){
796                                 echo '<input type="hidden" name="tid['.$i++.']" value="'.$content_test_row['test_id'].'" />';
797                         }
798                 }
799         }
800         if (!isset($_POST['allow_test_export']) && $current_tab != 6) {
801                 //export flag handling.
802                 $sql = "SELECT `allow_test_export` FROM ".TABLE_PREFIX."content WHERE content_id=$_REQUEST[cid]";
803                 $result2 = mysql_query($sql, $db);
804                 if ($result2){
805                         $c_row = mysql_fetch_assoc($result2);
806                 }
807                 if (intval($c_row['allow_test_export'])==1){
808                         echo '<input type="hidden" name="allow_test_export" value="1" />';
809                 } else {
810                         echo '<input type="hidden" name="allow_test_export" value="0" />';
811                 }
812         } else {
813                 echo '<input type="hidden" name="allow_test_export" value="'.intval($_POST['allow_test_export']).'" />';
814         }
815
816         if ($do_check) {
817                 $changes_made = check_for_changes($content_row);
818         }
819
820 ?>
821 <div align="center">
822         <?php output_tabs($current_tab, $changes_made); ?>
823 </div>
824 <div class="input-form" style="width: 95%">
825
826         <?php if ($changes_made): ?>
827                 <div class="unsaved">
828                         <span style="color:red;"><?php echo _AT('save_changes_unsaved'); ?></span> 
829                         <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;" /> 
830                         <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"'; } ?> />
831                         <label for="close"><?php echo _AT('close_after_saving'); ?></label>
832                 </div>
833
834         <?php else: ?>
835                 <div class="saved">
836                         <?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" /> <input type="submit" name="close" value="<?php echo _AT('close'); ?>" /> <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>
837                 </div>
838         <?php endif; ?>
839         <?php 
840         //      if ($current_tab != 5){
841                                 include(AT_INCLUDE_PATH.'html/editor_tabs/'.$tabs[$current_tab][1]);
842                                 echo '</div></form>';
843         //      }
844         //      else 
845         //      {
846         //              echo '</div></form>';
847         //              include(AT_INCLUDE_PATH.'html/editor_tabs/'.$tabs[$current_tab][1]);
848                         
849         //      }
850                         
851 ?>
852
853 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>