PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mds/web/modules/network/network/dnsrecords/loc.php

https://github.com/vmasilva/mmc
PHP | 600 lines | 474 code | 117 blank | 9 comment | 48 complexity | fcc41299b659ebeb74b336bc143d01ec MD5 | raw file
Possible License(s): GPL-2.0
  1. <?
  2. class locRecord extends RecordBase{
  3. function locRecord($config = array()){
  4. $this->recordBase($config);
  5. $this->values["latitude"]["deg"]="";
  6. $this->values["latitude"]["min"]="";
  7. $this->values["latitude"]["sec"]="";
  8. $this->values["latitude"]["dir"]="N";
  9. $this->values["longitude"]["deg"]="";
  10. $this->values["longitude"]["min"]="";
  11. $this->values["longitude"]["sec"]="";
  12. $this->values["longitude"]["dir"]="W";
  13. $this->values["altitude"]["value"]="";
  14. $this->values["altitude"]["unit"]="m";
  15. $this->values["size"]["base"]="";
  16. $this->values["size"]["deg"]="";
  17. $this->values["size"]["unit"]="m";
  18. $this->values["hprecision"]["value"]="";
  19. $this->values["hprecision"]["unit"]="m";
  20. $this->values["vprecision"]["value"]="";
  21. $this->values["vprecision"]["unit"]="m";
  22. }
  23. function check($zone = ""){
  24. if (strpos($this->hostname,"..") !== false)
  25. return _T("Host name is incrorrect");
  26. return "";
  27. }
  28. function initValuesFromArray($arr){
  29. $this->hostname = $arr[$this->pn("hostname")];
  30. $this->values["latitude"] = GlobalLocationTpl::arrayValue($arr[$this->pn("latitude")]);
  31. $this->values["longitude"] = GlobalLocationTpl::arrayValue($arr[$this->pn("longitude")]);
  32. $this->values["altitude"] = MeasureInputTpl::arrayValue($arr[$this->pn("altitude")]);
  33. $this->values["size"] = SizeMeasureInputTpl::arrayValue($arr[$this->pn("size")]);
  34. $this->values["hprecision"] = MeasureInputTpl::arrayValue($arr[$this->pn("hprecision")]);
  35. $this->values["vprecision"] = MeasureInputTpl::arrayValue($arr[$this->pn("vprecision")]);
  36. }
  37. function initValuesFromString($str){
  38. $parser = new LocDataParser();
  39. if ($parser->parse($str)==-1){
  40. $this->markError(RecordError::PARSE, $str);
  41. return;
  42. }
  43. $this->values = $parser->result();
  44. }
  45. function createUiContainers($editMode = false){
  46. $units = array("cm"=>_T("cm"), "m"=>_T("m"));
  47. $altRegExp = "/^((-([0-9]{0,5}(\.[0-9]{0,2})?|100000))|(([0-3]?[0-9]{0,7}|4[01][0-9]{0,6})(\.[0-9]{0,2})?|420{6}))$/";
  48. $precRegExp = "/^([0-8][0-9]{0,7}(\.[0-9]{0,2})?|90{7})$/";
  49. $t = new Table();
  50. $t->add($this->_createNameElement(_T("Host name")),
  51. array("value" => $this->hostname,/* "extra" => "." . $this->zone,*/ "required" => True));
  52. $t->add(new TrFormElement(_T("Latitude"), new GlobalLocationTpl($this->pn("latitude"),GlobalLocationTpl::LATITUDE)),
  53. array("value"=>$this->values["latitude"], "required"=>true));
  54. $t->add(new TrFormElement(_T("Longitude"), new GlobalLocationTpl($this->pn("longitude"),GlobalLocationTpl::LONGITUDE)),
  55. array("value"=>$this->values["longitude"], "required"=>true));
  56. $t->add(new TrFormElement(_T("Altitude"), new MeasureInputTpl($this->pn("altitude"),$units,9,$altRegExp)),
  57. array("value"=>$this->values["altitude"]));
  58. $t->add(new TrFormElement(_T("Diameter of a sphere enclosing the described entity"), new SizeMeasureInputTpl($this->pn("size"),$units)),
  59. array("value"=>$this->values["size"]));
  60. $t->add(new TrFormElement(_T("Horizontal precision"), new MeasureInputTpl($this->pn("hprecision"),$units,8, $precRegExp)),
  61. array("value"=>$this->values["hprecision"]));
  62. $t->add(new TrFormElement(_T("Vertical precision"), new MeasureInputTpl($this->pn("vprecision"),$units,8, $precRegExp)),
  63. array("value"=>$this->values["vprecision"]));
  64. return array($this->stackedUi($t));
  65. }
  66. function valuesToString(){
  67. $values = array();
  68. foreach (array("latitude","longitude") as $loc){
  69. $values[] = intval($this->values[$loc]["deg"]);
  70. if (intval($this->values[$loc]["sec"])){
  71. $values[] = intval($this->values[$loc]["min"]);
  72. $values[] = intval($this->values[$loc]["sec"]);
  73. } else {
  74. if (intval($this->values[$loc]["min"])){
  75. $values[] = intval($this->values[$loc]["min"]);
  76. }
  77. }
  78. $values[] = $this->values[$loc]["dir"];
  79. }
  80. $values[] = $this->values["altitude"]["value"] . (($this->values["altitude"]["unit"] == "m") ? "m":"");
  81. $size = strval(intval($this->values["size"]["base"])) . strval(intval($this->values["size"]["deg"])) .
  82. (($this->values["size"]["unit"] == "m") ? "m":"");
  83. $hprecision = strval(floatval($this->values["hprecision"]["value"])) . (($this->values["hprecision"]["unit"] == "m") ? "m":"");
  84. $vprecision = strval(floatval($this->values["vprecision"]["value"])) . (($this->values["vprecision"]["unit"] == "m") ? "m":"");
  85. if (floatval($this->values["vprecision"]["value"])){
  86. $values[] = $size;
  87. $values[] = $hprecision;
  88. $values[] = $vprecision;
  89. } else
  90. if (floatval($this->values["hprecision"]["value"])){
  91. $values[] = $size;
  92. $values[] = $hprecision;
  93. }
  94. else
  95. if (intval($this->values["size"]["base"]) || intval($this->values["size"]["deg"])){
  96. $values[] = $size;
  97. }
  98. return $this->_stringByValues($values);
  99. }
  100. function measureDescription($name){
  101. if (isset($this->values[$name]["value"]) && ($this->values[$name]["value"]!=="0"))
  102. return sprintf("%s %s",$this->values[$name]["value"], ($this->values[$name]["unit"] === "cm") ? _T("cm") : _T("m"));
  103. }
  104. function coordinateDescription($name){
  105. $directions = GlobalLocationTpl::directions(GlobalLocationTpl::typeByString($name));
  106. $result = intval($this->values[$name]["deg"]) . "°";
  107. if (intval($this->values[$name]["min"]))// && ($this->values[$name]["min"]!=="0"))
  108. $result .= " " . intval($this->values[$name]["min"]) . "′";
  109. if (intval($this->values[$name]["sec"]))
  110. $result .= " " . intval($this->values[$name]["sec"]) . "′′";
  111. $result .= " " . $directions[$this->values[$name]["dir"]];
  112. return $result;
  113. }
  114. function sizeDescription(){
  115. $val = intval($this->values["size"]["base"]) * pow(10, intval($this->values["size"]["deg"]));
  116. return sprintf("%s %s",$val, ($this->values["size"]["unit"] === "cm") ? _T("cm") : _T("m"));
  117. }
  118. function valuesToDescription(){
  119. $descrMap = array(
  120. _T("Latitude") => $this->coordinateDescription("latitude"),
  121. _T("Longitude") => $this->coordinateDescription("longitude"),
  122. _T("Altitude") => $this->measureDescription("altitude")
  123. );
  124. if (intval($this->values["size"]["base"]))
  125. $descrMap[_T("Diameter of a sphere enclosing the described entity")] = $this->sizeDescription();
  126. if (intval($this->values["hprecision"]["value"]))
  127. $descrMap[_T("Horizontal precision")] = $this->measureDescription("hprecision");
  128. if (intval($this->values["vprecision"]["value"]))
  129. $descrMap[_T("Vertical precision")] = $this->measureDescription("vprecision");
  130. return $this->_descriptionByValues($descrMap);
  131. }
  132. }
  133. class LocDataParser{
  134. var $values = array();
  135. function LocDataParser(){
  136. $this->values["latitude"]["deg"]="";
  137. $this->values["latitude"]["min"]="";
  138. $this->values["latitude"]["sec"]="";
  139. $this->values["latitude"]["dir"]="N";
  140. $this->values["longitude"]["deg"]="";
  141. $this->values["longitude"]["min"]="";
  142. $this->values["longitude"]["sec"]="";
  143. $this->values["longitude"]["dir"]="W";
  144. $this->values["altitude"]["value"]="";
  145. $this->values["altitude"]["unit"]="m";
  146. $this->values["size"]["base"]="";
  147. $this->values["size"]["deg"]="";
  148. $this->values["size"]["unit"]="m";
  149. $this->values["hprecision"]["value"]="";
  150. $this->values["hprecision"]["unit"]="m";
  151. $this->values["vprecision"]["value"]="";
  152. $this->values["vprecision"]["unit"]="m";
  153. }
  154. function parse($data){
  155. $values = split(" ", $data);
  156. $pos = $this->parseCoordinate("latitude", $values, 0);
  157. if ($pos == -1)
  158. return -1;
  159. $pos = $this->parseCoordinate("longitude", $values, $pos+1);
  160. if ($pos == -1)
  161. return -1;
  162. if (!$this->parseMeasure("altitude",$values[$pos + 1]))
  163. return -1;
  164. //extra parameters of record. can be ignored.
  165. if ($pos + 2 < count($values))
  166. $this->parseSize($values[$pos + 2]);
  167. if ($pos + 3 < count($values))
  168. $this->parseMeasure("hprecision",$values[$pos + 3]);
  169. if ($pos + 4 < count($values))
  170. $this->parseMeasure("vprecision",$values[$pos + 4]);
  171. return 0;
  172. }
  173. function result(){
  174. return $this->values;
  175. }
  176. function parseCoordinate($coord, $arr, $pos){
  177. $measures = array("deg","min","sec","dir");
  178. $curpos = $pos;
  179. $arrLength = count($arr);
  180. foreach ($measures as $m){
  181. //element index out of array
  182. if ($curpos + 1 > $arrLength)
  183. return -1;
  184. if (preg_match("/[NSWE]/i",$arr[$curpos])){
  185. $this->values[$coord]["dir"] = strtoupper($arr[$curpos]);
  186. //direction is the first parameter
  187. if ($pos == $curpos)
  188. return -1;
  189. return $curpos;
  190. }
  191. $this->values[$coord][$m] = $arr[$curpos];
  192. $curpos++;
  193. }
  194. //direction could not be found
  195. return -1;
  196. }
  197. function parseMeasure($name, $value){
  198. if (!isset($value)) return false;
  199. $unit = substr($value,-1,1);
  200. if ($unit === "m" || $unit === "M"){
  201. $this->values[$name]["unit"] = "m";
  202. $this->values[$name]["value"] = substr($value,0,-1);
  203. return true;
  204. }
  205. $this->values[$name]["unit"] = "cm";
  206. $this->values[$name]["value"] = $value;
  207. return true;
  208. }
  209. function parseSize($value){
  210. $this->values["size"]["base"] = substr($value,0,1);
  211. if (strlen($value) == 1){
  212. $this->values["size"]["unit"] = "cm";
  213. return;
  214. }
  215. if (strlen($value) == 2){
  216. $tmp = substr($value,1,1);
  217. if ($tmp === "m" || $tmp === "M")
  218. $this->values["size"]["unit"] = "m";
  219. else {
  220. $this->values["size"]["unit"] = "cm";
  221. $this->values["size"]["deg"] = $tmp;
  222. }
  223. return;
  224. }
  225. $this->values["size"]["deg"] = substr($value,1,1);
  226. $this->values["size"]["unit"] = (strlen($value) == 3) ? "m" : "cm";
  227. }
  228. }
  229. class GlobalLocationTpl extends InputTpl{
  230. var $type;
  231. const LATITUDE = true;
  232. const LONGITUDE = false;
  233. function GlobalLocationTpl($name, $type) {
  234. $this->name = $name;
  235. $this->type = $type;
  236. }
  237. function display($arrParam) {
  238. print '<div id="div'.$this->name.'">';
  239. //print '<table cellspacing="0">';
  240. $i = 0;
  241. $degRegExp = ($this->type == LATITUDE) ? "/^([0-8]?[0-9]|90)$/" : "/^([01]?[0-7]?[0-9]|[89]?[0-9]|180)$/";
  242. $minRegExp = "/^([0-5]?[0-9])$/";
  243. $secRegExp = "/^([0-5]?[0-9](\.[0-9]?[0-9]?[0-9])?)$/";
  244. foreach (array(
  245. 'deg'=>array(_T('Degr.: '), 3, $degRegExp),
  246. 'min'=>array(_T('Min.: '), 2, $minRegExp),
  247. 'sec'=>array(_T('Sec.: '), 5, $secRegExp)
  248. ) as $elem=>$a_params) {
  249. $e = new InputTpl($this->name.'_'.$elem, $a_params[2]); //, array('value'=>$arrParam[$elem]));
  250. $e->setSize($a_params[1]);
  251. print $a_params[0];
  252. $e->display(array('value'=>intval($arrParam["value"][$elem]) > 0 ? intval($arrParam["value"][$elem]) : "", 'onchange'=>'
  253. var elem = document.getElementById("'.$this->name.'");
  254. var loc = elem.value;
  255. var part = '.$i.';
  256. var value = document.getElementById("'.$this->name.'_'.$elem.'").value;
  257. var newloc = changeLocPart(loc, part, value);
  258. elem.value = newloc;
  259. '));
  260. $i += 1;
  261. }
  262. $this->displayDirection($arrParam["value"]["dir"]);
  263. print '<input name="'.$this->name.'" id="'.$this->name.'" type="hidden" value="' . $this->stringValue($arrParam["value"]) . '"/>';
  264. print '</div>';
  265. print '<script type="text/javascript">
  266. function changeLocPart(loc, part, value) {
  267. var re = new RegExp("/", "g");
  268. var aloc = loc.split(re);
  269. aloc[part] = value;
  270. return aloc.join("/");
  271. }
  272. </script>';
  273. }
  274. function displayDirection($dir){
  275. $onchange = 'onchange = \'
  276. var elem = document.getElementById("'.$this->name.'");
  277. var loc = elem.value;
  278. var elems = document.getElementsByName("'.$this->name.'_dir");
  279. var len = elems.length;
  280. var value="";
  281. for (i = 0; i <len; i++) {
  282. if (elems[i].checked) {
  283. value = elems[i].value;
  284. break;
  285. }
  286. }
  287. var newloc = changeLocPart(loc, 3, value);
  288. elem.value = newloc;
  289. \'';
  290. foreach ($this->directions($this->type) as $k => $v){
  291. $checked = ($k === $dir) ? "checked" : "";
  292. print '<input type=radio name="' . $this->name . '_dir" value="' . $k . '" '. $checked . ' ' . $onchange .'>' . $v;
  293. }
  294. }
  295. static function directions($type){
  296. return ($type == LATITUDE) ? array('N' => _T('N'),'S' => _T('S')) : array('W' => _T('W'),'E' => _T('E'));
  297. }
  298. static function typeByString($strtype){
  299. return ($strtype === "latitude") ? LATITUDE : LONGITUDE;
  300. }
  301. private function stringValue($arrValue, $format = "%s/%s/%s/%s"){
  302. $deg = $arrValue["deg"]?$arrValue["deg"]:0;
  303. $min = $arrValue["min"]?$arrValue["min"]:0;
  304. $sec = $arrValue["sec"]?$arrValue["sec"]:0;
  305. $dirs = array_keys($this->directions());
  306. $dir = $arrValue["dir"]?$arrValue["dir"]:$dirs[0];
  307. return sprintf($format, $deg, $min, $sec, $dir);
  308. }
  309. static function arrayValue($strValue){
  310. $value = split("/", $strValue);
  311. $keys = array("deg","min","sec","dir");
  312. foreach($keys as $k => $v)
  313. $result[$v]=$value[$k];
  314. return $result;
  315. }
  316. }
  317. class MeasureInputTpl extends InputTpl{
  318. var $units;
  319. function MeasureInputTpl($name, $units=array(), $size=23, $regExp="/.+/"){
  320. $this->name = $name;
  321. $this->units = $units;
  322. $this->size = $size;
  323. $this->regexp = $regExp;
  324. }
  325. function display($arrParam){
  326. print '<div id="div'.$this->name.'">';
  327. //print '<table cellspacing="0">';
  328. $e = new InputTpl($this->name.'_value', $this->regexp); //, array('value'=>$arrParam[$elem]));
  329. $e->setSize($this->size);
  330. $e->display(array('value'=>floatval($arrParam["value"]["value"]) != 0 ? floatval($arrParam["value"]["value"]) : "",
  331. 'onchange'=>'
  332. var elem = document.getElementById("'.$this->name.'");
  333. var data = elem.value;
  334. var value = document.getElementById("'.$this->name .'_value").value;
  335. var newdata = changePart(data,0,value);
  336. elem.value = newdata;'));
  337. $onchange = 'onchange = \'
  338. var elem = document.getElementById("'.$this->name.'");
  339. var data = elem.value;
  340. var elems = document.getElementsByName("'.$this->name.'_unit");
  341. var len = elems.length;
  342. var value="";
  343. for (i = 0; i <len; i++) {
  344. if (elems[i].checked) {
  345. value = elems[i].value;
  346. break;
  347. }
  348. }
  349. var newdata = changePart(data, 1, value);
  350. elem.value = newdata;
  351. \'';
  352. foreach ($this->units as $k => $v){
  353. $checked = ($k === $arrParam["value"]["unit"]) ? "checked" : "";
  354. print '<input type=radio name="' . $this->name . '_unit" value="' . $k . '" '. $checked . ' ' . $onchange .'>' . $v;
  355. }
  356. print '<input name="'.$this->name.'" id="'.$this->name.'" type="hidden" value="' . $this->stringValue($arrParam["value"]) . '"/>';
  357. print '</div>';
  358. print '<script type="text/javascript">
  359. function changePart(data, part, value) {
  360. var re = new RegExp("/", "g");
  361. var adata = data.split(re);
  362. adata[part] = value;
  363. return adata.join("/");
  364. }
  365. </script>';
  366. }
  367. private function stringValue($valueArr, $format = "%s/%s"){
  368. $value = $valueArr["value"]?$valueArr["value"]:0;
  369. $units = array_keys($this->units);
  370. $unit = $valueArr["unit"] ? $valueArr["unit"]:$units[0];
  371. return sprintf($format, $value, $unit);
  372. }
  373. static function arrayValue($strValue){
  374. $value = split("/", $strValue);
  375. $keys = array("value","unit");
  376. foreach($keys as $k => $v)
  377. $result[$v]=$value[$k];
  378. return $result;
  379. }
  380. }
  381. class SizeMeasureInputTpl extends InputTpl{
  382. var $units;
  383. function SizeMeasureInputTpl($name, $units=array()){
  384. $this->name = $name;
  385. $this->units = $units;
  386. }
  387. function display($arrParam){
  388. print '<div id="div'.$this->name.'">';
  389. //print '<table cellspacing="0">';
  390. $re = "/^\d$/";
  391. $i = 0;
  392. foreach (array( 'base'=>'', 'deg'=>''. " * 10 ^ ") as $k => $v){
  393. $e = new CustomInputTpl($this->name."_".$k,$re);
  394. $e->setSize(1);
  395. print $v;
  396. $e->display(array('value'=>intval($arrParam["value"][$k]) > 0 ? intval($arrParam["value"][$k]) : "",
  397. 'onchange'=>'
  398. var elem = document.getElementById("'.$this->name.'");
  399. var data = elem.value;
  400. var part = '.$i.';
  401. var value = document.getElementById("'.$this->name.'_'.$k.'").value;
  402. var newdata = changePart(data, part, value);
  403. elem.value = newdata;',
  404. 'onkeyup'=>'
  405. isInt1 = function( s ) {return !isNaN( parseInt( s )) && s>=0 && s<10 || s==""; }
  406. var base = document.getElementById("'.$this->name.'_base").value;
  407. var deg = document.getElementById("'.$this->name.'_deg").value;
  408. var result = "'._T("incorrect") .'";
  409. if (isInt1(base) && isInt1(deg))
  410. result = base * Math.pow(10,deg);
  411. document.getElementById("' . $this->name.'_result").innerHTML = result;'
  412. ));
  413. $i += 1;
  414. }
  415. print " = ";
  416. //print '<div id="div'.$this->name.'result">';
  417. $base = intval($arrParam["value"]["base"]);
  418. $deg = intval($arrParam["value"]["deg"]);
  419. print '<b id="' . $this->name.'_result">' . strval($base * pow(10, $deg)) . "</b>";
  420. //print '</div>';
  421. $onchange = 'onchange = \'
  422. var elem = document.getElementById("'.$this->name.'");
  423. var data = elem.value;
  424. var elems = document.getElementsByName("'.$this->name.'_unit");
  425. var len = elems.length;
  426. var value="";
  427. for (i = 0; i <len; i++) {
  428. if (elems[i].checked) {
  429. value = elems[i].value;
  430. break;
  431. }
  432. }
  433. var newdata = changePart(data, 2, value);
  434. elem.value = newdata;
  435. \'';
  436. foreach ($this->units as $k => $v){
  437. $checked = ($k === $arrParam["value"]["unit"]) ? "checked" : "";
  438. print '<input type=radio name="' . $this->name . '_unit" value="' . $k . '" '. $checked . ' ' . $onchange .'>' . $v;
  439. }
  440. print '<input name="'.$this->name.'" id="'.$this->name.'" type="hidden" value="' . $this->stringValue($arrParam["value"]) . '"/>';
  441. print '</div>';
  442. print '<script type="text/javascript">
  443. function changePart(data, part, value) {
  444. var re = new RegExp("/", "g");
  445. var adata = data.split(re);
  446. adata[part] = value;
  447. return adata.join("/");
  448. }
  449. </script>';
  450. }
  451. private function stringValue($valueArr, $format = "%s/%s/%s"){
  452. $value = $valueArr["base"]?$valueArr["base"]:0;
  453. $deg = $valueArr["deg"]?$valueArr["deg"]:0;
  454. $units = array_keys($this->units);
  455. $unit = $valueArr["unit"] ? $valueArr["unit"]:$units[0];
  456. return sprintf($format, $value, $deg, $unit);
  457. }
  458. static function arrayValue($strValue){
  459. $value = split("/", $strValue);
  460. $keys = array("base","deg","unit");
  461. foreach($keys as $k => $v)
  462. $result[$v]=$value[$k];
  463. return $result;
  464. }
  465. }
  466. class CustomInputTpl extends InputTpl{
  467. function CustomInputTpl($name, $re){
  468. $this->InputTpl($name,$re);
  469. }
  470. function display($arrParam){
  471. parent::display($arrParam);
  472. print '<script type="text/javascript">';
  473. if (isset($arrParam["onkeyup"])) {
  474. print '$(\''.$this->name.'\').onkeyup = function() {' . $arrParam["onkeyup"] . '};';
  475. }
  476. print '</script>';
  477. }
  478. }
  479. ?>