PageRenderTime 38ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/sahana/inc/lib_misc.inc

#
PHP | 364 lines | 278 code | 42 blank | 44 comment | 54 complexity | eae92df687faee1ea14b0e37bc18ae3f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * The Sahana
  4. *
  5. * PHP version 4 and 5
  6. *
  7. * LICENSE: This source file is subject to LGPL license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.gnu.org/copyleft/lesser.html
  10. *
  11. * @author Sudheera R. Fernando <sudheera@opensource.lk>
  12. * @author Ravindra De Silva <ravindra@opensource.lk><ravi@computer.org>
  13. * @copyright Lanka Software Foundation - http://www.opensource.lk
  14. * @package framework
  15. * @subpackage rms
  16. * @tutorial
  17. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General
  18. * Public License (LGPL)
  19. */
  20. function _shn_html_print_alert($alert)
  21. {
  22. ?>
  23. <div class="alert"> <!-- TODO: change style -->
  24. <strong><?php print _("Alert") . " : $alert"?></strong>
  25. </div>
  26. <?php
  27. }
  28. function _shn_html_print_message($msg,$br = false)
  29. {
  30. $br_str = '';
  31. if ($br == true)
  32. $br_str = '<br />';
  33. ?>
  34. <div class="message">
  35. <strong><?php print $msg?></strong>
  36. <?php print $br_str ?>
  37. </div>
  38. <?php
  39. }
  40. /**
  41. * _shn_create_unique_database_field_value
  42. *
  43. * @param string $base
  44. * @param string $width
  45. * @param string $table
  46. * @param string $field
  47. * @access protected
  48. * @return string
  49. */
  50. function shn_create_unique_database_field_value($base,$width,$table,$field,$constraints=null){
  51. global $global;
  52. $db=$global["db"];
  53. $not_done=true;
  54. $retry_limit=10;
  55. $count=0;
  56. while (($not_done)and ($count<$retry_limit)){
  57. $uuid = explode(" ", $base);
  58. $uuid = substr($uuid[0],0,3);
  59. for($i=0;$i<$width;$i++){
  60. if(($num = rand(0,36)) < 27 )
  61. $uuid .= chr($num+97);
  62. else
  63. $uuid .= 36 - $num;
  64. }
  65. $q="select $field from $table where $field='$uuid'";
  66. $res=$db->Execute($q);
  67. if(($res==NULL)or($res->EOF==true)){
  68. $not_done=false;
  69. }
  70. $count++;
  71. }
  72. return $uuid;
  73. }
  74. function shn_reset_form_javascript($form,$reset_what){
  75. ?>
  76. <script type="text/javascript">
  77. function change_action(action){
  78. var x=document.getElementsByName("<?php echo $reset_what?>");
  79. if(action=="reset"){
  80. for (i=0; i<=(x[0].options.length); i++){
  81. x[0].options[0]=null;
  82. }
  83. results = db_str.split(",");
  84. for (i=0; i<results.length; i++){
  85. opt = document.createElement("option") ;
  86. opt.value = results[i] ;
  87. opt.text = results[i].replace(/[^A-Za-z]$/,"");
  88. x[0].options[i] = opt;
  89. }
  90. var remove=document.getElementsByName("removed");
  91. var add=document.getElementsByName("added");
  92. add[0].value=",";
  93. remove[0].value=",";
  94. }else{
  95. document.<?php echo $form?>.submit();
  96. return;
  97. }
  98. }
  99. </script>
  100. <?
  101. }
  102. function shn_add_remove_javascript($name,$db_arr,$sort=true)
  103. {
  104. $count=0;
  105. foreach ($db_arr as $x) {
  106. if($count==0){
  107. $db_str.=$x;
  108. }else{
  109. $db_str.=",".$x;
  110. }
  111. $count++;
  112. }
  113. $db_str.=",";
  114. ?>
  115. <script type="text/javascript">
  116. var db_str="<?php echo $db_str;?>";
  117. var sort=<?php if($sort==true) {echo "true";}else{ echo "false";}?>;
  118. // sort function - ascending (case-insensitive)
  119. function sortFuncAsc(record1, record2) {
  120. var value1 = record1.optText.toLowerCase();
  121. var value2 = record2.optText.toLowerCase();
  122. if (value1 > value2) return(1);
  123. if (value1 < value2) return(-1);
  124. return(0);
  125. }
  126. // sort function - descending (case-insensitive)
  127. function sortFuncDesc(record1, record2) {
  128. var value1 = record1.optText.toLowerCase();
  129. var value2 = record2.optText.toLowerCase();
  130. if (value1 > value2) return(-1);
  131. if (value1 < value2) return(1);
  132. return(0);
  133. }
  134. function sortSelect(selectToSort, ascendingOrder) {
  135. if (arguments.length == 1) ascendingOrder = true; // default to ascending sort
  136. // copy options into an array
  137. var myOptions = [];
  138. for (var loop=0; loop<selectToSort.options.length; loop++) {
  139. myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
  140. }
  141. // sort array
  142. if (ascendingOrder) {
  143. myOptions.sort(sortFuncAsc);
  144. } else {
  145. myOptions.sort(sortFuncDesc);
  146. }
  147. // copy sorted options from array back to select box
  148. selectToSort.options.length = 0;
  149. for (var loop=0; loop<myOptions.length; loop++) {
  150. var optObj = document.createElement('option');
  151. optObj.text = myOptions[loop].optText;
  152. optObj.value = myOptions[loop].optValue;
  153. selectToSort.options.add(optObj);
  154. }
  155. }
  156. // Removes leading whitespaces
  157. function LTrim( value ) {
  158. var re = /\s*((\S+\s*)*)/;
  159. return value.replace(re, "$1");
  160. }
  161. // Removes ending whitespaces
  162. function RTrim( value ) {
  163. var re = /((\s*\S+)*)\s*/;
  164. return value.replace(re, "$1");
  165. }
  166. // Removes leading and ending whitespaces
  167. function trim( value ) {
  168. return LTrim(RTrim(value));
  169. }
  170. function check_for_special_char(data)
  171. {
  172. var iChars = "!@#$%^&*()+=-[]\';,./{}|\":<>?";
  173. var is_valid = false;
  174. for (var i = 0; i < data.length; i++) {
  175. if (iChars.indexOf(data.charAt(i)) != -1){
  176. is_valid = true;
  177. }
  178. }
  179. return is_valid;
  180. }
  181. function add_types(){
  182. /*we need to check whether the new type is in the database
  183. if so, notify and reject,therefore check against db_str */
  184. var y=document.getElementsByName("type");
  185. // blank space validation added by Ravith.
  186. y_val = y[0].value;
  187. y_val.replace(/^\s+|\s+$/g, '');
  188. if(y_val==null || y_val=="" || trim(y_val).length == 0){
  189. alert("<?php echo _("Whitespace is not a valid input."); ?>");
  190. return;
  191. }
  192. else if(check_for_special_char(y_val)){
  193. alert("Containts special characters. \n These are not allowed.\n Please remove them and try again.");
  194. return;
  195. }
  196. // end of blank space validation
  197. var exist=search(db_str,y[0].value);
  198. if(exist){
  199. alert("<?php echo _("The type already exists in the list"); ?>");
  200. return;
  201. }
  202. /*Now we need to check whether we added it during this session
  203. if so, notify and reject,therefore check against added */
  204. var add=document.getElementsByName("added");
  205. if(add[0].value!=0){
  206. exist=search(add[0].value,y[0].value);
  207. if(exist){
  208. alert("<?php echo _("The Type Exists,you just added it") ?>");
  209. return;
  210. }
  211. }
  212. var x=document.getElementsByName("<?php echo $name?>");
  213. opt = document.createElement("option") ;
  214. opt.text = y[0].value ;
  215. opt.value = y[0].value ;
  216. var k=x[0].options.length;
  217. x[0].options[k]=opt;
  218. if(sort==true){
  219. sortSelect(x[0], true) ;
  220. }
  221. add[0].value=add[0].value+y[0].value+",";
  222. y[0].value=null;
  223. }
  224. function remove_types(){
  225. var x=document.getElementsByName("<?php echo $name?>");
  226. removeSelectedOptions(x[0]);
  227. if(sort==true){
  228. sortSelect(x[0], true) ;
  229. }
  230. }
  231. function hasOptions(obj) {
  232. if (obj!=null && obj.options!=null) { return true; }
  233. return false;
  234. }
  235. function removeSelectedOptions(from) {
  236. if (!hasOptions(from)) { return; }
  237. if (from.type=="select-one") {
  238. from.options[from.selectedIndex] = null;
  239. }
  240. else {
  241. var remove=document.getElementsByName("removed");
  242. z=document.getElementsByName("added");
  243. var add=new String(z[0].value);
  244. for (var i=(from.options.length-1); i>=0; i--) {
  245. var o=from.options[i];
  246. if (o.selected) {
  247. exist=search(db_str,o.text);
  248. if(exist){
  249. remove[0].value= remove[0].value+","+o.value;
  250. // remove the removed value from db_str
  251. var vals = db_str.split(",");
  252. db_str="";
  253. for(n=0;n<vals.length;n++){
  254. if(vals[n].length>0){
  255. // not a blank string
  256. if(vals[n].toLowerCase() != o.text.toLowerCase()){
  257. db_str += (vals[n]+",");
  258. }
  259. }
  260. }
  261. }else{
  262. z[0].value=search_and_replace(add,o.text);
  263. }
  264. from.options[i] = null;
  265. }
  266. }
  267. }
  268. from.selectedIndex = -1;
  269. }
  270. function search(str_arr,value){
  271. value=value+",";
  272. if (window.RegExp) {
  273. var re = new RegExp(value);
  274. if (re.test(str_arr)) {
  275. return true;
  276. }
  277. }
  278. return false;
  279. }
  280. function search_and_replace(str_obj,value){
  281. value=value+",";
  282. if (window.RegExp) {
  283. var re = new RegExp(value);
  284. str_obj=str_obj.replace(re,"");
  285. }
  286. return new String(str_obj);
  287. }
  288. </script>
  289. <?php
  290. }
  291. function shn_action_change_javascript($form,$change)
  292. {
  293. ?>
  294. <script type="text/javascript">
  295. function change_action(action){
  296. var x=document.getElementsByName("<?php echo $change?>");
  297. x[0].value=action;
  298. document.<?php echo $form?>.submit();
  299. return;
  300. }
  301. </script>
  302. <?php
  303. }
  304. function shn_page_reload($mod = null, $act = null)
  305. {
  306. ?>
  307. <script type="text/javascript">
  308. var surl = unescape(window.location.pathname);
  309. var mod = '<?php echo $mod; ?>';
  310. var act = '<?php echo $act; ?>';
  311. var url = '';
  312. if(mod != null && act != null){
  313. url = surl + '?mod=' + mod + '&act=' + act;
  314. }
  315. else{
  316. url = surl;
  317. }
  318. window.location.replace(url);
  319. </script>
  320. <?php
  321. }
  322. ?>