includes/clientside/static/acl.js
changeset 679 80bf9b8fe6aa
parent 677 2a263b598a2b
child 694 43367c66d869
equal deleted inserted replaced
678:c5d4ce19e640 679:80bf9b8fe6aa
   536   
   536   
   537   document.getElementById(aclManagerID + '_next').style.display = 'inline';
   537   document.getElementById(aclManagerID + '_next').style.display = 'inline';
   538   
   538   
   539   html = '<h2>'+act_desc+'</h2>';
   539   html = '<h2>'+act_desc+'</h2>';
   540   html += '<p>' + $lang.get('acl_lbl_editwin_body', { target_type: target_type_t, target: target_name_t, scope_type: scope_type }) + '</p>';
   540   html += '<p>' + $lang.get('acl_lbl_editwin_body', { target_type: target_type_t, target: target_name_t, scope_type: scope_type }) + '</p>';
       
   541   
       
   542   // preset management
       
   543   var load_flags = 'href="#" onclick="aclShowPresetLoader(); return false;"';
       
   544   var save_flags = 'href="#" onclick="aclShowPresetSave(); return false;"';
       
   545   html += '<div style="float: right;">';
       
   546   html += $lang.get('acl_btn_edit_presets', { load_flags: load_flags, save_flags: save_flags });
       
   547   html += '</div>';
       
   548   html += '<div style="clear: both;"></div>';
       
   549   
   541   parser = new templateParser(data.template.acl_field_begin);
   550   parser = new templateParser(data.template.acl_field_begin);
   542   html += parser.run();
   551   html += parser.run();
   543   
   552   
   544   cls = 'row2';
   553   cls = 'row2';
   545   for(var i in data.acl_types)
   554   for(var i in data.acl_types)
   851       selections = new Object();
   860       selections = new Object();
   852       var dbg = '';
   861       var dbg = '';
   853       var warned_everyone = false;
   862       var warned_everyone = false;
   854       for(var i in aclPermList)
   863       for(var i in aclPermList)
   855       {
   864       {
   856         selections[aclPermList[i]] = getRadioState(form, aclPermList[i], [1, 2, 3, 4]);
   865         selections[aclPermList[i]] = getRadioState(form, aclPermList[i], ['i', 1, 2, 3, 4]);
   857         // If we're editing permissions for everyone on the entire site and the
   866         // If we're editing permissions for everyone on the entire site and the
   858         // admin selected to deny privileges, give a stern warning about it.
   867         // admin selected to deny privileges, give a stern warning about it.
   859         if ( selections[aclPermList[i]] == 1 && aclDataCache.target_type == 1 /* ACL_TYPE_GROUP */ && aclDataCache.target_id == 1 && !warned_everyone )
   868         if ( selections[aclPermList[i]] == 1 && aclDataCache.target_type == 1 /* ACL_TYPE_GROUP */ && aclDataCache.target_id == 1 && !warned_everyone )
   860         {
   869         {
   861           warned_everyone = true;
   870           warned_everyone = true;
  1058     div.innerHTML = rule.score_string;
  1067     div.innerHTML = rule.score_string;
  1059     main.appendChild(div);
  1068     main.appendChild(div);
  1060   }
  1069   }
  1061 }
  1070 }
  1062 
  1071 
       
  1072 function aclShowPresetLoader()
       
  1073 {
       
  1074   var prompt = miniPrompt(function(parent)
       
  1075     {
       
  1076       parent.innerHTML = '<img style="display: block; margin: 0 auto;" src="' + cdnPath + '/images/loading-big.gif" />';
       
  1077     });
       
  1078   var request = toJSONString({
       
  1079       mode: 'list_presets'
       
  1080     });
       
  1081   ajaxPost(stdAjaxPrefix + '&_mode=acljson', 'acl_params=' + ajaxEscape(request), function()
       
  1082     {
       
  1083       if ( ajax.readyState == 4 && ajax.status == 200 )
       
  1084       {
       
  1085         if ( !check_json_response(ajax.responseText) )
       
  1086         {
       
  1087           miniPromptDestroy(prompt);
       
  1088           return handle_invalid_json(ajax.responseText);
       
  1089         }
       
  1090         var response = parseJSON(ajax.responseText);
       
  1091         if ( response.mode == 'error' )
       
  1092         {
       
  1093           alert(response.error);
       
  1094           miniPromptDestroy(prompt);
       
  1095           return false;
       
  1096         }
       
  1097         prompt = prompt.firstChild.nextSibling;
       
  1098         prompt.style.textAlign = 'center';
       
  1099         prompt.innerHTML = '<h3>' + $lang.get('acl_lbl_preset_load_title') + '</h3>';
       
  1100         
       
  1101         if ( response.presets.length > 0 )
       
  1102         {
       
  1103           // selection box
       
  1104           var para = document.createElement('p');
       
  1105           var select = document.createElement('select');
       
  1106           
       
  1107           var option = document.createElement('option');
       
  1108           option.value = '0';
       
  1109           option.appendChild(document.createTextNode($lang.get('acl_lbl_preset_load')));
       
  1110           select.appendChild(option);
       
  1111           
       
  1112           for ( var i = 0; i < response.presets.length; i++ )
       
  1113           {
       
  1114             var preset = response.presets[i];
       
  1115             var option = document.createElement('option');
       
  1116             option.value = preset.rule_id;
       
  1117             option.preset_data = preset;
       
  1118             option.appendChild(document.createTextNode($lang.get(preset.preset_name)));
       
  1119             select.appendChild(option);
       
  1120           }
       
  1121           
       
  1122           para.appendChild(select);
       
  1123           prompt.appendChild(para);
       
  1124           
       
  1125           // buttons
       
  1126           var buttons = document.createElement('p');
       
  1127           
       
  1128           // load button
       
  1129           var btn_load = document.createElement('a');
       
  1130           btn_load.className = 'abutton abutton_green';
       
  1131           btn_load.style.fontWeight = 'bold';
       
  1132           btn_load.appendChild(document.createTextNode($lang.get('acl_btn_load_preset')));
       
  1133           btn_load.selectobj = select;
       
  1134           btn_load.onclick = function()
       
  1135           {
       
  1136             if ( this.selectobj.value == '0' )
       
  1137             {
       
  1138               alert($lang.get('acl_err_select_preset'));
       
  1139               return false;
       
  1140             }
       
  1141             // retrieve preset data
       
  1142             for ( var i = 0; i < this.selectobj.childNodes.length; i++ )
       
  1143             {
       
  1144               if ( this.selectobj.childNodes[i].tagName == 'OPTION' )
       
  1145               {
       
  1146                 var node = this.selectobj.childNodes[i];
       
  1147                 if ( node.value == this.selectobj.value )
       
  1148                 {
       
  1149                   aclSetRulesAbsolute(node.preset_data.rules);
       
  1150                   break;
       
  1151                 }
       
  1152               }
       
  1153             }
       
  1154             miniPromptDestroy(this);
       
  1155             return false;
       
  1156           }
       
  1157           btn_load.href = '#';
       
  1158           buttons.appendChild(btn_load);
       
  1159           
       
  1160           buttons.appendChild(document.createTextNode(' '));
       
  1161           
       
  1162           // cancel button
       
  1163           var btn_cancel = document.createElement('a');
       
  1164           btn_cancel.className = 'abutton';
       
  1165           btn_cancel.appendChild(document.createTextNode($lang.get('etc_cancel')));
       
  1166           btn_cancel.onclick = function()
       
  1167           {
       
  1168             miniPromptDestroy(this);
       
  1169             return false;
       
  1170           }
       
  1171           btn_cancel.href = '#';
       
  1172           buttons.appendChild(btn_cancel);
       
  1173           
       
  1174           prompt.appendChild(buttons);
       
  1175         }
       
  1176         else
       
  1177         {
       
  1178           // "no presets"
       
  1179           prompt.innerHTML += '<p>' + $lang.get('acl_msg_no_presets', { close_flags: 'href="#" onclick="miniPromptDestroy(this); return false;"' }) + '</p>';
       
  1180         }
       
  1181       }
       
  1182     });
       
  1183 }
       
  1184 
       
  1185 function aclSetRulesAbsolute(rules)
       
  1186 {
       
  1187   __aclSetAllRadios('i');
       
  1188   
       
  1189   var form = document.forms[aclManagerID + '_formobj'];
       
  1190   if (!form)
       
  1191   {
       
  1192     return false;
       
  1193   }
       
  1194   var inputs = form.getElementsByTagName('input');
       
  1195   var radios = new Array();
       
  1196   var dbg = '';
       
  1197   for(var i = 0; i < inputs.length; i++)
       
  1198   {
       
  1199     if(inputs[i].type == 'radio')
       
  1200       radios.push(inputs[i]);
       
  1201   }
       
  1202   for(var i in radios)
       
  1203   {
       
  1204     if ( typeof(rules[ radios[i]['name'] ]) == 'number' )
       
  1205     {
       
  1206       radios[i].checked = ( rules[radios[i]['name']] == radios[i].value );
       
  1207     }
       
  1208   }
       
  1209 }
       
  1210 
       
  1211 function aclShowPresetSave()
       
  1212 {
       
  1213   miniPrompt(function(parent)
       
  1214     {
       
  1215       parent.style.textAlign = 'center';
       
  1216       
       
  1217       parent.innerHTML = '<h3>' + $lang.get('acl_lbl_preset_save_title') + '</h3>';
       
  1218       var input = document.createElement('input');
       
  1219       input.id = aclManagerID + '_preset_save';
       
  1220       input.type = 'text';
       
  1221       input.size = '30';
       
  1222       input.onkeypress = function(e)
       
  1223       {
       
  1224         // javascript sucks. IE and several others throw myriad errors unless it's done this way.
       
  1225         if ( e )
       
  1226         if ( e.keyCode )
       
  1227         if ( e.keyCode == 13 )
       
  1228         {
       
  1229           if ( aclSavePreset() )
       
  1230           {
       
  1231             miniPromptDestroy(this);
       
  1232           }
       
  1233         }
       
  1234         else if ( e.keyCode == 27 )
       
  1235         {
       
  1236           miniPromptDestroy(this);
       
  1237         }
       
  1238       }
       
  1239       var para = document.createElement('p');
       
  1240       para.appendChild(input);
       
  1241       
       
  1242       parent.appendChild(para);
       
  1243       
       
  1244       // buttons
       
  1245       var buttons = document.createElement('p');
       
  1246       
       
  1247       // save button
       
  1248       var btn_save = document.createElement('a');
       
  1249       btn_save.className = 'abutton abutton_green';
       
  1250       btn_save.style.fontWeight = 'bold';
       
  1251       btn_save.appendChild(document.createTextNode($lang.get('acl_btn_save_preset')));
       
  1252       btn_save.selectobj = select;
       
  1253       btn_save.onclick = function()
       
  1254       {
       
  1255         if ( aclSavePreset() )
       
  1256         {
       
  1257           miniPromptDestroy(this);
       
  1258         }
       
  1259         return false;
       
  1260       }
       
  1261       btn_save.href = '#';
       
  1262       buttons.appendChild(btn_save);
       
  1263       
       
  1264       buttons.appendChild(document.createTextNode(' '));
       
  1265       
       
  1266       // cancel button
       
  1267       var btn_cancel = document.createElement('a');
       
  1268       btn_cancel.className = 'abutton';
       
  1269       btn_cancel.appendChild(document.createTextNode($lang.get('etc_cancel')));
       
  1270       btn_cancel.onclick = function()
       
  1271       {
       
  1272         miniPromptDestroy(this);
       
  1273         return false;
       
  1274       }
       
  1275       btn_cancel.href = '#';
       
  1276       buttons.appendChild(btn_cancel);
       
  1277       
       
  1278       parent.appendChild(buttons);
       
  1279       
       
  1280       var timeout = ( aclDisableTransitionFX ) ? 10 : 1000;
       
  1281       setTimeout(function()
       
  1282         {
       
  1283           input.focus();
       
  1284         }, timeout);
       
  1285     });
       
  1286 }
       
  1287 
       
  1288 function aclSavePreset()
       
  1289 {
       
  1290   var input = document.getElementById(aclManagerID + '_preset_save');
       
  1291   if ( trim(input.value) == '' )
       
  1292   {
       
  1293     alert($lang.get('acl_err_preset_name_empty'));
       
  1294     return false;
       
  1295   }
       
  1296   var form = document.forms[aclManagerID + '_formobj'], selections = {};
       
  1297   var dbg = '';
       
  1298   var warned_everyone = false;
       
  1299   for(var i in aclPermList)
       
  1300   {
       
  1301     selections[aclPermList[i]] = getRadioState(form, aclPermList[i], ['i', 1, 2, 3, 4]);
       
  1302     // If we're editing permissions for everyone on the entire site and the
       
  1303     // admin selected to deny privileges, give a stern warning about it.
       
  1304     if ( selections[aclPermList[i]] == 1 && aclDataCache.target_type == 1 /* ACL_TYPE_GROUP */ && aclDataCache.target_id == 1 && !warned_everyone )
       
  1305     {
       
  1306       warned_everyone = true;
       
  1307       if ( !confirm($lang.get('acl_msg_deny_everyone_confirm')) )
       
  1308       {
       
  1309         return false;
       
  1310       }
       
  1311     }
       
  1312     dbg += aclPermList[i] + ': ' + selections[aclPermList[i]] + "\n";
       
  1313     if(!selections[aclPermList[i]])
       
  1314     {
       
  1315       alert("Invalid return from getRadioState: "+i+": "+selections[i]+" ("+typeof(selections[i])+")");
       
  1316       return false;
       
  1317     }
       
  1318   }
       
  1319   
       
  1320   var packet = toJSONString({
       
  1321       mode: 'save_preset',
       
  1322       preset_name: input.value,
       
  1323       perms: selections
       
  1324     });
       
  1325   
       
  1326   var whitey = whiteOutElement(document.getElementById(aclManagerID));
       
  1327   
       
  1328   ajaxPost(stdAjaxPrefix + '&_mode=acljson', 'acl_params=' + ajaxEscape(packet), function()
       
  1329     {
       
  1330       if ( ajax.readyState == 4 && ajax.status == 200 )
       
  1331       {
       
  1332         if ( !check_json_response(ajax.responseText) )
       
  1333         {
       
  1334           whitey.parentNode.removeChild(whitey);
       
  1335           return handle_invalid_json(ajax.responseText);
       
  1336         }
       
  1337         var response = parseJSON(ajax.responseText);
       
  1338         if ( response.mode == 'error' )
       
  1339         {
       
  1340           whitey.parentNode.removeChild(whitey);
       
  1341           alert(response.error);
       
  1342           return false;
       
  1343         }
       
  1344         whiteOutReportSuccess(whitey);
       
  1345       }
       
  1346     });
       
  1347   
       
  1348   return true;
       
  1349 }
       
  1350 
  1063 function array_keys(obj)
  1351 function array_keys(obj)
  1064 {
  1352 {
  1065   keys = new Array();
  1353   keys = new Array();
  1066   for(var i in obj)
  1354   for(var i in obj)
  1067     keys.push(i);
  1355     keys.push(i);