AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / home / editor / save_alternative.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 /**
14  * This script handles the ajax post submit from "content editor" =? "adpated content"
15  * to save the selected alternative into database
16  * @see file_manager/filemanager_display.inc.php
17  * @var $_POST values: 
18  *      pid: primary resource id
19  *      a_type: alternative type, must be one of the values in resource_types.type_id
20  *      alternative: the location and name of the selected alternative
21  */
22
23 define('TR_INCLUDE_PATH', '../../include/');
24 require_once(TR_INCLUDE_PATH.'vitals.inc.php');
25
26 $pid = intval($_POST['pid']);
27 $type_id = intval($_POST['a_type']);
28 $secondary_resource = trim($_POST['alternative']);
29
30 // check post vars
31 if ($pid == 0 || $type_id == 0 || $secondary_resource == '') exit;
32
33 require_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
34 $dao = new DAO();
35
36 // delete the existing alternative for this (pid, a_type)
37 $sql = "SELECT sr.secondary_resource_id 
38           FROM ".TABLE_PREFIX."secondary_resources sr, ".TABLE_PREFIX."secondary_resources_types srt
39          WHERE sr.secondary_resource_id = srt.secondary_resource_id
40            AND sr.primary_resource_id = ".$pid."
41            AND sr.language_code = '".$_SESSION['lang']."'
42            AND srt.type_id=".$type_id;
43 //$existing_secondary_result = mysql_query($sql, $db);
44 $existing_secondary_rows = $dao->execute($sql);
45
46 if (is_array($existing_secondary_rows)) {
47         foreach ($existing_secondary_rows as $existing_secondary)
48         {
49                 $sql = "DELETE FROM ".TABLE_PREFIX."secondary_resources 
50                          WHERE secondary_resource_id = ".$existing_secondary['secondary_resource_id'];
51                 $dao->execute($sql);
52         
53                 $sql = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types 
54                          WHERE secondary_resource_id = ".$existing_secondary['secondary_resource_id']."
55                            AND type_id=".$type_id;
56                 $dao->execute($sql);
57         }
58 }
59
60 // insert new alternative
61 $sql = "INSERT INTO ".TABLE_PREFIX."secondary_resources (primary_resource_id, secondary_resource, language_code)
62         VALUES (".$pid.", '".mysql_real_escape_string($secondary_resource)."', '".$_SESSION['lang']."')";
63 $dao->execute($sql);
64 $secondary_resource_id = mysql_insert_id();
65
66 $sql = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types (secondary_resource_id, type_id)
67         VALUES (".$secondary_resource_id.", ".$type_id.")";
68 $dao->execute($sql);
69
70 exit;
71
72 ?>