7fe20dbeb45c81dc3eb409920030d4254ee67841
[acontent.git] / docs / home / editor / editor_tabs / alternatives.inc.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 creates the interface of "edit content" => "adapted content"
15  */
16
17 if (!defined('TR_INCLUDE_PATH')) { exit; }
18 include_once(TR_INCLUDE_PATH.'classes/DAO/ResourceTypesDAO.class.php');
19 include_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
20
21 $dao = new DAO();
22 $resourceTypesDAO = new ResourceTypesDAO();
23
24 global $_content_id, $content_row, $msg;
25 $cid = $_content_id;
26
27 if ($cid == 0) {
28         $msg->printErrors('SAVE_BEFORE_PROCEED');
29         require_once(TR_INCLUDE_PATH.'footer.inc.php');
30         exit;
31 }
32
33 /**
34  * When the file name is a remote URL, this function reduces the full URL
35  * @param  $filename
36  * @return the reduced name
37  */
38 function get_display_filename($filename)
39 {
40         if (substr($filename, 0 , 7) == 'http://' || substr($filename, 0 , 8) == 'https://') {
41                 if (substr($filename, 0 , 7) == 'http://') $prefix = 'http://';
42                 if (substr($filename, 0 , 8) == 'https://') $prefix = 'https://';
43                 $name = substr($filename, strrpos($filename, '/'));
44                 $filename = $prefix.'...'.$name;
45         }
46         return $filename;
47 }
48
49 /**
50  * Display alternative table cell
51  * @param $secondary_result   mysql result of all secondary alternatives
52  *        $alternative type   the resource type of the alternative to display. Must be one of the values in resource_types.type_id
53  *        $content_id         used to pass into file_manager/index.php
54  *        $pid                primary resource id
55  *        $td_header_id       the id of the table header cell, to comply with accessibility rule
56  * @return html of the table cell "<td>...</td>"
57  */ 
58 function display_alternative_cell($secondary_resources, $alternative_type, $content_id, $pid, $td_header_id)
59 {
60         global $content_row, $_course_id;
61         
62         $found_alternative = false;
63         
64         echo '    <td headers="'.$td_header_id.'">'."\n";
65         
66         if (is_array($secondary_resources))
67         {
68 //              mysql_data_seek($secondary_result, 0);  // move the mysql result cursor back to the first row
69                 foreach ($secondary_resources as $secondary_resource)
70                 {
71                         if ($secondary_resource['type_id'] == $alternative_type)
72                         {
73                                 echo '    <div id="'.$pid.'_'.$alternative_type.'">'."\n";
74                                 echo '      <a href="'.$secondary_resource['secondary_resource'].'" title="'._AT('new_window').'" target="_new">'.get_display_filename($secondary_resource['secondary_resource']).'</a><br />'."\n";
75                                 echo '      <a href="#" onclick="trans.utility.poptastic(\''.TR_BASE_HREF.'file_manager/index.php?framed=1'. SEP.'popup=1'. SEP.'cp='. $content_row['content_path'].SEP.'_cid='.$content_id.SEP.'pid='.$pid.SEP.'a_type='.$alternative_type.'\');return false;" title="'._AT('new_window').'">'."\n";
76                                 echo '        <img src="'.TR_BASE_HREF.'images/alter.png" border="0" title="'._AT('alter').'" alt="'._AT('alter').'" />'."\n";
77                                 echo '      </a>'."\n";
78                                 echo '      <a href="#" onclick="removeAlternative(\''.$content_row['content_path'].'\', '.$content_id.','.$pid.','.$alternative_type.');return false;">'."\n";
79                                 echo '        <img src="'.TR_BASE_HREF.'images/remove.gif" border="0" title="'._AT('remove').'" alt="'._AT('remove').'" />'."\n";
80                                 echo '      </a>'."\n";
81                                 echo '    </div>'."\n";
82                                 $found_alternative = true;
83                                 break;
84                         }
85                 }
86         }
87         if (!$found_alternative)
88         {
89                 echo '    <div id="'.$pid.'_'.$alternative_type.'">'."\n";
90                 echo '      <input type="button" value="'._AT('add').'" title="'._AT('new_window').'" onclick="trans.utility.poptastic(\''.TR_BASE_HREF.'file_manager/index.php?framed=1'. SEP.'popup=1'. SEP.'cp='. $content_row['content_path'].SEP.'_cid='.$content_id.SEP.'pid='.$pid.SEP.'a_type='.$alternative_type.'\');return false;" />'."\n";
91                 echo '    </div>'."\n";
92         }
93         echo '    </td>'."\n";
94 }
95
96 // Main program
97 global $db, $content_row;
98 populate_a4a($cid, $_POST['body_text'], $_POST['formatting']);
99
100 include_once(TR_INCLUDE_PATH.'classes/A4a/A4a.class.php');
101
102 $a4a = new A4a($cid);
103 $primary_resources = $a4a->getPrimaryResources();
104
105 if (count($primary_resources)==0)
106 {
107         echo '<p>'. _AT('No_resources'). '</p>';
108 }
109 else
110 {
111         $is_post_indicator_set = false;
112         // get all resource types
113 //      $sql = "SELECT * FROM ".TABLE_PREFIX."resource_types";
114 //      $resource_types_result = mysql_query($sql, $db);
115         $resource_types = $resourceTypesDAO->getAll();
116         
117         echo '<br /><table class="data" rules="all">'."\n";
118         echo '  <thead>'."\n";
119         echo '  <tr>'."\n";
120         echo '    <th rowspan="2" id="header1">'._AT('original_resource').'</th>'."\n";
121         echo '    <th rowspan="2" id="header2">'._AT('resource_type').'</th>'."\n";
122         echo '    <th colspan="4">'._AT('alternatives').'</th>'."\n";
123         echo '  </tr>'."\n";
124         echo '  <tr>'."\n";
125         echo '    <th id="header3">'._AT('text').'</th>'."\n";
126         echo '    <th id="header4">'._AT('audio').'</th>'."\n";
127         echo '    <th id="header5">'._AT('visual').'</th>'."\n";
128         echo '    <th id="header6">'._AT('sign_lang').'</th>'."\n";
129         echo '  </tr>'."\n";
130         echo '  </thead>'."\n";
131         
132         echo '  <tbody>';
133         foreach($primary_resources as $primary_resource_id => $primary_resource_row)
134         {
135                 $primary_resource = $primary_resource_row['resource'];
136                 
137                 $sql = "SELECT prt.type_id, rt.type
138                           FROM ".TABLE_PREFIX."primary_resources pr, ".
139                                  TABLE_PREFIX."primary_resources_types prt, ".
140                                  TABLE_PREFIX."resource_types rt
141                          WHERE pr.content_id = ".$cid."
142                            AND pr.language_code = '".$_SESSION['lang']."'
143                            AND pr.primary_resource_id='".$primary_resource_id."'
144                            AND pr.primary_resource_id = prt.primary_resource_id
145                            AND prt.type_id = rt.type_id";
146 //              $primary_type_result = mysql_query($sql, $db);
147                 $primary_types = $dao->execute($sql);
148                 
149                 if (!$is_post_indicator_set)
150                 {
151                         echo '  <input type="hidden" name="use_post_for_alt" value="1" />'."\n";
152                         $is_post_indicator_set = true;
153                 }
154                 
155                 // get secondary resources for the current primary resource
156                 $sql = "SELECT pr.primary_resource_id, sr.secondary_resource, srt.type_id
157                           FROM ".TABLE_PREFIX."primary_resources pr, ".
158                                  TABLE_PREFIX."secondary_resources sr, ".
159                                  TABLE_PREFIX."secondary_resources_types srt
160                          WHERE pr.content_id = ".$cid."
161                            AND pr.language_code = '".$_SESSION['lang']."'
162                            AND pr.primary_resource_id='".$primary_resource_id."'
163                            AND pr.primary_resource_id = sr.primary_resource_id
164                            AND sr.secondary_resource_id = srt.secondary_resource_id";
165 //              $secondary_result = mysql_query($sql, $db);
166                 $secondary_resources = $dao->execute($sql);
167                 
168                 echo '  <tr>'."\n";
169
170                 // table cell "original resource"
171                 echo '    <td headers="header1">'."\n";
172                 echo '    <a href="'.$primary_resource.'" title="'._AT('new_window').'" target="_new">'.get_display_filename($primary_resource).'</a>'."\n";
173                 echo '    </td>'."\n";
174
175                 // table cell "original resource type"
176                 echo '    <td headers="header2">'."\n";
177                 
178 //              mysql_data_seek($resource_types_result, 0);  // move the mysql result cursor back to the first row
179 //              while ($resource_type = mysql_fetch_assoc($resource_types_result))
180                 if (is_array($resource_types))
181                 {
182                         foreach ($resource_types as $resource_type) {
183                                 if ($resource_type['type'] == 'sign_language')
184                                         continue;
185                                 else 
186                                 {
187                                         echo '<input type="checkbox" name="alt_'.$primary_resource_id.'_'.$resource_type['type_id'].'" value="1" id="alt_'.$primary_resource_id.'_'.$resource_type['type_id'].'"';
188                                         if ($_POST['use_post_for_alt'])
189                                         {
190                                                 if (isset($_POST['alt_'.$primary_resource_id.'_'.$resource_type['type_id']])) {
191                                                         echo 'checked="checked"';
192                                                 }
193                                         }
194                                         else {
195                                                 if (is_array($primary_types)) {
196                                                         foreach ($primary_types as $primary_resource_type) {
197                                                                 if ($primary_resource_type['type_id'] == $resource_type['type_id']){
198                                                                         echo 'checked="checked"';
199                                                                         break;
200                                                                 }
201                                                         }
202                                                 }
203                                         }
204                                         echo '/>'."\n";
205                                         echo '<label for="alt_'.$primary_resource_id.'_'.$resource_type['type_id'].'">'. _AT($resource_type['type']).'</label><br/>'."\n";      
206                                 }
207                         }
208                 }
209                 echo '    </td>'."\n";
210                 
211                 // table cell "text alternative"
212                 display_alternative_cell($secondary_resources, 3, $cid, $primary_resource_id, "header3");
213                 
214                 // table cell "audio"
215                 display_alternative_cell($secondary_resources, 1, $cid, $primary_resource_id, "header4");
216                 
217                 // table cell "visual"
218                 display_alternative_cell($secondary_resources, 4, $cid, $primary_resource_id, "header5");
219                 
220                 // table cell "sign language"
221                 display_alternative_cell($secondary_resources, 2, $cid, $primary_resource_id, "header6");
222                 
223                 echo '  </tr>'."\n";
224         }
225         echo '  </tbody>'."\n";
226         echo '</table><br /><br />'."\n";
227 }
228 ?>
229
230 <script type="text/javascript">
231 //<!--
232 // This function does:
233 // 1. save the removal into db via ajax
234 // 2. set the according field to "add" button
235 function removeAlternative(contentPath, cid, pid, a_type) 
236 {
237         jQuery.post("<?php echo TR_BASE_HREF; ?>home/editor/remove_alternative.php", 
238                         {"pid":pid, "a_type":a_type}, 
239                         function(data) {});
240
241         var button_html = '      <input type="button" value="<?php echo _AT('add'); ?>" title="<?php echo _AT('new_window'); ?>" onclick="trans.utility.poptastic(\\\'<?php echo TR_BASE_HREF; ?>file_manager/index.php?framed=1<?php echo SEP; ?>popup=1<?php echo SEP; ?>cp='+contentPath+'<?php echo SEP; ?>_cid='+cid+'<?php echo SEP; ?>pid='+pid+'<?php echo SEP; ?>a_type='+a_type+'\\\');return false;" />';
242         eval("document.getElementById(\""+pid+"_"+a_type+"\").innerHTML = '"+button_html+"'");
243 }
244 //-->
245 </script>